Skip to content

Commit 5a20745

Browse files
authored
Rollup merge of rust-lang#62799 - RalfJung:uninit-array, r=Centril
use const array repeat expressions for uninit_array With a first implementation of rust-lang#49147 having landed, we can make this macro nicer and phase it out with the next bootstrap bump. However, to make this work, we have to mark `MaybeUninit::uninit()` as promotable. I do feel uneasy about promoting stuff involving uninitialized memory, but OTOH no *operation* on `MaybeUninit` is promotable, so maybe this is okay? r? @oli-obk @eddyb
2 parents 7dafdd2 + f3abbf7 commit 5a20745

File tree

6 files changed

+34
-10
lines changed

6 files changed

+34
-10
lines changed

src/liballoc/collections/btree/node.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ impl<K, V> LeafNode<K, V> {
106106
LeafNode {
107107
// As a general policy, we leave fields uninitialized if they can be, as this should
108108
// be both slightly faster and easier to track in Valgrind.
109-
keys: uninitialized_array![_; CAPACITY],
110-
vals: uninitialized_array![_; CAPACITY],
109+
keys: uninit_array![_; CAPACITY],
110+
vals: uninit_array![_; CAPACITY],
111111
parent: ptr::null(),
112112
parent_idx: MaybeUninit::uninit(),
113113
len: 0
@@ -159,7 +159,7 @@ impl<K, V> InternalNode<K, V> {
159159
unsafe fn new() -> Self {
160160
InternalNode {
161161
data: LeafNode::new(),
162-
edges: uninitialized_array![_; 2*B],
162+
edges: uninit_array![_; 2*B],
163163
}
164164
}
165165
}

src/liballoc/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,15 @@
7777
#![feature(box_syntax)]
7878
#![feature(cfg_target_has_atomic)]
7979
#![feature(coerce_unsized)]
80+
#![cfg_attr(not(bootstrap), feature(const_in_array_repeat_expressions))]
8081
#![feature(dispatch_from_dyn)]
8182
#![feature(core_intrinsics)]
8283
#![feature(dropck_eyepatch)]
8384
#![feature(exact_size_is_empty)]
8485
#![feature(fmt_internals)]
8586
#![feature(fn_traits)]
8687
#![feature(fundamental)]
88+
#![feature(internal_uninit_const)]
8789
#![feature(lang_items)]
8890
#![feature(libc)]
8991
#![feature(nll)]

src/libcore/fmt/num.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ trait GenericRadix {
5151
// characters for a base 2 number.
5252
let zero = T::zero();
5353
let is_nonnegative = x >= zero;
54-
let mut buf = uninitialized_array![u8; 128];
54+
let mut buf = [MaybeUninit::<u8>::uninit(); 128];
5555
let mut curr = buf.len();
5656
let base = T::from_u8(Self::BASE);
5757
if is_nonnegative {
@@ -189,7 +189,7 @@ static DEC_DIGITS_LUT: &[u8; 200] =
189189
macro_rules! impl_Display {
190190
($($t:ident),* as $u:ident via $conv_fn:ident named $name:ident) => {
191191
fn $name(mut n: $u, is_nonnegative: bool, f: &mut fmt::Formatter<'_>) -> fmt::Result {
192-
let mut buf = uninitialized_array![u8; 39];
192+
let mut buf = [MaybeUninit::<u8>::uninit(); 39];
193193
let mut curr = buf.len() as isize;
194194
let buf_ptr = MaybeUninit::first_ptr_mut(&mut buf);
195195
let lut_ptr = DEC_DIGITS_LUT.as_ptr();

src/libcore/macros.rs

+20-3
Original file line numberDiff line numberDiff line change
@@ -626,20 +626,37 @@ macro_rules! todo {
626626
/// Creates an array of [`MaybeUninit`].
627627
///
628628
/// This macro constructs an uninitialized array of the type `[MaybeUninit<K>; N]`.
629+
/// It exists solely because bootstrap does not yet support const array-init expressions.
629630
///
630631
/// [`MaybeUninit`]: mem/union.MaybeUninit.html
632+
// FIXME: Remove both versions of this macro once bootstrap is 1.38.
631633
#[macro_export]
632634
#[unstable(feature = "maybe_uninit_array", issue = "53491")]
633-
macro_rules! uninitialized_array {
635+
#[cfg(bootstrap)]
636+
macro_rules! uninit_array {
634637
// This `assume_init` is safe because an array of `MaybeUninit` does not
635638
// require initialization.
636-
// FIXME(#49147): Could be replaced by an array initializer, once those can
637-
// be any const expression.
638639
($t:ty; $size:expr) => (unsafe {
639640
MaybeUninit::<[MaybeUninit<$t>; $size]>::uninit().assume_init()
640641
});
641642
}
642643

644+
/// Creates an array of [`MaybeUninit`].
645+
///
646+
/// This macro constructs an uninitialized array of the type `[MaybeUninit<K>; N]`.
647+
/// It exists solely because bootstrap does not yet support const array-init expressions.
648+
///
649+
/// [`MaybeUninit`]: mem/union.MaybeUninit.html
650+
// FIXME: Just inline this version of the macro once bootstrap is 1.38.
651+
#[macro_export]
652+
#[unstable(feature = "maybe_uninit_array", issue = "53491")]
653+
#[cfg(not(bootstrap))]
654+
macro_rules! uninit_array {
655+
($t:ty; $size:expr) => (
656+
[MaybeUninit::<$t>::UNINIT; $size]
657+
);
658+
}
659+
643660
/// Built-in macros to the compiler itself.
644661
///
645662
/// These macros do not have any corresponding definition with a `macro_rules!`

src/libcore/mem/maybe_uninit.rs

+5
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,11 @@ impl<T> MaybeUninit<T> {
252252
MaybeUninit { uninit: () }
253253
}
254254

255+
/// A promotable constant, equivalent to `uninit()`.
256+
#[unstable(feature = "internal_uninit_const", issue = "0",
257+
reason = "hack to work around promotability")]
258+
pub const UNINIT: Self = Self::uninit();
259+
255260
/// Creates a new `MaybeUninit<T>` in an uninitialized state, with the memory being
256261
/// filled with `0` bytes. It depends on `T` whether that already makes for
257262
/// proper initialization. For example, `MaybeUninit<usize>::zeroed()` is initialized,

src/libcore/slice/sort.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -216,14 +216,14 @@ fn partition_in_blocks<T, F>(v: &mut [T], pivot: &T, is_less: &mut F) -> usize
216216
let mut block_l = BLOCK;
217217
let mut start_l = ptr::null_mut();
218218
let mut end_l = ptr::null_mut();
219-
let mut offsets_l: [MaybeUninit<u8>; BLOCK] = uninitialized_array![u8; BLOCK];
219+
let mut offsets_l = [MaybeUninit::<u8>::uninit(); BLOCK];
220220

221221
// The current block on the right side (from `r.sub(block_r)` to `r`).
222222
let mut r = unsafe { l.add(v.len()) };
223223
let mut block_r = BLOCK;
224224
let mut start_r = ptr::null_mut();
225225
let mut end_r = ptr::null_mut();
226-
let mut offsets_r: [MaybeUninit<u8>; BLOCK] = uninitialized_array![u8; BLOCK];
226+
let mut offsets_r = [MaybeUninit::<u8>::uninit(); BLOCK];
227227

228228
// FIXME: When we get VLAs, try creating one array of length `min(v.len(), 2 * BLOCK)` rather
229229
// than two fixed-size arrays of length `BLOCK`. VLAs might be more cache-efficient.

0 commit comments

Comments
 (0)