frontend 0.4.0

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
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
//! Errors emitted by `rustc_hir_analysis`.

// `#![no_std]`: these arrive with the standard prelude and name no path, so a `std::`
// search cannot see them - and a `#[derive]` can use them without the name appearing
// in this file at all, which is why they are not trimmed by inspection.
use alloc::borrow::ToOwned;
use alloc::boxed::Box;
use alloc::format;
use alloc::string::{String, ToString};
use alloc::vec;
use alloc::vec::Vec;

use crate::rustc_abi::ExternAbi;
use crate::rustc_errors::codes::*;
use crate::rustc_errors::{
    Applicability, Diag, DiagCtxtHandle, DiagSymbolList, Diagnostic, EmissionGuarantee, Level,
    MultiSpan, listify, msg,
};
use rustc_macros::{Diagnostic, Subdiagnostic};
use crate::rustc_middle::ty::{self, Ty};
use crate::rustc_span::{Ident, Span, Symbol};
use crate::rustc_structures::Limit;
pub(crate) mod wrong_number_of_generic_args;

mod precise_captures;
pub(crate) use precise_captures::*;

pub(crate) mod remove_or_use_generic;

#[derive(Diagnostic)]
#[diag("complex const arguments must be placed inside of a `const` block")]
pub(crate) struct ComplexConstArg {
    #[primary_span]
    pub span: Span,
}

#[derive(Diagnostic)]
#[diag("ambiguous associated {$assoc_kind} `{$assoc_ident}` in bounds of `{$qself}`")]
pub(crate) struct AmbiguousAssocItem<'a> {
    #[primary_span]
    #[label("ambiguous associated {$assoc_kind} `{$assoc_ident}`")]
    pub span: Span,
    pub assoc_kind: &'static str,
    pub assoc_ident: Ident,
    pub qself: &'a str,
}

#[derive(Diagnostic)]
#[diag("expected {$expected}, found {$got}")]
pub(crate) struct AssocKindMismatch {
    #[primary_span]
    #[label("unexpected {$got}")]
    pub span: Span,
    pub expected: &'static str,
    pub got: &'static str,
    #[label("expected a {$expected} because of this associated {$expected}")]
    pub expected_because_label: Option<Span>,
    pub assoc_kind: &'static str,
    #[note("the associated {$assoc_kind} is defined here")]
    pub def_span: Span,
    #[label("bounds are not allowed on associated constants")]
    pub bound_on_assoc_const_label: Option<Span>,
    #[subdiagnostic]
    pub wrap_in_braces_sugg: Option<AssocKindMismatchWrapInBracesSugg>,
}

#[derive(Subdiagnostic)]
#[multipart_suggestion("consider adding braces here", applicability = "maybe-incorrect")]
pub(crate) struct AssocKindMismatchWrapInBracesSugg {
    #[suggestion_part(code = "{{ ")]
    pub lo: Span,
    #[suggestion_part(code = " }}")]
    pub hi: Span,
}

#[derive(Diagnostic)]
#[diag("{$kind} `{$name}` is private", code = E0624)]
pub(crate) struct AssocItemIsPrivate {
    #[primary_span]
    #[label("private {$kind}")]
    pub span: Span,
    pub kind: &'static str,
    pub name: Ident,
    #[label("the {$kind} is defined here")]
    pub defined_here_label: Span,
}

#[derive(Diagnostic)]
#[diag("associated {$assoc_kind} `{$assoc_ident}` not found for `{$qself}`", code = E0220)]
pub(crate) struct AssocItemNotFound<'a> {
    #[primary_span]
    pub span: Span,
    pub assoc_ident: Ident,
    pub assoc_kind: &'static str,
    pub qself: &'a str,
    #[subdiagnostic]
    pub label: Option<AssocItemNotFoundLabel<'a>>,
    #[subdiagnostic]
    pub sugg: Option<AssocItemNotFoundSugg<'a>>,
    #[label("due to this macro variable")]
    pub within_macro_span: Option<Span>,
}

#[derive(Subdiagnostic)]
pub(crate) enum AssocItemNotFoundLabel<'a> {
    #[label("associated {$assoc_kind} `{$assoc_ident}` not found")]
    NotFound {
        #[primary_span]
        span: Span,
        assoc_ident: Ident,
        assoc_kind: &'static str,
    },
    #[label(
        "there is {$identically_named ->
            [true] an
            *[false] a similarly named
            } associated {$assoc_kind} `{$suggested_name}` in the trait `{$trait_name}`"
    )]
    FoundInOtherTrait {
        #[primary_span]
        span: Span,
        assoc_kind: &'static str,
        trait_name: &'a str,
        suggested_name: Symbol,
        identically_named: bool,
    },
}

#[derive(Subdiagnostic)]

pub(crate) enum AssocItemNotFoundSugg<'a> {
    #[suggestion(
        "there is an associated {$assoc_kind} with a similar name",
        code = "{suggested_name}",
        applicability = "maybe-incorrect"
    )]
    Similar {
        #[primary_span]
        span: Span,
        assoc_kind: &'static str,
        suggested_name: Symbol,
    },
    #[suggestion(
        "change the associated {$assoc_kind} name to use `{$suggested_name}` from `{$trait_name}`",
        code = "{suggested_name}",
        style = "verbose",
        applicability = "maybe-incorrect"
    )]
    SimilarInOtherTrait {
        #[primary_span]
        span: Span,
        trait_name: &'a str,
        assoc_kind: &'static str,
        suggested_name: Symbol,
    },
    #[multipart_suggestion(
        "consider fully qualifying{$identically_named ->
            [true] {\"\"}
            *[false] {\" \"}and renaming
        } the associated {$assoc_kind}",
        style = "verbose"
    )]
    SimilarInOtherTraitQPath {
        #[suggestion_part(code = "<")]
        lo: Span,
        #[suggestion_part(code = " as {trait_ref}>")]
        mi: Span,
        #[suggestion_part(code = "{suggested_name}")]
        hi: Option<Span>,
        trait_ref: String,
        suggested_name: Symbol,
        identically_named: bool,
        assoc_kind: &'static str,
        #[applicability]
        applicability: Applicability,
    },
    #[suggestion(
        "`{$qself}` has the following associated {$assoc_kind}",
        code = "{suggested_name}",
        applicability = "maybe-incorrect"
    )]
    Other {
        #[primary_span]
        span: Span,
        qself: &'a str,
        assoc_kind: &'static str,
        suggested_name: Symbol,
    },
}

#[derive(Diagnostic)]
#[diag("intrinsic has wrong number of {$descr} parameters: found {$found}, expected {$expected}", code = E0094)]
pub(crate) struct WrongNumberOfGenericArgumentsToIntrinsic<'a> {
    #[primary_span]
    #[label(
        "expected {$expected} {$descr} {$expected ->
            [one] parameter
            *[other] parameters
        }"
    )]
    pub span: Span,
    pub found: usize,
    pub expected: usize,
    pub descr: &'a str,
}

#[derive(Diagnostic)]
#[diag("unrecognized intrinsic function: `{$name}`", code = E0093)]
#[help("if you're adding an intrinsic, be sure to update `check_intrinsic_type`")]
pub(crate) struct UnrecognizedIntrinsicFunction {
    #[primary_span]
    #[label("unrecognized intrinsic")]
    pub span: Span,
    pub name: Symbol,
}

#[derive(Diagnostic)]
#[diag("lifetime parameters or bounds on {$item_kind} `{$ident}` do not match the trait declaration", code = E0195)]
pub(crate) struct LifetimesOrBoundsMismatchOnTrait {
    #[primary_span]
    #[label("lifetimes do not match {$item_kind} in trait")]
    pub span: Span,
    #[label("lifetimes in impl do not match this {$item_kind} in trait")]
    pub generics_span: Span,
    #[label("this `where` clause might not match the one in the trait")]
    pub where_span: Option<Span>,
    #[label("this bound might be missing in the impl")]
    pub bounds_span: Vec<Span>,
    pub item_kind: &'static str,
    pub ident: Ident,
}

#[derive(Diagnostic)]
#[diag("the `{$trait_}` trait may only be implemented for local structs, enums, and unions", code = E0120)]
pub(crate) struct DropImplOnWrongItem {
    #[primary_span]
    #[label("must be a struct, enum, or union in the current crate")]
    pub span: Span,
    pub trait_: Symbol,
}

#[derive(Diagnostic)]
pub(crate) enum FieldAlreadyDeclared {
    #[diag("field `{$field_name}` is already declared", code = E0124)]
    NotNested {
        field_name: Ident,
        #[primary_span]
        #[label("field already declared")]
        span: Span,
        #[label("`{$field_name}` first declared here")]
        prev_span: Span,
    },
    #[diag("field `{$field_name}` is already declared")]
    CurrentNested {
        field_name: Ident,
        #[primary_span]
        #[label("field `{$field_name}` declared in this unnamed field")]
        span: Span,
        #[note("field `{$field_name}` declared here")]
        nested_field_span: Span,
        #[subdiagnostic]
        help: FieldAlreadyDeclaredNestedHelp,
        #[label("`{$field_name}` first declared here")]
        prev_span: Span,
    },
    #[diag("field `{$field_name}` is already declared")]
    PreviousNested {
        field_name: Ident,
        #[primary_span]
        #[label("field already declared")]
        span: Span,
        #[label("`{$field_name}` first declared here in this unnamed field")]
        prev_span: Span,
        #[note("field `{$field_name}` first declared here")]
        prev_nested_field_span: Span,
        #[subdiagnostic]
        prev_help: FieldAlreadyDeclaredNestedHelp,
    },
    #[diag("field `{$field_name}` is already declared")]
    BothNested {
        field_name: Ident,
        #[primary_span]
        #[label("field `{$field_name}` declared in this unnamed field")]
        span: Span,
        #[note("field `{$field_name}` declared here")]
        nested_field_span: Span,
        #[subdiagnostic]
        help: FieldAlreadyDeclaredNestedHelp,
        #[label("`{$field_name}` first declared here in this unnamed field")]
        prev_span: Span,
        #[note("field `{$field_name}` first declared here")]
        prev_nested_field_span: Span,
        #[subdiagnostic]
        prev_help: FieldAlreadyDeclaredNestedHelp,
    },
}

#[derive(Subdiagnostic)]
#[help("fields from the type of this unnamed field are considered fields of the outer type")]
pub(crate) struct FieldAlreadyDeclaredNestedHelp {
    #[primary_span]
    pub span: Span,
}

#[derive(Diagnostic)]
#[diag("the trait `Copy` cannot be implemented for this type; the type has a destructor", code = E0184)]
pub(crate) struct CopyImplOnTypeWithDtor {
    #[primary_span]
    #[label("`Copy` not allowed on types with destructors")]
    pub span: Span,
    #[note("destructor declared here")]
    pub impl_: Span,
}

#[derive(Diagnostic)]
#[diag("the trait `Copy` cannot be implemented for this type", code = E0206)]
pub(crate) struct CopyImplOnNonAdt {
    #[primary_span]
    #[label("type is not a structure or enumeration")]
    pub span: Span,
}

#[derive(Diagnostic)]
#[diag("the trait `ConstParamTy` may not be implemented for this type")]
pub(crate) struct ConstParamTyImplOnUnsized {
    #[primary_span]
    #[label("type is not `Sized`")]
    pub span: Span,
}

#[derive(Diagnostic)]
#[diag("the trait `ConstParamTy` may not be implemented for this type")]
pub(crate) struct ConstParamTyImplOnNonAdt {
    #[primary_span]
    #[label("type is not a structure or enumeration")]
    pub span: Span,
}

#[derive(Diagnostic)]
#[diag("the trait `ConstParamTy` may not be implemented for this type")]
pub(crate) struct ConstParamTyImplOnNonExhaustive {
    #[primary_span]
    #[label("non exhaustive const params are forbidden")]
    pub defn_span: Span,
    #[label("caused by this attribute")]
    pub attr_span: Span,
}

#[derive(Diagnostic)]
#[diag("the trait `ConstParamTy` may not be implemented for this struct")]
pub(crate) struct ConstParamTyFieldVisMismatch {
    #[primary_span]
    #[label("struct fields are less visible than the struct")]
    pub span: Span,
}

#[derive(Diagnostic)]
#[diag("at least one trait is required for an object type", code = E0224)]
pub(crate) struct TraitObjectDeclaredWithNoTraits {
    #[primary_span]
    pub span: Span,
    #[label("this alias does not contain a trait")]
    pub trait_alias_span: Option<Span>,
}

#[derive(Diagnostic)]
#[diag("ambiguous lifetime bound, explicit lifetime bound required", code = E0227)]
pub(crate) struct AmbiguousLifetimeBound {
    #[primary_span]
    pub span: Span,
}

#[derive(Diagnostic)]
#[diag("associated item constraints are not allowed here", code = E0229)]
pub(crate) struct AssocItemConstraintsNotAllowedHere {
    #[primary_span]
    #[label("associated item constraint not allowed here")]
    pub span: Span,

    #[subdiagnostic]
    pub fn_trait_expansion: Option<ParenthesizedFnTraitExpansion>,
}

#[derive(Diagnostic)]
#[diag(
    "the type of the associated constant `{$assoc_const}` must not depend on {$param_category ->
        [self] `Self`
        [synthetic] `impl Trait`
        *[normal] generic parameters
    }"
)]
pub(crate) struct ParamInTyOfAssocConstBinding<'tcx> {
    #[primary_span]
    #[label(
        "its type must not depend on {$param_category ->
            [self] `Self`
            [synthetic] `impl Trait`
            *[normal] the {$param_def_kind} `{$param_name}`
        }"
    )]
    pub span: Span,
    pub assoc_const: Ident,
    pub param_name: Symbol,
    pub param_def_kind: &'static str,
    pub param_category: &'static str,
    #[label(
        "{$param_category ->
            [synthetic] the `impl Trait` is specified here
            *[normal] the {$param_def_kind} `{$param_name}` is defined here
        }"
    )]
    pub param_defined_here_label: Option<Span>,
    #[subdiagnostic]
    pub ty_note: Option<TyOfAssocConstBindingNote<'tcx>>,
}

#[derive(Subdiagnostic, Clone, Copy)]
#[note("`{$assoc_const}` has type `{$ty}`")]
pub(crate) struct TyOfAssocConstBindingNote<'tcx> {
    pub assoc_const: Ident,
    pub ty: Ty<'tcx>,
}

#[derive(Diagnostic)]
#[diag(
    "the type of the associated constant `{$assoc_const}` cannot capture late-bound generic parameters"
)]
pub(crate) struct EscapingBoundVarInTyOfAssocConstBinding<'tcx> {
    #[primary_span]
    #[label("its type cannot capture the late-bound {$var_def_kind} `{$var_name}`")]
    pub span: Span,
    pub assoc_const: Ident,
    pub var_name: Symbol,
    pub var_def_kind: &'static str,
    #[label("the late-bound {$var_def_kind} `{$var_name}` is defined here")]
    pub var_defined_here_label: Span,
    #[subdiagnostic]
    pub ty_note: Option<TyOfAssocConstBindingNote<'tcx>>,
}

#[derive(Subdiagnostic)]
#[help("parenthesized trait syntax expands to `{$expanded_type}`")]
pub(crate) struct ParenthesizedFnTraitExpansion {
    #[primary_span]
    pub span: Span,

    pub expanded_type: String,
}

#[derive(Diagnostic)]
#[diag("the value of the associated type `{$item_name}` in trait `{$def_path}` is already specified", code = E0719)]
pub(crate) struct ValueOfAssociatedStructAlreadySpecified {
    #[primary_span]
    #[label("re-bound here")]
    pub span: Span,
    #[label("`{$item_name}` bound here first")]
    pub prev_span: Span,
    pub item_name: Ident,
    pub def_path: String,
}

#[derive(Diagnostic)]
#[diag("unconstrained opaque type")]
#[note("`{$name}` must be used in combination with a concrete type within the same {$what}")]
pub(crate) struct UnconstrainedOpaqueType {
    #[primary_span]
    pub span: Span,
    pub name: Ident,
    pub what: &'static str,
}

pub(crate) struct MissingGenericParams {
    pub span: Span,
    pub def_span: Span,
    pub span_snippet: Option<String>,
    pub missing_generic_params: Vec<(Symbol, ty::GenericParamDefKind)>,
    pub empty_generic_args: bool,
}

// FIXME: This doesn't need to be a manual impl!
impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for MissingGenericParams {
    #[track_caller]
    fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, G> {
        let mut err = Diag::new(
            dcx,
            level,
            msg!(
                "the {$descr} {$parameterCount ->
                    [one] parameter
                    *[other] parameters
                } {$parameters} must be explicitly specified"
            ),
        );
        err.span(self.span);
        err.code(E0393);
        err.span_label(
            self.def_span,
            msg!(
                "{$descr} {$parameterCount ->
                    [one] parameter
                    *[other] parameters
                } {$parameters} must be specified for this"
            ),
        );

        enum Descr {
            Generic,
            Type,
            Const,
        }

        let mut descr = None;
        for (_, kind) in &self.missing_generic_params {
            descr = match (&descr, kind) {
                (None, ty::GenericParamDefKind::Type { .. }) => Some(Descr::Type),
                (None, ty::GenericParamDefKind::Const { .. }) => Some(Descr::Const),
                (Some(Descr::Type), ty::GenericParamDefKind::Const { .. })
                | (Some(Descr::Const), ty::GenericParamDefKind::Type { .. }) => {
                    Some(Descr::Generic)
                }
                _ => continue,
            }
        }

        err.arg(
            "descr",
            match descr.unwrap() {
                Descr::Generic => "generic",
                Descr::Type => "type",
                Descr::Const => "const",
            },
        );
        err.arg("parameterCount", self.missing_generic_params.len());
        err.arg(
            "parameters",
            listify(&self.missing_generic_params, |(n, _)| format!("`{n}`")).unwrap(),
        );

        let mut suggested = false;
        // Don't suggest setting the generic params if there are some already: The order is
        // tricky to get right and the user will already know what the syntax is.
        if let Some(snippet) = self.span_snippet
            && self.empty_generic_args
        {
            if snippet.ends_with('>') {
                // The user wrote `Trait<'a, T>` or similar. To provide an accurate suggestion
                // we would have to preserve the right order. For now, as clearly the user is
                // aware of the syntax, we do nothing.
            } else {
                // The user wrote `Trait`, so we don't have a type we can suggest, but at
                // least we can clue them to the correct syntax `Trait</* Term */>`.
                err.span_suggestion_verbose(
                    self.span.shrink_to_hi(),
                    msg!(
                        "explicitly specify the {$descr} {$parameterCount ->
                            [one] parameter
                            *[other] parameters
                        }"
                    ),
                    format!(
                        "<{}>",
                        self.missing_generic_params
                            .iter()
                            .map(|(n, _)| format!("/* {n} */"))
                            .collect::<Vec<_>>()
                            .join(", ")
                    ),
                    Applicability::HasPlaceholders,
                );
                suggested = true;
            }
        }
        if !suggested {
            err.span_label(
                self.span,
                msg!(
                    "missing {$parameterCount ->
                        [one] reference
                        *[other] references
                    } to {$parameters}"
                ),
            );
        }

        err.note(msg!(
            "because the parameter {$parameterCount ->
                [one] default references
                *[other] defaults reference
            } `Self`, the {$parameterCount ->
                [one] parameter
                *[other] parameters
            } must be specified on the trait object type"
        ));
        err
    }
}

#[derive(Diagnostic)]
#[diag("manual implementations of `{$trait_name}` are experimental", code = E0183)]
#[help("add `#![feature(unboxed_closures)]` to the crate attributes to enable")]
pub(crate) struct ManualImplementation {
    #[primary_span]
    #[label("manual implementations of `{$trait_name}` are experimental")]
    pub span: Span,
    pub trait_name: String,
}

#[derive(Diagnostic)]
#[diag("could not resolve generic parameters on overridden impl")]
pub(crate) struct GenericArgsOnOverriddenImpl {
    #[primary_span]
    pub span: Span,
}

#[derive(Diagnostic)]
#[diag("const `impl` for trait `{$trait_name}` which is not `const`")]
pub(crate) struct ConstImplForNonConstTrait {
    #[primary_span]
    #[label("this trait is not `const`")]
    pub trait_ref_span: Span,
    pub trait_name: String,
    #[suggestion(
        "{$suggestion_pre}mark `{$trait_name}` as `const` to allow it to have `const` implementations",
        applicability = "machine-applicable",
        code = "const ",
        style = "verbose"
    )]
    pub suggestion: Option<Span>,
    pub suggestion_pre: &'static str,
    #[note("marking a trait with `const` ensures all default method bodies are `const`")]
    pub marking: (),
    #[note("adding a non-const method body in the future would be a breaking change")]
    pub adding: (),
}

#[derive(Diagnostic)]
#[diag("`{$modifier}` can only be applied to `const` traits")]
pub(crate) struct ConstBoundForNonConstTrait {
    #[primary_span]
    #[label("can't be applied to `{$trait_name}`")]
    pub span: Span,
    pub modifier: &'static str,
    #[note("`{$trait_name}` can't be used with `{$modifier}` because it isn't `const`")]
    pub def_span: Option<Span>,
    #[suggestion(
        "{$suggestion_pre}mark `{$trait_name}` as `const` to allow it to have `const` implementations",
        applicability = "machine-applicable",
        code = "const ",
        style = "verbose"
    )]
    pub suggestion: Option<Span>,
    pub suggestion_pre: &'static str,
    pub trait_name: String,
}

#[derive(Diagnostic)]
#[diag("`Self` is not valid in the self type of an impl block")]
pub(crate) struct SelfInImplSelf {
    #[primary_span]
    pub span: MultiSpan,
    #[note("replace `Self` with a different type")]
    pub note: (),
}

#[derive(Diagnostic)]
#[diag("invalid type for variable with `#[linkage]` attribute", code = E0791)]
pub(crate) struct LinkageType {
    #[primary_span]
    pub span: Span,
}

#[derive(Diagnostic)]
#[help(
    "consider increasing the recursion limit by adding a `#![recursion_limit = \"{$suggested_limit}\"]` attribute to your crate (`{$crate_name}`)"
)]
#[diag("reached the recursion limit while auto-dereferencing `{$ty}`", code = E0055)]
pub(crate) struct AutoDerefReachedRecursionLimit<'a> {
    #[primary_span]
    #[label("deref recursion limit reached")]
    pub span: Span,
    pub ty: Ty<'a>,
    pub suggested_limit: Limit,
    pub crate_name: Symbol,
}

#[derive(Diagnostic)]
#[diag("`main` function is not allowed to have a `where` clause", code = E0646)]
pub(crate) struct WhereClauseOnMain {
    #[primary_span]
    pub span: Span,
    #[label("`main` cannot have a `where` clause")]
    pub generics_span: Option<Span>,
}

#[derive(Diagnostic)]
#[diag("`main` function is not allowed to be `#[track_caller]`")]
pub(crate) struct TrackCallerOnMain {
    #[primary_span]
    #[suggestion("remove this annotation", applicability = "maybe-incorrect", code = "")]
    pub span: Span,
    #[label("`main` function is not allowed to be `#[track_caller]`")]
    pub annotated: Span,
}

#[derive(Diagnostic)]
#[diag("`main` function is not allowed to have `#[target_feature]`")]
pub(crate) struct TargetFeatureOnMain {
    #[primary_span]
    #[label("`main` function is not allowed to have `#[target_feature]`")]
    pub main: Span,
}

#[derive(Diagnostic)]
#[diag("`main` function return type is not allowed to have generic parameters", code = E0131)]
pub(crate) struct MainFunctionReturnTypeGeneric {
    #[primary_span]
    pub span: Span,
}

#[derive(Diagnostic)]
#[diag("`main` function is not allowed to be `async`", code = E0752)]
pub(crate) struct MainFunctionAsync {
    #[primary_span]
    pub span: Span,
    #[label("`main` function is not allowed to be `async`")]
    pub asyncness: Option<Span>,
}

#[derive(Diagnostic)]
#[diag("`main` function is not allowed to have generic parameters", code = E0131)]
pub(crate) struct MainFunctionGenericParameters {
    #[primary_span]
    pub span: Span,
    #[label("`main` cannot have generic parameters")]
    pub label_span: Option<Span>,
}

#[derive(Diagnostic)]
#[diag("C-variadic functions with the {$convention} calling convention are not supported", code = E0045)]
pub(crate) struct VariadicFunctionCompatibleConvention<'a> {
    #[primary_span]
    #[label("C-variadic function must have a compatible calling convention")]
    pub span: Span,
    pub convention: &'a str,
}

#[derive(Diagnostic)]
pub(crate) enum CannotCaptureLateBound {
    #[diag("cannot capture late-bound type parameter in {$what}")]
    Type {
        #[primary_span]
        use_span: Span,
        #[label("parameter defined here")]
        def_span: Span,
        what: &'static str,
    },
    #[diag("cannot capture late-bound const parameter in {$what}")]
    Const {
        #[primary_span]
        use_span: Span,
        #[label("parameter defined here")]
        def_span: Span,
        what: &'static str,
    },
    #[diag("cannot capture late-bound lifetime in {$what}")]
    Lifetime {
        #[primary_span]
        use_span: Span,
        #[label("lifetime defined here")]
        def_span: Span,
        what: &'static str,
    },
}

#[derive(Diagnostic)]
#[diag("{$ty}")]
pub(crate) struct TypeOf<'tcx> {
    #[primary_span]
    pub span: Span,
    pub ty: Ty<'tcx>,
}

#[derive(Diagnostic)]
#[diag("field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union", code = E0740)]
pub(crate) struct InvalidUnionField {
    #[primary_span]
    pub field_span: Span,
    #[subdiagnostic]
    pub sugg: InvalidUnionFieldSuggestion,
    #[note(
        "union fields must not have drop side-effects, which is currently enforced via either `Copy` or `ManuallyDrop<...>`"
    )]
    pub note: (),
}

#[derive(Diagnostic)]
#[diag(
    "return type notation used on function that is not `async` and does not return `impl Trait`"
)]
pub(crate) struct ReturnTypeNotationOnNonRpitit<'tcx> {
    #[primary_span]
    pub span: Span,
    pub ty: Ty<'tcx>,
    #[label("this function must be `async` or return `impl Trait`")]
    pub fn_span: Option<Span>,
    #[note("function returns `{$ty}`, which is not compatible with associated type return bounds")]
    pub note: (),
}

#[derive(Subdiagnostic)]
#[multipart_suggestion(
    "wrap the field type in `ManuallyDrop<...>`",
    applicability = "machine-applicable"
)]
pub(crate) struct InvalidUnionFieldSuggestion {
    #[suggestion_part(code = "core::mem::ManuallyDrop<")]
    pub lo: Span,
    #[suggestion_part(code = ">")]
    pub hi: Span,
}

#[derive(Diagnostic)]
#[diag("return type notation is not allowed to use type equality")]
pub(crate) struct ReturnTypeNotationEqualityBound {
    #[primary_span]
    pub span: Span,
}

#[derive(Diagnostic)]
#[diag("the placeholder `_` is not allowed within types on item signatures for {$kind}", code = E0121)]
pub(crate) struct PlaceholderNotAllowedItemSignatures {
    #[primary_span]
    #[label("not allowed in type signatures")]
    pub spans: Vec<Span>,
    pub kind: String,
}

#[derive(Diagnostic)]
#[diag("cannot use the {$what} of a trait with uninferred generic parameters", code = E0212)]
pub(crate) struct AssociatedItemTraitUninferredGenericParams {
    #[primary_span]
    pub span: Span,
    #[suggestion(
        "use a fully qualified path with inferred lifetimes",
        style = "verbose",
        applicability = "maybe-incorrect",
        code = "{bound}"
    )]
    pub inferred_sugg: Option<Span>,
    pub bound: String,
    #[subdiagnostic]
    pub mpart_sugg: Option<AssociatedItemTraitUninferredGenericParamsMultipartSuggestion>,
    pub what: &'static str,
}

#[derive(Subdiagnostic)]
#[multipart_suggestion(
    "use a fully qualified path with explicit lifetimes",
    applicability = "maybe-incorrect"
)]
pub(crate) struct AssociatedItemTraitUninferredGenericParamsMultipartSuggestion {
    #[suggestion_part(code = "{first}")]
    pub fspan: Span,
    pub first: String,
    #[suggestion_part(code = "{second}")]
    pub sspan: Span,
    pub second: String,
}

#[derive(Diagnostic)]
#[diag("enum discriminant overflowed", code = E0370)]
#[note("explicitly set `{$item_name} = {$wrapped_discr}` if that is desired outcome")]
pub(crate) struct EnumDiscriminantOverflowed {
    #[primary_span]
    #[label("overflowed on value after {$discr}")]
    pub span: Span,
    pub discr: String,
    pub item_name: Ident,
    pub wrapped_discr: String,
}

#[derive(Diagnostic)]
#[diag("use of SIMD type{$snip} in FFI is highly experimental and may result in invalid code")]
#[help("add `#![feature(simd_ffi)]` to the crate attributes to enable")]
pub(crate) struct SIMDFFIHighlyExperimental {
    #[primary_span]
    pub span: Span,
    pub snip: String,
}

#[derive(Diagnostic)]
pub(crate) enum ImplNotMarkedDefault {
    #[diag("`{$ident}` specializes an item from a parent `impl`, but that item is not marked `default`", code = E0520)]
    #[note("to specialize, `{$ident}` in the parent `impl` must be marked `default`")]
    Ok {
        #[primary_span]
        #[label("cannot specialize default item `{$ident}`")]
        span: Span,
        #[label("parent `impl` is here")]
        ok_label: Span,
        ident: Ident,
    },
    #[diag("`{$ident}` specializes an item from a parent `impl`, but that item is not marked `default`", code = E0520)]
    #[note("parent implementation is in crate `{$cname}`")]
    Err {
        #[primary_span]
        span: Span,
        cname: Symbol,
        ident: Ident,
    },
}

#[derive(Diagnostic)]
#[diag("this item cannot be used as its where bounds are not satisfied for the `Self` type")]
pub(crate) struct UselessImplItem;

#[derive(Diagnostic)]
#[diag("cannot override `{$ident}` because it already has a `final` definition in the trait")]
pub(crate) struct OverridingFinalTraitFunction {
    #[primary_span]
    pub impl_span: Span,
    #[note("`{$ident}` is marked final here")]
    pub trait_span: Span,
    pub ident: Ident,
}

#[derive(Diagnostic)]
#[diag("not all trait items implemented, missing: `{$missing_items_msg}`", code = E0046)]
pub(crate) struct MissingTraitItem {
    #[primary_span]
    #[label("missing `{$missing_items_msg}` in implementation")]
    pub span: Span,
    #[subdiagnostic]
    pub missing_trait_item_label: Vec<MissingTraitItemLabel>,
    #[subdiagnostic]
    pub missing_trait_item: Vec<MissingTraitItemSuggestion>,
    #[subdiagnostic]
    pub missing_trait_item_none: Vec<MissingTraitItemSuggestionNone>,
    #[subdiagnostic]
    pub missing_trait_item_unstable: Vec<MissingTraitItemSuggestionUnstable>,
    pub missing_items_msg: String,
}

#[derive(Subdiagnostic)]
#[label("`{$item}` from trait")]
pub(crate) struct MissingTraitItemLabel {
    #[primary_span]
    pub span: Span,
    pub item: Symbol,
}

#[derive(Subdiagnostic)]
#[suggestion(
    "implement the missing item: `{$snippet}`",
    style = "tool-only",
    applicability = "has-placeholders",
    code = "{code}"
)]
pub(crate) struct MissingTraitItemSuggestion {
    #[primary_span]
    pub span: Span,
    pub code: String,
    pub snippet: String,
}

#[derive(Subdiagnostic)]
#[suggestion(
    "implement the missing item: `{$snippet}` (unstable, requires feature `{$feature}`)",
    style = "hidden",
    applicability = "has-placeholders",
    code = "{code}"
)]
pub(crate) struct MissingTraitItemSuggestionUnstable {
    #[primary_span]
    pub span: Span,
    pub code: String,
    pub snippet: String,
    pub feature: Symbol,
}

#[derive(Subdiagnostic)]
#[suggestion(
    "implement the missing item: `{$snippet}`",
    style = "hidden",
    applicability = "has-placeholders",
    code = "{code}"
)]
pub(crate) struct MissingTraitItemSuggestionNone {
    #[primary_span]
    pub span: Span,
    pub code: String,
    pub snippet: String,
}

#[derive(Diagnostic)]
#[diag("not all trait items implemented, missing one of: `{$missing_items_msg}`", code = E0046)]
pub(crate) struct MissingOneOfTraitItem {
    #[primary_span]
    #[label("missing one of `{$missing_items_msg}` in implementation")]
    pub span: Span,
    #[note("required because of this annotation")]
    pub note: Option<Span>,
    #[subdiagnostic]
    pub missing_trait_item_label: Vec<MissingTraitItemLabel>,
    #[subdiagnostic]
    pub missing_trait_item: Vec<MissingTraitItemSuggestion>,
    #[subdiagnostic]
    pub missing_trait_item_none: Vec<MissingTraitItemSuggestionNone>,
    #[subdiagnostic]
    pub missing_trait_item_unstable: Vec<MissingTraitItemSuggestionUnstable>,
    pub missing_items_msg: String,
}

#[derive(Diagnostic)]
#[diag("not all trait items implemented, missing: `{$missing_item_name}`", code = E0046)]
#[note("default implementation of `{$missing_item_name}` is unstable")]
pub(crate) struct MissingTraitItemUnstable {
    #[primary_span]
    pub span: Span,
    #[note("use of unstable library feature `{$feature}`: {$reason}")]
    pub some_note: bool,
    #[note("use of unstable library feature `{$feature}`")]
    pub none_note: bool,
    pub missing_item_name: Ident,
    pub feature: Symbol,
    pub reason: String,
}

#[derive(Diagnostic)]
#[diag("transparent enum needs exactly one variant, but has {$number}", code = E0731)]
pub(crate) struct TransparentEnumVariant {
    #[primary_span]
    #[label("needs exactly one variant, but has {$number}")]
    pub span: Span,
    #[label("variant here")]
    pub spans: Vec<Span>,
    #[label("too many variants in `{$path}`")]
    pub many: Option<Span>,
    pub number: usize,
    pub path: String,
}

#[derive(Diagnostic)]
#[diag("extern static is too large for the target architecture")]
pub(crate) struct TooLargeStatic {
    #[primary_span]
    pub span: Span,
}

#[derive(Diagnostic)]
#[diag("implementing `rustc_specialization_trait` traits is unstable")]
#[help("add `#![feature(min_specialization)]` to the crate attributes to enable")]
pub(crate) struct SpecializationTrait {
    #[primary_span]
    pub span: Span,
}

#[derive(Diagnostic)]
#[diag("trait cannot be implemented outside `{$restriction_path}`")]
pub(crate) struct ImplOfRestrictedTrait {
    #[primary_span]
    pub impl_span: Span,
    #[note("trait restricted here")]
    pub restriction_span: Span,
    pub restriction_path: String,
}

#[derive(Diagnostic)]
#[diag("implicit types in closure signatures are forbidden when `for<...>` is present")]
pub(crate) struct ClosureImplicitHrtb {
    #[primary_span]
    pub spans: Vec<Span>,
    #[label("`for<...>` is here")]
    pub for_sp: Span,
}

#[derive(Diagnostic)]
#[diag("specialization impl does not specialize any associated items")]
pub(crate) struct EmptySpecialization {
    #[primary_span]
    pub span: Span,
    #[note("impl is a specialization of this impl")]
    pub base_impl_span: Span,
}

#[derive(Diagnostic)]
#[diag("cannot specialize on `'static` lifetime")]
pub(crate) struct StaticSpecialize {
    #[primary_span]
    pub span: Span,
}

#[derive(Diagnostic)]
#[diag("negative `Drop` impls are not supported")]
pub(crate) struct NegativeDropImplPolarity {
    #[primary_span]
    pub span: Span,
}

#[derive(Diagnostic)]
pub(crate) enum ReturnTypeNotationIllegalParam {
    #[diag("return type notation is not allowed for functions that have type parameters")]
    Type {
        #[primary_span]
        span: Span,
        #[label("type parameter declared here")]
        param_span: Span,
    },
    #[diag("return type notation is not allowed for functions that have const parameters")]
    Const {
        #[primary_span]
        span: Span,
        #[label("const parameter declared here")]
        param_span: Span,
    },
}

#[derive(Diagnostic)]
pub(crate) enum LateBoundInApit {
    #[diag("`impl Trait` can only mention type parameters from an fn or impl")]
    Type {
        #[primary_span]
        span: Span,
        #[label("type parameter declared here")]
        param_span: Span,
    },
    #[diag("`impl Trait` can only mention const parameters from an fn or impl")]
    Const {
        #[primary_span]
        span: Span,
        #[label("const parameter declared here")]
        param_span: Span,
    },
    #[diag("`impl Trait` can only mention lifetimes from an fn or impl")]
    Lifetime {
        #[primary_span]
        span: Span,
        #[label("lifetime declared here")]
        param_span: Span,
    },
}

#[derive(Diagnostic)]
#[diag("unnecessary associated type bound for dyn-incompatible associated type")]
#[note(
    "this associated type has a `where Self: Sized` bound, and while the associated type can be specified, it cannot be used because trait objects are never `Sized`"
)]
pub(crate) struct UnusedAssociatedTypeBounds {
    #[suggestion("remove this bound", code = "")]
    pub span: Span,
}

#[derive(Diagnostic)]
#[diag("impl trait in impl method signature does not match trait method signature")]
#[note(
    "add `#[allow(refining_impl_trait)]` if it is intended for this to be part of the public API of this crate"
)]
#[note(
    "we are soliciting feedback, see issue #121718 <https://github.com/rust-lang/rust/issues/121718> for more information"
)]
pub(crate) struct ReturnPositionImplTraitInTraitRefined {
    #[suggestion(
        "replace the return type so that it matches the trait",
        applicability = "maybe-incorrect",
        code = "{pre}{return_ty}{post}"
    )]
    pub impl_return_span: Span,
    #[label("return type from trait method defined here")]
    pub trait_return_span: Option<Span>,
    #[label("this bound is stronger than that defined on the trait")]
    pub unmatched_bound: Option<Span>,

    pub pre: &'static str,
    pub post: &'static str,
    pub return_ty: String,
}

#[derive(Diagnostic)]
#[diag("impl trait in impl method captures fewer lifetimes than in trait")]
#[note(
    "add `#[allow(refining_impl_trait)]` if it is intended for this to be part of the public API of this crate"
)]
#[note(
    "we are soliciting feedback, see issue #121718 <https://github.com/rust-lang/rust/issues/121718> for more information"
)]
pub(crate) struct ReturnPositionImplTraitInTraitRefinedLifetimes {
    #[suggestion(
        "modify the `use<..>` bound to capture the same lifetimes that the trait does",
        applicability = "maybe-incorrect",
        code = "{suggestion}"
    )]
    pub suggestion_span: Span,
    pub suggestion: String,
}

#[derive(Diagnostic)]
#[diag("cannot define inherent `impl` for a type outside of the crate where the type is defined", code = E0390)]
#[help("consider moving this inherent impl into the crate defining the type if possible")]
pub(crate) struct InherentTyOutside {
    #[primary_span]
    #[help(
        "alternatively add `#[rustc_has_incoherent_inherent_impls]` to the type and `#[rustc_allow_incoherent_impl]` to the relevant impl items"
    )]
    pub span: Span,
}

#[derive(Diagnostic)]
#[diag("structs implementing `DispatchFromDyn` may not have `#[repr(packed)]` or `#[repr(C)]`", code = E0378)]
pub(crate) struct DispatchFromDynRepr {
    #[primary_span]
    pub span: Span,
}

#[derive(Diagnostic)]
#[diag("`derive(CoercePointee)` is only applicable to `struct`, instead of `{$kind}`", code = E0802)]
pub(crate) struct CoercePointeeNotStruct {
    #[primary_span]
    pub span: Span,
    pub kind: String,
}

#[derive(Diagnostic)]
#[diag("`derive(CoercePointee)` is only applicable to `struct`", code = E0802)]
pub(crate) struct CoercePointeeNotConcreteType {
    #[primary_span]
    pub span: Span,
}

#[derive(Diagnostic)]
#[diag("asserting applicability of `derive(CoercePointee)` on a target data is forbidden", code = E0802)]
pub(crate) struct CoercePointeeNoUserValidityAssertion {
    #[primary_span]
    pub span: Span,
}

#[derive(Diagnostic)]
#[diag("`derive(CoercePointee)` is only applicable to `struct` with `repr(transparent)` layout", code = E0802)]
pub(crate) struct CoercePointeeNotTransparent {
    #[primary_span]
    pub span: Span,
}

#[derive(Diagnostic)]
#[diag("`CoercePointee` can only be derived on `struct`s with at least one field", code = E0802)]
pub(crate) struct CoercePointeeNoField {
    #[primary_span]
    pub span: Span,
}

#[derive(Diagnostic)]
#[diag("cannot define inherent `impl` for a type outside of the crate where the type is defined", code = E0390)]
#[help("consider moving this inherent impl into the crate defining the type if possible")]
pub(crate) struct InherentTyOutsideRelevant {
    #[primary_span]
    pub span: Span,
    #[help("alternatively add `#[rustc_allow_incoherent_impl]` to the relevant impl items")]
    pub help_span: Span,
}

#[derive(Diagnostic)]
#[diag("cannot define inherent `impl` for a type outside of the crate where the type is defined", code = E0116)]
#[help(
    "consider defining a trait and implementing it for the type or using a newtype wrapper like `struct MyType(ExternalType);` and implement it"
)]
#[note(
    "for more details about the orphan rules, see <https://doc.rust-lang.org/reference/items/implementations.html?highlight=orphan#orphan-rules>"
)]
pub(crate) struct InherentTyOutsideNew {
    #[primary_span]
    #[label("impl for type defined outside of crate")]
    pub span: Span,
    #[subdiagnostic]
    pub note: Option<InherentTyOutsideNewAliasNote>,
}

#[derive(Subdiagnostic)]
#[note("`{$ty_name}` does not define a new type, only an alias of `{$alias_ty_name}` defined here")]
pub(crate) struct InherentTyOutsideNewAliasNote {
    #[primary_span]
    pub span: Span,
    pub ty_name: String,
    pub alias_ty_name: String,
}

#[derive(Diagnostic)]
#[diag("cannot define inherent `impl` for primitive types outside of `core`", code = E0390)]
#[help("consider moving this inherent impl into `core` if possible")]
pub(crate) struct InherentTyOutsidePrimitive {
    #[primary_span]
    pub span: Span,
    #[help("alternatively add `#[rustc_allow_incoherent_impl]` to the relevant impl items")]
    pub help_span: Span,
}

#[derive(Diagnostic)]
#[diag("cannot define inherent `impl` for primitive types", code = E0390)]
#[help("consider using an extension trait instead")]
pub(crate) struct InherentPrimitiveTy<'a> {
    #[primary_span]
    pub span: Span,
    #[subdiagnostic]
    pub note: Option<InherentPrimitiveTyNote<'a>>,
}

#[derive(Subdiagnostic)]
#[note(
    "you could also try moving the reference to uses of `{$subty}` (such as `self`) within the implementation"
)]
pub(crate) struct InherentPrimitiveTyNote<'a> {
    pub subty: Ty<'a>,
}

#[derive(Diagnostic)]
#[diag("cannot define inherent `impl` for a dyn auto trait", code = E0785)]
#[note("define and implement a new trait or type instead")]
pub(crate) struct InherentDyn {
    #[primary_span]
    #[label("impl requires at least one non-auto trait")]
    pub span: Span,
}

#[derive(Diagnostic)]
#[diag("no nominal type found for inherent implementation", code = E0118)]
#[note("either implement a trait on it or create a newtype to wrap it instead")]
pub(crate) struct InherentNominal {
    #[primary_span]
    #[label("impl requires a nominal type")]
    pub span: Span,
}

#[derive(Diagnostic)]
#[diag("the trait `DispatchFromDyn` may only be implemented for structs containing the field being coerced, ZST fields with 1 byte alignment that don't mention type/const generics, and nothing else", code = E0378)]
#[note("extra field `{$name}` of type `{$ty}` is not allowed")]
pub(crate) struct DispatchFromDynZST<'a> {
    #[primary_span]
    pub span: Span,
    pub name: Ident,
    pub ty: Ty<'a>,
}

#[derive(Diagnostic)]
#[diag("implementing `{$trait_name}` requires a field to be coerced", code = E0374)]
pub(crate) struct CoerceNoField {
    #[primary_span]
    pub span: Span,
    pub trait_name: &'static str,
    #[note("expected a single field to be coerced, none found")]
    pub note: bool,
}

#[derive(Diagnostic)]
#[diag("implementing `{$trait_name}` does not allow multiple fields to be coerced", code = E0375)]
pub(crate) struct CoerceMulti {
    pub trait_name: &'static str,
    #[primary_span]
    pub span: Span,
    pub number: usize,
    #[note(
        "the trait `{$trait_name}` may only be implemented when a single field is being coerced"
    )]
    pub fields: MultiSpan,
}

#[derive(Diagnostic)]
#[diag(
    "implementing `{$trait_name}` requires that a single lifetime parameter is passed between source and target"
)]
pub(crate) struct CoerceSharedNotSingleLifetimeParam {
    #[primary_span]
    pub span: Span,
    pub trait_name: &'static str,
}

#[derive(Diagnostic)]
#[diag(
    "implementing `{$trait_name}` requires exactly one lifetime argument in the reborrowed type"
)]
pub(crate) struct CoerceSharedMulti {
    #[primary_span]
    pub span: Span,
    pub trait_name: &'static str,
}

#[derive(Diagnostic)]
#[diag(
    "implementing `{$trait_name}` requires source and target to use the same reborrow lifetime \
     argument"
)]
pub(crate) struct CoerceSharedLifetimeMismatch {
    #[primary_span]
    pub span: Span,
    #[label("source reborrow lifetime")]
    pub source_lifetime_span: Option<Span>,
    #[label("target reborrow lifetime")]
    pub target_lifetime_span: Option<Span>,
    pub trait_name: &'static str,
}

#[derive(Diagnostic)]
#[diag(
    "implementing `{$trait_name}` requires corresponding fields to match, \
     be reborrowable with `CoerceShared`, or coerce a mutable reference field \
     to a shared reference field"
)]
pub(crate) struct CoerceSharedFieldMismatch<'tcx> {
    #[primary_span]
    #[label("target field `{$target_name}` has type `{$target_ty}`")]
    pub span: Span,
    #[label("source field `{$source_name}` has type `{$source_ty}`")]
    pub source_span: Span,
    #[label("required by this `CoerceShared` implementation")]
    pub impl_span: Span,
    pub source_name: Symbol,
    pub source_ty: Ty<'tcx>,
    pub target_name: Symbol,
    pub target_ty: Ty<'tcx>,
    pub trait_name: &'static str,
}

#[derive(Diagnostic)]
#[diag(
    "implementing `{$trait_name}` requires every target field to have a corresponding source field"
)]
pub(crate) struct CoerceSharedMissingField {
    #[primary_span]
    #[label("target field `{$field_name}` has no corresponding source field")]
    pub span: Span,
    #[label("source type `{$source_ty_name}` does not contain field `{$field_name}`")]
    pub source_ty_span: Span,
    pub trait_name: &'static str,
    pub source_ty_name: Symbol,
    pub field_name: Symbol,
}

#[derive(Diagnostic)]
#[diag(
    "implementing `{$trait_name}` requires source fields omitted from the target to be `Copy` or \
     `Reborrow`"
)]
pub(crate) struct CoerceSharedOmittedSourceFieldNotCopyOrReborrow<'tcx> {
    #[primary_span]
    #[label("source field `{$field_name}` has type `{$field_ty}`")]
    pub span: Span,
    #[label("required by this `CoerceShared` implementation")]
    pub impl_span: Span,
    pub trait_name: &'static str,
    pub field_name: Symbol,
    pub field_ty: Ty<'tcx>,
}

#[derive(Diagnostic)]
#[diag(
    "implementing `{$trait_name}` requires source and target structs to use the same field style"
)]
pub(crate) struct CoerceSharedFieldStyleMismatch {
    #[primary_span]
    pub span: Span,
    pub trait_name: &'static str,
}

#[derive(Diagnostic)]
#[diag(
    "implementing `{$trait_name}` requires all {$role} type fields to be accessible from the impl"
)]
pub(crate) struct CoerceSharedInaccessibleField {
    #[primary_span]
    pub span: Span,
    #[label("{$role} type `{$type_name}` has inaccessible reborrow data fields")]
    pub type_span: Span,
    pub trait_name: &'static str,
    pub role: &'static str,
    pub type_name: Symbol,
}

#[derive(Diagnostic)]
#[diag(
    "implementing `{$trait_name}` currently requires source and target to have at most one \
     non-ZST reborrow data field"
)]
#[note(
    "this is a temporary restriction until `CoerceShared` lowering supports non-trivially \
     memcpy-compatible field layouts"
)]
pub(crate) struct CoerceSharedMultipleNonZstFields {
    #[primary_span]
    #[label("in this `CoerceShared` implementation")]
    pub span: Span,
    #[label("source type has {$source_count} non-ZST reborrow data fields")]
    pub source_ty_span: Span,
    #[label("target type has {$target_count} non-ZST reborrow data fields")]
    pub target_ty_span: Span,
    pub trait_name: &'static str,
    pub source_count: usize,
    pub target_count: usize,
}

#[derive(Diagnostic)]
#[diag("the trait `{$trait_name}` may only be implemented for a coercion between structures", code = E0377)]
pub(crate) struct CoerceUnsizedNonStruct {
    #[primary_span]
    pub span: Span,
    pub trait_name: &'static str,
}

#[derive(Diagnostic)]
#[diag("only pattern types with the same pattern can be coerced between each other")]
pub(crate) struct CoerceSamePatKind {
    #[primary_span]
    pub span: Span,
    pub trait_name: &'static str,
    pub pat_a: String,
    pub pat_b: String,
}

#[derive(Diagnostic)]
#[diag("the trait `{$trait_name}` may only be implemented for a coercion between structures", code = E0377)]
pub(crate) struct CoerceSameStruct {
    #[primary_span]
    pub span: Span,
    pub trait_name: &'static str,
    #[note(
        "expected coercion between the same definition; expected `{$source_path}`, found `{$target_path}`"
    )]
    pub note: bool,
    pub source_path: String,
    pub target_path: String,
}

#[derive(Diagnostic)]
#[diag(
    "for `{$ty}` to have a valid implementation of `{$trait_name}`, it must be possible to coerce the field of type `{$field_ty}`"
)]
pub(crate) struct CoerceFieldValidity<'tcx> {
    #[primary_span]
    pub span: Span,
    pub ty: Ty<'tcx>,
    pub trait_name: &'static str,
    #[label(
        "`{$field_ty}` must be a pointer, reference, or smart pointer that is allowed to be unsized"
    )]
    pub field_span: Span,
    pub field_ty: Ty<'tcx>,
}

#[derive(Diagnostic)]
#[diag("the trait `{$trait_name}` cannot be implemented for this type", code = E0204)]
pub(crate) struct TraitCannotImplForTy {
    #[primary_span]
    pub span: Span,
    pub trait_name: String,
    #[label("this field does not implement `{$trait_name}`")]
    pub label_spans: Vec<Span>,
    #[subdiagnostic]
    pub notes: Vec<ImplForTyRequires>,
}

#[derive(Subdiagnostic)]
#[note("the `{$trait_name}` impl for `{$ty}` requires that `{$error_predicate}`")]
pub(crate) struct ImplForTyRequires {
    #[primary_span]
    pub span: MultiSpan,
    pub error_predicate: String,
    pub trait_name: String,
    pub ty: String,
}

#[derive(Diagnostic)]
#[diag("traits with a default impl, like `{$traits}`, cannot be implemented for {$problematic_kind} `{$self_ty}`", code = E0321)]
#[note(
    "a trait object implements `{$traits}` if and only if `{$traits}` is one of the trait object's trait bounds"
)]
pub(crate) struct TraitsWithDefaultImpl<'a> {
    #[primary_span]
    pub span: Span,
    pub traits: String,
    pub problematic_kind: &'a str,
    pub self_ty: Ty<'a>,
}

#[derive(Diagnostic)]
#[diag("cross-crate traits with a default impl, like `{$traits}`, can only be implemented for a struct/enum type, not `{$self_ty}`", code = E0321)]
pub(crate) struct CrossCrateTraits<'a> {
    #[primary_span]
    #[label("can't implement cross-crate trait with a default impl for non-struct/enum type")]
    pub span: Span,
    pub traits: String,
    pub self_ty: Ty<'a>,
}

#[derive(Diagnostic)]
#[diag("cross-crate traits with a default impl, like `{$traits}`, can only be implemented for a struct/enum type defined in the current crate", code = E0321)]
pub(crate) struct CrossCrateTraitsDefined {
    #[primary_span]
    #[label("can't implement cross-crate trait for type in another crate")]
    pub span: Span,
    pub traits: String,
}

#[derive(Diagnostic)]
#[diag("no variant named `{$ident}` found for enum `{$ty}`", code = E0599)]
pub struct NoVariantNamed<'tcx> {
    #[primary_span]
    pub span: Span,
    pub ident: Ident,
    pub ty: Ty<'tcx>,
}

#[derive(Diagnostic)]
#[diag("no field `{$field}` on type `{$ty}`", code = E0609)]
pub struct NoFieldOnType<'tcx> {
    #[primary_span]
    pub span: Span,
    pub ty: Ty<'tcx>,
    pub field: Ident,
}

#[derive(Diagnostic)]
pub(crate) enum OnlyCurrentTraits {
    #[diag("only traits defined in the current crate can be implemented for types defined outside of the crate", code = E0117)]
    Outside {
        #[primary_span]
        span: Span,
        #[note("impl doesn't have any local type before any uncovered type parameters")]
        #[note(
            "for more information see https://doc.rust-lang.org/reference/items/implementations.html#orphan-rules"
        )]
        #[note("define and implement a trait or new type instead")]
        note: (),
    },
    #[diag("only traits defined in the current crate can be implemented for primitive types", code = E0117)]
    Primitive {
        #[primary_span]
        span: Span,
        #[note("impl doesn't have any local type before any uncovered type parameters")]
        #[note(
            "for more information see https://doc.rust-lang.org/reference/items/implementations.html#orphan-rules"
        )]
        #[note("define and implement a trait or new type instead")]
        note: (),
    },
    #[diag("only traits defined in the current crate can be implemented for arbitrary types", code = E0117)]
    Arbitrary {
        #[primary_span]
        span: Span,
        #[note("impl doesn't have any local type before any uncovered type parameters")]
        #[note(
            "for more information see https://doc.rust-lang.org/reference/items/implementations.html#orphan-rules"
        )]
        #[note("define and implement a trait or new type instead")]
        note: (),
    },
}

#[derive(Subdiagnostic)]
#[label(
    "type alias impl trait is treated as if it were foreign, because its hidden type could be from a foreign crate"
)]
pub(crate) struct OnlyCurrentTraitsOpaque {
    #[primary_span]
    pub span: Span,
}
#[derive(Subdiagnostic)]
#[label("this is not defined in the current crate because this is a foreign trait")]
pub(crate) struct OnlyCurrentTraitsForeign {
    #[primary_span]
    pub span: Span,
}

#[derive(Subdiagnostic)]
#[label("this is not defined in the current crate because {$name} are always foreign")]
pub(crate) struct OnlyCurrentTraitsName<'a> {
    #[primary_span]
    pub span: Span,
    pub name: &'a str,
}

#[derive(Subdiagnostic)]
#[label("`{$pointer}` is not defined in the current crate because raw pointers are always foreign")]
pub(crate) struct OnlyCurrentTraitsPointer<'a> {
    #[primary_span]
    pub span: Span,
    pub pointer: Ty<'a>,
}

#[derive(Subdiagnostic)]
#[label("`{$ty}` is not defined in the current crate")]
pub(crate) struct OnlyCurrentTraitsTy<'a> {
    #[primary_span]
    pub span: Span,
    pub ty: Ty<'a>,
}

#[derive(Subdiagnostic)]
#[label("`{$name}` is not defined in the current crate")]
pub(crate) struct OnlyCurrentTraitsAdt {
    #[primary_span]
    pub span: Span,
    pub name: String,
}

#[derive(Subdiagnostic)]
#[multipart_suggestion(
    "consider introducing a new wrapper type",
    applicability = "maybe-incorrect"
)]
pub(crate) struct OnlyCurrentTraitsPointerSugg<'a> {
    #[suggestion_part(code = "WrapperType")]
    pub wrapper_span: Span,
    #[suggestion_part(code = "struct WrapperType(*{mut_key}{ptr_ty});\n\n")]
    pub(crate) struct_span: Span,
    pub mut_key: &'a str,
    pub ptr_ty: Ty<'a>,
}

#[derive(Diagnostic)]
#[diag("{$descr}")]
pub(crate) struct UnsupportedDelegation<'a> {
    #[primary_span]
    pub span: Span,
    pub descr: &'a str,
    #[label("callee defined here")]
    pub callee_span: Span,
}

#[derive(Diagnostic)]
#[diag("inferred lifetimes are not allowed in delegations as we need to inherit signature")]
pub(crate) struct ElidedLifetimesAreNotAllowedInDelegations {
    #[primary_span]
    pub span: Span,
}

#[derive(Diagnostic)]
#[diag("method should be `async` or return a future, but it is synchronous")]
pub(crate) struct MethodShouldReturnFuture {
    #[primary_span]
    pub span: Span,
    pub method_name: Ident,
    #[note("this method is `async` so it expects a future to be returned")]
    pub trait_item_span: Option<Span>,
}

#[derive(Diagnostic)]
#[diag("{$param_def_kind} `{$param_name}` is never used")]
pub(crate) struct UnusedGenericParameter {
    #[primary_span]
    #[label("unused {$param_def_kind}")]
    pub span: Span,
    pub param_name: Ident,
    pub param_def_kind: &'static str,
    #[label("`{$param_name}` is named here, but is likely unused in the containing type")]
    pub usage_spans: Vec<Span>,
    #[subdiagnostic]
    pub help: UnusedGenericParameterHelp,
    #[help(
        "if you intended `{$param_name}` to be a const parameter, use `const {$param_name}: /* Type */` instead"
    )]
    pub const_param_help: bool,
}

#[derive(Diagnostic)]
#[diag("{$param_def_kind} `{$param_name}` is only used recursively")]
pub(crate) struct RecursiveGenericParameter {
    #[primary_span]
    pub spans: Vec<Span>,
    #[label("{$param_def_kind} must be used non-recursively in the definition")]
    pub param_span: Span,
    pub param_name: Ident,
    pub param_def_kind: &'static str,
    #[subdiagnostic]
    pub help: UnusedGenericParameterHelp,
    #[note(
        "all type parameters must be used in a non-recursive way in order to constrain their variance"
    )]
    pub note: (),
}

#[derive(Subdiagnostic)]
pub(crate) enum UnusedGenericParameterHelp {
    #[help(
        "consider removing `{$param_name}`, referring to it in a field, or using a marker such as `{$phantom_data}`"
    )]
    Adt { param_name: Ident, phantom_data: String },
    #[help("consider removing `{$param_name}` or referring to it in a field")]
    AdtNoPhantomData { param_name: Ident },
    #[help("consider removing `{$param_name}` or referring to it in the body of the type alias")]
    TyAlias { param_name: Ident },
}

#[derive(Diagnostic)]
#[diag(
    "the {$param_def_kind} `{$param_name}` is not constrained by the impl trait, self type, or predicates"
)]
pub(crate) struct UnconstrainedGenericParameter {
    #[primary_span]
    #[label("unconstrained {$param_def_kind}")]
    pub span: Span,
    pub param_name: Ident,
    pub param_def_kind: &'static str,
    #[note("expressions using a const parameter must map each value to a distinct output value")]
    pub const_param_note: bool,
    #[note(
        "proving the result of expressions other than the parameter are unique is not supported"
    )]
    pub const_param_note2: bool,
}

#[derive(Diagnostic)]
#[diag("`impl Trait` cannot capture {$bad_place}", code = E0657)]
pub(crate) struct OpaqueCapturesHigherRankedLifetime {
    #[primary_span]
    pub span: MultiSpan,
    #[label("`impl Trait` implicitly captures all lifetimes in scope")]
    pub label: Option<Span>,
    #[note("lifetime declared here")]
    pub decl_span: MultiSpan,
    pub bad_place: &'static str,
}

#[derive(Subdiagnostic)]
pub(crate) enum InvalidReceiverTyHint {
    #[note(
        "`Weak` does not implement `Receiver` because it has methods that may shadow the referent; consider wrapping your `Weak` in a newtype wrapper for which you implement `Receiver`"
    )]
    Weak,
    #[note(
        "`NonNull` does not implement `Receiver` because it has methods that may shadow the referent; consider wrapping your `NonNull` in a newtype wrapper for which you implement `Receiver`"
    )]
    NonNull,
}

#[derive(Diagnostic)]
#[diag("invalid `self` parameter type: `{$receiver_ty}`", code = E0307)]
#[note("type of `self` must be `Self` or a type that dereferences to it")]
#[help(
    "consider changing to `self`, `&self`, `&mut self`, `self: Box<Self>`, `self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<P>` (where P is one of the previous types except `Self`)"
)]
pub(crate) struct InvalidReceiverTyNoArbitrarySelfTypes<'tcx> {
    #[primary_span]
    pub span: Span,
    pub receiver_ty: Ty<'tcx>,
}

#[derive(Diagnostic)]
#[diag("invalid `self` parameter type: `{$receiver_ty}`", code = E0307)]
#[note("type of `self` must be `Self` or some type implementing `Receiver`")]
#[help(
    "consider changing to `self`, `&self`, `&mut self`, or a type implementing `Receiver` such as `self: Box<Self>`, `self: Rc<Self>`, or `self: Arc<Self>`"
)]
pub(crate) struct InvalidReceiverTy<'tcx> {
    #[primary_span]
    pub span: Span,
    pub receiver_ty: Ty<'tcx>,
    #[subdiagnostic]
    pub hint: Option<InvalidReceiverTyHint>,
}

#[derive(Diagnostic)]
#[diag("invalid generic `self` parameter type: `{$receiver_ty}`", code = E0801)]
#[note("type of `self` must not be a method generic parameter type")]
#[help(
    "use a concrete type such as `self`, `&self`, `&mut self`, `self: Box<Self>`, `self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<P>` (where P is one of the previous types except `Self`)"
)]
pub(crate) struct InvalidGenericReceiverTy<'tcx> {
    #[primary_span]
    pub span: Span,
    pub receiver_ty: Ty<'tcx>,
}

#[derive(Diagnostic)]
#[diag("arguments for `{$abi}` function too large to pass via registers", code = E0798)]
#[note(
    "functions with the `{$abi}` ABI must pass all their arguments via the 4 32-bit argument registers"
)]
pub(crate) struct CmseInputsStackSpill {
    #[primary_span]
    #[label("does not fit in the available registers")]
    pub spans: Vec<Span>,
    pub abi: ExternAbi,
}

#[derive(Diagnostic)]
#[diag("return value of `{$abi}` function too large to pass via registers", code = E0798)]
#[note("functions with the `{$abi}` ABI must pass their result via the available return registers")]
#[note(
    "the result must either be a (transparently wrapped) i64, u64 or f64, or be at most 4 bytes in size"
)]
pub(crate) struct CmseOutputStackSpill {
    #[primary_span]
    #[label("this type doesn't fit in the available registers")]
    pub span: Span,
    pub abi: ExternAbi,
}

#[derive(Diagnostic)]
#[diag("generics are not allowed in `extern {$abi}` signatures", code = E0798)]
pub(crate) struct CmseGeneric {
    #[primary_span]
    pub span: Span,
    pub abi: ExternAbi,
}

#[derive(Diagnostic)]
#[diag("`impl Trait` is not allowed in `extern {$abi}` signatures", code = E0798)]
pub(crate) struct CmseImplTrait {
    #[primary_span]
    pub span: Span,
    pub abi: ExternAbi,
}

#[derive(Diagnostic)]
#[diag("return type notation not allowed in this position yet")]
pub(crate) struct BadReturnTypeNotation {
    #[primary_span]
    pub span: Span,
    #[suggestion(
        "furthermore, argument types not allowed with return type notation",
        applicability = "maybe-incorrect",
        code = "(..)",
        style = "verbose"
    )]
    pub suggestion: Option<Span>,
}

#[derive(Diagnostic)]
#[diag("trait item `{$item}` from `{$subtrait}` shadows identically named item from supertrait")]
pub(crate) struct SupertraitItemShadowing {
    pub item: Symbol,
    pub subtrait: Symbol,
    #[subdiagnostic]
    pub shadowee: SupertraitItemShadowee,
}

#[derive(Subdiagnostic)]
pub(crate) enum SupertraitItemShadowee {
    #[note("item from `{$supertrait}` is shadowed by a subtrait item")]
    Labeled {
        #[primary_span]
        span: Span,
        supertrait: Symbol,
    },
    #[note("items from several supertraits are shadowed: {$traits}")]
    Several {
        #[primary_span]
        spans: MultiSpan,
        traits: DiagSymbolList,
    },
}

#[derive(Diagnostic)]
#[diag("{$kind} binding in trait object type mentions `Self`")]
pub(crate) struct DynTraitAssocItemBindingMentionsSelf {
    #[primary_span]
    #[label("contains a mention of `Self`")]
    pub span: Span,
    pub kind: &'static str,
    #[label("this binding mentions `Self`")]
    pub binding: Span,
}

#[derive(Diagnostic)]
#[diag("`AsyncDrop` impl without `Drop` impl")]
#[help(
    "type implementing `AsyncDrop` trait must also implement `Drop` trait to be used in sync context and unwinds"
)]
pub(crate) struct AsyncDropWithoutSyncDrop {
    #[primary_span]
    pub span: Span,
}

#[derive(Diagnostic)]
#[diag("lifetime parameters or bounds of `{$ident}` do not match the declaration")]
pub(crate) struct LifetimesOrBoundsMismatchOnEii {
    #[primary_span]
    #[label("lifetimes do not match")]
    pub span: Span,
    #[label("lifetimes in impl do not match this signature")]
    pub generics_span: Span,
    #[label("this `where` clause might not match the one in the trait")]
    pub where_span: Option<Span>,
    #[label("this bound might be missing in the impl")]
    pub bounds_span: Vec<Span>,
    pub ident: Symbol,
}

#[derive(Diagnostic)]
#[diag("`{$impl_name}` cannot have generic parameters other than lifetimes")]
#[help("`#[{$eii_name}]` marks the implementation of an \"externally implementable item\"")]
pub(crate) struct EiiWithGenerics {
    #[primary_span]
    pub span: Span,
    #[label("required by this attribute")]
    pub attr: Span,
    pub eii_name: Symbol,
    pub impl_name: Symbol,
}

#[derive(Diagnostic)]
#[diag("explicit impls for the `Unpin` trait are not permitted for structurally pinned types")]
pub(crate) struct ImplUnpinForPinProjectedType {
    #[primary_span]
    #[label("impl of `Unpin` not allowed")]
    pub span: Span,
    #[help("`{$adt_name}` is structurally pinned because it is marked as `#[pin_v2]`")]
    pub adt_span: Span,
    pub adt_name: Symbol,
}

#[derive(Diagnostic)]
#[diag("`#[{$eii_name}]` must be used on a {$expected_kind}")]
pub(crate) struct EiiDefkindMismatch {
    #[primary_span]
    pub span: Span,
    pub eii_name: Symbol,
    pub expected_kind: &'static str,
}

#[derive(Diagnostic)]
#[diag("mutability does not match with the definition of`#[{$eii_name}]`")]
pub(crate) struct EiiDefkindMismatchStaticMutability {
    #[primary_span]
    pub span: Span,
    pub eii_name: Symbol,
}

#[derive(Diagnostic)]
#[diag("safety does not match with the definition of`#[{$eii_name}]`")]
pub(crate) struct EiiDefkindMismatchStaticSafety {
    #[primary_span]
    pub span: Span,
    pub eii_name: Symbol,
}

#[derive(Diagnostic)]
#[diag("conflicting implementations of `Drop::drop` and `Drop::pin_drop`")]
pub(crate) struct ConflictImplDropAndPinDrop {
    #[primary_span]
    pub span: Span,
    #[label("`drop(&mut self)` implemented here")]
    pub drop_span: Span,
    #[label("`pin_drop(&pin mut self)` implemented here")]
    pub pin_drop_span: Span,
}

#[derive(Diagnostic)]
#[diag("`{$adt_name}` must implement `pin_drop`")]
#[help("structurally pinned types must keep `Pin`'s safety contract")]
pub(crate) struct PinV2WithoutPinDrop {
    #[primary_span]
    #[suggestion(
        "implement `pin_drop` instead",
        code = "fn pin_drop(&pin mut self)",
        applicability = "maybe-incorrect"
    )]
    pub span: Span,
    #[note("`{$adt_name}` is marked `#[pin_v2]` here")]
    #[suggestion(
        "remove the `#[pin_v2]` attribute if it is not intended for structurally pinning",
        code = "",
        applicability = "maybe-incorrect"
    )]
    pub pin_v2_span: Option<Span>,
    pub adt_name: Symbol,
}

#[derive(Diagnostic)]
#[diag("`#[pin_v2]` types may not have `#[repr(packed)]`")]
#[note(
    "fields of a `#[repr(packed)]` type can be under-aligned, so a structurally pinned field may be moved to a properly aligned location, which `Pin` does not allow"
)]
pub(crate) struct PinV2OnPacked {
    #[primary_span]
    pub span: Span,
    #[note("`{$adt_name}` is marked `#[pin_v2]` here")]
    pub pin_v2_span: Option<Span>,
    pub adt_name: Symbol,
}

pub(crate) struct UncoveredTyParam<'tcx> {
    pub(crate) param: Ident,
    pub(crate) local_ty: Option<Ty<'tcx>>,
}

impl<G: EmissionGuarantee> Diagnostic<'_, G> for UncoveredTyParam<'_> {
    fn into_diag(self, dcx: DiagCtxtHandle<'_>, level: Level) -> Diag<'_, G> {
        let Self { param, local_ty } = self;

        let mut diag = Diag::new(dcx, level, "")
            .with_span(param.span)
            .with_span_label(param.span, "uncovered type parameter");
        if diag.is_error() {
            diag.code(E0210);
        }

        let note = "\
            implementing a foreign trait is only possible if \
            at least one of the types for which it is implemented is local";

        if let Some(local_ty) = local_ty {
            diag.primary_message(format!(
                "type parameter `{param}` must be covered by another type when \
                 it appears before the first local type (`{local_ty}`)"
            ));

            diag.note(format!(
                "{note},\nand no uncovered type parameters appear before that first local type"
            ));
            diag.note(
                "in this case, 'before' refers to the following order: \
                 `impl<..> ForeignTrait<T1, ..., Tn> for T0`,\n\
                 where `T0` is the first and `Tn` is the last",
            );
        } else {
            diag.primary_message(format!(
                "type parameter `{param}` must be used as an argument to \
                 some local type (e.g., `MyStruct<{param}>`)"
            ));

            diag.note(note);
            diag.note(
                "only traits defined in the current crate can be implemented for a type parameter",
            );
        }

        diag
    }
}

#[derive(Diagnostic)]
#[diag("field `{$name}` is already part of the view")]
pub(crate) struct ViewedFieldIsAlreadyPartOfTheView {
    #[primary_span]
    pub span: Span,
    pub name: Symbol,
    #[label("field `{$name}` is declared as viewed here")]
    pub previous_field_span: Span,
}

#[derive(Diagnostic)]
#[diag("only structs can be viewed")]
pub(crate) struct OnlyStructsCanBeViewedNonAdt<'tcx> {
    #[primary_span]
    #[label("type `{$ty}` cannot be viewed")]
    pub span: Span,
    pub ty: Ty<'tcx>,
}

#[derive(Diagnostic)]
#[diag("only structs can be viewed")]
pub(crate) struct OnlyStructsCanBeViewedAdt<'tcx> {
    #[primary_span]
    #[label("`{$ty}` is {$article} {$kind}, it cannot be viewed")]
    pub span: Span,
    pub ty: Ty<'tcx>,
    pub article: &'static str,
    pub kind: &'static str,
}

#[derive(Diagnostic)]
#[diag("the type of const parameters must not depend on other generic parameters", code = E0770)]
pub(crate) struct ParamInTyOfConstParam<'tcx> {
    #[primary_span]
    #[label("the type `{$ty}` must not depend on other generic parameter")]
    pub(crate) span: Span,
    pub(crate) ty: Ty<'tcx>,
}