Skip to content

Commit 6e423e1

Browse files
authored
Rollup merge of rust-lang#124218 - Xiretza:subsubdiagnostics, r=davidtwco
Allow nesting subdiagnostics in #[derive(Subdiagnostic)]
2 parents e15d6f9 + 6974e9c commit 6e423e1

File tree

23 files changed

+146
-115
lines changed

23 files changed

+146
-115
lines changed

compiler/rustc_ast_lowering/src/errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl Subdiagnostic for InvalidAbiReason {
4444
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
4545
self,
4646
diag: &mut Diag<'_, G>,
47-
_: F,
47+
_: &F,
4848
) {
4949
#[allow(rustc::untranslatable_diagnostic)]
5050
diag.note(self.0);

compiler/rustc_ast_passes/src/errors.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ impl Subdiagnostic for EmptyLabelManySpans {
382382
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
383383
self,
384384
diag: &mut Diag<'_, G>,
385-
_: F,
385+
_: &F,
386386
) {
387387
diag.span_labels(self.0, "");
388388
}
@@ -751,7 +751,7 @@ impl Subdiagnostic for StableFeature {
751751
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
752752
self,
753753
diag: &mut Diag<'_, G>,
754-
_: F,
754+
_: &F,
755755
) {
756756
diag.arg("name", self.name);
757757
diag.arg("since", self.since);

compiler/rustc_builtin_macros/src/errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ impl Subdiagnostic for FormatUnusedArg {
601601
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
602602
self,
603603
diag: &mut Diag<'_, G>,
604-
f: F,
604+
f: &F,
605605
) {
606606
diag.arg("named", self.named);
607607
let msg = f(diag, crate::fluent_generated::builtin_macros_format_unused_arg.into());

compiler/rustc_errors/src/diagnostic.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -177,15 +177,15 @@ where
177177
{
178178
/// Add a subdiagnostic to an existing diagnostic.
179179
fn add_to_diag<G: EmissionGuarantee>(self, diag: &mut Diag<'_, G>) {
180-
self.add_to_diag_with(diag, |_, m| m);
180+
self.add_to_diag_with(diag, &|_, m| m);
181181
}
182182

183183
/// Add a subdiagnostic to an existing diagnostic where `f` is invoked on every message used
184184
/// (to optionally perform eager translation).
185185
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
186186
self,
187187
diag: &mut Diag<'_, G>,
188-
f: F,
188+
f: &F,
189189
);
190190
}
191191

@@ -1197,7 +1197,7 @@ impl<'a, G: EmissionGuarantee> Diag<'a, G> {
11971197
dcx: &crate::DiagCtxt,
11981198
subdiagnostic: impl Subdiagnostic,
11991199
) -> &mut Self {
1200-
subdiagnostic.add_to_diag_with(self, |diag, msg| {
1200+
subdiagnostic.add_to_diag_with(self, &|diag, msg| {
12011201
let args = diag.args.iter();
12021202
let msg = diag.subdiagnostic_message_to_diagnostic_message(msg);
12031203
dcx.eagerly_translate(msg, args)

compiler/rustc_errors/src/diagnostic_impls.rs

+37-29
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,36 @@ impl IntoDiagArg for rustc_lint_defs::Level {
227227
}
228228
}
229229

230+
impl<Id> IntoDiagArg for hir::def::Res<Id> {
231+
fn into_diag_arg(self) -> DiagArgValue {
232+
DiagArgValue::Str(Cow::Borrowed(self.descr()))
233+
}
234+
}
235+
236+
impl IntoDiagArg for DiagLocation {
237+
fn into_diag_arg(self) -> DiagArgValue {
238+
DiagArgValue::Str(Cow::from(self.to_string()))
239+
}
240+
}
241+
242+
impl IntoDiagArg for Backtrace {
243+
fn into_diag_arg(self) -> DiagArgValue {
244+
DiagArgValue::Str(Cow::from(self.to_string()))
245+
}
246+
}
247+
248+
impl IntoDiagArg for Level {
249+
fn into_diag_arg(self) -> DiagArgValue {
250+
DiagArgValue::Str(Cow::from(self.to_string()))
251+
}
252+
}
253+
254+
impl IntoDiagArg for type_ir::ClosureKind {
255+
fn into_diag_arg(self) -> DiagArgValue {
256+
DiagArgValue::Str(self.as_str().into())
257+
}
258+
}
259+
230260
#[derive(Clone)]
231261
pub struct DiagSymbolList(Vec<Symbol>);
232262

@@ -244,12 +274,6 @@ impl IntoDiagArg for DiagSymbolList {
244274
}
245275
}
246276

247-
impl<Id> IntoDiagArg for hir::def::Res<Id> {
248-
fn into_diag_arg(self) -> DiagArgValue {
249-
DiagArgValue::Str(Cow::Borrowed(self.descr()))
250-
}
251-
}
252-
253277
impl<G: EmissionGuarantee> Diagnostic<'_, G> for TargetDataLayoutErrors<'_> {
254278
fn into_diag(self, dcx: &DiagCtxt, level: Level) -> Diag<'_, G> {
255279
match self {
@@ -302,7 +326,7 @@ impl Subdiagnostic for SingleLabelManySpans {
302326
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
303327
self,
304328
diag: &mut Diag<'_, G>,
305-
_: F,
329+
_: &F,
306330
) {
307331
diag.span_labels(self.spans, self.label);
308332
}
@@ -316,24 +340,6 @@ pub struct ExpectedLifetimeParameter {
316340
pub count: usize,
317341
}
318342

319-
impl IntoDiagArg for DiagLocation {
320-
fn into_diag_arg(self) -> DiagArgValue {
321-
DiagArgValue::Str(Cow::from(self.to_string()))
322-
}
323-
}
324-
325-
impl IntoDiagArg for Backtrace {
326-
fn into_diag_arg(self) -> DiagArgValue {
327-
DiagArgValue::Str(Cow::from(self.to_string()))
328-
}
329-
}
330-
331-
impl IntoDiagArg for Level {
332-
fn into_diag_arg(self) -> DiagArgValue {
333-
DiagArgValue::Str(Cow::from(self.to_string()))
334-
}
335-
}
336-
337343
#[derive(Subdiagnostic)]
338344
#[suggestion(errors_indicate_anonymous_lifetime, code = "{suggestion}", style = "verbose")]
339345
pub struct IndicateAnonymousLifetime {
@@ -343,8 +349,10 @@ pub struct IndicateAnonymousLifetime {
343349
pub suggestion: String,
344350
}
345351

346-
impl IntoDiagArg for type_ir::ClosureKind {
347-
fn into_diag_arg(self) -> DiagArgValue {
348-
DiagArgValue::Str(self.as_str().into())
349-
}
352+
#[derive(Subdiagnostic)]
353+
pub struct ElidedLifetimeInPathSubdiag {
354+
#[subdiagnostic]
355+
pub expected: ExpectedLifetimeParameter,
356+
#[subdiagnostic]
357+
pub indicate: Option<IndicateAnonymousLifetime>,
350358
}

compiler/rustc_errors/src/lib.rs

+15-18
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ pub use diagnostic::{
4040
SubdiagMessageOp, Subdiagnostic,
4141
};
4242
pub use diagnostic_impls::{
43-
DiagArgFromDisplay, DiagSymbolList, ExpectedLifetimeParameter, IndicateAnonymousLifetime,
44-
SingleLabelManySpans,
43+
DiagArgFromDisplay, DiagSymbolList, ElidedLifetimeInPathSubdiag, ExpectedLifetimeParameter,
44+
IndicateAnonymousLifetime, SingleLabelManySpans,
4545
};
4646
pub use emitter::ColorConfig;
4747
pub use rustc_error_messages::{
@@ -1911,27 +1911,24 @@ impl Level {
19111911
}
19121912

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

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

compiler/rustc_hir_typeck/src/errors.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ impl Subdiagnostic for TypeMismatchFruTypo {
191191
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
192192
self,
193193
diag: &mut Diag<'_, G>,
194-
_f: F,
194+
_f: &F,
195195
) {
196196
diag.arg("expr", self.expr.as_deref().unwrap_or("NONE"));
197197

@@ -370,7 +370,7 @@ impl Subdiagnostic for RemoveSemiForCoerce {
370370
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
371371
self,
372372
diag: &mut Diag<'_, G>,
373-
_f: F,
373+
_f: &F,
374374
) {
375375
let mut multispan: MultiSpan = self.semi.into();
376376
multispan.push_span_label(self.expr, fluent::hir_typeck_remove_semi_for_coerce_expr);
@@ -546,7 +546,7 @@ impl rustc_errors::Subdiagnostic for CastUnknownPointerSub {
546546
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
547547
self,
548548
diag: &mut Diag<'_, G>,
549-
f: F,
549+
f: &F,
550550
) {
551551
match self {
552552
CastUnknownPointerSub::To(span) => {

compiler/rustc_infer/src/errors/mod.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ impl Subdiagnostic for RegionOriginNote<'_> {
239239
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
240240
self,
241241
diag: &mut Diag<'_, G>,
242-
_f: F,
242+
_f: &F,
243243
) {
244244
let mut label_or_note = |span, msg: DiagMessage| {
245245
let sub_count = diag.children.iter().filter(|d| d.span.is_dummy()).count();
@@ -304,7 +304,7 @@ impl Subdiagnostic for LifetimeMismatchLabels {
304304
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
305305
self,
306306
diag: &mut Diag<'_, G>,
307-
_f: F,
307+
_f: &F,
308308
) {
309309
match self {
310310
LifetimeMismatchLabels::InRet { param_span, ret_span, span, label_var1 } => {
@@ -352,7 +352,7 @@ impl Subdiagnostic for AddLifetimeParamsSuggestion<'_> {
352352
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
353353
self,
354354
diag: &mut Diag<'_, G>,
355-
_f: F,
355+
_f: &F,
356356
) {
357357
let mut mk_suggestion = || {
358358
let (
@@ -454,7 +454,7 @@ impl Subdiagnostic for IntroducesStaticBecauseUnmetLifetimeReq {
454454
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
455455
mut self,
456456
diag: &mut Diag<'_, G>,
457-
_f: F,
457+
_f: &F,
458458
) {
459459
self.unmet_requirements
460460
.push_span_label(self.binding_span, fluent::infer_msl_introduces_static);
@@ -773,7 +773,7 @@ impl Subdiagnostic for ConsiderBorrowingParamHelp {
773773
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
774774
self,
775775
diag: &mut Diag<'_, G>,
776-
f: F,
776+
f: &F,
777777
) {
778778
let mut type_param_span: MultiSpan = self.spans.clone().into();
779779
for &span in &self.spans {
@@ -818,7 +818,7 @@ impl Subdiagnostic for DynTraitConstraintSuggestion {
818818
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
819819
self,
820820
diag: &mut Diag<'_, G>,
821-
f: F,
821+
f: &F,
822822
) {
823823
let mut multi_span: MultiSpan = vec![self.span].into();
824824
multi_span.push_span_label(self.span, fluent::infer_dtcs_has_lifetime_req_label);
@@ -865,7 +865,7 @@ impl Subdiagnostic for ReqIntroducedLocations {
865865
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
866866
mut self,
867867
diag: &mut Diag<'_, G>,
868-
f: F,
868+
f: &F,
869869
) {
870870
for sp in self.spans {
871871
self.span.push_span_label(sp, fluent::infer_ril_introduced_here);
@@ -888,7 +888,7 @@ impl Subdiagnostic for MoreTargeted {
888888
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
889889
self,
890890
diag: &mut Diag<'_, G>,
891-
_f: F,
891+
_f: &F,
892892
) {
893893
diag.code(E0772);
894894
diag.primary_message(fluent::infer_more_targeted);
@@ -1293,7 +1293,7 @@ impl Subdiagnostic for SuggestTuplePatternMany {
12931293
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
12941294
self,
12951295
diag: &mut Diag<'_, G>,
1296-
f: F,
1296+
f: &F,
12971297
) {
12981298
diag.arg("path", self.path);
12991299
let message = f(diag, crate::fluent_generated::infer_stp_wrap_many.into());

compiler/rustc_infer/src/errors/note_and_explain.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ impl Subdiagnostic for RegionExplanation<'_> {
163163
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
164164
self,
165165
diag: &mut Diag<'_, G>,
166-
f: F,
166+
f: &F,
167167
) {
168168
diag.arg("pref_kind", self.prefix);
169169
diag.arg("suff_kind", self.suffix);

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_lint/src/errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ impl Subdiagnostic for OverruledAttributeSub {
2727
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
2828
self,
2929
diag: &mut Diag<'_, G>,
30-
_f: F,
30+
_f: &F,
3131
) {
3232
match self {
3333
OverruledAttributeSub::DefaultSource { id } => {

0 commit comments

Comments
 (0)