2021-05-13 14:42:25 +00:00
|
|
|
union Foo {
|
|
|
|
bar: i8,
|
|
|
|
zst: (),
|
|
|
|
pizza: Pizza,
|
|
|
|
}
|
|
|
|
|
2022-06-30 02:33:18 +00:00
|
|
|
#[derive(Clone, Copy)]
|
2021-05-13 14:42:25 +00:00
|
|
|
struct Pizza {
|
|
|
|
topping: Option<PizzaTopping>
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
2022-06-30 02:33:18 +00:00
|
|
|
#[derive(Clone, Copy)]
|
2021-05-13 14:42:25 +00:00
|
|
|
enum PizzaTopping {
|
|
|
|
Cheese,
|
|
|
|
Pineapple,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn do_nothing(_x: &mut Foo) {}
|
|
|
|
|
|
|
|
pub fn main() {
|
|
|
|
let mut foo = Foo { bar: 5 };
|
|
|
|
do_nothing(&mut foo);
|
2022-03-21 13:32:26 +00:00
|
|
|
|
|
|
|
// This is UB, so this test isn't run
|
|
|
|
match foo {
|
|
|
|
Foo { bar: _a } => {}, //~ ERROR access to union field is unsafe
|
|
|
|
}
|
2023-12-07 11:56:48 +00:00
|
|
|
match foo {
|
2022-03-21 13:32:26 +00:00
|
|
|
Foo {
|
2023-12-07 11:56:48 +00:00
|
|
|
pizza: Pizza { //~ ERROR access to union field is unsafe
|
2022-03-21 13:32:26 +00:00
|
|
|
topping: Some(PizzaTopping::Cheese) | Some(PizzaTopping::Pineapple) | None
|
|
|
|
}
|
|
|
|
} => {},
|
2021-05-13 14:42:25 +00:00
|
|
|
}
|
|
|
|
|
2022-03-21 13:32:26 +00:00
|
|
|
match foo {
|
2023-12-07 11:56:48 +00:00
|
|
|
Foo { zst: () } => {} //~ ERROR access to union field is unsafe
|
2021-05-13 14:42:25 +00:00
|
|
|
}
|
2022-03-21 13:32:26 +00:00
|
|
|
match foo {
|
2023-12-07 11:56:48 +00:00
|
|
|
Foo { pizza: Pizza { .. } } => {} //~ ERROR access to union field is unsafe
|
2021-05-13 14:42:25 +00:00
|
|
|
}
|
2022-03-21 13:32:26 +00:00
|
|
|
|
2021-05-13 14:42:25 +00:00
|
|
|
// binding to wildcard is okay
|
|
|
|
match foo {
|
|
|
|
Foo { bar: _ } => {},
|
|
|
|
}
|
|
|
|
let Foo { bar: _ } = foo;
|
|
|
|
}
|