]> git.lizzy.rs Git - rust.git/blob - src/test/ui/union/union-deref.rs
Rollup merge of #100462 - zohnannor:master, r=thomcc
[rust.git] / src / test / ui / union / union-deref.rs
1 // revisions: mirunsafeck thirunsafeck
2 // [thirunsafeck]compile-flags: -Z thir-unsafeck
3
4 //! Test the part of RFC 2514 that is about not applying `DerefMut` coercions
5 //! of union fields.
6
7 use std::mem::ManuallyDrop;
8
9 union U1<T> { x:(), f: ManuallyDrop<(T,)> }
10
11 union U2<T> { x:(), f: (ManuallyDrop<(T,)>,) }
12
13 fn main() {
14     let mut u : U1<Vec<i32>> = U1 { x: () };
15     unsafe { (*u.f).0 = Vec::new() }; // explicit deref, this compiles
16     unsafe { u.f.0 = Vec::new() }; //~ERROR not automatically applying `DerefMut` on `ManuallyDrop` union field
17     unsafe { &mut (*u.f).0 }; // explicit deref, this compiles
18     unsafe { &mut u.f.0 }; //~ERROR not automatically applying `DerefMut` on `ManuallyDrop` union field
19     unsafe { (*u.f).0.push(0) }; // explicit deref, this compiles
20     unsafe { u.f.0.push(0) }; //~ERROR not automatically applying `DerefMut` on `ManuallyDrop` union field
21
22     let mut u : U2<Vec<i32>> = U2 { x: () };
23     unsafe { (*u.f.0).0 = Vec::new() }; // explicit deref, this compiles
24     unsafe { u.f.0.0 = Vec::new() }; //~ERROR not automatically applying `DerefMut` on `ManuallyDrop` union field
25     unsafe { &mut (*u.f.0).0 }; // explicit deref, this compiles
26     unsafe { &mut u.f.0.0 }; //~ERROR not automatically applying `DerefMut` on `ManuallyDrop` union field
27     unsafe { (*u.f.0).0.push(0) }; // explicit deref, this compiles
28     unsafe { u.f.0.0.push(0) }; //~ERROR not automatically applying `DerefMut` on `ManuallyDrop` union field
29 }