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/rust-toolchain b/rust-toolchain index b5eabc5352..e6c378230a 100644 --- a/rust-toolchain +++ b/rust-toolchain @@ -1 +1 @@ -nightly-2022-03-30 +nightly-2022-05-20 diff --git a/src/r3_core/src/bind.rs b/src/r3_core/src/bind.rs index f7b6a1d762..a0b0bd2d6a 100644 --- a/src/r3_core/src/bind.rs +++ b/src/r3_core/src/bind.rs @@ -1216,7 +1216,8 @@ macro_rules! impl_fn_bind { type BoundFn where $( $RuntimeBinderI: RuntimeBinder, )* - T: Copy + Send + 'static, + T: for<'call> FnOnce($( $RuntimeBinderI::Target<'call>, )*) + -> Output + Copy + Send + 'static, = impl FnOnce() -> Output + Copy + Send + 'static; const fn bind_inner< @@ -1345,8 +1346,8 @@ where type MappedBoundFn where - InnerBoundFn: Copy + Send + 'static, - Mapper: Copy + Send + 'static, + InnerBoundFn: FnOnce() -> Output + Copy + Send + 'static, + Mapper: FnOnce(Output) -> NewOutput + Copy + Send + 'static, = impl FnOnce() -> NewOutput + Copy + Send + 'static; const fn map_bind_inner( diff --git a/src/r3_core/src/lib.rs b/src/r3_core/src/lib.rs index eb1cc196e7..eb3eea0821 100644 --- a/src/r3_core/src/lib.rs +++ b/src/r3_core/src/lib.rs @@ -1,6 +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/time/duration.rs b/src/r3_core/src/time/duration.rs index e5c5292179..b97ea85d3d 100644 --- a/src/r3_core/src/time/duration.rs +++ b/src/r3_core/src/time/duration.rs @@ -222,7 +222,7 @@ impl TryFrom for core::time::Duration { impl fmt::Debug for Duration { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let abs_dur = core::time::Duration::from_micros((self.micros as i64).abs() as u64); + let abs_dur = core::time::Duration::from_micros(self.micros.unsigned_abs().into()); if self.micros < 0 { write!(f, "-")?; } 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 f27ad1974a..923984cf21 100644 --- a/src/r3_kernel/src/lib.rs +++ b/src/r3_kernel/src/lib.rs @@ -1,4 +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_port_riscv/CHANGELOG.md b/src/r3_port_riscv/CHANGELOG.md index 30b9ca8d9d..95cf614745 100644 --- a/src/r3_port_riscv/CHANGELOG.md +++ b/src/r3_port_riscv/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### Changed - **Breaking (semver-exempt):** Change the target compiler version to `nightly-2022-03-30` +- **Breaking:** `use_rt!` is now gated behind `riscv-rt` Cargo feature. ### Fixed diff --git a/src/r3_port_riscv/Cargo.toml b/src/r3_port_riscv/Cargo.toml index 1c0604cb69..f3e1146889 100644 --- a/src/r3_port_riscv/Cargo.toml +++ b/src/r3_port_riscv/Cargo.toml @@ -27,7 +27,7 @@ unstringify = { version = "0.1.4" } seq-macro = { version = "0.3.0" } svgbobdoc = { version = "0.3.0" } macropol = { version = "0.1.3" } -riscv-rt = { version = ">= 0.6.0, < 0.9.0" } +riscv-rt = { version = ">= 0.6.0, < 0.9.0", optional = true } riscv = { version = ">= 0.5.0, < 0.8.0" } [package.metadata.docs.rs] diff --git a/src/r3_port_riscv/src/lib.md b/src/r3_port_riscv/src/lib.md index 895d8473a0..ddf4b0213c 100644 --- a/src/r3_port_riscv/src/lib.md +++ b/src/r3_port_riscv/src/lib.md @@ -2,7 +2,7 @@ The RISC-V port for [the R3 kernel](::r3_kernel). # Startup code -[`use_rt!`] hooks up the entry points ([`EntryPoint`]) using `#[`[`::riscv_rt::entry`]`]`. If this is not desirable for some reason, you can opt not to use it and call the entry points in other ways. +[`use_rt!`] hooks up the entry points ([`EntryPoint`]) using `#[`[`::riscv_rt::entry`]`]` (requires the **`riscv-rt`** Cargo feature). If this is not desirable for some reason, you can opt not to use it and call the entry points in other ways. # Interrupts diff --git a/src/r3_port_riscv/src/lib.rs b/src/r3_port_riscv/src/lib.rs index 4f07523c27..5abe508e23 100644 --- a/src/r3_port_riscv/src/lib.rs +++ b/src/r3_port_riscv/src/lib.rs @@ -9,6 +9,7 @@ #![feature(raw_ref_op)] #![feature(asm_const)] #![feature(asm_sym)] +#![feature(doc_cfg)] #![deny(unsafe_op_in_unsafe_fn)] #![cfg_attr( feature = "doc", @@ -55,6 +56,8 @@ pub mod plic { /// The binding for [`::riscv_rt`]. #[doc(hidden)] +#[cfg(feature = "riscv-rt")] +#[doc(cfg(feature = "riscv-rt"))] pub mod rt { pub mod cfg; #[cfg(target_os = "none")] @@ -85,6 +88,7 @@ pub mod sbi_timer { pub use self::mtime::cfg::*; pub use self::plic::cfg::*; +#[cfg(feature = "riscv-rt")] pub use self::rt::cfg::*; pub use self::sbi_timer::cfg::*; pub use self::threading::cfg::*; diff --git a/src/r3_port_riscv_test_driver/Cargo.toml b/src/r3_port_riscv_test_driver/Cargo.toml index e154b94d22..949b750116 100644 --- a/src/r3_port_riscv_test_driver/Cargo.toml +++ b/src/r3_port_riscv_test_driver/Cargo.toml @@ -24,6 +24,7 @@ run = [ boot-minimal-s = [] # Use `riscv-rt` for the startup code boot-rt = [ + "r3_port_riscv/riscv-rt", "riscv-rt", ] diff --git a/src/r3_portkit/src/sym.rs b/src/r3_portkit/src/sym.rs index b90ff341cd..d02bf1b755 100644 --- a/src/r3_portkit/src/sym.rs +++ b/src/r3_portkit/src/sym.rs @@ -147,7 +147,7 @@ pub macro sym_static { .dc.a {2} ", sym Self::$sym_name, - const $crate::sym::mem::align_of::<&'static $ty>().trailing_zeros(), + const $crate::sym::mem::align_of::<*const $ty>().trailing_zeros(), sym $static, options(noreturn), ); @@ -170,7 +170,7 @@ mod tests { static S1: u32 = 1; static S2: u32 = 2; - impl Tr for &'static u8 { + impl Tr for u8 { sym_static!( #[sym(p_var)] fn var() -> &u32 { @@ -178,7 +178,7 @@ mod tests { } ); } - impl Tr for &'static u16 { + impl Tr for u16 { sym_static!( #[sym(p_var)] fn var() -> &u32 { @@ -186,7 +186,7 @@ mod tests { } ); } - impl Tr for &'static u32 { + impl Tr for u32 { sym_static!( #[sym(p_var)] fn var() -> &u32 { @@ -197,9 +197,9 @@ mod tests { #[test] fn uniqueness() { - let var1 = dbg!(<&'static u8>::var() as *const u32); - let var2 = dbg!(<&'static u16>::var() as *const u32); - let var3 = dbg!(<&'static u32>::var() as *const u32); + let var1 = dbg!(::var() as *const u32); + let var2 = dbg!(::var() as *const u32); + let var3 = dbg!(::var() as *const u32); assert_ne!(var1, var2); assert_ne!(var2, var3); assert_ne!(var1, var3); 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)]