-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Test interaction of unions with non-zero/niche-filling optimization #60590
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
48d1be4
Test interaction of unions with non-zero/niche-filling optimization
petertodd 76c5229
Document purpose of union-nonzero test
petertodd aa1db24
Add non-non-zero test to union-nonzero test
petertodd a91ad60
Make clear that status quo ≠ guarantee
petertodd 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 hidden or 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,51 @@ | ||
// run-pass | ||
#![allow(dead_code)] | ||
|
||
// Tests that unions aren't subject to unsafe non-zero/niche-filling optimizations. | ||
// | ||
// For example, if a union `U` can contain both a `&T` and a `*const T`, there's definitely no | ||
// bit-value that an `Option<U>` could reuse as `None`; this test makes sure that isn't done. | ||
// | ||
// Secondly, this tests the status quo (not a guarantee; subject to change!) to not apply such | ||
// optimizations to types containing unions even if they're theoretically possible. (discussion: | ||
// https://github.com/rust-lang/rust/issues/36394) | ||
// | ||
// Notably this nails down part of the behavior that `MaybeUninit` assumes: that a | ||
// `Option<MaybeUninit<&u8>>` does not take advantage of non-zero optimization, and thus is a safe | ||
petertodd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// construct. | ||
|
||
use std::mem::{size_of, transmute}; | ||
|
||
union U1<A: Copy> { | ||
a: A, | ||
} | ||
|
||
union U2<A: Copy, B: Copy> { | ||
a: A, | ||
b: B, | ||
} | ||
|
||
// Option<E> uses a value other than 0 and 1 as None | ||
#[derive(Clone,Copy)] | ||
enum E { | ||
A = 0, | ||
B = 1, | ||
} | ||
|
||
fn main() { | ||
// Unions do not participate in niche-filling/non-zero optimization... | ||
assert!(size_of::<Option<U2<&u8, u8>>>() > size_of::<U2<&u8, u8>>()); | ||
assert!(size_of::<Option<U2<&u8, ()>>>() > size_of::<U2<&u8, ()>>()); | ||
assert!(size_of::<Option<U2<u8, E>>>() > size_of::<U2<u8, E>>()); | ||
|
||
// ...even when theoretically possible: | ||
assert!(size_of::<Option<U1<&u8>>>() > size_of::<U1<&u8>>()); | ||
assert!(size_of::<Option<U2<&u8, &u8>>>() > size_of::<U2<&u8, &u8>>()); | ||
|
||
// The unused bits of the () variant can have any value. | ||
let zeroed: U2<&u8, ()> = unsafe { transmute(std::ptr::null::<u8>()) }; | ||
|
||
if let None = Some(zeroed) { | ||
panic!() | ||
} | ||
} |
This file contains hidden or 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 hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.