diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:18:58 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:18:58 +0000 |
commit | a4b7ed7a42c716ab9f05e351f003d589124fd55d (patch) | |
tree | b620cd3f223850b28716e474e80c58059dca5dd4 /tests/ui/unsafe/union.rs | |
parent | Adding upstream version 1.67.1+dfsg1. (diff) | |
download | rustc-a4b7ed7a42c716ab9f05e351f003d589124fd55d.tar.xz rustc-a4b7ed7a42c716ab9f05e351f003d589124fd55d.zip |
Adding upstream version 1.68.2+dfsg1.upstream/1.68.2+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/ui/unsafe/union.rs')
-rw-r--r-- | tests/ui/unsafe/union.rs | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/tests/ui/unsafe/union.rs b/tests/ui/unsafe/union.rs new file mode 100644 index 000000000..4338d78ea --- /dev/null +++ b/tests/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<PizzaTopping> +} + +#[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; +} |