diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:19:03 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:19:03 +0000 |
commit | 64d98f8ee037282c35007b64c2649055c56af1db (patch) | |
tree | 5492bcf97fce41ee1c0b1cc2add283f3e66cdab0 /tests/ui/union/union-nonzero.rs | |
parent | Adding debian version 1.67.1+dfsg1-1. (diff) | |
download | rustc-64d98f8ee037282c35007b64c2649055c56af1db.tar.xz rustc-64d98f8ee037282c35007b64c2649055c56af1db.zip |
Merging upstream version 1.68.2+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/ui/union/union-nonzero.rs')
-rw-r--r-- | tests/ui/union/union-nonzero.rs | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/tests/ui/union/union-nonzero.rs b/tests/ui/union/union-nonzero.rs new file mode 100644 index 000000000..3f4f7ea1c --- /dev/null +++ b/tests/ui/union/union-nonzero.rs @@ -0,0 +1,54 @@ +// run-pass +// revisions: mirunsafeck thirunsafeck +// [thirunsafeck]compile-flags: -Z thir-unsafeck + +#![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 an +// `Option<MaybeUninit<&u8>>` does not take advantage of non-zero optimization, and thus is a safe +// 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!() + } +} |