-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Add intrinsics::const_deallocate
#92274
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
aa6795e
29932db
7a7144f
9728cc4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1914,10 +1914,17 @@ 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. Should not be called at runtime. | ||
/// Allocate at compile time. | ||
/// Returns a null pointer at runtime. | ||
oli-obk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#[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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should mention that it does nothing when the allocation was created by another constant than the one that is currently being evaluated. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Documented. |
||
/// Does nothing at runtime. | ||
#[rustc_const_unstable(feature = "const_heap", issue = "79597")] | ||
#[cfg(not(bootstrap))] | ||
pub fn const_deallocate(ptr: *mut u8, size: usize, align: usize); | ||
|
||
/// Determines whether the raw bytes of the two values are equal. | ||
/// | ||
/// This is particularly handy for arrays, since it allows things like just | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// run-pass | ||
#![feature(core_intrinsics)] | ||
#![feature(const_heap)] | ||
#![feature(const_mut_refs)] | ||
|
||
use std::intrinsics; | ||
|
||
const _X: () = unsafe { | ||
let ptr = intrinsics::const_allocate(4, 4); | ||
intrinsics::const_deallocate(ptr, 4, 4); | ||
}; | ||
|
||
const Y: &u32 = unsafe { | ||
let ptr = intrinsics::const_allocate(4, 4) as *mut u32; | ||
*ptr = 42; | ||
&*ptr | ||
}; | ||
|
||
const Z: &u32 = &42; | ||
|
||
const _Z: () = unsafe { | ||
let ptr1 = Y as *const _ as *mut u8; | ||
intrinsics::const_deallocate(ptr1, 4, 4); // nop | ||
intrinsics::const_deallocate(ptr1, 2, 4); // nop | ||
intrinsics::const_deallocate(ptr1, 4, 2); // nop | ||
|
||
let ptr2 = Z as *const _ as *mut u8; | ||
intrinsics::const_deallocate(ptr2, 4, 4); // nop | ||
intrinsics::const_deallocate(ptr2, 2, 4); // nop | ||
intrinsics::const_deallocate(ptr2, 4, 2); // nop | ||
}; | ||
|
||
fn main() { | ||
assert_eq!(*Y, 42); | ||
assert_eq!(*Z, 42); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#![feature(core_intrinsics)] | ||
#![feature(const_heap)] | ||
#![feature(const_mut_refs)] | ||
|
||
use std::intrinsics; | ||
|
||
const _X: &'static u8 = unsafe { | ||
let ptr = intrinsics::const_allocate(4, 4); | ||
intrinsics::const_deallocate(ptr, 4, 4); | ||
&*ptr | ||
//~^ error: evaluation of constant value failed | ||
}; | ||
|
||
const _Y: u8 = unsafe { | ||
let ptr = intrinsics::const_allocate(4, 4); | ||
let reference = &*ptr; | ||
intrinsics::const_deallocate(ptr, 4, 4); | ||
*reference | ||
//~^ error: evaluation of constant value failed | ||
}; | ||
|
||
fn main() {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
error[E0080]: evaluation of constant value failed | ||
--> $DIR/dealloc_intrinsic_dangling.rs:10:5 | ||
| | ||
LL | &*ptr | ||
| ^^^^^ pointer to alloc2 was dereferenced after this allocation got freed | ||
|
||
error[E0080]: evaluation of constant value failed | ||
--> $DIR/dealloc_intrinsic_dangling.rs:18:5 | ||
| | ||
LL | *reference | ||
| ^^^^^^^^^^ pointer to alloc4 was dereferenced after this allocation got freed | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0080`. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#![feature(core_intrinsics)] | ||
#![feature(const_heap)] | ||
|
||
use std::intrinsics; | ||
|
||
const _X: () = unsafe { | ||
let ptr = intrinsics::const_allocate(4, 4); | ||
intrinsics::const_deallocate(ptr, 4, 4); | ||
intrinsics::const_deallocate(ptr, 4, 4); | ||
//~^ error: evaluation of constant value failed | ||
}; | ||
|
||
fn main() {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
error[E0080]: evaluation of constant value failed | ||
--> $DIR/dealloc_intrinsic_duplicate.rs:9:5 | ||
| | ||
LL | intrinsics::const_deallocate(ptr, 4, 4); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ pointer to alloc2 was dereferenced after this allocation got freed | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0080`. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#![feature(core_intrinsics)] | ||
#![feature(const_heap)] | ||
|
||
use std::intrinsics; | ||
|
||
const _X: () = unsafe { | ||
let ptr = intrinsics::const_allocate(4, 4); | ||
intrinsics::const_deallocate(ptr, 4, 2); | ||
//~^ error: evaluation of constant value failed | ||
}; | ||
const _Y: () = unsafe { | ||
let ptr = intrinsics::const_allocate(4, 4); | ||
intrinsics::const_deallocate(ptr, 2, 4); | ||
//~^ error: evaluation of constant value failed | ||
}; | ||
|
||
const _Z: () = unsafe { | ||
let ptr = intrinsics::const_allocate(4, 4); | ||
intrinsics::const_deallocate(ptr, 3, 4); | ||
//~^ error: evaluation of constant value failed | ||
}; | ||
|
||
const _W: () = unsafe { | ||
let ptr = intrinsics::const_allocate(4, 4); | ||
intrinsics::const_deallocate(ptr, 4, 3); | ||
//~^ error: evaluation of constant value failed | ||
}; | ||
|
||
fn main() {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
error[E0080]: evaluation of constant value failed | ||
--> $DIR/dealloc_intrinsic_incorrect_layout.rs:8:5 | ||
| | ||
LL | intrinsics::const_deallocate(ptr, 4, 2); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ incorrect layout on deallocation: alloc2 has size 4 and alignment 4, but gave size 4 and alignment 2 | ||
|
||
error[E0080]: evaluation of constant value failed | ||
--> $DIR/dealloc_intrinsic_incorrect_layout.rs:13:5 | ||
| | ||
LL | intrinsics::const_deallocate(ptr, 2, 4); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ incorrect layout on deallocation: alloc4 has size 4 and alignment 4, but gave size 2 and alignment 4 | ||
|
||
error[E0080]: evaluation of constant value failed | ||
--> $DIR/dealloc_intrinsic_incorrect_layout.rs:19:5 | ||
| | ||
LL | intrinsics::const_deallocate(ptr, 3, 4); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ incorrect layout on deallocation: alloc6 has size 4 and alignment 4, but gave size 3 and alignment 4 | ||
|
||
error[E0080]: evaluation of constant value failed | ||
--> $DIR/dealloc_intrinsic_incorrect_layout.rs:25:5 | ||
| | ||
LL | intrinsics::const_deallocate(ptr, 4, 3); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ align has to be a power of 2, `3` is not a power of 2 | ||
|
||
error: aborting due to 4 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0080`. |
Uh oh!
There was an error while loading. Please reload this page.