Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NLL: Improve move error loop detection (was "First shot at #54015") #54343

Merged
merged 4 commits into from
Oct 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
513 changes: 288 additions & 225 deletions src/librustc_mir/borrow_check/error_reporting.rs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/librustc_mir/borrow_check/nll/explain_borrow/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ impl<'tcx> BorrowExplanation<'tcx> {
},
BorrowExplanation::UsedLaterInLoop(is_in_closure, var_or_use_span) => {
let message = if is_in_closure {
"borrow captured here by closure in later iteration of loop"
"borrow captured here by closure, in later iteration of loop"
} else {
"borrow used here in later iteration of loop"
"borrow used here, in later iteration of loop"
};
err.span_label(var_or_use_span, message);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LL | for &x in &vector {
| -------
| |
| immutable borrow occurs here
| borrow used here in later iteration of loop
| borrow used here, in later iteration of loop
LL | let cap = vector.capacity();
LL | vector.extend(repeat(0)); //~ ERROR cannot borrow
| ^^^^^^^^^^^^^^^^^^^^^^^^ mutable borrow occurs here
Expand All @@ -17,7 +17,7 @@ LL | for &x in &vector {
| -------
| |
| immutable borrow occurs here
| borrow used here in later iteration of loop
| borrow used here, in later iteration of loop
...
LL | vector[1] = 5; //~ ERROR cannot borrow
| ^^^^^^ mutable borrow occurs here
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/borrowck/borrowck-lend-flow-loop.nll.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ LL | borrow(&*v); //~ ERROR cannot borrow
| ^^^ immutable borrow occurs here
LL | }
LL | *x = box 5;
| -- borrow used here in later iteration of loop
| -- borrow used here, in later iteration of loop

error[E0502]: cannot borrow `*v` as immutable because it is also borrowed as mutable
--> $DIR/borrowck-lend-flow-loop.rs:109:16
|
LL | **x += 1;
| -------- borrow used here in later iteration of loop
| -------- borrow used here, in later iteration of loop
LL | borrow(&*v); //~ ERROR cannot borrow
| ^^^ immutable borrow occurs here
LL | if cond2 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0499]: cannot borrow `x` as mutable more than once at a time
LL | 1 => { addr.push(&mut x); } //[ast]~ ERROR [E0499]
| ---- ^^^^^^ second mutable borrow occurs here
| |
| borrow used here in later iteration of loop
| borrow used here, in later iteration of loop
...
LL | _ => { addr.push(&mut x); } //[ast]~ ERROR [E0499]
| ------ first mutable borrow occurs here
Expand All @@ -13,7 +13,7 @@ error[E0499]: cannot borrow `x` as mutable more than once at a time
--> $DIR/borrowck-mut-borrow-linear-errors.rs:25:30
|
LL | 1 => { addr.push(&mut x); } //[ast]~ ERROR [E0499]
| ---- borrow used here in later iteration of loop
| ---- borrow used here, in later iteration of loop
LL | //[mir]~^ ERROR [E0499]
LL | 2 => { addr.push(&mut x); } //[ast]~ ERROR [E0499]
| ^^^^^^ second mutable borrow occurs here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0499]: cannot borrow `x` as mutable more than once at a time
LL | 1 => { addr.push(&mut x); } //[ast]~ ERROR [E0499]
| ---- ^^^^^^ second mutable borrow occurs here
| |
| borrow used here in later iteration of loop
| borrow used here, in later iteration of loop
...
LL | _ => { addr.push(&mut x); } //[ast]~ ERROR [E0499]
| ------ first mutable borrow occurs here
Expand All @@ -13,7 +13,7 @@ error[E0499]: cannot borrow `x` as mutable more than once at a time
--> $DIR/borrowck-mut-borrow-linear-errors.rs:25:30
|
LL | 1 => { addr.push(&mut x); } //[ast]~ ERROR [E0499]
| ---- borrow used here in later iteration of loop
| ---- borrow used here, in later iteration of loop
LL | //[mir]~^ ERROR [E0499]
LL | 2 => { addr.push(&mut x); } //[ast]~ ERROR [E0499]
| ^^^^^^ second mutable borrow occurs here
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/borrowck/issue-41962.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ error[E0382]: use of moved value (Mir)
--> $DIR/issue-41962.rs:17:21
|
LL | if let Some(thing) = maybe {
| ^^^^^ value moved here in previous iteration of loop
| ^^^^^ value moved here, in previous iteration of loop
|
= note: move occurs because value has type `std::vec::Vec<bool>`, which does not implement the `Copy` trait

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/borrowck/mut-borrow-outside-loop.nll.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ LL | let inner_second = &mut inner_void; //~ ERROR cannot borrow
| ^^^^^^^^^^^^^^^ second mutable borrow occurs here
LL | inner_second.use_mut();
LL | inner_first.use_mut();
| ----------- borrow used here in later iteration of loop
| ----------- borrow used here, in later iteration of loop

error: aborting due to 2 previous errors

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-12041.nll.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0382]: use of moved value: `tx`
--> $DIR/issue-12041.rs:18:22
|
LL | let tx = tx;
| ^^ value moved here in previous iteration of loop
| ^^ value moved here, in previous iteration of loop
|
= note: move occurs because `tx` has type `std::sync::mpsc::Sender<i32>`, which does not implement the `Copy` trait

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LL | let v: Vec<&str> = line.split_whitespace().collect();
| ^^^^ borrowed value does not live long enough
LL | //~^ ERROR `line` does not live long enough
LL | println!("accumulator before add_assign {:?}", acc.map);
| ------- borrow used here in later iteration of loop
| ------- borrow used here, in later iteration of loop
...
LL | }
| - `line` dropped here while still borrowed
Expand Down
11 changes: 11 additions & 0 deletions src/test/ui/liveness/liveness-move-call-arg.nll.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error[E0382]: use of moved value: `x`
--> $DIR/liveness-move-call-arg.rs:19:14
|
LL | take(x); //~ ERROR use of moved value: `x`
| ^ value moved here, in previous iteration of loop
|
= note: move occurs because `x` has type `std::boxed::Box<isize>`, which does not implement the `Copy` trait

error: aborting due to previous error

For more information about this error, try `rustc --explain E0382`.
11 changes: 11 additions & 0 deletions src/test/ui/liveness/liveness-move-in-loop.nll.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error[E0382]: use of moved value: `y`
--> $DIR/liveness-move-in-loop.rs:21:25
|
LL | x = y; //~ ERROR use of moved value
| ^ value moved here, in previous iteration of loop
|
= note: move occurs because `y` has type `std::boxed::Box<isize>`, which does not implement the `Copy` trait

error: aborting due to previous error

For more information about this error, try `rustc --explain E0382`.
2 changes: 1 addition & 1 deletion src/test/ui/liveness/liveness-move-in-while.nll.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0382]: borrow of moved value: `y`
LL | println!("{}", y); //~ ERROR use of moved value: `y`
| ^ value borrowed here after move
LL | while true { while true { while true { x = y; x.clone(); } } }
| - value moved here
| - value moved here, in previous iteration of loop
|
= note: move occurs because `y` has type `std::boxed::Box<isize>`, which does not implement the `Copy` trait

Expand Down
11 changes: 11 additions & 0 deletions src/test/ui/moves/move-in-guard-2.nll.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error[E0382]: use of moved value: `x`
--> $DIR/move-in-guard-2.rs:20:24
|
LL | (_, 2) if take(x) => (), //~ ERROR use of moved value: `x`
| ^ value moved here, in previous iteration of loop
|
= note: move occurs because `x` has type `std::boxed::Box<i32>`, which does not implement the `Copy` trait

error: aborting due to previous error

For more information about this error, try `rustc --explain E0382`.
2 changes: 1 addition & 1 deletion src/test/ui/nll/closures-in-loops.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0382]: use of moved value: `x`
LL | || x; //~ ERROR
| ^^ - use occurs due to use in closure
| |
| value moved into closure here in previous iteration of loop
| value moved into closure here, in previous iteration of loop
|
= note: move occurs because `x` has type `std::string::String`, which does not implement the `Copy` trait

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/nll/issue-53807.nll.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0382]: use of moved value
--> $DIR/issue-53807.rs:14:21
|
LL | if let Some(thing) = maybe {
| ^^^^^ value moved here in previous iteration of loop
| ^^^^^ value moved here, in previous iteration of loop
|
= note: move occurs because value has type `std::vec::Vec<bool>`, which does not implement the `Copy` trait

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | foo.mutate();
| ^^^^^^^^^^^^ mutable borrow occurs here
LL | //~^ ERROR cannot borrow `foo` as mutable
LL | println!("foo={:?}", *string);
| ------- borrow used here in later iteration of loop
| ------- borrow used here, in later iteration of loop

error: aborting due to previous error

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0597]: `x` does not live long enough
--> $DIR/regions-escape-loop-via-variable.rs:21:13
|
LL | let x = 1 + *p;
| -- borrow used here in later iteration of loop
| -- borrow used here, in later iteration of loop
LL | p = &x;
| ^^ borrowed value does not live long enough
LL | }
Expand Down
8 changes: 4 additions & 4 deletions src/test/ui/span/regions-escape-loop-via-vec.nll.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | while x < 10 { //~ ERROR cannot use `x` because it was mutably borrowed
| ^ use of borrowed `x`
LL | let mut z = x; //~ ERROR cannot use `x` because it was mutably borrowed
LL | _y.push(&mut z);
| -- borrow used here in later iteration of loop
| -- borrow used here, in later iteration of loop

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

error[E0597]: `z` does not live long enough
--> $DIR/regions-escape-loop-via-vec.rs:17:17
|
LL | _y.push(&mut z);
| -- ^^^^^^ borrowed value does not live long enough
| |
| borrow used here in later iteration of loop
| borrow used here, in later iteration of loop
...
LL | }
| - `z` dropped here while still borrowed
Expand All @@ -38,7 +38,7 @@ LL | let mut _y = vec![&mut x];
| ------ borrow of `x` occurs here
...
LL | _y.push(&mut z);
| -- borrow used here in later iteration of loop
| -- borrow used here, in later iteration of loop
LL | //~^ ERROR `z` does not live long enough
LL | x += 1; //~ ERROR cannot assign
| ^^^^^^ use of borrowed `x`
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/vec/vec-mut-iter-borrow.nll.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LL | for x in &mut xs {
| -------
| |
| first mutable borrow occurs here
| borrow used here in later iteration of loop
| borrow used here, in later iteration of loop
LL | xs.push(1) //~ ERROR cannot borrow `xs`
| ^^ second mutable borrow occurs here

Expand Down