]> git.lizzy.rs Git - rust.git/blob - src/test/ui/borrowck/borrowck-union-borrow.rs
Auto merge of #54624 - arielb1:evaluate-outlives, r=nikomatsakis
[rust.git] / src / test / ui / borrowck / borrowck-union-borrow.rs
1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // ignore-tidy-linelength
12 // revisions: ast mir
13 //[mir]compile-flags: -Z borrowck=mir
14
15 #[derive(Clone, Copy)]
16 union U {
17     a: u8,
18     b: u64,
19 }
20
21 fn main() {
22     unsafe {
23         let mut u = U { b: 0 };
24         // Imm borrow, same field
25         {
26             let ra = &u.a;
27             let ra2 = &u.a; // OK
28             drop(ra);
29         }
30         {
31             let ra = &u.a;
32             let a = u.a; // OK
33             drop(ra);
34         }
35         {
36             let ra = &u.a;
37             let rma = &mut u.a; //[ast]~ ERROR cannot borrow `u.a` as mutable because it is also borrowed as immutable
38                                 //[mir]~^ ERROR cannot borrow `u.a` as mutable because it is also borrowed as immutable
39             drop(ra);
40         }
41         {
42             let ra = &u.a;
43             u.a = 1; //[ast]~ ERROR cannot assign to `u.a` because it is borrowed
44                      //[mir]~^ ERROR cannot assign to `u.a` because it is borrowed
45             drop(ra);
46         }
47         // Imm borrow, other field
48         {
49             let ra = &u.a;
50             let rb = &u.b; // OK
51             drop(ra);
52         }
53         {
54             let ra = &u.a;
55             let b = u.b; // OK
56             drop(ra);
57         }
58         {
59             let ra = &u.a;
60             let rmb = &mut u.b; //[ast]~ ERROR cannot borrow `u` (via `u.b`) as mutable because `u` is also borrowed as immutable (via `u.a`)
61                                 //[mir]~^ ERROR cannot borrow `u.b` as mutable because it is also borrowed as immutable
62             drop(ra);
63         }
64         {
65             let ra = &u.a;
66             u.b = 1; //[ast]~ ERROR cannot assign to `u.b` because it is borrowed
67                      //[mir]~^ ERROR cannot assign to `u.b` because it is borrowed
68             drop(ra);
69         }
70         // Mut borrow, same field
71         {
72             let rma = &mut u.a;
73             let ra = &u.a; //[ast]~ ERROR cannot borrow `u.a` as immutable because it is also borrowed as mutable
74                          //[mir]~^ ERROR cannot borrow `u.a` as immutable because it is also borrowed as mutable
75             drop(rma);
76         }
77         {
78             let ra = &mut u.a;
79             let a = u.a; //[ast]~ ERROR cannot use `u.a` because it was mutably borrowed
80                          //[mir]~^ ERROR cannot use `u.a` because it was mutably borrowed
81             drop(ra);
82         }
83         {
84             let rma = &mut u.a;
85             let rma2 = &mut u.a; //[ast]~ ERROR cannot borrow `u.a` as mutable more than once at a time
86                                  //[mir]~^ ERROR cannot borrow `u.a` as mutable more than once at a time
87             drop(rma);
88         }
89         {
90             let rma = &mut u.a;
91             u.a = 1; //[ast]~ ERROR cannot assign to `u.a` because it is borrowed
92                      //[mir]~^ ERROR cannot assign to `u.a` because it is borrowed
93             drop(rma);
94         }
95         // Mut borrow, other field
96         {
97             let rma = &mut u.a;
98             let rb = &u.b; //[ast]~ ERROR cannot borrow `u` (via `u.b`) as immutable because `u` is also borrowed as mutable (via `u.a`)
99                            //[mir]~^ ERROR cannot borrow `u.b` as immutable because it is also borrowed as mutable
100             drop(rma);
101         }
102         {
103             let ra = &mut u.a;
104             let b = u.b; //[ast]~ ERROR cannot use `u.b` because it was mutably borrowed
105                          //[mir]~^ ERROR cannot use `u.b` because it was mutably borrowed
106
107             drop(ra);
108         }
109         {
110             let rma = &mut u.a;
111             let rmb2 = &mut u.b; //[ast]~ ERROR cannot borrow `u` (via `u.b`) as mutable more than once at a time
112                                  //[mir]~^ ERROR cannot borrow `u.b` as mutable more than once at a time
113             drop(rma);
114         }
115         {
116             let rma = &mut u.a;
117             u.b = 1; //[ast]~ ERROR cannot assign to `u.b` because it is borrowed
118                      //[mir]~^ ERROR cannot assign to `u.b` because it is borrowed
119             drop(rma);
120         }
121     }
122 }