]> git.lizzy.rs Git - rust.git/blob - src/test/ui/borrowck/borrowck-union-borrow.rs
Merge commit '3e7c6dec244539970b593824334876f8b6ed0b18' into clippyup
[rust.git] / src / test / ui / borrowck / borrowck-union-borrow.rs
1 // ignore-tidy-linelength
2
3 #[derive(Clone, Copy)]
4 union U {
5     a: u8,
6     b: u64,
7 }
8
9 fn main() {
10     unsafe {
11         let mut u = U { b: 0 };
12         // Imm borrow, same field
13         {
14             let ra = &u.a;
15             let ra2 = &u.a; // OK
16             drop(ra);
17         }
18         {
19             let ra = &u.a;
20             let a = u.a; // OK
21             drop(ra);
22         }
23         {
24             let ra = &u.a;
25             let rma = &mut u.a; //~ ERROR cannot borrow `u.a` as mutable because it is also borrowed as immutable
26             drop(ra);
27         }
28         {
29             let ra = &u.a;
30             u.a = 1; //~ ERROR cannot assign to `u.a` because it is borrowed
31             drop(ra);
32         }
33         // Imm borrow, other field
34         {
35             let ra = &u.a;
36             let rb = &u.b; // OK
37             drop(ra);
38         }
39         {
40             let ra = &u.a;
41             let b = u.b; // OK
42             drop(ra);
43         }
44         {
45             let ra = &u.a;
46             let rmb = &mut u.b; //~ ERROR cannot borrow `u` (via `u.b`) as mutable because it is also borrowed as immutable (via `u.a`)
47             drop(ra);
48         }
49         {
50             let ra = &u.a;
51             u.b = 1; //~ ERROR cannot assign to `u.b` because it is borrowed
52             drop(ra);
53         }
54         // Mut borrow, same field
55         {
56             let rma = &mut u.a;
57             let ra = &u.a; //~ ERROR cannot borrow `u.a` as immutable because it is also borrowed as mutable
58             drop(rma);
59         }
60         {
61             let ra = &mut u.a;
62             let a = u.a; //~ ERROR cannot use `u.a` because it was mutably borrowed
63             drop(ra);
64         }
65         {
66             let rma = &mut u.a;
67             let rma2 = &mut u.a; //~ ERROR cannot borrow `u.a` as mutable more than once at a time
68             drop(rma);
69         }
70         {
71             let rma = &mut u.a;
72             u.a = 1; //~ ERROR cannot assign to `u.a` because it is borrowed
73             drop(rma);
74         }
75         // Mut borrow, other field
76         {
77             let rma = &mut u.a;
78             let rb = &u.b; //~ ERROR cannot borrow `u` (via `u.b`) as immutable because it is also borrowed as mutable (via `u.a`)
79             drop(rma);
80         }
81         {
82             let ra = &mut u.a;
83             let b = u.b; //~ ERROR cannot use `u.b` because it was mutably borrowed
84
85             drop(ra);
86         }
87         {
88             let rma = &mut u.a;
89             let rmb2 = &mut u.b; //~ ERROR cannot borrow `u` (via `u.b`) as mutable more than once at a time
90             drop(rma);
91         }
92         {
93             let rma = &mut u.a;
94             u.b = 1; //~ ERROR cannot assign to `u.b` because it is borrowed
95             drop(rma);
96         }
97     }
98 }