]> git.lizzy.rs Git - rust.git/blob - src/test/ui-fulldeps/session-diagnostic/diagnostic-derive.rs
Rollup merge of #99323 - GuillaumeGomez:fix-gui-flaky, r=Dylan-DPC
[rust.git] / src / test / ui-fulldeps / session-diagnostic / diagnostic-derive.rs
1 // check-fail
2 // Tests error conditions for specifying diagnostics using #[derive(SessionDiagnostic)]
3
4 // normalize-stderr-test "the following other types implement trait `IntoDiagnosticArg`:(?:.*\n){0,9}\s+and \d+ others" -> "normalized in stderr"
5
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:
9 // ignore-beta
10 // ignore-stable
11
12 #![feature(rustc_private)]
13 #![crate_type = "lib"]
14
15 extern crate rustc_span;
16 use rustc_span::symbol::Ident;
17 use rustc_span::Span;
18
19 extern crate rustc_macros;
20 use rustc_macros::{SessionDiagnostic, LintDiagnostic, SessionSubdiagnostic};
21
22 extern crate rustc_middle;
23 use rustc_middle::ty::Ty;
24
25 extern crate rustc_errors;
26 use rustc_errors::{Applicability, MultiSpan};
27
28 extern crate rustc_session;
29
30 #[derive(SessionDiagnostic)]
31 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
32 struct Hello {}
33
34 #[derive(SessionDiagnostic)]
35 #[warning(typeck::ambiguous_lifetime_bound, code = "E0123")]
36 struct HelloWarn {}
37
38 #[derive(SessionDiagnostic)]
39 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
40 //~^ ERROR `#[derive(SessionDiagnostic)]` can only be used on structs
41 enum SessionDiagnosticOnEnum {
42     Foo,
43     Bar,
44 }
45
46 #[derive(SessionDiagnostic)]
47 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
48 #[error = "E0123"]
49 //~^ ERROR `#[error = ...]` is not a valid attribute
50 struct WrongStructAttrStyle {}
51
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 {}
58
59 #[derive(SessionDiagnostic)]
60 #[error("E0123")]
61 //~^ ERROR `#[error("...")]` is not a valid attribute
62 //~^^ ERROR diagnostic slug not specified
63 struct InvalidLitNestedAttr {}
64
65 #[derive(SessionDiagnostic)]
66 #[error(nonsense, code = "E0123")]
67 //~^ ERROR cannot find value `nonsense` in module `rustc_errors::fluent`
68 struct InvalidNestedStructAttr {}
69
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 {}
75
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 {}
81
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 {}
87
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 {}
92
93 #[derive(SessionDiagnostic)]
94 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
95 struct WrongPlaceField {
96     #[suggestion = "bar"]
97     //~^ ERROR `#[suggestion = ...]` is not a valid attribute
98     sp: Span,
99 }
100
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 {}
108
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 {}
116
117 #[derive(SessionDiagnostic)]
118 #[error(typeck::ambiguous_lifetime_bound, code = "E0456", code = "E0457")]
119 //~^ ERROR specified multiple times
120 struct CodeSpecifiedTwice {}
121
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 {}
126
127 #[derive(SessionDiagnostic)]
128 struct KindNotProvided {} //~ ERROR diagnostic kind not specified
129
130 #[derive(SessionDiagnostic)]
131 #[error(code = "E0456")]
132 //~^ ERROR diagnostic slug not specified
133 struct SlugNotProvided {}
134
135 #[derive(SessionDiagnostic)]
136 #[error(typeck::ambiguous_lifetime_bound)]
137 struct CodeNotProvided {}
138
139 #[derive(SessionDiagnostic)]
140 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
141 struct MessageWrongType {
142     #[primary_span]
143     //~^ ERROR `#[primary_span]` attribute can only be applied to fields of type `Span` or `MultiSpan`
144     foo: String,
145 }
146
147 #[derive(SessionDiagnostic)]
148 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
149 struct InvalidPathFieldAttr {
150     #[nonsense]
151     //~^ ERROR `#[nonsense]` is not a valid attribute
152     //~^^ ERROR cannot find attribute `nonsense` in this scope
153     foo: String,
154 }
155
156 #[derive(SessionDiagnostic)]
157 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
158 struct ErrorWithField {
159     name: String,
160     #[label(typeck::label)]
161     span: Span,
162 }
163
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`
169     name: String,
170 }
171
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),
178 }
179
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),
186     name: String,
187     val: usize,
188 }
189
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),
196     name: String,
197     val: usize,
198 }
199
200 #[derive(SessionDiagnostic)]
201 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
202 struct LabelOnSpan {
203     #[label(typeck::label)]
204     sp: Span,
205 }
206
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`
212     id: u32,
213 }
214
215 #[derive(SessionDiagnostic)]
216 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
217 struct Suggest {
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),
223 }
224
225 #[derive(SessionDiagnostic)]
226 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
227 struct SuggestWithoutCode {
228     #[suggestion(typeck::suggestion)]
229     suggestion: (Span, Applicability),
230 }
231
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),
238 }
239
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),
246 }
247
248 #[derive(SessionDiagnostic)]
249 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
250 struct SuggestWithoutMsg {
251     #[suggestion(code = "bar")]
252     suggestion: (Span, Applicability),
253 }
254
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),
260 }
261
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,
268 }
269
270 #[derive(SessionDiagnostic)]
271 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
272 struct SuggestWithSpanOnly {
273     #[suggestion(typeck::suggestion, code = "This is suggested code")]
274     suggestion: Span,
275 }
276
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),
283 }
284
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),
291 }
292
293 #[derive(SessionDiagnostic)]
294 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
295 struct WrongKindOfAnnotation {
296     #[label = "bar"]
297     //~^ ERROR `#[label = ...]` is not a valid attribute
298     z: Span,
299 }
300
301 #[derive(SessionDiagnostic)]
302 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
303 struct OptionsInErrors {
304     #[label(typeck::label)]
305     label: Option<Span>,
306     #[suggestion(typeck::suggestion)]
307     opt_sugg: Option<(Span, Applicability)>,
308 }
309
310 #[derive(SessionDiagnostic)]
311 #[error(typeck::ambiguous_lifetime_bound, code = "E0456")]
312 struct MoveOutOfBorrowError<'tcx> {
313     name: Ident,
314     ty: Ty<'tcx>,
315     #[primary_span]
316     #[label(typeck::label)]
317     span: Span,
318     #[label(typeck::label)]
319     other_span: Span,
320     #[suggestion(typeck::suggestion, code = "{name}.clone()")]
321     opt_sugg: Option<(Span, Applicability)>,
322 }
323
324 #[derive(SessionDiagnostic)]
325 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
326 struct ErrorWithLifetime<'a> {
327     #[label(typeck::label)]
328     span: Span,
329     name: &'a str,
330 }
331
332 #[derive(SessionDiagnostic)]
333 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
334 struct ErrorWithDefaultLabelAttr<'a> {
335     #[label]
336     span: Span,
337     name: &'a str,
338 }
339
340 #[derive(SessionDiagnostic)]
341 //~^ ERROR the trait bound `Hello: IntoDiagnosticArg` is not satisfied
342 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
343 struct ArgFieldWithoutSkip {
344     #[primary_span]
345     span: Span,
346     other: Hello,
347 }
348
349 #[derive(SessionDiagnostic)]
350 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
351 struct ArgFieldWithSkip {
352     #[primary_span]
353     span: Span,
354     // `Hello` does not implement `IntoDiagnosticArg` so this would result in an error if
355     // not for `#[skip_arg]`.
356     #[skip_arg]
357     other: Hello,
358 }
359
360 #[derive(SessionDiagnostic)]
361 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
362 struct ErrorWithSpannedNote {
363     #[note]
364     span: Span,
365 }
366
367 #[derive(SessionDiagnostic)]
368 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
369 struct ErrorWithSpannedNoteCustom {
370     #[note(typeck::note)]
371     span: Span,
372 }
373
374 #[derive(SessionDiagnostic)]
375 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
376 #[note]
377 struct ErrorWithNote {
378     val: String,
379 }
380
381 #[derive(SessionDiagnostic)]
382 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
383 #[note(typeck::note)]
384 struct ErrorWithNoteCustom {
385     val: String,
386 }
387
388 #[derive(SessionDiagnostic)]
389 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
390 struct ErrorWithSpannedHelp {
391     #[help]
392     span: Span,
393 }
394
395 #[derive(SessionDiagnostic)]
396 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
397 struct ErrorWithSpannedHelpCustom {
398     #[help(typeck::help)]
399     span: Span,
400 }
401
402 #[derive(SessionDiagnostic)]
403 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
404 #[help]
405 struct ErrorWithHelp {
406     val: String,
407 }
408
409 #[derive(SessionDiagnostic)]
410 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
411 #[help(typeck::help)]
412 struct ErrorWithHelpCustom {
413     val: String,
414 }
415
416 #[derive(SessionDiagnostic)]
417 #[help]
418 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
419 struct ErrorWithHelpWrongOrder {
420     val: String,
421 }
422
423 #[derive(SessionDiagnostic)]
424 #[help(typeck::help)]
425 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
426 struct ErrorWithHelpCustomWrongOrder {
427     val: String,
428 }
429
430 #[derive(SessionDiagnostic)]
431 #[note]
432 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
433 struct ErrorWithNoteWrongOrder {
434     val: String,
435 }
436
437 #[derive(SessionDiagnostic)]
438 #[note(typeck::note)]
439 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
440 struct ErrorWithNoteCustomWrongOrder {
441     val: String,
442 }
443
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),
450 }
451
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
457     suggestion: Span,
458 }
459
460 #[derive(SessionDiagnostic)]
461 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
462 struct ValidApplicability {
463     #[suggestion(typeck::suggestion, code = "...", applicability = "maybe-incorrect")]
464     suggestion: Span,
465 }
466
467 #[derive(SessionDiagnostic)]
468 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
469 struct NoApplicability {
470     #[suggestion(typeck::suggestion, code = "...")]
471     suggestion: Span,
472 }
473
474 #[derive(SessionSubdiagnostic)]
475 #[note(parser::add_paren)]
476 struct Note;
477
478 #[derive(SessionDiagnostic)]
479 #[error(typeck::ambiguous_lifetime_bound)]
480 struct Subdiagnostic {
481     #[subdiagnostic]
482     note: Note,
483 }
484
485 #[derive(SessionDiagnostic)]
486 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
487 struct VecField {
488     #[primary_span]
489     #[label]
490     spans: Vec<Span>,
491 }
492
493 #[derive(SessionDiagnostic)]
494 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
495 struct UnitField {
496     #[primary_span]
497     spans: Span,
498     #[help]
499     foo: (),
500     #[help(typeck::help)]
501     bar: (),
502 }
503
504 #[derive(SessionDiagnostic)]
505 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
506 struct OptUnitField {
507     #[primary_span]
508     spans: Span,
509     #[help]
510     foo: Option<()>,
511     #[help(typeck::help)]
512     bar: Option<()>,
513 }
514
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
520     span: Span,
521 }
522
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
528     span: Span,
529 }
530
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
536     span: Span,
537 }
538
539 #[derive(SessionDiagnostic)]
540 #[lint(typeck::ambiguous_lifetime_bound)]
541 //~^ ERROR only `#[error(..)]` and `#[warning(..)]` are supported
542 struct LintsBad {
543 }
544
545 #[derive(LintDiagnostic)]
546 #[lint(typeck::ambiguous_lifetime_bound)]
547 struct LintsGood {
548 }
549
550 #[derive(LintDiagnostic)]
551 #[error(typeck::ambiguous_lifetime_bound)]
552 //~^ ERROR only `#[lint(..)]` is supported
553 struct ErrorsBad {
554 }
555
556 #[derive(SessionDiagnostic)]
557 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
558 struct ErrorWithMultiSpan {
559     #[primary_span]
560     span: MultiSpan,
561 }
562
563 #[derive(SessionDiagnostic)]
564 #[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
565 #[warn_]
566 struct ErrorWithWarn {
567     val: String,
568 }