about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMazdak Farrokhzad <twingoow@gmail.com>2019-05-26 02:13:27 +0200
committerGitHub <noreply@github.com>2019-05-26 02:13:27 +0200
commitb4a3d44a873302d89873e89b825204ae20cbcc09 (patch)
treeb26285de7c9c1ef875ac326c76906cb9d84dfc21
parentf185ee5fda65a82d0efdfe0504e2712cecf18cdf (diff)
parent274b7e49e0fb9bb896386fd051e70090a2c8761d (diff)
downloadrust-b4a3d44a873302d89873e89b825204ae20cbcc09.tar.gz
rust-b4a3d44a873302d89873e89b825204ae20cbcc09.zip
Rollup merge of #61144 - estebank:issue-61108, r=matthewjasper
Suggest borrowing for loop head on move error

Fix #61108.
-rw-r--r--src/librustc_mir/borrow_check/conflict_errors.rs24
-rw-r--r--src/test/ui/issues/issue-61108.rs7
-rw-r--r--src/test/ui/issues/issue-61108.stderr17
-rw-r--r--src/test/ui/suggestions/borrow-for-loop-head.stderr9
4 files changed, 39 insertions, 18 deletions
diff --git a/src/librustc_mir/borrow_check/conflict_errors.rs b/src/librustc_mir/borrow_check/conflict_errors.rs
index 4253962f144..8022d1f0c73 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<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`.
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<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