diff --git a/doc/toolchain_limitations.md b/doc/toolchain_limitations.md index 8ea03929a8..65341c0160 100644 --- a/doc/toolchain_limitations.md +++ b/doc/toolchain_limitations.md @@ -366,28 +366,6 @@ const _: () = { b"".iter(); }; ``` -### `[tag:const_array_assume_init]` `MaybeUninit::array_assume_init` is not `const fn` - -```rust -#![feature(maybe_uninit_array_assume_init)] -use core::mem::MaybeUninit; -assert!(matches!( - unsafe { MaybeUninit::array_assume_init([MaybeUninit::new(42)]) }, - [42] -)); -``` - -```rust,compile_fail,E0015 -#![feature(maybe_uninit_array_assume_init)] -use core::mem::MaybeUninit; -const _: () = assert!(matches!( - // error[E0015]: cannot call non-const fn `MaybeUninit:::: - // array_assume_init::<1_usize>` in constants - unsafe { MaybeUninit::array_assume_init([MaybeUninit::new(42)]) }, - [42] -)); -``` - ### `[tag:const_uninit_array]` `MaybeUninit::uninit_array` is unstable ```rust,compile_fail,E0658 diff --git a/src/r3_core/src/lib.rs b/src/r3_core/src/lib.rs index f5f73c178c..eb3eea0821 100644 --- a/src/r3_core/src/lib.rs +++ b/src/r3_core/src/lib.rs @@ -1,7 +1,9 @@ +#![feature(const_maybe_uninit_array_assume_init)] #![feature(const_fn_floating_point_arithmetic)] #![feature(const_nonnull_slice_from_raw_parts)] #![feature(const_maybe_uninit_uninit_array)] #![feature(const_maybe_uninit_assume_init)] +#![feature(maybe_uninit_array_assume_init)] #![feature(const_maybe_uninit_as_mut_ptr)] #![feature(nonnull_slice_from_raw_parts)] #![feature(arbitrary_enum_discriminant)] diff --git a/src/r3_core/src/utils/for_times.rs b/src/r3_core/src/utils/for_times.rs index d9034a8ef0..94c61145fb 100644 --- a/src/r3_core/src/utils/for_times.rs +++ b/src/r3_core/src/utils/for_times.rs @@ -232,21 +232,8 @@ macro_rules! const_array_from_fn { )) } - // `MaybeUninit::array_assume_init` is not `const fn` yet - // [ref:const_array_assume_init] - const unsafe fn __assume_init< - $($iter_gparam $($iter_gparam_bounds)*,)* - const LEN: usize - >(array: [MaybeUninit<$ty>; LEN]) -> [$ty; LEN] { - // Safety: This is equivalent to `transmute_copy(&array)`. The - // memory layout of `[MaybeUninit; $len]` is identical to `[T; $len]`. - // We initialized all elements in `array[0..$len]`, so it's safe to - // reinterpret that range as `[T; $len]`. - unsafe { *(array.as_ptr() as *const _ as *const [$ty; LEN]) } - } - - // Safety: See the body of `__assume_init`. - unsafe { __assume_init::<$($ctx_t,)* {$len_value}>(array) } + // Safety: All elements of `array` are initialized + unsafe { MaybeUninit::array_assume_init(array) } }}; } diff --git a/src/r3_core/src/utils/init.rs b/src/r3_core/src/utils/init.rs index 77638585f3..6ed72b3ec6 100644 --- a/src/r3_core/src/utils/init.rs +++ b/src/r3_core/src/utils/init.rs @@ -44,12 +44,8 @@ impl Init for [T; LEN] { i += 1; } - // `MaybeUninit::array_assume_init` is not `const fn` yet - // [ref:const_array_assume_init] - // Safety: The memory layout of `[MaybeUninit; LEN]` is - // identical to `[T; LEN]`. We initialized all elements, so it's - // safe to reinterpret that range as `[T; LEN]`. - unsafe { super::mem::transmute(array) } + // Safety: `array`'s elements are fully initialized + unsafe { mem::MaybeUninit::array_assume_init(array) } }; } diff --git a/src/r3_core/src/utils/mem.rs b/src/r3_core/src/utils/mem.rs index 23d98330d3..c20b802aa3 100644 --- a/src/r3_core/src/utils/mem.rs +++ b/src/r3_core/src/utils/mem.rs @@ -44,9 +44,7 @@ mod tests { MaybeUninit::new(2), MaybeUninit::new(3), ]; - // `MaybeUninit::array_assume_init` is not `const fn` - // [ref:const_array_assume_init] - unsafe { transmute(array) } + unsafe { MaybeUninit::array_assume_init(array) } }; assert_eq!(ARRAY1, [1, 2, 3]); } diff --git a/src/r3_kernel/src/lib.rs b/src/r3_kernel/src/lib.rs index 933b7dca09..923984cf21 100644 --- a/src/r3_kernel/src/lib.rs +++ b/src/r3_kernel/src/lib.rs @@ -1,5 +1,7 @@ +#![feature(const_maybe_uninit_array_assume_init)] #![feature(const_maybe_uninit_uninit_array)] #![feature(const_maybe_uninit_assume_init)] +#![feature(maybe_uninit_array_assume_init)] #![feature(const_slice_from_raw_parts)] #![feature(maybe_uninit_uninit_array)] #![feature(const_precise_live_drops)] diff --git a/src/r3_test_suite/src/kernel_benchmarks/timer_start.rs b/src/r3_test_suite/src/kernel_benchmarks/timer_start.rs index f3aafec40b..403a92df1f 100644 --- a/src/r3_test_suite/src/kernel_benchmarks/timer_start.rs +++ b/src/r3_test_suite/src/kernel_benchmarks/timer_start.rs @@ -49,9 +49,7 @@ impl AppInner { i += 1; } - // `MaybeUninit::array_assume_init` is not `const fn` yet - // [ref:const_array_assume_init] - unsafe { core::mem::transmute_copy(&timers) } + unsafe { MaybeUninit::array_assume_init(timers) } }; Self { timers } diff --git a/src/r3_test_suite/src/lib.rs b/src/r3_test_suite/src/lib.rs index c40a923792..62c0d6bab1 100644 --- a/src/r3_test_suite/src/lib.rs +++ b/src/r3_test_suite/src/lib.rs @@ -1,4 +1,6 @@ +#![feature(const_maybe_uninit_array_assume_init)] #![feature(const_fn_floating_point_arithmetic)] +#![feature(maybe_uninit_array_assume_init)] #![feature(cfg_target_has_atomic)] #![feature(const_transmute_copy)] #![feature(const_refs_to_cell)]