]> git.proxmox.com Git - rustc.git/blob - src/test/ui-fulldeps/session-diagnostic/subdiagnostic-derive.rs
New upstream version 1.66.0+dfsg1
[rustc.git] / src / test / ui-fulldeps / session-diagnostic / subdiagnostic-derive.rs
1 // check-fail
2 // Tests error conditions for specifying subdiagnostics using #[derive(Subdiagnostic)]
3
4 // The proc_macro2 crate handles spans differently when on beta/stable release rather than nightly,
5 // changing the output of this test. Since Subdiagnostic is strictly internal to the compiler
6 // the test is just ignored on stable and beta:
7 // ignore-beta
8 // ignore-stable
9
10 #![feature(rustc_private)]
11 #![crate_type = "lib"]
12
13 extern crate rustc_errors;
14 extern crate rustc_session;
15 extern crate rustc_span;
16 extern crate rustc_macros;
17
18 use rustc_errors::Applicability;
19 use rustc_span::Span;
20 use rustc_macros::Subdiagnostic;
21
22 #[derive(Subdiagnostic)]
23 #[label(parser_add_paren)]
24 struct A {
25 #[primary_span]
26 span: Span,
27 var: String,
28 }
29
30 #[derive(Subdiagnostic)]
31 enum B {
32 #[label(parser_add_paren)]
33 A {
34 #[primary_span]
35 span: Span,
36 var: String,
37 },
38 #[label(parser_add_paren)]
39 B {
40 #[primary_span]
41 span: Span,
42 var: String,
43 }
44 }
45
46 #[derive(Subdiagnostic)]
47 #[label(parser_add_paren)]
48 //~^ ERROR label without `#[primary_span]` field
49 struct C {
50 var: String,
51 }
52
53 #[derive(Subdiagnostic)]
54 #[label]
55 //~^ ERROR diagnostic slug must be first argument
56 struct D {
57 #[primary_span]
58 span: Span,
59 var: String,
60 }
61
62 #[derive(Subdiagnostic)]
63 #[foo]
64 //~^ ERROR `#[foo]` is not a valid attribute
65 //~^^ ERROR cannot find attribute `foo` in this scope
66 struct E {
67 #[primary_span]
68 span: Span,
69 var: String,
70 }
71
72 #[derive(Subdiagnostic)]
73 #[label = "..."]
74 //~^ ERROR `#[label = ...]` is not a valid attribute
75 struct F {
76 #[primary_span]
77 span: Span,
78 var: String,
79 }
80
81 #[derive(Subdiagnostic)]
82 #[label(bug = "...")]
83 //~^ ERROR `#[label(bug = ...)]` is not a valid attribute
84 //~| ERROR diagnostic slug must be first argument
85 struct G {
86 #[primary_span]
87 span: Span,
88 var: String,
89 }
90
91 #[derive(Subdiagnostic)]
92 #[label("...")]
93 //~^ ERROR `#[label("...")]` is not a valid attribute
94 //~| ERROR diagnostic slug must be first argument
95 struct H {
96 #[primary_span]
97 span: Span,
98 var: String,
99 }
100
101 #[derive(Subdiagnostic)]
102 #[label(slug = 4)]
103 //~^ ERROR `#[label(slug = ...)]` is not a valid attribute
104 //~| ERROR diagnostic slug must be first argument
105 struct J {
106 #[primary_span]
107 span: Span,
108 var: String,
109 }
110
111 #[derive(Subdiagnostic)]
112 #[label(slug("..."))]
113 //~^ ERROR `#[label(slug(...))]` is not a valid attribute
114 //~| ERROR diagnostic slug must be first argument
115 struct K {
116 #[primary_span]
117 span: Span,
118 var: String,
119 }
120
121 #[derive(Subdiagnostic)]
122 #[label(slug)]
123 //~^ ERROR cannot find value `slug` in module `rustc_errors::fluent`
124 //~^^ NOTE not found in `rustc_errors::fluent`
125 struct L {
126 #[primary_span]
127 span: Span,
128 var: String,
129 }
130
131 #[derive(Subdiagnostic)]
132 #[label()]
133 //~^ ERROR diagnostic slug must be first argument of a `#[label(...)]` attribute
134 struct M {
135 #[primary_span]
136 span: Span,
137 var: String,
138 }
139
140 #[derive(Subdiagnostic)]
141 #[label(parser_add_paren, code = "...")]
142 //~^ ERROR `#[label(code = ...)]` is not a valid attribute
143 struct N {
144 #[primary_span]
145 span: Span,
146 var: String,
147 }
148
149 #[derive(Subdiagnostic)]
150 #[label(parser_add_paren, applicability = "machine-applicable")]
151 //~^ ERROR `#[label(applicability = ...)]` is not a valid attribute
152 struct O {
153 #[primary_span]
154 span: Span,
155 var: String,
156 }
157
158 #[derive(Subdiagnostic)]
159 #[foo]
160 //~^ ERROR cannot find attribute `foo` in this scope
161 //~^^ ERROR unsupported type attribute for subdiagnostic enum
162 enum P {
163 #[label(parser_add_paren)]
164 A {
165 #[primary_span]
166 span: Span,
167 var: String,
168 }
169 }
170
171 #[derive(Subdiagnostic)]
172 enum Q {
173 #[bar]
174 //~^ ERROR `#[bar]` is not a valid attribute
175 //~^^ ERROR cannot find attribute `bar` in this scope
176 A {
177 #[primary_span]
178 span: Span,
179 var: String,
180 }
181 }
182
183 #[derive(Subdiagnostic)]
184 enum R {
185 #[bar = "..."]
186 //~^ ERROR `#[bar = ...]` is not a valid attribute
187 //~^^ ERROR cannot find attribute `bar` in this scope
188 A {
189 #[primary_span]
190 span: Span,
191 var: String,
192 }
193 }
194
195 #[derive(Subdiagnostic)]
196 enum S {
197 #[bar = 4]
198 //~^ ERROR `#[bar = ...]` is not a valid attribute
199 //~^^ ERROR cannot find attribute `bar` in this scope
200 A {
201 #[primary_span]
202 span: Span,
203 var: String,
204 }
205 }
206
207 #[derive(Subdiagnostic)]
208 enum T {
209 #[bar("...")]
210 //~^ ERROR `#[bar(...)]` is not a valid attribute
211 //~^^ ERROR cannot find attribute `bar` in this scope
212 A {
213 #[primary_span]
214 span: Span,
215 var: String,
216 }
217 }
218
219 #[derive(Subdiagnostic)]
220 enum U {
221 #[label(code = "...")]
222 //~^ ERROR diagnostic slug must be first argument of a `#[label(...)]` attribute
223 //~| ERROR `#[label(code = ...)]` is not a valid attribute
224 A {
225 #[primary_span]
226 span: Span,
227 var: String,
228 }
229 }
230
231 #[derive(Subdiagnostic)]
232 enum V {
233 #[label(parser_add_paren)]
234 A {
235 #[primary_span]
236 span: Span,
237 var: String,
238 },
239 B {
240 #[primary_span]
241 span: Span,
242 var: String,
243 }
244 }
245
246 #[derive(Subdiagnostic)]
247 #[label(parser_add_paren)]
248 //~^ ERROR label without `#[primary_span]` field
249 struct W {
250 #[primary_span]
251 //~^ ERROR the `#[primary_span]` attribute can only be applied to fields of type `Span` or `MultiSpan`
252 span: String,
253 }
254
255 #[derive(Subdiagnostic)]
256 #[label(parser_add_paren)]
257 struct X {
258 #[primary_span]
259 span: Span,
260 #[applicability]
261 //~^ ERROR `#[applicability]` is only valid on suggestions
262 applicability: Applicability,
263 }
264
265 #[derive(Subdiagnostic)]
266 #[label(parser_add_paren)]
267 struct Y {
268 #[primary_span]
269 span: Span,
270 #[bar]
271 //~^ ERROR `#[bar]` is not a valid attribute
272 //~^^ ERROR cannot find attribute `bar` in this scope
273 bar: String,
274 }
275
276 #[derive(Subdiagnostic)]
277 #[label(parser_add_paren)]
278 struct Z {
279 #[primary_span]
280 span: Span,
281 #[bar = "..."]
282 //~^ ERROR `#[bar = ...]` is not a valid attribute
283 //~^^ ERROR cannot find attribute `bar` in this scope
284 bar: String,
285 }
286
287 #[derive(Subdiagnostic)]
288 #[label(parser_add_paren)]
289 struct AA {
290 #[primary_span]
291 span: Span,
292 #[bar("...")]
293 //~^ ERROR `#[bar(...)]` is not a valid attribute
294 //~^^ ERROR cannot find attribute `bar` in this scope
295 bar: String,
296 }
297
298 #[derive(Subdiagnostic)]
299 #[label(parser_add_paren)]
300 struct AB {
301 #[primary_span]
302 span: Span,
303 #[skip_arg]
304 z: Z
305 }
306
307 #[derive(Subdiagnostic)]
308 union AC {
309 //~^ ERROR unexpected unsupported untagged union
310 span: u32,
311 b: u64
312 }
313
314 #[derive(Subdiagnostic)]
315 #[label(parser_add_paren)]
316 #[label(parser_add_paren)]
317 struct AD {
318 #[primary_span]
319 span: Span,
320 }
321
322 #[derive(Subdiagnostic)]
323 #[label(parser_add_paren, parser_add_paren)]
324 //~^ ERROR `#[label(parser_add_paren)]` is not a valid attribute
325 struct AE {
326 #[primary_span]
327 span: Span,
328 }
329
330 #[derive(Subdiagnostic)]
331 #[label(parser_add_paren)]
332 struct AF {
333 #[primary_span]
334 //~^ NOTE previously specified here
335 span_a: Span,
336 #[primary_span]
337 //~^ ERROR specified multiple times
338 span_b: Span,
339 }
340
341 #[derive(Subdiagnostic)]
342 struct AG {
343 //~^ ERROR subdiagnostic kind not specified
344 #[primary_span]
345 span: Span,
346 }
347
348 #[derive(Subdiagnostic)]
349 #[suggestion(parser_add_paren, code = "...")]
350 struct AH {
351 #[primary_span]
352 span: Span,
353 #[applicability]
354 applicability: Applicability,
355 var: String,
356 }
357
358 #[derive(Subdiagnostic)]
359 enum AI {
360 #[suggestion(parser_add_paren, code = "...")]
361 A {
362 #[primary_span]
363 span: Span,
364 #[applicability]
365 applicability: Applicability,
366 var: String,
367 },
368 #[suggestion(parser_add_paren, code = "...")]
369 B {
370 #[primary_span]
371 span: Span,
372 #[applicability]
373 applicability: Applicability,
374 var: String,
375 }
376 }
377
378 #[derive(Subdiagnostic)]
379 #[suggestion(parser_add_paren, code = "...", code = "...")]
380 //~^ ERROR specified multiple times
381 //~^^ NOTE previously specified here
382 struct AJ {
383 #[primary_span]
384 span: Span,
385 #[applicability]
386 applicability: Applicability,
387 }
388
389 #[derive(Subdiagnostic)]
390 #[suggestion(parser_add_paren, code = "...")]
391 struct AK {
392 #[primary_span]
393 span: Span,
394 #[applicability]
395 //~^ NOTE previously specified here
396 applicability_a: Applicability,
397 #[applicability]
398 //~^ ERROR specified multiple times
399 applicability_b: Applicability,
400 }
401
402 #[derive(Subdiagnostic)]
403 #[suggestion(parser_add_paren, code = "...")]
404 struct AL {
405 #[primary_span]
406 span: Span,
407 #[applicability]
408 //~^ ERROR the `#[applicability]` attribute can only be applied to fields of type `Applicability`
409 applicability: Span,
410 }
411
412 #[derive(Subdiagnostic)]
413 #[suggestion(parser_add_paren, code = "...")]
414 struct AM {
415 #[primary_span]
416 span: Span,
417 }
418
419 #[derive(Subdiagnostic)]
420 #[suggestion(parser_add_paren)]
421 //~^ ERROR suggestion without `code = "..."`
422 struct AN {
423 #[primary_span]
424 span: Span,
425 #[applicability]
426 applicability: Applicability,
427 }
428
429 #[derive(Subdiagnostic)]
430 #[suggestion(parser_add_paren, code ="...", applicability = "foo")]
431 //~^ ERROR invalid applicability
432 struct AO {
433 #[primary_span]
434 span: Span,
435 }
436
437 #[derive(Subdiagnostic)]
438 #[help(parser_add_paren)]
439 struct AP {
440 var: String
441 }
442
443 #[derive(Subdiagnostic)]
444 #[note(parser_add_paren)]
445 struct AQ;
446
447 #[derive(Subdiagnostic)]
448 #[suggestion(parser_add_paren, code = "...")]
449 //~^ ERROR suggestion without `#[primary_span]` field
450 struct AR {
451 var: String,
452 }
453
454 #[derive(Subdiagnostic)]
455 #[suggestion(parser_add_paren, code ="...", applicability = "machine-applicable")]
456 struct AS {
457 #[primary_span]
458 span: Span,
459 }
460
461 #[derive(Subdiagnostic)]
462 #[label]
463 //~^ ERROR unsupported type attribute for subdiagnostic enum
464 enum AT {
465 #[label(parser_add_paren)]
466 A {
467 #[primary_span]
468 span: Span,
469 var: String,
470 }
471 }
472
473 #[derive(Subdiagnostic)]
474 #[suggestion(parser_add_paren, code ="{var}", applicability = "machine-applicable")]
475 struct AU {
476 #[primary_span]
477 span: Span,
478 var: String,
479 }
480
481 #[derive(Subdiagnostic)]
482 #[suggestion(parser_add_paren, code ="{var}", applicability = "machine-applicable")]
483 //~^ ERROR `var` doesn't refer to a field on this type
484 struct AV {
485 #[primary_span]
486 span: Span,
487 }
488
489 #[derive(Subdiagnostic)]
490 enum AW {
491 #[suggestion(parser_add_paren, code ="{var}", applicability = "machine-applicable")]
492 A {
493 #[primary_span]
494 span: Span,
495 var: String,
496 }
497 }
498
499 #[derive(Subdiagnostic)]
500 enum AX {
501 #[suggestion(parser_add_paren, code ="{var}", applicability = "machine-applicable")]
502 //~^ ERROR `var` doesn't refer to a field on this type
503 A {
504 #[primary_span]
505 span: Span,
506 }
507 }
508
509 #[derive(Subdiagnostic)]
510 #[warning(parser_add_paren)]
511 struct AY {}
512
513 #[derive(Subdiagnostic)]
514 #[warning(parser_add_paren)]
515 struct AZ {
516 #[primary_span]
517 span: Span,
518 }
519
520 #[derive(Subdiagnostic)]
521 #[suggestion(parser_add_paren, code = "...")]
522 //~^ ERROR suggestion without `#[primary_span]` field
523 struct BA {
524 #[suggestion_part]
525 //~^ ERROR `#[suggestion_part]` is not a valid attribute
526 span: Span,
527 #[suggestion_part(code = "...")]
528 //~^ ERROR `#[suggestion_part(...)]` is not a valid attribute
529 span2: Span,
530 #[applicability]
531 applicability: Applicability,
532 var: String,
533 }
534
535 #[derive(Subdiagnostic)]
536 #[multipart_suggestion(parser_add_paren, code = "...", applicability = "machine-applicable")]
537 //~^ ERROR multipart suggestion without any `#[suggestion_part(...)]` fields
538 //~| ERROR `#[multipart_suggestion(code = ...)]` is not a valid attribute
539 struct BBa {
540 var: String,
541 }
542
543 #[derive(Subdiagnostic)]
544 #[multipart_suggestion(parser_add_paren, applicability = "machine-applicable")]
545 struct BBb {
546 #[suggestion_part]
547 //~^ ERROR `#[suggestion_part(...)]` attribute without `code = "..."`
548 span1: Span,
549 }
550
551 #[derive(Subdiagnostic)]
552 #[multipart_suggestion(parser_add_paren, applicability = "machine-applicable")]
553 struct BBc {
554 #[suggestion_part()]
555 //~^ ERROR `#[suggestion_part(...)]` attribute without `code = "..."`
556 span1: Span,
557 }
558
559 #[derive(Subdiagnostic)]
560 #[multipart_suggestion(parser_add_paren)]
561 //~^ ERROR multipart suggestion without any `#[suggestion_part(...)]` fields
562 struct BC {
563 #[primary_span]
564 //~^ ERROR `#[primary_span]` is not a valid attribute
565 span: Span,
566 }
567
568 #[derive(Subdiagnostic)]
569 #[multipart_suggestion(parser_add_paren)]
570 struct BD {
571 #[suggestion_part]
572 //~^ ERROR `#[suggestion_part(...)]` attribute without `code = "..."`
573 span1: Span,
574 #[suggestion_part()]
575 //~^ ERROR `#[suggestion_part(...)]` attribute without `code = "..."`
576 span2: Span,
577 #[suggestion_part(foo = "bar")]
578 //~^ ERROR `#[suggestion_part(foo = ...)]` is not a valid attribute
579 span4: Span,
580 #[suggestion_part(code = "...")]
581 //~^ ERROR the `#[suggestion_part(...)]` attribute can only be applied to fields of type `Span` or `MultiSpan`
582 s1: String,
583 #[suggestion_part()]
584 //~^ ERROR the `#[suggestion_part(...)]` attribute can only be applied to fields of type `Span` or `MultiSpan`
585 s2: String,
586 }
587
588 #[derive(Subdiagnostic)]
589 #[multipart_suggestion(parser_add_paren, applicability = "machine-applicable")]
590 struct BE {
591 #[suggestion_part(code = "...", code = ",,,")]
592 //~^ ERROR specified multiple times
593 //~| NOTE previously specified here
594 span: Span,
595 }
596
597 #[derive(Subdiagnostic)]
598 #[multipart_suggestion(parser_add_paren, applicability = "machine-applicable")]
599 struct BF {
600 #[suggestion_part(code = "(")]
601 first: Span,
602 #[suggestion_part(code = ")")]
603 second: Span,
604 }
605
606 #[derive(Subdiagnostic)]
607 #[multipart_suggestion(parser_add_paren)]
608 struct BG {
609 #[applicability]
610 appl: Applicability,
611 #[suggestion_part(code = "(")]
612 first: Span,
613 #[suggestion_part(code = ")")]
614 second: Span,
615 }
616
617 #[derive(Subdiagnostic)]
618 #[multipart_suggestion(parser_add_paren, applicability = "machine-applicable")]
619 struct BH {
620 #[applicability]
621 //~^ ERROR `#[applicability]` has no effect
622 appl: Applicability,
623 #[suggestion_part(code = "(")]
624 first: Span,
625 #[suggestion_part(code = ")")]
626 second: Span,
627 }
628
629 #[derive(Subdiagnostic)]
630 #[multipart_suggestion(parser_add_paren, applicability = "machine-applicable")]
631 struct BI {
632 #[suggestion_part(code = "")]
633 spans: Vec<Span>,
634 }
635
636 #[derive(Subdiagnostic)]
637 #[label(parser_add_paren)]
638 struct BJ {
639 #[primary_span]
640 span: Span,
641 r#type: String,
642 }
643
644 /// with a doc comment on the type..
645 #[derive(Subdiagnostic)]
646 #[label(parser_add_paren)]
647 struct BK {
648 /// ..and the field
649 #[primary_span]
650 span: Span,
651 }
652
653 /// with a doc comment on the type..
654 #[derive(Subdiagnostic)]
655 enum BL {
656 /// ..and the variant..
657 #[label(parser_add_paren)]
658 Foo {
659 /// ..and the field
660 #[primary_span]
661 span: Span,
662 }
663 }
664
665 #[derive(Subdiagnostic)]
666 #[multipart_suggestion(parser_add_paren)]
667 struct BM {
668 #[suggestion_part(code("foo"))]
669 //~^ ERROR expected exactly one string literal for `code = ...`
670 span: Span,
671 r#type: String,
672 }
673
674 #[derive(Subdiagnostic)]
675 #[multipart_suggestion(parser_add_paren)]
676 struct BN {
677 #[suggestion_part(code("foo", "bar"))]
678 //~^ ERROR expected exactly one string literal for `code = ...`
679 span: Span,
680 r#type: String,
681 }
682
683 #[derive(Subdiagnostic)]
684 #[multipart_suggestion(parser_add_paren)]
685 struct BO {
686 #[suggestion_part(code(3))]
687 //~^ ERROR expected exactly one string literal for `code = ...`
688 span: Span,
689 r#type: String,
690 }
691
692 #[derive(Subdiagnostic)]
693 #[multipart_suggestion(parser_add_paren)]
694 struct BP {
695 #[suggestion_part(code())]
696 //~^ ERROR expected exactly one string literal for `code = ...`
697 span: Span,
698 r#type: String,
699 }
700
701 #[derive(Subdiagnostic)]
702 #[multipart_suggestion(parser_add_paren)]
703 struct BQ {
704 #[suggestion_part(code = 3)]
705 //~^ ERROR `code = "..."`/`code(...)` must contain only string literals
706 span: Span,
707 r#type: String,
708 }