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