From 7bb5b3eb3228df648a08b02c85eddcd9b9cc85bd Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Sun, 19 Aug 2018 15:51:35 +0200 Subject: [PATCH 01/19] add MaybeUninit and deprecate mem::{uninitialized,zeroed} --- src/libcore/mem.rs | 95 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/src/libcore/mem.rs b/src/libcore/mem.rs index 1803adee3c18e..91522885b598b 100644 --- a/src/libcore/mem.rs +++ b/src/libcore/mem.rs @@ -514,6 +514,7 @@ pub fn needs_drop() -> bool { /// assert_eq!(0, x); /// ``` #[inline] +#[rustc_deprecated(since = "1.30.0", reason = "use `mem::MaybeUninit::zeroed` instead")] #[stable(feature = "rust1", since = "1.0.0")] pub unsafe fn zeroed() -> T { intrinsics::init() @@ -608,6 +609,7 @@ pub unsafe fn zeroed() -> T { /// [copy_no]: ../intrinsics/fn.copy_nonoverlapping.html /// [`Drop`]: ../ops/trait.Drop.html #[inline] +#[rustc_deprecated(since = "1.30.0", reason = "use `mem::MaybeUninit::uninitialized` instead")] #[stable(feature = "rust1", since = "1.0.0")] pub unsafe fn uninitialized() -> T { intrinsics::uninit() @@ -1024,3 +1026,96 @@ impl DerefMut for ManuallyDrop { &mut self.value } } + +/// A newtype to construct uninitialized instances of `T` +#[allow(missing_debug_implementations)] +#[unstable(feature = "maybe_uninit", issue = "53491")] +pub union MaybeUninit { + uninit: (), + value: ManuallyDrop, +} + +impl MaybeUninit { + /// Create a new `MaybeUninit` in an uninitialized state. + /// + /// Note that dropping a `MaybeUninit` will never call `T`'s drop code. + /// It is your responsibility to make sure `T` gets dropped if it got initialized. + #[unstable(feature = "maybe_uninit", issue = "53491")] + pub const fn uninitialized() -> MaybeUninit { + MaybeUninit { uninit: () } + } + + /// Create a new `MaybeUninit` in an uninitialized state, with the memory being + /// filled with `0` bytes. It depends on `T` whether that already makes for + /// proper initialization. For example, `MaybeUninit::zeroed()` is initialized, + /// but `MaybeUninit<&'static i32>::zeroed()` is not because references must not + /// be null. + /// + /// Note that dropping a `MaybeUninit` will never call `T`'s drop code. + /// It is your responsibility to make sure `T` gets dropped if it got initialized. + #[unstable(feature = "maybe_uninit", issue = "53491")] + pub fn zeroed() -> MaybeUninit { + let mut u = MaybeUninit::::uninitialized(); + unsafe { + u.as_mut_ptr().write_bytes(0u8, 1); + } + u + } + + /// Set the value of the `MaybeUninit`. This overwrites any previous value without dropping it. + #[unstable(feature = "maybe_uninit", issue = "53491")] + pub fn set(&mut self, val: T) { + unsafe { + self.value = ManuallyDrop::new(val); + } + } + + /// Extract the value from the `MaybeUninit` container. This is a great way + /// to ensure that the data will get dropped, because the resulting `T` is + /// subject to the usual drop handling. + /// + /// # Unsafety + /// + /// It is up to the caller to guarantee that the the `MaybeUninit` really is in an initialized + /// state, otherwise this will immediately cause undefined behavior. + #[unstable(feature = "maybe_uninit", issue = "53491")] + pub unsafe fn into_inner(self) -> T { + ManuallyDrop::into_inner(self.value) + } + + /// Get a reference to the contained value. + /// + /// # Unsafety + /// + /// It is up to the caller to guarantee that the the `MaybeUninit` really is in an initialized + /// state, otherwise this will immediately cause undefined behavior. + #[unstable(feature = "maybe_uninit", issue = "53491")] + pub unsafe fn get_ref(&self) -> &T { + &*self.value + } + + /// Get a mutable reference to the contained value. + /// + /// # Unsafety + /// + /// It is up to the caller to guarantee that the the `MaybeUninit` really is in an initialized + /// state, otherwise this will immediately cause undefined behavior. + #[unstable(feature = "maybe_uninit", issue = "53491")] + pub unsafe fn get_mut(&mut self) -> &mut T { + &mut *self.value + } + + /// Get a pointer to the contained value. Reading from this pointer will be undefined + /// behavior unless the `MaybeUninit` is initialized. + #[unstable(feature = "maybe_uninit", issue = "53491")] + pub fn as_ptr(&self) -> *const T { + unsafe { &*self.value as *const T } + } + + /// Get a mutable pointer to the contained value. Reading from this pointer will be undefined + /// behavior unless the `MaybeUninit` is initialized. + #[unstable(feature = "maybe_uninit", issue = "53491")] + pub fn as_mut_ptr(&mut self) -> *mut T { + unsafe { &mut *self.value as *mut T } + } +} From 851acdd22dd2e99759e7f2f3e613ee9566ea0dcc Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Sun, 19 Aug 2018 17:45:31 +0200 Subject: [PATCH 02/19] core: fix deprecated warnings --- src/libcore/fmt/float.rs | 27 ++++++++++++++------------- src/libcore/lib.rs | 2 ++ src/libcore/ptr.rs | 32 ++++++++++++++------------------ src/libcore/slice/rotate.rs | 9 +++------ src/libcore/slice/sort.rs | 14 +++++++------- 5 files changed, 40 insertions(+), 44 deletions(-) diff --git a/src/libcore/fmt/float.rs b/src/libcore/fmt/float.rs index 03e7a9a49d8a7..d01cd012031db 100644 --- a/src/libcore/fmt/float.rs +++ b/src/libcore/fmt/float.rs @@ -9,7 +9,7 @@ // except according to those terms. use fmt::{Formatter, Result, LowerExp, UpperExp, Display, Debug}; -use mem; +use mem::MaybeUninit; use num::flt2dec; // Don't inline this so callers don't use the stack space this function @@ -20,11 +20,11 @@ fn float_to_decimal_common_exact(fmt: &mut Formatter, num: &T, where T: flt2dec::DecodableFloat { unsafe { - let mut buf: [u8; 1024] = mem::uninitialized(); // enough for f32 and f64 - let mut parts: [flt2dec::Part; 4] = mem::uninitialized(); + let mut buf = MaybeUninit::<[u8; 1024]>::uninitialized(); // enough for f32 and f64 + let mut parts = MaybeUninit::<[flt2dec::Part; 4]>::uninitialized(); let formatted = flt2dec::to_exact_fixed_str(flt2dec::strategy::grisu::format_exact, *num, sign, precision, - false, &mut buf, &mut parts); + false, buf.get_mut(), parts.get_mut()); fmt.pad_formatted_parts(&formatted) } } @@ -38,10 +38,11 @@ fn float_to_decimal_common_shortest(fmt: &mut Formatter, num: &T, { unsafe { // enough for f32 and f64 - let mut buf: [u8; flt2dec::MAX_SIG_DIGITS] = mem::uninitialized(); - let mut parts: [flt2dec::Part; 4] = mem::uninitialized(); + let mut buf = MaybeUninit::<[u8; flt2dec::MAX_SIG_DIGITS]>::uninitialized(); + let mut parts = MaybeUninit::<[flt2dec::Part; 4]>::uninitialized(); let formatted = flt2dec::to_shortest_str(flt2dec::strategy::grisu::format_shortest, *num, - sign, precision, false, &mut buf, &mut parts); + sign, precision, false, buf.get_mut(), + parts.get_mut()); fmt.pad_formatted_parts(&formatted) } } @@ -75,11 +76,11 @@ fn float_to_exponential_common_exact(fmt: &mut Formatter, num: &T, where T: flt2dec::DecodableFloat { unsafe { - let mut buf: [u8; 1024] = mem::uninitialized(); // enough for f32 and f64 - let mut parts: [flt2dec::Part; 6] = mem::uninitialized(); + let mut buf = MaybeUninit::<[u8; 1024]>::uninitialized(); // enough for f32 and f64 + let mut parts = MaybeUninit::<[flt2dec::Part; 6]>::uninitialized(); let formatted = flt2dec::to_exact_exp_str(flt2dec::strategy::grisu::format_exact, *num, sign, precision, - upper, &mut buf, &mut parts); + upper, buf.get_mut(), parts.get_mut()); fmt.pad_formatted_parts(&formatted) } } @@ -94,11 +95,11 @@ fn float_to_exponential_common_shortest(fmt: &mut Formatter, { unsafe { // enough for f32 and f64 - let mut buf: [u8; flt2dec::MAX_SIG_DIGITS] = mem::uninitialized(); - let mut parts: [flt2dec::Part; 6] = mem::uninitialized(); + let mut buf = MaybeUninit::<[u8; flt2dec::MAX_SIG_DIGITS]>::uninitialized(); + let mut parts = MaybeUninit::<[flt2dec::Part; 6]>::uninitialized(); let formatted = flt2dec::to_shortest_exp_str(flt2dec::strategy::grisu::format_shortest, *num, sign, (0, 0), upper, - &mut buf, &mut parts); + buf.get_mut(), parts.get_mut()); fmt.pad_formatted_parts(&formatted) } } diff --git a/src/libcore/lib.rs b/src/libcore/lib.rs index 675e73e952cc2..3b7646fa2686f 100644 --- a/src/libcore/lib.rs +++ b/src/libcore/lib.rs @@ -246,6 +246,8 @@ macro_rules! test_v512 { ($item:item) => {}; } #[allow(unused_macros)] macro_rules! vector_impl { ($([$f:ident, $($args:tt)*]),*) => { $($f!($($args)*);)* } } #[path = "../stdsimd/coresimd/mod.rs"] +// replacing uses of mem::{uninitialized,zeroed} with MaybeUninit needs to be in the stdsimd repo +#[allow(deprecated)] #[allow(missing_docs, missing_debug_implementations, dead_code, unused_imports)] #[unstable(feature = "stdsimd", issue = "48556")] #[cfg(not(stage0))] // allow changes to how stdsimd works in stage0 diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index 57351822cc3cf..caea88815145c 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -22,7 +22,7 @@ use ops::CoerceUnsized; use fmt; use hash; use marker::{PhantomData, Unsize}; -use mem; +use mem::{self, MaybeUninit}; use nonzero::NonZero; use cmp::Ordering::{self, Less, Equal, Greater}; @@ -142,16 +142,12 @@ pub const fn null_mut() -> *mut T { 0 as *mut T } #[stable(feature = "rust1", since = "1.0.0")] pub unsafe fn swap(x: *mut T, y: *mut T) { // Give ourselves some scratch space to work with - let mut tmp: T = mem::uninitialized(); + let mut tmp = MaybeUninit::::uninitialized(); // Perform the swap - copy_nonoverlapping(x, &mut tmp, 1); + copy_nonoverlapping(x, tmp.get_mut(), 1); copy(y, x, 1); // `x` and `y` may overlap - copy_nonoverlapping(&tmp, y, 1); - - // y and t now point to the same thing, but we need to completely forget `tmp` - // because it's no longer relevant. - mem::forget(tmp); + copy_nonoverlapping(tmp.get_ref(), y, 1); } /// Swaps a sequence of values at two mutable locations of the same type. @@ -224,8 +220,8 @@ unsafe fn swap_nonoverlapping_bytes(x: *mut u8, y: *mut u8, len: usize) { while i + block_size <= len { // Create some uninitialized memory as scratch space // Declaring `t` here avoids aligning the stack when this loop is unused - let mut t: Block = mem::uninitialized(); - let t = &mut t as *mut _ as *mut u8; + let mut t = mem::MaybeUninit::::uninitialized(); + let t = t.as_mut_ptr() as *mut u8; let x = x.add(i); let y = y.add(i); @@ -239,10 +235,10 @@ unsafe fn swap_nonoverlapping_bytes(x: *mut u8, y: *mut u8, len: usize) { if i < len { // Swap any remaining bytes - let mut t: UnalignedBlock = mem::uninitialized(); + let mut t = mem::MaybeUninit::::uninitialized(); let rem = len - i; - let t = &mut t as *mut _ as *mut u8; + let t = t.as_mut_ptr() as *mut u8; let x = x.add(i); let y = y.add(i); @@ -296,9 +292,9 @@ pub unsafe fn replace(dest: *mut T, mut src: T) -> T { #[inline] #[stable(feature = "rust1", since = "1.0.0")] pub unsafe fn read(src: *const T) -> T { - let mut tmp: T = mem::uninitialized(); - copy_nonoverlapping(src, &mut tmp, 1); - tmp + let mut tmp = MaybeUninit::::uninitialized(); + copy_nonoverlapping(src, tmp.get_mut(), 1); + tmp.into_inner() } /// Reads the value from `src` without moving it. This leaves the @@ -330,11 +326,11 @@ pub unsafe fn read(src: *const T) -> T { #[inline] #[stable(feature = "ptr_unaligned", since = "1.17.0")] pub unsafe fn read_unaligned(src: *const T) -> T { - let mut tmp: T = mem::uninitialized(); + let mut tmp = MaybeUninit::::uninitialized(); copy_nonoverlapping(src as *const u8, - &mut tmp as *mut T as *mut u8, + tmp.as_mut_ptr() as *mut u8, mem::size_of::()); - tmp + tmp.into_inner() } /// Overwrites a memory location with the given value without reading or diff --git a/src/libcore/slice/rotate.rs b/src/libcore/slice/rotate.rs index 0d182b8497452..affe525ae46e9 100644 --- a/src/libcore/slice/rotate.rs +++ b/src/libcore/slice/rotate.rs @@ -9,7 +9,7 @@ // except according to those terms. use cmp; -use mem; +use mem::{self, MaybeUninit}; use ptr; /// Rotation is much faster if it has access to a little bit of memory. This @@ -26,9 +26,6 @@ union RawArray { } impl RawArray { - fn new() -> Self { - unsafe { mem::uninitialized() } - } fn ptr(&self) -> *mut T { unsafe { &self.typed as *const T as *mut T } } @@ -88,8 +85,8 @@ pub unsafe fn ptr_rotate(mut left: usize, mid: *mut T, mut right: usize) { } } - let rawarray = RawArray::new(); - let buf = rawarray.ptr(); + let rawarray = MaybeUninit::>::uninitialized(); + let buf = rawarray.get_ref().ptr(); let dim = mid.sub(left).add(right); if left <= right { diff --git a/src/libcore/slice/sort.rs b/src/libcore/slice/sort.rs index e4c1fd03f9eb3..cf0c5d876abda 100644 --- a/src/libcore/slice/sort.rs +++ b/src/libcore/slice/sort.rs @@ -17,7 +17,7 @@ //! stable sorting implementation. use cmp; -use mem; +use mem::{self, MaybeUninit}; use ptr; /// When dropped, copies from `src` into `dest`. @@ -226,14 +226,14 @@ fn partition_in_blocks(v: &mut [T], pivot: &T, is_less: &mut F) -> usize let mut block_l = BLOCK; let mut start_l = ptr::null_mut(); let mut end_l = ptr::null_mut(); - let mut offsets_l: [u8; BLOCK] = unsafe { mem::uninitialized() }; + let mut offsets_l = MaybeUninit::<[u8; BLOCK]>::uninitialized(); // The current block on the right side (from `r.sub(block_r)` to `r`). let mut r = unsafe { l.add(v.len()) }; let mut block_r = BLOCK; let mut start_r = ptr::null_mut(); let mut end_r = ptr::null_mut(); - let mut offsets_r: [u8; BLOCK] = unsafe { mem::uninitialized() }; + let mut offsets_r = MaybeUninit::<[u8; BLOCK]>::uninitialized(); // FIXME: When we get VLAs, try creating one array of length `min(v.len(), 2 * BLOCK)` rather // than two fixed-size arrays of length `BLOCK`. VLAs might be more cache-efficient. @@ -272,8 +272,8 @@ fn partition_in_blocks(v: &mut [T], pivot: &T, is_less: &mut F) -> usize if start_l == end_l { // Trace `block_l` elements from the left side. - start_l = offsets_l.as_mut_ptr(); - end_l = offsets_l.as_mut_ptr(); + start_l = unsafe { offsets_l.get_mut().as_mut_ptr() }; + end_l = unsafe { offsets_l.get_mut().as_mut_ptr() }; let mut elem = l; for i in 0..block_l { @@ -288,8 +288,8 @@ fn partition_in_blocks(v: &mut [T], pivot: &T, is_less: &mut F) -> usize if start_r == end_r { // Trace `block_r` elements from the right side. - start_r = offsets_r.as_mut_ptr(); - end_r = offsets_r.as_mut_ptr(); + start_r = unsafe { offsets_r.get_mut().as_mut_ptr() }; + end_r = unsafe { offsets_r.get_mut().as_mut_ptr() }; let mut elem = r; for i in 0..block_r { From 96572cb5f8e9aab3e39162ff10fc6b8a41dde4ed Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Sun, 19 Aug 2018 17:36:04 +0200 Subject: [PATCH 03/19] panic when instantiating an uninhabited type via mem::{uninitialized,zeroed} --- src/librustc_codegen_llvm/mir/block.rs | 49 +++++++++++++++++++ src/librustc_target/abi/mod.rs | 8 +++ .../run-pass/panic-uninitialized-zeroed.rs | 31 ++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 src/test/run-pass/panic-uninitialized-zeroed.rs diff --git a/src/librustc_codegen_llvm/mir/block.rs b/src/librustc_codegen_llvm/mir/block.rs index a534b4e478fb7..72fb9df6f81bd 100644 --- a/src/librustc_codegen_llvm/mir/block.rs +++ b/src/librustc_codegen_llvm/mir/block.rs @@ -463,6 +463,55 @@ impl FunctionCx<'a, 'll, 'tcx> { return; } + if (intrinsic == Some("init") || intrinsic == Some("uninit")) && + bx.cx.layout_of(sig.output()).abi.is_uninhabited() + { + let loc = bx.sess().codemap().lookup_char_pos(span.lo()); + let filename = Symbol::intern(&loc.file.name.to_string()).as_str(); + let filename = C_str_slice(bx.cx, filename); + let line = C_u32(bx.cx, loc.line as u32); + let col = C_u32(bx.cx, loc.col.to_usize() as u32 + 1); + let align = tcx.data_layout.aggregate_align + .max(tcx.data_layout.i32_align) + .max(tcx.data_layout.pointer_align); + + let str = if intrinsic == Some("init") { + "Attempted to instantiate an uninhabited type (e.g. `!`) \ + using mem::zeroed()" + } else { + "Attempted to instantiate an uninhabited type (e.g. `!`) \ + using mem::uninitialized()" + }; + let msg_str = Symbol::intern(str).as_str(); + let msg_str = C_str_slice(bx.cx, msg_str); + let msg_file_line_col = C_struct(bx.cx, + &[msg_str, filename, line, col], + false); + let msg_file_line_col = consts::addr_of(bx.cx, + msg_file_line_col, + align, + Some("panic_loc")); + + // Obtain the panic entry point. + let def_id = + common::langcall(bx.tcx(), Some(span), "", lang_items::PanicFnLangItem); + let instance = ty::Instance::mono(bx.tcx(), def_id); + let fn_ty = FnType::of_instance(bx.cx, &instance); + let llfn = callee::get_fn(bx.cx, instance); + + // Codegen the actual panic invoke/call. + do_call( + self, + bx, + fn_ty, + llfn, + &[msg_file_line_col], + destination.as_ref().map(|(_, bb)| (ReturnDest::Nothing, *bb)), + cleanup, + ); + return; + } + let extra_args = &args[sig.inputs().len()..]; let extra_args = extra_args.iter().map(|op_arg| { let op_ty = op_arg.ty(self.mir, bx.tcx()); diff --git a/src/librustc_target/abi/mod.rs b/src/librustc_target/abi/mod.rs index 5c4cd849f89bc..96eb69163220e 100644 --- a/src/librustc_target/abi/mod.rs +++ b/src/librustc_target/abi/mod.rs @@ -802,6 +802,14 @@ impl Abi { _ => false, } } + + /// Returns true if this is an uninhabited type + pub fn is_uninhabited(&self) -> bool { + match *self { + Abi::Uninhabited => true, + _ => false, + } + } } #[derive(PartialEq, Eq, Hash, Debug)] diff --git a/src/test/run-pass/panic-uninitialized-zeroed.rs b/src/test/run-pass/panic-uninitialized-zeroed.rs new file mode 100644 index 0000000000000..a4115f8fa1d65 --- /dev/null +++ b/src/test/run-pass/panic-uninitialized-zeroed.rs @@ -0,0 +1,31 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// This test checks that instantiating an uninhabited type via `mem::{uninitialized,zeroed}` results +// in a runtime panic. + +#![feature(never_type)] + +use std::{mem, panic}; + +struct Foo { + x: u8, + y: !, +} + +fn main() { + unsafe { + panic::catch_unwind(|| mem::uninitialized::()).is_err(); + panic::catch_unwind(|| mem::zeroed::()).is_err(); + + panic::catch_unwind(|| mem::uninitialized::()).is_err(); + panic::catch_unwind(|| mem::zeroed::()).is_err(); + } +} From 7c37c6d33ef890f73c16323fe87cdbe403ba2ac8 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Sun, 19 Aug 2018 21:56:49 +0200 Subject: [PATCH 04/19] alloc: fix deprecated warnings --- src/liballoc/collections/btree/node.rs | 40 +++++++++++++------------- src/liballoc/lib.rs | 1 + 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/liballoc/collections/btree/node.rs b/src/liballoc/collections/btree/node.rs index 0315545262b6b..08bcfea980b60 100644 --- a/src/liballoc/collections/btree/node.rs +++ b/src/liballoc/collections/btree/node.rs @@ -42,7 +42,7 @@ // This implies that even an empty internal node has at least one edge. use core::marker::PhantomData; -use core::mem; +use core::mem::{self, MaybeUninit}; use core::ptr::{self, Unique, NonNull}; use core::slice; @@ -73,7 +73,7 @@ struct LeafNode { /// This node's index into the parent node's `edges` array. /// `*node.parent.edges[node.parent_idx]` should be the same thing as `node`. /// This is only guaranteed to be initialized when `parent` is nonnull. - parent_idx: u16, + parent_idx: MaybeUninit, /// The number of keys and values this node stores. /// @@ -83,8 +83,8 @@ struct LeafNode { /// The arrays storing the actual data of the node. Only the first `len` elements of each /// array are initialized and valid. - keys: [K; CAPACITY], - vals: [V; CAPACITY], + keys: MaybeUninit<[K; CAPACITY]>, + vals: MaybeUninit<[V; CAPACITY]>, } impl LeafNode { @@ -94,10 +94,10 @@ impl LeafNode { LeafNode { // As a general policy, we leave fields uninitialized if they can be, as this should // be both slightly faster and easier to track in Valgrind. - keys: mem::uninitialized(), - vals: mem::uninitialized(), + keys: MaybeUninit::uninitialized(), + vals: MaybeUninit::uninitialized(), parent: ptr::null(), - parent_idx: mem::uninitialized(), + parent_idx: MaybeUninit::uninitialized(), len: 0 } } @@ -115,10 +115,10 @@ unsafe impl Sync for LeafNode<(), ()> {} // ever take a pointer past the first key. static EMPTY_ROOT_NODE: LeafNode<(), ()> = LeafNode { parent: ptr::null(), - parent_idx: 0, + parent_idx: MaybeUninit::uninitialized(), len: 0, - keys: [(); CAPACITY], - vals: [(); CAPACITY], + keys: MaybeUninit::uninitialized(), + vals: MaybeUninit::uninitialized(), }; /// The underlying representation of internal nodes. As with `LeafNode`s, these should be hidden @@ -430,7 +430,7 @@ impl NodeRef { root: self.root, _marker: PhantomData }, - idx: self.as_leaf().parent_idx as usize, + idx: unsafe { usize::from(*self.as_leaf().parent_idx.get_ref()) }, _marker: PhantomData }) } else { @@ -567,7 +567,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef, K, V, Type> { // the node, which is allowed by LLVM. unsafe { slice::from_raw_parts( - self.as_leaf().keys.as_ptr(), + self.as_leaf().keys.get_ref().as_ptr(), self.len() ) } @@ -578,7 +578,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef, K, V, Type> { debug_assert!(!self.is_shared_root()); unsafe { slice::from_raw_parts( - self.as_leaf().vals.as_ptr(), + self.as_leaf().vals.get_ref().as_ptr(), self.len() ) } @@ -605,7 +605,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef, K, V, Type> { } else { unsafe { slice::from_raw_parts_mut( - &mut self.as_leaf_mut().keys as *mut [K] as *mut K, + self.as_leaf_mut().keys.get_mut() as *mut [K] as *mut K, self.len() ) } @@ -616,7 +616,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef, K, V, Type> { debug_assert!(!self.is_shared_root()); unsafe { slice::from_raw_parts_mut( - &mut self.as_leaf_mut().vals as *mut [V] as *mut V, + self.as_leaf_mut().vals.get_mut() as *mut [V] as *mut V, self.len() ) } @@ -1013,7 +1013,7 @@ impl<'a, K, V> Handle, K, V, marker::Internal>, marker:: let ptr = self.node.as_internal_mut() as *mut _; let mut child = self.descend(); child.as_leaf_mut().parent = ptr; - child.as_leaf_mut().parent_idx = idx; + child.as_leaf_mut().parent_idx.set(idx); } /// Unsafely asserts to the compiler some static information about whether the underlying @@ -1152,12 +1152,12 @@ impl<'a, K, V> Handle, K, V, marker::Leaf>, marker::KV> ptr::copy_nonoverlapping( self.node.keys().as_ptr().add(self.idx + 1), - new_node.keys.as_mut_ptr(), + new_node.keys.get_mut().as_mut_ptr(), new_len ); ptr::copy_nonoverlapping( self.node.vals().as_ptr().add(self.idx + 1), - new_node.vals.as_mut_ptr(), + new_node.vals.get_mut().as_mut_ptr(), new_len ); @@ -1210,12 +1210,12 @@ impl<'a, K, V> Handle, K, V, marker::Internal>, marker:: ptr::copy_nonoverlapping( self.node.keys().as_ptr().add(self.idx + 1), - new_node.data.keys.as_mut_ptr(), + new_node.data.keys.get_mut().as_mut_ptr(), new_len ); ptr::copy_nonoverlapping( self.node.vals().as_ptr().add(self.idx + 1), - new_node.data.vals.as_mut_ptr(), + new_node.data.vals.get_mut().as_mut_ptr(), new_len ); ptr::copy_nonoverlapping( diff --git a/src/liballoc/lib.rs b/src/liballoc/lib.rs index b2303a02cb265..089480c06d20d 100644 --- a/src/liballoc/lib.rs +++ b/src/liballoc/lib.rs @@ -119,6 +119,7 @@ #![feature(exact_chunks)] #![feature(rustc_const_unstable)] #![feature(const_vec_new)] +#![feature(maybe_uninit)] // Allow testing this library From ce8503d5afb51b22b44c457c95328411c17f078f Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Sun, 19 Aug 2018 21:59:21 +0200 Subject: [PATCH 05/19] don't deprecate mem::{uninitialized,zeroed} just yet --- src/libcore/mem.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/libcore/mem.rs b/src/libcore/mem.rs index 91522885b598b..c99c9f96f12d6 100644 --- a/src/libcore/mem.rs +++ b/src/libcore/mem.rs @@ -514,7 +514,7 @@ pub fn needs_drop() -> bool { /// assert_eq!(0, x); /// ``` #[inline] -#[rustc_deprecated(since = "1.30.0", reason = "use `mem::MaybeUninit::zeroed` instead")] +#[rustc_deprecated(since = "2.0.0", reason = "use `mem::MaybeUninit::zeroed` instead")] #[stable(feature = "rust1", since = "1.0.0")] pub unsafe fn zeroed() -> T { intrinsics::init() @@ -609,7 +609,7 @@ pub unsafe fn zeroed() -> T { /// [copy_no]: ../intrinsics/fn.copy_nonoverlapping.html /// [`Drop`]: ../ops/trait.Drop.html #[inline] -#[rustc_deprecated(since = "1.30.0", reason = "use `mem::MaybeUninit::uninitialized` instead")] +#[rustc_deprecated(since = "2.0.0", reason = "use `mem::MaybeUninit::uninitialized` instead")] #[stable(feature = "rust1", since = "1.0.0")] pub unsafe fn uninitialized() -> T { intrinsics::uninit() @@ -1030,6 +1030,7 @@ impl DerefMut for ManuallyDrop { /// A newtype to construct uninitialized instances of `T` #[allow(missing_debug_implementations)] #[unstable(feature = "maybe_uninit", issue = "53491")] +// NOTE after stabilizing `MaybeUninit` proceed to deprecate `mem::{uninitialized,zeroed}` pub union MaybeUninit { uninit: (), value: ManuallyDrop, From a6d011a98668cf672db2de8aae42f3654df4787f Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Thu, 23 Aug 2018 16:18:28 +0200 Subject: [PATCH 06/19] adapt to change in Session API --- src/librustc_codegen_llvm/mir/block.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustc_codegen_llvm/mir/block.rs b/src/librustc_codegen_llvm/mir/block.rs index 72fb9df6f81bd..e876585db656a 100644 --- a/src/librustc_codegen_llvm/mir/block.rs +++ b/src/librustc_codegen_llvm/mir/block.rs @@ -466,7 +466,7 @@ impl FunctionCx<'a, 'll, 'tcx> { if (intrinsic == Some("init") || intrinsic == Some("uninit")) && bx.cx.layout_of(sig.output()).abi.is_uninhabited() { - let loc = bx.sess().codemap().lookup_char_pos(span.lo()); + let loc = bx.sess().source_map().lookup_char_pos(span.lo()); let filename = Symbol::intern(&loc.file.name.to_string()).as_str(); let filename = C_str_slice(bx.cx, filename); let line = C_u32(bx.cx, loc.line as u32); From af101fdc331b6f607ed645d86a0f12bc7a2cbc5c Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Thu, 23 Aug 2018 16:29:01 +0200 Subject: [PATCH 07/19] address Mark-Simulacrum comments --- src/libcore/ptr.rs | 4 ++-- src/libcore/slice/rotate.rs | 7 ++----- src/libcore/slice/sort.rs | 8 ++++---- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index caea88815145c..6312aa439d20f 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -145,7 +145,7 @@ pub unsafe fn swap(x: *mut T, y: *mut T) { let mut tmp = MaybeUninit::::uninitialized(); // Perform the swap - copy_nonoverlapping(x, tmp.get_mut(), 1); + copy_nonoverlapping(x, tmp.as_mut_ptr(), 1); copy(y, x, 1); // `x` and `y` may overlap copy_nonoverlapping(tmp.get_ref(), y, 1); } @@ -293,7 +293,7 @@ pub unsafe fn replace(dest: *mut T, mut src: T) -> T { #[stable(feature = "rust1", since = "1.0.0")] pub unsafe fn read(src: *const T) -> T { let mut tmp = MaybeUninit::::uninitialized(); - copy_nonoverlapping(src, tmp.get_mut(), 1); + copy_nonoverlapping(src, tmp.as_mut_ptr(), 1); tmp.into_inner() } diff --git a/src/libcore/slice/rotate.rs b/src/libcore/slice/rotate.rs index affe525ae46e9..07153735300b8 100644 --- a/src/libcore/slice/rotate.rs +++ b/src/libcore/slice/rotate.rs @@ -26,9 +26,6 @@ union RawArray { } impl RawArray { - fn ptr(&self) -> *mut T { - unsafe { &self.typed as *const T as *mut T } - } fn cap() -> usize { if mem::size_of::() == 0 { usize::max_value() @@ -85,8 +82,8 @@ pub unsafe fn ptr_rotate(mut left: usize, mid: *mut T, mut right: usize) { } } - let rawarray = MaybeUninit::>::uninitialized(); - let buf = rawarray.get_ref().ptr(); + let mut rawarray = MaybeUninit::>::uninitialized(); + let buf = &mut (*rawarray.as_mut_ptr()).typed as *mut [T; 2] as *mut T; let dim = mid.sub(left).add(right); if left <= right { diff --git a/src/libcore/slice/sort.rs b/src/libcore/slice/sort.rs index cf0c5d876abda..affe84fbef91f 100644 --- a/src/libcore/slice/sort.rs +++ b/src/libcore/slice/sort.rs @@ -272,8 +272,8 @@ fn partition_in_blocks(v: &mut [T], pivot: &T, is_less: &mut F) -> usize if start_l == end_l { // Trace `block_l` elements from the left side. - start_l = unsafe { offsets_l.get_mut().as_mut_ptr() }; - end_l = unsafe { offsets_l.get_mut().as_mut_ptr() }; + start_l = offsets_l.as_mut_ptr() as *mut u8; + end_l = offsets_l.as_mut_ptr() as *mut u8; let mut elem = l; for i in 0..block_l { @@ -288,8 +288,8 @@ fn partition_in_blocks(v: &mut [T], pivot: &T, is_less: &mut F) -> usize if start_r == end_r { // Trace `block_r` elements from the right side. - start_r = unsafe { offsets_r.get_mut().as_mut_ptr() }; - end_r = unsafe { offsets_r.get_mut().as_mut_ptr() }; + start_r = offsets_r.as_mut_ptr() as *mut u8; + end_r = offsets_r.as_mut_ptr() as *mut u8; let mut elem = r; for i in 0..block_r { From ce6e6f93330b9333e188b7d3e99cbffdac725a59 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Thu, 23 Aug 2018 16:34:38 +0200 Subject: [PATCH 08/19] use is_uninhabited in more places --- src/librustc/ty/layout.rs | 10 +++++----- src/librustc_codegen_llvm/debuginfo/mod.rs | 2 +- src/librustc_codegen_llvm/declare.rs | 4 ++-- src/librustc_codegen_llvm/mir/place.rs | 4 ++-- src/librustc_codegen_llvm/mir/rvalue.rs | 2 +- src/librustc_mir/interpret/operand.rs | 2 +- 6 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/librustc/ty/layout.rs b/src/librustc/ty/layout.rs index 4e37a34a0c8a7..3d2088ea12e8e 100644 --- a/src/librustc/ty/layout.rs +++ b/src/librustc/ty/layout.rs @@ -449,7 +449,7 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> { } } - if sized && fields.iter().any(|f| f.abi == Abi::Uninhabited) { + if sized && fields.iter().any(|f| f.abi.is_uninhabited()) { abi = Abi::Uninhabited; } @@ -724,7 +724,7 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> { // See issue #49298 for more details on the need to leave space // for non-ZST uninhabited data (mostly partial initialization). let absent = |fields: &[TyLayout]| { - let uninhabited = fields.iter().any(|f| f.abi == Abi::Uninhabited); + let uninhabited = fields.iter().any(|f| f.abi.is_uninhabited()); let is_zst = fields.iter().all(|f| f.is_zst()); uninhabited && is_zst }; @@ -872,7 +872,7 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> { _ => Abi::Aggregate { sized: true }, }; - if st.iter().all(|v| v.abi == Abi::Uninhabited) { + if st.iter().all(|v| v.abi.is_uninhabited()) { abi = Abi::Uninhabited; } @@ -900,7 +900,7 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> { let discr_type = def.repr.discr_type(); let bits = Integer::from_attr(tcx, discr_type).size().bits(); for (i, discr) in def.discriminants(tcx).enumerate() { - if variants[i].iter().any(|f| f.abi == Abi::Uninhabited) { + if variants[i].iter().any(|f| f.abi.is_uninhabited()) { continue; } let mut x = discr.val as i128; @@ -1096,7 +1096,7 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> { } } - if layout_variants.iter().all(|v| v.abi == Abi::Uninhabited) { + if layout_variants.iter().all(|v| v.abi.is_uninhabited()) { abi = Abi::Uninhabited; } diff --git a/src/librustc_codegen_llvm/debuginfo/mod.rs b/src/librustc_codegen_llvm/debuginfo/mod.rs index 99919a940b405..7b0c413e85761 100644 --- a/src/librustc_codegen_llvm/debuginfo/mod.rs +++ b/src/librustc_codegen_llvm/debuginfo/mod.rs @@ -279,7 +279,7 @@ pub fn create_function_debug_context( } None => {} }; - if cx.layout_of(sig.output()).abi == ty::layout::Abi::Uninhabited { + if cx.layout_of(sig.output()).abi.is_uninhabited() { flags = flags | DIFlags::FlagNoReturn; } diff --git a/src/librustc_codegen_llvm/declare.rs b/src/librustc_codegen_llvm/declare.rs index 5e743ac51bc61..7141c9ece89d7 100644 --- a/src/librustc_codegen_llvm/declare.rs +++ b/src/librustc_codegen_llvm/declare.rs @@ -23,7 +23,7 @@ use llvm; use llvm::AttributePlace::Function; use rustc::ty::{self, Ty}; -use rustc::ty::layout::{self, LayoutOf}; +use rustc::ty::layout::LayoutOf; use rustc::session::config::Sanitizer; use rustc_data_structures::small_c_str::SmallCStr; use rustc_target::spec::PanicStrategy; @@ -137,7 +137,7 @@ pub fn declare_fn( let fty = FnType::new(cx, sig, &[]); let llfn = declare_raw_fn(cx, name, fty.llvm_cconv(), fty.llvm_type(cx)); - if cx.layout_of(sig.output()).abi == layout::Abi::Uninhabited { + if cx.layout_of(sig.output()).abi.is_uninhabited() { llvm::Attribute::NoReturn.apply_llfn(Function, llfn); } diff --git a/src/librustc_codegen_llvm/mir/place.rs b/src/librustc_codegen_llvm/mir/place.rs index 70ace15e5236f..bc6ebd360e831 100644 --- a/src/librustc_codegen_llvm/mir/place.rs +++ b/src/librustc_codegen_llvm/mir/place.rs @@ -278,7 +278,7 @@ impl PlaceRef<'ll, 'tcx> { /// Obtain the actual discriminant of a value. pub fn codegen_get_discr(self, bx: &Builder<'a, 'll, 'tcx>, cast_to: Ty<'tcx>) -> &'ll Value { let cast_to = bx.cx.layout_of(cast_to).immediate_llvm_type(bx.cx); - if self.layout.abi == layout::Abi::Uninhabited { + if self.layout.abi.is_uninhabited() { return C_undef(cast_to); } match self.layout.variants { @@ -341,7 +341,7 @@ impl PlaceRef<'ll, 'tcx> { /// Set the discriminant for a new value of the given case of the given /// representation. pub fn codegen_set_discr(&self, bx: &Builder<'a, 'll, 'tcx>, variant_index: usize) { - if self.layout.for_variant(bx.cx, variant_index).abi == layout::Abi::Uninhabited { + if self.layout.for_variant(bx.cx, variant_index).abi.is_uninhabited() { return; } match self.layout.variants { diff --git a/src/librustc_codegen_llvm/mir/rvalue.rs b/src/librustc_codegen_llvm/mir/rvalue.rs index c3ec347f60876..fa22bdff94ddd 100644 --- a/src/librustc_codegen_llvm/mir/rvalue.rs +++ b/src/librustc_codegen_llvm/mir/rvalue.rs @@ -290,7 +290,7 @@ impl FunctionCx<'a, 'll, 'tcx> { mir::CastKind::Misc => { assert!(cast.is_llvm_immediate()); let ll_t_out = cast.immediate_llvm_type(bx.cx); - if operand.layout.abi == layout::Abi::Uninhabited { + if operand.layout.abi.is_uninhabited() { return (bx, OperandRef { val: OperandValue::Immediate(C_undef(ll_t_out)), layout: cast, diff --git a/src/librustc_mir/interpret/operand.rs b/src/librustc_mir/interpret/operand.rs index c7f84f7683953..c966750f3aa07 100644 --- a/src/librustc_mir/interpret/operand.rs +++ b/src/librustc_mir/interpret/operand.rs @@ -524,7 +524,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> { rval: OpTy<'tcx>, ) -> EvalResult<'tcx, (u128, usize)> { trace!("read_discriminant_value {:#?}", rval.layout); - if rval.layout.abi == layout::Abi::Uninhabited { + if rval.layout.abi.is_uninhabited() { return err!(Unreachable); } From 33c10eae904a69422b47501444fc319185cc5158 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Thu, 23 Aug 2018 16:36:51 +0200 Subject: [PATCH 09/19] improve panic message --- src/librustc_codegen_llvm/mir/block.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/librustc_codegen_llvm/mir/block.rs b/src/librustc_codegen_llvm/mir/block.rs index e876585db656a..b3ca78d59def3 100644 --- a/src/librustc_codegen_llvm/mir/block.rs +++ b/src/librustc_codegen_llvm/mir/block.rs @@ -475,14 +475,12 @@ impl FunctionCx<'a, 'll, 'tcx> { .max(tcx.data_layout.i32_align) .max(tcx.data_layout.pointer_align); - let str = if intrinsic == Some("init") { - "Attempted to instantiate an uninhabited type (e.g. `!`) \ - using mem::zeroed()" - } else { - "Attempted to instantiate an uninhabited type (e.g. `!`) \ - using mem::uninitialized()" - }; - let msg_str = Symbol::intern(str).as_str(); + let str = format!( + "Attempted to instantiate uninhabited type {} using mem::{}", + sig.output(), + if intrinsic == Some("init") { "zeroed" } else { "uninitialized" } + ); + let msg_str = Symbol::intern(&str).as_str(); let msg_str = C_str_slice(bx.cx, msg_str); let msg_file_line_col = C_struct(bx.cx, &[msg_str, filename, line, col], From bc5b567a324b9fdab314458f501818269560bd77 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Thu, 23 Aug 2018 16:39:03 +0200 Subject: [PATCH 10/19] move our check to reuse a previous computation --- src/librustc_codegen_llvm/mir/block.rs | 41 +++++++++++++------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/src/librustc_codegen_llvm/mir/block.rs b/src/librustc_codegen_llvm/mir/block.rs index b3ca78d59def3..709fceb492509 100644 --- a/src/librustc_codegen_llvm/mir/block.rs +++ b/src/librustc_codegen_llvm/mir/block.rs @@ -463,8 +463,28 @@ impl FunctionCx<'a, 'll, 'tcx> { return; } + let extra_args = &args[sig.inputs().len()..]; + let extra_args = extra_args.iter().map(|op_arg| { + let op_ty = op_arg.ty(self.mir, bx.tcx()); + self.monomorphize(&op_ty) + }).collect::>(); + + let fn_ty = match def { + Some(ty::InstanceDef::Virtual(..)) => { + FnType::new_vtable(bx.cx, sig, &extra_args) + } + Some(ty::InstanceDef::DropGlue(_, None)) => { + // empty drop glue - a nop. + let &(_, target) = destination.as_ref().unwrap(); + funclet_br(self, bx, target); + return; + } + _ => FnType::new(bx.cx, sig, &extra_args) + }; + + // emit a panic instead of instantiating an uninhabited type if (intrinsic == Some("init") || intrinsic == Some("uninit")) && - bx.cx.layout_of(sig.output()).abi.is_uninhabited() + fn_ty.ret.layout.abi.is_uninhabited() { let loc = bx.sess().source_map().lookup_char_pos(span.lo()); let filename = Symbol::intern(&loc.file.name.to_string()).as_str(); @@ -510,25 +530,6 @@ impl FunctionCx<'a, 'll, 'tcx> { return; } - let extra_args = &args[sig.inputs().len()..]; - let extra_args = extra_args.iter().map(|op_arg| { - let op_ty = op_arg.ty(self.mir, bx.tcx()); - self.monomorphize(&op_ty) - }).collect::>(); - - let fn_ty = match def { - Some(ty::InstanceDef::Virtual(..)) => { - FnType::new_vtable(bx.cx, sig, &extra_args) - } - Some(ty::InstanceDef::DropGlue(_, None)) => { - // empty drop glue - a nop. - let &(_, target) = destination.as_ref().unwrap(); - funclet_br(self, bx, target); - return; - } - _ => FnType::new(bx.cx, sig, &extra_args) - }; - // The arguments we'll be passing. Plus one to account for outptr, if used. let arg_count = fn_ty.args.len() + fn_ty.ret.is_indirect() as usize; let mut llargs = Vec::with_capacity(arg_count); From d864edc349f2548d9b05c7fa99f1387946e18f8b Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Thu, 23 Aug 2018 16:49:00 +0200 Subject: [PATCH 11/19] improve the run-pass test --- .../run-pass/panic-uninitialized-zeroed.rs | 38 +++++++++++++++++-- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/src/test/run-pass/panic-uninitialized-zeroed.rs b/src/test/run-pass/panic-uninitialized-zeroed.rs index a4115f8fa1d65..fd88bba49c484 100644 --- a/src/test/run-pass/panic-uninitialized-zeroed.rs +++ b/src/test/run-pass/panic-uninitialized-zeroed.rs @@ -22,10 +22,40 @@ struct Foo { fn main() { unsafe { - panic::catch_unwind(|| mem::uninitialized::()).is_err(); - panic::catch_unwind(|| mem::zeroed::()).is_err(); + assert_eq!( + panic::catch_unwind(|| { + mem::uninitialized::() + }).err().and_then(|a| a.downcast_ref::().map(|s| { + s == "Attempted to instantiate uninhabited type ! using mem::uninitialized" + })), + Some(true) + ); - panic::catch_unwind(|| mem::uninitialized::()).is_err(); - panic::catch_unwind(|| mem::zeroed::()).is_err(); + assert_eq!( + panic::catch_unwind(|| { + mem::zeroed::() + }).err().and_then(|a| a.downcast_ref::().map(|s| { + s == "Attempted to instantiate uninhabited type ! using mem::zeroed" + })), + Some(true) + ); + + assert_eq!( + panic::catch_unwind(|| { + mem::uninitialized::() + }).err().and_then(|a| a.downcast_ref::().map(|s| { + s == "Attempted to instantiate uninhabited type Foo using mem::uninitialized" + })), + Some(true) + ); + + assert_eq!( + panic::catch_unwind(|| { + mem::zeroed::() + }).err().and_then(|a| a.downcast_ref::().map(|s| { + s == "Attempted to instantiate uninhabited type Foo using mem::zeroed" + })), + Some(true) + ); } } From 758ce16b3dea3f08bfdacea75bcc55a8cd4ff106 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Thu, 23 Aug 2018 16:49:35 +0200 Subject: [PATCH 12/19] add empty enum to the test cases --- .../run-pass/panic-uninitialized-zeroed.rs | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/test/run-pass/panic-uninitialized-zeroed.rs b/src/test/run-pass/panic-uninitialized-zeroed.rs index fd88bba49c484..f40ea7cce078a 100644 --- a/src/test/run-pass/panic-uninitialized-zeroed.rs +++ b/src/test/run-pass/panic-uninitialized-zeroed.rs @@ -20,6 +20,8 @@ struct Foo { y: !, } +enum Bar {} + fn main() { unsafe { assert_eq!( @@ -57,5 +59,23 @@ fn main() { })), Some(true) ); + + assert_eq!( + panic::catch_unwind(|| { + mem::uninitialized::() + }).err().and_then(|a| a.downcast_ref::().map(|s| { + s == "Attempted to instantiate uninhabited type Bar using mem::uninitialized" + })), + Some(true) + ); + + assert_eq!( + panic::catch_unwind(|| { + mem::zeroed::() + }).err().and_then(|a| a.downcast_ref::().map(|s| { + s == "Attempted to instantiate uninhabited type Bar using mem::zeroed" + })), + Some(true) + ); } } From 8fb0e80e405b755c6ea9cb77c1f88dbf4b75b245 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Thu, 23 Aug 2018 16:52:05 +0200 Subject: [PATCH 13/19] address RalfJung's comment --- src/liballoc/collections/btree/node.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/liballoc/collections/btree/node.rs b/src/liballoc/collections/btree/node.rs index 08bcfea980b60..c86278fc5ddaa 100644 --- a/src/liballoc/collections/btree/node.rs +++ b/src/liballoc/collections/btree/node.rs @@ -567,7 +567,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef, K, V, Type> { // the node, which is allowed by LLVM. unsafe { slice::from_raw_parts( - self.as_leaf().keys.get_ref().as_ptr(), + self.as_leaf().keys.as_ptr() as *const K, self.len() ) } @@ -578,7 +578,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef, K, V, Type> { debug_assert!(!self.is_shared_root()); unsafe { slice::from_raw_parts( - self.as_leaf().vals.get_ref().as_ptr(), + self.as_leaf().vals.as_ptr() as *const V, self.len() ) } @@ -1152,12 +1152,12 @@ impl<'a, K, V> Handle, K, V, marker::Leaf>, marker::KV> ptr::copy_nonoverlapping( self.node.keys().as_ptr().add(self.idx + 1), - new_node.keys.get_mut().as_mut_ptr(), + new_node.keys.as_mut_ptr() as *mut K, new_len ); ptr::copy_nonoverlapping( self.node.vals().as_ptr().add(self.idx + 1), - new_node.vals.get_mut().as_mut_ptr(), + new_node.vals.as_mut_ptr() as *mut V, new_len ); @@ -1210,12 +1210,12 @@ impl<'a, K, V> Handle, K, V, marker::Internal>, marker:: ptr::copy_nonoverlapping( self.node.keys().as_ptr().add(self.idx + 1), - new_node.data.keys.get_mut().as_mut_ptr(), + new_node.data.keys.as_mut_ptr() as *mut K, new_len ); ptr::copy_nonoverlapping( self.node.vals().as_ptr().add(self.idx + 1), - new_node.data.vals.get_mut().as_mut_ptr(), + new_node.data.vals.as_mut_ptr() as *mut V, new_len ); ptr::copy_nonoverlapping( From 8482c93362840857ba8c51c3edb7c6ad2277c496 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Sat, 1 Sep 2018 19:58:04 +0200 Subject: [PATCH 14/19] gdb_rust_pretty_printing: adapt to the changes in the layout of btree::LeafNode --- src/etc/gdb_rust_pretty_printing.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/etc/gdb_rust_pretty_printing.py b/src/etc/gdb_rust_pretty_printing.py index 216915dba5fe7..686c2c447f6ce 100755 --- a/src/etc/gdb_rust_pretty_printing.py +++ b/src/etc/gdb_rust_pretty_printing.py @@ -322,8 +322,10 @@ def to_string(self): def children(self): (length, data_ptr) = \ rustpp.extract_length_and_ptr_from_std_btreeset(self.__val) - val = GdbValue(data_ptr.get_wrapped_value().dereference()).get_child_at_index(3) - gdb_ptr = val.get_wrapped_value() + maybe_uninit_keys = GdbValue(data_ptr.get_wrapped_value().dereference()).get_child_at_index(3) + manually_drop_keys = maybe_uninit_keys.get_child_at_index(1) + keys = manually_drop_keys.get_child_at_index(0) + gdb_ptr = keys.get_wrapped_value() for index in xrange(length): yield (str(index), gdb_ptr[index]) @@ -345,9 +347,13 @@ def to_string(self): def children(self): (length, data_ptr) = \ rustpp.extract_length_and_ptr_from_std_btreemap(self.__val) - keys = GdbValue(data_ptr.get_wrapped_value().dereference()).get_child_at_index(3) + maybe_uninit_keys = GdbValue(data_ptr.get_wrapped_value().dereference()).get_child_at_index(3) + manually_drop_keys = maybe_uninit_keys.get_child_at_index(1) + keys = manually_drop_keys.get_child_at_index(0) keys_ptr = keys.get_wrapped_value() - vals = GdbValue(data_ptr.get_wrapped_value().dereference()).get_child_at_index(4) + maybe_uninit_vals = GdbValue(data_ptr.get_wrapped_value().dereference()).get_child_at_index(4) + manually_drop_vals = maybe_uninit_vals.get_child_at_index(1) + vals = manually_drop_vals.get_child_at_index(0) vals_ptr = vals.get_wrapped_value() for index in xrange(length): yield (str(index), keys_ptr[index]) From 7fea7f4912264397865c8baf037782f0cd025944 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Sat, 1 Sep 2018 20:13:13 +0200 Subject: [PATCH 15/19] make the nil-enum test work again --- src/test/debuginfo/nil-enum.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/test/debuginfo/nil-enum.rs b/src/test/debuginfo/nil-enum.rs index 94377421c0b0c..ab9c7e2dd2758 100644 --- a/src/test/debuginfo/nil-enum.rs +++ b/src/test/debuginfo/nil-enum.rs @@ -8,6 +8,8 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +// NOTE Instantiating an empty enum is UB. This test may break in the future. + // LLDB can't handle zero-sized values // ignore-lldb @@ -25,8 +27,11 @@ #![allow(unused_variables)] #![feature(omit_gdb_pretty_printer_section)] +#![feature(maybe_uninit)] #![omit_gdb_pretty_printer_section] +use std::mem::MaybeUninit; + enum ANilEnum {} enum AnotherNilEnum {} @@ -35,8 +40,8 @@ enum AnotherNilEnum {} // The error from gdbr is expected since nil enums are not supposed to exist. fn main() { unsafe { - let first: ANilEnum = ::std::mem::zeroed(); - let second: AnotherNilEnum = ::std::mem::zeroed(); + let first: ANilEnum = MaybeUninit::uninitialized().into_inner(); + let second: AnotherNilEnum = MaybeUninit::uninitialized().into_inner(); zzz(); // #break } From dd9f01966043c5bf256c181317cae7218d9b579a Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Sat, 1 Sep 2018 21:44:35 +0200 Subject: [PATCH 16/19] fix tidy --- src/etc/gdb_rust_pretty_printing.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/etc/gdb_rust_pretty_printing.py b/src/etc/gdb_rust_pretty_printing.py index 686c2c447f6ce..b1252f386df36 100755 --- a/src/etc/gdb_rust_pretty_printing.py +++ b/src/etc/gdb_rust_pretty_printing.py @@ -322,7 +322,8 @@ def to_string(self): def children(self): (length, data_ptr) = \ rustpp.extract_length_and_ptr_from_std_btreeset(self.__val) - maybe_uninit_keys = GdbValue(data_ptr.get_wrapped_value().dereference()).get_child_at_index(3) + leaf_node = GdbValue(data_ptr.get_wrapped_value().dereference()) + maybe_uninit_keys = leaf_node.get_child_at_index(3) manually_drop_keys = maybe_uninit_keys.get_child_at_index(1) keys = manually_drop_keys.get_child_at_index(0) gdb_ptr = keys.get_wrapped_value() @@ -347,11 +348,12 @@ def to_string(self): def children(self): (length, data_ptr) = \ rustpp.extract_length_and_ptr_from_std_btreemap(self.__val) - maybe_uninit_keys = GdbValue(data_ptr.get_wrapped_value().dereference()).get_child_at_index(3) + leaf_node = GdbValue(data_ptr.get_wrapped_value().dereference()) + maybe_uninit_keys = leaf_node.get_child_at_index(3) manually_drop_keys = maybe_uninit_keys.get_child_at_index(1) keys = manually_drop_keys.get_child_at_index(0) keys_ptr = keys.get_wrapped_value() - maybe_uninit_vals = GdbValue(data_ptr.get_wrapped_value().dereference()).get_child_at_index(4) + maybe_uninit_vals = leaf_node.get_child_at_index(4) manually_drop_vals = maybe_uninit_vals.get_child_at_index(1) vals = manually_drop_vals.get_child_at_index(0) vals_ptr = vals.get_wrapped_value() From 41b242eeece5b366f78888f88018538320ceb5de Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Thu, 20 Sep 2018 20:57:25 +0200 Subject: [PATCH 17/19] add codegen test --- src/test/codegen/box-maybe-uninit.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/test/codegen/box-maybe-uninit.rs diff --git a/src/test/codegen/box-maybe-uninit.rs b/src/test/codegen/box-maybe-uninit.rs new file mode 100644 index 0000000000000..168e1a3eba0c5 --- /dev/null +++ b/src/test/codegen/box-maybe-uninit.rs @@ -0,0 +1,23 @@ +// Copyright 2017 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// compile-flags: -O +#![crate_type="lib"] +#![feature(maybe_uninit)] + +use std::mem::MaybeUninit; + +// Boxing a `MaybeUninit` value should not copy junk from the stack +#[no_mangle] +pub fn box_uninitialized() -> Box> { + // CHECK-LABEL: @box_uninitialized + // CHECK-NOT: store + Box::new(MaybeUninit::uninitialized()) +} From d266722a59711502aa29af659387b5b035b90151 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Fri, 21 Sep 2018 23:40:00 +0200 Subject: [PATCH 18/19] the test requires unwinding so we don't run it on the wasm32-bare target --- src/test/run-pass/panic-uninitialized-zeroed.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/run-pass/panic-uninitialized-zeroed.rs b/src/test/run-pass/panic-uninitialized-zeroed.rs index f40ea7cce078a..0108a02e88aae 100644 --- a/src/test/run-pass/panic-uninitialized-zeroed.rs +++ b/src/test/run-pass/panic-uninitialized-zeroed.rs @@ -8,6 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +// ignore-wasm32-bare always compiled as panic=abort right now and this requires unwinding // This test checks that instantiating an uninhabited type via `mem::{uninitialized,zeroed}` results // in a runtime panic. From 1cdbad20224a2e8da4a63aea8389b34bf8313281 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Sun, 23 Sep 2018 00:37:08 +0200 Subject: [PATCH 19/19] allow dead_code --- src/test/run-pass/panic-uninitialized-zeroed.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/run-pass/panic-uninitialized-zeroed.rs b/src/test/run-pass/panic-uninitialized-zeroed.rs index 0108a02e88aae..2972f6efa32e0 100644 --- a/src/test/run-pass/panic-uninitialized-zeroed.rs +++ b/src/test/run-pass/panic-uninitialized-zeroed.rs @@ -16,6 +16,7 @@ use std::{mem, panic}; +#[allow(dead_code)] struct Foo { x: u8, y: !,