-
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
Draft: Miri/CTFE engine: Unions are not scalar #94411
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// run-pass | ||
|
||
#![feature(const_ptr_write)] | ||
#![feature(const_fn_trait_bound)] | ||
#![feature(const_mut_refs)] | ||
|
||
// Or, equivalently: `MaybeUninit`. | ||
pub union BagOfBits<T: Copy> { | ||
uninit: (), | ||
_storage: T, | ||
} | ||
|
||
pub const fn make_1u8_bag<T: Copy>() -> BagOfBits<T> { | ||
assert!(core::mem::size_of::<T>() >= 1); | ||
let mut bag = BagOfBits { uninit: () }; | ||
unsafe { (&mut bag as *mut _ as *mut u8).write(1); }; | ||
bag | ||
} | ||
|
||
pub fn check_bag<T: Copy>(bag: &BagOfBits<T>) { | ||
let val = unsafe { (bag as *const _ as *const u8).read() }; | ||
assert_eq!(val, 1); | ||
} | ||
|
||
fn main() { | ||
check_bag(&make_1u8_bag::<[usize; 1]>()); // Fine | ||
check_bag(&make_1u8_bag::<usize>()); // Fine | ||
|
||
const CONST_ARRAY_BAG: BagOfBits<[usize; 1]> = make_1u8_bag(); | ||
check_bag(&CONST_ARRAY_BAG); // Fine. | ||
const CONST_USIZE_BAG: BagOfBits<usize> = make_1u8_bag(); | ||
check_bag(&CONST_USIZE_BAG); // Panics | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// check-pass | ||
|
||
#![feature(const_swap)] | ||
#![feature(const_mut_refs)] | ||
|
||
#[repr(C)] | ||
struct Demo(u64, bool, u64, u32, u64, u64, u64); | ||
|
||
const C: (Demo, Demo) = { | ||
let mut x = Demo(1, true, 3, 4, 5, 6, 7); | ||
let mut y = Demo(10, false, 12, 13, 14, 15, 16); | ||
std::mem::swap(&mut x, &mut y); | ||
(x, y) | ||
}; | ||
|
||
fn main() {} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This line is the problem: some closures (and likely generators) can end up with a
Scalar
layout, and they can even have niches, which then means validity will callread_scalar
and cause an ICE when we do not treat it as a scalar.To fix this, we need a way to iterate over the types of all the fields of (all the variants of) a closure/generator. Any suggestion for how to do that? :D I browsed the code for a bit and found nothing.
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.
For closures you'll want https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/sty/struct.ClosureSubsts.html#method.upvar_tys which you get by copying the substs into a new instance of ClosureSubsts. The same helper exists for generators. I guess we should make this less cumbersome to do
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.
What about renaming this function to
type_can_be_scalar
and returning true in all cases except unions?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.
Then we'd still have the bug for (e.g.) closures that capture unions in a way that they still get
abi::Scalar
layout.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.
I am experimenting with changing the layout to support undef scalars. If perf is happy with it, we can just ask the layout whether the scalars are undef and not do an immediate representation then.