about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMazdak Farrokhzad <twingoow@gmail.com>2019-09-21 16:01:35 +0200
committerGitHub <noreply@github.com>2019-09-21 16:01:35 +0200
commit97ca0737c6b63507945e9198177ebadf9d4eff82 (patch)
tree931af44227c59522d720d6277caa4a411cbdf747
parent1486b7f91ce18962038ef36d2479e57e27a569ed (diff)
parentc3f72d1c098a61f26271021460b5ae420ff28ae8 (diff)
downloadrust-97ca0737c6b63507945e9198177ebadf9d4eff82.tar.gz
rust-97ca0737c6b63507945e9198177ebadf9d4eff82.zip
Rollup merge of #64642 - cuviper:move-for-loop-snippet, r=varkor
Fix the span used to suggest avoiding for-loop moves

It was using the snippet from the "use" span, which often renders the
same, but with closures that snippet is on the start of the closure
where the value is captured. We should be using the snippet from the
span where it was moved into the `for` loop, which is `move_span`.

Fixes #64559.
-rw-r--r--src/librustc_mir/borrow_check/conflict_errors.rs3
-rw-r--r--src/test/ui/issues/issue-64559.rs6
-rw-r--r--src/test/ui/issues/issue-64559.stderr18
3 files changed, 26 insertions, 1 deletions
diff --git a/src/librustc_mir/borrow_check/conflict_errors.rs b/src/librustc_mir/borrow_check/conflict_errors.rs
index 81359c6a46e..599a0ad0d0c 100644
--- a/src/librustc_mir/borrow_check/conflict_errors.rs
+++ b/src/librustc_mir/borrow_check/conflict_errors.rs
@@ -180,7 +180,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
                     );
                 }
                 if Some(DesugaringKind::ForLoop) == move_span.desugaring_kind() {
-                    if let Ok(snippet) = self.infcx.tcx.sess.source_map().span_to_snippet(span) {
+                    let sess = self.infcx.tcx.sess;
+                    if let Ok(snippet) = sess.source_map().span_to_snippet(move_span) {
                         err.span_suggestion(
                             move_span,
                             "consider borrowing to avoid moving into the for loop",
diff --git a/src/test/ui/issues/issue-64559.rs b/src/test/ui/issues/issue-64559.rs
new file mode 100644
index 00000000000..71e054b5d98
--- /dev/null
+++ b/src/test/ui/issues/issue-64559.rs
@@ -0,0 +1,6 @@
+fn main() {
+    let orig = vec![true];
+    for _val in orig {}
+    let _closure = || orig;
+    //~^ ERROR use of moved value: `orig`
+}
diff --git a/src/test/ui/issues/issue-64559.stderr b/src/test/ui/issues/issue-64559.stderr
new file mode 100644
index 00000000000..3c685dc8d08
--- /dev/null
+++ b/src/test/ui/issues/issue-64559.stderr
@@ -0,0 +1,18 @@
+error[E0382]: use of moved value: `orig`
+  --> $DIR/issue-64559.rs:4:20
+   |
+LL |     let orig = vec![true];
+   |         ---- move occurs because `orig` has type `std::vec::Vec<bool>`, which does not implement the `Copy` trait
+LL |     for _val in orig {}
+   |                 ----
+   |                 |
+   |                 value moved here
+   |                 help: consider borrowing to avoid moving into the for loop: `&orig`
+LL |     let _closure = || orig;
+   |                    ^^ ---- use occurs due to use in closure
+   |                    |
+   |                    value used here after move
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0382`.