Skip to content

Commit 1366d82

Browse files
authored
Rollup merge of rust-lang#105806 - mejrs:eager2, r=davidtwco
Support eager subdiagnostics again See rust-lang#104941 (comment) I'm not sure how to add a test for this. But I did pick some of the diagnostic structs in the mentioned PR and it works with them.
2 parents bfc44c6 + 7aff210 commit 1366d82

File tree

4 files changed

+29
-30
lines changed

4 files changed

+29
-30
lines changed

compiler/rustc_macros/src/diagnostics/diagnostic_builder.rs

+20-4
Original file line numberDiff line numberDiff line change
@@ -382,10 +382,26 @@ impl<'a> DiagnosticDeriveVariantBuilder<'a> {
382382
return Ok(quote! { #diag.subdiagnostic(#binding); });
383383
}
384384
}
385-
(Meta::List(_), "subdiagnostic") => {
386-
throw_invalid_attr!(attr, &meta, |diag| {
387-
diag.help("`subdiagnostic` does not support nested attributes")
388-
})
385+
(Meta::List(MetaList { ref nested, .. }), "subdiagnostic") => {
386+
if nested.len() == 1
387+
&& let Some(NestedMeta::Meta(Meta::Path(path))) = nested.first()
388+
&& path.is_ident("eager") {
389+
let handler = match &self.parent.kind {
390+
DiagnosticDeriveKind::Diagnostic { handler } => handler,
391+
DiagnosticDeriveKind::LintDiagnostic => {
392+
throw_invalid_attr!(attr, &meta, |diag| {
393+
diag.help("eager subdiagnostics are not supported on lints")
394+
})
395+
}
396+
};
397+
return Ok(quote! { #diag.eager_subdiagnostic(#handler, #binding); });
398+
} else {
399+
throw_invalid_attr!(attr, &meta, |diag| {
400+
diag.help(
401+
"`eager` is the only supported nested attribute for `subdiagnostic`",
402+
)
403+
})
404+
}
389405
}
390406
_ => (),
391407
}

compiler/rustc_macros/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![feature(allow_internal_unstable)]
22
#![feature(if_let_guard)]
3+
#![feature(let_chains)]
34
#![feature(never_type)]
45
#![feature(proc_macro_diagnostic)]
56
#![feature(proc_macro_span)]

tests/ui-fulldeps/session-diagnostic/diagnostic-derive.rs

-2
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,6 @@ struct SubdiagnosticEagerLint {
723723
#[diag(compiletest_example)]
724724
struct SubdiagnosticEagerCorrect {
725725
#[subdiagnostic(eager)]
726-
//~^ ERROR `#[subdiagnostic(...)]` is not a valid attribute
727726
note: Note,
728727
}
729728

@@ -744,7 +743,6 @@ pub(crate) struct SubdiagnosticWithSuggestion {
744743
#[diag(compiletest_example)]
745744
struct SubdiagnosticEagerSuggestion {
746745
#[subdiagnostic(eager)]
747-
//~^ ERROR `#[subdiagnostic(...)]` is not a valid attribute
748746
sub: SubdiagnosticWithSuggestion,
749747
}
750748

tests/ui-fulldeps/session-diagnostic/diagnostic-derive.stderr

+8-24
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ error: `#[subdiagnostic(...)]` is not a valid attribute
539539
LL | #[subdiagnostic(bad)]
540540
| ^^^^^^^^^^^^^^^^^^^^^
541541
|
542-
= help: `subdiagnostic` does not support nested attributes
542+
= help: `eager` is the only supported nested attribute for `subdiagnostic`
543543

544544
error: `#[subdiagnostic = ...]` is not a valid attribute
545545
--> $DIR/diagnostic-derive.rs:693:5
@@ -553,54 +553,38 @@ error: `#[subdiagnostic(...)]` is not a valid attribute
553553
LL | #[subdiagnostic(bad, bad)]
554554
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
555555
|
556-
= help: `subdiagnostic` does not support nested attributes
556+
= help: `eager` is the only supported nested attribute for `subdiagnostic`
557557

558558
error: `#[subdiagnostic(...)]` is not a valid attribute
559559
--> $DIR/diagnostic-derive.rs:709:5
560560
|
561561
LL | #[subdiagnostic("bad")]
562562
| ^^^^^^^^^^^^^^^^^^^^^^^
563563
|
564-
= help: `subdiagnostic` does not support nested attributes
564+
= help: `eager` is the only supported nested attribute for `subdiagnostic`
565565

566566
error: `#[subdiagnostic(...)]` is not a valid attribute
567567
--> $DIR/diagnostic-derive.rs:717:5
568568
|
569569
LL | #[subdiagnostic(eager)]
570570
| ^^^^^^^^^^^^^^^^^^^^^^^
571571
|
572-
= help: `subdiagnostic` does not support nested attributes
573-
574-
error: `#[subdiagnostic(...)]` is not a valid attribute
575-
--> $DIR/diagnostic-derive.rs:725:5
576-
|
577-
LL | #[subdiagnostic(eager)]
578-
| ^^^^^^^^^^^^^^^^^^^^^^^
579-
|
580-
= help: `subdiagnostic` does not support nested attributes
581-
582-
error: `#[subdiagnostic(...)]` is not a valid attribute
583-
--> $DIR/diagnostic-derive.rs:746:5
584-
|
585-
LL | #[subdiagnostic(eager)]
586-
| ^^^^^^^^^^^^^^^^^^^^^^^
587-
|
588-
= help: `subdiagnostic` does not support nested attributes
572+
= help: eager subdiagnostics are not supported on lints
589573

590574
error: expected at least one string literal for `code(...)`
591-
--> $DIR/diagnostic-derive.rs:777:18
575+
--> $DIR/diagnostic-derive.rs:775:18
592576
|
593577
LL | #[suggestion(code())]
594578
| ^^^^^^
595579

596580
error: `code(...)` must contain only string literals
597-
--> $DIR/diagnostic-derive.rs:785:23
581+
--> $DIR/diagnostic-derive.rs:783:23
598582
|
599583
LL | #[suggestion(code(foo))]
600584
| ^^^
601585

602586
error: `code = "..."`/`code(...)` must contain only string literals
603-
--> $DIR/diagnostic-derive.rs:793:18
587+
--> $DIR/diagnostic-derive.rs:791:18
604588
|
605589
LL | #[suggestion(code = 3)]
606590
| ^^^^^^^^
@@ -676,7 +660,7 @@ note: required by a bound in `DiagnosticBuilder::<'a, G>::set_arg`
676660
--> $COMPILER_DIR/rustc_errors/src/diagnostic_builder.rs:LL:CC
677661
= note: this error originates in the derive macro `Diagnostic` which comes from the expansion of the macro `forward` (in Nightly builds, run with -Z macro-backtrace for more info)
678662

679-
error: aborting due to 85 previous errors
663+
error: aborting due to 83 previous errors
680664

681665
Some errors have detailed explanations: E0277, E0425.
682666
For more information about an error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)