Skip to content

Commit 551daa6

Browse files
lzcuntmejrs
authored and
mejrs
committed
Migrate borrow of moved value diagnostic
1 parent afc9228 commit 551daa6

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

compiler/rustc_error_messages/locales/en-US/mir_build.ftl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,3 +287,8 @@ mir_build_irrefutable_let_patterns_while_let = irrefutable `while let` {$count -
287287
*[other] these patterns
288288
} will always match, so the loop will never exit
289289
.help = consider instead using a `loop {"{"} ... {"}"}` with a `let` inside it
290+
291+
mir_build_borrow_of_moved_value = borrow of moved value
292+
.label = value moved into `{$name}` here
293+
.occurs_because_label = move occurs because `{$name}` has type `{$ty}` which does not implement the `Copy` trait
294+
.value_borrowed_label = value borrowed here after move

compiler/rustc_mir_build/src/errors.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,3 +562,17 @@ pub struct IrrefutableLetPatternsLetElse {
562562
pub struct IrrefutableLetPatternsWhileLet {
563563
pub count: usize,
564564
}
565+
566+
#[derive(SessionDiagnostic)]
567+
#[diag(mir_build::borrow_of_moved_value)]
568+
pub struct BorrowOfMovedValue<'tcx> {
569+
#[primary_span]
570+
pub span: Span,
571+
#[label]
572+
#[label(mir_build::occurs_because_label)]
573+
pub binding_span: Span,
574+
#[label(mir_build::value_borrowed_label)]
575+
pub conflicts_ref: Vec<Span>,
576+
pub name: Ident,
577+
pub ty: Ty<'tcx>,
578+
}

compiler/rustc_mir_build/src/thir/pattern/check_match.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -962,16 +962,13 @@ fn check_borrow_conflicts_in_at_patterns(cx: &MatchVisitor<'_, '_, '_>, pat: &Pa
962962
}
963963
});
964964
if !conflicts_ref.is_empty() {
965-
let occurs_because = format!(
966-
"move occurs because `{}` has type `{}` which does not implement the `Copy` trait",
965+
sess.emit_err(BorrowOfMovedValue {
966+
span: pat.span,
967+
binding_span,
968+
conflicts_ref,
967969
name,
968-
typeck_results.node_type(pat.hir_id),
969-
);
970-
sess.struct_span_err(pat.span, "borrow of moved value")
971-
.span_label(binding_span, format!("value moved into `{}` here", name))
972-
.span_label(binding_span, occurs_because)
973-
.span_labels(conflicts_ref, "value borrowed here after move")
974-
.emit();
970+
ty: typeck_results.node_type(pat.hir_id),
971+
});
975972
}
976973
return;
977974
}

0 commit comments

Comments
 (0)