From 274b7e49e0fb9bb896386fd051e70090a2c8761d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Fri, 24 May 2019 18:23:43 -0700 Subject: [PATCH] Suggest borrowing for loop head on move error --- .../borrow_check/conflict_errors.rs | 24 +++++++++---------- src/test/ui/issues/issue-61108.rs | 7 ++++++ src/test/ui/issues/issue-61108.stderr | 17 +++++++++++++ .../suggestions/borrow-for-loop-head.stderr | 9 ++++--- 4 files changed, 39 insertions(+), 18 deletions(-) create mode 100644 src/test/ui/issues/issue-61108.rs create mode 100644 src/test/ui/issues/issue-61108.stderr diff --git a/src/librustc_mir/borrow_check/conflict_errors.rs b/src/librustc_mir/borrow_check/conflict_errors.rs index b00a75bb569..4ff6fa829ff 100644 --- a/src/librustc_mir/borrow_check/conflict_errors.rs +++ b/src/librustc_mir/borrow_check/conflict_errors.rs @@ -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( @@ -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( diff --git a/src/test/ui/issues/issue-61108.rs b/src/test/ui/issues/issue-61108.rs new file mode 100644 index 00000000000..0a883b95818 --- /dev/null +++ b/src/test/ui/issues/issue-61108.rs @@ -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` +} diff --git a/src/test/ui/issues/issue-61108.stderr b/src/test/ui/issues/issue-61108.stderr new file mode 100644 index 00000000000..8523a6f6548 --- /dev/null +++ b/src/test/ui/issues/issue-61108.stderr @@ -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`, 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`. diff --git a/src/test/ui/suggestions/borrow-for-loop-head.stderr b/src/test/ui/suggestions/borrow-for-loop-head.stderr index 10287f59cce..36bced9e433 100644 --- a/src/test/ui/suggestions/borrow-for-loop-head.stderr +++ b/src/test/ui/suggestions/borrow-for-loop-head.stderr @@ -13,11 +13,10 @@ LL | let a = vec![1, 2, 3]; | - move occurs because `a` has type `std::vec::Vec`, 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