Skip to content

Commit dd9f84f

Browse files
committed
Removed ignore-test-compare-mode-nll from borrowck-closures-unique.rs
by strengthening the tests there. In almost all cases the strengthening amount to just encoding a use that models the original lexical lifetime. A more invasive revision was made in one case where it seems the actual issue is MIR-borrowck's greater "knowledge" of unreachable code in the control flow...
1 parent a573305 commit dd9f84f

File tree

3 files changed

+90
-8
lines changed

3 files changed

+90
-8
lines changed
Lines changed: 54 additions & 0 deletions
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

Lines changed: 18 additions & 3 deletions
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

Lines changed: 18 additions & 5 deletions
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`.

0 commit comments

Comments
 (0)