Skip to content

Commit dca8861

Browse files
committed
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 <[email protected]>
1 parent e5d9371 commit dca8861

File tree

4 files changed

+16
-3
lines changed

4 files changed

+16
-3
lines changed

compiler/rustc_macros/src/diagnostics/diagnostic.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -404,9 +404,10 @@ impl SessionDiagnosticDeriveBuilder {
404404
report_error_if_not_applied_to_span(attr, &info)?;
405405
Ok(self.add_subdiagnostic(field_binding, name, name))
406406
}
407+
"subdiagnostic" => Ok(quote! { #diag.subdiagnostic(*#field_binding); }),
407408
_ => throw_invalid_attr!(attr, &meta, |diag| {
408409
diag
409-
.help("only `skip_arg`, `primary_span`, `label`, `note` and `help` are valid field attributes")
410+
.help("only `skip_arg`, `primary_span`, `label`, `note`, `help` and `subdiagnostic` are valid field attributes")
410411
}),
411412
},
412413
Meta::NameValue(MetaNameValue { lit: syn::Lit::Str(ref s), .. }) => match name {

compiler/rustc_macros/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ decl_derive!(
7373
skip_arg,
7474
primary_span,
7575
label,
76+
subdiagnostic,
7677
suggestion,
7778
suggestion_short,
7879
suggestion_hidden,

src/test/ui-fulldeps/session-diagnostic/diagnostic-derive.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_span::symbol::Ident;
1515
use rustc_span::Span;
1616

1717
extern crate rustc_macros;
18-
use rustc_macros::SessionDiagnostic;
18+
use rustc_macros::{SessionDiagnostic, SessionSubdiagnostic};
1919

2020
extern crate rustc_middle;
2121
use rustc_middle::ty::Ty;
@@ -463,3 +463,14 @@ struct NoApplicability {
463463
#[suggestion(message = "bar", code = "...")]
464464
suggestion: Span,
465465
}
466+
467+
#[derive(SessionSubdiagnostic)]
468+
#[note(slug = "note")]
469+
struct Note;
470+
471+
#[derive(SessionDiagnostic)]
472+
#[error(slug = "subdiagnostic")]
473+
struct Subdiagnostic {
474+
#[subdiagnostic]
475+
note: Note,
476+
}

src/test/ui-fulldeps/session-diagnostic/diagnostic-derive.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ error: `#[nonsense]` is not a valid attribute
211211
LL | #[nonsense]
212212
| ^^^^^^^^^^^
213213
|
214-
= help: only `skip_arg`, `primary_span`, `label`, `note` and `help` are valid field attributes
214+
= help: only `skip_arg`, `primary_span`, `label`, `note`, `help` and `subdiagnostic` are valid field attributes
215215

216216
error: the `#[label = ...]` attribute can only be applied to fields of type `Span`
217217
--> $DIR/diagnostic-derive.rs:156:5

0 commit comments

Comments
 (0)