Skip to content

Commit 1005f3b

Browse files
committed
Auto merge of #56113 - spastorino:erroneous-loop-diagnostic-in-nll, r=pnkfelix
Erroneous loop diagnostic in nll Closes #53773 r? @nikomatsakis
2 parents c6fd027 + a12982c commit 1005f3b

14 files changed

+361
-194
lines changed

src/librustc_mir/borrow_check/nll/explain_borrow/find_use.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,7 @@ impl<'cx, 'gcx, 'tcx> UseFinder<'cx, 'gcx, 'tcx> {
6565

6666
None => {
6767
if p.statement_index < block_data.statements.len() {
68-
queue.push_back(Location {
69-
statement_index: p.statement_index + 1,
70-
..p
71-
});
68+
queue.push_back(p.successor_within_block());
7269
} else {
7370
queue.extend(
7471
block_data

src/librustc_mir/borrow_check/nll/explain_borrow/mod.rs

+278-173
Large diffs are not rendered by default.

src/test/ui/borrowck/borrowck-for-loop-head-linkage.nll.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | for &x in &vector {
55
| -------
66
| |
77
| immutable borrow occurs here
8-
| immutable borrow used here, in later iteration of loop
8+
| immutable borrow later used here
99
LL | let cap = vector.capacity();
1010
LL | vector.extend(repeat(0)); //~ ERROR cannot borrow
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^ mutable borrow occurs here
@@ -17,7 +17,7 @@ LL | for &x in &vector {
1717
| -------
1818
| |
1919
| immutable borrow occurs here
20-
| immutable borrow used here, in later iteration of loop
20+
| immutable borrow later used here
2121
...
2222
LL | vector[1] = 5; //~ ERROR cannot borrow
2323
| ^^^^^^ mutable borrow occurs here

src/test/ui/borrowck/borrowck-lend-flow-loop.nll.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ LL | borrow(&*v); //[ast]~ ERROR cannot borrow
88
| ^^^ immutable borrow occurs here
99
...
1010
LL | *x = box 5;
11-
| -- mutable borrow used here, in later iteration of loop
11+
| -- mutable borrow later used here
1212

1313
error[E0502]: cannot borrow `*v` as immutable because it is also borrowed as mutable
1414
--> $DIR/borrowck-lend-flow-loop.rs:109:16
1515
|
1616
LL | **x += 1;
17-
| -------- mutable borrow used here, in later iteration of loop
17+
| -------- mutable borrow later used here
1818
LL | borrow(&*v); //[ast]~ ERROR cannot borrow
1919
| ^^^ immutable borrow occurs here
2020
...

src/test/ui/borrowck/borrowck-mut-borrow-linear-errors.ast.nll.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0499]: cannot borrow `x` as mutable more than once at a time
44
LL | 1 => { addr.push(&mut x); } //[ast]~ ERROR [E0499]
55
| ---- ^^^^^^ second mutable borrow occurs here
66
| |
7-
| first borrow used here, in later iteration of loop
7+
| first borrow later used here
88
...
99
LL | _ => { addr.push(&mut x); } //[ast]~ ERROR [E0499]
1010
| ------ first mutable borrow occurs here
@@ -13,7 +13,7 @@ error[E0499]: cannot borrow `x` as mutable more than once at a time
1313
--> $DIR/borrowck-mut-borrow-linear-errors.rs:15:30
1414
|
1515
LL | 1 => { addr.push(&mut x); } //[ast]~ ERROR [E0499]
16-
| ---- first borrow used here, in later iteration of loop
16+
| ---- first borrow later used here
1717
LL | //[mir]~^ ERROR [E0499]
1818
LL | 2 => { addr.push(&mut x); } //[ast]~ ERROR [E0499]
1919
| ^^^^^^ second mutable borrow occurs here

src/test/ui/borrowck/borrowck-mut-borrow-linear-errors.mir.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0499]: cannot borrow `x` as mutable more than once at a time
44
LL | 1 => { addr.push(&mut x); } //[ast]~ ERROR [E0499]
55
| ---- ^^^^^^ second mutable borrow occurs here
66
| |
7-
| first borrow used here, in later iteration of loop
7+
| first borrow later used here
88
...
99
LL | _ => { addr.push(&mut x); } //[ast]~ ERROR [E0499]
1010
| ------ first mutable borrow occurs here
@@ -13,7 +13,7 @@ error[E0499]: cannot borrow `x` as mutable more than once at a time
1313
--> $DIR/borrowck-mut-borrow-linear-errors.rs:15:30
1414
|
1515
LL | 1 => { addr.push(&mut x); } //[ast]~ ERROR [E0499]
16-
| ---- first borrow used here, in later iteration of loop
16+
| ---- first borrow later used here
1717
LL | //[mir]~^ ERROR [E0499]
1818
LL | 2 => { addr.push(&mut x); } //[ast]~ ERROR [E0499]
1919
| ^^^^^^ second mutable borrow occurs here

src/test/ui/borrowck/mut-borrow-outside-loop.nll.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ LL | let inner_second = &mut inner_void; //~ ERROR cannot borrow
1717
| ^^^^^^^^^^^^^^^ second mutable borrow occurs here
1818
LL | inner_second.use_mut();
1919
LL | inner_first.use_mut();
20-
| ----------- first borrow used here, in later iteration of loop
20+
| ----------- first borrow later used here
2121

2222
error: aborting due to 2 previous errors
2323

src/test/ui/issues/issue-52126-assign-op-invariance.nll.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | let v: Vec<&str> = line.split_whitespace().collect();
55
| ^^^^ borrowed value does not live long enough
66
...
77
LL | acc += cnt2;
8-
| --- borrow used here, in later iteration of loop
8+
| --- borrow later used here
99
...
1010
LL | }
1111
| - `line` dropped here while still borrowed

src/test/ui/nll/issue-53773.rs

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#![feature(nll)]
2+
3+
struct Archive;
4+
struct ArchiveIterator<'a> {
5+
x: &'a Archive,
6+
}
7+
struct ArchiveChild<'a> {
8+
x: &'a Archive,
9+
}
10+
11+
struct A {
12+
raw: &'static mut Archive,
13+
}
14+
struct Iter<'a> {
15+
raw: &'a mut ArchiveIterator<'a>,
16+
}
17+
struct C<'a> {
18+
raw: &'a mut ArchiveChild<'a>,
19+
}
20+
21+
impl A {
22+
pub fn iter(&self) -> Iter<'_> {
23+
panic!()
24+
}
25+
}
26+
impl Drop for A {
27+
fn drop(&mut self) {}
28+
}
29+
impl<'a> Drop for C<'a> {
30+
fn drop(&mut self) {}
31+
}
32+
33+
impl<'a> Iterator for Iter<'a> {
34+
type Item = C<'a>;
35+
fn next(&mut self) -> Option<C<'a>> {
36+
panic!()
37+
}
38+
}
39+
40+
fn error(archive: &A) {
41+
let mut members: Vec<&mut ArchiveChild<'_>> = vec![];
42+
for child in archive.iter() {
43+
members.push(child.raw);
44+
//~^ ERROR borrow may still be in use when destructor runs [E0713]
45+
}
46+
members.len();
47+
}
48+
49+
fn main() {}

src/test/ui/nll/issue-53773.stderr

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error[E0713]: borrow may still be in use when destructor runs
2+
--> $DIR/issue-53773.rs:43:22
3+
|
4+
LL | members.push(child.raw);
5+
| ^^^^^^^^^
6+
LL | //~^ ERROR borrow may still be in use when destructor runs [E0713]
7+
LL | }
8+
| - here, drop of `child` needs exclusive access to `*child.raw`, because the type `C<'_>` implements the `Drop` trait
9+
LL | members.len();
10+
| ------- borrow later used here
11+
|
12+
= note: consider using a `let` binding to create a longer lived value
13+
14+
error: aborting due to previous error
15+
16+
For more information about this error, try `rustc --explain E0713`.

src/test/ui/rfc-2005-default-binding-mode/borrowck-issue-49631.nll.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | foo.mutate();
77
| ^^^^^^^^^^^^ mutable borrow occurs here
88
LL | //~^ ERROR cannot borrow `foo` as mutable
99
LL | println!("foo={:?}", *string);
10-
| ------- immutable borrow used here, in later iteration of loop
10+
| ------- immutable borrow later used here
1111

1212
error: aborting due to previous error
1313

src/test/ui/span/regions-escape-loop-via-variable.nll.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0597]: `x` does not live long enough
22
--> $DIR/regions-escape-loop-via-variable.rs:11:13
33
|
44
LL | let x = 1 + *p;
5-
| -- borrow used here, in later iteration of loop
5+
| -- borrow later used here
66
LL | p = &x;
77
| ^^ borrowed value does not live long enough
88
LL | }

src/test/ui/span/regions-escape-loop-via-vec.nll.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | while x < 10 { //~ ERROR cannot use `x` because it was mutably borrowed
77
| ^ use of borrowed `x`
88
LL | let mut z = x; //~ ERROR cannot use `x` because it was mutably borrowed
99
LL | _y.push(&mut z);
10-
| -- borrow used here, in later iteration of loop
10+
| -- borrow later used here
1111

1212
error[E0503]: cannot use `x` because it was mutably borrowed
1313
--> $DIR/regions-escape-loop-via-vec.rs:6:21
@@ -18,15 +18,15 @@ LL | while x < 10 { //~ ERROR cannot use `x` because it was mutably borrowed
1818
LL | let mut z = x; //~ ERROR cannot use `x` because it was mutably borrowed
1919
| ^ use of borrowed `x`
2020
LL | _y.push(&mut z);
21-
| -- borrow used here, in later iteration of loop
21+
| -- borrow later used here
2222

2323
error[E0597]: `z` does not live long enough
2424
--> $DIR/regions-escape-loop-via-vec.rs:7:17
2525
|
2626
LL | _y.push(&mut z);
2727
| -- ^^^^^^ borrowed value does not live long enough
2828
| |
29-
| borrow used here, in later iteration of loop
29+
| borrow later used here
3030
...
3131
LL | }
3232
| - `z` dropped here while still borrowed
@@ -38,7 +38,7 @@ LL | let mut _y = vec![&mut x];
3838
| ------ borrow of `x` occurs here
3939
...
4040
LL | _y.push(&mut z);
41-
| -- borrow used here, in later iteration of loop
41+
| -- borrow later used here
4242
LL | //~^ ERROR `z` does not live long enough
4343
LL | x += 1; //~ ERROR cannot assign
4444
| ^^^^^^ use of borrowed `x`

src/test/ui/vec/vec-mut-iter-borrow.nll.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | for x in &mut xs {
55
| -------
66
| |
77
| first mutable borrow occurs here
8-
| first borrow used here, in later iteration of loop
8+
| first borrow later used here
99
LL | xs.push(1) //~ ERROR cannot borrow `xs`
1010
| ^^ second mutable borrow occurs here
1111

0 commit comments

Comments
 (0)