Skip to content

Commit cdd6bba

Browse files
committed
Simplify size checking
1 parent d935c70 commit cdd6bba

File tree

1 file changed

+31
-39
lines changed

1 file changed

+31
-39
lines changed

src/test/ui/layout/unsafe-cell-hides-niche.rs

+31-39
Original file line numberDiff line numberDiff line change
@@ -23,58 +23,50 @@ struct NoNiche<T>(UnsafeCell<T>);
2323
struct Size<const S: usize>;
2424

2525
// Overwriting the runtime assertion and making it a compile-time assertion
26-
macro_rules! assert_size {
27-
($a:ty, $b:expr) => {
28-
const _: Size::<{$b}> = Size::<{size_of::<$a>()}>;
26+
macro_rules! assert_size_eq {
27+
($ty:ty, $size:expr) => {
28+
const _: Size::<{$size}> = Size::<{size_of::<$ty>()}>;
29+
};
30+
($ty:ty, $size:expr, $optioned_size:expr) => {
31+
assert_size_eq!($ty, $size);
32+
assert_size_eq!(Option<$ty>, $optioned_size);
33+
const _: () = assert!($size == $optioned_size || size_of::<$ty>() < size_of::<Option<$ty>>());
2934
};
3035
}
3136

3237
const PTR_SIZE: usize = std::mem::size_of::<*const ()>();
3338

34-
assert_size!(Option<Wrapper<u32>>, 8);
35-
assert_size!(Option<Wrapper<N32>>, 4); // (✓ niche opt)
36-
assert_size!(Option<Transparent<u32>>, 8);
37-
assert_size!(Option<Transparent<N32>>, 4); // (✓ niche opt)
38-
assert_size!(Option<NoNiche<u32>>, 8);
39-
assert_size!(Option<NoNiche<N32>>, 8); // (✗ niche opt)
40-
41-
assert_size!(Option<UnsafeCell<u32>>, 8);
42-
assert_size!(Option<UnsafeCell<N32>>, 8); // (✗ niche opt)
43-
44-
assert_size!( UnsafeCell<&()> , PTR_SIZE);
45-
assert_size!(Option<UnsafeCell<&()>>, PTR_SIZE * 2); // (✗ niche opt)
46-
assert_size!( Cell<&()> , PTR_SIZE);
47-
assert_size!(Option< Cell<&()>>, PTR_SIZE * 2); // (✗ niche opt)
48-
assert_size!( RefCell<&()> , PTR_SIZE * 2);
49-
assert_size!(Option< RefCell<&()>>, PTR_SIZE * 3); // (✗ niche opt)
50-
assert_size!(
39+
assert_size_eq!(Wrapper<u32>, 4, 8);
40+
assert_size_eq!(Wrapper<N32>, 4, 4); // (✓ niche opt)
41+
assert_size_eq!(Transparent<u32>, 4, 8);
42+
assert_size_eq!(Transparent<N32>, 4, 4); // (✓ niche opt)
43+
assert_size_eq!(NoNiche<u32>, 4, 8);
44+
assert_size_eq!(NoNiche<N32>, 4, 8);
45+
46+
assert_size_eq!(UnsafeCell<u32>, 4, 8);
47+
assert_size_eq!(UnsafeCell<N32>, 4, 8);
48+
49+
assert_size_eq!(UnsafeCell<&()> , PTR_SIZE, PTR_SIZE * 2);
50+
assert_size_eq!( Cell<&()> , PTR_SIZE, PTR_SIZE * 2);
51+
assert_size_eq!( RefCell<&()> , PTR_SIZE * 2, PTR_SIZE * 3);
52+
assert_size_eq!(
5153
RwLock<&()>,
52-
if cfg!(target_pointer_width = "32") { 16 } else { 24 }
53-
);
54-
assert_size!(
55-
Option<RwLock<&()>>,
54+
if cfg!(target_pointer_width = "32") { 16 } else { 24 },
5655
if cfg!(target_pointer_width = "32") { 20 } else { 32 }
57-
); // (✗ niche opt)
58-
assert_size!(
59-
Mutex<&()> ,
60-
if cfg!(target_pointer_width = "32") { 12 } else { 16 }
6156
);
62-
assert_size!(
63-
Option<Mutex<&()>>,
57+
assert_size_eq!(
58+
Mutex<&()> ,
59+
if cfg!(target_pointer_width = "32") { 12 } else { 16 },
6460
if cfg!(target_pointer_width = "32") { 16 } else { 24 }
65-
); // (✗ niche opt)
61+
);
6662

67-
assert_size!( UnsafeCell<&[i32]> , PTR_SIZE * 2);
68-
assert_size!(Option<UnsafeCell<&[i32]>>, PTR_SIZE * 3); // (✗ niche opt)
69-
assert_size!( UnsafeCell<(&(), &())> , PTR_SIZE * 2);
70-
assert_size!(Option<UnsafeCell<(&(), &())>>, PTR_SIZE * 3); // (✗ niche opt)
63+
assert_size_eq!(UnsafeCell<&[i32]> , PTR_SIZE * 2, PTR_SIZE * 3);
64+
assert_size_eq!(UnsafeCell<(&(), &())> , PTR_SIZE * 2, PTR_SIZE * 3);
7165

7266
trait Trait {}
73-
assert_size!( UnsafeCell<&dyn Trait> , PTR_SIZE * 2);
74-
assert_size!(Option<UnsafeCell<&dyn Trait>>, PTR_SIZE * 3); // (✗ niche opt)
67+
assert_size_eq!(UnsafeCell<&dyn Trait> , PTR_SIZE * 2, PTR_SIZE * 3);
7568

7669
#[repr(simd)]
7770
pub struct Vec4<T>([T; 4]);
7871

79-
assert_size!( UnsafeCell<Vec4<N32>> , 16);
80-
assert_size!(Option<UnsafeCell<Vec4<N32>>>, 32); // (✗ niche opt)
72+
assert_size_eq!(UnsafeCell<Vec4<N32>> , 16, 32);

0 commit comments

Comments
 (0)