]> git.lizzy.rs Git - rust.git/blob - src/test/ui/union/union-basic.rs
Rollup merge of #100462 - zohnannor:master, r=thomcc
[rust.git] / src / test / ui / union / union-basic.rs
1 // run-pass
2 // revisions: mirunsafeck thirunsafeck
3 // [thirunsafeck]compile-flags: -Z thir-unsafeck
4
5 #![allow(unused_imports)]
6
7 // aux-build:union.rs
8
9 extern crate union;
10 use std::mem::{size_of, align_of, zeroed};
11
12 union U {
13     a: u8,
14     b: u16
15 }
16
17 fn local() {
18     assert_eq!(size_of::<U>(), 2);
19     assert_eq!(align_of::<U>(), 2);
20
21     let u = U { a: 10 };
22     unsafe {
23         assert_eq!(u.a, 10);
24         let U { a } = u;
25         assert_eq!(a, 10);
26     }
27
28     let mut w = U { b: 0 };
29     unsafe {
30         assert_eq!(w.a, 0);
31         assert_eq!(w.b, 0);
32         w.a = 1;
33         assert_eq!(w.a, 1);
34         assert_eq!(w.b.to_le(), 1);
35     }
36 }
37
38 fn xcrate() {
39     assert_eq!(size_of::<union::U>(), 2);
40     assert_eq!(align_of::<union::U>(), 2);
41
42     let u = union::U { a: 10 };
43     unsafe {
44         assert_eq!(u.a, 10);
45         let union::U { a } = u;
46         assert_eq!(a, 10);
47     }
48
49     let mut w = union::U { b: 0 };
50     unsafe {
51         assert_eq!(w.a, 0);
52         assert_eq!(w.b, 0);
53         w.a = 1;
54         assert_eq!(w.a, 1);
55         assert_eq!(w.b.to_le(), 1);
56     }
57 }
58
59 fn main() {
60     local();
61     xcrate();
62 }