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

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

30 lines
812 B
Rust
Raw Normal View History

2021-08-03 14:11:04 -05:00
// revisions: mirunsafeck thirunsafeck
// [thirunsafeck]compile-flags: -Z thir-unsafeck
use std::ops::AddAssign;
2022-06-29 21:33:18 -05:00
use std::mem::ManuallyDrop;
2021-08-03 14:11:04 -05:00
2022-06-29 21:33:18 -05:00
struct NonCopy;
impl AddAssign for NonCopy {
2021-08-03 14:11:04 -05:00
fn add_assign(&mut self, _: Self) {}
}
union Foo {
a: u8, // non-dropping
2022-06-29 21:33:18 -05:00
b: ManuallyDrop<NonCopy>,
2021-08-03 14:11:04 -05:00
}
fn main() {
let mut foo = Foo { a: 42 };
foo.a += 5; //~ ERROR access to union field is unsafe
2022-06-29 21:33:18 -05: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-03 14:11:04 -05: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
}