From 698f8c2f01ea549d77d7dc3338a12e04c11057b9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:02:58 +0200 Subject: Adding upstream version 1.64.0+dfsg1. Signed-off-by: Daniel Baumann --- src/test/ui/unsafe/union.rs | 53 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/test/ui/unsafe/union.rs (limited to 'src/test/ui/unsafe/union.rs') diff --git a/src/test/ui/unsafe/union.rs b/src/test/ui/unsafe/union.rs new file mode 100644 index 000000000..4338d78ea --- /dev/null +++ b/src/test/ui/unsafe/union.rs @@ -0,0 +1,53 @@ +// revisions: mir thir +// [thir]compile-flags: -Z thir-unsafeck + +union Foo { + bar: i8, + zst: (), + pizza: Pizza, +} + +#[derive(Clone, Copy)] +struct Pizza { + topping: Option +} + +#[allow(dead_code)] +#[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; +} -- cgit v1.2.3