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