diff options
| author | Oli Scherer <git-spam-no-reply9815368754983@oli-obk.de> | 2022-07-11 14:16:14 +0000 |
|---|---|---|
| committer | Oli Scherer <git-spam-no-reply9815368754983@oli-obk.de> | 2022-07-11 14:16:14 +0000 |
| commit | d935c70afa464dcf16db532e39ae53621debd096 (patch) | |
| tree | 3106e3d7a2201d779241302abb8072900cf66571 | |
| parent | 8440208eb13ec1f680592b9a7793a67f2b1be15e (diff) | |
| download | rust-d935c70afa464dcf16db532e39ae53621debd096.tar.gz rust-d935c70afa464dcf16db532e39ae53621debd096.zip | |
Don't allow accidental runtime-checks
| -rw-r--r-- | src/test/ui/layout/unsafe-cell-hides-niche.rs | 101 |
1 files changed, 50 insertions, 51 deletions
diff --git a/src/test/ui/layout/unsafe-cell-hides-niche.rs b/src/test/ui/layout/unsafe-cell-hides-niche.rs index 1d0e1fce178..865fc2ae297 100644 --- a/src/test/ui/layout/unsafe-cell-hides-niche.rs +++ b/src/test/ui/layout/unsafe-cell-hides-niche.rs @@ -4,6 +4,7 @@ // size in memory as an `Option<UnsafeCell<u32>>` (namely, 8 bytes). // check-pass +// compile-flags: --crate-type=lib #![feature(repr_simd)] @@ -23,59 +24,57 @@ struct Size<const S: usize>; // Overwriting the runtime assertion and making it a compile-time assertion macro_rules! assert_size { - ($a:ty, $b:expr) => {{ + ($a:ty, $b:expr) => { const _: Size::<{$b}> = Size::<{size_of::<$a>()}>; - }}; + }; } const PTR_SIZE: usize = std::mem::size_of::<*const ()>(); -fn main() { - assert_size!(Option<Wrapper<u32>>, 8); - assert_size!(Option<Wrapper<N32>>, 4); // (✓ niche opt) - assert_size!(Option<Transparent<u32>>, 8); - assert_size!(Option<Transparent<N32>>, 4); // (✓ niche opt) - assert_size!(Option<NoNiche<u32>>, 8); - assert_size!(Option<NoNiche<N32>>, 8); // (✗ niche opt) - - assert_size!(Option<UnsafeCell<u32>>, 8); - assert_size!(Option<UnsafeCell<N32>>, 8); // (✗ niche opt) - - assert_size!( UnsafeCell<&()> , PTR_SIZE); - assert_size!(Option<UnsafeCell<&()>>, PTR_SIZE * 2); // (✗ niche opt) - assert_size!( Cell<&()> , PTR_SIZE); - assert_size!(Option< Cell<&()>>, PTR_SIZE * 2); // (✗ niche opt) - assert_size!( RefCell<&()> , PTR_SIZE * 2); - assert_size!(Option< RefCell<&()>>, PTR_SIZE * 3); // (✗ niche opt) - assert_size!( - RwLock<&()>, - if cfg!(target_pointer_width = "32") { 16 } else { 24 } - ); - assert_size!( - Option<RwLock<&()>>, - if cfg!(target_pointer_width = "32") { 20 } else { 32 } - ); // (✗ niche opt) - assert_size!( - Mutex<&()> , - if cfg!(target_pointer_width = "32") { 12 } else { 16 } - ); - assert_size!( - Option<Mutex<&()>>, - if cfg!(target_pointer_width = "32") { 16 } else { 24 } - ); // (✗ niche opt) - - assert_size!( UnsafeCell<&[i32]> , PTR_SIZE * 2); - assert_size!(Option<UnsafeCell<&[i32]>>, PTR_SIZE * 3); // (✗ niche opt) - assert_size!( UnsafeCell<(&(), &())> , PTR_SIZE * 2); - assert_size!(Option<UnsafeCell<(&(), &())>>, PTR_SIZE * 3); // (✗ niche opt) - - trait Trait {} - assert_size!( UnsafeCell<&dyn Trait> , PTR_SIZE * 2); - assert_size!(Option<UnsafeCell<&dyn Trait>>, PTR_SIZE * 3); // (✗ niche opt) - - #[repr(simd)] - pub struct Vec4<T>([T; 4]); - - assert_size!( UnsafeCell<Vec4<N32>> , 16); - assert_size!(Option<UnsafeCell<Vec4<N32>>>, 32); // (✗ niche opt) -} +assert_size!(Option<Wrapper<u32>>, 8); +assert_size!(Option<Wrapper<N32>>, 4); // (✓ niche opt) +assert_size!(Option<Transparent<u32>>, 8); +assert_size!(Option<Transparent<N32>>, 4); // (✓ niche opt) +assert_size!(Option<NoNiche<u32>>, 8); +assert_size!(Option<NoNiche<N32>>, 8); // (✗ niche opt) + +assert_size!(Option<UnsafeCell<u32>>, 8); +assert_size!(Option<UnsafeCell<N32>>, 8); // (✗ niche opt) + +assert_size!( UnsafeCell<&()> , PTR_SIZE); +assert_size!(Option<UnsafeCell<&()>>, PTR_SIZE * 2); // (✗ niche opt) +assert_size!( Cell<&()> , PTR_SIZE); +assert_size!(Option< Cell<&()>>, PTR_SIZE * 2); // (✗ niche opt) +assert_size!( RefCell<&()> , PTR_SIZE * 2); +assert_size!(Option< RefCell<&()>>, PTR_SIZE * 3); // (✗ niche opt) +assert_size!( + RwLock<&()>, + if cfg!(target_pointer_width = "32") { 16 } else { 24 } +); +assert_size!( + Option<RwLock<&()>>, + if cfg!(target_pointer_width = "32") { 20 } else { 32 } +); // (✗ niche opt) +assert_size!( + Mutex<&()> , + if cfg!(target_pointer_width = "32") { 12 } else { 16 } +); +assert_size!( + Option<Mutex<&()>>, + if cfg!(target_pointer_width = "32") { 16 } else { 24 } +); // (✗ niche opt) + +assert_size!( UnsafeCell<&[i32]> , PTR_SIZE * 2); +assert_size!(Option<UnsafeCell<&[i32]>>, PTR_SIZE * 3); // (✗ niche opt) +assert_size!( UnsafeCell<(&(), &())> , PTR_SIZE * 2); +assert_size!(Option<UnsafeCell<(&(), &())>>, PTR_SIZE * 3); // (✗ niche opt) + +trait Trait {} +assert_size!( UnsafeCell<&dyn Trait> , PTR_SIZE * 2); +assert_size!(Option<UnsafeCell<&dyn Trait>>, PTR_SIZE * 3); // (✗ niche opt) + +#[repr(simd)] +pub struct Vec4<T>([T; 4]); + +assert_size!( UnsafeCell<Vec4<N32>> , 16); +assert_size!(Option<UnsafeCell<Vec4<N32>>>, 32); // (✗ niche opt) |
