Skip to content

Commit d20b270

Browse files
committed
Don't name macro internals in "does not live long enough" errors.
1 parent 1dd77cd commit d20b270

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

Diff for: compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -2959,21 +2959,27 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
29592959
}
29602960
}
29612961

2962-
let mut err = self.path_does_not_live_long_enough(borrow_span, &format!("`{name}`"));
2962+
let name = if borrow_span.in_external_macro(self.infcx.tcx.sess.source_map()) {
2963+
// Don't name local variables in external macros.
2964+
"value".to_string()
2965+
} else {
2966+
format!("`{name}`")
2967+
};
2968+
2969+
let mut err = self.path_does_not_live_long_enough(borrow_span, &name);
29632970

29642971
if let Some(annotation) = self.annotate_argument_and_return_for_borrow(borrow) {
29652972
let region_name = annotation.emit(self, &mut err);
29662973

29672974
err.span_label(
29682975
borrow_span,
2969-
format!("`{name}` would have to be valid for `{region_name}`..."),
2976+
format!("{name} would have to be valid for `{region_name}`..."),
29702977
);
29712978

29722979
err.span_label(
29732980
drop_span,
29742981
format!(
2975-
"...but `{}` will be dropped here, when the {} returns",
2976-
name,
2982+
"...but {name} will be dropped here, when the {} returns",
29772983
self.infcx
29782984
.tcx
29792985
.opt_item_name(self.mir_def_id().to_def_id())
@@ -3011,7 +3017,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
30113017
}
30123018
} else {
30133019
err.span_label(borrow_span, "borrowed value does not live long enough");
3014-
err.span_label(drop_span, format!("`{name}` dropped here while still borrowed"));
3020+
err.span_label(drop_span, format!("{name} dropped here while still borrowed"));
30153021

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

Diff for: 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)