summaryrefslogtreecommitdiffstats
path: root/src/test/ui-fulldeps/session-diagnostic/diagnostic-derive.rs
blob: 0a210cbdc94307677afd1428e626190bb68503eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
// check-fail
// Tests error conditions for specifying diagnostics using #[derive(SessionDiagnostic)]

// normalize-stderr-test "the following other types implement trait `IntoDiagnosticArg`:(?:.*\n){0,9}\s+and \d+ others" -> "normalized in stderr"

// The proc_macro2 crate handles spans differently when on beta/stable release rather than nightly,
// changing the output of this test. Since SessionDiagnostic is strictly internal to the compiler
// the test is just ignored on stable and beta:
// ignore-beta
// ignore-stable

#![feature(rustc_private)]
#![crate_type = "lib"]

extern crate rustc_span;
use rustc_span::symbol::Ident;
use rustc_span::Span;

extern crate rustc_macros;
use rustc_macros::{SessionDiagnostic, LintDiagnostic, SessionSubdiagnostic};

extern crate rustc_middle;
use rustc_middle::ty::Ty;

extern crate rustc_errors;
use rustc_errors::{Applicability, MultiSpan};

extern crate rustc_session;

#[derive(SessionDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
struct Hello {}

#[derive(SessionDiagnostic)]
#[warning(typeck::ambiguous_lifetime_bound, code = "E0123")]
struct HelloWarn {}

#[derive(SessionDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
//~^ ERROR `#[derive(SessionDiagnostic)]` can only be used on structs
enum SessionDiagnosticOnEnum {
    Foo,
    Bar,
}

#[derive(SessionDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
#[error = "E0123"]
//~^ ERROR `#[error = ...]` is not a valid attribute
struct WrongStructAttrStyle {}

#[derive(SessionDiagnostic)]
#[nonsense(typeck::ambiguous_lifetime_bound, code = "E0123")]
//~^ ERROR `#[nonsense(...)]` is not a valid attribute
//~^^ ERROR diagnostic kind not specified
//~^^^ ERROR cannot find attribute `nonsense` in this scope
struct InvalidStructAttr {}

#[derive(SessionDiagnostic)]
#[error("E0123")]
//~^ ERROR `#[error("...")]` is not a valid attribute
//~^^ ERROR diagnostic slug not specified
struct InvalidLitNestedAttr {}

#[derive(SessionDiagnostic)]
#[error(nonsense, code = "E0123")]
//~^ ERROR cannot find value `nonsense` in module `rustc_errors::fluent`
struct InvalidNestedStructAttr {}

#[derive(SessionDiagnostic)]
#[error(nonsense("foo"), code = "E0123", slug = "foo")]
//~^ ERROR `#[error(nonsense(...))]` is not a valid attribute
//~^^ ERROR diagnostic slug not specified
struct InvalidNestedStructAttr1 {}

#[derive(SessionDiagnostic)]
#[error(nonsense = "...", code = "E0123", slug = "foo")]
//~^ ERROR `#[error(nonsense = ...)]` is not a valid attribute
//~^^ ERROR diagnostic slug not specified
struct InvalidNestedStructAttr2 {}

#[derive(SessionDiagnostic)]
#[error(nonsense = 4, code = "E0123", slug = "foo")]
//~^ ERROR `#[error(nonsense = ...)]` is not a valid attribute
//~^^ ERROR diagnostic slug not specified
struct InvalidNestedStructAttr3 {}

#[derive(SessionDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound, code = "E0123", slug = "foo")]
//~^ ERROR `#[error(slug = ...)]` is not a valid attribute
struct InvalidNestedStructAttr4 {}

#[derive(SessionDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
struct WrongPlaceField {
    #[suggestion = "bar"]
    //~^ ERROR `#[suggestion = ...]` is not a valid attribute
    sp: Span,
}

#[derive(SessionDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
#[error(typeck::ambiguous_lifetime_bound, code = "E0456")]
//~^ ERROR specified multiple times
//~^^ ERROR specified multiple times
//~^^^ ERROR specified multiple times
struct ErrorSpecifiedTwice {}

#[derive(SessionDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
#[warning(typeck::ambiguous_lifetime_bound, code = "E0293")]
//~^ ERROR specified multiple times
//~^^ ERROR specified multiple times
//~^^^ ERROR specified multiple times
struct WarnSpecifiedAfterError {}

#[derive(SessionDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound, code = "E0456", code = "E0457")]
//~^ ERROR specified multiple times
struct CodeSpecifiedTwice {}

#[derive(SessionDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound, typeck::ambiguous_lifetime_bound, code = "E0456")]
//~^ ERROR `#[error(typeck::ambiguous_lifetime_bound)]` is not a valid attribute
struct SlugSpecifiedTwice {}

#[derive(SessionDiagnostic)]
struct KindNotProvided {} //~ ERROR diagnostic kind not specified

#[derive(SessionDiagnostic)]
#[error(code = "E0456")]
//~^ ERROR diagnostic slug not specified
struct SlugNotProvided {}

#[derive(SessionDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound)]
struct CodeNotProvided {}

#[derive(SessionDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
struct MessageWrongType {
    #[primary_span]
    //~^ ERROR `#[primary_span]` attribute can only be applied to fields of type `Span` or `MultiSpan`
    foo: String,
}

#[derive(SessionDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
struct InvalidPathFieldAttr {
    #[nonsense]
    //~^ ERROR `#[nonsense]` is not a valid attribute
    //~^^ ERROR cannot find attribute `nonsense` in this scope
    foo: String,
}

#[derive(SessionDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
struct ErrorWithField {
    name: String,
    #[label(typeck::label)]
    span: Span,
}

#[derive(SessionDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
struct ErrorWithMessageAppliedToField {
    #[label(typeck::label)]
    //~^ ERROR the `#[label(...)]` attribute can only be applied to fields of type `Span` or `MultiSpan`
    name: String,
}

#[derive(SessionDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
struct ErrorWithNonexistentField {
    #[suggestion(typeck::suggestion, code = "{name}")]
    //~^ ERROR `name` doesn't refer to a field on this type
    suggestion: (Span, Applicability),
}

#[derive(SessionDiagnostic)]
//~^ ERROR invalid format string: expected `'}'`
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
struct ErrorMissingClosingBrace {
    #[suggestion(typeck::suggestion, code = "{name")]
    suggestion: (Span, Applicability),
    name: String,
    val: usize,
}

#[derive(SessionDiagnostic)]
//~^ ERROR invalid format string: unmatched `}`
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
struct ErrorMissingOpeningBrace {
    #[suggestion(typeck::suggestion, code = "name}")]
    suggestion: (Span, Applicability),
    name: String,
    val: usize,
}

#[derive(SessionDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
struct LabelOnSpan {
    #[label(typeck::label)]
    sp: Span,
}

#[derive(SessionDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
struct LabelOnNonSpan {
    #[label(typeck::label)]
    //~^ ERROR the `#[label(...)]` attribute can only be applied to fields of type `Span` or `MultiSpan`
    id: u32,
}

#[derive(SessionDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
struct Suggest {
    #[suggestion(typeck::suggestion, code = "This is the suggested code")]
    #[suggestion_short(typeck::suggestion, code = "This is the suggested code")]
    #[suggestion_hidden(typeck::suggestion, code = "This is the suggested code")]
    #[suggestion_verbose(typeck::suggestion, code = "This is the suggested code")]
    suggestion: (Span, Applicability),
}

#[derive(SessionDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
struct SuggestWithoutCode {
    #[suggestion(typeck::suggestion)]
    suggestion: (Span, Applicability),
}

#[derive(SessionDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
struct SuggestWithBadKey {
    #[suggestion(nonsense = "bar")]
    //~^ ERROR `#[suggestion(nonsense = ...)]` is not a valid attribute
    suggestion: (Span, Applicability),
}

#[derive(SessionDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
struct SuggestWithShorthandMsg {
    #[suggestion(msg = "bar")]
    //~^ ERROR `#[suggestion(msg = ...)]` is not a valid attribute
    suggestion: (Span, Applicability),
}

#[derive(SessionDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
struct SuggestWithoutMsg {
    #[suggestion(code = "bar")]
    suggestion: (Span, Applicability),
}

#[derive(SessionDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
struct SuggestWithTypesSwapped {
    #[suggestion(typeck::suggestion, code = "This is suggested code")]
    suggestion: (Applicability, Span),
}

#[derive(SessionDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
struct SuggestWithWrongTypeApplicabilityOnly {
    #[suggestion(typeck::suggestion, code = "This is suggested code")]
    //~^ ERROR wrong field type for suggestion
    suggestion: Applicability,
}

#[derive(SessionDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
struct SuggestWithSpanOnly {
    #[suggestion(typeck::suggestion, code = "This is suggested code")]
    suggestion: Span,
}

#[derive(SessionDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
struct SuggestWithDuplicateSpanAndApplicability {
    #[suggestion(typeck::suggestion, code = "This is suggested code")]
    //~^ ERROR type of field annotated with `#[suggestion(...)]` contains more than one `Span`
    suggestion: (Span, Span, Applicability),
}

#[derive(SessionDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
struct SuggestWithDuplicateApplicabilityAndSpan {
    #[suggestion(typeck::suggestion, code = "This is suggested code")]
    //~^ ERROR type of field annotated with `#[suggestion(...)]` contains more than one
    suggestion: (Applicability, Applicability, Span),
}

#[derive(SessionDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
struct WrongKindOfAnnotation {
    #[label = "bar"]
    //~^ ERROR `#[label = ...]` is not a valid attribute
    z: Span,
}

#[derive(SessionDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
struct OptionsInErrors {
    #[label(typeck::label)]
    label: Option<Span>,
    #[suggestion(typeck::suggestion)]
    opt_sugg: Option<(Span, Applicability)>,
}

#[derive(SessionDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound, code = "E0456")]
struct MoveOutOfBorrowError<'tcx> {
    name: Ident,
    ty: Ty<'tcx>,
    #[primary_span]
    #[label(typeck::label)]
    span: Span,
    #[label(typeck::label)]
    other_span: Span,
    #[suggestion(typeck::suggestion, code = "{name}.clone()")]
    opt_sugg: Option<(Span, Applicability)>,
}

#[derive(SessionDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
struct ErrorWithLifetime<'a> {
    #[label(typeck::label)]
    span: Span,
    name: &'a str,
}

#[derive(SessionDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
struct ErrorWithDefaultLabelAttr<'a> {
    #[label]
    span: Span,
    name: &'a str,
}

#[derive(SessionDiagnostic)]
//~^ ERROR the trait bound `Hello: IntoDiagnosticArg` is not satisfied
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
struct ArgFieldWithoutSkip {
    #[primary_span]
    span: Span,
    other: Hello,
}

#[derive(SessionDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
struct ArgFieldWithSkip {
    #[primary_span]
    span: Span,
    // `Hello` does not implement `IntoDiagnosticArg` so this would result in an error if
    // not for `#[skip_arg]`.
    #[skip_arg]
    other: Hello,
}

#[derive(SessionDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
struct ErrorWithSpannedNote {
    #[note]
    span: Span,
}

#[derive(SessionDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
struct ErrorWithSpannedNoteCustom {
    #[note(typeck::note)]
    span: Span,
}

#[derive(SessionDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
#[note]
struct ErrorWithNote {
    val: String,
}

#[derive(SessionDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
#[note(typeck::note)]
struct ErrorWithNoteCustom {
    val: String,
}

#[derive(SessionDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
struct ErrorWithSpannedHelp {
    #[help]
    span: Span,
}

#[derive(SessionDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
struct ErrorWithSpannedHelpCustom {
    #[help(typeck::help)]
    span: Span,
}

#[derive(SessionDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
#[help]
struct ErrorWithHelp {
    val: String,
}

#[derive(SessionDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
#[help(typeck::help)]
struct ErrorWithHelpCustom {
    val: String,
}

#[derive(SessionDiagnostic)]
#[help]
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
struct ErrorWithHelpWrongOrder {
    val: String,
}

#[derive(SessionDiagnostic)]
#[help(typeck::help)]
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
struct ErrorWithHelpCustomWrongOrder {
    val: String,
}

#[derive(SessionDiagnostic)]
#[note]
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
struct ErrorWithNoteWrongOrder {
    val: String,
}

#[derive(SessionDiagnostic)]
#[note(typeck::note)]
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
struct ErrorWithNoteCustomWrongOrder {
    val: String,
}

#[derive(SessionDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
struct ApplicabilityInBoth {
    #[suggestion(typeck::suggestion, code = "...", applicability = "maybe-incorrect")]
    //~^ ERROR applicability cannot be set in both the field and attribute
    suggestion: (Span, Applicability),
}

#[derive(SessionDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
struct InvalidApplicability {
    #[suggestion(typeck::suggestion, code = "...", applicability = "batman")]
    //~^ ERROR invalid applicability
    suggestion: Span,
}

#[derive(SessionDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
struct ValidApplicability {
    #[suggestion(typeck::suggestion, code = "...", applicability = "maybe-incorrect")]
    suggestion: Span,
}

#[derive(SessionDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
struct NoApplicability {
    #[suggestion(typeck::suggestion, code = "...")]
    suggestion: Span,
}

#[derive(SessionSubdiagnostic)]
#[note(parser::add_paren)]
struct Note;

#[derive(SessionDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound)]
struct Subdiagnostic {
    #[subdiagnostic]
    note: Note,
}

#[derive(SessionDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
struct VecField {
    #[primary_span]
    #[label]
    spans: Vec<Span>,
}

#[derive(SessionDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
struct UnitField {
    #[primary_span]
    spans: Span,
    #[help]
    foo: (),
    #[help(typeck::help)]
    bar: (),
}

#[derive(SessionDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
struct OptUnitField {
    #[primary_span]
    spans: Span,
    #[help]
    foo: Option<()>,
    #[help(typeck::help)]
    bar: Option<()>,
}

#[derive(SessionDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
struct LabelWithTrailingPath {
    #[label(typeck::label, foo)]
    //~^ ERROR `#[label(...)]` is not a valid attribute
    span: Span,
}

#[derive(SessionDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
struct LabelWithTrailingNameValue {
    #[label(typeck::label, foo = "...")]
    //~^ ERROR `#[label(...)]` is not a valid attribute
    span: Span,
}

#[derive(SessionDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
struct LabelWithTrailingList {
    #[label(typeck::label, foo("..."))]
    //~^ ERROR `#[label(...)]` is not a valid attribute
    span: Span,
}

#[derive(SessionDiagnostic)]
#[lint(typeck::ambiguous_lifetime_bound)]
//~^ ERROR only `#[error(..)]` and `#[warning(..)]` are supported
struct LintsBad {
}

#[derive(LintDiagnostic)]
#[lint(typeck::ambiguous_lifetime_bound)]
struct LintsGood {
}

#[derive(LintDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound)]
//~^ ERROR only `#[lint(..)]` is supported
struct ErrorsBad {
}

#[derive(SessionDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
struct ErrorWithMultiSpan {
    #[primary_span]
    span: MultiSpan,
}

#[derive(SessionDiagnostic)]
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
#[warn_]
struct ErrorWithWarn {
    val: String,
}