]> git.lizzy.rs Git - rust.git/blob - src/test/ui/layout/unsafe-cell-hides-niche.rs
Auto merge of #75936 - sdroege:chunks-exact-construction-bounds-check, r=nagisa
[rust.git] / src / test / ui / layout / unsafe-cell-hides-niche.rs
1 // For rust-lang/rust#68303: the contents of `UnsafeCell<T>` cannot
2 // participate in the niche-optimization for enum discriminants. This
3 // test checks that an `Option<UnsafeCell<NonZeroU32>>` has the same
4 // size in memory as an `Option<UnsafeCell<u32>>` (namely, 8 bytes).
5
6 // run-pass
7
8 #![feature(no_niche)]
9
10 use std::cell::UnsafeCell;
11 use std::mem::size_of;
12 use std::num::NonZeroU32 as N32;
13
14 struct Wrapper<T>(T);
15
16 #[repr(transparent)]
17 struct Transparent<T>(T);
18
19 #[repr(no_niche)]
20 struct NoNiche<T>(T);
21
22 fn main() {
23     assert_eq!(size_of::<Option<Wrapper<u32>>>(),     8);
24     assert_eq!(size_of::<Option<Wrapper<N32>>>(),     4);
25     assert_eq!(size_of::<Option<Transparent<u32>>>(), 8);
26     assert_eq!(size_of::<Option<Transparent<N32>>>(), 4);
27     assert_eq!(size_of::<Option<NoNiche<u32>>>(),     8);
28     assert_eq!(size_of::<Option<NoNiche<N32>>>(),     8);
29
30     assert_eq!(size_of::<Option<UnsafeCell<u32>>>(),  8);
31     assert_eq!(size_of::<Option<UnsafeCell<N32>>>(),  8);
32 }