Skip to content

Commit 97ca073

Browse files
authored
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.
2 parents 1486b7f + c3f72d1 commit 97ca073

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

src/librustc_mir/borrow_check/conflict_errors.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
180180
);
181181
}
182182
if Some(DesugaringKind::ForLoop) == move_span.desugaring_kind() {
183-
if let Ok(snippet) = self.infcx.tcx.sess.source_map().span_to_snippet(span) {
183+
let sess = self.infcx.tcx.sess;
184+
if let Ok(snippet) = sess.source_map().span_to_snippet(move_span) {
184185
err.span_suggestion(
185186
move_span,
186187
"consider borrowing to avoid moving into the for loop",

src/test/ui/issues/issue-64559.rs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fn main() {
2+
let orig = vec![true];
3+
for _val in orig {}
4+
let _closure = || orig;
5+
//~^ ERROR use of moved value: `orig`
6+
}

src/test/ui/issues/issue-64559.stderr

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error[E0382]: use of moved value: `orig`
2+
--> $DIR/issue-64559.rs:4:20
3+
|
4+
LL | let orig = vec![true];
5+
| ---- move occurs because `orig` has type `std::vec::Vec<bool>`, which does not implement the `Copy` trait
6+
LL | for _val in orig {}
7+
| ----
8+
| |
9+
| value moved here
10+
| help: consider borrowing to avoid moving into the for loop: `&orig`
11+
LL | let _closure = || orig;
12+
| ^^ ---- use occurs due to use in closure
13+
| |
14+
| value used here after move
15+
16+
error: aborting due to previous error
17+
18+
For more information about this error, try `rustc --explain E0382`.

0 commit comments

Comments
 (0)