]> git.lizzy.rs Git - rust.git/blob - tests/ui/union/union-move.rs
add (currently ICEing) test
[rust.git] / tests / ui / union / union-move.rs
1 // revisions: mirunsafeck thirunsafeck
2 // [thirunsafeck]compile-flags: -Z thir-unsafeck
3
4 //! Test the behavior of moving out of non-`Copy` union fields.
5 //! Avoid types that `Drop`, we want to focus on moving.
6
7 use std::cell::RefCell;
8 use std::mem::ManuallyDrop;
9
10 fn move_out<T>(x: T) {}
11
12 union U1 {
13     f1_nocopy: ManuallyDrop<RefCell<i32>>,
14     f2_nocopy: ManuallyDrop<RefCell<i32>>,
15     f3_copy: i32,
16 }
17
18 union U2 {
19     f1_nocopy: ManuallyDrop<RefCell<i32>>,
20 }
21 impl Drop for U2 {
22     fn drop(&mut self) {}
23 }
24
25 fn test1(x: U1) {
26     // Moving out of a nocopy field prevents accessing other nocopy field.
27     unsafe {
28         move_out(x.f1_nocopy);
29         move_out(x.f2_nocopy); //~ ERROR use of moved value: `x`
30     }
31 }
32
33 fn test2(x: U1) {
34     // "Moving" out of copy field doesn't prevent later field accesses.
35     unsafe {
36         move_out(x.f3_copy);
37         move_out(x.f2_nocopy); // no error
38     }
39 }
40
41 fn test3(x: U1) {
42     // Moving out of a nocopy field prevents accessing other copy field.
43     unsafe {
44         move_out(x.f2_nocopy);
45         move_out(x.f3_copy); //~ ERROR use of moved value: `x`
46     }
47 }
48
49 fn test4(x: U2) {
50     // Cannot move out of union that implements `Drop`.
51     unsafe {
52         move_out(x.f1_nocopy); //~ ERROR cannot move out of type `U2`, which implements the `Drop`
53     }
54 }
55
56 fn main() {}