Skip to content

Commit 57a3d89

Browse files
committed
Don't name macro internals in "does not live long enough" errors.
1 parent 62a8263 commit 57a3d89

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -2972,21 +2972,27 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
29722972
}
29732973
}
29742974

2975-
let mut err = self.path_does_not_live_long_enough(borrow_span, &format!("`{name}`"));
2975+
let name = if borrow_span.in_external_macro(self.infcx.tcx.sess.source_map()) {
2976+
// Don't name local variables in external macros.
2977+
"value".to_string()
2978+
} else {
2979+
format!("`{name}`")
2980+
};
2981+
2982+
let mut err = self.path_does_not_live_long_enough(borrow_span, &name);
29762983

29772984
if let Some(annotation) = self.annotate_argument_and_return_for_borrow(borrow) {
29782985
let region_name = annotation.emit(self, &mut err);
29792986

29802987
err.span_label(
29812988
borrow_span,
2982-
format!("`{name}` would have to be valid for `{region_name}`..."),
2989+
format!("{name} would have to be valid for `{region_name}`..."),
29832990
);
29842991

29852992
err.span_label(
29862993
drop_span,
29872994
format!(
2988-
"...but `{}` will be dropped here, when the {} returns",
2989-
name,
2995+
"...but {name} will be dropped here, when the {} returns",
29902996
self.infcx
29912997
.tcx
29922998
.opt_item_name(self.mir_def_id().to_def_id())
@@ -3024,7 +3030,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
30243030
}
30253031
} else {
30263032
err.span_label(borrow_span, "borrowed value does not live long enough");
3027-
err.span_label(drop_span, format!("`{name}` dropped here while still borrowed"));
3033+
err.span_label(drop_span, format!("{name} dropped here while still borrowed"));
30283034

30293035
borrow_spans.args_subdiag(&mut err, |args_span| {
30303036
crate::session_diagnostics::CaptureArgLabel::Capture {

compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ impl<'tcx> BorrowExplanation<'tcx> {
9595
&& let hir::def::Res::Local(hir_id) = p.res
9696
&& let hir::Node::Pat(pat) = tcx.hir_node(hir_id)
9797
{
98-
err.span_label(pat.span, format!("binding `{ident}` declared here"));
98+
if !ident.span.in_external_macro(tcx.sess.source_map()) {
99+
err.span_label(pat.span, format!("binding `{ident}` declared here"));
100+
}
99101
}
100102
}
101103
}

0 commit comments

Comments
 (0)