Skip to content

Commit

Permalink
Document about some behaviors of const_(de)allocate and add some te…
Browse files Browse the repository at this point in the history
…sts.
  • Loading branch information
lilasta committed Jan 29, 2022
1 parent 7a7144f commit 9728cc4
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
22 changes: 18 additions & 4 deletions library/core/src/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
16 changes: 16 additions & 0 deletions src/test/ui/consts/const-eval/heap/alloc_intrinsic_zero_sized.rs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
17 changes: 17 additions & 0 deletions src/test/ui/consts/const-eval/heap/dealloc_intrinsic_zero_sized.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}

0 comments on commit 9728cc4

Please sign in to comment.