rust/tests/ui/unsafe/union-assignop.rs

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

27 lines
723 B
Rust
Raw Normal View History

2021-08-04 03:11:04 +08:00
use std::ops::AddAssign;
2022-06-30 10:33:18 +08:00
use std::mem::ManuallyDrop;
2021-08-04 03:11:04 +08:00
2022-06-30 10:33:18 +08:00
struct NonCopy;
impl AddAssign for NonCopy {
2021-08-04 03:11:04 +08:00
fn add_assign(&mut self, _: Self) {}
}
union Foo {
a: u8, // non-dropping
2022-06-30 10:33:18 +08:00
b: ManuallyDrop<NonCopy>,
2021-08-04 03:11:04 +08:00
}
fn main() {
let mut foo = Foo { a: 42 };
foo.a += 5; //~ ERROR access to union field is unsafe
2022-06-30 10:33:18 +08:00
*foo.b += NonCopy; //~ ERROR access to union field is unsafe
*foo.b = NonCopy; //~ ERROR access to union field is unsafe
foo.b = ManuallyDrop::new(NonCopy);
2021-08-04 03:11:04 +08:00
foo.a; //~ ERROR access to union field is unsafe
let foo = Foo { a: 42 };
foo.b; //~ ERROR access to union field is unsafe
let mut foo = Foo { a: 42 };
foo.b = foo.b;
//~^ ERROR access to union field is unsafe
}