]> git.lizzy.rs Git - rust.git/blob - src/test/ui/structs-enums/align-enum.rs
Rollup merge of #93112 - pietroalbini:pa-cve-2022-21658-nightly, r=pietroalbini
[rust.git] / src / test / ui / structs-enums / align-enum.rs
1 // run-pass
2 #![allow(dead_code)]
3
4 use std::mem;
5
6 // Raising alignment
7 #[repr(align(16))]
8 enum Align16 {
9     Foo { foo: u32 },
10     Bar { bar: u32 },
11 }
12
13 // Raise alignment by maximum
14 #[repr(align(1), align(16))]
15 #[repr(align(32))]
16 #[repr(align(4))]
17 enum Align32 {
18     Foo,
19     Bar,
20 }
21
22 // Not reducing alignment
23 #[repr(align(4))]
24 enum AlsoAlign16 {
25     Foo { limb_with_align16: Align16 },
26     Bar,
27 }
28
29 // No niche for discriminant when used as limb
30 #[repr(align(16))]
31 struct NoNiche16(u64, u64);
32
33 // Discriminant will require extra space, but enum needs to stay compatible
34 // with alignment 16
35 #[repr(align(1))]
36 enum AnotherAlign16 {
37     Foo { limb_with_noniche16: NoNiche16 },
38     Bar,
39     Baz,
40 }
41
42 fn main() {
43     assert_eq!(mem::align_of::<Align16>(), 16);
44     assert_eq!(mem::size_of::<Align16>(), 16);
45
46     assert_eq!(mem::align_of::<Align32>(), 32);
47     assert_eq!(mem::size_of::<Align32>(), 32);
48
49     assert_eq!(mem::align_of::<AlsoAlign16>(), 16);
50     assert_eq!(mem::size_of::<AlsoAlign16>(), 16);
51
52     assert_eq!(mem::align_of::<AnotherAlign16>(), 16);
53     assert_eq!(mem::size_of::<AnotherAlign16>(), 32);
54 }