]> git.lizzy.rs Git - rust.git/blob - src/test/ui/union/union-const-eval-field.rs
Rollup merge of #102685 - nbdd0121:unwind, r=m-ou-se
[rust.git] / src / test / ui / union / union-const-eval-field.rs
1 // run-pass
2 // revisions: mirunsafeck thirunsafeck
3 // [thirunsafeck]compile-flags: -Z thir-unsafeck
4
5 type Field1 = (i32, u32);
6 type Field2 = f32;
7 type Field3 = i64;
8
9 union DummyUnion {
10     field1: Field1,
11     field2: Field2,
12     field3: Field3,
13 }
14
15 const FLOAT1_AS_I32: i32 = 1065353216;
16 const UNION: DummyUnion = DummyUnion { field1: (FLOAT1_AS_I32, 0) };
17
18 const fn read_field1() -> Field1 {
19     const FIELD1: Field1 = unsafe { UNION.field1 };
20     FIELD1
21 }
22
23 const fn read_field2() -> Field2 {
24     const FIELD2: Field2 = unsafe { UNION.field2 };
25     FIELD2
26 }
27
28 const fn read_field3() -> Field3 {
29     const FIELD3: Field3 = unsafe { UNION.field3 };
30     FIELD3
31 }
32
33 fn main() {
34     let foo = FLOAT1_AS_I32;
35     assert_eq!(read_field1().0, foo);
36     assert_eq!(read_field1().0, FLOAT1_AS_I32);
37
38     let foo = 1.0;
39     assert_eq!(read_field2(), foo);
40     assert_eq!(read_field2(), 1.0);
41
42     assert_eq!(read_field3(), unsafe { UNION.field3 });
43     let foo = unsafe { UNION.field3 };
44     assert_eq!(read_field3(), foo);
45 }