Skip to content

Commit

Permalink
Rollup merge of rust-lang#62799 - RalfJung:uninit-array, r=Centril
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Centril committed Jul 22, 2019
2 parents 7dafdd2 + f3abbf7 commit 5a20745
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/liballoc/collections/btree/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ impl<K, V> LeafNode<K, V> {
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: uninitialized_array![_; CAPACITY],
vals: uninitialized_array![_; CAPACITY],
keys: uninit_array![_; CAPACITY],
vals: uninit_array![_; CAPACITY],
parent: ptr::null(),
parent_idx: MaybeUninit::uninit(),
len: 0
Expand Down Expand Up @@ -159,7 +159,7 @@ impl<K, V> InternalNode<K, V> {
unsafe fn new() -> Self {
InternalNode {
data: LeafNode::new(),
edges: uninitialized_array![_; 2*B],
edges: uninit_array![_; 2*B],
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/liballoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,15 @@
#![feature(box_syntax)]
#![feature(cfg_target_has_atomic)]
#![feature(coerce_unsized)]
#![cfg_attr(not(bootstrap), feature(const_in_array_repeat_expressions))]
#![feature(dispatch_from_dyn)]
#![feature(core_intrinsics)]
#![feature(dropck_eyepatch)]
#![feature(exact_size_is_empty)]
#![feature(fmt_internals)]
#![feature(fn_traits)]
#![feature(fundamental)]
#![feature(internal_uninit_const)]
#![feature(lang_items)]
#![feature(libc)]
#![feature(nll)]
Expand Down
4 changes: 2 additions & 2 deletions src/libcore/fmt/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ trait GenericRadix {
// characters for a base 2 number.
let zero = T::zero();
let is_nonnegative = x >= zero;
let mut buf = uninitialized_array![u8; 128];
let mut buf = [MaybeUninit::<u8>::uninit(); 128];
let mut curr = buf.len();
let base = T::from_u8(Self::BASE);
if is_nonnegative {
Expand Down Expand Up @@ -189,7 +189,7 @@ static DEC_DIGITS_LUT: &[u8; 200] =
macro_rules! impl_Display {
($($t:ident),* as $u:ident via $conv_fn:ident named $name:ident) => {
fn $name(mut n: $u, is_nonnegative: bool, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut buf = uninitialized_array![u8; 39];
let mut buf = [MaybeUninit::<u8>::uninit(); 39];
let mut curr = buf.len() as isize;
let buf_ptr = MaybeUninit::first_ptr_mut(&mut buf);
let lut_ptr = DEC_DIGITS_LUT.as_ptr();
Expand Down
23 changes: 20 additions & 3 deletions src/libcore/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -626,20 +626,37 @@ macro_rules! todo {
/// Creates an array of [`MaybeUninit`].
///
/// This macro constructs an uninitialized array of the type `[MaybeUninit<K>; N]`.
/// It exists solely because bootstrap does not yet support const array-init expressions.
///
/// [`MaybeUninit`]: mem/union.MaybeUninit.html
// FIXME: Remove both versions of this macro once bootstrap is 1.38.
#[macro_export]
#[unstable(feature = "maybe_uninit_array", issue = "53491")]
macro_rules! uninitialized_array {
#[cfg(bootstrap)]
macro_rules! uninit_array {
// This `assume_init` is safe because an array of `MaybeUninit` does not
// require initialization.
// FIXME(#49147): Could be replaced by an array initializer, once those can
// be any const expression.
($t:ty; $size:expr) => (unsafe {
MaybeUninit::<[MaybeUninit<$t>; $size]>::uninit().assume_init()
});
}

/// Creates an array of [`MaybeUninit`].
///
/// This macro constructs an uninitialized array of the type `[MaybeUninit<K>; N]`.
/// It exists solely because bootstrap does not yet support const array-init expressions.
///
/// [`MaybeUninit`]: mem/union.MaybeUninit.html
// FIXME: Just inline this version of the macro once bootstrap is 1.38.
#[macro_export]
#[unstable(feature = "maybe_uninit_array", issue = "53491")]
#[cfg(not(bootstrap))]
macro_rules! uninit_array {
($t:ty; $size:expr) => (
[MaybeUninit::<$t>::UNINIT; $size]
);
}

/// Built-in macros to the compiler itself.
///
/// These macros do not have any corresponding definition with a `macro_rules!`
Expand Down
5 changes: 5 additions & 0 deletions src/libcore/mem/maybe_uninit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,11 @@ impl<T> MaybeUninit<T> {
MaybeUninit { uninit: () }
}

/// A promotable constant, equivalent to `uninit()`.
#[unstable(feature = "internal_uninit_const", issue = "0",
reason = "hack to work around promotability")]
pub const UNINIT: Self = Self::uninit();

/// Creates a new `MaybeUninit<T>` 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<usize>::zeroed()` is initialized,
Expand Down
4 changes: 2 additions & 2 deletions src/libcore/slice/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,14 +216,14 @@ fn partition_in_blocks<T, F>(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: [MaybeUninit<u8>; BLOCK] = uninitialized_array![u8; BLOCK];
let mut offsets_l = [MaybeUninit::<u8>::uninit(); BLOCK];

// 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: [MaybeUninit<u8>; BLOCK] = uninitialized_array![u8; BLOCK];
let mut offsets_r = [MaybeUninit::<u8>::uninit(); BLOCK];

// 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.
Expand Down

0 comments on commit 5a20745

Please sign in to comment.