]> git.lizzy.rs Git - rust.git/blob - src/test/ui/unsafe/union.rs
Rollup merge of #103033 - alyssais:pkg-config, r=joshtriplett
[rust.git] / src / test / ui / unsafe / union.rs
1 // revisions: mir thir
2 // [thir]compile-flags: -Z thir-unsafeck
3
4 union Foo {
5     bar: i8,
6     zst: (),
7     pizza: Pizza,
8 }
9
10 #[derive(Clone, Copy)]
11 struct Pizza {
12     topping: Option<PizzaTopping>
13 }
14
15 #[allow(dead_code)]
16 #[derive(Clone, Copy)]
17 enum PizzaTopping {
18     Cheese,
19     Pineapple,
20 }
21
22 fn do_nothing(_x: &mut Foo) {}
23
24 pub fn main() {
25     let mut foo = Foo { bar: 5 };
26     do_nothing(&mut foo);
27
28     // This is UB, so this test isn't run
29     match foo {
30         Foo { bar: _a } => {}, //~ ERROR access to union field is unsafe
31     }
32     match foo { //[mir]~ ERROR access to union field is unsafe
33         Foo {
34             pizza: Pizza { //[thir]~ ERROR access to union field is unsafe
35                 topping: Some(PizzaTopping::Cheese) | Some(PizzaTopping::Pineapple) | None
36             }
37         } => {},
38     }
39
40     // MIR unsafeck incorrectly thinks that no unsafe block is needed to do these
41     match foo {
42         Foo { zst: () } => {}, //[thir]~ ERROR access to union field is unsafe
43     }
44     match foo {
45         Foo { pizza: Pizza { .. } } => {}, //[thir]~ ERROR access to union field is unsafe
46     }
47
48     // binding to wildcard is okay
49     match foo {
50         Foo { bar: _ } => {},
51     }
52     let Foo { bar: _ } = foo;
53 }