Skip to content

Commit d49306d

Browse files
committed
implement zeroed and uninitialized with MaybeUninit
1 parent 303d8af commit d49306d

File tree

9 files changed

+14
-110
lines changed

9 files changed

+14
-110
lines changed

src/libcore/intrinsics.rs

+1-39
Original file line numberDiff line numberDiff line change
@@ -1021,46 +1021,8 @@ extern "rust-intrinsic" {
10211021
#[rustc_const_unstable(feature = "const_caller_location", issue = "47809")]
10221022
pub fn caller_location() -> &'static crate::panic::Location<'static>;
10231023

1024-
/// Creates a value initialized to zero.
1025-
///
1026-
/// `init` is unsafe because it returns a zeroed-out datum,
1027-
/// which is unsafe unless `T` is `Copy`. Also, even if T is
1028-
/// `Copy`, an all-zero value may not correspond to any legitimate
1029-
/// state for the type in question.
1030-
///
1031-
/// The stabilized version of this intrinsic is
1032-
/// [`std::mem::zeroed`](../../std/mem/fn.zeroed.html).
1033-
#[unstable(
1034-
feature = "core_intrinsics",
1035-
reason = "intrinsics are unlikely to ever be stabilized, instead \
1036-
they should be used through stabilized interfaces \
1037-
in the rest of the standard library",
1038-
issue = "none"
1039-
)]
1040-
#[rustc_deprecated(reason = "superseded by MaybeUninit, removal planned", since = "1.38.0")]
1041-
pub fn init<T>() -> T;
1042-
1043-
/// Creates an uninitialized value.
1044-
///
1045-
/// `uninit` is unsafe because there is no guarantee of what its
1046-
/// contents are. In particular its drop-flag may be set to any
1047-
/// state, which means it may claim either dropped or
1048-
/// undropped. In the general case one must use `ptr::write` to
1049-
/// initialize memory previous set to the result of `uninit`.
1050-
///
1051-
/// The stabilized version of this intrinsic is
1052-
/// [`std::mem::MaybeUninit`](../../std/mem/union.MaybeUninit.html).
1053-
#[unstable(
1054-
feature = "core_intrinsics",
1055-
reason = "intrinsics are unlikely to ever be stabilized, instead \
1056-
they should be used through stabilized interfaces \
1057-
in the rest of the standard library",
1058-
issue = "none"
1059-
)]
1060-
#[rustc_deprecated(reason = "superseded by MaybeUninit, removal planned", since = "1.38.0")]
1061-
pub fn uninit<T>() -> T;
1062-
10631024
/// Moves a value out of scope without running drop glue.
1025+
/// This exists solely for `mem::forget_unsized`; normal `forget` uses `ManuallyDrop` instead.
10641026
pub fn forget<T: ?Sized>(_: T);
10651027

10661028
/// Reinterprets the bits of a value of one type as another type.

src/libcore/mem/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ pub unsafe fn zeroed<T>() -> T {
500500
intrinsics::panic_if_zero_invalid::<T>();
501501
#[cfg(bootstrap)]
502502
intrinsics::panic_if_uninhabited::<T>();
503-
intrinsics::init()
503+
MaybeUninit::zeroed().assume_init()
504504
}
505505

506506
/// Bypasses Rust's normal memory-initialization checks by pretending to
@@ -536,7 +536,7 @@ pub unsafe fn uninitialized<T>() -> T {
536536
intrinsics::panic_if_any_invalid::<T>();
537537
#[cfg(bootstrap)]
538538
intrinsics::panic_if_uninhabited::<T>();
539-
intrinsics::uninit()
539+
MaybeUninit::uninit().assume_init()
540540
}
541541

542542
/// Swaps the values at two mutable locations, without deinitializing either one.

src/librustc_codegen_llvm/intrinsic.rs

+2-20
Original file line numberDiff line numberDiff line change
@@ -195,26 +195,8 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
195195
.unwrap();
196196
OperandRef::from_const(self, ty_name, ret_ty).immediate_or_packed_pair(self)
197197
}
198-
"init" => {
199-
let ty = substs.type_at(0);
200-
if !self.layout_of(ty).is_zst() {
201-
// Just zero out the stack slot.
202-
// If we store a zero constant, LLVM will drown in vreg allocation for large
203-
// data structures, and the generated code will be awful. (A telltale sign of
204-
// this is large quantities of `mov [byte ptr foo],0` in the generated code.)
205-
memset_intrinsic(
206-
self,
207-
false,
208-
ty,
209-
llresult,
210-
self.const_u8(0),
211-
self.const_usize(1),
212-
);
213-
}
214-
return;
215-
}
216-
// Effectively no-ops
217-
"uninit" | "forget" => {
198+
// Effectively no-op
199+
"forget" => {
218200
return;
219201
}
220202
"offset" => {

src/librustc_typeck/check/intrinsic.rs

-2
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,6 @@ pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) {
150150
"panic_if_uninhabited" | "panic_if_zero_invalid" | "panic_if_any_invalid" => {
151151
(1, Vec::new(), tcx.mk_unit())
152152
}
153-
"init" => (1, Vec::new(), param(0)),
154-
"uninit" => (1, Vec::new(), param(0)),
155153
"forget" => (1, vec![param(0)], tcx.mk_unit()),
156154
"transmute" => (2, vec![param(0)], param(1)),
157155
"move_val_init" => (1, vec![tcx.mk_mut_ptr(param(0)), param(0)], tcx.mk_unit()),

src/test/ui/init-large-type.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,13 @@
1010

1111
#![feature(intrinsics)]
1212

13-
use std::thread;
14-
15-
extern "rust-intrinsic" {
16-
pub fn init<T>() -> T;
17-
}
13+
use std::{mem, thread};
1814

1915
const SIZE: usize = 1024 * 1024;
2016

2117
fn main() {
2218
// do the test in a new thread to avoid (spurious?) stack overflows
2319
thread::spawn(|| {
24-
let _memory: [u8; SIZE] = unsafe { init() };
20+
let _memory: [u8; SIZE] = unsafe { mem::zeroed() };
2521
}).join();
2622
}

src/test/ui/init-unsafe.rs

-9
This file was deleted.

src/test/ui/init-unsafe.stderr

-11
This file was deleted.

src/test/ui/intrinsics/intrinsic-move-val.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
mod rusti {
77
extern "rust-intrinsic" {
8-
pub fn init<T>() -> T;
98
pub fn move_val_init<T>(dst: *mut T, src: T);
109
}
1110
}
@@ -15,17 +14,17 @@ pub fn main() {
1514
// sanity check
1615
check_drops_state(0, None);
1716

18-
let mut x: Box<D> = box D(1);
19-
assert_eq!(x.0, 1);
17+
let mut x: Option<Box<D>> = Some(box D(1));
18+
assert_eq!(x.as_ref().unwrap().0, 1);
2019

2120
// A normal overwrite, to demonstrate `check_drops_state`.
22-
x = box D(2);
21+
x = Some(box D(2));
2322

2423
// At this point, one destructor has run, because the
2524
// overwrite of `x` drops its initial value.
2625
check_drops_state(1, Some(1));
2726

28-
let mut y: Box<D> = rusti::init();
27+
let mut y: Option<Box<D>> = std::mem::zeroed();
2928

3029
// An initial binding does not overwrite anything.
3130
check_drops_state(1, Some(1));
@@ -51,9 +50,9 @@ pub fn main() {
5150
// during such a destructor call. We do so after the end of
5251
// this scope.
5352

54-
assert_eq!(y.0, 2);
55-
y.0 = 3;
56-
assert_eq!(y.0, 3);
53+
assert_eq!(y.as_ref().unwrap().0, 2);
54+
y.as_mut().unwrap().0 = 3;
55+
assert_eq!(y.as_ref().unwrap().0, 3);
5756

5857
check_drops_state(1, Some(1));
5958
}

src/test/ui/intrinsics/intrinsic-uninit.rs

-13
This file was deleted.

0 commit comments

Comments
 (0)