Skip to content

Commit d935c70

Browse files
committed
Don't allow accidental runtime-checks
1 parent 8440208 commit d935c70

File tree

1 file changed

+50
-51
lines changed

1 file changed

+50
-51
lines changed

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

+50-51
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// size in memory as an `Option<UnsafeCell<u32>>` (namely, 8 bytes).
55

66
// check-pass
7+
// compile-flags: --crate-type=lib
78

89
#![feature(repr_simd)]
910

@@ -23,59 +24,57 @@ struct Size<const S: usize>;
2324

2425
// Overwriting the runtime assertion and making it a compile-time assertion
2526
macro_rules! assert_size {
26-
($a:ty, $b:expr) => {{
27+
($a:ty, $b:expr) => {
2728
const _: Size::<{$b}> = Size::<{size_of::<$a>()}>;
28-
}};
29+
};
2930
}
3031

3132
const PTR_SIZE: usize = std::mem::size_of::<*const ()>();
3233

33-
fn main() {
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!(
51-
RwLock<&()>,
52-
if cfg!(target_pointer_width = "32") { 16 } else { 24 }
53-
);
54-
assert_size!(
55-
Option<RwLock<&()>>,
56-
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 }
61-
);
62-
assert_size!(
63-
Option<Mutex<&()>>,
64-
if cfg!(target_pointer_width = "32") { 16 } else { 24 }
65-
); // (✗ niche opt)
66-
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)
71-
72-
trait Trait {}
73-
assert_size!( UnsafeCell<&dyn Trait> , PTR_SIZE * 2);
74-
assert_size!(Option<UnsafeCell<&dyn Trait>>, PTR_SIZE * 3); // (✗ niche opt)
75-
76-
#[repr(simd)]
77-
pub struct Vec4<T>([T; 4]);
78-
79-
assert_size!( UnsafeCell<Vec4<N32>> , 16);
80-
assert_size!(Option<UnsafeCell<Vec4<N32>>>, 32); // (✗ niche opt)
81-
}
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!(
51+
RwLock<&()>,
52+
if cfg!(target_pointer_width = "32") { 16 } else { 24 }
53+
);
54+
assert_size!(
55+
Option<RwLock<&()>>,
56+
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 }
61+
);
62+
assert_size!(
63+
Option<Mutex<&()>>,
64+
if cfg!(target_pointer_width = "32") { 16 } else { 24 }
65+
); // (✗ niche opt)
66+
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)
71+
72+
trait Trait {}
73+
assert_size!( UnsafeCell<&dyn Trait> , PTR_SIZE * 2);
74+
assert_size!(Option<UnsafeCell<&dyn Trait>>, PTR_SIZE * 3); // (✗ niche opt)
75+
76+
#[repr(simd)]
77+
pub struct Vec4<T>([T; 4]);
78+
79+
assert_size!( UnsafeCell<Vec4<N32>> , 16);
80+
assert_size!(Option<UnsafeCell<Vec4<N32>>>, 32); // (✗ niche opt)

0 commit comments

Comments
 (0)