1
Fork 0

macros: allow subdiagnostic-kind-less variants

Sometimes it is convenient to return a subdiagnostic enum where one or
more of the variants don't add anything to the diagnostic.

Signed-off-by: David Wood <david.wood@huawei.com>
This commit is contained in:
David Wood 2022-10-14 11:09:52 +01:00
parent 7fbaf27696
commit 1045e69e73
3 changed files with 59 additions and 55 deletions

View file

@ -41,7 +41,8 @@ impl SubdiagnosticDeriveBuilder {
} }
} }
if matches!(ast.data, syn::Data::Enum(..)) { let is_enum = matches!(ast.data, syn::Data::Enum(..));
if is_enum {
for attr in &ast.attrs { for attr in &ast.attrs {
// Always allow documentation comments. // Always allow documentation comments.
if is_doc_comment(attr) { if is_doc_comment(attr) {
@ -67,6 +68,7 @@ impl SubdiagnosticDeriveBuilder {
span_field: None, span_field: None,
applicability: None, applicability: None,
has_suggestion_parts: false, has_suggestion_parts: false,
is_enum,
}; };
builder.into_tokens().unwrap_or_else(|v| v.to_compile_error()) builder.into_tokens().unwrap_or_else(|v| v.to_compile_error())
}); });
@ -127,6 +129,9 @@ struct SubdiagnosticDeriveVariantBuilder<'parent, 'a> {
/// Set to true when a `#[suggestion_part]` field is encountered, used to generate an error /// Set to true when a `#[suggestion_part]` field is encountered, used to generate an error
/// during finalization if still `false`. /// during finalization if still `false`.
has_suggestion_parts: bool, has_suggestion_parts: bool,
/// Set to true when this variant is an enum variant rather than just the body of a struct.
is_enum: bool,
} }
impl<'parent, 'a> HasFieldMap for SubdiagnosticDeriveVariantBuilder<'parent, 'a> { impl<'parent, 'a> HasFieldMap for SubdiagnosticDeriveVariantBuilder<'parent, 'a> {
@ -457,10 +462,16 @@ impl<'parent, 'a> SubdiagnosticDeriveVariantBuilder<'parent, 'a> {
pub fn into_tokens(&mut self) -> Result<TokenStream, DiagnosticDeriveError> { pub fn into_tokens(&mut self) -> Result<TokenStream, DiagnosticDeriveError> {
let kind_slugs = self.identify_kind()?; let kind_slugs = self.identify_kind()?;
if kind_slugs.is_empty() { if kind_slugs.is_empty() {
if self.is_enum {
// It's okay for a variant to not be a subdiagnostic at all..
return Ok(quote! {});
} else {
// ..but structs should always be _something_.
throw_span_err!( throw_span_err!(
self.variant.ast().ident.span().unwrap(), self.variant.ast().ident.span().unwrap(),
"subdiagnostic kind not specified" "subdiagnostic kind not specified"
); );
}
}; };
let kind_stats: KindsStatistics = kind_slugs.iter().map(|(kind, _slug)| kind).collect(); let kind_stats: KindsStatistics = kind_slugs.iter().map(|(kind, _slug)| kind).collect();

View file

@ -237,7 +237,6 @@ enum V {
var: String, var: String,
}, },
B { B {
//~^ ERROR subdiagnostic kind not specified
#[primary_span] #[primary_span]
span: Span, span: Span,
var: String, var: String,

View file

@ -134,20 +134,14 @@ error: diagnostic slug must be first argument of a `#[label(...)]` attribute
LL | #[label(code = "...")] LL | #[label(code = "...")]
| ^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^
error: subdiagnostic kind not specified
--> $DIR/subdiagnostic-derive.rs:239:5
|
LL | B {
| ^
error: the `#[primary_span]` attribute can only be applied to fields of type `Span` or `MultiSpan` error: the `#[primary_span]` attribute can only be applied to fields of type `Span` or `MultiSpan`
--> $DIR/subdiagnostic-derive.rs:251:5 --> $DIR/subdiagnostic-derive.rs:250:5
| |
LL | #[primary_span] LL | #[primary_span]
| ^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^
error: label without `#[primary_span]` field error: label without `#[primary_span]` field
--> $DIR/subdiagnostic-derive.rs:248:1 --> $DIR/subdiagnostic-derive.rs:247:1
| |
LL | / #[label(parser::add_paren)] LL | / #[label(parser::add_paren)]
LL | | LL | |
@ -159,13 +153,13 @@ LL | | }
| |_^ | |_^
error: `#[applicability]` is only valid on suggestions error: `#[applicability]` is only valid on suggestions
--> $DIR/subdiagnostic-derive.rs:261:5 --> $DIR/subdiagnostic-derive.rs:260:5
| |
LL | #[applicability] LL | #[applicability]
| ^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^
error: `#[bar]` is not a valid attribute error: `#[bar]` is not a valid attribute
--> $DIR/subdiagnostic-derive.rs:271:5 --> $DIR/subdiagnostic-derive.rs:270:5
| |
LL | #[bar] LL | #[bar]
| ^^^^^^ | ^^^^^^
@ -173,13 +167,13 @@ LL | #[bar]
= help: only `primary_span`, `applicability` and `skip_arg` are valid field attributes = help: only `primary_span`, `applicability` and `skip_arg` are valid field attributes
error: `#[bar = ...]` is not a valid attribute error: `#[bar = ...]` is not a valid attribute
--> $DIR/subdiagnostic-derive.rs:282:5 --> $DIR/subdiagnostic-derive.rs:281:5
| |
LL | #[bar = "..."] LL | #[bar = "..."]
| ^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^
error: `#[bar(...)]` is not a valid attribute error: `#[bar(...)]` is not a valid attribute
--> $DIR/subdiagnostic-derive.rs:293:5 --> $DIR/subdiagnostic-derive.rs:292:5
| |
LL | #[bar("...")] LL | #[bar("...")]
| ^^^^^^^^^^^^^ | ^^^^^^^^^^^^^
@ -187,7 +181,7 @@ LL | #[bar("...")]
= help: only `primary_span`, `applicability` and `skip_arg` are valid field attributes = help: only `primary_span`, `applicability` and `skip_arg` are valid field attributes
error: unexpected unsupported untagged union error: unexpected unsupported untagged union
--> $DIR/subdiagnostic-derive.rs:309:1 --> $DIR/subdiagnostic-derive.rs:308:1
| |
LL | / union AC { LL | / union AC {
LL | | LL | |
@ -197,7 +191,7 @@ LL | | }
| |_^ | |_^
error: `#[label(parser::add_paren)]` is not a valid attribute error: `#[label(parser::add_paren)]` is not a valid attribute
--> $DIR/subdiagnostic-derive.rs:324:28 --> $DIR/subdiagnostic-derive.rs:323:28
| |
LL | #[label(parser::add_paren, parser::add_paren)] LL | #[label(parser::add_paren, parser::add_paren)]
| ^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^
@ -205,67 +199,67 @@ LL | #[label(parser::add_paren, parser::add_paren)]
= help: a diagnostic slug must be the first argument to the attribute = help: a diagnostic slug must be the first argument to the attribute
error: specified multiple times error: specified multiple times
--> $DIR/subdiagnostic-derive.rs:337:5 --> $DIR/subdiagnostic-derive.rs:336:5
| |
LL | #[primary_span] LL | #[primary_span]
| ^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^
| |
note: previously specified here note: previously specified here
--> $DIR/subdiagnostic-derive.rs:334:5 --> $DIR/subdiagnostic-derive.rs:333:5
| |
LL | #[primary_span] LL | #[primary_span]
| ^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^
error: subdiagnostic kind not specified error: subdiagnostic kind not specified
--> $DIR/subdiagnostic-derive.rs:343:8 --> $DIR/subdiagnostic-derive.rs:342:8
| |
LL | struct AG { LL | struct AG {
| ^^ | ^^
error: specified multiple times error: specified multiple times
--> $DIR/subdiagnostic-derive.rs:380:47 --> $DIR/subdiagnostic-derive.rs:379:47
| |
LL | #[suggestion(parser::add_paren, code = "...", code = "...")] LL | #[suggestion(parser::add_paren, code = "...", code = "...")]
| ^^^^^^^^^^^^ | ^^^^^^^^^^^^
| |
note: previously specified here note: previously specified here
--> $DIR/subdiagnostic-derive.rs:380:33 --> $DIR/subdiagnostic-derive.rs:379:33
| |
LL | #[suggestion(parser::add_paren, code = "...", code = "...")] LL | #[suggestion(parser::add_paren, code = "...", code = "...")]
| ^^^^^^^^^^^^ | ^^^^^^^^^^^^
error: specified multiple times error: specified multiple times
--> $DIR/subdiagnostic-derive.rs:398:5 --> $DIR/subdiagnostic-derive.rs:397:5
| |
LL | #[applicability] LL | #[applicability]
| ^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^
| |
note: previously specified here note: previously specified here
--> $DIR/subdiagnostic-derive.rs:395:5 --> $DIR/subdiagnostic-derive.rs:394:5
| |
LL | #[applicability] LL | #[applicability]
| ^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^
error: the `#[applicability]` attribute can only be applied to fields of type `Applicability` error: the `#[applicability]` attribute can only be applied to fields of type `Applicability`
--> $DIR/subdiagnostic-derive.rs:408:5 --> $DIR/subdiagnostic-derive.rs:407:5
| |
LL | #[applicability] LL | #[applicability]
| ^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^
error: suggestion without `code = "..."` error: suggestion without `code = "..."`
--> $DIR/subdiagnostic-derive.rs:421:1 --> $DIR/subdiagnostic-derive.rs:420:1
| |
LL | #[suggestion(parser::add_paren)] LL | #[suggestion(parser::add_paren)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: invalid applicability error: invalid applicability
--> $DIR/subdiagnostic-derive.rs:431:46 --> $DIR/subdiagnostic-derive.rs:430:46
| |
LL | #[suggestion(parser::add_paren, code ="...", applicability = "foo")] LL | #[suggestion(parser::add_paren, code ="...", applicability = "foo")]
| ^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^
error: suggestion without `#[primary_span]` field error: suggestion without `#[primary_span]` field
--> $DIR/subdiagnostic-derive.rs:449:1 --> $DIR/subdiagnostic-derive.rs:448:1
| |
LL | / #[suggestion(parser::add_paren, code = "...")] LL | / #[suggestion(parser::add_paren, code = "...")]
LL | | LL | |
@ -275,25 +269,25 @@ LL | | }
| |_^ | |_^
error: unsupported type attribute for subdiagnostic enum error: unsupported type attribute for subdiagnostic enum
--> $DIR/subdiagnostic-derive.rs:463:1 --> $DIR/subdiagnostic-derive.rs:462:1
| |
LL | #[label] LL | #[label]
| ^^^^^^^^ | ^^^^^^^^
error: `var` doesn't refer to a field on this type error: `var` doesn't refer to a field on this type
--> $DIR/subdiagnostic-derive.rs:483:39 --> $DIR/subdiagnostic-derive.rs:482:39
| |
LL | #[suggestion(parser::add_paren, code ="{var}", applicability = "machine-applicable")] LL | #[suggestion(parser::add_paren, code ="{var}", applicability = "machine-applicable")]
| ^^^^^^^ | ^^^^^^^
error: `var` doesn't refer to a field on this type error: `var` doesn't refer to a field on this type
--> $DIR/subdiagnostic-derive.rs:502:43 --> $DIR/subdiagnostic-derive.rs:501:43
| |
LL | #[suggestion(parser::add_paren, code ="{var}", applicability = "machine-applicable")] LL | #[suggestion(parser::add_paren, code ="{var}", applicability = "machine-applicable")]
| ^^^^^^^ | ^^^^^^^
error: `#[suggestion_part]` is not a valid attribute error: `#[suggestion_part]` is not a valid attribute
--> $DIR/subdiagnostic-derive.rs:525:5 --> $DIR/subdiagnostic-derive.rs:524:5
| |
LL | #[suggestion_part] LL | #[suggestion_part]
| ^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^
@ -301,7 +295,7 @@ LL | #[suggestion_part]
= help: `#[suggestion_part(...)]` is only valid in multipart suggestions, use `#[primary_span]` instead = help: `#[suggestion_part(...)]` is only valid in multipart suggestions, use `#[primary_span]` instead
error: `#[suggestion_part(...)]` is not a valid attribute error: `#[suggestion_part(...)]` is not a valid attribute
--> $DIR/subdiagnostic-derive.rs:528:5 --> $DIR/subdiagnostic-derive.rs:527:5
| |
LL | #[suggestion_part(code = "...")] LL | #[suggestion_part(code = "...")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -309,7 +303,7 @@ LL | #[suggestion_part(code = "...")]
= help: `#[suggestion_part(...)]` is only valid in multipart suggestions = help: `#[suggestion_part(...)]` is only valid in multipart suggestions
error: suggestion without `#[primary_span]` field error: suggestion without `#[primary_span]` field
--> $DIR/subdiagnostic-derive.rs:522:1 --> $DIR/subdiagnostic-derive.rs:521:1
| |
LL | / #[suggestion(parser::add_paren, code = "...")] LL | / #[suggestion(parser::add_paren, code = "...")]
LL | | LL | |
@ -321,7 +315,7 @@ LL | | }
| |_^ | |_^
error: `#[multipart_suggestion(code = ...)]` is not a valid attribute error: `#[multipart_suggestion(code = ...)]` is not a valid attribute
--> $DIR/subdiagnostic-derive.rs:537:43 --> $DIR/subdiagnostic-derive.rs:536:43
| |
LL | #[multipart_suggestion(parser::add_paren, code = "...", applicability = "machine-applicable")] LL | #[multipart_suggestion(parser::add_paren, code = "...", applicability = "machine-applicable")]
| ^^^^^^^^^^^^ | ^^^^^^^^^^^^
@ -329,7 +323,7 @@ LL | #[multipart_suggestion(parser::add_paren, code = "...", applicability = "ma
= help: only `applicability` is a valid nested attributes = help: only `applicability` is a valid nested attributes
error: multipart suggestion without any `#[suggestion_part(...)]` fields error: multipart suggestion without any `#[suggestion_part(...)]` fields
--> $DIR/subdiagnostic-derive.rs:537:1 --> $DIR/subdiagnostic-derive.rs:536:1
| |
LL | / #[multipart_suggestion(parser::add_paren, code = "...", applicability = "machine-applicable")] LL | / #[multipart_suggestion(parser::add_paren, code = "...", applicability = "machine-applicable")]
LL | | LL | |
@ -340,19 +334,19 @@ LL | | }
| |_^ | |_^
error: `#[suggestion_part(...)]` attribute without `code = "..."` error: `#[suggestion_part(...)]` attribute without `code = "..."`
--> $DIR/subdiagnostic-derive.rs:547:5 --> $DIR/subdiagnostic-derive.rs:546:5
| |
LL | #[suggestion_part] LL | #[suggestion_part]
| ^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^
error: `#[suggestion_part(...)]` attribute without `code = "..."` error: `#[suggestion_part(...)]` attribute without `code = "..."`
--> $DIR/subdiagnostic-derive.rs:555:5 --> $DIR/subdiagnostic-derive.rs:554:5
| |
LL | #[suggestion_part()] LL | #[suggestion_part()]
| ^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^
error: `#[primary_span]` is not a valid attribute error: `#[primary_span]` is not a valid attribute
--> $DIR/subdiagnostic-derive.rs:564:5 --> $DIR/subdiagnostic-derive.rs:563:5
| |
LL | #[primary_span] LL | #[primary_span]
| ^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^
@ -360,7 +354,7 @@ LL | #[primary_span]
= help: multipart suggestions use one or more `#[suggestion_part]`s rather than one `#[primary_span]` = help: multipart suggestions use one or more `#[suggestion_part]`s rather than one `#[primary_span]`
error: multipart suggestion without any `#[suggestion_part(...)]` fields error: multipart suggestion without any `#[suggestion_part(...)]` fields
--> $DIR/subdiagnostic-derive.rs:561:1 --> $DIR/subdiagnostic-derive.rs:560:1
| |
LL | / #[multipart_suggestion(parser::add_paren)] LL | / #[multipart_suggestion(parser::add_paren)]
LL | | LL | |
@ -372,19 +366,19 @@ LL | | }
| |_^ | |_^
error: `#[suggestion_part(...)]` attribute without `code = "..."` error: `#[suggestion_part(...)]` attribute without `code = "..."`
--> $DIR/subdiagnostic-derive.rs:572:5 --> $DIR/subdiagnostic-derive.rs:571:5
| |
LL | #[suggestion_part] LL | #[suggestion_part]
| ^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^
error: `#[suggestion_part(...)]` attribute without `code = "..."` error: `#[suggestion_part(...)]` attribute without `code = "..."`
--> $DIR/subdiagnostic-derive.rs:575:5 --> $DIR/subdiagnostic-derive.rs:574:5
| |
LL | #[suggestion_part()] LL | #[suggestion_part()]
| ^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^
error: `#[suggestion_part(foo = ...)]` is not a valid attribute error: `#[suggestion_part(foo = ...)]` is not a valid attribute
--> $DIR/subdiagnostic-derive.rs:578:23 --> $DIR/subdiagnostic-derive.rs:577:23
| |
LL | #[suggestion_part(foo = "bar")] LL | #[suggestion_part(foo = "bar")]
| ^^^^^^^^^^^ | ^^^^^^^^^^^
@ -392,31 +386,31 @@ LL | #[suggestion_part(foo = "bar")]
= help: `code` is the only valid nested attribute = help: `code` is the only valid nested attribute
error: the `#[suggestion_part(...)]` attribute can only be applied to fields of type `Span` or `MultiSpan` error: the `#[suggestion_part(...)]` attribute can only be applied to fields of type `Span` or `MultiSpan`
--> $DIR/subdiagnostic-derive.rs:581:5 --> $DIR/subdiagnostic-derive.rs:580:5
| |
LL | #[suggestion_part(code = "...")] LL | #[suggestion_part(code = "...")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: the `#[suggestion_part(...)]` attribute can only be applied to fields of type `Span` or `MultiSpan` error: the `#[suggestion_part(...)]` attribute can only be applied to fields of type `Span` or `MultiSpan`
--> $DIR/subdiagnostic-derive.rs:584:5 --> $DIR/subdiagnostic-derive.rs:583:5
| |
LL | #[suggestion_part()] LL | #[suggestion_part()]
| ^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^
error: specified multiple times error: specified multiple times
--> $DIR/subdiagnostic-derive.rs:592:37 --> $DIR/subdiagnostic-derive.rs:591:37
| |
LL | #[suggestion_part(code = "...", code = ",,,")] LL | #[suggestion_part(code = "...", code = ",,,")]
| ^^^^^^^^^^^^ | ^^^^^^^^^^^^
| |
note: previously specified here note: previously specified here
--> $DIR/subdiagnostic-derive.rs:592:23 --> $DIR/subdiagnostic-derive.rs:591:23
| |
LL | #[suggestion_part(code = "...", code = ",,,")] LL | #[suggestion_part(code = "...", code = ",,,")]
| ^^^^^^^^^^^^ | ^^^^^^^^^^^^
error: `#[applicability]` has no effect if all `#[suggestion]`/`#[multipart_suggestion]` attributes have a static `applicability = "..."` error: `#[applicability]` has no effect if all `#[suggestion]`/`#[multipart_suggestion]` attributes have a static `applicability = "..."`
--> $DIR/subdiagnostic-derive.rs:621:5 --> $DIR/subdiagnostic-derive.rs:620:5
| |
LL | #[applicability] LL | #[applicability]
| ^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^
@ -458,19 +452,19 @@ LL | #[bar("...")]
| ^^^ | ^^^
error: cannot find attribute `bar` in this scope error: cannot find attribute `bar` in this scope
--> $DIR/subdiagnostic-derive.rs:271:7 --> $DIR/subdiagnostic-derive.rs:270:7
| |
LL | #[bar] LL | #[bar]
| ^^^ | ^^^
error: cannot find attribute `bar` in this scope error: cannot find attribute `bar` in this scope
--> $DIR/subdiagnostic-derive.rs:282:7 --> $DIR/subdiagnostic-derive.rs:281:7
| |
LL | #[bar = "..."] LL | #[bar = "..."]
| ^^^ | ^^^
error: cannot find attribute `bar` in this scope error: cannot find attribute `bar` in this scope
--> $DIR/subdiagnostic-derive.rs:293:7 --> $DIR/subdiagnostic-derive.rs:292:7
| |
LL | #[bar("...")] LL | #[bar("...")]
| ^^^ | ^^^
@ -481,6 +475,6 @@ error[E0425]: cannot find value `slug` in module `rustc_errors::fluent`
LL | #[label(slug)] LL | #[label(slug)]
| ^^^^ not found in `rustc_errors::fluent` | ^^^^ not found in `rustc_errors::fluent`
error: aborting due to 68 previous errors error: aborting due to 67 previous errors
For more information about this error, try `rustc --explain E0425`. For more information about this error, try `rustc --explain E0425`.