Skip to content

Commit 9728cc4

Browse files
committed
Document about some behaviors of const_(de)allocate and add some tests.
1 parent 7a7144f commit 9728cc4

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed

library/core/src/intrinsics.rs

+18-4
Original file line numberDiff line numberDiff line change
@@ -1914,13 +1914,27 @@ 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.
1918-
/// Returns a null pointer at runtime.
1917+
/// Allocates a block of memory at compile time.
1918+
/// At runtime, just returns a null pointer.
1919+
///
1920+
/// # Safety
1921+
///
1922+
/// - The `align` argument must be a power of two.
1923+
/// - At compile time, a compile error occurs if this constraint is violated.
1924+
/// - At runtime, it is not checked.
19191925
#[rustc_const_unstable(feature = "const_heap", issue = "79597")]
19201926
pub fn const_allocate(size: usize, align: usize) -> *mut u8;
19211927

1922-
/// Deallocate a memory which allocated by `intrinsics::const_allocate` at compile time.
1923-
/// Does nothing at runtime.
1928+
/// Deallocates a memory which allocated by `intrinsics::const_allocate` at compile time.
1929+
/// At runtime, does nothing.
1930+
///
1931+
/// # Safety
1932+
///
1933+
/// - The `align` argument must be a power of two.
1934+
/// - At compile time, a compile error occurs if this constraint is violated.
1935+
/// - At runtime, it is not checked.
1936+
/// - If the `ptr` is created in an another const, this intrinsic doesn't deallocate it.
1937+
/// - If the `ptr` is pointing to a local variable, this intrinsic doesn't deallocate it.
19241938
#[rustc_const_unstable(feature = "const_heap", issue = "79597")]
19251939
#[cfg(not(bootstrap))]
19261940
pub fn const_deallocate(ptr: *mut u8, size: usize, align: usize);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// run-pass
2+
#![feature(core_intrinsics)]
3+
#![feature(const_heap)]
4+
#![feature(inline_const)]
5+
6+
use std::intrinsics;
7+
8+
struct ZST;
9+
10+
fn main() {
11+
const {
12+
unsafe {
13+
let _ = intrinsics::const_allocate(0, 0) as *mut ZST;
14+
}
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// run-pass
2+
#![feature(core_intrinsics)]
3+
#![feature(const_heap)]
4+
#![feature(inline_const)]
5+
6+
use std::intrinsics;
7+
8+
fn main() {
9+
const {
10+
unsafe {
11+
let ptr1 = intrinsics::const_allocate(0, 0);
12+
let ptr2 = intrinsics::const_allocate(0, 0);
13+
intrinsics::const_deallocate(ptr1, 0, 0);
14+
intrinsics::const_deallocate(ptr2, 0, 0);
15+
}
16+
}
17+
}

0 commit comments

Comments
 (0)