]> git.lizzy.rs Git - rust.git/blob - src/test/ui/union/union-deref.rs
Auto merge of #86898 - the8472:path-cmp, r=dtolnay
[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 #![feature(untagged_unions)]
7
8 use std::mem::ManuallyDrop;
9
10 union U1<T> { x:(), f: ManuallyDrop<(T,)> }
11
12 union U2<T> { x:(), f: (ManuallyDrop<(T,)>,) }
13
14 fn main() {
15     let mut u : U1<Vec<i32>> = U1 { x: () };
16     unsafe { (*u.f).0 = Vec::new() }; // explicit deref, this compiles
17     unsafe { u.f.0 = Vec::new() }; //~ERROR not automatically applying `DerefMut` on `ManuallyDrop` union field
18     unsafe { &mut (*u.f).0 }; // explicit deref, this compiles
19     unsafe { &mut u.f.0 }; //~ERROR not automatically applying `DerefMut` on `ManuallyDrop` union field
20     unsafe { (*u.f).0.push(0) }; // explicit deref, this compiles
21     unsafe { u.f.0.push(0) }; //~ERROR not automatically applying `DerefMut` on `ManuallyDrop` union field
22
23     let mut u : U2<Vec<i32>> = U2 { x: () };
24     unsafe { (*u.f.0).0 = Vec::new() }; // explicit deref, this compiles
25     unsafe { u.f.0.0 = Vec::new() }; //~ERROR not automatically applying `DerefMut` on `ManuallyDrop` union field
26     unsafe { &mut (*u.f.0).0 }; // explicit deref, this compiles
27     unsafe { &mut u.f.0.0 }; //~ERROR not automatically applying `DerefMut` on `ManuallyDrop` union field
28     unsafe { (*u.f.0).0.push(0) }; // explicit deref, this compiles
29     unsafe { u.f.0.0.push(0) }; //~ERROR not automatically applying `DerefMut` on `ManuallyDrop` union field
30 }