]> git.lizzy.rs Git - rust.git/blob - src/test/ui/consts/const-eval/union-const-eval-field.rs
Auto merge of #81132 - bugadani:map-prealloc, r=matthewjasper
[rust.git] / src / test / ui / consts / const-eval / union-const-eval-field.rs
1 #![feature(const_fn)]
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 it is undefined behavior to use this value
30     FIELD3
31 }
32
33 fn main() {
34     assert_eq!(read_field1(), FLOAT1_AS_I32);
35     assert_eq!(read_field2(), 1.0);
36     assert_eq!(read_field3(), unsafe { UNION.field3 });
37 }