Skip to content

Commit 5ad7b91

Browse files
authored
Rollup merge of rust-lang#53407 - pnkfelix:partial-53351-make-more-ported-compile-fail-tests-more-robust-wrt-nll, r=nikomatsakis
make more ported compile fail tests more robust w.r.t. NLL This is similar to PR rust-lang#53369, except it covers a disjoint (and much smaller) set of tests that I needed to look at more carefully before being 100% certain they were the same kind of issue.
2 parents 4cdcb23 + cd89fdb commit 5ad7b91

14 files changed

+236
-23
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
error[E0499]: cannot borrow `x` as mutable more than once at a time
2+
--> $DIR/borrowck-closures-two-mut-fail.rs:26:24
3+
|
4+
LL | let c1 = to_fn_mut(|| x = 4);
5+
| -- - first borrow occurs due to use of `x` in closure
6+
| |
7+
| first mutable borrow occurs here
8+
LL | let c2 = to_fn_mut(|| x = 5); //~ ERROR cannot borrow `x` as mutable more than once
9+
| ^^ - second borrow occurs due to use of `x` in closure
10+
| |
11+
| second mutable borrow occurs here
12+
LL | c1;
13+
| -- borrow later used here
14+
15+
error[E0499]: cannot borrow `x` as mutable more than once at a time
16+
--> $DIR/borrowck-closures-two-mut-fail.rs:37:24
17+
|
18+
LL | let c1 = to_fn_mut(|| set(&mut x));
19+
| -- - first borrow occurs due to use of `x` in closure
20+
| |
21+
| first mutable borrow occurs here
22+
LL | let c2 = to_fn_mut(|| set(&mut x)); //~ ERROR cannot borrow `x` as mutable more than once
23+
| ^^ - second borrow occurs due to use of `x` in closure
24+
| |
25+
| second mutable borrow occurs here
26+
LL | c1;
27+
| -- borrow later used here
28+
29+
error[E0499]: cannot borrow `x` as mutable more than once at a time
30+
--> $DIR/borrowck-closures-two-mut-fail.rs:44:24
31+
|
32+
LL | let c1 = to_fn_mut(|| x = 5);
33+
| -- - first borrow occurs due to use of `x` in closure
34+
| |
35+
| first mutable borrow occurs here
36+
LL | let c2 = to_fn_mut(|| set(&mut x)); //~ ERROR cannot borrow `x` as mutable more than once
37+
| ^^ - second borrow occurs due to use of `x` in closure
38+
| |
39+
| second mutable borrow occurs here
40+
LL | c1;
41+
| -- borrow later used here
42+
43+
error[E0499]: cannot borrow `x` as mutable more than once at a time
44+
--> $DIR/borrowck-closures-two-mut-fail.rs:51:24
45+
|
46+
LL | let c1 = to_fn_mut(|| x = 5);
47+
| -- - first borrow occurs due to use of `x` in closure
48+
| |
49+
| first mutable borrow occurs here
50+
LL | let c2 = to_fn_mut(|| { let _y = to_fn_mut(|| set(&mut x)); }); // (nested closure)
51+
| ^^ - second borrow occurs due to use of `x` in closure
52+
| |
53+
| second mutable borrow occurs here
54+
LL | //~^ ERROR cannot borrow `x` as mutable more than once
55+
LL | c1;
56+
| -- borrow later used here
57+
58+
error[E0499]: cannot borrow `x` as mutable more than once at a time
59+
--> $DIR/borrowck-closures-two-mut-fail.rs:63:24
60+
|
61+
LL | let c1 = to_fn_mut(|| set(&mut *x.f));
62+
| -- - first borrow occurs due to use of `x` in closure
63+
| |
64+
| first mutable borrow occurs here
65+
LL | let c2 = to_fn_mut(|| set(&mut *x.f));
66+
| ^^ - second borrow occurs due to use of `x` in closure
67+
| |
68+
| second mutable borrow occurs here
69+
LL | //~^ ERROR cannot borrow `x` as mutable more than once
70+
LL | c1;
71+
| -- borrow later used here
72+
73+
error: aborting due to 5 previous errors
74+
75+
For more information about this error, try `rustc --explain E0499`.

src/test/ui/borrowck/borrowck-closures-two-mut-fail.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,23 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// ignore-compare-mode-nll
12-
1311
// Tests that two closures cannot simultaneously have mutable
1412
// access to the variable, whether that mutable access be used
1513
// for direct assignment or for taking mutable ref. Issue #6801.
1614

17-
// ignore-compare-mode-nll
18-
1915
#![feature(box_syntax)]
2016

17+
18+
19+
20+
2121
fn to_fn_mut<F: FnMut()>(f: F) -> F { f }
2222

2323
fn a() {
2424
let mut x = 3;
2525
let c1 = to_fn_mut(|| x = 4);
2626
let c2 = to_fn_mut(|| x = 5); //~ ERROR cannot borrow `x` as mutable more than once
27+
c1;
2728
}
2829

2930
fn set(x: &mut isize) {
@@ -34,19 +35,22 @@ fn b() {
3435
let mut x = 3;
3536
let c1 = to_fn_mut(|| set(&mut x));
3637
let c2 = to_fn_mut(|| set(&mut x)); //~ ERROR cannot borrow `x` as mutable more than once
38+
c1;
3739
}
3840

3941
fn c() {
4042
let mut x = 3;
4143
let c1 = to_fn_mut(|| x = 5);
4244
let c2 = to_fn_mut(|| set(&mut x)); //~ ERROR cannot borrow `x` as mutable more than once
45+
c1;
4346
}
4447

4548
fn d() {
4649
let mut x = 3;
4750
let c1 = to_fn_mut(|| x = 5);
4851
let c2 = to_fn_mut(|| { let _y = to_fn_mut(|| set(&mut x)); }); // (nested closure)
4952
//~^ ERROR cannot borrow `x` as mutable more than once
53+
c1;
5054
}
5155

5256
fn g() {
@@ -58,6 +62,7 @@ fn g() {
5862
let c1 = to_fn_mut(|| set(&mut *x.f));
5963
let c2 = to_fn_mut(|| set(&mut *x.f));
6064
//~^ ERROR cannot borrow `x` as mutable more than once
65+
c1;
6166
}
6267

6368
fn main() {

src/test/ui/borrowck/borrowck-closures-two-mut-fail.stderr

+9-6
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ LL | let c2 = to_fn_mut(|| x = 5); //~ ERROR cannot borrow `x` as mutable mo
99
| ^^ - borrow occurs due to use of `x` in closure
1010
| |
1111
| second mutable borrow occurs here
12+
LL | c1;
1213
LL | }
1314
| - first borrow ends here
1415

1516
error[E0499]: cannot borrow `x` as mutable more than once at a time
16-
--> $DIR/borrowck-closures-two-mut-fail.rs:36:24
17+
--> $DIR/borrowck-closures-two-mut-fail.rs:37:24
1718
|
1819
LL | let c1 = to_fn_mut(|| set(&mut x));
1920
| -- - previous borrow occurs due to use of `x` in closure
@@ -23,11 +24,12 @@ LL | let c2 = to_fn_mut(|| set(&mut x)); //~ ERROR cannot borrow `x` as muta
2324
| ^^ - borrow occurs due to use of `x` in closure
2425
| |
2526
| second mutable borrow occurs here
27+
LL | c1;
2628
LL | }
2729
| - first borrow ends here
2830

2931
error[E0499]: cannot borrow `x` as mutable more than once at a time
30-
--> $DIR/borrowck-closures-two-mut-fail.rs:42:24
32+
--> $DIR/borrowck-closures-two-mut-fail.rs:44:24
3133
|
3234
LL | let c1 = to_fn_mut(|| x = 5);
3335
| -- - previous borrow occurs due to use of `x` in closure
@@ -37,11 +39,12 @@ LL | let c2 = to_fn_mut(|| set(&mut x)); //~ ERROR cannot borrow `x` as muta
3739
| ^^ - borrow occurs due to use of `x` in closure
3840
| |
3941
| second mutable borrow occurs here
42+
LL | c1;
4043
LL | }
4144
| - first borrow ends here
4245

4346
error[E0499]: cannot borrow `x` as mutable more than once at a time
44-
--> $DIR/borrowck-closures-two-mut-fail.rs:48:24
47+
--> $DIR/borrowck-closures-two-mut-fail.rs:51:24
4548
|
4649
LL | let c1 = to_fn_mut(|| x = 5);
4750
| -- - previous borrow occurs due to use of `x` in closure
@@ -51,12 +54,12 @@ LL | let c2 = to_fn_mut(|| { let _y = to_fn_mut(|| set(&mut x)); }); // (nes
5154
| ^^ - borrow occurs due to use of `x` in closure
5255
| |
5356
| second mutable borrow occurs here
54-
LL | //~^ ERROR cannot borrow `x` as mutable more than once
57+
...
5558
LL | }
5659
| - first borrow ends here
5760

5861
error[E0499]: cannot borrow `x` as mutable more than once at a time
59-
--> $DIR/borrowck-closures-two-mut-fail.rs:59:24
62+
--> $DIR/borrowck-closures-two-mut-fail.rs:63:24
6063
|
6164
LL | let c1 = to_fn_mut(|| set(&mut *x.f));
6265
| -- - previous borrow occurs due to use of `x` in closure
@@ -66,7 +69,7 @@ LL | let c2 = to_fn_mut(|| set(&mut *x.f));
6669
| ^^ - borrow occurs due to use of `x` in closure
6770
| |
6871
| second mutable borrow occurs here
69-
LL | //~^ ERROR cannot borrow `x` as mutable more than once
72+
...
7073
LL | }
7174
| - first borrow ends here
7275

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
error[E0500]: closure requires unique access to `x` but it is already borrowed
2+
--> $DIR/borrowck-closures-unique.rs:36:14
3+
|
4+
LL | let c1 = || get(x);
5+
| -- - first borrow occurs due to use of `x` in closure
6+
| |
7+
| borrow occurs here
8+
LL | let c2 = || set(x); //~ ERROR closure requires unique access to `x`
9+
| ^^ - second borrow occurs due to use of `x` in closure
10+
| |
11+
| closure construction occurs here
12+
LL | c1;
13+
| -- borrow later used here
14+
15+
error[E0500]: closure requires unique access to `x` but it is already borrowed
16+
--> $DIR/borrowck-closures-unique.rs:42:14
17+
|
18+
LL | let c1 = || get(x);
19+
| -- - first borrow occurs due to use of `x` in closure
20+
| |
21+
| borrow occurs here
22+
LL | let c2 = || { get(x); set(x); }; //~ ERROR closure requires unique access to `x`
23+
| ^^ - second borrow occurs due to use of `x` in closure
24+
| |
25+
| closure construction occurs here
26+
LL | c1;
27+
| -- borrow later used here
28+
29+
error[E0524]: two closures require unique access to `x` at the same time
30+
--> $DIR/borrowck-closures-unique.rs:48:14
31+
|
32+
LL | let c1 = || set(x);
33+
| -- - first borrow occurs due to use of `x` in closure
34+
| |
35+
| first closure is constructed here
36+
LL | let c2 = || set(x); //~ ERROR two closures require unique access to `x` at the same time
37+
| ^^ - second borrow occurs due to use of `x` in closure
38+
| |
39+
| second closure is constructed here
40+
LL | c1;
41+
| -- borrow later used here
42+
43+
error[E0594]: cannot assign to `x`, as it is not declared as mutable
44+
--> $DIR/borrowck-closures-unique.rs:57:38
45+
|
46+
LL | fn e(x: &'static mut isize) {
47+
| - help: consider changing this to be mutable: `mut x`
48+
LL | let c1 = |y: &'static mut isize| x = y; //~ ERROR closure cannot assign to immutable argument
49+
| ^^^^^ cannot assign
50+
51+
error: aborting due to 4 previous errors
52+
53+
Some errors occurred: E0500, E0524, E0594.
54+
For more information about an error, try `rustc --explain E0500`.

src/test/ui/borrowck/borrowck-closures-unique.rs

+18-3
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// ignore-compare-mode-nll
12-
1311
// Tests that a closure which requires mutable access to the referent
1412
// of an `&mut` requires a "unique" borrow -- that is, the variable to
1513
// be borrowed (here, `x`) will not be borrowed *mutably*, but
1614
// may be *immutable*, but we cannot allow
1715
// multiple borrows.
1816

17+
18+
1919
fn get(x: &isize) -> isize {
2020
*x
2121
}
@@ -27,25 +27,40 @@ fn set(x: &mut isize) -> isize {
2727
fn a(x: &mut isize) {
2828
let c1 = || get(x);
2929
let c2 = || get(x);
30+
c1();
31+
c2();
3032
}
3133

3234
fn b(x: &mut isize) {
3335
let c1 = || get(x);
3436
let c2 = || set(x); //~ ERROR closure requires unique access to `x`
37+
c1;
3538
}
3639

3740
fn c(x: &mut isize) {
3841
let c1 = || get(x);
3942
let c2 = || { get(x); set(x); }; //~ ERROR closure requires unique access to `x`
43+
c1;
4044
}
4145

4246
fn d(x: &mut isize) {
4347
let c1 = || set(x);
4448
let c2 = || set(x); //~ ERROR two closures require unique access to `x` at the same time
49+
c1;
50+
}
51+
52+
// This test was originally encoded in the form shown as `fn f` below.
53+
// However, since MIR-borrowck and thus NLL takes more control-flow information
54+
// into account, it was necessary to change the test in order to witness the
55+
// same (expected) error under both AST-borrowck and NLL.
56+
fn e(x: &'static mut isize) {
57+
let c1 = |y: &'static mut isize| x = y; //~ ERROR closure cannot assign to immutable argument
58+
c1;
4559
}
4660

47-
fn e(x: &mut isize) {
61+
fn f(x: &'static mut isize) {
4862
let c1 = || x = panic!(); //~ ERROR closure cannot assign to immutable argument
63+
c1;
4964
}
5065

5166
fn main() {

src/test/ui/borrowck/borrowck-closures-unique.stderr

+18-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0500]: closure requires unique access to `x` but it is already borrowed
2-
--> $DIR/borrowck-closures-unique.rs:34:14
2+
--> $DIR/borrowck-closures-unique.rs:36:14
33
|
44
LL | let c1 = || get(x);
55
| -- - previous borrow occurs due to use of `x` in closure
@@ -9,11 +9,12 @@ LL | let c2 = || set(x); //~ ERROR closure requires unique access to `x`
99
| ^^ - borrow occurs due to use of `x` in closure
1010
| |
1111
| closure construction occurs here
12+
LL | c1;
1213
LL | }
1314
| - borrow ends here
1415

1516
error[E0500]: closure requires unique access to `x` but it is already borrowed
16-
--> $DIR/borrowck-closures-unique.rs:39:14
17+
--> $DIR/borrowck-closures-unique.rs:42:14
1718
|
1819
LL | let c1 = || get(x);
1920
| -- - previous borrow occurs due to use of `x` in closure
@@ -23,11 +24,12 @@ LL | let c2 = || { get(x); set(x); }; //~ ERROR closure requires unique acce
2324
| ^^ - borrow occurs due to use of `x` in closure
2425
| |
2526
| closure construction occurs here
27+
LL | c1;
2628
LL | }
2729
| - borrow ends here
2830

2931
error[E0524]: two closures require unique access to `x` at the same time
30-
--> $DIR/borrowck-closures-unique.rs:44:14
32+
--> $DIR/borrowck-closures-unique.rs:48:14
3133
|
3234
LL | let c1 = || set(x);
3335
| -- - previous borrow occurs due to use of `x` in closure
@@ -37,11 +39,22 @@ LL | let c2 = || set(x); //~ ERROR two closures require unique access to `x`
3739
| ^^ - borrow occurs due to use of `x` in closure
3840
| |
3941
| second closure is constructed here
42+
LL | c1;
4043
LL | }
4144
| - borrow from first closure ends here
4245

4346
error[E0595]: closure cannot assign to immutable argument `x`
44-
--> $DIR/borrowck-closures-unique.rs:48:14
47+
--> $DIR/borrowck-closures-unique.rs:57:14
48+
|
49+
LL | let c1 = |y: &'static mut isize| x = y; //~ ERROR closure cannot assign to immutable argument
50+
| ^^^^^^^^^^^^^^^^^^^^^^^ cannot borrow mutably
51+
help: consider removing the `&mut`, as it is an immutable binding to a mutable reference
52+
|
53+
LL | x //~ ERROR closure cannot assign to immutable argument
54+
| ^
55+
56+
error[E0595]: closure cannot assign to immutable argument `x`
57+
--> $DIR/borrowck-closures-unique.rs:62:14
4558
|
4659
LL | let c1 = || x = panic!(); //~ ERROR closure cannot assign to immutable argument
4760
| ^^ cannot borrow mutably
@@ -50,7 +63,7 @@ help: consider removing the `&mut`, as it is an immutable binding to a mutable r
5063
LL | x //~ ERROR closure cannot assign to immutable argument
5164
| ^
5265

53-
error: aborting due to 4 previous errors
66+
error: aborting due to 5 previous errors
5467

5568
Some errors occurred: E0500, E0524, E0595.
5669
For more information about an error, try `rustc --explain E0500`.
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error[E0502]: cannot borrow `my_stuff` as mutable because it is also borrowed as immutable
2+
--> $DIR/hashmap-lifetimes.rs:18:5
3+
|
4+
LL | let mut it = my_stuff.iter();
5+
| -------- immutable borrow occurs here
6+
LL | my_stuff.insert(1, 43); //~ ERROR cannot borrow
7+
| ^^^^^^^^^^^^^^^^^^^^^^ mutable borrow occurs here
8+
LL | it;
9+
| -- borrow later used here
10+
11+
error: aborting due to previous error
12+
13+
For more information about this error, try `rustc --explain E0502`.

0 commit comments

Comments
 (0)