]> git.lizzy.rs Git - rust.git/blob - src/test/ui/consts/const-eval/union-const-eval-field.rs
Rollup merge of #80733 - steffahn:prettify_pin_links, r=jyn514
[rust.git] / src / test / ui / consts / const-eval / union-const-eval-field.rs
1 // only-x86_64
2 #![feature(const_fn)]
3
4 type Field1 = i32;
5 type Field2 = f32;
6 type Field3 = i64;
7
8 #[repr(C)]
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 };
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     //~^ ERROR it is undefined behavior to use this value
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 }