Rollup merge of #62150 - alex:mem-uninit-refactor, r=RalfJung

Implement mem::{zeroed,uninitialized} in terms of MaybeUninit.

Refs #62061

r? @oli-obk
This commit is contained in:
Mazdak Farrokhzad 2019-07-05 13:53:00 +02:00 committed by GitHub
commit 1d45156866
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 12 additions and 29 deletions

View file

@ -700,17 +700,15 @@ extern "rust-intrinsic" {
/// which is unsafe unless `T` is `Copy`. Also, even if T is
/// `Copy`, an all-zero value may not correspond to any legitimate
/// state for the type in question.
#[unstable(feature = "core_intrinsics",
reason = "intrinsics are unlikely to ever be stabilized, instead \
they should be used through stabilized interfaces \
in the rest of the standard library",
issue = "0")]
#[rustc_deprecated(reason = "no longer used by rustc, will be removed - use MaybeUnint instead",
since = "1.38.0")]
pub fn init<T>() -> T;
/// Creates an uninitialized value.
///
/// `uninit` is unsafe because there is no guarantee of what its
/// contents are. In particular its drop-flag may be set to any
/// state, which means it may claim either dropped or
/// undropped. In the general case one must use `ptr::write` to
/// initialize memory previous set to the result of `uninit`.
pub fn uninit<T>() -> T;
/// Moves a value out of scope without running drop glue.
pub fn forget<T: ?Sized>(_: T);

View file

@ -450,8 +450,7 @@ pub const fn needs_drop<T>() -> bool {
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub unsafe fn zeroed<T>() -> T {
intrinsics::panic_if_uninhabited::<T>();
intrinsics::init()
MaybeUninit::zeroed().assume_init()
}
/// Bypasses Rust's normal memory-initialization checks by pretending to
@ -476,8 +475,7 @@ pub unsafe fn zeroed<T>() -> T {
#[rustc_deprecated(since = "1.38.0", reason = "use `mem::MaybeUninit` instead")]
#[stable(feature = "rust1", since = "1.0.0")]
pub unsafe fn uninitialized<T>() -> T {
intrinsics::panic_if_uninhabited::<T>();
intrinsics::uninit()
MaybeUninit::uninit().assume_init()
}
/// Swaps the values at two mutable locations, without deinitializing either one.

View file

@ -234,7 +234,7 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
return;
}
// Effectively no-ops
"uninit" | "forget" => {
"forget" => {
return;
}
"needs_drop" => {

View file

@ -145,7 +145,6 @@ pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem) {
"rustc_peek" => (1, vec![param(0)], param(0)),
"panic_if_uninhabited" => (1, Vec::new(), tcx.mk_unit()),
"init" => (1, Vec::new(), param(0)),
"uninit" => (1, Vec::new(), param(0)),
"forget" => (1, vec![param(0)], tcx.mk_unit()),
"transmute" => (2, vec![ param(0) ], param(1)),
"move_val_init" => {

View file

@ -1,13 +0,0 @@
// run-pass
// pretty-expanded FIXME #23616
#![feature(intrinsics)]
mod rusti {
extern "rust-intrinsic" {
pub fn uninit<T>() -> T;
}
}
pub fn main() {
let _a : isize = unsafe {rusti::uninit()};
}

View file

@ -1,3 +1,4 @@
#![allow(deprecated)]
#![feature(core_intrinsics)]
use std::intrinsics::{init};

View file

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