rust/tests/ui/unsafe/union.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

54 lines
1.2 KiB
Rust
Raw Normal View History

// revisions: mir thir
// [thir]compile-flags: -Z thir-unsafeck
union Foo {
bar: i8,
zst: (),
pizza: Pizza,
}
2022-06-29 21:33:18 -05:00
#[derive(Clone, Copy)]
struct Pizza {
topping: Option<PizzaTopping>
}
#[allow(dead_code)]
2022-06-29 21:33:18 -05:00
#[derive(Clone, Copy)]
enum PizzaTopping {
Cheese,
Pineapple,
}
fn do_nothing(_x: &mut Foo) {}
pub fn main() {
let mut foo = Foo { bar: 5 };
do_nothing(&mut foo);
// This is UB, so this test isn't run
match foo {
Foo { bar: _a } => {}, //~ ERROR access to union field is unsafe
}
match foo { //[mir]~ ERROR access to union field is unsafe
Foo {
pizza: Pizza { //[thir]~ ERROR access to union field is unsafe
topping: Some(PizzaTopping::Cheese) | Some(PizzaTopping::Pineapple) | None
}
} => {},
}
// MIR unsafeck incorrectly thinks that no unsafe block is needed to do these
match foo {
Foo { zst: () } => {}, //[thir]~ ERROR access to union field is unsafe
}
match foo {
Foo { pizza: Pizza { .. } } => {}, //[thir]~ ERROR access to union field is unsafe
}
// binding to wildcard is okay
match foo {
Foo { bar: _ } => {},
}
let Foo { bar: _ } = foo;
}