Skip to content

Commit 6c7d82e

Browse files
committed
Make ui/borrowck/borrowck-reborrow-from-mut.rs robust w.r.t. NLL.
1 parent 9f9bf94 commit 6c7d82e

File tree

3 files changed

+135
-19
lines changed

3 files changed

+135
-19
lines changed
Lines changed: 107 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,107 @@
1+
error[E0499]: cannot borrow `foo.bar1` as mutable more than once at a time
2+
--> $DIR/borrowck-reborrow-from-mut.rs:23:17
3+
|
4+
LL | let _bar1 = &mut foo.bar1;
5+
| ------------- first mutable borrow occurs here
6+
LL | let _bar2 = &mut foo.bar1; //~ ERROR cannot borrow
7+
| ^^^^^^^^^^^^^ second mutable borrow occurs here
8+
LL | use_mut(_bar1);
9+
| ----- first borrow later used here
10+
11+
error[E0502]: cannot borrow `foo.bar1` as immutable because it is also borrowed as mutable
12+
--> $DIR/borrowck-reborrow-from-mut.rs:28:17
13+
|
14+
LL | let _bar1 = &mut foo.bar1;
15+
| ------------- mutable borrow occurs here
16+
LL | let _bar2 = &foo.bar1; //~ ERROR cannot borrow
17+
| ^^^^^^^^^ immutable borrow occurs here
18+
LL | use_mut(_bar1);
19+
| ----- mutable borrow later used here
20+
21+
error[E0502]: cannot borrow `foo.bar1` as mutable because it is also borrowed as immutable
22+
--> $DIR/borrowck-reborrow-from-mut.rs:33:17
23+
|
24+
LL | let _bar1 = &foo.bar1;
25+
| --------- immutable borrow occurs here
26+
LL | let _bar2 = &mut foo.bar1; //~ ERROR cannot borrow
27+
| ^^^^^^^^^^^^^ mutable borrow occurs here
28+
LL | use_imm(_bar1);
29+
| ----- immutable borrow later used here
30+
31+
error[E0499]: cannot borrow `foo.bar1` as mutable more than once at a time
32+
--> $DIR/borrowck-reborrow-from-mut.rs:55:21
33+
|
34+
LL | let _bar1 = &mut foo.bar1;
35+
| ------------- first mutable borrow occurs here
36+
LL | match *foo {
37+
LL | Foo { bar1: ref mut _bar1, bar2: _ } => {}
38+
| ^^^^^^^^^^^^^ second mutable borrow occurs here
39+
...
40+
LL | use_mut(_bar1);
41+
| ----- first borrow later used here
42+
43+
error[E0502]: cannot borrow `foo.bar1` as immutable because it is also borrowed as mutable
44+
--> $DIR/borrowck-reborrow-from-mut.rs:62:17
45+
|
46+
LL | let _bar1 = &mut foo.bar1.int1;
47+
| ------------------ mutable borrow occurs here
48+
LL | let _foo1 = &foo.bar1; //~ ERROR cannot borrow
49+
| ^^^^^^^^^ immutable borrow occurs here
50+
LL | let _foo2 = &*foo; //~ ERROR cannot borrow
51+
LL | use_mut(_bar1);
52+
| ----- mutable borrow later used here
53+
54+
error[E0502]: cannot borrow `*foo` as immutable because it is also borrowed as mutable
55+
--> $DIR/borrowck-reborrow-from-mut.rs:63:17
56+
|
57+
LL | let _bar1 = &mut foo.bar1.int1;
58+
| ------------------ mutable borrow occurs here
59+
LL | let _foo1 = &foo.bar1; //~ ERROR cannot borrow
60+
LL | let _foo2 = &*foo; //~ ERROR cannot borrow
61+
| ^^^^^ immutable borrow occurs here
62+
LL | use_mut(_bar1);
63+
| ----- mutable borrow later used here
64+
65+
error[E0499]: cannot borrow `foo.bar1` as mutable more than once at a time
66+
--> $DIR/borrowck-reborrow-from-mut.rs:68:17
67+
|
68+
LL | let _bar1 = &mut foo.bar1.int1;
69+
| ------------------ first mutable borrow occurs here
70+
LL | let _foo1 = &mut foo.bar1; //~ ERROR cannot borrow
71+
| ^^^^^^^^^^^^^ second mutable borrow occurs here
72+
LL | use_mut(_bar1);
73+
| ----- first borrow later used here
74+
75+
error[E0499]: cannot borrow `*foo` as mutable more than once at a time
76+
--> $DIR/borrowck-reborrow-from-mut.rs:73:17
77+
|
78+
LL | let _bar1 = &mut foo.bar1.int1;
79+
| ------------------ first mutable borrow occurs here
80+
LL | let _foo2 = &mut *foo; //~ ERROR cannot borrow
81+
| ^^^^^^^^^ second mutable borrow occurs here
82+
LL | use_mut(_bar1);
83+
| ----- first borrow later used here
84+
85+
error[E0502]: cannot borrow `foo.bar1` as mutable because it is also borrowed as immutable
86+
--> $DIR/borrowck-reborrow-from-mut.rs:78:17
87+
|
88+
LL | let _bar1 = &foo.bar1.int1;
89+
| -------------- immutable borrow occurs here
90+
LL | let _foo1 = &mut foo.bar1; //~ ERROR cannot borrow
91+
| ^^^^^^^^^^^^^ mutable borrow occurs here
92+
LL | use_imm(_bar1);
93+
| ----- immutable borrow later used here
94+
95+
error[E0502]: cannot borrow `*foo` as mutable because it is also borrowed as immutable
96+
--> $DIR/borrowck-reborrow-from-mut.rs:83:17
97+
|
98+
LL | let _bar1 = &foo.bar1.int1;
99+
| -------------- immutable borrow occurs here
100+
LL | let _foo2 = &mut *foo; //~ ERROR cannot borrow
101+
| ^^^^^^^^^ mutable borrow occurs here
102+
LL | use_imm(_bar1);
103+
| ----- immutable borrow later used here
104+
1105
error[E0596]: cannot borrow `foo.bar1` as mutable, as it is behind a `&` reference
2106
--> $DIR/borrowck-reborrow-from-mut.rs:98:17
3107
|
@@ -6,6 +110,7 @@ LL | fn borrow_mut_from_imm(foo: &Foo) {
6110
LL | let _bar1 = &mut foo.bar1; //~ ERROR cannot borrow
7111
| ^^^^^^^^^^^^^ `foo` is a `&` reference, so the data it refers to cannot be borrowed as mutable
8112

9-
error: aborting due to previous error
113+
error: aborting due to 11 previous errors
10114

11-
For more information about this error, try `rustc --explain E0596`.
115+
Some errors occurred: E0499, E0502, E0596.
116+
For more information about an error, try `rustc --explain E0499`.

src/test/ui/borrowck/borrowck-reborrow-from-mut.rs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,86 +21,89 @@ struct Bar {
2121
fn borrow_same_field_twice_mut_mut(foo: &mut Foo) {
2222
let _bar1 = &mut foo.bar1;
2323
let _bar2 = &mut foo.bar1; //~ ERROR cannot borrow
24+
use_mut(_bar1);
2425
}
25-
2626
fn borrow_same_field_twice_mut_imm(foo: &mut Foo) {
2727
let _bar1 = &mut foo.bar1;
2828
let _bar2 = &foo.bar1; //~ ERROR cannot borrow
29+
use_mut(_bar1);
2930
}
30-
3131
fn borrow_same_field_twice_imm_mut(foo: &mut Foo) {
3232
let _bar1 = &foo.bar1;
3333
let _bar2 = &mut foo.bar1; //~ ERROR cannot borrow
34+
use_imm(_bar1);
3435
}
35-
3636
fn borrow_same_field_twice_imm_imm(foo: &mut Foo) {
3737
let _bar1 = &foo.bar1;
3838
let _bar2 = &foo.bar1;
39+
use_imm(_bar1);
3940
}
40-
4141
fn borrow_both_mut(foo: &mut Foo) {
4242
let _bar1 = &mut foo.bar1;
4343
let _bar2 = &mut foo.bar2;
44+
use_mut(_bar1);
4445
}
45-
4646
fn borrow_both_mut_pattern(foo: &mut Foo) {
4747
match *foo {
48-
Foo { bar1: ref mut _bar1, bar2: ref mut _bar2 } => {}
48+
Foo { bar1: ref mut _bar1, bar2: ref mut _bar2 } =>
49+
{ use_mut(_bar1); use_mut(_bar2); }
4950
}
5051
}
51-
5252
fn borrow_var_and_pattern(foo: &mut Foo) {
5353
let _bar1 = &mut foo.bar1;
5454
match *foo {
5555
Foo { bar1: ref mut _bar1, bar2: _ } => {}
5656
//~^ ERROR cannot borrow
5757
}
58+
use_mut(_bar1);
5859
}
59-
6060
fn borrow_mut_and_base_imm(foo: &mut Foo) {
6161
let _bar1 = &mut foo.bar1.int1;
6262
let _foo1 = &foo.bar1; //~ ERROR cannot borrow
6363
let _foo2 = &*foo; //~ ERROR cannot borrow
64+
use_mut(_bar1);
6465
}
65-
6666
fn borrow_mut_and_base_mut(foo: &mut Foo) {
6767
let _bar1 = &mut foo.bar1.int1;
6868
let _foo1 = &mut foo.bar1; //~ ERROR cannot borrow
69+
use_mut(_bar1);
6970
}
70-
7171
fn borrow_mut_and_base_mut2(foo: &mut Foo) {
7272
let _bar1 = &mut foo.bar1.int1;
7373
let _foo2 = &mut *foo; //~ ERROR cannot borrow
74+
use_mut(_bar1);
7475
}
75-
7676
fn borrow_imm_and_base_mut(foo: &mut Foo) {
7777
let _bar1 = &foo.bar1.int1;
7878
let _foo1 = &mut foo.bar1; //~ ERROR cannot borrow
79+
use_imm(_bar1);
7980
}
80-
8181
fn borrow_imm_and_base_mut2(foo: &mut Foo) {
8282
let _bar1 = &foo.bar1.int1;
8383
let _foo2 = &mut *foo; //~ ERROR cannot borrow
84+
use_imm(_bar1);
8485
}
85-
8686
fn borrow_imm_and_base_imm(foo: &mut Foo) {
8787
let _bar1 = &foo.bar1.int1;
8888
let _foo1 = &foo.bar1;
8989
let _foo2 = &*foo;
90+
use_imm(_bar1);
9091
}
91-
9292
fn borrow_mut_and_imm(foo: &mut Foo) {
9393
let _bar1 = &mut foo.bar1;
9494
let _foo1 = &foo.bar2;
95+
use_mut(_bar1);
9596
}
96-
9797
fn borrow_mut_from_imm(foo: &Foo) {
9898
let _bar1 = &mut foo.bar1; //~ ERROR cannot borrow
9999
}
100100

101101
fn borrow_long_path_both_mut(foo: &mut Foo) {
102102
let _bar1 = &mut foo.bar1.int1;
103103
let _foo1 = &mut foo.bar2.int2;
104+
use_mut(_bar1);
104105
}
105-
106106
fn main() {}
107+
108+
fn use_mut<T>(_: &mut T) { }
109+
fn use_imm<T>(_: &T) { }

src/test/ui/borrowck/borrowck-reborrow-from-mut.stderr

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ LL | let _bar1 = &mut foo.bar1;
55
| -------- first mutable borrow occurs here
66
LL | let _bar2 = &mut foo.bar1; //~ ERROR cannot borrow
77
| ^^^^^^^^ second mutable borrow occurs here
8+
LL | use_mut(_bar1);
89
LL | }
910
| - first borrow ends here
1011

@@ -15,6 +16,7 @@ LL | let _bar1 = &mut foo.bar1;
1516
| -------- mutable borrow occurs here
1617
LL | let _bar2 = &foo.bar1; //~ ERROR cannot borrow
1718
| ^^^^^^^^ immutable borrow occurs here
19+
LL | use_mut(_bar1);
1820
LL | }
1921
| - mutable borrow ends here
2022

@@ -25,6 +27,7 @@ LL | let _bar1 = &foo.bar1;
2527
| -------- immutable borrow occurs here
2628
LL | let _bar2 = &mut foo.bar1; //~ ERROR cannot borrow
2729
| ^^^^^^^^ mutable borrow occurs here
30+
LL | use_imm(_bar1);
2831
LL | }
2932
| - immutable borrow ends here
3033

@@ -47,7 +50,7 @@ LL | let _bar1 = &mut foo.bar1.int1;
4750
| ------------- mutable borrow occurs here
4851
LL | let _foo1 = &foo.bar1; //~ ERROR cannot borrow
4952
| ^^^^^^^^ immutable borrow occurs here
50-
LL | let _foo2 = &*foo; //~ ERROR cannot borrow
53+
...
5154
LL | }
5255
| - mutable borrow ends here
5356

@@ -59,6 +62,7 @@ LL | let _bar1 = &mut foo.bar1.int1;
5962
LL | let _foo1 = &foo.bar1; //~ ERROR cannot borrow
6063
LL | let _foo2 = &*foo; //~ ERROR cannot borrow
6164
| ^^^^ immutable borrow occurs here
65+
LL | use_mut(_bar1);
6266
LL | }
6367
| - mutable borrow ends here
6468

@@ -69,6 +73,7 @@ LL | let _bar1 = &mut foo.bar1.int1;
6973
| ------------- first mutable borrow occurs here
7074
LL | let _foo1 = &mut foo.bar1; //~ ERROR cannot borrow
7175
| ^^^^^^^^ second mutable borrow occurs here
76+
LL | use_mut(_bar1);
7277
LL | }
7378
| - first borrow ends here
7479

@@ -79,6 +84,7 @@ LL | let _bar1 = &mut foo.bar1.int1;
7984
| ------------- first mutable borrow occurs here
8085
LL | let _foo2 = &mut *foo; //~ ERROR cannot borrow
8186
| ^^^^ second mutable borrow occurs here
87+
LL | use_mut(_bar1);
8288
LL | }
8389
| - first borrow ends here
8490

@@ -89,6 +95,7 @@ LL | let _bar1 = &foo.bar1.int1;
8995
| ------------- immutable borrow occurs here
9096
LL | let _foo1 = &mut foo.bar1; //~ ERROR cannot borrow
9197
| ^^^^^^^^ mutable borrow occurs here
98+
LL | use_imm(_bar1);
9299
LL | }
93100
| - immutable borrow ends here
94101

@@ -99,6 +106,7 @@ LL | let _bar1 = &foo.bar1.int1;
99106
| ------------- immutable borrow occurs here
100107
LL | let _foo2 = &mut *foo; //~ ERROR cannot borrow
101108
| ^^^^ mutable borrow occurs here
109+
LL | use_imm(_bar1);
102110
LL | }
103111
| - immutable borrow ends here
104112

0 commit comments

Comments
 (0)