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
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
use crate::diagnostics::{ImportSuggestion, LabelSuggestion, TypoSuggestion};
use crate::late::lifetimes::{ElisionFailureInfo, LifetimeContext};
use crate::late::{AliasPossibility, LateResolutionVisitor, RibKind};
use crate::path_names_to_string;
use crate::{CrateLint, Module, ModuleKind, ModuleOrUniformRoot};
use crate::{PathResult, PathSource, Segment};

use rustc_ast::visit::FnKind;
use rustc_ast::{
    self as ast, AssocItemKind, Expr, ExprKind, GenericParam, GenericParamKind, Item, ItemKind,
    NodeId, Path, Ty, TyKind,
};
use rustc_ast_pretty::pprust::path_segment_to_string;
use rustc_data_structures::fx::FxHashSet;
use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticBuilder};
use rustc_hir as hir;
use rustc_hir::def::Namespace::{self, *};
use rustc_hir::def::{self, CtorKind, CtorOf, DefKind};
use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
use rustc_hir::PrimTy;
use rustc_session::parse::feature_err;
use rustc_span::edition::Edition;
use rustc_span::hygiene::MacroKind;
use rustc_span::lev_distance::find_best_match_for_name;
use rustc_span::symbol::{kw, sym, Ident, Symbol};
use rustc_span::{BytePos, MultiSpan, Span, DUMMY_SP};

use std::iter;

use tracing::debug;

type Res = def::Res<ast::NodeId>;

/// A field or associated item from self type suggested in case of resolution failure.
enum AssocSuggestion {
    Field,
    MethodWithSelf,
    AssocFn,
    AssocType,
    AssocConst,
}

impl AssocSuggestion {
    fn action(&self) -> &'static str {
        match self {
            AssocSuggestion::Field => "use the available field",
            AssocSuggestion::MethodWithSelf => "call the method with the fully-qualified path",
            AssocSuggestion::AssocFn => "call the associated function",
            AssocSuggestion::AssocConst => "use the associated `const`",
            AssocSuggestion::AssocType => "use the associated type",
        }
    }
}

crate enum MissingLifetimeSpot<'tcx> {
    Generics(&'tcx hir::Generics<'tcx>),
    HigherRanked { span: Span, span_type: ForLifetimeSpanType },
    Static,
}

crate enum ForLifetimeSpanType {
    BoundEmpty,
    BoundTail,
    TypeEmpty,
    TypeTail,
}

impl ForLifetimeSpanType {
    crate fn descr(&self) -> &'static str {
        match self {
            Self::BoundEmpty | Self::BoundTail => "bound",
            Self::TypeEmpty | Self::TypeTail => "type",
        }
    }

    crate fn suggestion(&self, sugg: &str) -> String {
        match self {
            Self::BoundEmpty | Self::TypeEmpty => format!("for<{}> ", sugg),
            Self::BoundTail | Self::TypeTail => format!(", {}", sugg),
        }
    }
}

impl<'tcx> Into<MissingLifetimeSpot<'tcx>> for &'tcx hir::Generics<'tcx> {
    fn into(self) -> MissingLifetimeSpot<'tcx> {
        MissingLifetimeSpot::Generics(self)
    }
}

fn is_self_type(path: &[Segment], namespace: Namespace) -> bool {
    namespace == TypeNS && path.len() == 1 && path[0].ident.name == kw::SelfUpper
}

fn is_self_value(path: &[Segment], namespace: Namespace) -> bool {
    namespace == ValueNS && path.len() == 1 && path[0].ident.name == kw::SelfLower
}

/// Gets the stringified path for an enum from an `ImportSuggestion` for an enum variant.
fn import_candidate_to_enum_paths(suggestion: &ImportSuggestion) -> (String, String) {
    let variant_path = &suggestion.path;
    let variant_path_string = path_names_to_string(variant_path);

    let path_len = suggestion.path.segments.len();
    let enum_path = ast::Path {
        span: suggestion.path.span,
        segments: suggestion.path.segments[0..path_len - 1].to_vec(),
        tokens: None,
    };
    let enum_path_string = path_names_to_string(&enum_path);

    (variant_path_string, enum_path_string)
}

impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
    fn def_span(&self, def_id: DefId) -> Option<Span> {
        match def_id.krate {
            LOCAL_CRATE => self.r.opt_span(def_id),
            _ => Some(
                self.r
                    .session
                    .source_map()
                    .guess_head_span(self.r.cstore().get_span_untracked(def_id, self.r.session)),
            ),
        }
    }

    /// Handles error reporting for `smart_resolve_path_fragment` function.
    /// Creates base error and amends it with one short label and possibly some longer helps/notes.
    pub(crate) fn smart_resolve_report_errors(
        &mut self,
        path: &[Segment],
        span: Span,
        source: PathSource<'_>,
        res: Option<Res>,
    ) -> (DiagnosticBuilder<'a>, Vec<ImportSuggestion>) {
        let ident_span = path.last().map_or(span, |ident| ident.ident.span);
        let ns = source.namespace();
        let is_expected = &|res| source.is_expected(res);
        let is_enum_variant = &|res| matches!(res, Res::Def(DefKind::Variant, _));

        // Make the base error.
        let mut expected = source.descr_expected();
        let path_str = Segment::names_to_string(path);
        let item_str = path.last().unwrap().ident;
        let (base_msg, fallback_label, base_span, could_be_expr) = if let Some(res) = res {
            (
                format!("expected {}, found {} `{}`", expected, res.descr(), path_str),
                format!("not a {}", expected),
                span,
                match res {
                    Res::Def(DefKind::Fn, _) => {
                        // Verify whether this is a fn call or an Fn used as a type.
                        self.r
                            .session
                            .source_map()
                            .span_to_snippet(span)
                            .map(|snippet| snippet.ends_with(')'))
                            .unwrap_or(false)
                    }
                    Res::Def(
                        DefKind::Ctor(..) | DefKind::AssocFn | DefKind::Const | DefKind::AssocConst,
                        _,
                    )
                    | Res::SelfCtor(_)
                    | Res::PrimTy(_)
                    | Res::Local(_) => true,
                    _ => false,
                },
            )
        } else {
            let item_span = path.last().unwrap().ident.span;
            let (mod_prefix, mod_str) = if path.len() == 1 {
                (String::new(), "this scope".to_string())
            } else if path.len() == 2 && path[0].ident.name == kw::PathRoot {
                if self.r.session.edition() > Edition::Edition2015 {
                    // In edition 2018 onwards, the `::foo` syntax may only pull from the extern prelude
                    // which overrides all other expectations of item type
                    expected = "crate";
                    (String::new(), "the list of imported crates".to_string())
                } else {
                    (String::new(), "the crate root".to_string())
                }
            } else if path.len() == 2 && path[0].ident.name == kw::Crate {
                (String::new(), "the crate root".to_string())
            } else {
                let mod_path = &path[..path.len() - 1];
                let mod_prefix =
                    match self.resolve_path(mod_path, Some(TypeNS), false, span, CrateLint::No) {
                        PathResult::Module(ModuleOrUniformRoot::Module(module)) => module.res(),
                        _ => None,
                    }
                    .map_or_else(String::new, |res| format!("{} ", res.descr()));
                (mod_prefix, format!("`{}`", Segment::names_to_string(mod_path)))
            };
            (
                format!("cannot find {} `{}` in {}{}", expected, item_str, mod_prefix, mod_str),
                if path_str == "async" && expected.starts_with("struct") {
                    "`async` blocks are only allowed in Rust 2018 or later".to_string()
                } else {
                    format!("not found in {}", mod_str)
                },
                item_span,
                false,
            )
        };

        let code = source.error_code(res.is_some());
        let mut err = self.r.session.struct_span_err_with_code(base_span, &base_msg, code);

        if let Some(span) = self.diagnostic_metadata.current_block_could_be_bare_struct_literal {
            err.multipart_suggestion(
                "you might have meant to write a `struct` literal",
                vec![
                    (span.shrink_to_lo(), "{ SomeStruct ".to_string()),
                    (span.shrink_to_hi(), "}".to_string()),
                ],
                Applicability::HasPlaceholders,
            );
        }
        match (source, self.diagnostic_metadata.in_if_condition) {
            (PathSource::Expr(_), Some(Expr { span, kind: ExprKind::Assign(..), .. })) => {
                err.span_suggestion_verbose(
                    span.shrink_to_lo(),
                    "you might have meant to use pattern matching",
                    "let ".to_string(),
                    Applicability::MaybeIncorrect,
                );
            }
            _ => {}
        }

        let is_assoc_fn = self.self_type_is_available(span);
        // Emit help message for fake-self from other languages (e.g., `this` in Javascript).
        if ["this", "my"].contains(&&*item_str.as_str()) && is_assoc_fn {
            err.span_suggestion_short(
                span,
                "you might have meant to use `self` here instead",
                "self".to_string(),
                Applicability::MaybeIncorrect,
            );
            if !self.self_value_is_available(path[0].ident.span, span) {
                if let Some((FnKind::Fn(_, _, sig, ..), fn_span)) =
                    &self.diagnostic_metadata.current_function
                {
                    let (span, sugg) = if let Some(param) = sig.decl.inputs.get(0) {
                        (param.span.shrink_to_lo(), "&self, ")
                    } else {
                        (
                            self.r
                                .session
                                .source_map()
                                .span_through_char(*fn_span, '(')
                                .shrink_to_hi(),
                            "&self",
                        )
                    };
                    err.span_suggestion_verbose(
                        span,
                        "if you meant to use `self`, you are also missing a `self` receiver \
                         argument",
                        sugg.to_string(),
                        Applicability::MaybeIncorrect,
                    );
                }
            }
        }

        // Emit special messages for unresolved `Self` and `self`.
        if is_self_type(path, ns) {
            err.code(rustc_errors::error_code!(E0411));
            err.span_label(
                span,
                "`Self` is only available in impls, traits, and type definitions".to_string(),
            );
            return (err, Vec::new());
        }
        if is_self_value(path, ns) {
            debug!("smart_resolve_path_fragment: E0424, source={:?}", source);

            err.code(rustc_errors::error_code!(E0424));
            err.span_label(span, match source {
                PathSource::Pat => "`self` value is a keyword and may not be bound to variables or shadowed"
                                   .to_string(),
                _ => "`self` value is a keyword only available in methods with a `self` parameter"
                     .to_string(),
            });
            if let Some((fn_kind, span)) = &self.diagnostic_metadata.current_function {
                // The current function has a `self' parameter, but we were unable to resolve
                // a reference to `self`. This can only happen if the `self` identifier we
                // are resolving came from a different hygiene context.
                if fn_kind.decl().inputs.get(0).map_or(false, |p| p.is_self()) {
                    err.span_label(*span, "this function has a `self` parameter, but a macro invocation can only access identifiers it receives from parameters");
                } else {
                    let doesnt = if is_assoc_fn {
                        let (span, sugg) = fn_kind
                            .decl()
                            .inputs
                            .get(0)
                            .map(|p| (p.span.shrink_to_lo(), "&self, "))
                            .unwrap_or_else(|| {
                                (
                                    self.r
                                        .session
                                        .source_map()
                                        .span_through_char(*span, '(')
                                        .shrink_to_hi(),
                                    "&self",
                                )
                            });
                        err.span_suggestion_verbose(
                            span,
                            "add a `self` receiver parameter to make the associated `fn` a method",
                            sugg.to_string(),
                            Applicability::MaybeIncorrect,
                        );
                        "doesn't"
                    } else {
                        "can't"
                    };
                    if let Some(ident) = fn_kind.ident() {
                        err.span_label(
                            ident.span,
                            &format!("this function {} have a `self` parameter", doesnt),
                        );
                    }
                }
            }
            return (err, Vec::new());
        }

        // Try to lookup name in more relaxed fashion for better error reporting.
        let ident = path.last().unwrap().ident;
        let candidates = self
            .r
            .lookup_import_candidates(ident, ns, &self.parent_scope, is_expected)
            .drain(..)
            .filter(|ImportSuggestion { did, .. }| {
                match (did, res.and_then(|res| res.opt_def_id())) {
                    (Some(suggestion_did), Some(actual_did)) => *suggestion_did != actual_did,
                    _ => true,
                }
            })
            .collect::<Vec<_>>();
        let crate_def_id = DefId::local(CRATE_DEF_INDEX);
        if candidates.is_empty() && is_expected(Res::Def(DefKind::Enum, crate_def_id)) {
            let mut enum_candidates: Vec<_> = self
                .r
                .lookup_import_candidates(ident, ns, &self.parent_scope, is_enum_variant)
                .into_iter()
                .map(|suggestion| import_candidate_to_enum_paths(&suggestion))
                .filter(|(_, enum_ty_path)| !enum_ty_path.starts_with("std::prelude::"))
                .collect();
            if !enum_candidates.is_empty() {
                if let (PathSource::Type, Some(span)) =
                    (source, self.diagnostic_metadata.current_type_ascription.last())
                {
                    if self
                        .r
                        .session
                        .parse_sess
                        .type_ascription_path_suggestions
                        .borrow()
                        .contains(span)
                    {
                        // Already reported this issue on the lhs of the type ascription.
                        err.delay_as_bug();
                        return (err, candidates);
                    }
                }

                enum_candidates.sort();

                // Contextualize for E0412 "cannot find type", but don't belabor the point
                // (that it's a variant) for E0573 "expected type, found variant".
                let preamble = if res.is_none() {
                    let others = match enum_candidates.len() {
                        1 => String::new(),
                        2 => " and 1 other".to_owned(),
                        n => format!(" and {} others", n),
                    };
                    format!("there is an enum variant `{}`{}; ", enum_candidates[0].0, others)
                } else {
                    String::new()
                };
                let msg = format!("{}try using the variant's enum", preamble);

                err.span_suggestions(
                    span,
                    &msg,
                    enum_candidates.into_iter().map(|(_variant_path, enum_ty_path)| enum_ty_path),
                    Applicability::MachineApplicable,
                );
            }
        }
        if path.len() == 1 && self.self_type_is_available(span) {
            if let Some(candidate) = self.lookup_assoc_candidate(ident, ns, is_expected) {
                let self_is_available = self.self_value_is_available(path[0].ident.span, span);
                match candidate {
                    AssocSuggestion::Field => {
                        if self_is_available {
                            err.span_suggestion(
                                span,
                                "you might have meant to use the available field",
                                format!("self.{}", path_str),
                                Applicability::MachineApplicable,
                            );
                        } else {
                            err.span_label(span, "a field by this name exists in `Self`");
                        }
                    }
                    AssocSuggestion::MethodWithSelf if self_is_available => {
                        err.span_suggestion(
                            span,
                            "you might have meant to call the method",
                            format!("self.{}", path_str),
                            Applicability::MachineApplicable,
                        );
                    }
                    AssocSuggestion::MethodWithSelf
                    | AssocSuggestion::AssocFn
                    | AssocSuggestion::AssocConst
                    | AssocSuggestion::AssocType => {
                        err.span_suggestion(
                            span,
                            &format!("you might have meant to {}", candidate.action()),
                            format!("Self::{}", path_str),
                            Applicability::MachineApplicable,
                        );
                    }
                }
                return (err, candidates);
            }

            // If the first argument in call is `self` suggest calling a method.
            if let Some((call_span, args_span)) = self.call_has_self_arg(source) {
                let mut args_snippet = String::new();
                if let Some(args_span) = args_span {
                    if let Ok(snippet) = self.r.session.source_map().span_to_snippet(args_span) {
                        args_snippet = snippet;
                    }
                }

                err.span_suggestion(
                    call_span,
                    &format!("try calling `{}` as a method", ident),
                    format!("self.{}({})", path_str, args_snippet),
                    Applicability::MachineApplicable,
                );
                return (err, candidates);
            }
        }

        // Try Levenshtein algorithm.
        let typo_sugg = self.lookup_typo_candidate(path, ns, is_expected, span);
        // Try context-dependent help if relaxed lookup didn't work.
        if let Some(res) = res {
            if self.smart_resolve_context_dependent_help(
                &mut err,
                span,
                source,
                res,
                &path_str,
                &fallback_label,
            ) {
                // We do this to avoid losing a secondary span when we override the main error span.
                self.r.add_typo_suggestion(&mut err, typo_sugg, ident_span);
                return (err, candidates);
            }
        }

        let is_macro = base_span.from_expansion() && base_span.desugaring_kind().is_none();
        if !self.type_ascription_suggestion(&mut err, base_span) {
            let mut fallback = false;
            if let (
                PathSource::Trait(AliasPossibility::Maybe),
                Some(Res::Def(DefKind::Struct | DefKind::Enum | DefKind::Union, _)),
                false,
            ) = (source, res, is_macro)
            {
                if let Some(bounds @ [_, .., _]) = self.diagnostic_metadata.current_trait_object {
                    fallback = true;
                    let spans: Vec<Span> = bounds
                        .iter()
                        .map(|bound| bound.span())
                        .filter(|&sp| sp != base_span)
                        .collect();

                    let start_span = bounds.iter().map(|bound| bound.span()).next().unwrap();
                    // `end_span` is the end of the poly trait ref (Foo + 'baz + Bar><)
                    let end_span = bounds.iter().map(|bound| bound.span()).last().unwrap();
                    // `last_bound_span` is the last bound of the poly trait ref (Foo + >'baz< + Bar)
                    let last_bound_span = spans.last().cloned().unwrap();
                    let mut multi_span: MultiSpan = spans.clone().into();
                    for sp in spans {
                        let msg = if sp == last_bound_span {
                            format!(
                                "...because of {} bound{}",
                                if bounds.len() <= 2 { "this" } else { "these" },
                                if bounds.len() <= 2 { "" } else { "s" },
                            )
                        } else {
                            String::new()
                        };
                        multi_span.push_span_label(sp, msg);
                    }
                    multi_span.push_span_label(
                        base_span,
                        "expected this type to be a trait...".to_string(),
                    );
                    err.span_help(
                        multi_span,
                        "`+` is used to constrain a \"trait object\" type with lifetimes or \
                         auto-traits; structs and enums can't be bound in that way",
                    );
                    if bounds.iter().all(|bound| match bound {
                        ast::GenericBound::Outlives(_) => true,
                        ast::GenericBound::Trait(tr, _) => tr.span == base_span,
                    }) {
                        let mut sugg = vec![];
                        if base_span != start_span {
                            sugg.push((start_span.until(base_span), String::new()));
                        }
                        if base_span != end_span {
                            sugg.push((base_span.shrink_to_hi().to(end_span), String::new()));
                        }

                        err.multipart_suggestion(
                            "if you meant to use a type and not a trait here, remove the bounds",
                            sugg,
                            Applicability::MaybeIncorrect,
                        );
                    }
                }
            }

            fallback |= self.restrict_assoc_type_in_where_clause(span, &mut err);

            if !self.r.add_typo_suggestion(&mut err, typo_sugg, ident_span) {
                fallback = true;
                match self.diagnostic_metadata.current_let_binding {
                    Some((pat_sp, Some(ty_sp), None))
                        if ty_sp.contains(base_span) && could_be_expr =>
                    {
                        err.span_suggestion_short(
                            pat_sp.between(ty_sp),
                            "use `=` if you meant to assign",
                            " = ".to_string(),
                            Applicability::MaybeIncorrect,
                        );
                    }
                    _ => {}
                }

                // If the trait has a single item (which wasn't matched by Levenshtein), suggest it
                let suggestion = self.get_single_associated_item(&path, span, &source, is_expected);
                self.r.add_typo_suggestion(&mut err, suggestion, ident_span);
            }
            if fallback {
                // Fallback label.
                err.span_label(base_span, fallback_label);
            }
        }
        if let Some(err_code) = &err.code {
            if err_code == &rustc_errors::error_code!(E0425) {
                for label_rib in &self.label_ribs {
                    for (label_ident, node_id) in &label_rib.bindings {
                        if format!("'{}", ident) == label_ident.to_string() {
                            err.span_label(label_ident.span, "a label with a similar name exists");
                            if let PathSource::Expr(Some(Expr {
                                kind: ExprKind::Break(None, Some(_)),
                                ..
                            })) = source
                            {
                                err.span_suggestion(
                                    span,
                                    "use the similarly named label",
                                    label_ident.name.to_string(),
                                    Applicability::MaybeIncorrect,
                                );
                                // Do not lint against unused label when we suggest them.
                                self.diagnostic_metadata.unused_labels.remove(node_id);
                            }
                        }
                    }
                }
            } else if err_code == &rustc_errors::error_code!(E0412) {
                if let Some(correct) = Self::likely_rust_type(path) {
                    err.span_suggestion(
                        span,
                        "perhaps you intended to use this type",
                        correct.to_string(),
                        Applicability::MaybeIncorrect,
                    );
                }
            }
        }

        (err, candidates)
    }

    fn get_single_associated_item(
        &mut self,
        path: &[Segment],
        span: Span,
        source: &PathSource<'_>,
        filter_fn: &impl Fn(Res) -> bool,
    ) -> Option<TypoSuggestion> {
        if let crate::PathSource::TraitItem(_) = source {
            let mod_path = &path[..path.len() - 1];
            if let PathResult::Module(ModuleOrUniformRoot::Module(module)) =
                self.resolve_path(mod_path, None, false, span, CrateLint::No)
            {
                let resolutions = self.r.resolutions(module).borrow();
                let targets: Vec<_> =
                    resolutions
                        .iter()
                        .filter_map(|(key, resolution)| {
                            resolution.borrow().binding.map(|binding| binding.res()).and_then(
                                |res| if filter_fn(res) { Some((key, res)) } else { None },
                            )
                        })
                        .collect();
                if targets.len() == 1 {
                    let target = targets[0];
                    return Some(TypoSuggestion::single_item_from_res(
                        target.0.ident.name,
                        target.1,
                    ));
                }
            }
        }
        None
    }

    /// Given `where <T as Bar>::Baz: String`, suggest `where T: Bar<Baz = String>`.
    fn restrict_assoc_type_in_where_clause(
        &mut self,
        span: Span,
        err: &mut DiagnosticBuilder<'_>,
    ) -> bool {
        // Detect that we are actually in a `where` predicate.
        let (bounded_ty, bounds, where_span) =
            if let Some(ast::WherePredicate::BoundPredicate(ast::WhereBoundPredicate {
                bounded_ty,
                bound_generic_params,
                bounds,
                span,
            })) = self.diagnostic_metadata.current_where_predicate
            {
                if !bound_generic_params.is_empty() {
                    return false;
                }
                (bounded_ty, bounds, span)
            } else {
                return false;
            };

        // Confirm that the target is an associated type.
        let (ty, position, path) = if let ast::TyKind::Path(
            Some(ast::QSelf { ty, position, .. }),
            path,
        ) = &bounded_ty.kind
        {
            // use this to verify that ident is a type param.
            let partial_res = if let Ok(Some(partial_res)) = self.resolve_qpath_anywhere(
                bounded_ty.id,
                None,
                &Segment::from_path(path),
                Namespace::TypeNS,
                span,
                true,
                CrateLint::No,
            ) {
                partial_res
            } else {
                return false;
            };
            if !(matches!(
                partial_res.base_res(),
                hir::def::Res::Def(hir::def::DefKind::AssocTy, _)
            ) && partial_res.unresolved_segments() == 0)
            {
                return false;
            }
            (ty, position, path)
        } else {
            return false;
        };

        if let ast::TyKind::Path(None, type_param_path) = &ty.peel_refs().kind {
            // Confirm that the `SelfTy` is a type parameter.
            let partial_res = if let Ok(Some(partial_res)) = self.resolve_qpath_anywhere(
                bounded_ty.id,
                None,
                &Segment::from_path(type_param_path),
                Namespace::TypeNS,
                span,
                true,
                CrateLint::No,
            ) {
                partial_res
            } else {
                return false;
            };
            if !(matches!(
                partial_res.base_res(),
                hir::def::Res::Def(hir::def::DefKind::TyParam, _)
            ) && partial_res.unresolved_segments() == 0)
            {
                return false;
            }
            if let (
                [ast::PathSegment { ident: constrain_ident, args: None, .. }],
                [ast::GenericBound::Trait(poly_trait_ref, ast::TraitBoundModifier::None)],
            ) = (&type_param_path.segments[..], &bounds[..])
            {
                if let [ast::PathSegment { ident, args: None, .. }] =
                    &poly_trait_ref.trait_ref.path.segments[..]
                {
                    if ident.span == span {
                        err.span_suggestion_verbose(
                            *where_span,
                            &format!("constrain the associated type to `{}`", ident),
                            format!(
                                "{}: {}<{} = {}>",
                                self.r
                                    .session
                                    .source_map()
                                    .span_to_snippet(ty.span) // Account for `<&'a T as Foo>::Bar`.
                                    .unwrap_or_else(|_| constrain_ident.to_string()),
                                path.segments[..*position]
                                    .iter()
                                    .map(|segment| path_segment_to_string(segment))
                                    .collect::<Vec<_>>()
                                    .join("::"),
                                path.segments[*position..]
                                    .iter()
                                    .map(|segment| path_segment_to_string(segment))
                                    .collect::<Vec<_>>()
                                    .join("::"),
                                ident,
                            ),
                            Applicability::MaybeIncorrect,
                        );
                    }
                    return true;
                }
            }
        }
        false
    }

    /// Check if the source is call expression and the first argument is `self`. If true,
    /// return the span of whole call and the span for all arguments expect the first one (`self`).
    fn call_has_self_arg(&self, source: PathSource<'_>) -> Option<(Span, Option<Span>)> {
        let mut has_self_arg = None;
        if let PathSource::Expr(Some(parent)) = source {
            match &parent.kind {
                ExprKind::Call(_, args) if !args.is_empty() => {
                    let mut expr_kind = &args[0].kind;
                    loop {
                        match expr_kind {
                            ExprKind::Path(_, arg_name) if arg_name.segments.len() == 1 => {
                                if arg_name.segments[0].ident.name == kw::SelfLower {
                                    let call_span = parent.span;
                                    let tail_args_span = if args.len() > 1 {
                                        Some(Span::new(
                                            args[1].span.lo(),
                                            args.last().unwrap().span.hi(),
                                            call_span.ctxt(),
                                            None,
                                        ))
                                    } else {
                                        None
                                    };
                                    has_self_arg = Some((call_span, tail_args_span));
                                }
                                break;
                            }
                            ExprKind::AddrOf(_, _, expr) => expr_kind = &expr.kind,
                            _ => break,
                        }
                    }
                }
                _ => (),
            }
        };
        has_self_arg
    }

    fn followed_by_brace(&self, span: Span) -> (bool, Option<Span>) {
        // HACK(estebank): find a better way to figure out that this was a
        // parser issue where a struct literal is being used on an expression
        // where a brace being opened means a block is being started. Look
        // ahead for the next text to see if `span` is followed by a `{`.
        let sm = self.r.session.source_map();
        let mut sp = span;
        loop {
            sp = sm.next_point(sp);
            match sm.span_to_snippet(sp) {
                Ok(ref snippet) => {
                    if snippet.chars().any(|c| !c.is_whitespace()) {
                        break;
                    }
                }
                _ => break,
            }
        }
        let followed_by_brace = matches!(sm.span_to_snippet(sp), Ok(ref snippet) if snippet == "{");
        // In case this could be a struct literal that needs to be surrounded
        // by parentheses, find the appropriate span.
        let mut i = 0;
        let mut closing_brace = None;
        loop {
            sp = sm.next_point(sp);
            match sm.span_to_snippet(sp) {
                Ok(ref snippet) => {
                    if snippet == "}" {
                        closing_brace = Some(span.to(sp));
                        break;
                    }
                }
                _ => break,
            }
            i += 1;
            // The bigger the span, the more likely we're incorrect --
            // bound it to 100 chars long.
            if i > 100 {
                break;
            }
        }
        (followed_by_brace, closing_brace)
    }

    /// Provides context-dependent help for errors reported by the `smart_resolve_path_fragment`
    /// function.
    /// Returns `true` if able to provide context-dependent help.
    fn smart_resolve_context_dependent_help(
        &mut self,
        err: &mut DiagnosticBuilder<'a>,
        span: Span,
        source: PathSource<'_>,
        res: Res,
        path_str: &str,
        fallback_label: &str,
    ) -> bool {
        let ns = source.namespace();
        let is_expected = &|res| source.is_expected(res);

        let path_sep = |err: &mut DiagnosticBuilder<'_>, expr: &Expr| match expr.kind {
            ExprKind::Field(_, ident) => {
                err.span_suggestion(
                    expr.span,
                    "use the path separator to refer to an item",
                    format!("{}::{}", path_str, ident),
                    Applicability::MaybeIncorrect,
                );
                true
            }
            ExprKind::MethodCall(ref segment, ..) => {
                let span = expr.span.with_hi(segment.ident.span.hi());
                err.span_suggestion(
                    span,
                    "use the path separator to refer to an item",
                    format!("{}::{}", path_str, segment.ident),
                    Applicability::MaybeIncorrect,
                );
                true
            }
            _ => false,
        };

        let find_span = |source: &PathSource<'_>, err: &mut DiagnosticBuilder<'_>| {
            match source {
                PathSource::Expr(Some(Expr { span, kind: ExprKind::Call(_, _), .. }))
                | PathSource::TupleStruct(span, _) => {
                    // We want the main underline to cover the suggested code as well for
                    // cleaner output.
                    err.set_span(*span);
                    *span
                }
                _ => span,
            }
        };

        let mut bad_struct_syntax_suggestion = |def_id: DefId| {
            let (followed_by_brace, closing_brace) = self.followed_by_brace(span);

            match source {
                PathSource::Expr(Some(
                    parent @ Expr { kind: ExprKind::Field(..) | ExprKind::MethodCall(..), .. },
                )) if path_sep(err, &parent) => {}
                PathSource::Expr(
                    None
                    | Some(Expr {
                        kind:
                            ExprKind::Path(..)
                            | ExprKind::Binary(..)
                            | ExprKind::Unary(..)
                            | ExprKind::If(..)
                            | ExprKind::While(..)
                            | ExprKind::ForLoop(..)
                            | ExprKind::Match(..),
                        ..
                    }),
                ) if followed_by_brace => {
                    if let Some(sp) = closing_brace {
                        err.span_label(span, fallback_label);
                        err.multipart_suggestion(
                            "surround the struct literal with parentheses",
                            vec![
                                (sp.shrink_to_lo(), "(".to_string()),
                                (sp.shrink_to_hi(), ")".to_string()),
                            ],
                            Applicability::MaybeIncorrect,
                        );
                    } else {
                        err.span_label(
                            span, // Note the parentheses surrounding the suggestion below
                            format!(
                                "you might want to surround a struct literal with parentheses: \
                                 `({} {{ /* fields */ }})`?",
                                path_str
                            ),
                        );
                    }
                }
                PathSource::Expr(_) | PathSource::TupleStruct(..) | PathSource::Pat => {
                    let span = find_span(&source, err);
                    if let Some(span) = self.def_span(def_id) {
                        err.span_label(span, &format!("`{}` defined here", path_str));
                    }
                    let (tail, descr, applicability) = match source {
                        PathSource::Pat | PathSource::TupleStruct(..) => {
                            ("", "pattern", Applicability::MachineApplicable)
                        }
                        _ => (": val", "literal", Applicability::HasPlaceholders),
                    };
                    let (fields, applicability) = match self.r.field_names.get(&def_id) {
                        Some(fields) => (
                            fields
                                .iter()
                                .map(|f| format!("{}{}", f.node, tail))
                                .collect::<Vec<String>>()
                                .join(", "),
                            applicability,
                        ),
                        None => ("/* fields */".to_string(), Applicability::HasPlaceholders),
                    };
                    let pad = match self.r.field_names.get(&def_id) {
                        Some(fields) if fields.is_empty() => "",
                        _ => " ",
                    };
                    err.span_suggestion(
                        span,
                        &format!("use struct {} syntax instead", descr),
                        format!("{path_str} {{{pad}{fields}{pad}}}"),
                        applicability,
                    );
                }
                _ => {
                    err.span_label(span, fallback_label);
                }
            }
        };

        match (res, source) {
            (Res::Def(DefKind::Macro(MacroKind::Bang), _), _) => {
                err.span_label(span, fallback_label);
                err.span_suggestion_verbose(
                    span.shrink_to_hi(),
                    "use `!` to invoke the macro",
                    "!".to_string(),
                    Applicability::MaybeIncorrect,
                );
                if path_str == "try" && span.rust_2015() {
                    err.note("if you want the `try` keyword, you need Rust 2018 or later");
                }
            }
            (Res::Def(DefKind::TyAlias, def_id), PathSource::Trait(_)) => {
                err.span_label(span, "type aliases cannot be used as traits");
                if self.r.session.is_nightly_build() {
                    let msg = "you might have meant to use `#![feature(trait_alias)]` instead of a \
                               `type` alias";
                    if let Some(span) = self.def_span(def_id) {
                        if let Ok(snip) = self.r.session.source_map().span_to_snippet(span) {
                            // The span contains a type alias so we should be able to
                            // replace `type` with `trait`.
                            let snip = snip.replacen("type", "trait", 1);
                            err.span_suggestion(span, msg, snip, Applicability::MaybeIncorrect);
                        } else {
                            err.span_help(span, msg);
                        }
                    } else {
                        err.help(msg);
                    }
                }
            }
            (Res::Def(DefKind::Mod, _), PathSource::Expr(Some(parent))) => {
                if !path_sep(err, &parent) {
                    return false;
                }
            }
            (
                Res::Def(DefKind::Enum, def_id),
                PathSource::TupleStruct(..) | PathSource::Expr(..),
            ) => {
                if self
                    .diagnostic_metadata
                    .current_type_ascription
                    .last()
                    .map(|sp| {
                        self.r
                            .session
                            .parse_sess
                            .type_ascription_path_suggestions
                            .borrow()
                            .contains(&sp)
                    })
                    .unwrap_or(false)
                {
                    err.delay_as_bug();
                    // We already suggested changing `:` into `::` during parsing.
                    return false;
                }

                self.suggest_using_enum_variant(err, source, def_id, span);
            }
            (Res::Def(DefKind::Struct, def_id), source) if ns == ValueNS => {
                let (ctor_def, ctor_vis, fields) =
                    if let Some(struct_ctor) = self.r.struct_constructors.get(&def_id).cloned() {
                        if let PathSource::Expr(Some(parent)) = source {
                            if let ExprKind::Field(..) | ExprKind::MethodCall(..) = parent.kind {
                                bad_struct_syntax_suggestion(def_id);
                                return true;
                            }
                        }
                        struct_ctor
                    } else {
                        bad_struct_syntax_suggestion(def_id);
                        return true;
                    };

                let is_accessible = self.r.is_accessible_from(ctor_vis, self.parent_scope.module);
                if !is_expected(ctor_def) || is_accessible {
                    return true;
                }

                let field_spans = match source {
                    // e.g. `if let Enum::TupleVariant(field1, field2) = _`
                    PathSource::TupleStruct(_, pattern_spans) => {
                        err.set_primary_message(
                            "cannot match against a tuple struct which contains private fields",
                        );

                        // Use spans of the tuple struct pattern.
                        Some(Vec::from(pattern_spans))
                    }
                    // e.g. `let _ = Enum::TupleVariant(field1, field2);`
                    _ if source.is_call() => {
                        err.set_primary_message(
                            "cannot initialize a tuple struct which contains private fields",
                        );

                        // Use spans of the tuple struct definition.
                        self.r
                            .field_names
                            .get(&def_id)
                            .map(|fields| fields.iter().map(|f| f.span).collect::<Vec<_>>())
                    }
                    _ => None,
                };

                if let Some(spans) =
                    field_spans.filter(|spans| spans.len() > 0 && fields.len() == spans.len())
                {
                    let non_visible_spans: Vec<Span> = iter::zip(&fields, &spans)
                        .filter(|(vis, _)| {
                            !self.r.is_accessible_from(**vis, self.parent_scope.module)
                        })
                        .map(|(_, span)| *span)
                        .collect();

                    if non_visible_spans.len() > 0 {
                        let mut m: rustc_span::MultiSpan = non_visible_spans.clone().into();
                        non_visible_spans
                            .into_iter()
                            .for_each(|s| m.push_span_label(s, "private field".to_string()));
                        err.span_note(m, "constructor is not visible here due to private fields");
                    }

                    return true;
                }

                err.span_label(
                    span,
                    "constructor is not visible here due to private fields".to_string(),
                );
            }
            (
                Res::Def(
                    DefKind::Union | DefKind::Variant | DefKind::Ctor(_, CtorKind::Fictive),
                    def_id,
                ),
                _,
            ) if ns == ValueNS => {
                bad_struct_syntax_suggestion(def_id);
            }
            (Res::Def(DefKind::Ctor(_, CtorKind::Const), def_id), _) if ns == ValueNS => {
                match source {
                    PathSource::Expr(_) | PathSource::TupleStruct(..) | PathSource::Pat => {
                        let span = find_span(&source, err);
                        if let Some(span) = self.def_span(def_id) {
                            err.span_label(span, &format!("`{}` defined here", path_str));
                        }
                        err.span_suggestion(
                            span,
                            &"use this syntax instead",
                            format!("{path_str}"),
                            Applicability::MaybeIncorrect,
                        );
                    }
                    _ => return false,
                }
            }
            (Res::Def(DefKind::Ctor(_, CtorKind::Fn), def_id), _) if ns == ValueNS => {
                if let Some(span) = self.def_span(def_id) {
                    err.span_label(span, &format!("`{}` defined here", path_str));
                }
                let fields = self.r.field_names.get(&def_id).map_or_else(
                    || "/* fields */".to_string(),
                    |fields| vec!["_"; fields.len()].join(", "),
                );
                err.span_suggestion(
                    span,
                    "use the tuple variant pattern syntax instead",
                    format!("{}({})", path_str, fields),
                    Applicability::HasPlaceholders,
                );
            }
            (Res::SelfTy(..), _) if ns == ValueNS => {
                err.span_label(span, fallback_label);
                err.note("can't use `Self` as a constructor, you must use the implemented struct");
            }
            (Res::Def(DefKind::TyAlias | DefKind::AssocTy, _), _) if ns == ValueNS => {
                err.note("can't use a type alias as a constructor");
            }
            _ => return false,
        }
        true
    }

    /// Given the target `ident` and `kind`, search for the similarly named associated item
    /// in `self.current_trait_ref`.
    crate fn find_similarly_named_assoc_item(
        &mut self,
        ident: Symbol,
        kind: &AssocItemKind,
    ) -> Option<Symbol> {
        let module = if let Some((module, _)) = self.current_trait_ref {
            module
        } else {
            return None;
        };
        if ident == kw::Underscore {
            // We do nothing for `_`.
            return None;
        }

        let resolutions = self.r.resolutions(module);
        let targets = resolutions
            .borrow()
            .iter()
            .filter_map(|(key, res)| res.borrow().binding.map(|binding| (key, binding.res())))
            .filter(|(_, res)| match (kind, res) {
                (AssocItemKind::Const(..), Res::Def(DefKind::AssocConst, _)) => true,
                (AssocItemKind::Fn(_), Res::Def(DefKind::AssocFn, _)) => true,
                (AssocItemKind::TyAlias(..), Res::Def(DefKind::AssocTy, _)) => true,
                _ => false,
            })
            .map(|(key, _)| key.ident.name)
            .collect::<Vec<_>>();

        find_best_match_for_name(&targets, ident, None)
    }

    fn lookup_assoc_candidate<FilterFn>(
        &mut self,
        ident: Ident,
        ns: Namespace,
        filter_fn: FilterFn,
    ) -> Option<AssocSuggestion>
    where
        FilterFn: Fn(Res) -> bool,
    {
        fn extract_node_id(t: &Ty) -> Option<NodeId> {
            match t.kind {
                TyKind::Path(None, _) => Some(t.id),
                TyKind::Rptr(_, ref mut_ty) => extract_node_id(&mut_ty.ty),
                // This doesn't handle the remaining `Ty` variants as they are not
                // that commonly the self_type, it might be interesting to provide
                // support for those in future.
                _ => None,
            }
        }

        // Fields are generally expected in the same contexts as locals.
        if filter_fn(Res::Local(ast::DUMMY_NODE_ID)) {
            if let Some(node_id) =
                self.diagnostic_metadata.current_self_type.as_ref().and_then(extract_node_id)
            {
                // Look for a field with the same name in the current self_type.
                if let Some(resolution) = self.r.partial_res_map.get(&node_id) {
                    match resolution.base_res() {
                        Res::Def(DefKind::Struct | DefKind::Union, did)
                            if resolution.unresolved_segments() == 0 =>
                        {
                            if let Some(field_names) = self.r.field_names.get(&did) {
                                if field_names
                                    .iter()
                                    .any(|&field_name| ident.name == field_name.node)
                                {
                                    return Some(AssocSuggestion::Field);
                                }
                            }
                        }
                        _ => {}
                    }
                }
            }
        }

        if let Some(items) = self.diagnostic_metadata.current_trait_assoc_items {
            for assoc_item in items {
                if assoc_item.ident == ident {
                    return Some(match &assoc_item.kind {
                        ast::AssocItemKind::Const(..) => AssocSuggestion::AssocConst,
                        ast::AssocItemKind::Fn(box ast::FnKind(_, sig, ..))
                            if sig.decl.has_self() =>
                        {
                            AssocSuggestion::MethodWithSelf
                        }
                        ast::AssocItemKind::Fn(..) => AssocSuggestion::AssocFn,
                        ast::AssocItemKind::TyAlias(..) => AssocSuggestion::AssocType,
                        ast::AssocItemKind::MacCall(_) => continue,
                    });
                }
            }
        }

        // Look for associated items in the current trait.
        if let Some((module, _)) = self.current_trait_ref {
            if let Ok(binding) = self.r.resolve_ident_in_module(
                ModuleOrUniformRoot::Module(module),
                ident,
                ns,
                &self.parent_scope,
                false,
                module.span,
            ) {
                let res = binding.res();
                if filter_fn(res) {
                    if self.r.has_self.contains(&res.def_id()) {
                        return Some(AssocSuggestion::MethodWithSelf);
                    } else {
                        match res {
                            Res::Def(DefKind::AssocFn, _) => return Some(AssocSuggestion::AssocFn),
                            Res::Def(DefKind::AssocConst, _) => {
                                return Some(AssocSuggestion::AssocConst);
                            }
                            Res::Def(DefKind::AssocTy, _) => {
                                return Some(AssocSuggestion::AssocType);
                            }
                            _ => {}
                        }
                    }
                }
            }
        }

        None
    }

    fn lookup_typo_candidate(
        &mut self,
        path: &[Segment],
        ns: Namespace,
        filter_fn: &impl Fn(Res) -> bool,
        span: Span,
    ) -> Option<TypoSuggestion> {
        let mut names = Vec::new();
        if path.len() == 1 {
            // Search in lexical scope.
            // Walk backwards up the ribs in scope and collect candidates.
            for rib in self.ribs[ns].iter().rev() {
                // Locals and type parameters
                for (ident, binding) in &rib.bindings {
                    let res = binding.res();
                    if filter_fn(res) {
                        names.push(TypoSuggestion::typo_from_res(ident.name, res));
                    }
                }
                // Items in scope
                if let RibKind::ModuleRibKind(module) = rib.kind {
                    // Items from this module
                    self.r.add_module_candidates(module, &mut names, &filter_fn);

                    if let ModuleKind::Block(..) = module.kind {
                        // We can see through blocks
                    } else {
                        // Items from the prelude
                        if !module.no_implicit_prelude {
                            let extern_prelude = self.r.extern_prelude.clone();
                            names.extend(extern_prelude.iter().flat_map(|(ident, _)| {
                                self.r.crate_loader.maybe_process_path_extern(ident.name).and_then(
                                    |crate_id| {
                                        let crate_mod = Res::Def(
                                            DefKind::Mod,
                                            DefId { krate: crate_id, index: CRATE_DEF_INDEX },
                                        );

                                        if filter_fn(crate_mod) {
                                            Some(TypoSuggestion::typo_from_res(
                                                ident.name, crate_mod,
                                            ))
                                        } else {
                                            None
                                        }
                                    },
                                )
                            }));

                            if let Some(prelude) = self.r.prelude {
                                self.r.add_module_candidates(prelude, &mut names, &filter_fn);
                            }
                        }
                        break;
                    }
                }
            }
            // Add primitive types to the mix
            if filter_fn(Res::PrimTy(PrimTy::Bool)) {
                names.extend(PrimTy::ALL.iter().map(|prim_ty| {
                    TypoSuggestion::typo_from_res(prim_ty.name(), Res::PrimTy(*prim_ty))
                }))
            }
        } else {
            // Search in module.
            let mod_path = &path[..path.len() - 1];
            if let PathResult::Module(module) =
                self.resolve_path(mod_path, Some(TypeNS), false, span, CrateLint::No)
            {
                if let ModuleOrUniformRoot::Module(module) = module {
                    self.r.add_module_candidates(module, &mut names, &filter_fn);
                }
            }
        }

        let name = path[path.len() - 1].ident.name;
        // Make sure error reporting is deterministic.
        names.sort_by_cached_key(|suggestion| suggestion.candidate.as_str());

        match find_best_match_for_name(
            &names.iter().map(|suggestion| suggestion.candidate).collect::<Vec<Symbol>>(),
            name,
            None,
        ) {
            Some(found) if found != name => {
                names.into_iter().find(|suggestion| suggestion.candidate == found)
            }
            _ => None,
        }
    }

    // Returns the name of the Rust type approximately corresponding to
    // a type name in another programming language.
    fn likely_rust_type(path: &[Segment]) -> Option<Symbol> {
        let name = path[path.len() - 1].ident.as_str();
        // Common Java types
        Some(match &*name {
            "byte" => sym::u8, // In Java, bytes are signed, but in practice one almost always wants unsigned bytes.
            "short" => sym::i16,
            "boolean" => sym::bool,
            "int" => sym::i32,
            "long" => sym::i64,
            "float" => sym::f32,
            "double" => sym::f64,
            _ => return None,
        })
    }

    /// Only used in a specific case of type ascription suggestions
    fn get_colon_suggestion_span(&self, start: Span) -> Span {
        let sm = self.r.session.source_map();
        start.to(sm.next_point(start))
    }

    fn type_ascription_suggestion(&self, err: &mut DiagnosticBuilder<'_>, base_span: Span) -> bool {
        let sm = self.r.session.source_map();
        let base_snippet = sm.span_to_snippet(base_span);
        if let Some(&sp) = self.diagnostic_metadata.current_type_ascription.last() {
            if let Ok(snippet) = sm.span_to_snippet(sp) {
                let len = snippet.trim_end().len() as u32;
                if snippet.trim() == ":" {
                    let colon_sp =
                        sp.with_lo(sp.lo() + BytePos(len - 1)).with_hi(sp.lo() + BytePos(len));
                    let mut show_label = true;
                    if sm.is_multiline(sp) {
                        err.span_suggestion_short(
                            colon_sp,
                            "maybe you meant to write `;` here",
                            ";".to_string(),
                            Applicability::MaybeIncorrect,
                        );
                    } else {
                        let after_colon_sp =
                            self.get_colon_suggestion_span(colon_sp.shrink_to_hi());
                        if snippet.len() == 1 {
                            // `foo:bar`
                            err.span_suggestion(
                                colon_sp,
                                "maybe you meant to write a path separator here",
                                "::".to_string(),
                                Applicability::MaybeIncorrect,
                            );
                            show_label = false;
                            if !self
                                .r
                                .session
                                .parse_sess
                                .type_ascription_path_suggestions
                                .borrow_mut()
                                .insert(colon_sp)
                            {
                                err.delay_as_bug();
                            }
                        }
                        if let Ok(base_snippet) = base_snippet {
                            let mut sp = after_colon_sp;
                            for _ in 0..100 {
                                // Try to find an assignment
                                sp = sm.next_point(sp);
                                let snippet = sm.span_to_snippet(sp.to(sm.next_point(sp)));
                                match snippet {
                                    Ok(ref x) if x.as_str() == "=" => {
                                        err.span_suggestion(
                                            base_span,
                                            "maybe you meant to write an assignment here",
                                            format!("let {}", base_snippet),
                                            Applicability::MaybeIncorrect,
                                        );
                                        show_label = false;
                                        break;
                                    }
                                    Ok(ref x) if x.as_str() == "\n" => break,
                                    Err(_) => break,
                                    Ok(_) => {}
                                }
                            }
                        }
                    }
                    if show_label {
                        err.span_label(
                            base_span,
                            "expecting a type here because of type ascription",
                        );
                    }
                    return show_label;
                }
            }
        }
        false
    }

    fn find_module(&mut self, def_id: DefId) -> Option<(Module<'a>, ImportSuggestion)> {
        let mut result = None;
        let mut seen_modules = FxHashSet::default();
        let mut worklist = vec![(self.r.graph_root, Vec::new())];

        while let Some((in_module, path_segments)) = worklist.pop() {
            // abort if the module is already found
            if result.is_some() {
                break;
            }

            in_module.for_each_child(self.r, |_, ident, _, name_binding| {
                // abort if the module is already found or if name_binding is private external
                if result.is_some() || !name_binding.vis.is_visible_locally() {
                    return;
                }
                if let Some(module) = name_binding.module() {
                    // form the path
                    let mut path_segments = path_segments.clone();
                    path_segments.push(ast::PathSegment::from_ident(ident));
                    let module_def_id = module.def_id();
                    if module_def_id == def_id {
                        let path =
                            Path { span: name_binding.span, segments: path_segments, tokens: None };
                        result = Some((
                            module,
                            ImportSuggestion {
                                did: Some(def_id),
                                descr: "module",
                                path,
                                accessible: true,
                            },
                        ));
                    } else {
                        // add the module to the lookup
                        if seen_modules.insert(module_def_id) {
                            worklist.push((module, path_segments));
                        }
                    }
                }
            });
        }

        result
    }

    fn collect_enum_ctors(&mut self, def_id: DefId) -> Option<Vec<(Path, DefId, CtorKind)>> {
        self.find_module(def_id).map(|(enum_module, enum_import_suggestion)| {
            let mut variants = Vec::new();
            enum_module.for_each_child(self.r, |_, ident, _, name_binding| {
                if let Res::Def(DefKind::Ctor(CtorOf::Variant, kind), def_id) = name_binding.res() {
                    let mut segms = enum_import_suggestion.path.segments.clone();
                    segms.push(ast::PathSegment::from_ident(ident));
                    let path = Path { span: name_binding.span, segments: segms, tokens: None };
                    variants.push((path, def_id, kind));
                }
            });
            variants
        })
    }

    /// Adds a suggestion for using an enum's variant when an enum is used instead.
    fn suggest_using_enum_variant(
        &mut self,
        err: &mut DiagnosticBuilder<'a>,
        source: PathSource<'_>,
        def_id: DefId,
        span: Span,
    ) {
        let variants = match self.collect_enum_ctors(def_id) {
            Some(variants) => variants,
            None => {
                err.note("you might have meant to use one of the enum's variants");
                return;
            }
        };

        let suggest_only_tuple_variants =
            matches!(source, PathSource::TupleStruct(..)) || source.is_call();
        if suggest_only_tuple_variants {
            // Suggest only tuple variants regardless of whether they have fields and do not
            // suggest path with added parentheses.
            let mut suggestable_variants = variants
                .iter()
                .filter(|(.., kind)| *kind == CtorKind::Fn)
                .map(|(variant, ..)| path_names_to_string(variant))
                .collect::<Vec<_>>();

            let non_suggestable_variant_count = variants.len() - suggestable_variants.len();

            let source_msg = if source.is_call() {
                "to construct"
            } else if matches!(source, PathSource::TupleStruct(..)) {
                "to match against"
            } else {
                unreachable!()
            };

            if !suggestable_variants.is_empty() {
                let msg = if non_suggestable_variant_count == 0 && suggestable_variants.len() == 1 {
                    format!("try {} the enum's variant", source_msg)
                } else {
                    format!("try {} one of the enum's variants", source_msg)
                };

                err.span_suggestions(
                    span,
                    &msg,
                    suggestable_variants.drain(..),
                    Applicability::MaybeIncorrect,
                );
            }

            // If the enum has no tuple variants..
            if non_suggestable_variant_count == variants.len() {
                err.help(&format!("the enum has no tuple variants {}", source_msg));
            }

            // If there are also non-tuple variants..
            if non_suggestable_variant_count == 1 {
                err.help(&format!(
                    "you might have meant {} the enum's non-tuple variant",
                    source_msg
                ));
            } else if non_suggestable_variant_count >= 1 {
                err.help(&format!(
                    "you might have meant {} one of the enum's non-tuple variants",
                    source_msg
                ));
            }
        } else {
            let needs_placeholder = |def_id: DefId, kind: CtorKind| {
                let has_no_fields = self.r.field_names.get(&def_id).map_or(false, |f| f.is_empty());
                match kind {
                    CtorKind::Const => false,
                    CtorKind::Fn | CtorKind::Fictive if has_no_fields => false,
                    _ => true,
                }
            };

            let mut suggestable_variants = variants
                .iter()
                .filter(|(_, def_id, kind)| !needs_placeholder(*def_id, *kind))
                .map(|(variant, _, kind)| (path_names_to_string(variant), kind))
                .map(|(variant, kind)| match kind {
                    CtorKind::Const => variant,
                    CtorKind::Fn => format!("({}())", variant),
                    CtorKind::Fictive => format!("({} {{}})", variant),
                })
                .collect::<Vec<_>>();

            if !suggestable_variants.is_empty() {
                let msg = if suggestable_variants.len() == 1 {
                    "you might have meant to use the following enum variant"
                } else {
                    "you might have meant to use one of the following enum variants"
                };

                err.span_suggestions(
                    span,
                    msg,
                    suggestable_variants.drain(..),
                    Applicability::MaybeIncorrect,
                );
            }

            let mut suggestable_variants_with_placeholders = variants
                .iter()
                .filter(|(_, def_id, kind)| needs_placeholder(*def_id, *kind))
                .map(|(variant, _, kind)| (path_names_to_string(variant), kind))
                .filter_map(|(variant, kind)| match kind {
                    CtorKind::Fn => Some(format!("({}(/* fields */))", variant)),
                    CtorKind::Fictive => Some(format!("({} {{ /* fields */ }})", variant)),
                    _ => None,
                })
                .collect::<Vec<_>>();

            if !suggestable_variants_with_placeholders.is_empty() {
                let msg = match (
                    suggestable_variants.is_empty(),
                    suggestable_variants_with_placeholders.len(),
                ) {
                    (true, 1) => "the following enum variant is available",
                    (true, _) => "the following enum variants are available",
                    (false, 1) => "alternatively, the following enum variant is available",
                    (false, _) => "alternatively, the following enum variants are also available",
                };

                err.span_suggestions(
                    span,
                    msg,
                    suggestable_variants_with_placeholders.drain(..),
                    Applicability::HasPlaceholders,
                );
            }
        };

        if def_id.is_local() {
            if let Some(span) = self.def_span(def_id) {
                err.span_note(span, "the enum is defined here");
            }
        }
    }

    crate fn report_missing_type_error(
        &self,
        path: &[Segment],
    ) -> Option<(Span, &'static str, String, Applicability)> {
        let (ident, span) = match path {
            [segment] if !segment.has_generic_args => {
                (segment.ident.to_string(), segment.ident.span)
            }
            _ => return None,
        };
        let mut iter = ident.chars().map(|c| c.is_uppercase());
        let single_uppercase_char =
            matches!(iter.next(), Some(true)) && matches!(iter.next(), None);
        if !self.diagnostic_metadata.currently_processing_generics && !single_uppercase_char {
            return None;
        }
        match (self.diagnostic_metadata.current_item, single_uppercase_char, self.diagnostic_metadata.currently_processing_generics) {
            (Some(Item { kind: ItemKind::Fn(..), ident, .. }), _, _) if ident.name == sym::main => {
                // Ignore `fn main()` as we don't want to suggest `fn main<T>()`
            }
            (
                Some(Item {
                    kind:
                        kind @ ItemKind::Fn(..)
                        | kind @ ItemKind::Enum(..)
                        | kind @ ItemKind::Struct(..)
                        | kind @ ItemKind::Union(..),
                    ..
                }),
                true, _
            )
            // Without the 2nd `true`, we'd suggest `impl <T>` for `impl T` when a type `T` isn't found
            | (Some(Item { kind: kind @ ItemKind::Impl(..), .. }), true, true)
            | (Some(Item { kind, .. }), false, _) => {
                // Likely missing type parameter.
                if let Some(generics) = kind.generics() {
                    if span.overlaps(generics.span) {
                        // Avoid the following:
                        // error[E0405]: cannot find trait `A` in this scope
                        //  --> $DIR/typo-suggestion-named-underscore.rs:CC:LL
                        //   |
                        // L | fn foo<T: A>(x: T) {} // Shouldn't suggest underscore
                        //   |           ^- help: you might be missing a type parameter: `, A`
                        //   |           |
                        //   |           not found in this scope
                        return None;
                    }
                    let msg = "you might be missing a type parameter";
                    let (span, sugg) = if let [.., param] = &generics.params[..] {
                        let span = if let [.., bound] = &param.bounds[..] {
                            bound.span()
                        } else if let GenericParam {
                            kind: GenericParamKind::Const { ty, kw_span: _, default  }, ..
                        } = param {
                            default.as_ref().map(|def| def.value.span).unwrap_or(ty.span)
                        } else {
                            param.ident.span
                        };
                        (span, format!(", {}", ident))
                    } else {
                        (generics.span, format!("<{}>", ident))
                    };
                    // Do not suggest if this is coming from macro expansion.
                    if !span.from_expansion() {
                        return Some((
                            span.shrink_to_hi(),
                            msg,
                            sugg,
                            Applicability::MaybeIncorrect,
                        ));
                    }
                }
            }
            _ => {}
        }
        None
    }

    /// Given the target `label`, search the `rib_index`th label rib for similarly named labels,
    /// optionally returning the closest match and whether it is reachable.
    crate fn suggestion_for_label_in_rib(
        &self,
        rib_index: usize,
        label: Ident,
    ) -> Option<LabelSuggestion> {
        // Are ribs from this `rib_index` within scope?
        let within_scope = self.is_label_valid_from_rib(rib_index);

        let rib = &self.label_ribs[rib_index];
        let names = rib
            .bindings
            .iter()
            .filter(|(id, _)| id.span.ctxt() == label.span.ctxt())
            .map(|(id, _)| id.name)
            .collect::<Vec<Symbol>>();

        find_best_match_for_name(&names, label.name, None).map(|symbol| {
            // Upon finding a similar name, get the ident that it was from - the span
            // contained within helps make a useful diagnostic. In addition, determine
            // whether this candidate is within scope.
            let (ident, _) = rib.bindings.iter().find(|(ident, _)| ident.name == symbol).unwrap();
            (*ident, within_scope)
        })
    }
}

impl<'tcx> LifetimeContext<'_, 'tcx> {
    crate fn report_missing_lifetime_specifiers(
        &self,
        spans: Vec<Span>,
        count: usize,
    ) -> DiagnosticBuilder<'tcx> {
        struct_span_err!(
            self.tcx.sess,
            spans,
            E0106,
            "missing lifetime specifier{}",
            pluralize!(count)
        )
    }

    crate fn emit_undeclared_lifetime_error(&self, lifetime_ref: &hir::Lifetime) {
        let mut err = struct_span_err!(
            self.tcx.sess,
            lifetime_ref.span,
            E0261,
            "use of undeclared lifetime name `{}`",
            lifetime_ref
        );
        err.span_label(lifetime_ref.span, "undeclared lifetime");
        let mut suggests_in_band = false;
        let mut suggest_note = true;
        for missing in &self.missing_named_lifetime_spots {
            match missing {
                MissingLifetimeSpot::Generics(generics) => {
                    let (span, sugg) = if let Some(param) = generics.params.iter().find(|p| {
                        !matches!(
                            p.kind,
                            hir::GenericParamKind::Type {
                                synthetic: Some(hir::SyntheticTyParamKind::ImplTrait),
                                ..
                            } | hir::GenericParamKind::Lifetime {
                                kind: hir::LifetimeParamKind::Elided,
                            }
                        )
                    }) {
                        (param.span.shrink_to_lo(), format!("{}, ", lifetime_ref))
                    } else {
                        suggests_in_band = true;
                        (generics.span, format!("<{}>", lifetime_ref))
                    };
                    if !span.from_expansion() {
                        err.span_suggestion(
                            span,
                            &format!("consider introducing lifetime `{}` here", lifetime_ref),
                            sugg,
                            Applicability::MaybeIncorrect,
                        );
                    } else if suggest_note {
                        suggest_note = false; // Avoid displaying the same help multiple times.
                        err.span_label(
                            span,
                            &format!(
                                "lifetime `{}` is missing in item created through this procedural \
                                 macro",
                                lifetime_ref,
                            ),
                        );
                    }
                }
                MissingLifetimeSpot::HigherRanked { span, span_type } => {
                    err.span_suggestion(
                        *span,
                        &format!(
                            "consider making the {} lifetime-generic with a new `{}` lifetime",
                            span_type.descr(),
                            lifetime_ref
                        ),
                        span_type.suggestion(&lifetime_ref.to_string()),
                        Applicability::MaybeIncorrect,
                    );
                    err.note(
                        "for more information on higher-ranked polymorphism, visit \
                         https://doc.rust-lang.org/nomicon/hrtb.html",
                    );
                }
                _ => {}
            }
        }
        if self.tcx.sess.is_nightly_build()
            && !self.tcx.features().in_band_lifetimes
            && suggests_in_band
        {
            err.help(
                "if you want to experiment with in-band lifetime bindings, \
                 add `#![feature(in_band_lifetimes)]` to the crate attributes",
            );
        }
        err.emit();
    }

    // FIXME(const_generics): This patches over an ICE caused by non-'static lifetimes in const
    // generics. We are disallowing this until we can decide on how we want to handle non-'static
    // lifetimes in const generics. See issue #74052 for discussion.
    crate fn emit_non_static_lt_in_const_generic_error(&self, lifetime_ref: &hir::Lifetime) {
        let mut err = struct_span_err!(
            self.tcx.sess,
            lifetime_ref.span,
            E0771,
            "use of non-static lifetime `{}` in const generic",
            lifetime_ref
        );
        err.note(
            "for more information, see issue #74052 \
            <https://github.com/rust-lang/rust/issues/74052>",
        );
        err.emit();
    }

    crate fn is_trait_ref_fn_scope(&mut self, trait_ref: &'tcx hir::PolyTraitRef<'tcx>) -> bool {
        if let def::Res::Def(_, did) = trait_ref.trait_ref.path.res {
            if [
                self.tcx.lang_items().fn_once_trait(),
                self.tcx.lang_items().fn_trait(),
                self.tcx.lang_items().fn_mut_trait(),
            ]
            .contains(&Some(did))
            {
                let (span, span_type) = match &trait_ref.bound_generic_params {
                    [] => (trait_ref.span.shrink_to_lo(), ForLifetimeSpanType::BoundEmpty),
                    [.., bound] => (bound.span.shrink_to_hi(), ForLifetimeSpanType::BoundTail),
                };
                self.missing_named_lifetime_spots
                    .push(MissingLifetimeSpot::HigherRanked { span, span_type });
                return true;
            }
        };
        false
    }

    crate fn add_missing_lifetime_specifiers_label(
        &self,
        err: &mut DiagnosticBuilder<'_>,
        mut spans_with_counts: Vec<(Span, usize)>,
        lifetime_names: &FxHashSet<Symbol>,
        lifetime_spans: Vec<Span>,
        params: &[ElisionFailureInfo],
    ) {
        let snippets: Vec<Option<String>> = spans_with_counts
            .iter()
            .map(|(span, _)| self.tcx.sess.source_map().span_to_snippet(*span).ok())
            .collect();

        // Empty generics are marked with a span of "<", but since from now on
        // that information is in the snippets it can be removed from the spans.
        for ((span, _), snippet) in spans_with_counts.iter_mut().zip(&snippets) {
            if snippet.as_deref() == Some("<") {
                *span = span.shrink_to_hi();
            }
        }

        for &(span, count) in &spans_with_counts {
            err.span_label(
                span,
                format!(
                    "expected {} lifetime parameter{}",
                    if count == 1 { "named".to_string() } else { count.to_string() },
                    pluralize!(count),
                ),
            );
        }

        let suggest_existing =
            |err: &mut DiagnosticBuilder<'_>,
             name: &str,
             formatters: Vec<Option<Box<dyn Fn(&str) -> String>>>| {
                if let Some(MissingLifetimeSpot::HigherRanked { span: for_span, span_type }) =
                    self.missing_named_lifetime_spots.iter().rev().next()
                {
                    // When we have `struct S<'a>(&'a dyn Fn(&X) -> &X);` we want to not only suggest
                    // using `'a`, but also introduce the concept of HRLTs by suggesting
                    // `struct S<'a>(&'a dyn for<'b> Fn(&X) -> &'b X);`. (#72404)
                    let mut introduce_suggestion = vec![];

                    let a_to_z_repeat_n = |n| {
                        (b'a'..=b'z').map(move |c| {
                            let mut s = '\''.to_string();
                            s.extend(std::iter::repeat(char::from(c)).take(n));
                            s
                        })
                    };

                    // If all single char lifetime names are present, we wrap around and double the chars.
                    let lt_name = (1..)
                        .flat_map(a_to_z_repeat_n)
                        .find(|lt| !lifetime_names.contains(&Symbol::intern(&lt)))
                        .unwrap();
                    let msg = format!(
                        "consider making the {} lifetime-generic with a new `{}` lifetime",
                        span_type.descr(),
                        lt_name,
                    );
                    err.note(
                        "for more information on higher-ranked polymorphism, visit \
                    https://doc.rust-lang.org/nomicon/hrtb.html",
                    );
                    let for_sugg = span_type.suggestion(&lt_name);
                    for param in params {
                        if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(param.span)
                        {
                            if snippet.starts_with('&') && !snippet.starts_with("&'") {
                                introduce_suggestion
                                    .push((param.span, format!("&{} {}", lt_name, &snippet[1..])));
                            } else if let Some(stripped) = snippet.strip_prefix("&'_ ") {
                                introduce_suggestion
                                    .push((param.span, format!("&{} {}", lt_name, stripped)));
                            }
                        }
                    }
                    introduce_suggestion.push((*for_span, for_sugg));
                    for ((span, _), formatter) in spans_with_counts.iter().zip(formatters.iter()) {
                        if let Some(formatter) = formatter {
                            introduce_suggestion.push((*span, formatter(&lt_name)));
                        }
                    }
                    err.multipart_suggestion_verbose(
                        &msg,
                        introduce_suggestion,
                        Applicability::MaybeIncorrect,
                    );
                }

                let spans_suggs: Vec<_> = formatters
                    .into_iter()
                    .zip(spans_with_counts.iter())
                    .filter_map(|(fmt, (span, _))| {
                        if let Some(formatter) = fmt { Some((formatter, span)) } else { None }
                    })
                    .map(|(formatter, span)| (*span, formatter(name)))
                    .collect();
                err.multipart_suggestion_verbose(
                    &format!(
                        "consider using the `{}` lifetime",
                        lifetime_names.iter().next().unwrap()
                    ),
                    spans_suggs,
                    Applicability::MaybeIncorrect,
                );
            };
        let suggest_new = |err: &mut DiagnosticBuilder<'_>, suggs: Vec<Option<String>>| {
            for missing in self.missing_named_lifetime_spots.iter().rev() {
                let mut introduce_suggestion = vec![];
                let msg;
                let should_break;
                introduce_suggestion.push(match missing {
                    MissingLifetimeSpot::Generics(generics) => {
                        if generics.span == DUMMY_SP {
                            // Account for malformed generics in the HIR. This shouldn't happen,
                            // but if we make a mistake elsewhere, mainly by keeping something in
                            // `missing_named_lifetime_spots` that we shouldn't, like associated
                            // `const`s or making a mistake in the AST lowering we would provide
                            // non-sensical suggestions. Guard against that by skipping these.
                            // (#74264)
                            continue;
                        }
                        msg = "consider introducing a named lifetime parameter".to_string();
                        should_break = true;
                        if let Some(param) = generics.params.iter().find(|p| {
                            !matches!(
                                p.kind,
                                hir::GenericParamKind::Type {
                                    synthetic: Some(hir::SyntheticTyParamKind::ImplTrait),
                                    ..
                                } | hir::GenericParamKind::Lifetime {
                                    kind: hir::LifetimeParamKind::Elided
                                }
                            )
                        }) {
                            (param.span.shrink_to_lo(), "'a, ".to_string())
                        } else {
                            (generics.span, "<'a>".to_string())
                        }
                    }
                    MissingLifetimeSpot::HigherRanked { span, span_type } => {
                        msg = format!(
                            "consider making the {} lifetime-generic with a new `'a` lifetime",
                            span_type.descr(),
                        );
                        should_break = false;
                        err.note(
                            "for more information on higher-ranked polymorphism, visit \
                            https://doc.rust-lang.org/nomicon/hrtb.html",
                        );
                        (*span, span_type.suggestion("'a"))
                    }
                    MissingLifetimeSpot::Static => {
                        let mut spans_suggs = Vec::new();
                        for ((span, count), snippet) in
                            spans_with_counts.iter().copied().zip(snippets.iter())
                        {
                            let (span, sugg) = match snippet.as_deref() {
                                Some("&") => (span.shrink_to_hi(), "'static ".to_owned()),
                                Some("'_") => (span, "'static".to_owned()),
                                Some(snippet) if !snippet.ends_with('>') => {
                                    if snippet == "" {
                                        (
                                            span,
                                            std::iter::repeat("'static")
                                                .take(count)
                                                .collect::<Vec<_>>()
                                                .join(", "),
                                        )
                                    } else if snippet == "<" || snippet == "(" {
                                        (
                                            span.shrink_to_hi(),
                                            std::iter::repeat("'static")
                                                .take(count)
                                                .collect::<Vec<_>>()
                                                .join(", "),
                                        )
                                    } else {
                                        (
                                            span.shrink_to_hi(),
                                            format!(
                                                "<{}>",
                                                std::iter::repeat("'static")
                                                    .take(count)
                                                    .collect::<Vec<_>>()
                                                    .join(", "),
                                            ),
                                        )
                                    }
                                }
                                _ => continue,
                            };
                            spans_suggs.push((span, sugg.to_string()));
                        }
                        err.multipart_suggestion_verbose(
                            "consider using the `'static` lifetime",
                            spans_suggs,
                            Applicability::MaybeIncorrect,
                        );
                        continue;
                    }
                });

                struct Lifetime(Span, String);
                impl Lifetime {
                    fn is_unnamed(&self) -> bool {
                        self.1.starts_with('&') && !self.1.starts_with("&'")
                    }
                    fn is_underscore(&self) -> bool {
                        self.1.starts_with("&'_ ")
                    }
                    fn is_named(&self) -> bool {
                        self.1.starts_with("&'")
                    }
                    fn suggestion(&self, sugg: String) -> Option<(Span, String)> {
                        Some(
                            match (
                                self.is_unnamed(),
                                self.is_underscore(),
                                self.is_named(),
                                sugg.starts_with('&'),
                            ) {
                                (true, _, _, false) => (self.span_unnamed_borrow(), sugg),
                                (true, _, _, true) => {
                                    (self.span_unnamed_borrow(), sugg[1..].to_string())
                                }
                                (_, true, _, false) => {
                                    (self.span_underscore_borrow(), sugg.trim().to_string())
                                }
                                (_, true, _, true) => {
                                    (self.span_underscore_borrow(), sugg[1..].trim().to_string())
                                }
                                (_, _, true, false) => {
                                    (self.span_named_borrow(), sugg.trim().to_string())
                                }
                                (_, _, true, true) => {
                                    (self.span_named_borrow(), sugg[1..].trim().to_string())
                                }
                                _ => return None,
                            },
                        )
                    }
                    fn span_unnamed_borrow(&self) -> Span {
                        let lo = self.0.lo() + BytePos(1);
                        self.0.with_lo(lo).with_hi(lo)
                    }
                    fn span_named_borrow(&self) -> Span {
                        let lo = self.0.lo() + BytePos(1);
                        self.0.with_lo(lo)
                    }
                    fn span_underscore_borrow(&self) -> Span {
                        let lo = self.0.lo() + BytePos(1);
                        let hi = lo + BytePos(2);
                        self.0.with_lo(lo).with_hi(hi)
                    }
                }

                for param in params {
                    if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(param.span) {
                        if let Some((span, sugg)) =
                            Lifetime(param.span, snippet).suggestion("'a ".to_string())
                        {
                            introduce_suggestion.push((span, sugg));
                        }
                    }
                }
                for (span, sugg) in spans_with_counts.iter().copied().zip(suggs.iter()).filter_map(
                    |((span, _), sugg)| match &sugg {
                        Some(sugg) => Some((span, sugg.to_string())),
                        _ => None,
                    },
                ) {
                    let (span, sugg) = self
                        .tcx
                        .sess
                        .source_map()
                        .span_to_snippet(span)
                        .ok()
                        .and_then(|snippet| Lifetime(span, snippet).suggestion(sugg.clone()))
                        .unwrap_or((span, sugg));
                    introduce_suggestion.push((span, sugg.to_string()));
                }
                err.multipart_suggestion_verbose(
                    &msg,
                    introduce_suggestion,
                    Applicability::MaybeIncorrect,
                );
                if should_break {
                    break;
                }
            }
        };

        let lifetime_names: Vec<_> = lifetime_names.iter().collect();
        match &lifetime_names[..] {
            [name] => {
                let mut suggs: Vec<Option<Box<dyn Fn(&str) -> String>>> = Vec::new();
                for (snippet, (_, count)) in snippets.iter().zip(spans_with_counts.iter().copied())
                {
                    suggs.push(match snippet.as_deref() {
                        Some("&") => Some(Box::new(|name| format!("&{} ", name))),
                        Some("'_") => Some(Box::new(|n| n.to_string())),
                        Some("") => Some(Box::new(move |n| format!("{}, ", n).repeat(count))),
                        Some("<") => Some(Box::new(move |n| {
                            std::iter::repeat(n).take(count).collect::<Vec<_>>().join(", ")
                        })),
                        Some(snippet) if !snippet.ends_with('>') => Some(Box::new(move |name| {
                            format!(
                                "{}<{}>",
                                snippet,
                                std::iter::repeat(name.to_string())
                                    .take(count)
                                    .collect::<Vec<_>>()
                                    .join(", ")
                            )
                        })),
                        _ => None,
                    });
                }
                suggest_existing(err, &name.as_str()[..], suggs);
            }
            [] => {
                let mut suggs = Vec::new();
                for (snippet, (_, count)) in
                    snippets.iter().cloned().zip(spans_with_counts.iter().copied())
                {
                    suggs.push(match snippet.as_deref() {
                        Some("&") => Some("&'a ".to_string()),
                        Some("'_") => Some("'a".to_string()),
                        Some("") => {
                            Some(std::iter::repeat("'a, ").take(count).collect::<Vec<_>>().join(""))
                        }
                        Some("<") => {
                            Some(std::iter::repeat("'a").take(count).collect::<Vec<_>>().join(", "))
                        }
                        Some(snippet) => Some(format!(
                            "{}<{}>",
                            snippet,
                            std::iter::repeat("'a").take(count).collect::<Vec<_>>().join(", "),
                        )),
                        None => None,
                    });
                }
                suggest_new(err, suggs);
            }
            lts if lts.len() > 1 => {
                err.span_note(lifetime_spans, "these named lifetimes are available to use");

                let mut spans_suggs: Vec<_> = Vec::new();
                for ((span, _), snippet) in spans_with_counts.iter().copied().zip(snippets.iter()) {
                    match snippet.as_deref() {
                        Some("") => spans_suggs.push((span, "'lifetime, ".to_string())),
                        Some("&") => spans_suggs
                            .push((span.with_lo(span.lo() + BytePos(1)), "'lifetime ".to_string())),
                        _ => {}
                    }
                }

                if spans_suggs.len() > 0 {
                    // This happens when we have `Foo<T>` where we point at the space before `T`,
                    // but this can be confusing so we give a suggestion with placeholders.
                    err.multipart_suggestion_verbose(
                        "consider using one of the available lifetimes here",
                        spans_suggs,
                        Applicability::HasPlaceholders,
                    );
                }
            }
            _ => unreachable!(),
        }
    }

    /// Non-static lifetimes are prohibited in anonymous constants under `min_const_generics`.
    /// This function will emit an error if `generic_const_exprs` is not enabled, the body identified by
    /// `body_id` is an anonymous constant and `lifetime_ref` is non-static.
    crate fn maybe_emit_forbidden_non_static_lifetime_error(
        &self,
        body_id: hir::BodyId,
        lifetime_ref: &'tcx hir::Lifetime,
    ) {
        let is_anon_const = matches!(
            self.tcx.def_kind(self.tcx.hir().body_owner_def_id(body_id)),
            hir::def::DefKind::AnonConst
        );
        let is_allowed_lifetime = matches!(
            lifetime_ref.name,
            hir::LifetimeName::Implicit | hir::LifetimeName::Static | hir::LifetimeName::Underscore
        );

        if !self.tcx.lazy_normalization() && is_anon_const && !is_allowed_lifetime {
            feature_err(
                &self.tcx.sess.parse_sess,
                sym::generic_const_exprs,
                lifetime_ref.span,
                "a non-static lifetime is not allowed in a `const`",
            )
            .emit();
        }
    }
}