Skip to content

Commit 4f5fdac

Browse files
committed
Migrate borrow of moved value diagnostic
1 parent 3b57a3e commit 4f5fdac

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
@@ -955,16 +955,13 @@ fn check_borrow_conflicts_in_at_patterns(cx: &MatchVisitor<'_, '_, '_>, pat: &Pa
955955
}
956956
});
957957
if !conflicts_ref.is_empty() {
958-
let occurs_because = format!(
959-
"move occurs because `{}` has type `{}` which does not implement the `Copy` trait",
958+
sess.emit_err(BorrowOfMovedValue {
959+
span: pat.span,
960+
binding_span,
961+
conflicts_ref,
960962
name,
961-
typeck_results.node_type(pat.hir_id),
962-
);
963-
sess.struct_span_err(pat.span, "borrow of moved value")
964-
.span_label(binding_span, format!("value moved into `{}` here", name))
965-
.span_label(binding_span, occurs_because)
966-
.span_labels(conflicts_ref, "value borrowed here after move")
967-
.emit();
963+
ty: typeck_results.node_type(pat.hir_id),
964+
});
968965
}
969966
return;
970967
}

0 commit comments

Comments
 (0)