2 // Tests error conditions for specifying diagnostics using #[derive(Diagnostic)]
4 // normalize-stderr-test "the following other types implement trait `IntoDiagnosticArg`:(?:.*\n){0,9}\s+and \d+ others" -> "normalized in stderr"
5 // normalize-stderr-test "diagnostic_builder\.rs:[0-9]+:[0-9]+" -> "diagnostic_builder.rs:LL:CC"
6 // The proc_macro2 crate handles spans differently when on beta/stable release rather than nightly,
7 // changing the output of this test. Since Diagnostic is strictly internal to the compiler
8 // the test is just ignored on stable and beta:
12 #![feature(rustc_private)]
13 #![crate_type = "lib"]
15 extern crate rustc_span;
16 use rustc_span::symbol::Ident;
19 extern crate rustc_macros;
20 use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
22 extern crate rustc_middle;
23 use rustc_middle::ty::Ty;
25 extern crate rustc_errors;
26 use rustc_errors::{Applicability, MultiSpan};
28 extern crate rustc_session;
31 #[diag(compiletest::example, code = "E0123")]
35 #[diag(compiletest::example, code = "E0123")]
39 #[diag(compiletest::example, code = "E0123")]
40 //~^ ERROR unsupported type attribute for diagnostic derive enum
41 enum DiagnosticOnEnum {
43 //~^ ERROR diagnostic slug not specified
45 //~^ ERROR diagnostic slug not specified
49 #[diag(compiletest::example, code = "E0123")]
51 //~^ ERROR `#[diag = ...]` is not a valid attribute
52 struct WrongStructAttrStyle {}
55 #[nonsense(compiletest::example, code = "E0123")]
56 //~^ ERROR `#[nonsense(...)]` is not a valid attribute
57 //~^^ ERROR diagnostic slug not specified
58 //~^^^ ERROR cannot find attribute `nonsense` in this scope
59 struct InvalidStructAttr {}
63 //~^ ERROR `#[diag("...")]` is not a valid attribute
64 //~^^ ERROR diagnostic slug not specified
65 struct InvalidLitNestedAttr {}
68 #[diag(nonsense, code = "E0123")]
69 //~^ ERROR cannot find value `nonsense` in module `rustc_errors::fluent`
70 struct InvalidNestedStructAttr {}
73 #[diag(nonsense("foo"), code = "E0123", slug = "foo")]
74 //~^ ERROR `#[diag(nonsense(...))]` is not a valid attribute
75 //~^^ ERROR diagnostic slug not specified
76 struct InvalidNestedStructAttr1 {}
79 #[diag(nonsense = "...", code = "E0123", slug = "foo")]
80 //~^ ERROR `#[diag(nonsense = ...)]` is not a valid attribute
81 //~| ERROR `#[diag(slug = ...)]` is not a valid attribute
82 //~| ERROR diagnostic slug not specified
83 struct InvalidNestedStructAttr2 {}
86 #[diag(nonsense = 4, code = "E0123", slug = "foo")]
87 //~^ ERROR `#[diag(nonsense = ...)]` is not a valid attribute
88 //~| ERROR `#[diag(slug = ...)]` is not a valid attribute
89 //~| ERROR diagnostic slug not specified
90 struct InvalidNestedStructAttr3 {}
93 #[diag(compiletest::example, code = "E0123", slug = "foo")]
94 //~^ ERROR `#[diag(slug = ...)]` is not a valid attribute
95 struct InvalidNestedStructAttr4 {}
98 #[diag(compiletest::example, code = "E0123")]
99 struct WrongPlaceField {
100 #[suggestion = "bar"]
101 //~^ ERROR `#[suggestion = ...]` is not a valid attribute
105 #[derive(Diagnostic)]
106 #[diag(compiletest::example, code = "E0123")]
107 #[diag(compiletest::example, code = "E0456")]
108 //~^ ERROR specified multiple times
109 //~^^ ERROR specified multiple times
110 struct DiagSpecifiedTwice {}
112 #[derive(Diagnostic)]
113 #[diag(compiletest::example, code = "E0456", code = "E0457")]
114 //~^ ERROR specified multiple times
115 struct CodeSpecifiedTwice {}
117 #[derive(Diagnostic)]
118 #[diag(compiletest::example, compiletest::example, code = "E0456")]
119 //~^ ERROR `#[diag(compiletest::example)]` is not a valid attribute
120 struct SlugSpecifiedTwice {}
122 #[derive(Diagnostic)]
123 struct KindNotProvided {} //~ ERROR diagnostic slug not specified
125 #[derive(Diagnostic)]
126 #[diag(code = "E0456")]
127 //~^ ERROR diagnostic slug not specified
128 struct SlugNotProvided {}
130 #[derive(Diagnostic)]
131 #[diag(compiletest::example)]
132 struct CodeNotProvided {}
134 #[derive(Diagnostic)]
135 #[diag(compiletest::example, code = "E0123")]
136 struct MessageWrongType {
138 //~^ ERROR `#[primary_span]` attribute can only be applied to fields of type `Span` or `MultiSpan`
142 #[derive(Diagnostic)]
143 #[diag(compiletest::example, code = "E0123")]
144 struct InvalidPathFieldAttr {
146 //~^ ERROR `#[nonsense]` is not a valid attribute
147 //~^^ ERROR cannot find attribute `nonsense` in this scope
151 #[derive(Diagnostic)]
152 #[diag(compiletest::example, code = "E0123")]
153 struct ErrorWithField {
155 #[label(compiletest::label)]
159 #[derive(Diagnostic)]
160 #[diag(compiletest::example, code = "E0123")]
161 struct ErrorWithMessageAppliedToField {
162 #[label(compiletest::label)]
163 //~^ ERROR the `#[label(...)]` attribute can only be applied to fields of type `Span` or `MultiSpan`
167 #[derive(Diagnostic)]
168 #[diag(compiletest::example, code = "E0123")]
169 struct ErrorWithNonexistentField {
170 #[suggestion(compiletest::suggestion, code = "{name}")]
171 //~^ ERROR `name` doesn't refer to a field on this type
172 suggestion: (Span, Applicability),
175 #[derive(Diagnostic)]
176 //~^ ERROR invalid format string: expected `'}'`
177 #[diag(compiletest::example, code = "E0123")]
178 struct ErrorMissingClosingBrace {
179 #[suggestion(compiletest::suggestion, code = "{name")]
180 suggestion: (Span, Applicability),
185 #[derive(Diagnostic)]
186 //~^ ERROR invalid format string: unmatched `}`
187 #[diag(compiletest::example, code = "E0123")]
188 struct ErrorMissingOpeningBrace {
189 #[suggestion(compiletest::suggestion, code = "name}")]
190 suggestion: (Span, Applicability),
195 #[derive(Diagnostic)]
196 #[diag(compiletest::example, code = "E0123")]
198 #[label(compiletest::label)]
202 #[derive(Diagnostic)]
203 #[diag(compiletest::example, code = "E0123")]
204 struct LabelOnNonSpan {
205 #[label(compiletest::label)]
206 //~^ ERROR the `#[label(...)]` attribute can only be applied to fields of type `Span` or `MultiSpan`
210 #[derive(Diagnostic)]
211 #[diag(compiletest::example, code = "E0123")]
213 #[suggestion(compiletest::suggestion, code = "This is the suggested code")]
214 #[suggestion_short(compiletest::suggestion, code = "This is the suggested code")]
215 #[suggestion_hidden(compiletest::suggestion, code = "This is the suggested code")]
216 #[suggestion_verbose(compiletest::suggestion, code = "This is the suggested code")]
217 suggestion: (Span, Applicability),
220 #[derive(Diagnostic)]
221 #[diag(compiletest::example, code = "E0123")]
222 struct SuggestWithoutCode {
223 #[suggestion(compiletest::suggestion)]
224 //~^ ERROR suggestion without `code = "..."`
225 suggestion: (Span, Applicability),
228 #[derive(Diagnostic)]
229 #[diag(compiletest::example, code = "E0123")]
230 struct SuggestWithBadKey {
231 #[suggestion(nonsense = "bar")]
232 //~^ ERROR `#[suggestion(nonsense = ...)]` is not a valid attribute
233 //~| ERROR suggestion without `code = "..."`
234 suggestion: (Span, Applicability),
237 #[derive(Diagnostic)]
238 #[diag(compiletest::example, code = "E0123")]
239 struct SuggestWithShorthandMsg {
240 #[suggestion(msg = "bar")]
241 //~^ ERROR `#[suggestion(msg = ...)]` is not a valid attribute
242 //~| ERROR suggestion without `code = "..."`
243 suggestion: (Span, Applicability),
246 #[derive(Diagnostic)]
247 #[diag(compiletest::example, code = "E0123")]
248 struct SuggestWithoutMsg {
249 #[suggestion(code = "bar")]
250 suggestion: (Span, Applicability),
253 #[derive(Diagnostic)]
254 #[diag(compiletest::example, code = "E0123")]
255 struct SuggestWithTypesSwapped {
256 #[suggestion(compiletest::suggestion, code = "This is suggested code")]
257 suggestion: (Applicability, Span),
260 #[derive(Diagnostic)]
261 #[diag(compiletest::example, code = "E0123")]
262 struct SuggestWithWrongTypeApplicabilityOnly {
263 #[suggestion(compiletest::suggestion, code = "This is suggested code")]
264 //~^ ERROR wrong field type for suggestion
265 suggestion: Applicability,
268 #[derive(Diagnostic)]
269 #[diag(compiletest::example, code = "E0123")]
270 struct SuggestWithSpanOnly {
271 #[suggestion(compiletest::suggestion, code = "This is suggested code")]
275 #[derive(Diagnostic)]
276 #[diag(compiletest::example, code = "E0123")]
277 struct SuggestWithDuplicateSpanAndApplicability {
278 #[suggestion(compiletest::suggestion, code = "This is suggested code")]
279 suggestion: (Span, Span, Applicability),
280 //~^ ERROR specified multiple times
283 #[derive(Diagnostic)]
284 #[diag(compiletest::example, code = "E0123")]
285 struct SuggestWithDuplicateApplicabilityAndSpan {
286 #[suggestion(compiletest::suggestion, code = "This is suggested code")]
287 suggestion: (Applicability, Applicability, Span),
288 //~^ ERROR specified multiple times
291 #[derive(Diagnostic)]
292 #[diag(compiletest::example, code = "E0123")]
293 struct WrongKindOfAnnotation {
295 //~^ ERROR `#[label = ...]` is not a valid attribute
299 #[derive(Diagnostic)]
300 #[diag(compiletest::example, code = "E0123")]
301 struct OptionsInErrors {
302 #[label(compiletest::label)]
304 #[suggestion(compiletest::suggestion, code = "...")]
305 opt_sugg: Option<(Span, Applicability)>,
308 #[derive(Diagnostic)]
309 #[diag(compiletest::example, code = "E0456")]
310 struct MoveOutOfBorrowError<'tcx> {
314 #[label(compiletest::label)]
316 #[label(compiletest::label)]
318 #[suggestion(compiletest::suggestion, code = "{name}.clone()")]
319 opt_sugg: Option<(Span, Applicability)>,
322 #[derive(Diagnostic)]
323 #[diag(compiletest::example, code = "E0123")]
324 struct ErrorWithLifetime<'a> {
325 #[label(compiletest::label)]
330 #[derive(Diagnostic)]
331 #[diag(compiletest::example, code = "E0123")]
332 struct ErrorWithDefaultLabelAttr<'a> {
338 #[derive(Diagnostic)]
339 //~^ ERROR the trait bound `Hello: IntoDiagnosticArg` is not satisfied
340 #[diag(compiletest::example, code = "E0123")]
341 struct ArgFieldWithoutSkip {
347 #[derive(Diagnostic)]
348 #[diag(compiletest::example, code = "E0123")]
349 struct ArgFieldWithSkip {
352 // `Hello` does not implement `IntoDiagnosticArg` so this would result in an error if
353 // not for `#[skip_arg]`.
358 #[derive(Diagnostic)]
359 #[diag(compiletest::example, code = "E0123")]
360 struct ErrorWithSpannedNote {
365 #[derive(Diagnostic)]
366 #[diag(compiletest::example, code = "E0123")]
367 struct ErrorWithSpannedNoteCustom {
368 #[note(compiletest::note)]
372 #[derive(Diagnostic)]
373 #[diag(compiletest::example, code = "E0123")]
375 struct ErrorWithNote {
379 #[derive(Diagnostic)]
380 #[diag(compiletest::example, code = "E0123")]
381 #[note(compiletest::note)]
382 struct ErrorWithNoteCustom {
386 #[derive(Diagnostic)]
387 #[diag(compiletest::example, code = "E0123")]
388 struct ErrorWithSpannedHelp {
393 #[derive(Diagnostic)]
394 #[diag(compiletest::example, code = "E0123")]
395 struct ErrorWithSpannedHelpCustom {
396 #[help(compiletest::help)]
400 #[derive(Diagnostic)]
401 #[diag(compiletest::example, code = "E0123")]
403 struct ErrorWithHelp {
407 #[derive(Diagnostic)]
408 #[diag(compiletest::example, code = "E0123")]
409 #[help(compiletest::help)]
410 struct ErrorWithHelpCustom {
414 #[derive(Diagnostic)]
416 #[diag(compiletest::example, code = "E0123")]
417 struct ErrorWithHelpWrongOrder {
421 #[derive(Diagnostic)]
422 #[help(compiletest::help)]
423 #[diag(compiletest::example, code = "E0123")]
424 struct ErrorWithHelpCustomWrongOrder {
428 #[derive(Diagnostic)]
430 #[diag(compiletest::example, code = "E0123")]
431 struct ErrorWithNoteWrongOrder {
435 #[derive(Diagnostic)]
436 #[note(compiletest::note)]
437 #[diag(compiletest::example, code = "E0123")]
438 struct ErrorWithNoteCustomWrongOrder {
442 #[derive(Diagnostic)]
443 #[diag(compiletest::example, code = "E0123")]
444 struct ApplicabilityInBoth {
445 #[suggestion(compiletest::suggestion, code = "...", applicability = "maybe-incorrect")]
446 //~^ ERROR specified multiple times
447 suggestion: (Span, Applicability),
450 #[derive(Diagnostic)]
451 #[diag(compiletest::example, code = "E0123")]
452 struct InvalidApplicability {
453 #[suggestion(compiletest::suggestion, code = "...", applicability = "batman")]
454 //~^ ERROR invalid applicability
458 #[derive(Diagnostic)]
459 #[diag(compiletest::example, code = "E0123")]
460 struct ValidApplicability {
461 #[suggestion(compiletest::suggestion, code = "...", applicability = "maybe-incorrect")]
465 #[derive(Diagnostic)]
466 #[diag(compiletest::example, code = "E0123")]
467 struct NoApplicability {
468 #[suggestion(compiletest::suggestion, code = "...")]
472 #[derive(Subdiagnostic)]
473 #[note(parser::add_paren)]
476 #[derive(Diagnostic)]
477 #[diag(compiletest::example)]
478 struct Subdiagnostic {
483 #[derive(Diagnostic)]
484 #[diag(compiletest::example, code = "E0123")]
491 #[derive(Diagnostic)]
492 #[diag(compiletest::example, code = "E0123")]
498 #[help(compiletest::help)]
502 #[derive(Diagnostic)]
503 #[diag(compiletest::example, code = "E0123")]
504 struct OptUnitField {
509 #[help(compiletest::help)]
513 #[derive(Diagnostic)]
514 #[diag(compiletest::example, code = "E0123")]
515 struct LabelWithTrailingPath {
516 #[label(compiletest::label, foo)]
517 //~^ ERROR `#[label(foo)]` is not a valid attribute
521 #[derive(Diagnostic)]
522 #[diag(compiletest::example, code = "E0123")]
523 struct LabelWithTrailingNameValue {
524 #[label(compiletest::label, foo = "...")]
525 //~^ ERROR `#[label(foo = ...)]` is not a valid attribute
529 #[derive(Diagnostic)]
530 #[diag(compiletest::example, code = "E0123")]
531 struct LabelWithTrailingList {
532 #[label(compiletest::label, foo("..."))]
533 //~^ ERROR `#[label(foo(...))]` is not a valid attribute
537 #[derive(LintDiagnostic)]
538 #[diag(compiletest::example)]
542 #[derive(LintDiagnostic)]
543 #[diag(compiletest::example)]
544 struct PrimarySpanOnLint {
546 //~^ ERROR `#[primary_span]` is not a valid attribute
550 #[derive(Diagnostic)]
551 #[diag(compiletest::example, code = "E0123")]
552 struct ErrorWithMultiSpan {
557 #[derive(Diagnostic)]
558 #[diag(compiletest::example, code = "E0123")]
560 struct ErrorWithWarn {
564 #[derive(Diagnostic)]
565 #[error(compiletest::example, code = "E0123")]
566 //~^ ERROR `#[error(...)]` is not a valid attribute
567 //~| ERROR diagnostic slug not specified
568 //~| ERROR cannot find attribute `error` in this scope
569 struct ErrorAttribute {}
571 #[derive(Diagnostic)]
572 #[warn_(compiletest::example, code = "E0123")]
573 //~^ ERROR `#[warn_(...)]` is not a valid attribute
574 //~| ERROR diagnostic slug not specified
575 //~| ERROR cannot find attribute `warn_` in this scope
576 struct WarnAttribute {}
578 #[derive(Diagnostic)]
579 #[lint(compiletest::example, code = "E0123")]
580 //~^ ERROR `#[lint(...)]` is not a valid attribute
581 //~| ERROR diagnostic slug not specified
582 //~| ERROR cannot find attribute `lint` in this scope
583 struct LintAttributeOnSessionDiag {}
585 #[derive(LintDiagnostic)]
586 #[lint(compiletest::example, code = "E0123")]
587 //~^ ERROR `#[lint(...)]` is not a valid attribute
588 //~| ERROR `#[lint(...)]` is not a valid attribute
589 //~| ERROR diagnostic slug not specified
590 //~| ERROR cannot find attribute `lint` in this scope
591 struct LintAttributeOnLintDiag {}
593 #[derive(Diagnostic)]
594 #[diag(compiletest::example, code = "E0123")]
595 struct DuplicatedSuggestionCode {
596 #[suggestion(compiletest::suggestion, code = "...", code = ",,,")]
597 //~^ ERROR specified multiple times
601 #[derive(Diagnostic)]
602 #[diag(compiletest::example, code = "E0123")]
603 struct InvalidTypeInSuggestionTuple {
604 #[suggestion(compiletest::suggestion, code = "...")]
605 suggestion: (Span, usize),
606 //~^ ERROR wrong types for suggestion
609 #[derive(Diagnostic)]
610 #[diag(compiletest::example, code = "E0123")]
611 struct MissingApplicabilityInSuggestionTuple {
612 #[suggestion(compiletest::suggestion, code = "...")]
614 //~^ ERROR wrong types for suggestion
617 #[derive(Diagnostic)]
618 #[diag(compiletest::example, code = "E0123")]
619 struct MissingCodeInSuggestion {
620 #[suggestion(compiletest::suggestion)]
621 //~^ ERROR suggestion without `code = "..."`
625 #[derive(Diagnostic)]
626 #[diag(compiletest::example, code = "E0123")]
627 #[multipart_suggestion(compiletest::suggestion)]
628 //~^ ERROR `#[multipart_suggestion(...)]` is not a valid attribute
629 //~| ERROR cannot find attribute `multipart_suggestion` in this scope
630 #[multipart_suggestion()]
631 //~^ ERROR `#[multipart_suggestion(...)]` is not a valid attribute
632 //~| ERROR cannot find attribute `multipart_suggestion` in this scope
633 struct MultipartSuggestion {
634 #[multipart_suggestion(compiletest::suggestion)]
635 //~^ ERROR `#[multipart_suggestion(...)]` is not a valid attribute
636 //~| ERROR cannot find attribute `multipart_suggestion` in this scope
640 #[derive(Diagnostic)]
641 #[diag(compiletest::example, code = "E0123")]
642 #[suggestion(compiletest::suggestion, code = "...")]
643 //~^ ERROR `#[suggestion(...)]` is not a valid attribute
644 struct SuggestionOnStruct {
649 #[derive(Diagnostic)]
650 #[diag(compiletest::example, code = "E0123")]
652 //~^ ERROR `#[label]` is not a valid attribute
653 struct LabelOnStruct {
658 #[derive(Diagnostic)]
660 #[diag(compiletest::example)]
667 #[diag(compiletest::example)]
672 #[diag(compiletest::example)]
676 #[derive(Diagnostic)]
677 #[diag(compiletest::example, code = "E0123")]
678 struct RawIdentDiagnosticArg {