Don't allow accidental runtime-checks
This commit is contained in:
parent
8440208eb1
commit
d935c70afa
1 changed files with 44 additions and 45 deletions
|
@ -4,6 +4,7 @@
|
||||||
// size in memory as an `Option<UnsafeCell<u32>>` (namely, 8 bytes).
|
// size in memory as an `Option<UnsafeCell<u32>>` (namely, 8 bytes).
|
||||||
|
|
||||||
// check-pass
|
// check-pass
|
||||||
|
// compile-flags: --crate-type=lib
|
||||||
|
|
||||||
#![feature(repr_simd)]
|
#![feature(repr_simd)]
|
||||||
|
|
||||||
|
@ -23,59 +24,57 @@ struct Size<const S: usize>;
|
||||||
|
|
||||||
// Overwriting the runtime assertion and making it a compile-time assertion
|
// Overwriting the runtime assertion and making it a compile-time assertion
|
||||||
macro_rules! assert_size {
|
macro_rules! assert_size {
|
||||||
($a:ty, $b:expr) => {{
|
($a:ty, $b:expr) => {
|
||||||
const _: Size::<{$b}> = Size::<{size_of::<$a>()}>;
|
const _: Size::<{$b}> = Size::<{size_of::<$a>()}>;
|
||||||
}};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const PTR_SIZE: usize = std::mem::size_of::<*const ()>();
|
const PTR_SIZE: usize = std::mem::size_of::<*const ()>();
|
||||||
|
|
||||||
fn main() {
|
assert_size!(Option<Wrapper<u32>>, 8);
|
||||||
assert_size!(Option<Wrapper<u32>>, 8);
|
assert_size!(Option<Wrapper<N32>>, 4); // (✓ niche opt)
|
||||||
assert_size!(Option<Wrapper<N32>>, 4); // (✓ niche opt)
|
assert_size!(Option<Transparent<u32>>, 8);
|
||||||
assert_size!(Option<Transparent<u32>>, 8);
|
assert_size!(Option<Transparent<N32>>, 4); // (✓ niche opt)
|
||||||
assert_size!(Option<Transparent<N32>>, 4); // (✓ niche opt)
|
assert_size!(Option<NoNiche<u32>>, 8);
|
||||||
assert_size!(Option<NoNiche<u32>>, 8);
|
assert_size!(Option<NoNiche<N32>>, 8); // (✗ niche opt)
|
||||||
assert_size!(Option<NoNiche<N32>>, 8); // (✗ niche opt)
|
|
||||||
|
|
||||||
assert_size!(Option<UnsafeCell<u32>>, 8);
|
assert_size!(Option<UnsafeCell<u32>>, 8);
|
||||||
assert_size!(Option<UnsafeCell<N32>>, 8); // (✗ niche opt)
|
assert_size!(Option<UnsafeCell<N32>>, 8); // (✗ niche opt)
|
||||||
|
|
||||||
assert_size!( UnsafeCell<&()> , PTR_SIZE);
|
assert_size!( UnsafeCell<&()> , PTR_SIZE);
|
||||||
assert_size!(Option<UnsafeCell<&()>>, PTR_SIZE * 2); // (✗ niche opt)
|
assert_size!(Option<UnsafeCell<&()>>, PTR_SIZE * 2); // (✗ niche opt)
|
||||||
assert_size!( Cell<&()> , PTR_SIZE);
|
assert_size!( Cell<&()> , PTR_SIZE);
|
||||||
assert_size!(Option< Cell<&()>>, PTR_SIZE * 2); // (✗ niche opt)
|
assert_size!(Option< Cell<&()>>, PTR_SIZE * 2); // (✗ niche opt)
|
||||||
assert_size!( RefCell<&()> , PTR_SIZE * 2);
|
assert_size!( RefCell<&()> , PTR_SIZE * 2);
|
||||||
assert_size!(Option< RefCell<&()>>, PTR_SIZE * 3); // (✗ niche opt)
|
assert_size!(Option< RefCell<&()>>, PTR_SIZE * 3); // (✗ niche opt)
|
||||||
assert_size!(
|
assert_size!(
|
||||||
RwLock<&()>,
|
RwLock<&()>,
|
||||||
if cfg!(target_pointer_width = "32") { 16 } else { 24 }
|
if cfg!(target_pointer_width = "32") { 16 } else { 24 }
|
||||||
);
|
);
|
||||||
assert_size!(
|
assert_size!(
|
||||||
Option<RwLock<&()>>,
|
Option<RwLock<&()>>,
|
||||||
if cfg!(target_pointer_width = "32") { 20 } else { 32 }
|
if cfg!(target_pointer_width = "32") { 20 } else { 32 }
|
||||||
); // (✗ niche opt)
|
); // (✗ niche opt)
|
||||||
assert_size!(
|
assert_size!(
|
||||||
Mutex<&()> ,
|
Mutex<&()> ,
|
||||||
if cfg!(target_pointer_width = "32") { 12 } else { 16 }
|
if cfg!(target_pointer_width = "32") { 12 } else { 16 }
|
||||||
);
|
);
|
||||||
assert_size!(
|
assert_size!(
|
||||||
Option<Mutex<&()>>,
|
Option<Mutex<&()>>,
|
||||||
if cfg!(target_pointer_width = "32") { 16 } else { 24 }
|
if cfg!(target_pointer_width = "32") { 16 } else { 24 }
|
||||||
); // (✗ niche opt)
|
); // (✗ niche opt)
|
||||||
|
|
||||||
assert_size!( UnsafeCell<&[i32]> , PTR_SIZE * 2);
|
assert_size!( UnsafeCell<&[i32]> , PTR_SIZE * 2);
|
||||||
assert_size!(Option<UnsafeCell<&[i32]>>, PTR_SIZE * 3); // (✗ niche opt)
|
assert_size!(Option<UnsafeCell<&[i32]>>, PTR_SIZE * 3); // (✗ niche opt)
|
||||||
assert_size!( UnsafeCell<(&(), &())> , PTR_SIZE * 2);
|
assert_size!( UnsafeCell<(&(), &())> , PTR_SIZE * 2);
|
||||||
assert_size!(Option<UnsafeCell<(&(), &())>>, PTR_SIZE * 3); // (✗ niche opt)
|
assert_size!(Option<UnsafeCell<(&(), &())>>, PTR_SIZE * 3); // (✗ niche opt)
|
||||||
|
|
||||||
trait Trait {}
|
trait Trait {}
|
||||||
assert_size!( UnsafeCell<&dyn Trait> , PTR_SIZE * 2);
|
assert_size!( UnsafeCell<&dyn Trait> , PTR_SIZE * 2);
|
||||||
assert_size!(Option<UnsafeCell<&dyn Trait>>, PTR_SIZE * 3); // (✗ niche opt)
|
assert_size!(Option<UnsafeCell<&dyn Trait>>, PTR_SIZE * 3); // (✗ niche opt)
|
||||||
|
|
||||||
#[repr(simd)]
|
#[repr(simd)]
|
||||||
pub struct Vec4<T>([T; 4]);
|
pub struct Vec4<T>([T; 4]);
|
||||||
|
|
||||||
assert_size!( UnsafeCell<Vec4<N32>> , 16);
|
assert_size!( UnsafeCell<Vec4<N32>> , 16);
|
||||||
assert_size!(Option<UnsafeCell<Vec4<N32>>>, 32); // (✗ niche opt)
|
assert_size!(Option<UnsafeCell<Vec4<N32>>>, 32); // (✗ niche opt)
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue