diff --git a/compiler/rustc_borrowck/messages.ftl b/compiler/rustc_borrowck/messages.ftl index 67fdb671742da..a0ce31d8b517b 100644 --- a/compiler/rustc_borrowck/messages.ftl +++ b/compiler/rustc_borrowck/messages.ftl @@ -33,6 +33,8 @@ borrowck_cannot_move_when_borrowed = *[other] {$value_place} } occurs here +borrowck_cannot_use_when_mutably_borrowed = cannot use `{$desc}` when mutably borrowed + borrowck_capture_immute = capture is immutable because of use here diff --git a/compiler/rustc_borrowck/src/borrowck_errors.rs b/compiler/rustc_borrowck/src/borrowck_errors.rs index acca1a1477f25..fa4100e3df729 100644 --- a/compiler/rustc_borrowck/src/borrowck_errors.rs +++ b/compiler/rustc_borrowck/src/borrowck_errors.rs @@ -1,3 +1,4 @@ +use crate::session_diagnostics::CannotUseWhenMutablyBorrowed; use rustc_errors::{ struct_span_err, DiagnosticBuilder, DiagnosticId, DiagnosticMessage, ErrorGuaranteed, MultiSpan, }; @@ -29,17 +30,12 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> { borrow_span: Span, borrow_desc: &str, ) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> { - let mut err = struct_span_err!( - self, + self.infcx.tcx.sess.create_err(CannotUseWhenMutablyBorrowed { span, - E0503, - "cannot use {} because it was mutably borrowed", + borrow_span, + borrow_desc, desc, - ); - - err.span_label(borrow_span, format!("{} is borrowed here", borrow_desc)); - err.span_label(span, format!("use of borrowed {}", borrow_desc)); - err + }) } pub(crate) fn cannot_mutably_borrow_multiply( diff --git a/compiler/rustc_borrowck/src/session_diagnostics.rs b/compiler/rustc_borrowck/src/session_diagnostics.rs index fceae5bb3ffe0..6a155fc64f26b 100644 --- a/compiler/rustc_borrowck/src/session_diagnostics.rs +++ b/compiler/rustc_borrowck/src/session_diagnostics.rs @@ -5,6 +5,17 @@ use rustc_span::Span; use crate::diagnostics::RegionName; +#[derive(Diagnostic)] +#[diag(borrowck_cannot_use_when_mutably_borrowed, code = "E0503")] +pub(crate) struct CannotUseWhenMutablyBorrowed<'a> { + #[primary_span] + pub span: Span, + #[label] + pub borrow_span: Span, + pub borrow_desc: &'a str, + pub desc: &'a str, +} + #[derive(Diagnostic)] #[diag(borrowck_move_unsized, code = "E0161")] pub(crate) struct MoveUnsized<'tcx> {