Document about some behaviors of const_(de)allocate and add some tests.

This commit is contained in:
woppopo 2022-01-29 19:13:23 +09:00
parent 7a7144f413
commit 9728cc4e26
3 changed files with 51 additions and 4 deletions

View file

@ -1914,13 +1914,27 @@ extern "rust-intrinsic" {
#[rustc_const_unstable(feature = "const_raw_ptr_comparison", issue = "53020")]
pub fn ptr_guaranteed_ne<T>(ptr: *const T, other: *const T) -> bool;
/// Allocate at compile time.
/// Returns a null pointer at runtime.
/// Allocates a block of memory at compile time.
/// At runtime, just returns a null pointer.
///
/// # Safety
///
/// - The `align` argument must be a power of two.
/// - At compile time, a compile error occurs if this constraint is violated.
/// - At runtime, it is not checked.
#[rustc_const_unstable(feature = "const_heap", issue = "79597")]
pub fn const_allocate(size: usize, align: usize) -> *mut u8;
/// Deallocate a memory which allocated by `intrinsics::const_allocate` at compile time.
/// Does nothing at runtime.
/// Deallocates a memory which allocated by `intrinsics::const_allocate` at compile time.
/// At runtime, does nothing.
///
/// # Safety
///
/// - The `align` argument must be a power of two.
/// - At compile time, a compile error occurs if this constraint is violated.
/// - At runtime, it is not checked.
/// - If the `ptr` is created in an another const, this intrinsic doesn't deallocate it.
/// - If the `ptr` is pointing to a local variable, this intrinsic doesn't deallocate it.
#[rustc_const_unstable(feature = "const_heap", issue = "79597")]
#[cfg(not(bootstrap))]
pub fn const_deallocate(ptr: *mut u8, size: usize, align: usize);

View file

@ -0,0 +1,16 @@
// run-pass
#![feature(core_intrinsics)]
#![feature(const_heap)]
#![feature(inline_const)]
use std::intrinsics;
struct ZST;
fn main() {
const {
unsafe {
let _ = intrinsics::const_allocate(0, 0) as *mut ZST;
}
}
}

View file

@ -0,0 +1,17 @@
// run-pass
#![feature(core_intrinsics)]
#![feature(const_heap)]
#![feature(inline_const)]
use std::intrinsics;
fn main() {
const {
unsafe {
let ptr1 = intrinsics::const_allocate(0, 0);
let ptr2 = intrinsics::const_allocate(0, 0);
intrinsics::const_deallocate(ptr1, 0, 0);
intrinsics::const_deallocate(ptr2, 0, 0);
}
}
}