Skip to content

Commit 22c95b6

Browse files
authored
Rollup merge of rust-lang#61144 - estebank:issue-61108, r=matthewjasper
Suggest borrowing for loop head on move error Fix rust-lang#61108.
2 parents f9818b7 + 274b7e4 commit 22c95b6

File tree

4 files changed

+39
-18
lines changed

4 files changed

+39
-18
lines changed

src/librustc_mir/borrow_check/conflict_errors.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -158,18 +158,6 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
158158
span,
159159
format!("value moved{} here, in previous iteration of loop", move_msg),
160160
);
161-
if Some(CompilerDesugaringKind::ForLoop) == span.compiler_desugaring_kind() {
162-
if let Ok(snippet) = self.infcx.tcx.sess.source_map()
163-
.span_to_snippet(span)
164-
{
165-
err.span_suggestion(
166-
move_span,
167-
"consider borrowing this to avoid moving it into the for loop",
168-
format!("&{}", snippet),
169-
Applicability::MaybeIncorrect,
170-
);
171-
}
172-
}
173161
is_loop_move = true;
174162
} else if move_site.traversed_back_edge {
175163
err.span_label(
@@ -185,7 +173,17 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
185173
&mut err,
186174
format!("variable moved due to use{}", move_spans.describe()),
187175
);
188-
};
176+
}
177+
if Some(CompilerDesugaringKind::ForLoop) == move_span.compiler_desugaring_kind() {
178+
if let Ok(snippet) = self.infcx.tcx.sess.source_map().span_to_snippet(span) {
179+
err.span_suggestion(
180+
move_span,
181+
"consider borrowing to avoid moving into the for loop",
182+
format!("&{}", snippet),
183+
Applicability::MaybeIncorrect,
184+
);
185+
}
186+
}
189187
}
190188

191189
use_spans.var_span_label(

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fn main() {
2+
let mut bad_letters = vec!['e', 't', 'o', 'i'];
3+
for l in bad_letters {
4+
// something here
5+
}
6+
bad_letters.push('s'); //~ ERROR borrow of moved value: `bad_letters`
7+
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0382]: borrow of moved value: `bad_letters`
2+
--> $DIR/issue-61108.rs:6:5
3+
|
4+
LL | let mut bad_letters = vec!['e', 't', 'o', 'i'];
5+
| --------------- move occurs because `bad_letters` has type `std::vec::Vec<char>`, which does not implement the `Copy` trait
6+
LL | for l in bad_letters {
7+
| -----------
8+
| |
9+
| value moved here
10+
| help: consider borrowing to avoid moving into the for loop: `&bad_letters`
11+
...
12+
LL | bad_letters.push('s');
13+
| ^^^^^^^^^^^ value borrowed here after move
14+
15+
error: aborting due to previous error
16+
17+
For more information about this error, try `rustc --explain E0382`.

src/test/ui/suggestions/borrow-for-loop-head.stderr

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@ LL | let a = vec![1, 2, 3];
1313
| - move occurs because `a` has type `std::vec::Vec<i32>`, which does not implement the `Copy` trait
1414
LL | for i in &a {
1515
LL | for j in a {
16-
| ^ value moved here, in previous iteration of loop
17-
help: consider borrowing this to avoid moving it into the for loop
18-
|
19-
LL | for j in &a {
20-
| ^^
16+
| ^
17+
| |
18+
| value moved here, in previous iteration of loop
19+
| help: consider borrowing to avoid moving into the for loop: `&a`
2120

2221
error: aborting due to 2 previous errors
2322

0 commit comments

Comments
 (0)