]> git.lizzy.rs Git - rust.git/blob - src/test/ui/consts/const-eval/union-const-eval-field.rs
Rollup merge of #101655 - dns2utf8:box_docs, r=dtolnay
[rust.git] / src / test / ui / consts / const-eval / union-const-eval-field.rs
1 // only-x86_64
2
3 type Field1 = i32;
4 type Field2 = f32;
5 type Field3 = i64;
6
7 #[repr(C)]
8 union DummyUnion {
9     field1: Field1,
10     field2: Field2,
11     field3: Field3,
12 }
13
14 const FLOAT1_AS_I32: i32 = 1065353216;
15 const UNION: DummyUnion = DummyUnion { field1: FLOAT1_AS_I32 };
16
17 const fn read_field1() -> Field1 {
18     const FIELD1: Field1 = unsafe { UNION.field1 };
19     FIELD1
20 }
21
22 const fn read_field2() -> Field2 {
23     const FIELD2: Field2 = unsafe { UNION.field2 };
24     FIELD2
25 }
26
27 const fn read_field3() -> Field3 {
28     const FIELD3: Field3 = unsafe { UNION.field3 };
29     //~^ ERROR evaluation of constant value failed
30     //~| uninitialized
31     FIELD3
32 }
33
34 fn main() {
35     assert_eq!(read_field1(), FLOAT1_AS_I32);
36     assert_eq!(read_field2(), 1.0);
37     assert_eq!(read_field3(), unsafe { UNION.field3 });
38 }