Skip to content

Commit d66d230

Browse files
committed
const_deallocate: Don't deallocate memory allocated in an another const. Does nothing at runtime.
`const_allocate`: Returns a null pointer at runtime.
1 parent a492e33 commit d66d230

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

core/src/intrinsics.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1914,12 +1914,13 @@ extern "rust-intrinsic" {
19141914
#[rustc_const_unstable(feature = "const_raw_ptr_comparison", issue = "53020")]
19151915
pub fn ptr_guaranteed_ne<T>(ptr: *const T, other: *const T) -> bool;
19161916

1917-
/// Allocate at compile time. Should not be called at runtime.
1917+
/// Allocate at compile time.
1918+
/// Returns a null pointer at runtime.
19181919
#[rustc_const_unstable(feature = "const_heap", issue = "79597")]
19191920
pub fn const_allocate(size: usize, align: usize) -> *mut u8;
19201921

19211922
/// Deallocate a memory which allocated by `intrinsics::const_allocate` at compile time.
1922-
/// Should not be called at runtime.
1923+
/// Does nothing at runtime.
19231924
#[rustc_const_unstable(feature = "const_heap", issue = "79597")]
19241925
#[cfg(not(bootstrap))]
19251926
pub fn const_deallocate(ptr: *mut u8, size: usize, align: usize);

core/tests/intrinsics.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,16 @@ fn test_hints_in_const_contexts() {
8080
assert!(42u32 == core::hint::black_box(42u32));
8181
}
8282
}
83+
84+
#[cfg(not(bootstrap))]
85+
#[test]
86+
fn test_const_dealocate_at_runtime() {
87+
use core::intrinsics::const_deallocate;
88+
const X: &u32 = &42u32;
89+
let x = &0u32;
90+
unsafe {
91+
const_deallocate(X as *const _ as *mut u8, 4, 4); // nop
92+
const_deallocate(x as *const _ as *mut u8, 4, 4); // nop
93+
const_deallocate(core::ptr::null_mut(), 1, 1); // nop
94+
}
95+
}

core/tests/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#![feature(const_bool_to_option)]
1414
#![feature(const_cell_into_inner)]
1515
#![feature(const_convert)]
16+
#![feature(const_heap)]
1617
#![feature(const_maybe_uninit_as_mut_ptr)]
1718
#![feature(const_maybe_uninit_assume_init)]
1819
#![feature(const_maybe_uninit_assume_init_read)]

0 commit comments

Comments
 (0)