-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test interaction of unions with non-zero/niche-filling optimization
Notably this nails down part of the behavior that MaybeUninit assumes, e.g. that a Option<MaybeUninit<&u8>> does not take advantage of non-zero optimization, and thus is a safe construct.
- Loading branch information
Showing
3 changed files
with
73 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// run-pass | ||
#![allow(dead_code)] | ||
|
||
use std::mem::{size_of, transmute}; | ||
|
||
union U1<A: Copy> { | ||
a: A, | ||
} | ||
|
||
union U2<A: Copy, B: Copy> { | ||
a: A, | ||
b: B, | ||
} | ||
|
||
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, ()>>()); | ||
|
||
// ...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 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