From cdd6bba8f662b25d52a58d5ffebbe5c3eac60220 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Mon, 11 Jul 2022 14:23:32 +0000 Subject: [PATCH] Simplify size checking --- src/test/ui/layout/unsafe-cell-hides-niche.rs | 66 ++++++++----------- 1 file changed, 29 insertions(+), 37 deletions(-) diff --git a/src/test/ui/layout/unsafe-cell-hides-niche.rs b/src/test/ui/layout/unsafe-cell-hides-niche.rs index 865fc2ae297..9e80a708f2f 100644 --- a/src/test/ui/layout/unsafe-cell-hides-niche.rs +++ b/src/test/ui/layout/unsafe-cell-hides-niche.rs @@ -23,58 +23,50 @@ struct Size; // Overwriting the runtime assertion and making it a compile-time assertion -macro_rules! assert_size { - ($a:ty, $b:expr) => { - const _: Size::<{$b}> = Size::<{size_of::<$a>()}>; +macro_rules! assert_size_eq { + ($ty:ty, $size:expr) => { + const _: Size::<{$size}> = Size::<{size_of::<$ty>()}>; + }; + ($ty:ty, $size:expr, $optioned_size:expr) => { + assert_size_eq!($ty, $size); + assert_size_eq!(Option<$ty>, $optioned_size); + const _: () = assert!($size == $optioned_size || size_of::<$ty>() < size_of::>()); }; } const PTR_SIZE: usize = std::mem::size_of::<*const ()>(); -assert_size!(Option>, 8); -assert_size!(Option>, 4); // (✓ niche opt) -assert_size!(Option>, 8); -assert_size!(Option>, 4); // (✓ niche opt) -assert_size!(Option>, 8); -assert_size!(Option>, 8); // (✗ niche opt) +assert_size_eq!(Wrapper, 4, 8); +assert_size_eq!(Wrapper, 4, 4); // (✓ niche opt) +assert_size_eq!(Transparent, 4, 8); +assert_size_eq!(Transparent, 4, 4); // (✓ niche opt) +assert_size_eq!(NoNiche, 4, 8); +assert_size_eq!(NoNiche, 4, 8); -assert_size!(Option>, 8); -assert_size!(Option>, 8); // (✗ niche opt) +assert_size_eq!(UnsafeCell, 4, 8); +assert_size_eq!(UnsafeCell, 4, 8); -assert_size!( UnsafeCell<&()> , PTR_SIZE); -assert_size!(Option>, 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!( +assert_size_eq!(UnsafeCell<&()> , PTR_SIZE, PTR_SIZE * 2); +assert_size_eq!( Cell<&()> , PTR_SIZE, PTR_SIZE * 2); +assert_size_eq!( RefCell<&()> , PTR_SIZE * 2, PTR_SIZE * 3); +assert_size_eq!( RwLock<&()>, - if cfg!(target_pointer_width = "32") { 16 } else { 24 } -); -assert_size!( - Option>, + if cfg!(target_pointer_width = "32") { 16 } else { 24 }, 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>, +assert_size_eq!( + Mutex<&()> , + if cfg!(target_pointer_width = "32") { 12 } else { 16 }, if cfg!(target_pointer_width = "32") { 16 } else { 24 } -); // (✗ niche opt) +); -assert_size!( UnsafeCell<&[i32]> , PTR_SIZE * 2); -assert_size!(Option>, PTR_SIZE * 3); // (✗ niche opt) -assert_size!( UnsafeCell<(&(), &())> , PTR_SIZE * 2); -assert_size!(Option>, PTR_SIZE * 3); // (✗ niche opt) +assert_size_eq!(UnsafeCell<&[i32]> , PTR_SIZE * 2, PTR_SIZE * 3); +assert_size_eq!(UnsafeCell<(&(), &())> , PTR_SIZE * 2, PTR_SIZE * 3); trait Trait {} -assert_size!( UnsafeCell<&dyn Trait> , PTR_SIZE * 2); -assert_size!(Option>, PTR_SIZE * 3); // (✗ niche opt) +assert_size_eq!(UnsafeCell<&dyn Trait> , PTR_SIZE * 2, PTR_SIZE * 3); #[repr(simd)] pub struct Vec4([T; 4]); -assert_size!( UnsafeCell> , 16); -assert_size!(Option>>, 32); // (✗ niche opt) +assert_size_eq!(UnsafeCell> , 16, 32);