diff options
| author | woppopo <woppopo@protonmail.com> | 2022-01-29 19:13:23 +0900 |
|---|---|---|
| committer | woppopo <woppopo@protonmail.com> | 2022-01-29 19:13:23 +0900 |
| commit | 9728cc4e26c936e17a107ceeccf029bde4e7e1f0 (patch) | |
| tree | b9b298917679b3de25e9ee41a30ade8acd5e69c2 | |
| parent | 7a7144f4136897605842886b1ca51515e75d654e (diff) | |
| download | rust-9728cc4e26c936e17a107ceeccf029bde4e7e1f0.tar.gz rust-9728cc4e26c936e17a107ceeccf029bde4e7e1f0.zip | |
Document about some behaviors of `const_(de)allocate` and add some tests.
| -rw-r--r-- | library/core/src/intrinsics.rs | 22 | ||||
| -rw-r--r-- | src/test/ui/consts/const-eval/heap/alloc_intrinsic_zero_sized.rs | 16 | ||||
| -rw-r--r-- | src/test/ui/consts/const-eval/heap/dealloc_intrinsic_zero_sized.rs | 17 |
3 files changed, 51 insertions, 4 deletions
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<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); 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); + } + } +} |
