Skip to content

Commit e4f250e

Browse files
committed
Implement mem::{zeroed,uninitialized} in terms of MaybeUninit.
Refs #62061
1 parent b43eb42 commit e4f250e

File tree

7 files changed

+12
-29
lines changed

7 files changed

+12
-29
lines changed

src/libcore/intrinsics.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -700,17 +700,15 @@ extern "rust-intrinsic" {
700700
/// which is unsafe unless `T` is `Copy`. Also, even if T is
701701
/// `Copy`, an all-zero value may not correspond to any legitimate
702702
/// state for the type in question.
703+
#[unstable(feature = "core_intrinsics",
704+
reason = "intrinsics are unlikely to ever be stabilized, instead \
705+
they should be used through stabilized interfaces \
706+
in the rest of the standard library",
707+
issue = "0")]
708+
#[rustc_deprecated(reason = "no longer used by rustc, will be removed - use MaybeUnint instead",
709+
since = "1.38.0")]
703710
pub fn init<T>() -> T;
704711

705-
/// Creates an uninitialized value.
706-
///
707-
/// `uninit` is unsafe because there is no guarantee of what its
708-
/// contents are. In particular its drop-flag may be set to any
709-
/// state, which means it may claim either dropped or
710-
/// undropped. In the general case one must use `ptr::write` to
711-
/// initialize memory previous set to the result of `uninit`.
712-
pub fn uninit<T>() -> T;
713-
714712
/// Moves a value out of scope without running drop glue.
715713
pub fn forget<T: ?Sized>(_: T);
716714

src/libcore/mem/mod.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -450,8 +450,7 @@ pub const fn needs_drop<T>() -> bool {
450450
#[inline]
451451
#[stable(feature = "rust1", since = "1.0.0")]
452452
pub unsafe fn zeroed<T>() -> T {
453-
intrinsics::panic_if_uninhabited::<T>();
454-
intrinsics::init()
453+
MaybeUninit::zeroed().assume_init()
455454
}
456455

457456
/// Bypasses Rust's normal memory-initialization checks by pretending to
@@ -476,8 +475,7 @@ pub unsafe fn zeroed<T>() -> T {
476475
#[rustc_deprecated(since = "1.38.0", reason = "use `mem::MaybeUninit` instead")]
477476
#[stable(feature = "rust1", since = "1.0.0")]
478477
pub unsafe fn uninitialized<T>() -> T {
479-
intrinsics::panic_if_uninhabited::<T>();
480-
intrinsics::uninit()
478+
MaybeUninit::uninit().assume_init()
481479
}
482480

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

src/librustc_codegen_llvm/intrinsic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
234234
return;
235235
}
236236
// Effectively no-ops
237-
"uninit" | "forget" => {
237+
"forget" => {
238238
return;
239239
}
240240
"needs_drop" => {

src/librustc_typeck/check/intrinsic.rs

-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem) {
145145
"rustc_peek" => (1, vec![param(0)], param(0)),
146146
"panic_if_uninhabited" => (1, Vec::new(), tcx.mk_unit()),
147147
"init" => (1, Vec::new(), param(0)),
148-
"uninit" => (1, Vec::new(), param(0)),
149148
"forget" => (1, vec![param(0)], tcx.mk_unit()),
150149
"transmute" => (2, vec![ param(0) ], param(1)),
151150
"move_val_init" => {

src/test/run-pass/intrinsics/intrinsic-uninit.rs

-13
This file was deleted.

src/test/ui/init-unsafe.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![allow(deprecated)]
12
#![feature(core_intrinsics)]
23

34
use std::intrinsics::{init};

src/test/ui/init-unsafe.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0133]: call to unsafe function is unsafe and requires unsafe function or block
2-
--> $DIR/init-unsafe.rs:7:17
2+
--> $DIR/init-unsafe.rs:8:17
33
|
44
LL | let stuff = init::<isize>();
55
| ^^^^^^^^^^^^^^^ call to unsafe function

0 commit comments

Comments
 (0)