Skip to content

Commit af8536e

Browse files
committed
Simplify assertion macro
1 parent 3f4cf59 commit af8536e

File tree

1 file changed

+28
-29
lines changed

1 file changed

+28
-29
lines changed

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

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#![feature(repr_simd)]
99

1010
use std::cell::{UnsafeCell, RefCell, Cell};
11-
use std::mem::size_of;
1211
use std::num::NonZeroU32 as N32;
1312
use std::sync::{Mutex, RwLock};
1413

@@ -21,45 +20,45 @@ struct NoNiche<T>(UnsafeCell<T>);
2120

2221
// Overwriting the runtime assertion and making it a compile-time assertion
2322
macro_rules! assert_eq {
24-
($a:expr, $b:literal) => {{
25-
const _: () = assert!($a == $b);
23+
($a:ty, $b:literal) => {{
24+
const _: () = assert!(std::mem::size_of::<$a>() == $b);
2625
}};
2726
}
2827

2928
fn main() {
30-
assert_eq!(size_of::<Option<Wrapper<u32>>>(), 8);
31-
assert_eq!(size_of::<Option<Wrapper<N32>>>(), 4); // (✓ niche opt)
32-
assert_eq!(size_of::<Option<Transparent<u32>>>(), 8);
33-
assert_eq!(size_of::<Option<Transparent<N32>>>(), 4); // (✓ niche opt)
34-
assert_eq!(size_of::<Option<NoNiche<u32>>>(), 8);
35-
assert_eq!(size_of::<Option<NoNiche<N32>>>(), 8); // (✗ niche opt)
29+
assert_eq!(Option<Wrapper<u32>>, 8);
30+
assert_eq!(Option<Wrapper<N32>>, 4); // (✓ niche opt)
31+
assert_eq!(Option<Transparent<u32>>, 8);
32+
assert_eq!(Option<Transparent<N32>>, 4); // (✓ niche opt)
33+
assert_eq!(Option<NoNiche<u32>>, 8);
34+
assert_eq!(Option<NoNiche<N32>>, 8); // (✗ niche opt)
3635

37-
assert_eq!(size_of::<Option<UnsafeCell<u32>>>(), 8);
38-
assert_eq!(size_of::<Option<UnsafeCell<N32>>>(), 8); // (✗ niche opt)
36+
assert_eq!(Option<UnsafeCell<u32>>, 8);
37+
assert_eq!(Option<UnsafeCell<N32>>, 8); // (✗ niche opt)
3938

40-
assert_eq!(size_of::< UnsafeCell<&()> >(), 8);
41-
assert_eq!(size_of::<Option<UnsafeCell<&()>>>(), 16); // (✗ niche opt)
42-
assert_eq!(size_of::< Cell<&()> >(), 8);
43-
assert_eq!(size_of::<Option< Cell<&()>>>(), 16); // (✗ niche opt)
44-
assert_eq!(size_of::< RefCell<&()> >(), 16);
45-
assert_eq!(size_of::<Option< RefCell<&()>>>(), 24); // (✗ niche opt)
46-
assert_eq!(size_of::< RwLock<&()> >(), 24);
47-
assert_eq!(size_of::<Option< RwLock<&()>>>(), 32); // (✗ niche opt)
48-
assert_eq!(size_of::< Mutex<&()> >(), 16);
49-
assert_eq!(size_of::<Option< Mutex<&()>>>(), 24); // (✗ niche opt)
39+
assert_eq!( UnsafeCell<&()> , 8);
40+
assert_eq!(Option<UnsafeCell<&()>>, 16); // (✗ niche opt)
41+
assert_eq!( Cell<&()> , 8);
42+
assert_eq!(Option< Cell<&()>>, 16); // (✗ niche opt)
43+
assert_eq!( RefCell<&()> , 16);
44+
assert_eq!(Option< RefCell<&()>>, 24); // (✗ niche opt)
45+
assert_eq!( RwLock<&()> , 24);
46+
assert_eq!(Option< RwLock<&()>>, 32); // (✗ niche opt)
47+
assert_eq!( Mutex<&()> , 16);
48+
assert_eq!(Option< Mutex<&()>>, 24); // (✗ niche opt)
5049

51-
assert_eq!(size_of::< UnsafeCell<&[i32]> >(), 16);
52-
assert_eq!(size_of::<Option<UnsafeCell<&[i32]>>>(), 24); // (✗ niche opt)
53-
assert_eq!(size_of::< UnsafeCell<(&(), &())> >(), 16);
54-
assert_eq!(size_of::<Option<UnsafeCell<(&(), &())>>>(), 24); // (✗ niche opt)
50+
assert_eq!( UnsafeCell<&[i32]> , 16);
51+
assert_eq!(Option<UnsafeCell<&[i32]>>, 24); // (✗ niche opt)
52+
assert_eq!( UnsafeCell<(&(), &())> , 16);
53+
assert_eq!(Option<UnsafeCell<(&(), &())>>, 24); // (✗ niche opt)
5554

5655
trait Trait {}
57-
assert_eq!(size_of::< UnsafeCell<&dyn Trait> >(), 16);
58-
assert_eq!(size_of::<Option<UnsafeCell<&dyn Trait>>>(), 24); // (✗ niche opt)
56+
assert_eq!( UnsafeCell<&dyn Trait> , 16);
57+
assert_eq!(Option<UnsafeCell<&dyn Trait>>, 24); // (✗ niche opt)
5958

6059
#[repr(simd)]
6160
pub struct Vec4<T>([T; 4]);
6261

63-
assert_eq!(size_of::< UnsafeCell<Vec4<N32>> >(), 16);
64-
assert_eq!(size_of::<Option<UnsafeCell<Vec4<N32>>>>(), 32); // (✗ niche opt)
62+
assert_eq!( UnsafeCell<Vec4<N32>> , 16);
63+
assert_eq!(Option<UnsafeCell<Vec4<N32>>>, 32); // (✗ niche opt)
6564
}

0 commit comments

Comments
 (0)