Don't name macro internals in "does not live long enough" errors.

This commit is contained in:
Mara Bos 2025-03-28 09:41:31 +01:00
parent 1dd77cd24a
commit d20b270b4e
2 changed files with 14 additions and 6 deletions

View file

@ -2959,21 +2959,27 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
}
}
let mut err = self.path_does_not_live_long_enough(borrow_span, &format!("`{name}`"));
let name = if borrow_span.in_external_macro(self.infcx.tcx.sess.source_map()) {
// Don't name local variables in external macros.
"value".to_string()
} else {
format!("`{name}`")
};
let mut err = self.path_does_not_live_long_enough(borrow_span, &name);
if let Some(annotation) = self.annotate_argument_and_return_for_borrow(borrow) {
let region_name = annotation.emit(self, &mut err);
err.span_label(
borrow_span,
format!("`{name}` would have to be valid for `{region_name}`..."),
format!("{name} would have to be valid for `{region_name}`..."),
);
err.span_label(
drop_span,
format!(
"...but `{}` will be dropped here, when the {} returns",
name,
"...but {name} will be dropped here, when the {} returns",
self.infcx
.tcx
.opt_item_name(self.mir_def_id().to_def_id())
@ -3011,7 +3017,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
}
} else {
err.span_label(borrow_span, "borrowed value does not live long enough");
err.span_label(drop_span, format!("`{name}` dropped here while still borrowed"));
err.span_label(drop_span, format!("{name} dropped here while still borrowed"));
borrow_spans.args_subdiag(&mut err, |args_span| {
crate::session_diagnostics::CaptureArgLabel::Capture {

View file

@ -95,7 +95,9 @@ impl<'tcx> BorrowExplanation<'tcx> {
&& let hir::def::Res::Local(hir_id) = p.res
&& let hir::Node::Pat(pat) = tcx.hir_node(hir_id)
{
err.span_label(pat.span, format!("binding `{ident}` declared here"));
if !ident.span.in_external_macro(tcx.sess.source_map()) {
err.span_label(pat.span, format!("binding `{ident}` declared here"));
}
}
}
}