-
Notifications
You must be signed in to change notification settings - Fork 13.4k
add const_allocate intrinsic #79594
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
add const_allocate intrinsic #79594
Changes from 3 commits
528355c
b5b811a
a6c4cbd
1b7fe09
899a59e
bc6eb6f
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 |
---|---|---|
|
@@ -104,7 +104,7 @@ fn intern_shallow<'rt, 'mir, 'tcx, M: CompileTimeMachine<'mir, 'tcx>>( | |
// This match is just a canary for future changes to `MemoryKind`, which most likely need | ||
// changes in this function. | ||
match kind { | ||
MemoryKind::Stack | MemoryKind::Vtable | MemoryKind::CallerLocation => {} | ||
MemoryKind::Stack | MemoryKind::Heap | MemoryKind::Vtable | MemoryKind::CallerLocation => {} | ||
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. Should Miri also use this 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. ah, good point. I think miri will need its own heap variant to prevent us from accidentally mixing the two somewhere. 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. @oli-obk so what do you think about using a non- 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. 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. if you are worried about miri breakage, we can also do that change to an 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. No I think this is fine (but please leave a FIXME in the |
||
} | ||
// Set allocation mutability as appropriate. This is used by LLVM to put things into | ||
// read-only memory, and also by Miri when evaluating other globals that | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#![feature(core_intrinsics)] | ||
#![feature(const_heap)] | ||
#![feature(const_raw_ptr_deref)] | ||
#![feature(const_mut_refs)] | ||
use std::intrinsics; | ||
|
||
const FOO: i32 = foo(); | ||
const fn foo() -> i32 { | ||
unsafe { | ||
let _ = intrinsics::const_allocate(4, 3) as * mut i32; | ||
//~^ error: any use of this value will cause an error [const_err] | ||
} | ||
1 | ||
|
||
} | ||
|
||
fn main() {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
error: any use of this value will cause an error | ||
--> $DIR/alloc_intrinsic_errors.rs:10:17 | ||
| | ||
LL | const FOO: i32 = foo(); | ||
| ----------------------- | ||
... | ||
LL | let _ = intrinsics::const_allocate(4, 3) as * mut i32; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| align has to be a power of 2, `3` is not a power of 2 | ||
| inside `foo` at $DIR/alloc_intrinsic_errors.rs:10:17 | ||
| inside `FOO` at $DIR/alloc_intrinsic_errors.rs:7:18 | ||
| | ||
= note: `#[deny(const_err)]` on by default | ||
|
||
error: aborting due to previous error | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// run-pass | ||
#![feature(core_intrinsics)] | ||
#![feature(const_heap)] | ||
#![feature(const_raw_ptr_deref)] | ||
#![feature(const_mut_refs)] | ||
use std::intrinsics; | ||
|
||
const FOO: &i32 = foo(); | ||
|
||
const fn foo() -> &'static i32 { | ||
let t = unsafe { | ||
let i = intrinsics::const_allocate(4, 4) as * mut i32; | ||
*i = 20; | ||
i | ||
}; | ||
unsafe { &*t } | ||
} | ||
fn main() { | ||
assert_eq!(*FOO, 20) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// run-pass | ||
oli-obk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#![feature(core_intrinsics)] | ||
#![feature(const_heap)] | ||
#![feature(const_raw_ptr_deref)] | ||
#![feature(const_mut_refs)] | ||
use std::intrinsics; | ||
|
||
const FOO: &i32 = foo(); | ||
|
||
const fn foo() -> &'static i32 { | ||
let t = unsafe { | ||
let i = intrinsics::const_allocate(4, 4) as * mut i32; | ||
*i = 20; | ||
i | ||
}; | ||
unsafe { &*t } | ||
} | ||
fn main() { | ||
assert_eq!(*FOO, 20) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// run-pass | ||
#![feature(core_intrinsics)] | ||
#![feature(const_heap)] | ||
#![feature(const_raw_ptr_deref)] | ||
#![feature(const_mut_refs)] | ||
use std::intrinsics; | ||
|
||
const FOO: i32 = foo(); | ||
|
||
const fn foo() -> i32 { | ||
let t = unsafe { | ||
let i = intrinsics::const_allocate(4, 4) as * mut i32; | ||
*i = 20; | ||
i | ||
}; | ||
unsafe { *t } | ||
} | ||
fn main() { | ||
assert_eq!(FOO, 20); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// compile-test | ||
#![feature(core_intrinsics)] | ||
#![feature(const_heap)] | ||
#![feature(const_raw_ptr_deref)] | ||
#![feature(const_mut_refs)] | ||
use std::intrinsics; | ||
|
||
const BAR: &i32 = unsafe { &*(intrinsics::const_allocate(4, 4) as *mut i32) }; | ||
//~^ error: it is undefined behavior to use this value | ||
fn main() {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
error[E0080]: it is undefined behavior to use this value | ||
--> $DIR/alloc_intrinsic_uninit.rs:8:1 | ||
| | ||
LL | const BAR: &i32 = unsafe { &*(intrinsics::const_allocate(4, 4) as *mut i32) }; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized bytes at .<deref>, but expected initialized plain (non-pointer) bytes | ||
| | ||
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. | ||
|
||
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,10 @@ | ||
#![feature(core_intrinsics)] | ||
#![feature(const_heap)] | ||
#![feature(const_raw_ptr_deref)] | ||
#![feature(const_mut_refs)] | ||
use std::intrinsics; | ||
|
||
const BAR: *mut i32 = unsafe { intrinsics::const_allocate(4, 4) as *mut i32}; | ||
//~^ error: untyped pointers are not allowed in constant | ||
|
||
fn main() {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
error: untyped pointers are not allowed in constant | ||
--> $DIR/alloc_intrinsic_untyped.rs:7:1 | ||
| | ||
LL | const BAR: *mut i32 = unsafe { intrinsics::const_allocate(4, 4) as *mut i32}; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|
Uh oh!
There was an error while loading. Please reload this page.