frontend 0.4.1

rustc's frontend with no LLVM and no std: parsing through MIR, as a library
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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
//! Errors emitted by ast_passes.

use alloc::vec::Vec;
use alloc::string::String;
use crate::rustc_abi::ExternAbi;
use crate::rustc_errors::codes::*;
use crate::rustc_errors::{Applicability, Diag, EmissionGuarantee, Subdiagnostic};
use rustc_macros::{Diagnostic, Subdiagnostic};
use crate::rustc_span::{Ident, Span, Symbol};

#[derive(Diagnostic)]
#[diag("visibility qualifiers are not permitted here", code = E0449)]
pub(crate) struct VisibilityNotPermitted {
    #[primary_span]
    pub span: Span,
    #[subdiagnostic]
    pub note: VisibilityNotPermittedNote,
    #[suggestion("remove the qualifier", code = "", applicability = "machine-applicable")]
    pub remove_qualifier_sugg: Span,
}

#[derive(Subdiagnostic)]
pub(crate) enum VisibilityNotPermittedNote {
    #[note("enum variants and their fields always share the visibility of the enum they are in")]
    EnumVariant,
    #[note("trait items always share the visibility of their trait")]
    TraitImpl,
    #[note("place qualifiers on individual impl items instead")]
    IndividualImplItems,
    #[note("place qualifiers on individual foreign items instead")]
    IndividualForeignItems,
}
#[derive(Diagnostic)]
#[diag("redundant `const` fn marker in const impl")]
pub(crate) struct ImplFnConst {
    #[primary_span]
    #[suggestion("remove the `const`", code = "", applicability = "machine-applicable")]
    pub span: Span,
    #[label("this declares all associated functions implicitly const")]
    pub parent_constness: Span,
}

#[derive(Diagnostic)]
#[diag("`feature(generic_const_exprs)` is not supported with the next-generation trait solver")]
#[note("`-Znext-solver=globally` is currently enabled by default for testing")]
#[note("reverted the setting to `-Znext-solver=coherence` for this crate")]
#[note("the currently stable trait solver will be used for this crate")]
#[note("see issues #160895 <https://github.com/rust-lang/rust/issues/160895> for more information")]
pub(crate) struct NextSolverDisabledForGenericConstExprs {
    #[primary_span]
    pub span: Span,
}

#[derive(Diagnostic)]
#[diag("functions in {$in_impl ->
        [true] trait impls
        *[false] traits
    } cannot be declared const", code = E0379)]
pub(crate) struct TraitFnConst {
    #[primary_span]
    #[label(
        "functions in {$in_impl ->
            [true] trait impls
            *[false] traits
        } cannot be const"
    )]
    pub span: Span,
    pub in_impl: bool,
    #[label("this declares all associated functions implicitly const")]
    pub const_context_label: Option<Span>,
    #[suggestion(
        "remove the `const`{$requires_multiple_changes ->
            [true] {\" ...\"}
            *[false] {\"\"}
        }",
        code = ""
    )]
    pub remove_const_sugg: (Span, Applicability),
    pub requires_multiple_changes: bool,
    #[suggestion(
        "... and declare the impl to be const instead",
        code = "const ",
        applicability = "maybe-incorrect"
    )]
    pub make_impl_const_sugg: Option<Span>,
    #[suggestion(
        "... and declare the trait to be const instead",
        code = "const ",
        applicability = "maybe-incorrect"
    )]
    pub make_trait_const_sugg: Option<Span>,
}

#[derive(Diagnostic)]
#[diag(
    "async functions are not allowed in `const` {$context ->
        [trait_impl] trait impls
        [impl] impls
        *[trait] traits
    }"
)]
pub(crate) struct AsyncFnInConstTraitOrTraitImpl {
    #[primary_span]
    pub async_keyword: Span,
    pub context: &'static str,
    #[label("associated functions of `const` cannot be declared `async`")]
    pub const_keyword: Span,
}

#[derive(Diagnostic)]
#[diag("bounds cannot be used in this context")]
pub(crate) struct ForbiddenBound {
    #[primary_span]
    pub spans: Vec<Span>,
}

#[derive(Diagnostic)]
#[diag("late-bound const parameters cannot be used currently")]
pub(crate) struct ForbiddenConstParam {
    #[primary_span]
    pub const_param_spans: Vec<Span>,
}

#[derive(Diagnostic)]
#[diag("function can not have more than {$max_num_args} arguments")]
pub(crate) struct FnParamTooMany {
    #[primary_span]
    pub span: Span,
    pub max_num_args: usize,
}

#[derive(Diagnostic)]
#[diag("`...` must be the last argument of a C-variadic function")]
pub(crate) struct FnParamCVarArgsNotLast {
    #[primary_span]
    pub span: Span,
}

#[derive(Diagnostic)]
#[diag(
    "`#[rustc_splat]` is only supported on argument index {$max_valid_splatted_arg_index} or less, this `#[rustc_splat]` is on index {$first_invalid_splatted_arg_index}"
)]
#[help(
    "remove `#[rustc_splat]`, or use it on an argument closer to the start of the argument list"
)]
pub(crate) struct InvalidSplattedArgs {
    pub max_valid_splatted_arg_index: u16,

    pub first_invalid_splatted_arg_index: u16,

    #[primary_span]
    #[label("`#[rustc_splat]` is not supported here")]
    pub spans: Vec<Span>,
}

#[derive(Diagnostic)]
#[diag("multiple `#[rustc_splat]`s are not allowed in the same function argument list")]
#[help("remove `#[rustc_splat]` from all but one argument")]
pub(crate) struct DuplicateSplattedArgs {
    #[primary_span]
    pub spans: Vec<Span>,
}

#[derive(Diagnostic)]
#[diag("`...` and `#[rustc_splat]` are not allowed in the same function argument list")]
#[help("remove `#[rustc_splat]` or remove `...`")]
pub(crate) struct CVarArgsAndSplat {
    #[primary_span]
    pub spans: Vec<Span>,
}

#[derive(Diagnostic)]
#[diag("`#[rustc_splat]` is not allowed on closure arguments")]
#[help("remove `#[rustc_splat]` or turn the closure into a function")]
pub(crate) struct SplatNotAllowedOnClosures {
    #[primary_span]
    pub spans: Vec<Span>,
}

#[derive(Diagnostic)]
#[diag("`#[rustc_splat]` is not allowed in the arguments of functions with the `{$abi}` ABI")]
#[help("remove `#[rustc_splat]` or change the ABI")]
pub(crate) struct SplatNotAllowedOnAbiCall {
    #[primary_span]
    pub spans: Vec<Span>,
    pub abi: Symbol,
}

#[derive(Diagnostic)]
#[diag("documentation comments cannot be applied to function parameters")]
pub(crate) struct FnParamDocComment {
    #[primary_span]
    #[label("doc comments are not allowed here")]
    pub span: Span,
}

// FIXME(splat): add splat to the allowed built-in attributes when it is complete/stabilized
#[derive(Diagnostic)]
#[diag(
    "allow, cfg, cfg_attr, deny, expect, forbid, and warn are the only allowed built-in attributes in function parameters"
)]
pub(crate) struct FnParamForbiddenAttr {
    #[primary_span]
    pub span: Span,
}

#[derive(Diagnostic)]
#[diag("`#[{$eii_name}]` is not allowed to have `#[{$attr_name}]`")]
pub(crate) struct EiiImplAttributeNotSupported<'a> {
    #[primary_span]
    pub attr_span: Span,
    pub attr_name: &'a str,
    pub eii_name: String,
    #[label("`#[{$eii_name}]` is not allowed to have `#[{$attr_name}]`")]
    pub eii_span: Span,
}

#[derive(Diagnostic)]
#[diag("`self` parameter is only allowed in associated functions")]
#[note("associated functions are those in `impl` or `trait` definitions")]
pub(crate) struct FnParamForbiddenSelf {
    #[primary_span]
    #[label("not semantically valid as function parameter")]
    pub span: Span,
}

#[derive(Diagnostic)]
#[diag("`default` is only allowed on items in trait impls")]
pub(crate) struct ForbiddenDefault {
    #[primary_span]
    pub span: Span,
    #[label("`default` because of this")]
    pub def_span: Span,
}

#[derive(Diagnostic)]
#[diag("`final` is only allowed on associated functions in traits")]
pub(crate) struct ForbiddenFinal {
    #[primary_span]
    pub span: Span,
    #[label("`final` because of this")]
    pub def_span: Span,
}

#[derive(Diagnostic)]
#[diag("`final` is only allowed on associated functions if they have a body")]
pub(crate) struct ForbiddenFinalWithoutBody {
    #[primary_span]
    pub span: Span,
    #[label("`final` because of this")]
    pub def_span: Span,
}

#[derive(Diagnostic)]
#[diag("associated constant in `impl` without body")]
pub(crate) struct AssocConstWithoutBody {
    #[primary_span]
    pub span: Span,
    #[suggestion(
        "provide a definition for the constant",
        code = " = <expr>;",
        applicability = "has-placeholders"
    )]
    pub replace_span: Span,
}

#[derive(Diagnostic)]
#[diag("associated function in `impl` without body")]
pub(crate) struct AssocFnWithoutBody {
    #[primary_span]
    pub span: Span,
    #[suggestion(
        "provide a definition for the function",
        code = " {{ <body> }}",
        applicability = "has-placeholders"
    )]
    pub replace_span: Span,
}

#[derive(Diagnostic)]
#[diag("associated type in `impl` without body")]
pub(crate) struct AssocTypeWithoutBody {
    #[primary_span]
    pub span: Span,
    #[suggestion(
        "provide a definition for the type",
        code = " = <type>;",
        applicability = "has-placeholders"
    )]
    pub replace_span: Span,
}

#[derive(Diagnostic)]
#[diag("free constant item without body")]
pub(crate) struct ConstWithoutBody {
    #[primary_span]
    pub span: Span,
    #[suggestion(
        "provide a definition for the constant",
        code = " = <expr>;",
        applicability = "has-placeholders"
    )]
    pub replace_span: Span,
}

#[derive(Diagnostic)]
#[diag("free static item without body")]
pub(crate) struct StaticWithoutBody {
    #[primary_span]
    pub span: Span,
    #[suggestion(
        "provide a definition for the static",
        code = " = <expr>;",
        applicability = "has-placeholders"
    )]
    pub replace_span: Span,
}

#[derive(Diagnostic)]
#[diag("free type alias without body")]
pub(crate) struct TyAliasWithoutBody {
    #[primary_span]
    pub span: Span,
    #[suggestion(
        "provide a definition for the type",
        code = " = <type>;",
        applicability = "has-placeholders"
    )]
    pub replace_span: Span,
}

#[derive(Diagnostic)]
#[diag("free function without a body")]
pub(crate) struct FnWithoutBody {
    #[primary_span]
    pub span: Span,
    #[suggestion(
        "provide a definition for the function",
        code = " {{ <body> }}",
        applicability = "has-placeholders"
    )]
    pub replace_span: Span,
    #[subdiagnostic]
    pub extern_block_suggestion: Option<ExternBlockSuggestion>,
}

#[derive(Subdiagnostic)]
pub(crate) enum ExternBlockSuggestion {
    #[multipart_suggestion(
        "if you meant to declare an externally defined function, use an `extern` block",
        applicability = "maybe-incorrect"
    )]
    Implicit {
        #[suggestion_part(code = "extern {{")]
        start_span: Span,
        #[suggestion_part(code = " }}")]
        end_span: Span,
    },
    #[multipart_suggestion(
        "if you meant to declare an externally defined function, use an `extern` block",
        applicability = "maybe-incorrect"
    )]
    Explicit {
        #[suggestion_part(code = "extern \"{abi}\" {{")]
        start_span: Span,
        #[suggestion_part(code = " }}")]
        end_span: Span,
        abi: Symbol,
    },
}

#[derive(Diagnostic)]
#[diag("items in `extern` blocks without an `unsafe` qualifier cannot have safety qualifiers")]
pub(crate) struct InvalidSafetyOnExtern {
    #[primary_span]
    pub item_span: Span,
    #[suggestion(
        "add `unsafe` to this `extern` block",
        code = "unsafe ",
        applicability = "machine-applicable",
        style = "verbose"
    )]
    pub block: Option<Span>,
}

#[derive(Diagnostic)]
#[diag(
    "items outside of `unsafe extern {\"{ }\"}` cannot be declared with `safe` safety qualifier"
)]
pub(crate) struct InvalidSafetyOnItem {
    #[primary_span]
    pub span: Span,
}

#[derive(Diagnostic)]
#[diag("function pointers cannot be declared with `safe` safety qualifier")]
pub(crate) struct InvalidSafetyOnFnPtr {
    #[primary_span]
    pub span: Span,
    #[suggestion(
        "remove the `safe` qualifier",
        code = "",
        applicability = "machine-applicable",
        style = "verbose"
    )]
    pub safe_span: Span,
}

#[derive(Diagnostic)]
#[diag("static items cannot be declared with `unsafe` safety qualifier outside of `extern` block")]
pub(crate) struct UnsafeStatic {
    #[primary_span]
    pub span: Span,
}

#[derive(Diagnostic)]
#[diag("bounds on `type`s in {$ctx} have no effect")]
pub(crate) struct BoundInContext<'a> {
    #[primary_span]
    pub span: Span,
    pub ctx: &'a str,
}

#[derive(Diagnostic)]
#[diag("`type`s inside `extern` blocks cannot have {$descr}")]
#[note("for more information, visit https://doc.rust-lang.org/std/keyword.extern.html")]
pub(crate) struct ExternTypesCannotHave<'a> {
    #[primary_span]
    #[suggestion("remove the {$remove_descr}", code = "", applicability = "maybe-incorrect")]
    pub span: Span,
    pub descr: &'a str,
    pub remove_descr: &'a str,
    #[label("`extern` block begins here")]
    pub block_span: Span,
}

#[derive(Diagnostic)]
#[diag("incorrect `{$kind}` inside `extern` block")]
#[note("for more information, visit https://doc.rust-lang.org/std/keyword.extern.html")]
pub(crate) struct BodyInExtern<'a> {
    #[primary_span]
    #[label("cannot have a body")]
    pub span: Span,
    #[label("the invalid body")]
    pub body: Span,
    #[label(
        "`extern` blocks define existing foreign {$kind}s and {$kind}s inside of them cannot have a body"
    )]
    pub block: Span,
    pub kind: &'a str,
}

#[derive(Diagnostic)]
#[diag("incorrect function inside `extern` block")]
#[help(
    "you might have meant to write a function accessible through FFI, which can be done by writing `extern fn` outside of the `extern` block"
)]
#[note("for more information, visit https://doc.rust-lang.org/std/keyword.extern.html")]
pub(crate) struct FnBodyInExtern {
    #[primary_span]
    #[label("cannot have a body")]
    pub span: Span,
    #[suggestion("remove the invalid body", code = ";", applicability = "maybe-incorrect")]
    pub body: Span,
    #[label(
        "`extern` blocks define existing foreign functions and functions inside of them cannot have a body"
    )]
    pub block: Span,
}

#[derive(Diagnostic)]
#[diag("functions in `extern` blocks cannot have `{$kw}` qualifier")]
pub(crate) struct FnQualifierInExtern {
    #[primary_span]
    #[suggestion("remove the `{$kw}` qualifier", code = "", applicability = "maybe-incorrect")]
    pub span: Span,
    #[label("in this `extern` block")]
    pub block: Span,
    pub kw: &'static str,
}

#[derive(Diagnostic)]
#[diag("items in `extern` blocks cannot use non-ascii identifiers")]
#[note(
    "this limitation may be lifted in the future; see issue #83942 <https://github.com/rust-lang/rust/issues/83942> for more information"
)]
pub(crate) struct ExternItemAscii {
    #[primary_span]
    pub span: Span,
    #[label("in this `extern` block")]
    pub block: Span,
}

#[derive(Diagnostic)]
#[diag("`...` is not supported for non-extern functions")]
#[help(
    "only `extern \"C\"` and `extern \"C-unwind\"` functions may have a C variable argument list"
)]
pub(crate) struct CVariadicNoExtern {
    #[primary_span]
    pub span: Span,
}

#[derive(Diagnostic)]
#[diag("functions with a C variable argument list must be unsafe")]
pub(crate) struct CVariadicMustBeUnsafe {
    #[primary_span]
    pub span: Span,

    #[suggestion(
        "add the `unsafe` keyword to this definition",
        applicability = "maybe-incorrect",
        code = "unsafe ",
        style = "verbose"
    )]
    pub unsafe_span: Span,
}

#[derive(Diagnostic)]
#[diag("`...` is not supported for `extern \"{$abi}\"` functions")]
#[help(
    "only `extern \"C\"` and `extern \"C-unwind\"` functions may have a C variable argument list"
)]
pub(crate) struct CVariadicBadExtern {
    #[primary_span]
    pub span: Span,
    pub abi: &'static str,
    #[label("`extern \"{$abi}\"` because of this")]
    pub extern_span: Span,
}

#[derive(Diagnostic)]
#[diag("`...` is not supported for `extern \"{$abi}\"` naked functions")]
#[help("C-variadic function must have a compatible calling convention")]
pub(crate) struct CVariadicBadNakedExtern {
    #[primary_span]
    pub span: Span,
    pub abi: &'static str,
    #[label("`extern \"{$abi}\"` because of this")]
    pub extern_span: Span,
}

#[derive(Diagnostic)]
#[diag("`{$kind}` items in this context need a name")]
pub(crate) struct ItemUnderscore<'a> {
    #[primary_span]
    #[label("`_` is not a valid name for this `{$kind}` item")]
    pub span: Span,
    pub kind: &'a str,
}

#[derive(Diagnostic)]
#[diag("`#[no_mangle]` requires ASCII identifier", code = E0754)]
pub(crate) struct NoMangleAscii {
    #[primary_span]
    pub span: Span,
}

#[derive(Diagnostic)]
#[diag("trying to load file for module `{$name}` with non-ascii identifier name", code = E0754)]
#[help("consider using the `#[path]` attribute to specify filesystem path")]
pub(crate) struct ModuleNonAscii {
    #[primary_span]
    pub span: Span,
    pub name: Symbol,
}

#[derive(Diagnostic)]
#[diag("auto traits cannot have generic parameters", code = E0567)]
pub(crate) struct AutoTraitGeneric {
    #[primary_span]
    #[suggestion(
        "remove the parameters",
        code = "",
        applicability = "machine-applicable",
        style = "tool-only"
    )]
    pub span: Span,
    #[label("auto trait cannot have generic parameters")]
    pub ident: Span,
}

#[derive(Diagnostic)]
#[diag("auto traits cannot have super traits or lifetime bounds", code = E0568)]
pub(crate) struct AutoTraitBounds {
    #[primary_span]
    pub span: Vec<Span>,
    #[suggestion(
        "remove the super traits or lifetime bounds",
        code = "",
        applicability = "machine-applicable",
        style = "tool-only"
    )]
    pub removal: Span,
    #[label("auto traits cannot have super traits or lifetime bounds")]
    pub ident: Span,
}

#[derive(Diagnostic)]
#[diag("auto traits cannot have associated items", code = E0380)]
pub(crate) struct AutoTraitItems {
    #[primary_span]
    pub spans: Vec<Span>,
    #[suggestion(
        "remove the associated items",
        code = "",
        applicability = "machine-applicable",
        style = "tool-only"
    )]
    pub total: Span,
    #[label("auto traits cannot have associated items")]
    pub ident: Span,
}

#[derive(Diagnostic)]
#[diag("auto traits cannot be const")]
#[help("remove the `const` keyword")]
pub(crate) struct ConstAutoTrait {
    #[primary_span]
    pub span: Span,
}

#[derive(Diagnostic)]
#[diag("generic arguments must come before the first constraint")]
pub(crate) struct ArgsBeforeConstraint {
    #[primary_span]
    pub arg_spans: Vec<Span>,
    #[label(
        "{$constraint_len ->
            [one] constraint
            *[other] constraints
        }"
    )]
    pub constraints: Span,
    #[label(
        "generic {$args_len ->
            [one] argument
            *[other] arguments
        }"
    )]
    pub args: Span,
    #[suggestion(
        "move the {$constraint_len ->
            [one] constraint
            *[other] constraints
        } after the generic {$args_len ->
            [one] argument
            *[other] arguments
        }",
        code = "{suggestion}",
        applicability = "machine-applicable",
        style = "verbose"
    )]
    pub data: Span,
    pub suggestion: String,
    pub constraint_len: usize,
    pub args_len: usize,
    #[subdiagnostic]
    pub constraint_spans: EmptyLabelManySpans,
    #[subdiagnostic]
    pub arg_spans2: EmptyLabelManySpans,
}

pub(crate) struct EmptyLabelManySpans(pub Vec<Span>);

// The derive for `Vec<Span>` does multiple calls to `span_label`, adding commas between each
impl Subdiagnostic for EmptyLabelManySpans {
    fn add_to_diag<G: EmissionGuarantee>(self, diag: &mut Diag<'_, G>) {
        diag.span_labels(self.0, "");
    }
}

#[derive(Diagnostic)]
#[diag("patterns aren't allowed in function pointer types", code = E0561)]
pub(crate) struct PatternFnPointer {
    #[primary_span]
    pub span: Span,
}

#[derive(Diagnostic)]
#[diag("only a single explicit lifetime bound is permitted", code = E0226)]
pub(crate) struct TraitObjectBound {
    #[primary_span]
    pub span: Span,
}

#[derive(Diagnostic)]
#[diag("nested `impl Trait` is not allowed", code = E0666)]
pub(crate) struct NestedImplTrait {
    #[primary_span]
    pub span: Span,
    #[label("outer `impl Trait`")]
    pub outer: Span,
    #[label("nested `impl Trait` here")]
    pub inner: Span,
}

#[derive(Diagnostic)]
#[diag("at least one trait must be specified")]
pub(crate) struct AtLeastOneTrait {
    #[primary_span]
    pub span: Span,
}

#[derive(Diagnostic)]
#[diag("{$param_ord} parameters must be declared prior to {$max_param} parameters")]
pub(crate) struct OutOfOrderParams<'a> {
    #[primary_span]
    pub spans: Vec<Span>,
    #[suggestion(
        "reorder the parameters: lifetimes, then consts and types",
        code = "{ordered_params}",
        applicability = "machine-applicable"
    )]
    pub sugg_span: Span,
    pub param_ord: String,
    pub max_param: String,
    pub ordered_params: &'a str,
}

#[derive(Diagnostic)]
#[diag("`impl Trait for .. {\"{}\"}` is an obsolete syntax")]
#[help("use `auto trait Trait {\"{}\"}` instead")]
pub(crate) struct ObsoleteAuto {
    #[primary_span]
    pub span: Span,
}

#[derive(Diagnostic)]
#[diag("negative impls cannot be unsafe", code = E0198)]
pub(crate) struct UnsafeNegativeImpl {
    #[primary_span]
    pub span: Span,
    #[label("negative because of this")]
    pub negative: Span,
    #[label("unsafe because of this")]
    pub r#unsafe: Span,
}

#[derive(Diagnostic)]
#[diag("{$kind} cannot be declared unsafe")]
pub(crate) struct UnsafeItem {
    #[primary_span]
    pub span: Span,
    pub kind: &'static str,
}

#[derive(Diagnostic)]
#[diag("extern blocks must be unsafe")]
pub(crate) struct MissingUnsafeOnExtern {
    #[primary_span]
    pub span: Span,

    #[suggestion(
        "needs `unsafe` before the extern keyword",
        code = "unsafe ",
        applicability = "machine-applicable"
    )]
    pub unsafe_span: Span,
}

#[derive(Diagnostic)]
#[diag("extern blocks should be unsafe")]
pub(crate) struct MissingUnsafeOnExternLint {
    #[suggestion(
        "needs `unsafe` before the extern keyword",
        code = "unsafe ",
        applicability = "machine-applicable"
    )]
    pub suggestion: Span,
}

#[derive(Diagnostic)]
#[diag("unions cannot have zero fields")]
pub(crate) struct FieldlessUnion {
    #[primary_span]
    pub span: Span,
}

#[derive(Diagnostic)]
#[diag("where clauses are not allowed after the type for type aliases")]
#[note("see issue #112792 <https://github.com/rust-lang/rust/issues/112792> for more information")]
pub(crate) struct WhereClauseAfterTypeAlias {
    #[primary_span]
    pub span: Span,
    #[help("add `#![feature(checked_type_aliases)]` to the crate attributes to enable")]
    pub help: bool,
}

#[derive(Diagnostic)]
#[diag("where clauses are not allowed before the type for type aliases")]
#[note("see issue #89122 <https://github.com/rust-lang/rust/issues/89122> for more information")]
pub(crate) struct WhereClauseBeforeTypeAlias {
    #[primary_span]
    pub span: Span,
    #[subdiagnostic]
    pub sugg: WhereClauseBeforeTypeAliasSugg,
}

#[derive(Subdiagnostic)]
pub(crate) enum WhereClauseBeforeTypeAliasSugg {
    #[suggestion("remove this `where`", applicability = "machine-applicable", code = "")]
    Remove {
        #[primary_span]
        span: Span,
    },
    #[multipart_suggestion(
        "move it to the end of the type declaration",
        applicability = "machine-applicable",
        style = "verbose"
    )]
    Move {
        #[suggestion_part(code = "")]
        left: Span,
        snippet: String,
        #[suggestion_part(code = "{snippet}")]
        right: Span,
    },
}

#[derive(Diagnostic)]
#[diag("generic parameters with a default must be trailing")]
pub(crate) struct GenericDefaultTrailing {
    #[primary_span]
    pub span: Span,
}

#[derive(Diagnostic)]
#[diag("nested quantification of lifetimes", code = E0316)]
pub(crate) struct NestedLifetimes {
    #[primary_span]
    pub span: Span,
}

#[derive(Diagnostic)]
#[diag("const trait bounds are not allowed in trait object types")]
pub(crate) struct ConstBoundTraitObject {
    #[primary_span]
    pub span: Span,
}

// FIXME(const_trait_impl): Consider making the note/reason the message of the diagnostic.
// FIXME(const_trait_impl): Provide structured suggestions (e.g., add `const` here).
#[derive(Diagnostic)]
#[diag("`[const]` is not allowed here")]
pub(crate) struct TildeConstDisallowed {
    #[primary_span]
    pub span: Span,
    #[subdiagnostic]
    pub reason: TildeConstReason,
}

#[derive(Subdiagnostic, Copy, Clone)]
pub(crate) enum TildeConstReason {
    #[note("closures cannot have `[const]` trait bounds")]
    Closure,
    #[note("this function is not `const`, so it cannot have `[const]` trait bounds")]
    Function {
        #[primary_span]
        ident: Span,
    },
    #[note("this trait is not `const`, so it cannot have `[const]` trait bounds")]
    Trait {
        #[primary_span]
        span: Span,
    },
    #[note("this impl is not `const`, so it cannot have `[const]` trait bounds")]
    TraitImpl {
        #[primary_span]
        span: Span,
    },
    #[note("inherent impls cannot have `[const]` trait bounds")]
    Impl {
        #[primary_span]
        span: Span,
    },
    #[note("associated types in non-`const` traits cannot have `[const]` trait bounds")]
    TraitAssocTy {
        #[primary_span]
        span: Span,
    },
    #[note("associated types in non-const impls cannot have `[const]` trait bounds")]
    TraitImplAssocTy {
        #[primary_span]
        span: Span,
    },
    #[note("inherent associated types cannot have `[const]` trait bounds")]
    InherentAssocTy {
        #[primary_span]
        span: Span,
    },
    #[note("structs cannot have `[const]` trait bounds")]
    Struct {
        #[primary_span]
        span: Span,
    },
    #[note("enums cannot have `[const]` trait bounds")]
    Enum {
        #[primary_span]
        span: Span,
    },
    #[note("unions cannot have `[const]` trait bounds")]
    Union {
        #[primary_span]
        span: Span,
    },
    #[note("anonymous constants cannot have `[const]` trait bounds")]
    AnonConst {
        #[primary_span]
        span: Span,
    },
    #[note("trait objects cannot have `[const]` trait bounds")]
    TraitObject,
    #[note("this item cannot have `[const]` trait bounds")]
    Item,
}

#[derive(Diagnostic)]
#[diag("functions cannot be both `const` and `{$coroutine_kind}`")]
pub(crate) struct ConstAndCoroutine {
    #[primary_span]
    pub spans: Vec<Span>,
    #[label("`const` because of this")]
    pub const_span: Span,
    #[label("`{$coroutine_kind}` because of this")]
    pub coroutine_span: Span,
    #[label("{\"\"}")]
    pub span: Span,
    pub coroutine_kind: &'static str,
}

#[derive(Diagnostic)]
#[diag("functions cannot be both `{$coroutine_kind}` and C-variadic")]
pub(crate) struct CoroutineAndCVariadic {
    #[primary_span]
    pub spans: Vec<Span>,
    pub coroutine_kind: &'static str,
    #[label("`{$coroutine_kind}` because of this")]
    pub coroutine_span: Span,
    #[label("C-variadic because of this")]
    pub variadic_span: Span,
}

#[derive(Diagnostic)]
#[diag("the `{$target}` target does not support c-variadic functions")]
pub(crate) struct CVariadicNotSupported<'a> {
    #[primary_span]
    pub variadic_span: Span,
    pub target: &'a str,
}

#[derive(Diagnostic)]
#[diag("patterns aren't allowed in foreign function declarations", code = E0130)]
// FIXME: deduplicate with rustc_lint (`BuiltinLintDiag::PatternsInFnsWithoutBody`)
pub(crate) struct PatternInForeign {
    #[primary_span]
    #[label("pattern not allowed in foreign function")]
    pub span: Span,
}

#[derive(Diagnostic)]
#[diag("patterns aren't allowed in functions without bodies", code = E0642)]
// FIXME: deduplicate with rustc_lint (`BuiltinLintDiag::PatternsInFnsWithoutBody`)
pub(crate) struct PatternInBodiless {
    #[primary_span]
    #[label("pattern not allowed in function without body")]
    pub span: Span,
}

#[derive(Diagnostic)]
#[diag("`#![feature]` may not be used on the {$channel} release channel", code = E0554)]
pub(crate) struct FeatureOnNonNightly {
    #[primary_span]
    pub span: Span,
    pub channel: &'static str,
    #[subdiagnostic]
    pub stable_features: Vec<StableFeature>,
    #[suggestion("remove the attribute", code = "", applicability = "machine-applicable")]
    pub sugg: Option<Span>,
}

#[derive(Subdiagnostic)]
#[help(
    "the feature `{$name}` has been stable since `{$since}` and no longer requires an attribute to enable"
)]
pub(crate) struct StableFeature {
    pub name: Symbol,
    pub since: Symbol,
}

#[derive(Diagnostic)]
#[diag("`{$f1}` and `{$f2}` are incompatible, using them at the same time is not allowed")]
#[help("remove one of these features")]
pub(crate) struct IncompatibleFeatures {
    #[primary_span]
    pub spans: Vec<Span>,
    pub f1: Symbol,
    pub f2: Symbol,
}

#[derive(Diagnostic)]
#[diag("`{$parent}` requires {$missing} to be enabled")]
#[help("enable all of these features")]
pub(crate) struct MissingDependentFeatures {
    #[primary_span]
    pub parent_span: Span,
    pub parent: Symbol,
    pub missing: String,
}

#[derive(Diagnostic)]
#[diag("negative bounds are not supported")]
pub(crate) struct NegativeBoundUnsupported {
    #[primary_span]
    pub span: Span,
}

#[derive(Diagnostic)]
#[diag("associated type constraints not allowed on negative bounds")]
pub(crate) struct ConstraintOnNegativeBound {
    #[primary_span]
    pub span: Span,
}

#[derive(Diagnostic)]
#[diag("parenthetical notation may not be used for negative bounds")]
pub(crate) struct NegativeBoundWithParentheticalNotation {
    #[primary_span]
    pub span: Span,
}

#[derive(Diagnostic)]
#[diag("`match` arm with no body")]
pub(crate) struct MatchArmWithNoBody {
    #[primary_span]
    pub span: Span,
    // We include the braces around `todo!()` so that a comma is optional, and we don't have to have
    // any logic looking at the arm being replaced if there was a comma already or not for the
    // resulting code to be correct.
    #[suggestion(
        "add a body after the pattern",
        // ignore-tidy-todo
        code = " => {{ todo!() }}",
        applicability = "has-placeholders",
        style = "verbose"
    )]
    pub suggestion: Span,
}

#[derive(Diagnostic)]
#[diag("`use<...>` precise capturing syntax not allowed in {$loc}")]
pub(crate) struct PreciseCapturingNotAllowedHere {
    #[primary_span]
    pub span: Span,
    pub loc: &'static str,
}

#[derive(Diagnostic)]
#[diag("duplicate `use<...>` precise capturing syntax")]
pub(crate) struct DuplicatePreciseCapturing {
    #[primary_span]
    pub bound1: Span,
    #[label("second `use<...>` here")]
    pub bound2: Span,
}

#[derive(Diagnostic)]
#[diag("`extern` declarations without an explicit ABI are disallowed")]
#[help("prior to Rust 2024, a default ABI was inferred")]
pub(crate) struct MissingAbi {
    #[primary_span]
    #[suggestion("specify an ABI", code = "extern \"<abi>\"", applicability = "has-placeholders")]
    pub span: Span,
}

#[derive(Diagnostic)]
#[diag("`extern` declarations without an explicit ABI are deprecated")]
pub(crate) struct MissingAbiSugg {
    #[suggestion(
        "explicitly specify the {$default_abi} ABI",
        code = "extern {default_abi}",
        applicability = "machine-applicable"
    )]
    pub span: Span,
    pub default_abi: ExternAbi,
}

#[derive(Diagnostic)]
#[diag("foreign functions with the \"custom\" ABI cannot be safe")]
pub(crate) struct AbiCustomSafeForeignFunction {
    #[primary_span]
    pub span: Span,

    #[suggestion(
        "remove the `safe` keyword from this definition",
        applicability = "maybe-incorrect",
        code = "",
        style = "verbose"
    )]
    pub safe_span: Span,
}

#[derive(Diagnostic)]
#[diag("functions with the \"custom\" ABI must be unsafe")]
pub(crate) struct AbiCustomSafeFunction {
    #[primary_span]
    pub span: Span,
    pub abi: ExternAbi,

    #[suggestion(
        "add the `unsafe` keyword to this definition",
        applicability = "maybe-incorrect",
        code = "unsafe ",
        style = "verbose"
    )]
    pub unsafe_span: Span,
}

#[derive(Diagnostic)]
#[diag("functions with the {$abi} ABI cannot be `{$coroutine_kind_str}`")]
pub(crate) struct AbiCannotBeCoroutine {
    #[primary_span]
    pub span: Span,
    pub abi: ExternAbi,

    #[suggestion(
        "remove the `{$coroutine_kind_str}` keyword from this definition",
        applicability = "maybe-incorrect",
        code = "",
        style = "verbose"
    )]
    pub coroutine_kind_span: Span,
    pub coroutine_kind_str: &'static str,
}

#[derive(Diagnostic)]
#[diag("invalid signature for `extern {$abi}` function")]
#[note("functions with the {$abi} ABI cannot have any parameters or return type")]
pub(crate) struct AbiMustNotHaveParametersOrReturnType {
    #[primary_span]
    pub spans: Vec<Span>,
    pub abi: ExternAbi,

    #[suggestion(
        "remove the parameters and return type",
        applicability = "maybe-incorrect",
        code = "{padding}fn{symbol}()",
        style = "verbose"
    )]
    pub suggestion_span: Span,
    pub symbol: String,
    pub padding: &'static str,
}

#[derive(Diagnostic)]
#[diag("invalid signature for `extern {$abi}` function")]
#[note("functions with the {$abi} ABI cannot have a return type")]
pub(crate) struct AbiMustNotHaveReturnType {
    #[primary_span]
    #[help("remove the return type")]
    pub span: Span,
    pub abi: ExternAbi,
}

#[derive(Diagnostic)]
#[diag("invalid signature for `extern \"x86-interrupt\"` function")]
#[note(
    "functions with the \"x86-interrupt\" ABI must be have either 1 or 2 parameters (but found {$param_count})"
)]
pub(crate) struct AbiX86Interrupt {
    #[primary_span]
    pub spans: Vec<Span>,
    pub param_count: usize,
}

#[derive(Diagnostic)]
#[diag("scalable vectors must be tuple structs")]
pub(crate) struct ScalableVectorNotTupleStruct {
    #[primary_span]
    pub span: Span,
}

#[derive(Diagnostic)]
#[diag("scalable vectors are not supported on this architecture")]
pub(crate) struct ScalableVectorBadArch {
    #[primary_span]
    pub span: Span,
}

#[derive(Diagnostic)]
#[diag("`#[track_caller]` can only be used with the Rust ABI", code = E0737)]
pub(crate) struct RequiresRustAbi {
    #[primary_span]
    #[label("using `#[track_caller]` here")]
    pub track_caller_span: Span,
    #[label("not using the Rust ABI because of this")]
    pub extern_abi_span: Span,
}

#[derive(Diagnostic)]
#[diag("visibility qualifiers have no effect on `const _` declarations")]
#[note("`const _` does not declare a name, so there is nothing for the qualifier to apply to")]
pub(crate) struct UnusedVisibility {
    #[suggestion(
        "remove the qualifier",
        style = "short",
        code = "",
        applicability = "machine-applicable"
    )]
    pub span: Span,
}

#[derive(Subdiagnostic)]
#[suggestion(
    "remove `mut` from the parameter",
    code = "{ident}",
    applicability = "machine-applicable"
)]
pub(crate) struct PatternsInFnsWithoutBodySub {
    #[primary_span]
    pub span: Span,

    pub ident: Ident,
}

#[derive(Diagnostic)]
pub(crate) enum PatternsInFnsWithoutBody {
    #[diag("patterns aren't allowed in foreign function declarations")]
    Foreign {
        #[subdiagnostic]
        sub: PatternsInFnsWithoutBodySub,
    },
    #[diag("patterns aren't allowed in functions without bodies")]
    Bodiless {
        #[subdiagnostic]
        sub: PatternsInFnsWithoutBodySub,
    },
}

#[derive(Diagnostic)]
#[diag("where clause not allowed here")]
#[note("see issue #89122 <https://github.com/rust-lang/rust/issues/89122> for more information")]
pub(crate) struct DeprecatedWhereClauseLocation {
    #[subdiagnostic]
    pub suggestion: DeprecatedWhereClauseLocationSugg,
}

#[derive(Subdiagnostic)]
pub(crate) enum DeprecatedWhereClauseLocationSugg {
    #[multipart_suggestion(
        "move it to the end of the type declaration",
        applicability = "machine-applicable"
    )]
    MoveToEnd {
        #[suggestion_part(code = "")]
        left: Span,
        #[suggestion_part(code = "{sugg}")]
        right: Span,

        sugg: String,
    },
    #[suggestion("remove this `where`", code = "", applicability = "machine-applicable")]
    RemoveWhere {
        #[primary_span]
        span: Span,
    },
}

#[derive(Diagnostic)]
#[diag("missing pattern for `...` argument")]
pub(crate) struct VarargsWithoutPattern {
    #[suggestion(
        "add a pattern for this argument",
        applicability = "machine-applicable",
        code = "_: ..."
    )]
    #[primary_span]
    pub span: Span,
}

#[derive(Diagnostic)]
#[diag(
    "an `extern \"custom\"` function can only be declared externally or defined via naked functions"
)]
pub(crate) struct AbiCustomMustBeNaked {
    #[primary_span]
    pub span: Span,
    #[suggestion(
        "convert this to an `#[unsafe(naked)]` function",
        applicability = "maybe-incorrect",
        code = "#[unsafe(naked)]\n",
        style = "short"
    )]
    pub naked_span: Span,
}

#[derive(Diagnostic)]
#[diag("an `extern \"custom\"` function cannot be marked `#[cold]`")]
pub(crate) struct AbiCustomCannotBeCold {
    #[primary_span]
    pub span: Span,

    #[suggestion(
        "remove the `#[cold]` attribute",
        applicability = "maybe-incorrect",
        code = "",
        style = "short"
    )]
    pub cold_span: Span,

    #[label("`extern \"custom\"` because of this")]
    pub abi_span: Span,
}