Skip to content

Commit e074db7

Browse files
committed
use const array repeat expressions for uninit_array
1 parent fe499a7 commit e074db7

File tree

7 files changed

+30
-10
lines changed

7 files changed

+30
-10
lines changed

src/liballoc/collections/btree/node.rs

Lines changed: 3 additions & 3 deletions
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
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)]

src/libcore/fmt/num.rs

Lines changed: 2 additions & 2 deletions
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 = uninit_array![u8; 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 = uninit_array![u8; 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/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
#![feature(const_fn)]
7676
#![feature(const_fn_union)]
7777
#![cfg_attr(not(bootstrap), feature(const_generics))]
78+
#![cfg_attr(not(bootstrap), feature(const_in_array_repeat_expressions))]
7879
#![feature(custom_inner_attributes)]
7980
#![feature(decl_macro)]
8081
#![feature(doc_cfg)]

src/libcore/macros.rs

Lines changed: 20 additions & 3 deletions
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ impl<T> MaybeUninit<T> {
248248
/// [type]: union.MaybeUninit.html
249249
#[stable(feature = "maybe_uninit", since = "1.36.0")]
250250
#[inline(always)]
251+
#[rustc_promotable]
251252
pub const fn uninit() -> MaybeUninit<T> {
252253
MaybeUninit { uninit: () }
253254
}

src/libcore/slice/sort.rs

Lines changed: 2 additions & 2 deletions
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>; BLOCK] = uninit_array![u8; 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>; BLOCK] = uninit_array![u8; 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)