From 9728cc4e26c936e17a107ceeccf029bde4e7e1f0 Mon Sep 17 00:00:00 2001 From: woppopo Date: Sat, 29 Jan 2022 19:13:23 +0900 Subject: [PATCH] Document about some behaviors of `const_(de)allocate` and add some tests. --- library/core/src/intrinsics.rs | 22 +++++++++++++++---- .../heap/alloc_intrinsic_zero_sized.rs | 16 ++++++++++++++ .../heap/dealloc_intrinsic_zero_sized.rs | 17 ++++++++++++++ 3 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 src/test/ui/consts/const-eval/heap/alloc_intrinsic_zero_sized.rs create mode 100644 src/test/ui/consts/const-eval/heap/dealloc_intrinsic_zero_sized.rs diff --git a/library/core/src/intrinsics.rs b/library/core/src/intrinsics.rs index b03f1268e36..b5228397f0a 100644 --- a/library/core/src/intrinsics.rs +++ b/library/core/src/intrinsics.rs @@ -1914,13 +1914,27 @@ extern "rust-intrinsic" { #[rustc_const_unstable(feature = "const_raw_ptr_comparison", issue = "53020")] pub fn ptr_guaranteed_ne(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); diff --git a/src/test/ui/consts/const-eval/heap/alloc_intrinsic_zero_sized.rs b/src/test/ui/consts/const-eval/heap/alloc_intrinsic_zero_sized.rs new file mode 100644 index 00000000000..407e69d41a0 --- /dev/null +++ b/src/test/ui/consts/const-eval/heap/alloc_intrinsic_zero_sized.rs @@ -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; + } + } +} diff --git a/src/test/ui/consts/const-eval/heap/dealloc_intrinsic_zero_sized.rs b/src/test/ui/consts/const-eval/heap/dealloc_intrinsic_zero_sized.rs new file mode 100644 index 00000000000..84fb4d2ea87 --- /dev/null +++ b/src/test/ui/consts/const-eval/heap/dealloc_intrinsic_zero_sized.rs @@ -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); + } + } +}