diff options
| author | Ralf Jung <post@ralfj.de> | 2019-07-19 10:44:11 +0200 |
|---|---|---|
| committer | Ralf Jung <post@ralfj.de> | 2019-07-19 14:47:56 +0200 |
| commit | e074db764a0f25af073cb3f472d39a86e6fa7f39 (patch) | |
| tree | 6d0263f84567761ea6e65dd81f1c92bd9e7299e3 | |
| parent | fe499a7b34dcb1fc054dd637ea561a19a268d2de (diff) | |
| download | rust-e074db764a0f25af073cb3f472d39a86e6fa7f39.tar.gz rust-e074db764a0f25af073cb3f472d39a86e6fa7f39.zip | |
use const array repeat expressions for uninit_array
| -rw-r--r-- | src/liballoc/collections/btree/node.rs | 6 | ||||
| -rw-r--r-- | src/liballoc/lib.rs | 1 | ||||
| -rw-r--r-- | src/libcore/fmt/num.rs | 4 | ||||
| -rw-r--r-- | src/libcore/lib.rs | 1 | ||||
| -rw-r--r-- | src/libcore/macros.rs | 23 | ||||
| -rw-r--r-- | src/libcore/mem/maybe_uninit.rs | 1 | ||||
| -rw-r--r-- | src/libcore/slice/sort.rs | 4 |
7 files changed, 30 insertions, 10 deletions
diff --git a/src/liballoc/collections/btree/node.rs b/src/liballoc/collections/btree/node.rs index 7cf077d61d6..e067096f0c7 100644 --- a/src/liballoc/collections/btree/node.rs +++ b/src/liballoc/collections/btree/node.rs @@ -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 @@ -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], } } } diff --git a/src/liballoc/lib.rs b/src/liballoc/lib.rs index 2e48825e81c..68df0137ee6 100644 --- a/src/liballoc/lib.rs +++ b/src/liballoc/lib.rs @@ -77,6 +77,7 @@ #![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)] diff --git a/src/libcore/fmt/num.rs b/src/libcore/fmt/num.rs index f9b4c26496c..e2d00e654dd 100644 --- a/src/libcore/fmt/num.rs +++ b/src/libcore/fmt/num.rs @@ -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 = uninit_array![u8; 128]; let mut curr = buf.len(); let base = T::from_u8(Self::BASE); if is_nonnegative { @@ -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 = uninit_array![u8; 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(); diff --git a/src/libcore/lib.rs b/src/libcore/lib.rs index fe149d634e2..8d3c42cbf35 100644 --- a/src/libcore/lib.rs +++ b/src/libcore/lib.rs @@ -75,6 +75,7 @@ #![feature(const_fn)] #![feature(const_fn_union)] #![cfg_attr(not(bootstrap), feature(const_generics))] +#![cfg_attr(not(bootstrap), feature(const_in_array_repeat_expressions))] #![feature(custom_inner_attributes)] #![feature(decl_macro)] #![feature(doc_cfg)] diff --git a/src/libcore/macros.rs b/src/libcore/macros.rs index 293a2dd9492..9855c5fb9c3 100644 --- a/src/libcore/macros.rs +++ b/src/libcore/macros.rs @@ -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!` diff --git a/src/libcore/mem/maybe_uninit.rs b/src/libcore/mem/maybe_uninit.rs index f6f7ccffdb0..1c69e7f90f6 100644 --- a/src/libcore/mem/maybe_uninit.rs +++ b/src/libcore/mem/maybe_uninit.rs @@ -248,6 +248,7 @@ impl<T> MaybeUninit<T> { /// [type]: union.MaybeUninit.html #[stable(feature = "maybe_uninit", since = "1.36.0")] #[inline(always)] + #[rustc_promotable] pub const fn uninit() -> MaybeUninit<T> { MaybeUninit { uninit: () } } diff --git a/src/libcore/slice/sort.rs b/src/libcore/slice/sort.rs index c293b190018..fbf9caeaece 100644 --- a/src/libcore/slice/sort.rs +++ b/src/libcore/slice/sort.rs @@ -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>; BLOCK] = uninit_array![u8; 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>; BLOCK] = uninit_array![u8; 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. |
