-
Notifications
You must be signed in to change notification settings - Fork 13k
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
add const_allocate intrinsic #79594
Changes from 1 commit
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 | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -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: *const i32 = foo(); | ||||||||||||
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. It's super curious that this doesn't error with the same error that src/test/ui/consts/const-eval/heap/alloc_intrinsic_untyped.rs emits: "untyped pointer". 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. Oh... I think I know why... rust/compiler/rustc_mir/src/const_eval/machine.rs Lines 40 to 44 in 75f1db1
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. I guess we'll need to disable that optimization for function items now 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.
Why that? 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 test memoizes function alls to 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. Basically if you used const fn foo() -> &'static i32 {
const fn bar() -> *mut i32 {
unsafe { intrinsics::const_allocate(4, 4) as *mut i32 }
}
unsafe {
let i = bar();
*i = 20;
...
}
} the 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. Oh, lol... yeah, |
||||||||||||
|
||||||||||||
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!(unsafe { *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 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should Miri also use this
Heap
variant? Or should we rename it toConstHeap
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
@oli-obk so what do you think about using a non-
!
MemoryKind
for the CTFE machine?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, apologies, forgot to post after discussing with @vn-ki . @vn-ki experimented with that, but it's a nontrivial change, because
ConstProp
reuses a lot of the same machinery. I am still in favor of doing that change, but in a follow up PR that does just that single changeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The 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
enum MemoryKind {}
in a PR and then rebase this PR on top of it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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
enum
definition).