]> git.lizzy.rs Git - rust.git/blob - src/test/ui/union/union-align.rs
Rollup merge of #100462 - zohnannor:master, r=thomcc
[rust.git] / src / test / ui / union / union-align.rs
1 // run-pass
2 // revisions: mirunsafeck thirunsafeck
3 // [thirunsafeck]compile-flags: -Z thir-unsafeck
4
5 #![allow(dead_code)]
6
7 use std::mem::{size_of, size_of_val, align_of, align_of_val};
8
9 #[repr(align(16))]
10 pub union U16 {
11     a: u8,
12     b: u32
13 }
14
15 fn main() {
16     assert_eq!(align_of::<U16>(), 16);
17     assert_eq!(size_of::<U16>(), 16);
18     let u = U16 { a: 10 };
19     unsafe {
20         assert_eq!(align_of_val(&u.a), 1);
21         assert_eq!(size_of_val(&u.a), 1);
22         assert_eq!(u.a, 10);
23     }
24
25     let u = U16 { b: 11 };
26     unsafe {
27         assert_eq!(align_of_val(&u.b), 4);
28         assert_eq!(size_of_val(&u.b), 4);
29         assert_eq!(u.b, 11);
30     }
31
32     hybrid::check_hybrid();
33 }
34
35 mod hybrid {
36     use std::mem::{size_of, align_of};
37
38     #[repr(align(16))]
39     #[derive(Copy, Clone)]
40     struct S1 {
41         a: u16,
42         b: u8,
43     }
44
45     #[repr(align(32))]
46     union U {
47         s: S1,
48         c: u16,
49     }
50
51     #[repr(align(64))]
52     struct S2 {
53         d: u8,
54         u: U,
55     }
56
57     pub fn check_hybrid() {
58         assert_eq!(align_of::<S1>(), 16);
59         assert_eq!(size_of::<S1>(), 16);
60         assert_eq!(align_of::<U>(), 32);
61         assert_eq!(size_of::<U>(), 32);
62         assert_eq!(align_of::<S2>(), 64);
63         assert_eq!(size_of::<S2>(), 64);
64     }
65 }