Skip to content

Commit fc01b4b

Browse files
committed
fix and bless ui tests 1/2
1 parent af74ef8 commit fc01b4b

File tree

5 files changed

+288
-271
lines changed

5 files changed

+288
-271
lines changed

compiler/rustc_macros/src/diagnostics/diagnostic_builder.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,15 @@ impl<'a> DiagnosticDeriveVariantBuilder<'a> {
214214
if path.is_ident("code") {
215215
self.code.set_once((), path.span().unwrap());
216216

217-
let code = nested.parse::<TokenStream>()?;
217+
let code = nested.parse::<syn::LitStr>()?;
218218
tokens.extend(quote! {
219219
#diag.code(rustc_errors::DiagnosticId::Error(#code.to_string()));
220220
});
221+
} else {
222+
span_err(path.span().unwrap(), "unknown argument").note("only the `code` parameter is valid after the slug").emit();
223+
224+
// consume the buffer so we don't have syntax errors from syn
225+
let _ = nested.parse::<TokenStream>();
221226
}
222227
Ok(())
223228
})?;

compiler/rustc_macros/src/diagnostics/utils.rs

+13
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,9 @@ impl SubdiagnosticKind {
716716
}
717717
}
718718

719+
let mut has_errors = false;
720+
let input = nested.input;
721+
719722
match (nested_name, &mut kind) {
720723
("code", SubdiagnosticKind::Suggestion { code_field, .. }) => {
721724
let code_init = build_suggestion_code(
@@ -734,6 +737,7 @@ impl SubdiagnosticKind {
734737
let value = get_string!();
735738
let value = Applicability::from_str(&value.value()).unwrap_or_else(|()| {
736739
span_err(value.span().unwrap(), "invalid applicability").emit();
740+
has_errors = true;
737741
Applicability::Unspecified
738742
});
739743
applicability.set_once(value, span);
@@ -749,6 +753,7 @@ impl SubdiagnosticKind {
749753
span_err(value.span().unwrap(), "invalid suggestion style")
750754
.help("valid styles are `normal`, `short`, `hidden`, `verbose` and `tool-only`")
751755
.emit();
756+
has_errors = true;
752757
SuggestionKind::Normal
753758
});
754759

@@ -762,17 +767,25 @@ impl SubdiagnosticKind {
762767
"only `style`, `code` and `applicability` are valid nested attributes",
763768
)
764769
.emit();
770+
has_errors = true;
765771
}
766772
(_, SubdiagnosticKind::MultipartSuggestion { .. }) => {
767773
span_err(path_span, "invalid nested attribute")
768774
.help("only `style` and `applicability` are valid nested attributes")
769775
.emit();
776+
has_errors = true;
770777
}
771778
_ => {
772779
span_err(path_span, "invalid nested attribute").emit();
780+
has_errors = true;
773781
}
774782
}
775783

784+
if has_errors {
785+
// Consume the rest of the input to avoid spamming errors
786+
let _ = input.parse::<TokenStream>();
787+
}
788+
776789
Ok(())
777790
})?;
778791

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

+21-23
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ enum DiagnosticOnEnum {
5050
#[derive(Diagnostic)]
5151
#[diag(no_crate_example, code = "E0123")]
5252
#[diag = "E0123"]
53-
//~^ ERROR `#[diag = ...]` is not a valid attribute
53+
//~^ ERROR expected parentheses: #[diag(...)]
5454
struct WrongStructAttrStyle {}
5555

5656
#[derive(Diagnostic)]
@@ -62,8 +62,7 @@ struct InvalidStructAttr {}
6262

6363
#[derive(Diagnostic)]
6464
#[diag("E0123")]
65-
//~^ ERROR `#[diag("...")]` is not a valid attribute
66-
//~^^ ERROR diagnostic slug not specified
65+
//~^ ERROR diagnostic slug not specified
6766
struct InvalidLitNestedAttr {}
6867

6968
#[derive(Diagnostic)]
@@ -73,27 +72,25 @@ struct InvalidNestedStructAttr {}
7372

7473
#[derive(Diagnostic)]
7574
#[diag(nonsense("foo"), code = "E0123", slug = "foo")]
76-
//~^ ERROR `#[diag(nonsense(...))]` is not a valid attribute
77-
//~^^ ERROR diagnostic slug not specified
75+
//~^ ERROR diagnostic slug must be the first argument
76+
//~| ERROR diagnostic slug not specified
7877
struct InvalidNestedStructAttr1 {}
7978

8079
#[derive(Diagnostic)]
8180
#[diag(nonsense = "...", code = "E0123", slug = "foo")]
82-
//~^ ERROR `#[diag(nonsense = ...)]` is not a valid attribute
83-
//~| ERROR `#[diag(slug = ...)]` is not a valid attribute
81+
//~^ ERROR unknown argument
8482
//~| ERROR diagnostic slug not specified
8583
struct InvalidNestedStructAttr2 {}
8684

8785
#[derive(Diagnostic)]
8886
#[diag(nonsense = 4, code = "E0123", slug = "foo")]
89-
//~^ ERROR `#[diag(nonsense = ...)]` is not a valid attribute
90-
//~| ERROR `#[diag(slug = ...)]` is not a valid attribute
87+
//~^ ERROR unknown argument
9188
//~| ERROR diagnostic slug not specified
9289
struct InvalidNestedStructAttr3 {}
9390

9491
#[derive(Diagnostic)]
9592
#[diag(no_crate_example, code = "E0123", slug = "foo")]
96-
//~^ ERROR `#[diag(slug = ...)]` is not a valid attribute
93+
//~^ ERROR unknown argument
9794
struct InvalidNestedStructAttr4 {}
9895

9996
#[derive(Diagnostic)]
@@ -118,7 +115,7 @@ struct CodeSpecifiedTwice {}
118115

119116
#[derive(Diagnostic)]
120117
#[diag(no_crate_example, no_crate::example, code = "E0456")]
121-
//~^ ERROR `#[diag(no_crate::example)]` is not a valid attribute
118+
//~^ ERROR diagnostic slug must be the first argument
122119
struct SlugSpecifiedTwice {}
123120

124121
#[derive(Diagnostic)]
@@ -232,7 +229,7 @@ struct SuggestWithoutCode {
232229
#[diag(no_crate_example, code = "E0123")]
233230
struct SuggestWithBadKey {
234231
#[suggestion(nonsense = "bar")]
235-
//~^ ERROR `#[suggestion(nonsense = ...)]` is not a valid attribute
232+
//~^ ERROR invalid nested attribute
236233
//~| ERROR suggestion without `code = "..."`
237234
suggestion: (Span, Applicability),
238235
}
@@ -241,7 +238,7 @@ struct SuggestWithBadKey {
241238
#[diag(no_crate_example, code = "E0123")]
242239
struct SuggestWithShorthandMsg {
243240
#[suggestion(msg = "bar")]
244-
//~^ ERROR `#[suggestion(msg = ...)]` is not a valid attribute
241+
//~^ ERROR invalid nested attribute
245242
//~| ERROR suggestion without `code = "..."`
246243
suggestion: (Span, Applicability),
247244
}
@@ -530,23 +527,23 @@ struct BoolField {
530527
#[diag(no_crate_example, code = "E0123")]
531528
struct LabelWithTrailingPath {
532529
#[label(no_crate_label, foo)]
533-
//~^ ERROR `#[label(foo)]` is not a valid attribute
530+
//~^ ERROR a diagnostic slug must be the first argument to the attribute
534531
span: Span,
535532
}
536533

537534
#[derive(Diagnostic)]
538535
#[diag(no_crate_example, code = "E0123")]
539536
struct LabelWithTrailingNameValue {
540537
#[label(no_crate_label, foo = "...")]
541-
//~^ ERROR `#[label(foo = ...)]` is not a valid attribute
538+
//~^ ERROR invalid nested attribute
542539
span: Span,
543540
}
544541

545542
#[derive(Diagnostic)]
546543
#[diag(no_crate_example, code = "E0123")]
547544
struct LabelWithTrailingList {
548545
#[label(no_crate_label, foo("..."))]
549-
//~^ ERROR `#[label(foo(...))]` is not a valid attribute
546+
//~^ ERROR invalid nested attribute
550547
span: Span,
551548
}
552549

@@ -643,8 +640,8 @@ struct MissingCodeInSuggestion {
643640
//~^ ERROR `#[multipart_suggestion(...)]` is not a valid attribute
644641
//~| ERROR cannot find attribute `multipart_suggestion` in this scope
645642
#[multipart_suggestion()]
646-
//~^ ERROR `#[multipart_suggestion(...)]` is not a valid attribute
647-
//~| ERROR cannot find attribute `multipart_suggestion` in this scope
643+
//~^ ERROR cannot find attribute `multipart_suggestion` in this scope
644+
//~| ERROR unexpected end of input, unexpected token in nested attribute, expected ident
648645
struct MultipartSuggestion {
649646
#[multipart_suggestion(no_crate_suggestion)]
650647
//~^ ERROR `#[multipart_suggestion(...)]` is not a valid attribute
@@ -698,7 +695,7 @@ struct RawIdentDiagnosticArg {
698695
#[diag(no_crate_example)]
699696
struct SubdiagnosticBad {
700697
#[subdiagnostic(bad)]
701-
//~^ ERROR `#[subdiagnostic(...)]` is not a valid attribute
698+
//~^ ERROR `eager` is the only supported nested attribute for `subdiagnostic`
702699
note: Note,
703700
}
704701

@@ -714,15 +711,15 @@ struct SubdiagnosticBadStr {
714711
#[diag(no_crate_example)]
715712
struct SubdiagnosticBadTwice {
716713
#[subdiagnostic(bad, bad)]
717-
//~^ ERROR `#[subdiagnostic(...)]` is not a valid attribute
714+
//~^ ERROR `eager` is the only supported nested attribute for `subdiagnostic`
718715
note: Note,
719716
}
720717

721718
#[derive(Diagnostic)]
722719
#[diag(no_crate_example)]
723720
struct SubdiagnosticBadLitStr {
724721
#[subdiagnostic("bad")]
725-
//~^ ERROR `#[subdiagnostic(...)]` is not a valid attribute
722+
//~^ ERROR `eager` is the only supported nested attribute for `subdiagnostic`
726723
note: Note,
727724
}
728725

@@ -797,14 +794,15 @@ struct SuggestionsNoItem {
797794
struct SuggestionsInvalidItem {
798795
#[suggestion(code(foo))]
799796
//~^ ERROR `code(...)` must contain only string literals
797+
//~| ERROR unexpected token
800798
sub: Span,
801799
}
802800

803-
#[derive(Diagnostic)]
801+
#[derive(Diagnostic)] //~ ERROR cannot find value `__code_34` in this scope
804802
#[diag(no_crate_example)]
805803
struct SuggestionsInvalidLiteral {
806804
#[suggestion(code = 3)]
807-
//~^ ERROR `code = "..."`/`code(...)` must contain only string literals
805+
//~^ ERROR expected string literal
808806
sub: Span,
809807
}
810808

0 commit comments

Comments
 (0)