Skip to content

Commit

Permalink
Rollup merge of rust-lang#61144 - estebank:issue-61108, r=matthewjasper
Browse files Browse the repository at this point in the history
Suggest borrowing for loop head on move error

Fix rust-lang#61108.
  • Loading branch information
Centril committed May 26, 2019
2 parents f185ee5 + 274b7e4 commit b4a3d44
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 18 deletions.
24 changes: 11 additions & 13 deletions src/librustc_mir/borrow_check/conflict_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,18 +158,6 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
span,
format!("value moved{} here, in previous iteration of loop", move_msg),
);
if Some(CompilerDesugaringKind::ForLoop) == span.compiler_desugaring_kind() {
if let Ok(snippet) = self.infcx.tcx.sess.source_map()
.span_to_snippet(span)
{
err.span_suggestion(
move_span,
"consider borrowing this to avoid moving it into the for loop",
format!("&{}", snippet),
Applicability::MaybeIncorrect,
);
}
}
is_loop_move = true;
} else if move_site.traversed_back_edge {
err.span_label(
Expand All @@ -185,7 +173,17 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
&mut err,
format!("variable moved due to use{}", move_spans.describe()),
);
};
}
if Some(CompilerDesugaringKind::ForLoop) == move_span.compiler_desugaring_kind() {
if let Ok(snippet) = self.infcx.tcx.sess.source_map().span_to_snippet(span) {
err.span_suggestion(
move_span,
"consider borrowing to avoid moving into the for loop",
format!("&{}", snippet),
Applicability::MaybeIncorrect,
);
}
}
}

use_spans.var_span_label(
Expand Down
7 changes: 7 additions & 0 deletions src/test/ui/issues/issue-61108.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fn main() {
let mut bad_letters = vec!['e', 't', 'o', 'i'];
for l in bad_letters {
// something here
}
bad_letters.push('s'); //~ ERROR borrow of moved value: `bad_letters`
}
17 changes: 17 additions & 0 deletions src/test/ui/issues/issue-61108.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error[E0382]: borrow of moved value: `bad_letters`
--> $DIR/issue-61108.rs:6:5
|
LL | let mut bad_letters = vec!['e', 't', 'o', 'i'];
| --------------- move occurs because `bad_letters` has type `std::vec::Vec<char>`, which does not implement the `Copy` trait
LL | for l in bad_letters {
| -----------
| |
| value moved here
| help: consider borrowing to avoid moving into the for loop: `&bad_letters`
...
LL | bad_letters.push('s');
| ^^^^^^^^^^^ value borrowed here after move

error: aborting due to previous error

For more information about this error, try `rustc --explain E0382`.
9 changes: 4 additions & 5 deletions src/test/ui/suggestions/borrow-for-loop-head.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ LL | let a = vec![1, 2, 3];
| - move occurs because `a` has type `std::vec::Vec<i32>`, which does not implement the `Copy` trait
LL | for i in &a {
LL | for j in a {
| ^ value moved here, in previous iteration of loop
help: consider borrowing this to avoid moving it into the for loop
|
LL | for j in &a {
| ^^
| ^
| |
| value moved here, in previous iteration of loop
| help: consider borrowing to avoid moving into the for loop: `&a`

error: aborting due to 2 previous errors

Expand Down

0 comments on commit b4a3d44

Please sign in to comment.