2 // Tests error conditions for specifying diagnostics using #[derive(SessionDiagnostic)]
4 // normalize-stderr-test "the following other types implement trait `IntoDiagnosticArg`:(?:.*\n){0,9}\s+and \d+ others" -> "normalized in stderr"
6 // The proc_macro2 crate handles spans differently when on beta/stable release rather than nightly,
7 // changing the output of this test. Since SessionDiagnostic 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::{SessionDiagnostic, LintDiagnostic, SessionSubdiagnostic};
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;
30 #[derive(SessionDiagnostic)]
31 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
34 #[derive(SessionDiagnostic)]
35 #[warning(typeck::ambiguous_lifetime_bound, code = "E0123")]
38 #[derive(SessionDiagnostic)]
39 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
40 //~^ ERROR `#[derive(SessionDiagnostic)]` can only be used on structs
41 enum SessionDiagnosticOnEnum {
46 #[derive(SessionDiagnostic)]
47 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
49 //~^ ERROR `#[error = ...]` is not a valid attribute
50 struct WrongStructAttrStyle {}
52 #[derive(SessionDiagnostic)]
53 #[nonsense(typeck::ambiguous_lifetime_bound, code = "E0123")]
54 //~^ ERROR `#[nonsense(...)]` is not a valid attribute
55 //~^^ ERROR diagnostic kind not specified
56 //~^^^ ERROR cannot find attribute `nonsense` in this scope
57 struct InvalidStructAttr {}
59 #[derive(SessionDiagnostic)]
61 //~^ ERROR `#[error("...")]` is not a valid attribute
62 //~^^ ERROR diagnostic slug not specified
63 struct InvalidLitNestedAttr {}
65 #[derive(SessionDiagnostic)]
66 #[error(nonsense, code = "E0123")]
67 //~^ ERROR cannot find value `nonsense` in module `rustc_errors::fluent`
68 struct InvalidNestedStructAttr {}
70 #[derive(SessionDiagnostic)]
71 #[error(nonsense("foo"), code = "E0123", slug = "foo")]
72 //~^ ERROR `#[error(nonsense(...))]` is not a valid attribute
73 //~^^ ERROR diagnostic slug not specified
74 struct InvalidNestedStructAttr1 {}
76 #[derive(SessionDiagnostic)]
77 #[error(nonsense = "...", code = "E0123", slug = "foo")]
78 //~^ ERROR `#[error(nonsense = ...)]` is not a valid attribute
79 //~^^ ERROR diagnostic slug not specified
80 struct InvalidNestedStructAttr2 {}
82 #[derive(SessionDiagnostic)]
83 #[error(nonsense = 4, code = "E0123", slug = "foo")]
84 //~^ ERROR `#[error(nonsense = ...)]` is not a valid attribute
85 //~^^ ERROR diagnostic slug not specified
86 struct InvalidNestedStructAttr3 {}
88 #[derive(SessionDiagnostic)]
89 #[error(typeck::ambiguous_lifetime_bound, code = "E0123", slug = "foo")]
90 //~^ ERROR `#[error(slug = ...)]` is not a valid attribute
91 struct InvalidNestedStructAttr4 {}
93 #[derive(SessionDiagnostic)]
94 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
95 struct WrongPlaceField {
97 //~^ ERROR `#[suggestion = ...]` is not a valid attribute
101 #[derive(SessionDiagnostic)]
102 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
103 #[error(typeck::ambiguous_lifetime_bound, code = "E0456")]
104 //~^ ERROR specified multiple times
105 //~^^ ERROR specified multiple times
106 //~^^^ ERROR specified multiple times
107 struct ErrorSpecifiedTwice {}
109 #[derive(SessionDiagnostic)]
110 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
111 #[warning(typeck::ambiguous_lifetime_bound, code = "E0293")]
112 //~^ ERROR specified multiple times
113 //~^^ ERROR specified multiple times
114 //~^^^ ERROR specified multiple times
115 struct WarnSpecifiedAfterError {}
117 #[derive(SessionDiagnostic)]
118 #[error(typeck::ambiguous_lifetime_bound, code = "E0456", code = "E0457")]
119 //~^ ERROR specified multiple times
120 struct CodeSpecifiedTwice {}
122 #[derive(SessionDiagnostic)]
123 #[error(typeck::ambiguous_lifetime_bound, typeck::ambiguous_lifetime_bound, code = "E0456")]
124 //~^ ERROR `#[error(typeck::ambiguous_lifetime_bound)]` is not a valid attribute
125 struct SlugSpecifiedTwice {}
127 #[derive(SessionDiagnostic)]
128 struct KindNotProvided {} //~ ERROR diagnostic kind not specified
130 #[derive(SessionDiagnostic)]
131 #[error(code = "E0456")]
132 //~^ ERROR diagnostic slug not specified
133 struct SlugNotProvided {}
135 #[derive(SessionDiagnostic)]
136 #[error(typeck::ambiguous_lifetime_bound)]
137 struct CodeNotProvided {}
139 #[derive(SessionDiagnostic)]
140 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
141 struct MessageWrongType {
143 //~^ ERROR `#[primary_span]` attribute can only be applied to fields of type `Span` or `MultiSpan`
147 #[derive(SessionDiagnostic)]
148 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
149 struct InvalidPathFieldAttr {
151 //~^ ERROR `#[nonsense]` is not a valid attribute
152 //~^^ ERROR cannot find attribute `nonsense` in this scope
156 #[derive(SessionDiagnostic)]
157 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
158 struct ErrorWithField {
160 #[label(typeck::label)]
164 #[derive(SessionDiagnostic)]
165 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
166 struct ErrorWithMessageAppliedToField {
167 #[label(typeck::label)]
168 //~^ ERROR the `#[label(...)]` attribute can only be applied to fields of type `Span` or `MultiSpan`
172 #[derive(SessionDiagnostic)]
173 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
174 struct ErrorWithNonexistentField {
175 #[suggestion(typeck::suggestion, code = "{name}")]
176 //~^ ERROR `name` doesn't refer to a field on this type
177 suggestion: (Span, Applicability),
180 #[derive(SessionDiagnostic)]
181 //~^ ERROR invalid format string: expected `'}'`
182 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
183 struct ErrorMissingClosingBrace {
184 #[suggestion(typeck::suggestion, code = "{name")]
185 suggestion: (Span, Applicability),
190 #[derive(SessionDiagnostic)]
191 //~^ ERROR invalid format string: unmatched `}`
192 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
193 struct ErrorMissingOpeningBrace {
194 #[suggestion(typeck::suggestion, code = "name}")]
195 suggestion: (Span, Applicability),
200 #[derive(SessionDiagnostic)]
201 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
203 #[label(typeck::label)]
207 #[derive(SessionDiagnostic)]
208 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
209 struct LabelOnNonSpan {
210 #[label(typeck::label)]
211 //~^ ERROR the `#[label(...)]` attribute can only be applied to fields of type `Span` or `MultiSpan`
215 #[derive(SessionDiagnostic)]
216 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
218 #[suggestion(typeck::suggestion, code = "This is the suggested code")]
219 #[suggestion_short(typeck::suggestion, code = "This is the suggested code")]
220 #[suggestion_hidden(typeck::suggestion, code = "This is the suggested code")]
221 #[suggestion_verbose(typeck::suggestion, code = "This is the suggested code")]
222 suggestion: (Span, Applicability),
225 #[derive(SessionDiagnostic)]
226 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
227 struct SuggestWithoutCode {
228 #[suggestion(typeck::suggestion)]
229 suggestion: (Span, Applicability),
232 #[derive(SessionDiagnostic)]
233 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
234 struct SuggestWithBadKey {
235 #[suggestion(nonsense = "bar")]
236 //~^ ERROR `#[suggestion(nonsense = ...)]` is not a valid attribute
237 suggestion: (Span, Applicability),
240 #[derive(SessionDiagnostic)]
241 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
242 struct SuggestWithShorthandMsg {
243 #[suggestion(msg = "bar")]
244 //~^ ERROR `#[suggestion(msg = ...)]` is not a valid attribute
245 suggestion: (Span, Applicability),
248 #[derive(SessionDiagnostic)]
249 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
250 struct SuggestWithoutMsg {
251 #[suggestion(code = "bar")]
252 suggestion: (Span, Applicability),
255 #[derive(SessionDiagnostic)]
256 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
257 struct SuggestWithTypesSwapped {
258 #[suggestion(typeck::suggestion, code = "This is suggested code")]
259 suggestion: (Applicability, Span),
262 #[derive(SessionDiagnostic)]
263 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
264 struct SuggestWithWrongTypeApplicabilityOnly {
265 #[suggestion(typeck::suggestion, code = "This is suggested code")]
266 //~^ ERROR wrong field type for suggestion
267 suggestion: Applicability,
270 #[derive(SessionDiagnostic)]
271 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
272 struct SuggestWithSpanOnly {
273 #[suggestion(typeck::suggestion, code = "This is suggested code")]
277 #[derive(SessionDiagnostic)]
278 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
279 struct SuggestWithDuplicateSpanAndApplicability {
280 #[suggestion(typeck::suggestion, code = "This is suggested code")]
281 //~^ ERROR type of field annotated with `#[suggestion(...)]` contains more than one `Span`
282 suggestion: (Span, Span, Applicability),
285 #[derive(SessionDiagnostic)]
286 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
287 struct SuggestWithDuplicateApplicabilityAndSpan {
288 #[suggestion(typeck::suggestion, code = "This is suggested code")]
289 //~^ ERROR type of field annotated with `#[suggestion(...)]` contains more than one
290 suggestion: (Applicability, Applicability, Span),
293 #[derive(SessionDiagnostic)]
294 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
295 struct WrongKindOfAnnotation {
297 //~^ ERROR `#[label = ...]` is not a valid attribute
301 #[derive(SessionDiagnostic)]
302 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
303 struct OptionsInErrors {
304 #[label(typeck::label)]
306 #[suggestion(typeck::suggestion)]
307 opt_sugg: Option<(Span, Applicability)>,
310 #[derive(SessionDiagnostic)]
311 #[error(typeck::ambiguous_lifetime_bound, code = "E0456")]
312 struct MoveOutOfBorrowError<'tcx> {
316 #[label(typeck::label)]
318 #[label(typeck::label)]
320 #[suggestion(typeck::suggestion, code = "{name}.clone()")]
321 opt_sugg: Option<(Span, Applicability)>,
324 #[derive(SessionDiagnostic)]
325 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
326 struct ErrorWithLifetime<'a> {
327 #[label(typeck::label)]
332 #[derive(SessionDiagnostic)]
333 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
334 struct ErrorWithDefaultLabelAttr<'a> {
340 #[derive(SessionDiagnostic)]
341 //~^ ERROR the trait bound `Hello: IntoDiagnosticArg` is not satisfied
342 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
343 struct ArgFieldWithoutSkip {
349 #[derive(SessionDiagnostic)]
350 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
351 struct ArgFieldWithSkip {
354 // `Hello` does not implement `IntoDiagnosticArg` so this would result in an error if
355 // not for `#[skip_arg]`.
360 #[derive(SessionDiagnostic)]
361 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
362 struct ErrorWithSpannedNote {
367 #[derive(SessionDiagnostic)]
368 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
369 struct ErrorWithSpannedNoteCustom {
370 #[note(typeck::note)]
374 #[derive(SessionDiagnostic)]
375 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
377 struct ErrorWithNote {
381 #[derive(SessionDiagnostic)]
382 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
383 #[note(typeck::note)]
384 struct ErrorWithNoteCustom {
388 #[derive(SessionDiagnostic)]
389 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
390 struct ErrorWithSpannedHelp {
395 #[derive(SessionDiagnostic)]
396 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
397 struct ErrorWithSpannedHelpCustom {
398 #[help(typeck::help)]
402 #[derive(SessionDiagnostic)]
403 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
405 struct ErrorWithHelp {
409 #[derive(SessionDiagnostic)]
410 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
411 #[help(typeck::help)]
412 struct ErrorWithHelpCustom {
416 #[derive(SessionDiagnostic)]
418 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
419 struct ErrorWithHelpWrongOrder {
423 #[derive(SessionDiagnostic)]
424 #[help(typeck::help)]
425 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
426 struct ErrorWithHelpCustomWrongOrder {
430 #[derive(SessionDiagnostic)]
432 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
433 struct ErrorWithNoteWrongOrder {
437 #[derive(SessionDiagnostic)]
438 #[note(typeck::note)]
439 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
440 struct ErrorWithNoteCustomWrongOrder {
444 #[derive(SessionDiagnostic)]
445 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
446 struct ApplicabilityInBoth {
447 #[suggestion(typeck::suggestion, code = "...", applicability = "maybe-incorrect")]
448 //~^ ERROR applicability cannot be set in both the field and attribute
449 suggestion: (Span, Applicability),
452 #[derive(SessionDiagnostic)]
453 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
454 struct InvalidApplicability {
455 #[suggestion(typeck::suggestion, code = "...", applicability = "batman")]
456 //~^ ERROR invalid applicability
460 #[derive(SessionDiagnostic)]
461 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
462 struct ValidApplicability {
463 #[suggestion(typeck::suggestion, code = "...", applicability = "maybe-incorrect")]
467 #[derive(SessionDiagnostic)]
468 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
469 struct NoApplicability {
470 #[suggestion(typeck::suggestion, code = "...")]
474 #[derive(SessionSubdiagnostic)]
475 #[note(parser::add_paren)]
478 #[derive(SessionDiagnostic)]
479 #[error(typeck::ambiguous_lifetime_bound)]
480 struct Subdiagnostic {
485 #[derive(SessionDiagnostic)]
486 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
493 #[derive(SessionDiagnostic)]
494 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
500 #[help(typeck::help)]
504 #[derive(SessionDiagnostic)]
505 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
506 struct OptUnitField {
511 #[help(typeck::help)]
515 #[derive(SessionDiagnostic)]
516 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
517 struct LabelWithTrailingPath {
518 #[label(typeck::label, foo)]
519 //~^ ERROR `#[label(...)]` is not a valid attribute
523 #[derive(SessionDiagnostic)]
524 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
525 struct LabelWithTrailingNameValue {
526 #[label(typeck::label, foo = "...")]
527 //~^ ERROR `#[label(...)]` is not a valid attribute
531 #[derive(SessionDiagnostic)]
532 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
533 struct LabelWithTrailingList {
534 #[label(typeck::label, foo("..."))]
535 //~^ ERROR `#[label(...)]` is not a valid attribute
539 #[derive(SessionDiagnostic)]
540 #[lint(typeck::ambiguous_lifetime_bound)]
541 //~^ ERROR only `#[error(..)]` and `#[warning(..)]` are supported
545 #[derive(LintDiagnostic)]
546 #[lint(typeck::ambiguous_lifetime_bound)]
550 #[derive(LintDiagnostic)]
551 #[error(typeck::ambiguous_lifetime_bound)]
552 //~^ ERROR only `#[lint(..)]` is supported
556 #[derive(SessionDiagnostic)]
557 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
558 struct ErrorWithMultiSpan {
563 #[derive(SessionDiagnostic)]
564 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
566 struct ErrorWithWarn {