]> git.lizzy.rs Git - rust.git/blob - src/test/ui/unsafe/union-assignop.rs
Auto merge of #103894 - mati865:gnullvm-libunwind-changes, r=thomcc
[rust.git] / src / test / ui / unsafe / union-assignop.rs
1 // revisions: mirunsafeck thirunsafeck
2 // [thirunsafeck]compile-flags: -Z thir-unsafeck
3
4 use std::ops::AddAssign;
5 use std::mem::ManuallyDrop;
6
7 struct NonCopy;
8 impl AddAssign for NonCopy {
9     fn add_assign(&mut self, _: Self) {}
10 }
11
12 union Foo {
13     a: u8, // non-dropping
14     b: ManuallyDrop<NonCopy>,
15 }
16
17 fn main() {
18     let mut foo = Foo { a: 42 };
19     foo.a += 5; //~ ERROR access to union field is unsafe
20     *foo.b += NonCopy; //~ ERROR access to union field is unsafe
21     *foo.b = NonCopy; //~ ERROR access to union field is unsafe
22     foo.b = ManuallyDrop::new(NonCopy);
23     foo.a; //~ ERROR access to union field is unsafe
24     let foo = Foo { a: 42 };
25     foo.b; //~ ERROR access to union field is unsafe
26     let mut foo = Foo { a: 42 };
27     foo.b = foo.b;
28     //~^ ERROR access to union field is unsafe
29 }