Skip to content

Commit 4d5e54e

Browse files
committed
Move "elided lifetime in path" to subdiagnostic struct
This requires nested subdiagnostics.
1 parent 8ae35dd commit 4d5e54e

File tree

5 files changed

+41
-34
lines changed

5 files changed

+41
-34
lines changed

compiler/rustc_errors/src/diagnostic_impls.rs

+8
Original file line numberDiff line numberDiff line change
@@ -348,3 +348,11 @@ pub struct IndicateAnonymousLifetime {
348348
pub count: usize,
349349
pub suggestion: String,
350350
}
351+
352+
#[derive(Subdiagnostic)]
353+
pub struct ElidedLifetimeInPathSubdiag {
354+
#[subdiagnostic]
355+
pub expected: ExpectedLifetimeParameter,
356+
#[subdiagnostic]
357+
pub indicate: Option<IndicateAnonymousLifetime>,
358+
}

compiler/rustc_errors/src/lib.rs

+15-18
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ pub use diagnostic::{
4141
SubdiagMessageOp, Subdiagnostic,
4242
};
4343
pub use diagnostic_impls::{
44-
DiagArgFromDisplay, DiagSymbolList, ExpectedLifetimeParameter, IndicateAnonymousLifetime,
45-
SingleLabelManySpans,
44+
DiagArgFromDisplay, DiagSymbolList, ElidedLifetimeInPathSubdiag, ExpectedLifetimeParameter,
45+
IndicateAnonymousLifetime, SingleLabelManySpans,
4646
};
4747
pub use emitter::ColorConfig;
4848
pub use rustc_error_messages::{
@@ -1912,27 +1912,24 @@ impl Level {
19121912
}
19131913

19141914
// FIXME(eddyb) this doesn't belong here AFAICT, should be moved to callsite.
1915-
pub fn add_elided_lifetime_in_path_suggestion<G: EmissionGuarantee>(
1915+
pub fn elided_lifetime_in_path_suggestion(
19161916
source_map: &SourceMap,
1917-
diag: &mut Diag<'_, G>,
19181917
n: usize,
19191918
path_span: Span,
19201919
incl_angl_brckt: bool,
19211920
insertion_span: Span,
1922-
) {
1923-
diag.subdiagnostic(diag.dcx, ExpectedLifetimeParameter { span: path_span, count: n });
1924-
if !source_map.is_span_accessible(insertion_span) {
1925-
// Do not try to suggest anything if generated by a proc-macro.
1926-
return;
1927-
}
1928-
let anon_lts = vec!["'_"; n].join(", ");
1929-
let suggestion =
1930-
if incl_angl_brckt { format!("<{anon_lts}>") } else { format!("{anon_lts}, ") };
1931-
1932-
diag.subdiagnostic(
1933-
diag.dcx,
1934-
IndicateAnonymousLifetime { span: insertion_span.shrink_to_hi(), count: n, suggestion },
1935-
);
1921+
) -> ElidedLifetimeInPathSubdiag {
1922+
let expected = ExpectedLifetimeParameter { span: path_span, count: n };
1923+
// Do not try to suggest anything if generated by a proc-macro.
1924+
let indicate = source_map.is_span_accessible(insertion_span).then(|| {
1925+
let anon_lts = vec!["'_"; n].join(", ");
1926+
let suggestion =
1927+
if incl_angl_brckt { format!("<{anon_lts}>") } else { format!("{anon_lts}, ") };
1928+
1929+
IndicateAnonymousLifetime { span: insertion_span.shrink_to_hi(), count: n, suggestion }
1930+
});
1931+
1932+
ElidedLifetimeInPathSubdiag { expected, indicate }
19361933
}
19371934

19381935
pub fn report_ambiguity_error<'a, G: EmissionGuarantee>(

compiler/rustc_lint/src/context/diagnostics.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#![allow(rustc::untranslatable_diagnostic)]
33

44
use rustc_ast::util::unicode::TEXT_FLOW_CONTROL_CHARS;
5-
use rustc_errors::{add_elided_lifetime_in_path_suggestion, Diag};
5+
use rustc_errors::{elided_lifetime_in_path_suggestion, Diag};
66
use rustc_errors::{Applicability, SuggestionStyle};
77
use rustc_middle::middle::stability;
88
use rustc_session::lint::BuiltinLintDiag;
@@ -74,13 +74,15 @@ pub(super) fn builtin(sess: &Session, diagnostic: BuiltinLintDiag, diag: &mut Di
7474
diag.span_note(span_def, "the macro is defined here");
7575
}
7676
BuiltinLintDiag::ElidedLifetimesInPaths(n, path_span, incl_angl_brckt, insertion_span) => {
77-
add_elided_lifetime_in_path_suggestion(
78-
sess.source_map(),
79-
diag,
80-
n,
81-
path_span,
82-
incl_angl_brckt,
83-
insertion_span,
77+
diag.subdiagnostic(
78+
sess.dcx(),
79+
elided_lifetime_in_path_suggestion(
80+
sess.source_map(),
81+
n,
82+
path_span,
83+
incl_angl_brckt,
84+
insertion_span,
85+
),
8486
);
8587
}
8688
BuiltinLintDiag::UnknownCrateTypes(span, note, sugg) => {

compiler/rustc_resolve/src/errors.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_errors::{codes::*, Applicability, MultiSpan};
1+
use rustc_errors::{codes::*, Applicability, ElidedLifetimeInPathSubdiag, MultiSpan};
22
use rustc_macros::{Diagnostic, Subdiagnostic};
33
use rustc_span::{
44
symbol::{Ident, Symbol},
@@ -907,6 +907,8 @@ pub(crate) struct ExplicitAnonymousLivetimeReportError {
907907
pub(crate) struct ImplicitElidedLifetimeNotAllowedHere {
908908
#[primary_span]
909909
pub(crate) span: Span,
910+
#[subdiagnostic]
911+
pub(crate) subdiag: ElidedLifetimeInPathSubdiag,
910912
}
911913

912914
#[derive(Diagnostic)]

compiler/rustc_resolve/src/late.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -1883,20 +1883,18 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
18831883
// async fn foo(_: std::cell::Ref<u32>) { ... }
18841884
LifetimeRibKind::AnonymousCreateParameter { report_in_path: true, .. }
18851885
| LifetimeRibKind::AnonymousWarn(_) => {
1886-
let mut err =
1887-
self.r.dcx().create_err(errors::ImplicitElidedLifetimeNotAllowedHere {
1888-
span: path_span,
1889-
});
18901886
let sess = self.r.tcx.sess;
1891-
rustc_errors::add_elided_lifetime_in_path_suggestion(
1887+
let subdiag = rustc_errors::elided_lifetime_in_path_suggestion(
18921888
sess.source_map(),
1893-
&mut err,
18941889
expected_lifetimes,
18951890
path_span,
18961891
!segment.has_generic_args,
18971892
elided_lifetime_span,
18981893
);
1899-
err.emit();
1894+
self.r.dcx().emit_err(errors::ImplicitElidedLifetimeNotAllowedHere {
1895+
span: path_span,
1896+
subdiag,
1897+
});
19001898
should_lint = false;
19011899

19021900
for id in node_ids {

0 commit comments

Comments
 (0)