about summary refs log tree commit diff
diff options
context:
space:
mode:
authorOli Scherer <git-spam-no-reply9815368754983@oli-obk.de>2022-07-11 15:41:38 +0000
committerOli Scherer <git-spam-no-reply9815368754983@oli-obk.de>2022-07-11 15:41:38 +0000
commit3338593afd6a010a2ce6799d3bf4bf4c2a252950 (patch)
tree187573b101024b2b256c4f6b70bc3296f8a6417f
parent0318b70514e13d09fd0e9043da4b7c49e0285f75 (diff)
downloadrust-3338593afd6a010a2ce6799d3bf4bf4c2a252950.tar.gz
rust-3338593afd6a010a2ce6799d3bf4bf4c2a252950.zip
Only check relative sizes on platform specific types
-rw-r--r--src/test/ui/layout/unsafe-cell-hides-niche.rs69
1 files changed, 32 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 7f159887ba1..9eed2ad361c 100644
--- a/src/test/ui/layout/unsafe-cell-hides-niche.rs
+++ b/src/test/ui/layout/unsafe-cell-hides-niche.rs
@@ -22,54 +22,49 @@ struct NoNiche<T>(UnsafeCell<T>);
 
 struct Size<const S: usize>;
 
-// Overwriting the runtime assertion and making it a compile-time assertion
-macro_rules! assert_size_eq {
-    ($ty:ty, $size:expr) => {
+macro_rules! check_sizes {
+    (check_one_specific_size: $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::<Option<$ty>>()
-        );
+        check_sizes!(check_one_specific_size: $ty, $size);
+        check_sizes!(check_one_specific_size: Option<$ty>, $optioned_size);
+        check_sizes!(check_no_niche_opt: $size != $optioned_size, $ty);
+    };
+    ($ty:ty) => {
+        check_sizes!(check_no_niche_opt: true, $ty);
+    };
+    (check_no_niche_opt: $no_niche_opt:expr, $ty:ty) => {
+        const _: () = if $no_niche_opt { assert!(size_of::<$ty>() < size_of::<Option<$ty>>()); };
     };
 }
 
 const PTR_SIZE: usize = std::mem::size_of::<*const ()>();
 
-assert_size_eq!(Wrapper<u32>,     4, 8);
-assert_size_eq!(Wrapper<N32>,     4, 4); // (✓ niche opt)
-assert_size_eq!(Transparent<u32>, 4, 8);
-assert_size_eq!(Transparent<N32>, 4, 4); // (✓ niche opt)
-assert_size_eq!(NoNiche<u32>,     4, 8);
-assert_size_eq!(NoNiche<N32>,     4, 8);
-
-assert_size_eq!(UnsafeCell<u32>,  4, 8);
-assert_size_eq!(UnsafeCell<N32>,  4, 8);
-
-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 },
-    if cfg!(target_pointer_width = "32") { 20 } else { 32 }
-);
-assert_size_eq!(
-    Mutex<&()> ,
-    if cfg!(target_pointer_width = "32") { 12 } else { 16 },
-    if cfg!(target_pointer_width = "32") { 16 } else { 24 }
-);
-
-assert_size_eq!(UnsafeCell<&[i32]> , PTR_SIZE * 2, PTR_SIZE * 3);
-assert_size_eq!(UnsafeCell<(&(), &())> , PTR_SIZE * 2, PTR_SIZE * 3);
+check_sizes!(Wrapper<u32>,     4, 8);
+check_sizes!(Wrapper<N32>,     4, 4); // (✓ niche opt)
+check_sizes!(Transparent<u32>, 4, 8);
+check_sizes!(Transparent<N32>, 4, 4); // (✓ niche opt)
+check_sizes!(NoNiche<u32>,     4, 8);
+check_sizes!(NoNiche<N32>,     4, 8);
+
+check_sizes!(UnsafeCell<u32>,  4, 8);
+check_sizes!(UnsafeCell<N32>,  4, 8);
+
+check_sizes!(UnsafeCell<&()> , PTR_SIZE, PTR_SIZE * 2);
+check_sizes!(      Cell<&()> , PTR_SIZE, PTR_SIZE * 2);
+check_sizes!(   RefCell<&()> , PTR_SIZE * 2, PTR_SIZE * 3);
+
+check_sizes!(RwLock<&()>);
+check_sizes!(Mutex<&()>);
+
+check_sizes!(UnsafeCell<&[i32]> , PTR_SIZE * 2, PTR_SIZE * 3);
+check_sizes!(UnsafeCell<(&(), &())> , PTR_SIZE * 2, PTR_SIZE * 3);
 
 trait Trait {}
-assert_size_eq!(UnsafeCell<&dyn Trait> , PTR_SIZE * 2, PTR_SIZE * 3);
+check_sizes!(UnsafeCell<&dyn Trait> , PTR_SIZE * 2, PTR_SIZE * 3);
 
 #[repr(simd)]
 pub struct Vec4<T>([T; 4]);
 
-assert_size_eq!(UnsafeCell<Vec4<N32>> , 16, 32);
+check_sizes!(UnsafeCell<Vec4<N32>> , 16, 32);