Skip to content

Commit 14b2fee

Browse files
committed
E0502 && should we trim end of a ftl msg as well?
1 parent e9e190a commit 14b2fee

File tree

3 files changed

+90
-31
lines changed

3 files changed

+90
-31
lines changed

compiler/rustc_borrowck/src/borrowck_errors.rs

+22-31
Original file line numberDiff line numberDiff line change
@@ -161,41 +161,32 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
161161
msg_old: &str,
162162
old_load_end_span: Option<Span>,
163163
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
164-
let via =
165-
|msg: &str| if msg.is_empty() { "".to_string() } else { format!(" (via {})", msg) };
166-
let mut err = struct_span_err!(
167-
self,
168-
span,
169-
E0502,
170-
"cannot borrow {}{} as {} because {} is also borrowed as {}{}",
164+
use crate::session_diagnostics::BorrowOccurLabel::*;
165+
let via = |msg: &str| msg.is_empty();
166+
let (new_occur, old_occur) = if msg_new == "" {
167+
// If `msg_new` is empty, then this isn't a borrow of a union field.
168+
(Here { span, kind: kind_new }, Here { span: old_span, kind: kind_old })
169+
} else {
170+
// If `msg_new` isn't empty, then this a borrow of a union field.
171+
(
172+
HereOverlap { span, kind_new, msg_new, msg_old },
173+
HereVia { span: old_span, kind_old, is_msg_old_empty: via(msg_old), msg_old },
174+
)
175+
};
176+
self.infcx.tcx.sess.create_err(crate::session_diagnostics::ReborrowBorrowedErr {
171177
desc_new,
172-
via(msg_new),
178+
is_msg_new_empty: via(msg_new),
179+
msg_new,
173180
kind_new,
174181
noun_old,
175182
kind_old,
176-
via(msg_old),
177-
);
178-
179-
if msg_new == "" {
180-
// If `msg_new` is empty, then this isn't a borrow of a union field.
181-
err.span_label(span, format!("{} borrow occurs here", kind_new));
182-
err.span_label(old_span, format!("{} borrow occurs here", kind_old));
183-
} else {
184-
// If `msg_new` isn't empty, then this a borrow of a union field.
185-
err.span_label(
186-
span,
187-
format!(
188-
"{} borrow of {} -- which overlaps with {} -- occurs here",
189-
kind_new, msg_new, msg_old,
190-
),
191-
);
192-
err.span_label(old_span, format!("{} borrow occurs here{}", kind_old, via(msg_old)));
193-
}
194-
195-
if let Some(old_load_end_span) = old_load_end_span {
196-
err.span_label(old_load_end_span, format!("{} borrow ends here", kind_old));
197-
}
198-
err
183+
is_msg_old_empty: via(msg_old),
184+
msg_old,
185+
span,
186+
old_load_end_span,
187+
new_occur,
188+
old_occur,
189+
})
199190
}
200191

201192
pub(crate) fn cannot_assign_to_borrowed(

compiler/rustc_borrowck/src/session_diagnostics.rs

+47
Original file line numberDiff line numberDiff line change
@@ -784,3 +784,50 @@ pub(crate) struct ClosureReBorrowErr<'a> {
784784
pub previous_end_span: Option<Span>,
785785
pub second_borrow_desc: &'a str,
786786
}
787+
788+
#[derive(Subdiagnostic)]
789+
pub(crate) enum BorrowOccurLabel<'a> {
790+
#[label(borrowck_borrow_occurs_here)]
791+
Here {
792+
#[primary_span]
793+
span: Span,
794+
kind: &'a str,
795+
},
796+
#[label(borrowck_borrow_occurs_here_overlap)]
797+
HereOverlap {
798+
#[primary_span]
799+
span: Span,
800+
kind_new: &'a str,
801+
msg_new: &'a str,
802+
msg_old: &'a str,
803+
},
804+
#[label(borrowck_borrow_occurs_here_via)]
805+
HereVia {
806+
#[primary_span]
807+
span: Span,
808+
kind_old: &'a str,
809+
is_msg_old_empty: bool,
810+
msg_old: &'a str,
811+
},
812+
}
813+
814+
#[derive(Diagnostic)]
815+
#[diag(borrowck_cannot_reborrow_already_borrowed, code = "E0502")]
816+
pub(crate) struct ReborrowBorrowedErr<'a> {
817+
pub desc_new: &'a str,
818+
pub is_msg_new_empty: bool,
819+
pub msg_new: &'a str,
820+
pub kind_new: &'a str,
821+
pub noun_old: &'a str,
822+
pub kind_old: &'a str,
823+
pub is_msg_old_empty: bool,
824+
pub msg_old: &'a str,
825+
#[primary_span]
826+
pub span: Span,
827+
#[label]
828+
pub old_load_end_span: Option<Span>,
829+
#[subdiagnostic(eager)]
830+
pub new_occur: BorrowOccurLabel<'a>,
831+
#[subdiagnostic(eager)]
832+
pub old_occur: BorrowOccurLabel<'a>,
833+
}

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

+21
Original file line numberDiff line numberDiff line change
@@ -474,3 +474,24 @@ borrowck_cannot_reborrow_already_uniquely_borrowed =
474474
.label = {$second_borrow_desc}borrow occurs here{$opt_via}
475475
.occurs_label = {$container_name} construction occurs here{$old_opt_via}
476476
.ends_label = borrow from closure ends here
477+
478+
borrowck_borrow_occurs_here = {$kind} borrow occurs here
479+
480+
borrowck_borrow_occurs_here_overlap =
481+
{$kind_new} borrow of {$msg_new} -- which overlaps with {$msg_old} -- occurs here
482+
483+
borrowck_borrow_occurs_here_via =
484+
{$kind_old} borrow occurs here {$is_msg_old_empty ->
485+
*[true] {""}
486+
[false] (via {$msg_old})
487+
}
488+
489+
borrowck_cannot_reborrow_already_borrowed =
490+
cannot borrow {$desc_new}{$is_msg_new_empty ->
491+
*[true] {""}
492+
[false] {""} (via {$msg_new})
493+
} as {$kind_new} because {$noun_old} is also borrowed as {$kind_old}{$is_msg_old_empty ->
494+
*[true] {""}
495+
[false] {""} (via {$msg_old})
496+
}
497+
.label = {$kind_old} borrow ends here

0 commit comments

Comments
 (0)