]> git.lizzy.rs Git - rust.git/blob - src/test/ui/union/union-pat-refutability.rs
Rollup merge of #102258 - cjgillot:core-kappa, r=m-ou-se
[rust.git] / src / test / ui / union / union-pat-refutability.rs
1 // run-pass
2 // revisions: mirunsafeck thirunsafeck
3 // [thirunsafeck]compile-flags: -Z thir-unsafeck
4
5 #![allow(dead_code)]
6 #![allow(illegal_floating_point_literal_pattern)]
7
8 #[repr(u32)]
9 enum Tag { I, F }
10
11 #[repr(C)]
12 union U {
13     i: i32,
14     f: f32,
15 }
16
17 #[repr(C)]
18 struct Value {
19     tag: Tag,
20     u: U,
21 }
22
23 fn is_zero(v: Value) -> bool {
24     unsafe {
25         match v {
26             Value { tag: Tag::I, u: U { i: 0 } } => true,
27             Value { tag: Tag::F, u: U { f: 0.0 } } => true,
28             _ => false,
29         }
30     }
31 }
32
33 union W {
34     a: u8,
35     b: u8,
36 }
37
38 fn refut(w: W) {
39     unsafe {
40         match w {
41             W { a: 10 } => {
42                 panic!();
43             }
44             W { b } => {
45                 assert_eq!(b, 11);
46             }
47         }
48     }
49 }
50
51 fn main() {
52     let v = Value { tag: Tag::I, u: U { i: 1 } };
53     assert_eq!(is_zero(v), false);
54
55     let w = W { a: 11 };
56     refut(w);
57 }