]> git.lizzy.rs Git - rust.git/blob - src/test/ui/consts/const-eval/union-const-eval-field.rs
Rollup merge of #56425 - scottmcm:redo-vec-set_len-docs, r=Centril
[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 union DummyUnion {
8     field1: Field1,
9     field2: Field2,
10     field3: Field3,
11 }
12
13 const FLOAT1_AS_I32: i32 = 1065353216;
14 const UNION: DummyUnion = DummyUnion { field1: FLOAT1_AS_I32 };
15
16 const fn read_field1() -> Field1 {
17     const FIELD1: Field1 = unsafe { UNION.field1 };
18     FIELD1
19 }
20
21 const fn read_field2() -> Field2 {
22     const FIELD2: Field2 = unsafe { UNION.field2 };
23     FIELD2
24 }
25
26 const fn read_field3() -> Field3 {
27     const FIELD3: Field3 = unsafe { UNION.field3 };
28     //~^ ERROR it is undefined behavior to use this value
29     FIELD3
30 }
31
32 fn main() {
33     assert_eq!(read_field1(), FLOAT1_AS_I32);
34     assert_eq!(read_field2(), 1.0);
35     assert_eq!(read_field3(), unsafe { UNION.field3 });
36 }