From dca88612b96db761f86ab66d85836baf0e804910 Mon Sep 17 00:00:00 2001 From: David Wood Date: Wed, 27 Apr 2022 05:59:48 +0100 Subject: [PATCH] macros: add interop between diagnostic derives Add `#[subdiagnostic]` field attribute to the diagnostic derive which is applied to fields that have types which use the subdiagnostic derive. Signed-off-by: David Wood --- compiler/rustc_macros/src/diagnostics/diagnostic.rs | 3 ++- compiler/rustc_macros/src/lib.rs | 1 + .../session-diagnostic/diagnostic-derive.rs | 13 ++++++++++++- .../session-diagnostic/diagnostic-derive.stderr | 2 +- 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_macros/src/diagnostics/diagnostic.rs b/compiler/rustc_macros/src/diagnostics/diagnostic.rs index c163f233b47..f49166433fa 100644 --- a/compiler/rustc_macros/src/diagnostics/diagnostic.rs +++ b/compiler/rustc_macros/src/diagnostics/diagnostic.rs @@ -404,9 +404,10 @@ impl SessionDiagnosticDeriveBuilder { report_error_if_not_applied_to_span(attr, &info)?; Ok(self.add_subdiagnostic(field_binding, name, name)) } + "subdiagnostic" => Ok(quote! { #diag.subdiagnostic(*#field_binding); }), _ => throw_invalid_attr!(attr, &meta, |diag| { diag - .help("only `skip_arg`, `primary_span`, `label`, `note` and `help` are valid field attributes") + .help("only `skip_arg`, `primary_span`, `label`, `note`, `help` and `subdiagnostic` are valid field attributes") }), }, Meta::NameValue(MetaNameValue { lit: syn::Lit::Str(ref s), .. }) => match name { diff --git a/compiler/rustc_macros/src/lib.rs b/compiler/rustc_macros/src/lib.rs index ed2021e7d74..b01e01414e8 100644 --- a/compiler/rustc_macros/src/lib.rs +++ b/compiler/rustc_macros/src/lib.rs @@ -73,6 +73,7 @@ decl_derive!( skip_arg, primary_span, label, + subdiagnostic, suggestion, suggestion_short, suggestion_hidden, diff --git a/src/test/ui-fulldeps/session-diagnostic/diagnostic-derive.rs b/src/test/ui-fulldeps/session-diagnostic/diagnostic-derive.rs index ddcaa05e1e4..efbf78ac87d 100644 --- a/src/test/ui-fulldeps/session-diagnostic/diagnostic-derive.rs +++ b/src/test/ui-fulldeps/session-diagnostic/diagnostic-derive.rs @@ -15,7 +15,7 @@ use rustc_span::symbol::Ident; use rustc_span::Span; extern crate rustc_macros; -use rustc_macros::SessionDiagnostic; +use rustc_macros::{SessionDiagnostic, SessionSubdiagnostic}; extern crate rustc_middle; use rustc_middle::ty::Ty; @@ -463,3 +463,14 @@ struct NoApplicability { #[suggestion(message = "bar", code = "...")] suggestion: Span, } + +#[derive(SessionSubdiagnostic)] +#[note(slug = "note")] +struct Note; + +#[derive(SessionDiagnostic)] +#[error(slug = "subdiagnostic")] +struct Subdiagnostic { + #[subdiagnostic] + note: Note, +} diff --git a/src/test/ui-fulldeps/session-diagnostic/diagnostic-derive.stderr b/src/test/ui-fulldeps/session-diagnostic/diagnostic-derive.stderr index b26e805718a..b1738b60bc0 100644 --- a/src/test/ui-fulldeps/session-diagnostic/diagnostic-derive.stderr +++ b/src/test/ui-fulldeps/session-diagnostic/diagnostic-derive.stderr @@ -211,7 +211,7 @@ error: `#[nonsense]` is not a valid attribute LL | #[nonsense] | ^^^^^^^^^^^ | - = help: only `skip_arg`, `primary_span`, `label`, `note` and `help` are valid field attributes + = help: only `skip_arg`, `primary_span`, `label`, `note`, `help` and `subdiagnostic` are valid field attributes error: the `#[label = ...]` attribute can only be applied to fields of type `Span` --> $DIR/diagnostic-derive.rs:156:5