Skip to content

Commit b3d99cb

Browse files
authored
Rollup merge of rust-lang#73600 - Aaron1011:fix/move-in-macro, r=ecstatic-morse
Fix spurious 'value moved here in previous iteration of loop' messages Fixes rust-lang#46099 Previously, we would check the 'move' and 'use' spans to see if we should emit this message. However, this can give false positives when macros are involved, since two distinct expressions may end up with the same span. Instead, we check the actual MIR `Location`, which eliminates false positives.
2 parents 490d820 + 953104e commit b3d99cb

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

src/librustc_mir/borrow_check/diagnostics/conflict_errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
138138

139139
let move_msg = if move_spans.for_closure() { " into closure" } else { "" };
140140

141-
if span == move_span {
141+
if location == move_out.source {
142142
err.span_label(
143143
span,
144144
format!("value moved{} here, in previous iteration of loop", move_msg),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Regression test for issue #46099
2+
// Tests that we don't emit spurious
3+
// 'value moved in previous iteration of loop' message
4+
5+
macro_rules! test {
6+
($v:expr) => {{
7+
drop(&$v);
8+
$v
9+
}}
10+
}
11+
12+
fn main() {
13+
let b = Box::new(true);
14+
test!({b}); //~ ERROR use of moved value
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0382]: use of moved value: `b`
2+
--> $DIR/issue-46099-move-in-macro.rs:14:12
3+
|
4+
LL | let b = Box::new(true);
5+
| - move occurs because `b` has type `std::boxed::Box<bool>`, which does not implement the `Copy` trait
6+
LL | test!({b});
7+
| ^
8+
| |
9+
| value moved here
10+
| value used here after move
11+
12+
error: aborting due to previous error
13+
14+
For more information about this error, try `rustc --explain E0382`.

src/test/ui/moves/move-in-guard-2.stderr

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ LL | let x: Box<_> = box 1;
55
| - move occurs because `x` has type `std::boxed::Box<i32>`, which does not implement the `Copy` trait
66
...
77
LL | (_, 2) if take(x) => (),
8-
| ^ value moved here, in previous iteration of loop
8+
| ^
9+
| |
10+
| value moved here
11+
| value used here after move
912

1013
error: aborting due to previous error
1114

0 commit comments

Comments
 (0)