Skip to content

Commit 3338593

Browse files
committed
Only check relative sizes on platform specific types
1 parent 0318b70 commit 3338593

File tree

1 file changed

+32
-37
lines changed

1 file changed

+32
-37
lines changed

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

+32-37
Original file line numberDiff line numberDiff line change
@@ -22,54 +22,49 @@ struct NoNiche<T>(UnsafeCell<T>);
2222

2323
struct Size<const S: usize>;
2424

25-
// Overwriting the runtime assertion and making it a compile-time assertion
26-
macro_rules! assert_size_eq {
27-
($ty:ty, $size:expr) => {
25+
macro_rules! check_sizes {
26+
(check_one_specific_size: $ty:ty, $size:expr) => {
2827
const _: Size::<{$size}> = Size::<{size_of::<$ty>()}>;
2928
};
3029
($ty:ty, $size:expr, $optioned_size:expr) => {
31-
assert_size_eq!($ty, $size);
32-
assert_size_eq!(Option<$ty>, $optioned_size);
33-
const _: () = assert!(
34-
$size == $optioned_size ||
35-
size_of::<$ty>() < size_of::<Option<$ty>>()
36-
);
30+
check_sizes!(check_one_specific_size: $ty, $size);
31+
check_sizes!(check_one_specific_size: Option<$ty>, $optioned_size);
32+
check_sizes!(check_no_niche_opt: $size != $optioned_size, $ty);
33+
};
34+
($ty:ty) => {
35+
check_sizes!(check_no_niche_opt: true, $ty);
36+
};
37+
(check_no_niche_opt: $no_niche_opt:expr, $ty:ty) => {
38+
const _: () = if $no_niche_opt { assert!(size_of::<$ty>() < size_of::<Option<$ty>>()); };
3739
};
3840
}
3941

4042
const PTR_SIZE: usize = std::mem::size_of::<*const ()>();
4143

42-
assert_size_eq!(Wrapper<u32>, 4, 8);
43-
assert_size_eq!(Wrapper<N32>, 4, 4); // (✓ niche opt)
44-
assert_size_eq!(Transparent<u32>, 4, 8);
45-
assert_size_eq!(Transparent<N32>, 4, 4); // (✓ niche opt)
46-
assert_size_eq!(NoNiche<u32>, 4, 8);
47-
assert_size_eq!(NoNiche<N32>, 4, 8);
48-
49-
assert_size_eq!(UnsafeCell<u32>, 4, 8);
50-
assert_size_eq!(UnsafeCell<N32>, 4, 8);
51-
52-
assert_size_eq!(UnsafeCell<&()> , PTR_SIZE, PTR_SIZE * 2);
53-
assert_size_eq!( Cell<&()> , PTR_SIZE, PTR_SIZE * 2);
54-
assert_size_eq!( RefCell<&()> , PTR_SIZE * 2, PTR_SIZE * 3);
55-
assert_size_eq!(
56-
RwLock<&()>,
57-
if cfg!(target_pointer_width = "32") { 16 } else { 24 },
58-
if cfg!(target_pointer_width = "32") { 20 } else { 32 }
59-
);
60-
assert_size_eq!(
61-
Mutex<&()> ,
62-
if cfg!(target_pointer_width = "32") { 12 } else { 16 },
63-
if cfg!(target_pointer_width = "32") { 16 } else { 24 }
64-
);
65-
66-
assert_size_eq!(UnsafeCell<&[i32]> , PTR_SIZE * 2, PTR_SIZE * 3);
67-
assert_size_eq!(UnsafeCell<(&(), &())> , PTR_SIZE * 2, PTR_SIZE * 3);
44+
check_sizes!(Wrapper<u32>, 4, 8);
45+
check_sizes!(Wrapper<N32>, 4, 4); // (✓ niche opt)
46+
check_sizes!(Transparent<u32>, 4, 8);
47+
check_sizes!(Transparent<N32>, 4, 4); // (✓ niche opt)
48+
check_sizes!(NoNiche<u32>, 4, 8);
49+
check_sizes!(NoNiche<N32>, 4, 8);
50+
51+
check_sizes!(UnsafeCell<u32>, 4, 8);
52+
check_sizes!(UnsafeCell<N32>, 4, 8);
53+
54+
check_sizes!(UnsafeCell<&()> , PTR_SIZE, PTR_SIZE * 2);
55+
check_sizes!( Cell<&()> , PTR_SIZE, PTR_SIZE * 2);
56+
check_sizes!( RefCell<&()> , PTR_SIZE * 2, PTR_SIZE * 3);
57+
58+
check_sizes!(RwLock<&()>);
59+
check_sizes!(Mutex<&()>);
60+
61+
check_sizes!(UnsafeCell<&[i32]> , PTR_SIZE * 2, PTR_SIZE * 3);
62+
check_sizes!(UnsafeCell<(&(), &())> , PTR_SIZE * 2, PTR_SIZE * 3);
6863

6964
trait Trait {}
70-
assert_size_eq!(UnsafeCell<&dyn Trait> , PTR_SIZE * 2, PTR_SIZE * 3);
65+
check_sizes!(UnsafeCell<&dyn Trait> , PTR_SIZE * 2, PTR_SIZE * 3);
7166

7267
#[repr(simd)]
7368
pub struct Vec4<T>([T; 4]);
7469

75-
assert_size_eq!(UnsafeCell<Vec4<N32>> , 16, 32);
70+
check_sizes!(UnsafeCell<Vec4<N32>> , 16, 32);

0 commit comments

Comments
 (0)