Skip to content

Commit c6e3d7f

Browse files
committed
Auto merge of #53508 - japaric:maybe-uninit, r=RalfJung
Implement `MaybeUninit` This PR: - Adds `MaybeUninit` (see #53491) to `{core,std}::mem`. - Makes `mem::{uninitialized,zeroed}` panic when they are used to instantiate an uninhabited type. - Does *not* deprecate `mem::{uninitialized,zeroed}` just yet. As per #53491 (comment), we should not deprecate them until `MaybeUninit` is stabilized. - It replaces uses of `mem::{uninitialized,zeroed}` in core and alloc with `MaybeUninit`. There are still several instances of `mem::{uninitialized,zeroed}` in `std` that *this* PR doesn't address. r? @RalfJung cc @eddyb you may want to look at the new panicking logic
2 parents 4591a24 + 1cdbad2 commit c6e3d7f

File tree

20 files changed

+350
-85
lines changed

20 files changed

+350
-85
lines changed

Diff for: src/etc/gdb_rust_pretty_printing.py

+12-4
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,11 @@ def to_string(self):
322322
def children(self):
323323
(length, data_ptr) = \
324324
rustpp.extract_length_and_ptr_from_std_btreeset(self.__val)
325-
val = GdbValue(data_ptr.get_wrapped_value().dereference()).get_child_at_index(3)
326-
gdb_ptr = val.get_wrapped_value()
325+
leaf_node = GdbValue(data_ptr.get_wrapped_value().dereference())
326+
maybe_uninit_keys = leaf_node.get_child_at_index(3)
327+
manually_drop_keys = maybe_uninit_keys.get_child_at_index(1)
328+
keys = manually_drop_keys.get_child_at_index(0)
329+
gdb_ptr = keys.get_wrapped_value()
327330
for index in xrange(length):
328331
yield (str(index), gdb_ptr[index])
329332

@@ -345,9 +348,14 @@ def to_string(self):
345348
def children(self):
346349
(length, data_ptr) = \
347350
rustpp.extract_length_and_ptr_from_std_btreemap(self.__val)
348-
keys = GdbValue(data_ptr.get_wrapped_value().dereference()).get_child_at_index(3)
351+
leaf_node = GdbValue(data_ptr.get_wrapped_value().dereference())
352+
maybe_uninit_keys = leaf_node.get_child_at_index(3)
353+
manually_drop_keys = maybe_uninit_keys.get_child_at_index(1)
354+
keys = manually_drop_keys.get_child_at_index(0)
349355
keys_ptr = keys.get_wrapped_value()
350-
vals = GdbValue(data_ptr.get_wrapped_value().dereference()).get_child_at_index(4)
356+
maybe_uninit_vals = leaf_node.get_child_at_index(4)
357+
manually_drop_vals = maybe_uninit_vals.get_child_at_index(1)
358+
vals = manually_drop_vals.get_child_at_index(0)
351359
vals_ptr = vals.get_wrapped_value()
352360
for index in xrange(length):
353361
yield (str(index), keys_ptr[index])

Diff for: src/liballoc/collections/btree/node.rs

+20-20
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
// This implies that even an empty internal node has at least one edge.
4343

4444
use core::marker::PhantomData;
45-
use core::mem;
45+
use core::mem::{self, MaybeUninit};
4646
use core::ptr::{self, Unique, NonNull};
4747
use core::slice;
4848

@@ -73,7 +73,7 @@ struct LeafNode<K, V> {
7373
/// This node's index into the parent node's `edges` array.
7474
/// `*node.parent.edges[node.parent_idx]` should be the same thing as `node`.
7575
/// This is only guaranteed to be initialized when `parent` is nonnull.
76-
parent_idx: u16,
76+
parent_idx: MaybeUninit<u16>,
7777

7878
/// The number of keys and values this node stores.
7979
///
@@ -83,8 +83,8 @@ struct LeafNode<K, V> {
8383

8484
/// The arrays storing the actual data of the node. Only the first `len` elements of each
8585
/// array are initialized and valid.
86-
keys: [K; CAPACITY],
87-
vals: [V; CAPACITY],
86+
keys: MaybeUninit<[K; CAPACITY]>,
87+
vals: MaybeUninit<[V; CAPACITY]>,
8888
}
8989

9090
impl<K, V> LeafNode<K, V> {
@@ -94,10 +94,10 @@ impl<K, V> LeafNode<K, V> {
9494
LeafNode {
9595
// As a general policy, we leave fields uninitialized if they can be, as this should
9696
// be both slightly faster and easier to track in Valgrind.
97-
keys: mem::uninitialized(),
98-
vals: mem::uninitialized(),
97+
keys: MaybeUninit::uninitialized(),
98+
vals: MaybeUninit::uninitialized(),
9999
parent: ptr::null(),
100-
parent_idx: mem::uninitialized(),
100+
parent_idx: MaybeUninit::uninitialized(),
101101
len: 0
102102
}
103103
}
@@ -115,10 +115,10 @@ unsafe impl Sync for LeafNode<(), ()> {}
115115
// ever take a pointer past the first key.
116116
static EMPTY_ROOT_NODE: LeafNode<(), ()> = LeafNode {
117117
parent: ptr::null(),
118-
parent_idx: 0,
118+
parent_idx: MaybeUninit::uninitialized(),
119119
len: 0,
120-
keys: [(); CAPACITY],
121-
vals: [(); CAPACITY],
120+
keys: MaybeUninit::uninitialized(),
121+
vals: MaybeUninit::uninitialized(),
122122
};
123123

124124
/// The underlying representation of internal nodes. As with `LeafNode`s, these should be hidden
@@ -430,7 +430,7 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
430430
root: self.root,
431431
_marker: PhantomData
432432
},
433-
idx: self.as_leaf().parent_idx as usize,
433+
idx: unsafe { usize::from(*self.as_leaf().parent_idx.get_ref()) },
434434
_marker: PhantomData
435435
})
436436
} else {
@@ -567,7 +567,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Immut<'a>, K, V, Type> {
567567
// the node, which is allowed by LLVM.
568568
unsafe {
569569
slice::from_raw_parts(
570-
self.as_leaf().keys.as_ptr(),
570+
self.as_leaf().keys.as_ptr() as *const K,
571571
self.len()
572572
)
573573
}
@@ -578,7 +578,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Immut<'a>, K, V, Type> {
578578
debug_assert!(!self.is_shared_root());
579579
unsafe {
580580
slice::from_raw_parts(
581-
self.as_leaf().vals.as_ptr(),
581+
self.as_leaf().vals.as_ptr() as *const V,
582582
self.len()
583583
)
584584
}
@@ -605,7 +605,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
605605
} else {
606606
unsafe {
607607
slice::from_raw_parts_mut(
608-
&mut self.as_leaf_mut().keys as *mut [K] as *mut K,
608+
self.as_leaf_mut().keys.get_mut() as *mut [K] as *mut K,
609609
self.len()
610610
)
611611
}
@@ -616,7 +616,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
616616
debug_assert!(!self.is_shared_root());
617617
unsafe {
618618
slice::from_raw_parts_mut(
619-
&mut self.as_leaf_mut().vals as *mut [V] as *mut V,
619+
self.as_leaf_mut().vals.get_mut() as *mut [V] as *mut V,
620620
self.len()
621621
)
622622
}
@@ -1013,7 +1013,7 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
10131013
let ptr = self.node.as_internal_mut() as *mut _;
10141014
let mut child = self.descend();
10151015
child.as_leaf_mut().parent = ptr;
1016-
child.as_leaf_mut().parent_idx = idx;
1016+
child.as_leaf_mut().parent_idx.set(idx);
10171017
}
10181018

10191019
/// Unsafely asserts to the compiler some static information about whether the underlying
@@ -1152,12 +1152,12 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, marker::KV>
11521152

11531153
ptr::copy_nonoverlapping(
11541154
self.node.keys().as_ptr().add(self.idx + 1),
1155-
new_node.keys.as_mut_ptr(),
1155+
new_node.keys.as_mut_ptr() as *mut K,
11561156
new_len
11571157
);
11581158
ptr::copy_nonoverlapping(
11591159
self.node.vals().as_ptr().add(self.idx + 1),
1160-
new_node.vals.as_mut_ptr(),
1160+
new_node.vals.as_mut_ptr() as *mut V,
11611161
new_len
11621162
);
11631163

@@ -1210,12 +1210,12 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
12101210

12111211
ptr::copy_nonoverlapping(
12121212
self.node.keys().as_ptr().add(self.idx + 1),
1213-
new_node.data.keys.as_mut_ptr(),
1213+
new_node.data.keys.as_mut_ptr() as *mut K,
12141214
new_len
12151215
);
12161216
ptr::copy_nonoverlapping(
12171217
self.node.vals().as_ptr().add(self.idx + 1),
1218-
new_node.data.vals.as_mut_ptr(),
1218+
new_node.data.vals.as_mut_ptr() as *mut V,
12191219
new_len
12201220
);
12211221
ptr::copy_nonoverlapping(

Diff for: src/liballoc/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@
119119
#![feature(exact_chunks)]
120120
#![feature(rustc_const_unstable)]
121121
#![feature(const_vec_new)]
122+
#![feature(maybe_uninit)]
122123

123124
// Allow testing this library
124125

Diff for: src/libcore/fmt/float.rs

+14-13
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// except according to those terms.
1010

1111
use fmt::{Formatter, Result, LowerExp, UpperExp, Display, Debug};
12-
use mem;
12+
use mem::MaybeUninit;
1313
use num::flt2dec;
1414

1515
// Don't inline this so callers don't use the stack space this function
@@ -20,11 +20,11 @@ fn float_to_decimal_common_exact<T>(fmt: &mut Formatter, num: &T,
2020
where T: flt2dec::DecodableFloat
2121
{
2222
unsafe {
23-
let mut buf: [u8; 1024] = mem::uninitialized(); // enough for f32 and f64
24-
let mut parts: [flt2dec::Part; 4] = mem::uninitialized();
23+
let mut buf = MaybeUninit::<[u8; 1024]>::uninitialized(); // enough for f32 and f64
24+
let mut parts = MaybeUninit::<[flt2dec::Part; 4]>::uninitialized();
2525
let formatted = flt2dec::to_exact_fixed_str(flt2dec::strategy::grisu::format_exact,
2626
*num, sign, precision,
27-
false, &mut buf, &mut parts);
27+
false, buf.get_mut(), parts.get_mut());
2828
fmt.pad_formatted_parts(&formatted)
2929
}
3030
}
@@ -38,10 +38,11 @@ fn float_to_decimal_common_shortest<T>(fmt: &mut Formatter, num: &T,
3838
{
3939
unsafe {
4040
// enough for f32 and f64
41-
let mut buf: [u8; flt2dec::MAX_SIG_DIGITS] = mem::uninitialized();
42-
let mut parts: [flt2dec::Part; 4] = mem::uninitialized();
41+
let mut buf = MaybeUninit::<[u8; flt2dec::MAX_SIG_DIGITS]>::uninitialized();
42+
let mut parts = MaybeUninit::<[flt2dec::Part; 4]>::uninitialized();
4343
let formatted = flt2dec::to_shortest_str(flt2dec::strategy::grisu::format_shortest, *num,
44-
sign, precision, false, &mut buf, &mut parts);
44+
sign, precision, false, buf.get_mut(),
45+
parts.get_mut());
4546
fmt.pad_formatted_parts(&formatted)
4647
}
4748
}
@@ -75,11 +76,11 @@ fn float_to_exponential_common_exact<T>(fmt: &mut Formatter, num: &T,
7576
where T: flt2dec::DecodableFloat
7677
{
7778
unsafe {
78-
let mut buf: [u8; 1024] = mem::uninitialized(); // enough for f32 and f64
79-
let mut parts: [flt2dec::Part; 6] = mem::uninitialized();
79+
let mut buf = MaybeUninit::<[u8; 1024]>::uninitialized(); // enough for f32 and f64
80+
let mut parts = MaybeUninit::<[flt2dec::Part; 6]>::uninitialized();
8081
let formatted = flt2dec::to_exact_exp_str(flt2dec::strategy::grisu::format_exact,
8182
*num, sign, precision,
82-
upper, &mut buf, &mut parts);
83+
upper, buf.get_mut(), parts.get_mut());
8384
fmt.pad_formatted_parts(&formatted)
8485
}
8586
}
@@ -94,11 +95,11 @@ fn float_to_exponential_common_shortest<T>(fmt: &mut Formatter,
9495
{
9596
unsafe {
9697
// enough for f32 and f64
97-
let mut buf: [u8; flt2dec::MAX_SIG_DIGITS] = mem::uninitialized();
98-
let mut parts: [flt2dec::Part; 6] = mem::uninitialized();
98+
let mut buf = MaybeUninit::<[u8; flt2dec::MAX_SIG_DIGITS]>::uninitialized();
99+
let mut parts = MaybeUninit::<[flt2dec::Part; 6]>::uninitialized();
99100
let formatted = flt2dec::to_shortest_exp_str(flt2dec::strategy::grisu::format_shortest,
100101
*num, sign, (0, 0), upper,
101-
&mut buf, &mut parts);
102+
buf.get_mut(), parts.get_mut());
102103
fmt.pad_formatted_parts(&formatted)
103104
}
104105
}

Diff for: src/libcore/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@ macro_rules! test_v512 { ($item:item) => {}; }
246246
#[allow(unused_macros)]
247247
macro_rules! vector_impl { ($([$f:ident, $($args:tt)*]),*) => { $($f!($($args)*);)* } }
248248
#[path = "../stdsimd/coresimd/mod.rs"]
249+
// replacing uses of mem::{uninitialized,zeroed} with MaybeUninit needs to be in the stdsimd repo
250+
#[allow(deprecated)]
249251
#[allow(missing_docs, missing_debug_implementations, dead_code, unused_imports)]
250252
#[unstable(feature = "stdsimd", issue = "48556")]
251253
#[cfg(not(stage0))] // allow changes to how stdsimd works in stage0

Diff for: src/libcore/mem.rs

+96
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,7 @@ pub fn needs_drop<T>() -> bool {
514514
/// assert_eq!(0, x);
515515
/// ```
516516
#[inline]
517+
#[rustc_deprecated(since = "2.0.0", reason = "use `mem::MaybeUninit::zeroed` instead")]
517518
#[stable(feature = "rust1", since = "1.0.0")]
518519
pub unsafe fn zeroed<T>() -> T {
519520
intrinsics::init()
@@ -608,6 +609,7 @@ pub unsafe fn zeroed<T>() -> T {
608609
/// [copy_no]: ../intrinsics/fn.copy_nonoverlapping.html
609610
/// [`Drop`]: ../ops/trait.Drop.html
610611
#[inline]
612+
#[rustc_deprecated(since = "2.0.0", reason = "use `mem::MaybeUninit::uninitialized` instead")]
611613
#[stable(feature = "rust1", since = "1.0.0")]
612614
pub unsafe fn uninitialized<T>() -> T {
613615
intrinsics::uninit()
@@ -1024,3 +1026,97 @@ impl<T: ?Sized> DerefMut for ManuallyDrop<T> {
10241026
&mut self.value
10251027
}
10261028
}
1029+
1030+
/// A newtype to construct uninitialized instances of `T`
1031+
#[allow(missing_debug_implementations)]
1032+
#[unstable(feature = "maybe_uninit", issue = "53491")]
1033+
// NOTE after stabilizing `MaybeUninit` proceed to deprecate `mem::{uninitialized,zeroed}`
1034+
pub union MaybeUninit<T> {
1035+
uninit: (),
1036+
value: ManuallyDrop<T>,
1037+
}
1038+
1039+
impl<T> MaybeUninit<T> {
1040+
/// Create a new `MaybeUninit` in an uninitialized state.
1041+
///
1042+
/// Note that dropping a `MaybeUninit` will never call `T`'s drop code.
1043+
/// It is your responsibility to make sure `T` gets dropped if it got initialized.
1044+
#[unstable(feature = "maybe_uninit", issue = "53491")]
1045+
pub const fn uninitialized() -> MaybeUninit<T> {
1046+
MaybeUninit { uninit: () }
1047+
}
1048+
1049+
/// Create a new `MaybeUninit` in an uninitialized state, with the memory being
1050+
/// filled with `0` bytes. It depends on `T` whether that already makes for
1051+
/// proper initialization. For example, `MaybeUninit<usize>::zeroed()` is initialized,
1052+
/// but `MaybeUninit<&'static i32>::zeroed()` is not because references must not
1053+
/// be null.
1054+
///
1055+
/// Note that dropping a `MaybeUninit` will never call `T`'s drop code.
1056+
/// It is your responsibility to make sure `T` gets dropped if it got initialized.
1057+
#[unstable(feature = "maybe_uninit", issue = "53491")]
1058+
pub fn zeroed() -> MaybeUninit<T> {
1059+
let mut u = MaybeUninit::<T>::uninitialized();
1060+
unsafe {
1061+
u.as_mut_ptr().write_bytes(0u8, 1);
1062+
}
1063+
u
1064+
}
1065+
1066+
/// Set the value of the `MaybeUninit`. This overwrites any previous value without dropping it.
1067+
#[unstable(feature = "maybe_uninit", issue = "53491")]
1068+
pub fn set(&mut self, val: T) {
1069+
unsafe {
1070+
self.value = ManuallyDrop::new(val);
1071+
}
1072+
}
1073+
1074+
/// Extract the value from the `MaybeUninit` container. This is a great way
1075+
/// to ensure that the data will get dropped, because the resulting `T` is
1076+
/// subject to the usual drop handling.
1077+
///
1078+
/// # Unsafety
1079+
///
1080+
/// It is up to the caller to guarantee that the the `MaybeUninit` really is in an initialized
1081+
/// state, otherwise this will immediately cause undefined behavior.
1082+
#[unstable(feature = "maybe_uninit", issue = "53491")]
1083+
pub unsafe fn into_inner(self) -> T {
1084+
ManuallyDrop::into_inner(self.value)
1085+
}
1086+
1087+
/// Get a reference to the contained value.
1088+
///
1089+
/// # Unsafety
1090+
///
1091+
/// It is up to the caller to guarantee that the the `MaybeUninit` really is in an initialized
1092+
/// state, otherwise this will immediately cause undefined behavior.
1093+
#[unstable(feature = "maybe_uninit", issue = "53491")]
1094+
pub unsafe fn get_ref(&self) -> &T {
1095+
&*self.value
1096+
}
1097+
1098+
/// Get a mutable reference to the contained value.
1099+
///
1100+
/// # Unsafety
1101+
///
1102+
/// It is up to the caller to guarantee that the the `MaybeUninit` really is in an initialized
1103+
/// state, otherwise this will immediately cause undefined behavior.
1104+
#[unstable(feature = "maybe_uninit", issue = "53491")]
1105+
pub unsafe fn get_mut(&mut self) -> &mut T {
1106+
&mut *self.value
1107+
}
1108+
1109+
/// Get a pointer to the contained value. Reading from this pointer will be undefined
1110+
/// behavior unless the `MaybeUninit` is initialized.
1111+
#[unstable(feature = "maybe_uninit", issue = "53491")]
1112+
pub fn as_ptr(&self) -> *const T {
1113+
unsafe { &*self.value as *const T }
1114+
}
1115+
1116+
/// Get a mutable pointer to the contained value. Reading from this pointer will be undefined
1117+
/// behavior unless the `MaybeUninit` is initialized.
1118+
#[unstable(feature = "maybe_uninit", issue = "53491")]
1119+
pub fn as_mut_ptr(&mut self) -> *mut T {
1120+
unsafe { &mut *self.value as *mut T }
1121+
}
1122+
}

0 commit comments

Comments
 (0)