]> git.proxmox.com Git - rustc.git/blob - src/test/run-pass/ctfe/union-ice.rs
New upstream version 1.26.2+dfsg1
[rustc.git] / src / test / run-pass / ctfe / union-ice.rs
1 // Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #![feature(const_fn)]
12
13 type Field1 = i32;
14 type Field2 = f32;
15 type Field3 = i64;
16
17 union DummyUnion {
18 field1: Field1,
19 field2: Field2,
20 field3: Field3,
21 }
22
23 const FLOAT1_AS_I32: i32 = 1065353216;
24 const UNION: DummyUnion = DummyUnion { field1: FLOAT1_AS_I32 };
25
26 const fn read_field1() -> Field1 {
27 const FIELD1: Field1 = unsafe { UNION.field1 };
28 FIELD1
29 }
30
31 const fn read_field2() -> Field2 {
32 const FIELD2: Field2 = unsafe { UNION.field2 };
33 FIELD2
34 }
35
36 const fn read_field3() -> Field3 {
37 const FIELD3: Field3 = unsafe { UNION.field3 };
38 FIELD3
39 }
40
41 fn main() {
42 assert_eq!(read_field1(), FLOAT1_AS_I32);
43 assert_eq!(read_field2(), 1.0);
44 assert_eq!(read_field3(), unsafe { UNION.field3 });
45 }