Skip to content

Commit 335790a

Browse files
committed
fix(core): const_allocate now handles the runtime case by returning a null pointer
The change to `const_allocate`'s behavior was introduced by [rust-lang/rust#92274][1]. This means two things: For one, we no longer need `const_eval_select` to keep the compiler from panicking. For two, it can now return a null pointer, for which we must be prepared. [1]: rust-lang/rust#92274
1 parent 3b2f3a0 commit 335790a

File tree

4 files changed

+13
-22
lines changed

4 files changed

+13
-22
lines changed

src/r3_core/src/closure.rs

+12
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,14 @@ impl Closure {
116116
/// C1.call();
117117
/// C2.call();
118118
/// ```
119+
///
120+
/// Don't call it at runtime:
121+
///
122+
/// ```rust,should_panic
123+
/// use r3_core::closure::Closure;
124+
/// let x = [1, 2, 3];
125+
/// Closure::from_fn_const(move || { let _x = x; });
126+
/// ```
119127
pub const fn from_fn_const<T: FnOnce() + Copy + Send + 'static>(func: T) -> Self {
120128
let size = size_of::<T>();
121129
let align = align_of::<T>();
@@ -128,6 +136,10 @@ impl Closure {
128136
Self::from_raw_parts(trampoline_zst::<T>, ClosureEnv(None))
129137
} else {
130138
let env = core::intrinsics::const_allocate(size, align);
139+
assert!(
140+
!env.guaranteed_eq(core::ptr::null_mut()),
141+
"heap allocation failed"
142+
);
131143
env.cast::<T>().write(func);
132144
Self::from_raw_parts(trampoline_indirect::<T>, transmute(env))
133145
}

src/r3_core/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#![feature(const_raw_ptr_comparison)]
1414
#![feature(const_ptr_offset_from)]
1515
#![feature(maybe_uninit_slice)]
16-
#![feature(const_eval_select)]
1716
#![feature(const_mut_refs)]
1817
#![feature(const_slice_from_raw_parts)]
1918
#![feature(const_option)]

src/r3_core/src/utils/alloc/allocator.rs

+1-20
Original file line numberDiff line numberDiff line change
@@ -462,17 +462,7 @@ unsafe impl const rlsf::FlexSource for ConstFlexSource {
462462

463463
assert!(min_size != 0);
464464

465-
// FIXME: Directly calling `const_allocate` from here causes the
466-
// compiler to panic
467-
// Safety: `const_allocate_{in_const, at_rt}` behave observably
468-
// equivalent... if their results are ever observed.
469-
let ptr = unsafe {
470-
core::intrinsics::const_eval_select(
471-
(size, BLOCK_SIZE),
472-
const_allocate_in_const,
473-
const_allocate_at_rt,
474-
)
475-
};
465+
let ptr = unsafe { core::intrinsics::const_allocate(size, BLOCK_SIZE) };
476466

477467
// FIXME: `NonNull::new` is not `const fn` yet
478468
assert!(!ptr.guaranteed_eq(core::ptr::null_mut()));
@@ -485,12 +475,3 @@ unsafe impl const rlsf::FlexSource for ConstFlexSource {
485475
BLOCK_SIZE
486476
}
487477
}
488-
489-
const fn const_allocate_in_const(size: usize, align: usize) -> *mut u8 {
490-
// Safety: Technically it's not `unsafe`
491-
unsafe { core::intrinsics::const_allocate(size, align) }
492-
}
493-
494-
fn const_allocate_at_rt(_: usize, _: usize) -> *mut u8 {
495-
loop {}
496-
}

src/r3_kernel/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#![feature(generic_const_exprs)]
1111
#![feature(const_refs_to_cell)]
1212
#![feature(maybe_uninit_slice)]
13-
#![feature(const_eval_select)]
1413
#![feature(const_option_ext)]
1514
#![feature(const_ptr_as_ref)]
1615
#![feature(const_ptr_offset)]

0 commit comments

Comments
 (0)