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
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
//! Give useful errors and suggestions to users when an item can't be
//! found or is otherwise invalid.

// ignore-tidy-filelength

use core::ops::ControlFlow;
use std::borrow::Cow;

use hir::Expr;
use rustc_ast::ast::Mutability;
use rustc_attr::parse_confusables;
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
use rustc_data_structures::sorted_map::SortedMap;
use rustc_data_structures::unord::UnordSet;
use rustc_errors::codes::*;
use rustc_errors::{pluralize, struct_span_code_err, Applicability, Diag, MultiSpan, StashKey};
use rustc_hir::def::DefKind;
use rustc_hir::def_id::DefId;
use rustc_hir::intravisit::{self, Visitor};
use rustc_hir::lang_items::LangItem;
use rustc_hir::{self as hir, ExprKind, HirId, Node, PathSegment, QPath};
use rustc_infer::infer::{self, RegionVariableOrigin};
use rustc_middle::bug;
use rustc_middle::ty::fast_reject::{simplify_type, DeepRejectCtxt, TreatParams};
use rustc_middle::ty::print::{
    with_crate_prefix, with_forced_trimmed_paths, PrintTraitRefExt as _,
};
use rustc_middle::ty::{self, GenericArgKind, IsSuggestable, Ty, TyCtxt, TypeVisitableExt};
use rustc_span::def_id::DefIdSet;
use rustc_span::symbol::{kw, sym, Ident};
use rustc_span::{
    edit_distance, ErrorGuaranteed, ExpnKind, FileName, MacroKind, Span, Symbol, DUMMY_SP,
};
use rustc_trait_selection::error_reporting::traits::on_unimplemented::OnUnimplementedNote;
use rustc_trait_selection::infer::InferCtxtExt;
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt as _;
use rustc_trait_selection::traits::{
    supertraits, FulfillmentError, Obligation, ObligationCause, ObligationCauseCode,
};
use tracing::{debug, info, instrument};

use super::probe::{AutorefOrPtrAdjustment, IsSuggestion, Mode, ProbeScope};
use super::{CandidateSource, MethodError, NoMatchData};
use crate::errors::{self, CandidateTraitNote, NoAssociatedItem};
use crate::{Expectation, FnCtxt};

impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
    fn is_fn_ty(&self, ty: Ty<'tcx>, span: Span) -> bool {
        let tcx = self.tcx;
        match ty.kind() {
            // Not all of these (e.g., unsafe fns) implement `FnOnce`,
            // so we look for these beforehand.
            // FIXME(async_closures): These don't impl `FnOnce` by default.
            ty::Closure(..) | ty::FnDef(..) | ty::FnPtr(..) => true,
            // If it's not a simple function, look for things which implement `FnOnce`.
            _ => {
                let Some(fn_once) = tcx.lang_items().fn_once_trait() else {
                    return false;
                };

                // This conditional prevents us from asking to call errors and unresolved types.
                // It might seem that we can use `predicate_must_hold_modulo_regions`,
                // but since a Dummy binder is used to fill in the FnOnce trait's arguments,
                // type resolution always gives a "maybe" here.
                if self.autoderef(span, ty).any(|(ty, _)| {
                    info!("check deref {:?} error", ty);
                    matches!(ty.kind(), ty::Error(_) | ty::Infer(_))
                }) {
                    return false;
                }

                self.autoderef(span, ty).any(|(ty, _)| {
                    info!("check deref {:?} impl FnOnce", ty);
                    self.probe(|_| {
                        let trait_ref =
                            ty::TraitRef::new(tcx, fn_once, [ty, self.next_ty_var(span)]);
                        let poly_trait_ref = ty::Binder::dummy(trait_ref);
                        let obligation = Obligation::misc(
                            tcx,
                            span,
                            self.body_id,
                            self.param_env,
                            poly_trait_ref,
                        );
                        self.predicate_may_hold(&obligation)
                    })
                })
            }
        }
    }

    fn is_slice_ty(&self, ty: Ty<'tcx>, span: Span) -> bool {
        self.autoderef(span, ty).any(|(ty, _)| matches!(ty.kind(), ty::Slice(..) | ty::Array(..)))
    }

    fn impl_into_iterator_should_be_iterator(
        &self,
        ty: Ty<'tcx>,
        span: Span,
        unsatisfied_predicates: &Vec<(
            ty::Predicate<'_>,
            Option<ty::Predicate<'_>>,
            Option<ObligationCause<'_>>,
        )>,
    ) -> bool {
        fn predicate_bounds_generic_param<'tcx>(
            predicate: ty::Predicate<'_>,
            generics: &'tcx ty::Generics,
            generic_param: &ty::GenericParamDef,
            tcx: TyCtxt<'tcx>,
        ) -> bool {
            if let ty::PredicateKind::Clause(ty::ClauseKind::Trait(trait_pred)) =
                predicate.kind().as_ref().skip_binder()
            {
                let ty::TraitPredicate { trait_ref: ty::TraitRef { args, .. }, .. } = trait_pred;
                if args.is_empty() {
                    return false;
                }
                let Some(arg_ty) = args[0].as_type() else {
                    return false;
                };
                let ty::Param(param) = *arg_ty.kind() else {
                    return false;
                };
                // Is `generic_param` the same as the arg for this trait predicate?
                generic_param.index == generics.type_param(param, tcx).index
            } else {
                false
            }
        }

        fn is_iterator_predicate(predicate: ty::Predicate<'_>, tcx: TyCtxt<'_>) -> bool {
            if let ty::PredicateKind::Clause(ty::ClauseKind::Trait(trait_pred)) =
                predicate.kind().as_ref().skip_binder()
            {
                tcx.is_diagnostic_item(sym::Iterator, trait_pred.trait_ref.def_id)
            } else {
                false
            }
        }

        // Does the `ty` implement `IntoIterator`?
        let Some(into_iterator_trait) = self.tcx.get_diagnostic_item(sym::IntoIterator) else {
            return false;
        };
        let trait_ref = ty::TraitRef::new(self.tcx, into_iterator_trait, [ty]);
        let cause = ObligationCause::new(span, self.body_id, ObligationCauseCode::Misc);
        let obligation = Obligation::new(self.tcx, cause, self.param_env, trait_ref);
        if !self.predicate_must_hold_modulo_regions(&obligation) {
            return false;
        }

        match *ty.peel_refs().kind() {
            ty::Param(param) => {
                let generics = self.tcx.generics_of(self.body_id);
                let generic_param = generics.type_param(param, self.tcx);
                for unsatisfied in unsatisfied_predicates.iter() {
                    // The parameter implements `IntoIterator`
                    // but it has called a method that requires it to implement `Iterator`
                    if predicate_bounds_generic_param(
                        unsatisfied.0,
                        generics,
                        generic_param,
                        self.tcx,
                    ) && is_iterator_predicate(unsatisfied.0, self.tcx)
                    {
                        return true;
                    }
                }
            }
            ty::Slice(..) | ty::Adt(..) | ty::Alias(ty::Opaque, _) => {
                for unsatisfied in unsatisfied_predicates.iter() {
                    if is_iterator_predicate(unsatisfied.0, self.tcx) {
                        return true;
                    }
                }
            }
            _ => return false,
        }
        false
    }

    #[instrument(level = "debug", skip(self))]
    pub fn report_method_error(
        &self,
        call_id: HirId,
        rcvr_ty: Ty<'tcx>,
        error: MethodError<'tcx>,
        expected: Expectation<'tcx>,
        trait_missing_method: bool,
    ) -> ErrorGuaranteed {
        let (span, sugg_span, source, item_name, args) = match self.tcx.hir_node(call_id) {
            hir::Node::Expr(&hir::Expr {
                kind: hir::ExprKind::MethodCall(segment, rcvr, args, _),
                span,
                ..
            }) => {
                (segment.ident.span, span, SelfSource::MethodCall(rcvr), segment.ident, Some(args))
            }
            hir::Node::Expr(&hir::Expr {
                kind: hir::ExprKind::Path(QPath::TypeRelative(rcvr, segment)),
                span,
                ..
            })
            | hir::Node::Pat(&hir::Pat {
                kind:
                    hir::PatKind::Path(QPath::TypeRelative(rcvr, segment))
                    | hir::PatKind::Struct(QPath::TypeRelative(rcvr, segment), ..)
                    | hir::PatKind::TupleStruct(QPath::TypeRelative(rcvr, segment), ..),
                span,
                ..
            }) => {
                let args = match self.tcx.parent_hir_node(call_id) {
                    hir::Node::Expr(&hir::Expr {
                        kind: hir::ExprKind::Call(callee, args), ..
                    }) if callee.hir_id == call_id => Some(args),
                    _ => None,
                };
                (segment.ident.span, span, SelfSource::QPath(rcvr), segment.ident, args)
            }
            node => unreachable!("{node:?}"),
        };

        // Avoid suggestions when we don't know what's going on.
        if let Err(guar) = rcvr_ty.error_reported() {
            return guar;
        }

        match error {
            MethodError::NoMatch(mut no_match_data) => {
                return self.report_no_match_method_error(
                    span,
                    rcvr_ty,
                    item_name,
                    call_id,
                    source,
                    args,
                    sugg_span,
                    &mut no_match_data,
                    expected,
                    trait_missing_method,
                );
            }

            MethodError::Ambiguity(mut sources) => {
                let mut err = struct_span_code_err!(
                    self.dcx(),
                    item_name.span,
                    E0034,
                    "multiple applicable items in scope"
                );
                err.span_label(item_name.span, format!("multiple `{item_name}` found"));

                self.note_candidates_on_method_error(
                    rcvr_ty,
                    item_name,
                    source,
                    args,
                    span,
                    &mut err,
                    &mut sources,
                    Some(sugg_span),
                );
                return err.emit();
            }

            MethodError::PrivateMatch(kind, def_id, out_of_scope_traits) => {
                let kind = self.tcx.def_kind_descr(kind, def_id);
                let mut err = struct_span_code_err!(
                    self.dcx(),
                    item_name.span,
                    E0624,
                    "{} `{}` is private",
                    kind,
                    item_name
                );
                err.span_label(item_name.span, format!("private {kind}"));
                let sp = self
                    .tcx
                    .hir()
                    .span_if_local(def_id)
                    .unwrap_or_else(|| self.tcx.def_span(def_id));
                err.span_label(sp, format!("private {kind} defined here"));
                self.suggest_valid_traits(&mut err, item_name, out_of_scope_traits, true);
                return err.emit();
            }

            MethodError::IllegalSizedBound { candidates, needs_mut, bound_span, self_expr } => {
                let msg = if needs_mut {
                    with_forced_trimmed_paths!(format!(
                        "the `{item_name}` method cannot be invoked on `{rcvr_ty}`"
                    ))
                } else {
                    format!("the `{item_name}` method cannot be invoked on a trait object")
                };
                let mut err = self.dcx().struct_span_err(span, msg);
                if !needs_mut {
                    err.span_label(bound_span, "this has a `Sized` requirement");
                }
                if !candidates.is_empty() {
                    let help = format!(
                        "{an}other candidate{s} {were} found in the following trait{s}",
                        an = if candidates.len() == 1 { "an" } else { "" },
                        s = pluralize!(candidates.len()),
                        were = pluralize!("was", candidates.len()),
                    );
                    self.suggest_use_candidates(
                        candidates,
                        |accessible_sugg, inaccessible_sugg, span| {
                            let suggest_for_access =
                                |err: &mut Diag<'_>, mut msg: String, sugg: Vec<_>| {
                                    msg += &format!(
                                        ", perhaps add a `use` for {one_of_them}:",
                                        one_of_them =
                                            if sugg.len() == 1 { "it" } else { "one_of_them" },
                                    );
                                    err.span_suggestions(
                                        span,
                                        msg,
                                        sugg,
                                        Applicability::MaybeIncorrect,
                                    );
                                };
                            let suggest_for_privacy =
                                |err: &mut Diag<'_>, mut msg: String, suggs: Vec<String>| {
                                    if let [sugg] = suggs.as_slice() {
                                        err.help(format!("\
                                            trait `{}` provides `{item_name}` is implemented but not reachable",
                                            sugg.trim(),
                                        ));
                                    } else {
                                        msg += &format!(" but {} not reachable", pluralize!("is", suggs.len()));
                                        err.span_suggestions(
                                            span,
                                            msg,
                                            suggs,
                                            Applicability::MaybeIncorrect,
                                        );
                                    }
                                };
                            if accessible_sugg.is_empty() {
                                // `inaccessible_sugg` must not be empty
                                suggest_for_privacy(&mut err, help, inaccessible_sugg);
                            } else if inaccessible_sugg.is_empty() {
                                suggest_for_access(&mut err, help, accessible_sugg);
                            } else {
                                suggest_for_access(&mut err, help.clone(), accessible_sugg);
                                suggest_for_privacy(&mut err, help, inaccessible_sugg);
                            }
                        },
                    );
                }
                if let ty::Ref(region, t_type, mutability) = rcvr_ty.kind() {
                    if needs_mut {
                        let trait_type =
                            Ty::new_ref(self.tcx, *region, *t_type, mutability.invert());
                        let msg = format!("you need `{trait_type}` instead of `{rcvr_ty}`");
                        let mut kind = &self_expr.kind;
                        while let hir::ExprKind::AddrOf(_, _, expr)
                        | hir::ExprKind::Unary(hir::UnOp::Deref, expr) = kind
                        {
                            kind = &expr.kind;
                        }
                        if let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = kind
                            && let hir::def::Res::Local(hir_id) = path.res
                            && let hir::Node::Pat(b) = self.tcx.hir_node(hir_id)
                            && let hir::Node::Param(p) = self.tcx.parent_hir_node(b.hir_id)
                            && let Some(decl) = self.tcx.parent_hir_node(p.hir_id).fn_decl()
                            && let Some(ty) = decl.inputs.iter().find(|ty| ty.span == p.ty_span)
                            && let hir::TyKind::Ref(_, mut_ty) = &ty.kind
                            && let hir::Mutability::Not = mut_ty.mutbl
                        {
                            err.span_suggestion_verbose(
                                mut_ty.ty.span.shrink_to_lo(),
                                msg,
                                "mut ",
                                Applicability::MachineApplicable,
                            );
                        } else {
                            err.help(msg);
                        }
                    }
                }
                return err.emit();
            }

            MethodError::BadReturnType => bug!("no return type expectations but got BadReturnType"),
        }
    }

    fn suggest_missing_writer(&self, rcvr_ty: Ty<'tcx>, rcvr_expr: &hir::Expr<'tcx>) -> Diag<'_> {
        let mut file = None;
        let ty_str = self.tcx.short_ty_string(rcvr_ty, &mut file);
        let mut err = struct_span_code_err!(
            self.dcx(),
            rcvr_expr.span,
            E0599,
            "cannot write into `{}`",
            ty_str
        );
        err.span_note(
            rcvr_expr.span,
            "must implement `io::Write`, `fmt::Write`, or have a `write_fmt` method",
        );
        if let ExprKind::Lit(_) = rcvr_expr.kind {
            err.span_help(
                rcvr_expr.span.shrink_to_lo(),
                "a writer is needed before this format string",
            );
        };
        if let Some(file) = file {
            err.note(format!("the full type name has been written to '{}'", file.display()));
            err.note("consider using `--verbose` to print the full type name to the console");
        }

        err
    }

    fn suggest_use_shadowed_binding_with_method(
        &self,
        self_source: SelfSource<'tcx>,
        method_name: Ident,
        ty_str_reported: &str,
        err: &mut Diag<'_>,
    ) {
        #[derive(Debug)]
        struct LetStmt {
            ty_hir_id_opt: Option<hir::HirId>,
            binding_id: hir::HirId,
            span: Span,
            init_hir_id: hir::HirId,
        }

        // Used for finding suggest binding.
        // ```rust
        // earlier binding for suggesting:
        // let y = vec![1, 2];
        // now binding:
        // if let Some(y) = x {
        //     y.push(y);
        // }
        // ```
        struct LetVisitor<'a, 'tcx> {
            // Error binding which don't have `method_name`.
            binding_name: Symbol,
            binding_id: hir::HirId,
            // Used for check if the suggest binding has `method_name`.
            fcx: &'a FnCtxt<'a, 'tcx>,
            call_expr: &'tcx Expr<'tcx>,
            method_name: Ident,
            // Suggest the binding which is shallowed.
            sugg_let: Option<LetStmt>,
        }

        impl<'a, 'tcx> LetVisitor<'a, 'tcx> {
            // Check scope of binding.
            fn is_sub_scope(&self, sub_id: hir::ItemLocalId, super_id: hir::ItemLocalId) -> bool {
                let scope_tree = self.fcx.tcx.region_scope_tree(self.fcx.body_id);
                if let Some(sub_var_scope) = scope_tree.var_scope(sub_id)
                    && let Some(super_var_scope) = scope_tree.var_scope(super_id)
                    && scope_tree.is_subscope_of(sub_var_scope, super_var_scope)
                {
                    return true;
                }
                false
            }

            // Check if an earlier shadowed binding make `the receiver` of a MethodCall has the method.
            // If it does, record the earlier binding for subsequent notes.
            fn check_and_add_sugg_binding(&mut self, binding: LetStmt) -> bool {
                if !self.is_sub_scope(self.binding_id.local_id, binding.binding_id.local_id) {
                    return false;
                }

                // Get the earlier shadowed binding'ty and use it to check the method.
                if let Some(ty_hir_id) = binding.ty_hir_id_opt
                    && let Some(tyck_ty) = self.fcx.node_ty_opt(ty_hir_id)
                {
                    if self
                        .fcx
                        .lookup_probe_for_diagnostic(
                            self.method_name,
                            tyck_ty,
                            self.call_expr,
                            ProbeScope::TraitsInScope,
                            None,
                        )
                        .is_ok()
                    {
                        self.sugg_let = Some(binding);
                        return true;
                    } else {
                        return false;
                    }
                }

                // If the shadowed binding has an itializer expression,
                // use the initializer expression'ty to try to find the method again.
                // For example like:  `let mut x = Vec::new();`,
                // `Vec::new()` is the itializer expression.
                if let Some(self_ty) = self.fcx.node_ty_opt(binding.init_hir_id)
                    && self
                        .fcx
                        .lookup_probe_for_diagnostic(
                            self.method_name,
                            self_ty,
                            self.call_expr,
                            ProbeScope::TraitsInScope,
                            None,
                        )
                        .is_ok()
                {
                    self.sugg_let = Some(binding);
                    return true;
                }
                return false;
            }
        }

        impl<'v> Visitor<'v> for LetVisitor<'_, '_> {
            type Result = ControlFlow<()>;
            fn visit_stmt(&mut self, ex: &'v hir::Stmt<'v>) -> Self::Result {
                if let hir::StmtKind::Let(&hir::LetStmt { pat, ty, init, .. }) = ex.kind
                    && let hir::PatKind::Binding(_, binding_id, binding_name, ..) = pat.kind
                    && let Some(init) = init
                    && binding_name.name == self.binding_name
                    && binding_id != self.binding_id
                {
                    if self.check_and_add_sugg_binding(LetStmt {
                        ty_hir_id_opt: if let Some(ty) = ty { Some(ty.hir_id) } else { None },
                        binding_id,
                        span: pat.span,
                        init_hir_id: init.hir_id,
                    }) {
                        return ControlFlow::Break(());
                    }
                    ControlFlow::Continue(())
                } else {
                    hir::intravisit::walk_stmt(self, ex)
                }
            }

            // Used for find the error binding.
            // When the visitor reaches this point, all the shadowed bindings
            // have been found, so the visitor ends.
            fn visit_pat(&mut self, p: &'v hir::Pat<'v>) -> Self::Result {
                match p.kind {
                    hir::PatKind::Binding(_, binding_id, binding_name, _) => {
                        if binding_name.name == self.binding_name && binding_id == self.binding_id {
                            return ControlFlow::Break(());
                        }
                    }
                    _ => {
                        intravisit::walk_pat(self, p);
                    }
                }
                ControlFlow::Continue(())
            }
        }

        if let SelfSource::MethodCall(rcvr) = self_source
            && let hir::ExprKind::Path(QPath::Resolved(_, path)) = rcvr.kind
            && let hir::def::Res::Local(recv_id) = path.res
            && let Some(segment) = path.segments.first()
        {
            let body = self.tcx.hir().body_owned_by(self.body_id);

            if let Node::Expr(call_expr) = self.tcx.parent_hir_node(rcvr.hir_id) {
                let mut let_visitor = LetVisitor {
                    fcx: self,
                    call_expr,
                    binding_name: segment.ident.name,
                    binding_id: recv_id,
                    method_name,
                    sugg_let: None,
                };
                let_visitor.visit_body(&body);
                if let Some(sugg_let) = let_visitor.sugg_let
                    && let Some(self_ty) = self.node_ty_opt(sugg_let.init_hir_id)
                {
                    let _sm = self.infcx.tcx.sess.source_map();
                    let rcvr_name = segment.ident.name;
                    let mut span = MultiSpan::from_span(sugg_let.span);
                    span.push_span_label(sugg_let.span,
                            format!("`{rcvr_name}` of type `{self_ty}` that has method `{method_name}` defined earlier here"));
                    span.push_span_label(
                        self.tcx.hir().span(recv_id),
                        format!(
                            "earlier `{rcvr_name}` shadowed here with type `{ty_str_reported}`"
                        ),
                    );
                    err.span_note(
                        span,
                        format!(
                            "there's an earlier shadowed binding `{rcvr_name}` of type `{self_ty}` \
                                    that has method `{method_name}` available"
                        ),
                    );
                }
            }
        }
    }

    fn report_no_match_method_error(
        &self,
        mut span: Span,
        rcvr_ty: Ty<'tcx>,
        item_name: Ident,
        expr_id: hir::HirId,
        source: SelfSource<'tcx>,
        args: Option<&'tcx [hir::Expr<'tcx>]>,
        sugg_span: Span,
        no_match_data: &mut NoMatchData<'tcx>,
        expected: Expectation<'tcx>,
        trait_missing_method: bool,
    ) -> ErrorGuaranteed {
        let mode = no_match_data.mode;
        let tcx = self.tcx;
        let rcvr_ty = self.resolve_vars_if_possible(rcvr_ty);
        let mut ty_file = None;
        let (mut ty_str, short_ty_str) =
            if trait_missing_method && let ty::Dynamic(predicates, _, _) = rcvr_ty.kind() {
                (predicates.to_string(), with_forced_trimmed_paths!(predicates.to_string()))
            } else {
                (
                    tcx.short_ty_string(rcvr_ty, &mut ty_file),
                    with_forced_trimmed_paths!(rcvr_ty.to_string()),
                )
            };
        let is_method = mode == Mode::MethodCall;
        let unsatisfied_predicates = &no_match_data.unsatisfied_predicates;
        let similar_candidate = no_match_data.similar_candidate;
        let item_kind = if is_method {
            "method"
        } else if rcvr_ty.is_enum() {
            "variant or associated item"
        } else {
            match (item_name.as_str().chars().next(), rcvr_ty.is_fresh_ty()) {
                (Some(name), false) if name.is_lowercase() => "function or associated item",
                (Some(_), false) => "associated item",
                (Some(_), true) | (None, false) => "variant or associated item",
                (None, true) => "variant",
            }
        };

        // We could pass the file for long types into these two, but it isn't strictly necessary
        // given how targeted they are.
        if let Err(guar) = self.report_failed_method_call_on_range_end(
            tcx,
            rcvr_ty,
            source,
            span,
            item_name,
            &short_ty_str,
        ) {
            return guar;
        }
        if let Err(guar) = self.report_failed_method_call_on_numerical_infer_var(
            tcx,
            rcvr_ty,
            source,
            span,
            item_kind,
            item_name,
            &short_ty_str,
        ) {
            return guar;
        }
        span = item_name.span;

        // Don't show generic arguments when the method can't be found in any implementation (#81576).
        let mut ty_str_reported = ty_str.clone();
        if let ty::Adt(_, generics) = rcvr_ty.kind() {
            if generics.len() > 0 {
                let mut autoderef = self.autoderef(span, rcvr_ty);
                let candidate_found = autoderef.any(|(ty, _)| {
                    if let ty::Adt(adt_def, _) = ty.kind() {
                        self.tcx
                            .inherent_impls(adt_def.did())
                            .into_iter()
                            .flatten()
                            .any(|def_id| self.associated_value(*def_id, item_name).is_some())
                    } else {
                        false
                    }
                });
                let has_deref = autoderef.step_count() > 0;
                if !candidate_found && !has_deref && unsatisfied_predicates.is_empty() {
                    if let Some((path_string, _)) = ty_str.split_once('<') {
                        ty_str_reported = path_string.to_string();
                    }
                }
            }
        }

        let is_write = sugg_span.ctxt().outer_expn_data().macro_def_id.is_some_and(|def_id| {
            tcx.is_diagnostic_item(sym::write_macro, def_id)
                || tcx.is_diagnostic_item(sym::writeln_macro, def_id)
        }) && item_name.name == Symbol::intern("write_fmt");
        let mut err = if is_write && let SelfSource::MethodCall(rcvr_expr) = source {
            self.suggest_missing_writer(rcvr_ty, rcvr_expr)
        } else {
            let mut err = self.dcx().create_err(NoAssociatedItem {
                span,
                item_kind,
                item_name,
                ty_prefix: if trait_missing_method {
                    // FIXME(mu001999) E0599 maybe not suitable here because it is for types
                    Cow::from("trait")
                } else {
                    rcvr_ty.prefix_string(self.tcx)
                },
                ty_str: ty_str_reported.clone(),
                trait_missing_method,
            });

            if is_method {
                self.suggest_use_shadowed_binding_with_method(
                    source,
                    item_name,
                    &ty_str_reported,
                    &mut err,
                );
            }

            err
        };
        if tcx.sess.source_map().is_multiline(sugg_span) {
            err.span_label(sugg_span.with_hi(span.lo()), "");
        }

        if short_ty_str.len() < ty_str.len() && ty_str.len() > 10 {
            ty_str = short_ty_str;
        }

        if let Some(file) = ty_file {
            err.note(format!("the full type name has been written to '{}'", file.display(),));
            err.note("consider using `--verbose` to print the full type name to the console");
        }
        if rcvr_ty.references_error() {
            err.downgrade_to_delayed_bug();
        }

        if matches!(source, SelfSource::QPath(_)) && args.is_some() {
            self.find_builder_fn(&mut err, rcvr_ty, expr_id);
        }

        if tcx.ty_is_opaque_future(rcvr_ty) && item_name.name == sym::poll {
            err.help(format!(
                "method `poll` found on `Pin<&mut {ty_str}>`, \
                see documentation for `std::pin::Pin`"
            ));
            err.help("self type must be pinned to call `Future::poll`, \
                see https://rust-lang.github.io/async-book/04_pinning/01_chapter.html#pinning-in-practice"
            );
        }

        if let Mode::MethodCall = mode
            && let SelfSource::MethodCall(cal) = source
        {
            self.suggest_await_before_method(
                &mut err,
                item_name,
                rcvr_ty,
                cal,
                span,
                expected.only_has_type(self),
            );
        }
        if let Some(span) =
            tcx.resolutions(()).confused_type_with_std_module.get(&span.with_parent(None))
        {
            err.span_suggestion(
                span.shrink_to_lo(),
                "you are looking for the module in `std`, not the primitive type",
                "std::",
                Applicability::MachineApplicable,
            );
        }

        // on pointers, check if the method would exist on a reference
        if let SelfSource::MethodCall(rcvr_expr) = source
            && let ty::RawPtr(ty, ptr_mutbl) = *rcvr_ty.kind()
            && let Ok(pick) = self.lookup_probe_for_diagnostic(
                item_name,
                Ty::new_ref(tcx, ty::Region::new_error_misc(tcx), ty, ptr_mutbl),
                self.tcx.hir().expect_expr(self.tcx.parent_hir_id(rcvr_expr.hir_id)),
                ProbeScope::TraitsInScope,
                None,
            )
            && let ty::Ref(_, _, sugg_mutbl) = *pick.self_ty.kind()
            && (sugg_mutbl.is_not() || ptr_mutbl.is_mut())
        {
            let (method, method_anchor) = match sugg_mutbl {
                Mutability::Not => {
                    let method_anchor = match ptr_mutbl {
                        Mutability::Not => "as_ref",
                        Mutability::Mut => "as_ref-1",
                    };
                    ("as_ref", method_anchor)
                }
                Mutability::Mut => ("as_mut", "as_mut"),
            };
            err.span_note(
                tcx.def_span(pick.item.def_id),
                format!("the method `{item_name}` exists on the type `{ty}`", ty = pick.self_ty),
            );
            let mut_str = ptr_mutbl.ptr_str();
            err.note(format!(
                "you might want to use the unsafe method `<*{mut_str} T>::{method}` to get \
                an optional reference to the value behind the pointer"
            ));
            err.note(format!(
                "read the documentation for `<*{mut_str} T>::{method}` and ensure you satisfy its \
                safety preconditions before calling it to avoid undefined behavior: \
                https://doc.rust-lang.org/std/primitive.pointer.html#method.{method_anchor}"
            ));
        }

        let mut ty_span = match rcvr_ty.kind() {
            ty::Param(param_type) => {
                Some(param_type.span_from_generics(self.tcx, self.body_id.to_def_id()))
            }
            ty::Adt(def, _) if def.did().is_local() => Some(tcx.def_span(def.did())),
            _ => None,
        };

        if let SelfSource::MethodCall(rcvr_expr) = source {
            self.suggest_fn_call(&mut err, rcvr_expr, rcvr_ty, |output_ty| {
                let call_expr =
                    self.tcx.hir().expect_expr(self.tcx.parent_hir_id(rcvr_expr.hir_id));
                let probe = self.lookup_probe_for_diagnostic(
                    item_name,
                    output_ty,
                    call_expr,
                    ProbeScope::AllTraits,
                    expected.only_has_type(self),
                );
                probe.is_ok()
            });
            self.note_internal_mutation_in_method(
                &mut err,
                rcvr_expr,
                expected.to_option(self),
                rcvr_ty,
            );
        }

        let mut custom_span_label = false;

        let static_candidates = &mut no_match_data.static_candidates;

        // `static_candidates` may have same candidates appended by
        // inherent and extension, which may result in incorrect
        // diagnostic.
        static_candidates.dedup();

        if !static_candidates.is_empty() {
            err.note(
                "found the following associated functions; to be used as methods, \
                 functions must have a `self` parameter",
            );
            err.span_label(span, "this is an associated function, not a method");
            custom_span_label = true;
        }
        if static_candidates.len() == 1 {
            self.suggest_associated_call_syntax(
                &mut err,
                static_candidates,
                rcvr_ty,
                source,
                item_name,
                args,
                sugg_span,
            );
            self.note_candidates_on_method_error(
                rcvr_ty,
                item_name,
                source,
                args,
                span,
                &mut err,
                static_candidates,
                None,
            );
        } else if static_candidates.len() > 1 {
            self.note_candidates_on_method_error(
                rcvr_ty,
                item_name,
                source,
                args,
                span,
                &mut err,
                static_candidates,
                Some(sugg_span),
            );
        }

        let mut bound_spans: SortedMap<Span, Vec<String>> = Default::default();
        let mut restrict_type_params = false;
        let mut suggested_derive = false;
        let mut unsatisfied_bounds = false;
        if item_name.name == sym::count && self.is_slice_ty(rcvr_ty, span) {
            let msg = "consider using `len` instead";
            if let SelfSource::MethodCall(_expr) = source {
                err.span_suggestion_short(span, msg, "len", Applicability::MachineApplicable);
            } else {
                err.span_label(span, msg);
            }
            if let Some(iterator_trait) = self.tcx.get_diagnostic_item(sym::Iterator) {
                let iterator_trait = self.tcx.def_path_str(iterator_trait);
                err.note(format!(
                    "`count` is defined on `{iterator_trait}`, which `{rcvr_ty}` does not implement"
                ));
            }
        } else if self.impl_into_iterator_should_be_iterator(rcvr_ty, span, unsatisfied_predicates)
        {
            err.span_label(span, format!("`{rcvr_ty}` is not an iterator"));
            err.multipart_suggestion_verbose(
                "call `.into_iter()` first",
                vec![(span.shrink_to_lo(), format!("into_iter()."))],
                Applicability::MaybeIncorrect,
            );
            return err.emit();
        } else if !unsatisfied_predicates.is_empty() && matches!(rcvr_ty.kind(), ty::Param(_)) {
            // We special case the situation where we are looking for `_` in
            // `<TypeParam as _>::method` because otherwise the machinery will look for blanket
            // implementations that have unsatisfied trait bounds to suggest, leading us to claim
            // things like "we're looking for a trait with method `cmp`, both `Iterator` and `Ord`
            // have one, in order to implement `Ord` you need to restrict `TypeParam: FnPtr` so
            // that `impl<T: FnPtr> Ord for T` can apply", which is not what we want. We have a type
            // parameter, we want to directly say "`Ord::cmp` and `Iterator::cmp` exist, restrict
            // `TypeParam: Ord` or `TypeParam: Iterator`"". That is done further down when calling
            // `self.suggest_traits_to_import`, so we ignore the `unsatisfied_predicates`
            // suggestions.
        } else if !unsatisfied_predicates.is_empty() {
            let mut type_params = FxIndexMap::default();

            // Pick out the list of unimplemented traits on the receiver.
            // This is used for custom error messages with the `#[rustc_on_unimplemented]` attribute.
            let mut unimplemented_traits = FxIndexMap::default();
            let mut unimplemented_traits_only = true;
            for (predicate, _parent_pred, cause) in unsatisfied_predicates {
                if let (ty::PredicateKind::Clause(ty::ClauseKind::Trait(p)), Some(cause)) =
                    (predicate.kind().skip_binder(), cause.as_ref())
                {
                    if p.trait_ref.self_ty() != rcvr_ty {
                        // This is necessary, not just to keep the errors clean, but also
                        // because our derived obligations can wind up with a trait ref that
                        // requires a different param_env to be correctly compared.
                        continue;
                    }
                    unimplemented_traits.entry(p.trait_ref.def_id).or_insert((
                        predicate.kind().rebind(p.trait_ref),
                        Obligation {
                            cause: cause.clone(),
                            param_env: self.param_env,
                            predicate: *predicate,
                            recursion_depth: 0,
                        },
                    ));
                }
            }

            // Make sure that, if any traits other than the found ones were involved,
            // we don't report an unimplemented trait.
            // We don't want to say that `iter::Cloned` is not an iterator, just
            // because of some non-Clone item being iterated over.
            for (predicate, _parent_pred, _cause) in unsatisfied_predicates {
                match predicate.kind().skip_binder() {
                    ty::PredicateKind::Clause(ty::ClauseKind::Trait(p))
                        if unimplemented_traits.contains_key(&p.trait_ref.def_id) => {}
                    _ => {
                        unimplemented_traits_only = false;
                        break;
                    }
                }
            }

            let mut collect_type_param_suggestions =
                |self_ty: Ty<'tcx>, parent_pred: ty::Predicate<'tcx>, obligation: &str| {
                    // We don't care about regions here, so it's fine to skip the binder here.
                    if let (ty::Param(_), ty::PredicateKind::Clause(ty::ClauseKind::Trait(p))) =
                        (self_ty.kind(), parent_pred.kind().skip_binder())
                    {
                        let node = match p.trait_ref.self_ty().kind() {
                            ty::Param(_) => {
                                // Account for `fn` items like in `issue-35677.rs` to
                                // suggest restricting its type params.
                                Some(self.tcx.hir_node_by_def_id(self.body_id))
                            }
                            ty::Adt(def, _) => def
                                .did()
                                .as_local()
                                .map(|def_id| self.tcx.hir_node_by_def_id(def_id)),
                            _ => None,
                        };
                        if let Some(hir::Node::Item(hir::Item { kind, .. })) = node
                            && let Some(g) = kind.generics()
                        {
                            let key = (
                                g.tail_span_for_predicate_suggestion(),
                                g.add_where_or_trailing_comma(),
                            );
                            type_params
                                .entry(key)
                                .or_insert_with(UnordSet::default)
                                .insert(obligation.to_owned());
                            return true;
                        }
                    }
                    false
                };
            let mut bound_span_label = |self_ty: Ty<'_>, obligation: &str, quiet: &str| {
                let msg = format!("`{}`", if obligation.len() > 50 { quiet } else { obligation });
                match self_ty.kind() {
                    // Point at the type that couldn't satisfy the bound.
                    ty::Adt(def, _) => {
                        bound_spans.get_mut_or_insert_default(tcx.def_span(def.did())).push(msg)
                    }
                    // Point at the trait object that couldn't satisfy the bound.
                    ty::Dynamic(preds, _, _) => {
                        for pred in preds.iter() {
                            match pred.skip_binder() {
                                ty::ExistentialPredicate::Trait(tr) => {
                                    bound_spans
                                        .get_mut_or_insert_default(tcx.def_span(tr.def_id))
                                        .push(msg.clone());
                                }
                                ty::ExistentialPredicate::Projection(_)
                                | ty::ExistentialPredicate::AutoTrait(_) => {}
                            }
                        }
                    }
                    // Point at the closure that couldn't satisfy the bound.
                    ty::Closure(def_id, _) => {
                        bound_spans
                            .get_mut_or_insert_default(tcx.def_span(*def_id))
                            .push(format!("`{quiet}`"));
                    }
                    _ => {}
                }
            };
            let mut format_pred = |pred: ty::Predicate<'tcx>| {
                let bound_predicate = pred.kind();
                match bound_predicate.skip_binder() {
                    ty::PredicateKind::Clause(ty::ClauseKind::Projection(pred)) => {
                        let pred = bound_predicate.rebind(pred);
                        // `<Foo as Iterator>::Item = String`.
                        let projection_term = pred.skip_binder().projection_term;
                        let quiet_projection_term =
                            projection_term.with_self_ty(tcx, Ty::new_var(tcx, ty::TyVid::ZERO));

                        let term = pred.skip_binder().term;

                        let obligation = format!("{projection_term} = {term}");
                        let quiet = with_forced_trimmed_paths!(format!(
                            "{} = {}",
                            quiet_projection_term, term
                        ));

                        bound_span_label(projection_term.self_ty(), &obligation, &quiet);
                        Some((obligation, projection_term.self_ty()))
                    }
                    ty::PredicateKind::Clause(ty::ClauseKind::Trait(poly_trait_ref)) => {
                        let p = poly_trait_ref.trait_ref;
                        let self_ty = p.self_ty();
                        let path = p.print_only_trait_path();
                        let obligation = format!("{self_ty}: {path}");
                        let quiet = with_forced_trimmed_paths!(format!("_: {}", path));
                        bound_span_label(self_ty, &obligation, &quiet);
                        Some((obligation, self_ty))
                    }
                    _ => None,
                }
            };

            // Find all the requirements that come from a local `impl` block.
            let mut skip_list: UnordSet<_> = Default::default();
            let mut spanned_predicates = FxIndexMap::default();
            for (p, parent_p, cause) in unsatisfied_predicates {
                // Extract the predicate span and parent def id of the cause,
                // if we have one.
                let (item_def_id, cause_span) = match cause.as_ref().map(|cause| cause.code()) {
                    Some(ObligationCauseCode::ImplDerived(data)) => {
                        (data.impl_or_alias_def_id, data.span)
                    }
                    Some(
                        ObligationCauseCode::WhereClauseInExpr(def_id, span, _, _)
                        | ObligationCauseCode::WhereClause(def_id, span),
                    ) if !span.is_dummy() => (*def_id, *span),
                    _ => continue,
                };

                // Don't point out the span of `WellFormed` predicates.
                if !matches!(
                    p.kind().skip_binder(),
                    ty::PredicateKind::Clause(
                        ty::ClauseKind::Projection(..) | ty::ClauseKind::Trait(..)
                    )
                ) {
                    continue;
                };

                match self.tcx.hir().get_if_local(item_def_id) {
                    // Unmet obligation comes from a `derive` macro, point at it once to
                    // avoid multiple span labels pointing at the same place.
                    Some(Node::Item(hir::Item {
                        kind: hir::ItemKind::Impl(hir::Impl { of_trait, self_ty, .. }),
                        ..
                    })) if matches!(
                        self_ty.span.ctxt().outer_expn_data().kind,
                        ExpnKind::Macro(MacroKind::Derive, _)
                    ) || matches!(
                        of_trait.as_ref().map(|t| t.path.span.ctxt().outer_expn_data().kind),
                        Some(ExpnKind::Macro(MacroKind::Derive, _))
                    ) =>
                    {
                        let span = self_ty.span.ctxt().outer_expn_data().call_site;
                        let entry = spanned_predicates.entry(span);
                        let entry = entry.or_insert_with(|| {
                            (FxIndexSet::default(), FxIndexSet::default(), Vec::new())
                        });
                        entry.0.insert(span);
                        entry.1.insert((
                            span,
                            "unsatisfied trait bound introduced in this `derive` macro",
                        ));
                        entry.2.push(p);
                        skip_list.insert(p);
                    }

                    // Unmet obligation coming from an `impl`.
                    Some(Node::Item(hir::Item {
                        kind: hir::ItemKind::Impl(hir::Impl { of_trait, self_ty, generics, .. }),
                        span: item_span,
                        ..
                    })) => {
                        let sized_pred =
                            unsatisfied_predicates.iter().any(|(pred, _, _)| {
                                match pred.kind().skip_binder() {
                                    ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred)) => {
                                        self.tcx.is_lang_item(pred.def_id(), LangItem::Sized)
                                            && pred.polarity == ty::PredicatePolarity::Positive
                                    }
                                    _ => false,
                                }
                            });
                        for param in generics.params {
                            if param.span == cause_span && sized_pred {
                                let (sp, sugg) = match param.colon_span {
                                    Some(sp) => (sp.shrink_to_hi(), " ?Sized +"),
                                    None => (param.span.shrink_to_hi(), ": ?Sized"),
                                };
                                err.span_suggestion_verbose(
                                    sp,
                                    "consider relaxing the type parameter's implicit `Sized` bound",
                                    sugg,
                                    Applicability::MachineApplicable,
                                );
                            }
                        }
                        if let Some(pred) = parent_p {
                            // Done to add the "doesn't satisfy" `span_label`.
                            let _ = format_pred(*pred);
                        }
                        skip_list.insert(p);
                        let entry = spanned_predicates.entry(self_ty.span);
                        let entry = entry.or_insert_with(|| {
                            (FxIndexSet::default(), FxIndexSet::default(), Vec::new())
                        });
                        entry.2.push(p);
                        if cause_span != *item_span {
                            entry.0.insert(cause_span);
                            entry.1.insert((cause_span, "unsatisfied trait bound introduced here"));
                        } else {
                            if let Some(trait_ref) = of_trait {
                                entry.0.insert(trait_ref.path.span);
                            }
                            entry.0.insert(self_ty.span);
                        };
                        if let Some(trait_ref) = of_trait {
                            entry.1.insert((trait_ref.path.span, ""));
                        }
                        entry.1.insert((self_ty.span, ""));
                    }
                    Some(Node::Item(hir::Item {
                        kind: hir::ItemKind::Trait(rustc_ast::ast::IsAuto::Yes, ..),
                        span: item_span,
                        ..
                    })) => {
                        self.dcx().span_delayed_bug(
                            *item_span,
                            "auto trait is invoked with no method error, but no error reported?",
                        );
                    }
                    Some(
                        Node::Item(hir::Item {
                            ident,
                            kind: hir::ItemKind::Trait(..) | hir::ItemKind::TraitAlias(..),
                            ..
                        })
                        // We may also encounter unsatisfied GAT or method bounds
                        | Node::TraitItem(hir::TraitItem { ident, .. })
                        | Node::ImplItem(hir::ImplItem { ident, .. }),
                    ) => {
                        skip_list.insert(p);
                        let entry = spanned_predicates.entry(ident.span);
                        let entry = entry.or_insert_with(|| {
                            (FxIndexSet::default(), FxIndexSet::default(), Vec::new())
                        });
                        entry.0.insert(cause_span);
                        entry.1.insert((ident.span, ""));
                        entry.1.insert((cause_span, "unsatisfied trait bound introduced here"));
                        entry.2.push(p);
                    }
                    Some(node) => unreachable!("encountered `{node:?}` due to `{cause:#?}`"),
                    None => (),
                }
            }
            let mut spanned_predicates: Vec<_> = spanned_predicates.into_iter().collect();
            spanned_predicates.sort_by_key(|(span, _)| *span);
            for (_, (primary_spans, span_labels, predicates)) in spanned_predicates {
                let mut preds: Vec<_> = predicates
                    .iter()
                    .filter_map(|pred| format_pred(**pred))
                    .map(|(p, _)| format!("`{p}`"))
                    .collect();
                preds.sort();
                preds.dedup();
                let msg = if let [pred] = &preds[..] {
                    format!("trait bound {pred} was not satisfied")
                } else {
                    format!("the following trait bounds were not satisfied:\n{}", preds.join("\n"),)
                };
                let mut span: MultiSpan = primary_spans.into_iter().collect::<Vec<_>>().into();
                for (sp, label) in span_labels {
                    span.push_span_label(sp, label);
                }
                err.span_note(span, msg);
                unsatisfied_bounds = true;
            }

            let mut suggested_bounds = UnordSet::default();
            // The requirements that didn't have an `impl` span to show.
            let mut bound_list = unsatisfied_predicates
                .iter()
                .filter_map(|(pred, parent_pred, _cause)| {
                    let mut suggested = false;
                    format_pred(*pred).map(|(p, self_ty)| {
                        if let Some(parent) = parent_pred
                            && suggested_bounds.contains(parent)
                        {
                            // We don't suggest `PartialEq` when we already suggest `Eq`.
                        } else if !suggested_bounds.contains(pred) {
                            if collect_type_param_suggestions(self_ty, *pred, &p) {
                                suggested = true;
                                suggested_bounds.insert(pred);
                            }
                        }
                        (
                            match parent_pred {
                                None => format!("`{p}`"),
                                Some(parent_pred) => match format_pred(*parent_pred) {
                                    None => format!("`{p}`"),
                                    Some((parent_p, _)) => {
                                        if !suggested
                                            && !suggested_bounds.contains(pred)
                                            && !suggested_bounds.contains(parent_pred)
                                        {
                                            if collect_type_param_suggestions(
                                                self_ty,
                                                *parent_pred,
                                                &p,
                                            ) {
                                                suggested_bounds.insert(pred);
                                            }
                                        }
                                        format!("`{p}`\nwhich is required by `{parent_p}`")
                                    }
                                },
                            },
                            *pred,
                        )
                    })
                })
                .filter(|(_, pred)| !skip_list.contains(&pred))
                .map(|(t, _)| t)
                .enumerate()
                .collect::<Vec<(usize, String)>>();

            if !matches!(rcvr_ty.peel_refs().kind(), ty::Param(_)) {
                for ((span, add_where_or_comma), obligations) in type_params.into_iter() {
                    restrict_type_params = true;
                    // #74886: Sort here so that the output is always the same.
                    let obligations = obligations.into_sorted_stable_ord();
                    err.span_suggestion_verbose(
                        span,
                        format!(
                            "consider restricting the type parameter{s} to satisfy the trait \
                             bound{s}",
                            s = pluralize!(obligations.len())
                        ),
                        format!("{} {}", add_where_or_comma, obligations.join(", ")),
                        Applicability::MaybeIncorrect,
                    );
                }
            }

            bound_list.sort_by(|(_, a), (_, b)| a.cmp(b)); // Sort alphabetically.
            bound_list.dedup_by(|(_, a), (_, b)| a == b); // #35677
            bound_list.sort_by_key(|(pos, _)| *pos); // Keep the original predicate order.

            if !bound_list.is_empty() || !skip_list.is_empty() {
                let bound_list =
                    bound_list.into_iter().map(|(_, path)| path).collect::<Vec<_>>().join("\n");
                let actual_prefix = rcvr_ty.prefix_string(self.tcx);
                info!("unimplemented_traits.len() == {}", unimplemented_traits.len());
                let mut long_ty_file = None;
                let (primary_message, label) = if unimplemented_traits.len() == 1
                    && unimplemented_traits_only
                {
                    unimplemented_traits
                        .into_iter()
                        .next()
                        .map(|(_, (trait_ref, obligation))| {
                            if trait_ref.self_ty().references_error() || rcvr_ty.references_error()
                            {
                                // Avoid crashing.
                                return (None, None);
                            }
                            let OnUnimplementedNote { message, label, .. } = self
                                .err_ctxt()
                                .on_unimplemented_note(trait_ref, &obligation, &mut long_ty_file);
                            (message, label)
                        })
                        .unwrap()
                } else {
                    (None, None)
                };
                let primary_message = primary_message.unwrap_or_else(|| {
                    format!(
                        "the {item_kind} `{item_name}` exists for {actual_prefix} `{ty_str}`, \
                         but its trait bounds were not satisfied"
                    )
                });
                err.primary_message(primary_message);
                if let Some(file) = long_ty_file {
                    err.note(format!(
                        "the full name for the type has been written to '{}'",
                        file.display(),
                    ));
                    err.note(
                        "consider using `--verbose` to print the full type name to the console",
                    );
                }
                if let Some(label) = label {
                    custom_span_label = true;
                    err.span_label(span, label);
                }
                if !bound_list.is_empty() {
                    err.note(format!(
                        "the following trait bounds were not satisfied:\n{bound_list}"
                    ));
                }
                suggested_derive = self.suggest_derive(&mut err, unsatisfied_predicates);

                unsatisfied_bounds = true;
            }
        } else if let ty::Adt(def, targs) = rcvr_ty.kind()
            && let SelfSource::MethodCall(rcvr_expr) = source
        {
            // This is useful for methods on arbitrary self types that might have a simple
            // mutability difference, like calling a method on `Pin<&mut Self>` that is on
            // `Pin<&Self>`.
            if targs.len() == 1 {
                let mut item_segment = hir::PathSegment::invalid();
                item_segment.ident = item_name;
                for t in [Ty::new_mut_ref, Ty::new_imm_ref, |_, _, t| t] {
                    let new_args =
                        tcx.mk_args_from_iter(targs.iter().map(|arg| match arg.as_type() {
                            Some(ty) => ty::GenericArg::from(t(
                                tcx,
                                tcx.lifetimes.re_erased,
                                ty.peel_refs(),
                            )),
                            _ => arg,
                        }));
                    let rcvr_ty = Ty::new_adt(tcx, *def, new_args);
                    if let Ok(method) = self.lookup_method_for_diagnostic(
                        rcvr_ty,
                        &item_segment,
                        span,
                        tcx.parent_hir_node(rcvr_expr.hir_id).expect_expr(),
                        rcvr_expr,
                    ) {
                        err.span_note(
                            tcx.def_span(method.def_id),
                            format!("{item_kind} is available for `{rcvr_ty}`"),
                        );
                    }
                }
            }
        }

        let mut find_candidate_for_method = false;

        let mut label_span_not_found = |err: &mut Diag<'_>| {
            if unsatisfied_predicates.is_empty() {
                err.span_label(span, format!("{item_kind} not found in `{ty_str}`"));
                let is_string_or_ref_str = match rcvr_ty.kind() {
                    ty::Ref(_, ty, _) => {
                        ty.is_str()
                            || matches!(
                                ty.kind(),
                                ty::Adt(adt, _) if self.tcx.is_lang_item(adt.did(), LangItem::String)
                            )
                    }
                    ty::Adt(adt, _) => self.tcx.is_lang_item(adt.did(), LangItem::String),
                    _ => false,
                };
                if is_string_or_ref_str && item_name.name == sym::iter {
                    err.span_suggestion_verbose(
                        item_name.span,
                        "because of the in-memory representation of `&str`, to obtain \
                         an `Iterator` over each of its codepoint use method `chars`",
                        "chars",
                        Applicability::MachineApplicable,
                    );
                }
                if let ty::Adt(adt, _) = rcvr_ty.kind() {
                    let mut inherent_impls_candidate = self
                        .tcx
                        .inherent_impls(adt.did())
                        .into_iter()
                        .flatten()
                        .copied()
                        .filter(|def_id| {
                            if let Some(assoc) = self.associated_value(*def_id, item_name) {
                                // Check for both mode is the same so we avoid suggesting
                                // incorrect associated item.
                                match (mode, assoc.fn_has_self_parameter, source) {
                                    (Mode::MethodCall, true, SelfSource::MethodCall(_)) => {
                                        // We check that the suggest type is actually
                                        // different from the received one
                                        // So we avoid suggestion method with Box<Self>
                                        // for instance
                                        self.tcx.at(span).type_of(*def_id).instantiate_identity()
                                            != rcvr_ty
                                    }
                                    (Mode::Path, false, _) => true,
                                    _ => false,
                                }
                            } else {
                                false
                            }
                        })
                        .collect::<Vec<_>>();
                    if !inherent_impls_candidate.is_empty() {
                        inherent_impls_candidate.sort_by_key(|id| self.tcx.def_path_str(id));
                        inherent_impls_candidate.dedup();

                        // number of types to show at most
                        let limit = if inherent_impls_candidate.len() == 5 { 5 } else { 4 };
                        let type_candidates = inherent_impls_candidate
                            .iter()
                            .take(limit)
                            .map(|impl_item| {
                                format!(
                                    "- `{}`",
                                    self.tcx.at(span).type_of(*impl_item).instantiate_identity()
                                )
                            })
                            .collect::<Vec<_>>()
                            .join("\n");
                        let additional_types = if inherent_impls_candidate.len() > limit {
                            format!("\nand {} more types", inherent_impls_candidate.len() - limit)
                        } else {
                            "".to_string()
                        };
                        err.note(format!(
                            "the {item_kind} was found for\n{type_candidates}{additional_types}"
                        ));
                        find_candidate_for_method = mode == Mode::MethodCall;
                    }
                }
            } else {
                let ty_str =
                    if ty_str.len() > 50 { String::new() } else { format!("on `{ty_str}` ") };
                err.span_label(
                    span,
                    format!("{item_kind} cannot be called {ty_str}due to unsatisfied trait bounds"),
                );
            }
        };

        // If the method name is the name of a field with a function or closure type,
        // give a helping note that it has to be called as `(x.f)(...)`.
        if let SelfSource::MethodCall(expr) = source {
            if !self.suggest_calling_field_as_fn(span, rcvr_ty, expr, item_name, &mut err)
                && similar_candidate.is_none()
                && !custom_span_label
            {
                label_span_not_found(&mut err);
            }
        } else if !custom_span_label {
            label_span_not_found(&mut err);
        }

        let confusable_suggested = self.confusable_method_name(
            &mut err,
            rcvr_ty,
            item_name,
            args.map(|args| {
                args.iter()
                    .map(|expr| {
                        self.node_ty_opt(expr.hir_id).unwrap_or_else(|| self.next_ty_var(expr.span))
                    })
                    .collect()
            }),
        );

        // Don't suggest (for example) `expr.field.clone()` if `expr.clone()`
        // can't be called due to `typeof(expr): Clone` not holding.
        if unsatisfied_predicates.is_empty() {
            self.suggest_calling_method_on_field(
                &mut err,
                source,
                span,
                rcvr_ty,
                item_name,
                expected.only_has_type(self),
            );
        }

        self.suggest_unwrapping_inner_self(&mut err, source, rcvr_ty, item_name);

        for (span, mut bounds) in bound_spans {
            if !tcx.sess.source_map().is_span_accessible(span) {
                continue;
            }
            bounds.sort();
            bounds.dedup();
            let pre = if Some(span) == ty_span {
                ty_span.take();
                format!(
                    "{item_kind} `{item_name}` not found for this {} because it ",
                    rcvr_ty.prefix_string(self.tcx)
                )
            } else {
                String::new()
            };
            let msg = match &bounds[..] {
                [bound] => format!("{pre}doesn't satisfy {bound}"),
                bounds if bounds.len() > 4 => format!("doesn't satisfy {} bounds", bounds.len()),
                [bounds @ .., last] => {
                    format!("{pre}doesn't satisfy {} or {last}", bounds.join(", "))
                }
                [] => unreachable!(),
            };
            err.span_label(span, msg);
        }
        if let Some(span) = ty_span {
            err.span_label(
                span,
                format!(
                    "{item_kind} `{item_name}` not found for this {}",
                    rcvr_ty.prefix_string(self.tcx)
                ),
            );
        }

        if rcvr_ty.is_numeric() && rcvr_ty.is_fresh() || restrict_type_params || suggested_derive {
        } else {
            self.suggest_traits_to_import(
                &mut err,
                span,
                rcvr_ty,
                item_name,
                args.map(|args| args.len() + 1),
                source,
                no_match_data.out_of_scope_traits.clone(),
                static_candidates,
                unsatisfied_bounds,
                expected.only_has_type(self),
                trait_missing_method,
            );
        }

        // Don't emit a suggestion if we found an actual method
        // that had unsatisfied trait bounds
        if unsatisfied_predicates.is_empty() && rcvr_ty.is_enum() {
            let adt_def = rcvr_ty.ty_adt_def().expect("enum is not an ADT");
            if let Some(var_name) = edit_distance::find_best_match_for_name(
                &adt_def.variants().iter().map(|s| s.name).collect::<Vec<_>>(),
                item_name.name,
                None,
            ) && let Some(variant) = adt_def.variants().iter().find(|s| s.name == var_name)
            {
                let mut suggestion = vec![(span, var_name.to_string())];
                if let SelfSource::QPath(ty) = source
                    && let hir::Node::Expr(ref path_expr) = self.tcx.parent_hir_node(ty.hir_id)
                    && let hir::ExprKind::Path(_) = path_expr.kind
                    && let hir::Node::Stmt(hir::Stmt {
                        kind: hir::StmtKind::Semi(ref parent), ..
                    })
                    | hir::Node::Expr(ref parent) = self.tcx.parent_hir_node(path_expr.hir_id)
                {
                    let replacement_span =
                        if let hir::ExprKind::Call(..) | hir::ExprKind::Struct(..) = parent.kind {
                            // We want to replace the parts that need to go, like `()` and `{}`.
                            span.with_hi(parent.span.hi())
                        } else {
                            span
                        };
                    match (variant.ctor, parent.kind) {
                        (None, hir::ExprKind::Struct(..)) => {
                            // We want a struct and we have a struct. We won't suggest changing
                            // the fields (at least for now).
                            suggestion = vec![(span, var_name.to_string())];
                        }
                        (None, _) => {
                            // struct
                            suggestion = vec![(
                                replacement_span,
                                if variant.fields.is_empty() {
                                    format!("{var_name} {{}}")
                                } else {
                                    format!(
                                        "{var_name} {{ {} }}",
                                        variant
                                            .fields
                                            .iter()
                                            .map(|f| format!("{}: /* value */", f.name))
                                            .collect::<Vec<_>>()
                                            .join(", ")
                                    )
                                },
                            )];
                        }
                        (Some((hir::def::CtorKind::Const, _)), _) => {
                            // unit, remove the `()`.
                            suggestion = vec![(replacement_span, var_name.to_string())];
                        }
                        (
                            Some((hir::def::CtorKind::Fn, def_id)),
                            hir::ExprKind::Call(rcvr, args),
                        ) => {
                            let fn_sig = self.tcx.fn_sig(def_id).instantiate_identity();
                            let inputs = fn_sig.inputs().skip_binder();
                            // FIXME: reuse the logic for "change args" suggestion to account for types
                            // involved and detect things like substitution.
                            match (inputs, args) {
                                (inputs, []) => {
                                    // Add arguments.
                                    suggestion.push((
                                        rcvr.span.shrink_to_hi().with_hi(parent.span.hi()),
                                        format!(
                                            "({})",
                                            inputs
                                                .iter()
                                                .map(|i| format!("/* {i} */"))
                                                .collect::<Vec<String>>()
                                                .join(", ")
                                        ),
                                    ));
                                }
                                (_, [arg]) if inputs.len() != args.len() => {
                                    // Replace arguments.
                                    suggestion.push((
                                        arg.span,
                                        inputs
                                            .iter()
                                            .map(|i| format!("/* {i} */"))
                                            .collect::<Vec<String>>()
                                            .join(", "),
                                    ));
                                }
                                (_, [arg_start, .., arg_end]) if inputs.len() != args.len() => {
                                    // Replace arguments.
                                    suggestion.push((
                                        arg_start.span.to(arg_end.span),
                                        inputs
                                            .iter()
                                            .map(|i| format!("/* {i} */"))
                                            .collect::<Vec<String>>()
                                            .join(", "),
                                    ));
                                }
                                // Argument count is the same, keep as is.
                                _ => {}
                            }
                        }
                        (Some((hir::def::CtorKind::Fn, def_id)), _) => {
                            let fn_sig = self.tcx.fn_sig(def_id).instantiate_identity();
                            let inputs = fn_sig.inputs().skip_binder();
                            suggestion = vec![(
                                replacement_span,
                                format!(
                                    "{var_name}({})",
                                    inputs
                                        .iter()
                                        .map(|i| format!("/* {i} */"))
                                        .collect::<Vec<String>>()
                                        .join(", ")
                                ),
                            )];
                        }
                    }
                }
                err.multipart_suggestion_verbose(
                    "there is a variant with a similar name",
                    suggestion,
                    Applicability::HasPlaceholders,
                );
            }
        }

        if item_name.name == sym::as_str && rcvr_ty.peel_refs().is_str() {
            let msg = "remove this method call";
            let mut fallback_span = true;
            if let SelfSource::MethodCall(expr) = source {
                let call_expr = self.tcx.hir().expect_expr(self.tcx.parent_hir_id(expr.hir_id));
                if let Some(span) = call_expr.span.trim_start(expr.span) {
                    err.span_suggestion(span, msg, "", Applicability::MachineApplicable);
                    fallback_span = false;
                }
            }
            if fallback_span {
                err.span_label(span, msg);
            }
        } else if let Some(similar_candidate) = similar_candidate {
            // Don't emit a suggestion if we found an actual method
            // that had unsatisfied trait bounds
            if unsatisfied_predicates.is_empty()
                // ...or if we already suggested that name because of `rustc_confusable` annotation.
                && Some(similar_candidate.name) != confusable_suggested
            {
                self.find_likely_intended_associated_item(
                    &mut err,
                    similar_candidate,
                    span,
                    args,
                    mode,
                );
            }
        }

        if !find_candidate_for_method {
            self.lookup_segments_chain_for_no_match_method(
                &mut err,
                item_name,
                item_kind,
                source,
                no_match_data,
            );
        }

        self.note_derefed_ty_has_method(&mut err, source, rcvr_ty, item_name, expected);
        err.emit()
    }

    /// If an appropriate error source is not found, check method chain for possible candidates
    fn lookup_segments_chain_for_no_match_method(
        &self,
        err: &mut Diag<'_>,
        item_name: Ident,
        item_kind: &str,
        source: SelfSource<'tcx>,
        no_match_data: &NoMatchData<'tcx>,
    ) {
        if no_match_data.unsatisfied_predicates.is_empty()
            && let Mode::MethodCall = no_match_data.mode
            && let SelfSource::MethodCall(mut source_expr) = source
        {
            let mut stack_methods = vec![];
            while let hir::ExprKind::MethodCall(_path_segment, rcvr_expr, _args, method_span) =
                source_expr.kind
            {
                // Pop the matching receiver, to align on it's notional span
                if let Some(prev_match) = stack_methods.pop() {
                    err.span_label(
                        method_span,
                        format!("{item_kind} `{item_name}` is available on `{prev_match}`"),
                    );
                }
                let rcvr_ty = self.resolve_vars_if_possible(
                    self.typeck_results
                        .borrow()
                        .expr_ty_adjusted_opt(rcvr_expr)
                        .unwrap_or(Ty::new_misc_error(self.tcx)),
                );

                let Ok(candidates) = self.probe_for_name_many(
                    Mode::MethodCall,
                    item_name,
                    None,
                    IsSuggestion(true),
                    rcvr_ty,
                    source_expr.hir_id,
                    ProbeScope::TraitsInScope,
                ) else {
                    return;
                };

                // FIXME: `probe_for_name_many` searches for methods in inherent implementations,
                // so it may return a candidate that doesn't belong to this `revr_ty`. We need to
                // check whether the instantiated type matches the received one.
                for _matched_method in candidates {
                    // found a match, push to stack
                    stack_methods.push(rcvr_ty);
                }
                source_expr = rcvr_expr;
            }
            // If there is a match at the start of the chain, add a label for it too!
            if let Some(prev_match) = stack_methods.pop() {
                err.span_label(
                    source_expr.span,
                    format!("{item_kind} `{item_name}` is available on `{prev_match}`"),
                );
            }
        }
    }

    fn find_likely_intended_associated_item(
        &self,
        err: &mut Diag<'_>,
        similar_candidate: ty::AssocItem,
        span: Span,
        args: Option<&'tcx [hir::Expr<'tcx>]>,
        mode: Mode,
    ) {
        let tcx = self.tcx;
        let def_kind = similar_candidate.kind.as_def_kind();
        let an = self.tcx.def_kind_descr_article(def_kind, similar_candidate.def_id);
        let msg = format!(
            "there is {an} {} `{}` with a similar name",
            self.tcx.def_kind_descr(def_kind, similar_candidate.def_id),
            similar_candidate.name,
        );
        // Methods are defined within the context of a struct and their first parameter
        // is always `self`, which represents the instance of the struct the method is
        // being called on Associated functions don’t take self as a parameter and they are
        // not methods because they don’t have an instance of the struct to work with.
        if def_kind == DefKind::AssocFn {
            let ty_args = self.infcx.fresh_args_for_item(span, similar_candidate.def_id);
            let fn_sig = tcx.fn_sig(similar_candidate.def_id).instantiate(tcx, ty_args);
            let fn_sig = self.instantiate_binder_with_fresh_vars(span, infer::FnCall, fn_sig);
            if similar_candidate.fn_has_self_parameter {
                if let Some(args) = args
                    && fn_sig.inputs()[1..].len() == args.len()
                {
                    // We found a method with the same number of arguments as the method
                    // call expression the user wrote.
                    err.span_suggestion_verbose(
                        span,
                        msg,
                        similar_candidate.name,
                        Applicability::MaybeIncorrect,
                    );
                } else {
                    // We found a method but either the expression is not a method call or
                    // the argument count didn't match.
                    err.span_help(
                        tcx.def_span(similar_candidate.def_id),
                        format!(
                            "{msg}{}",
                            if let None = args { "" } else { ", but with different arguments" },
                        ),
                    );
                }
            } else if let Some(args) = args
                && fn_sig.inputs().len() == args.len()
            {
                // We have fn call expression and the argument count match the associated
                // function we found.
                err.span_suggestion_verbose(
                    span,
                    msg,
                    similar_candidate.name,
                    Applicability::MaybeIncorrect,
                );
            } else {
                err.span_help(tcx.def_span(similar_candidate.def_id), msg);
            }
        } else if let Mode::Path = mode
            && args.unwrap_or(&[]).is_empty()
        {
            // We have an associated item syntax and we found something that isn't an fn.
            err.span_suggestion_verbose(
                span,
                msg,
                similar_candidate.name,
                Applicability::MaybeIncorrect,
            );
        } else {
            // The expression is a function or method call, but the item we found is an
            // associated const or type.
            err.span_help(tcx.def_span(similar_candidate.def_id), msg);
        }
    }

    pub(crate) fn confusable_method_name(
        &self,
        err: &mut Diag<'_>,
        rcvr_ty: Ty<'tcx>,
        item_name: Ident,
        call_args: Option<Vec<Ty<'tcx>>>,
    ) -> Option<Symbol> {
        if let ty::Adt(adt, adt_args) = rcvr_ty.kind() {
            for inherent_impl_did in self.tcx.inherent_impls(adt.did()).into_iter().flatten() {
                for inherent_method in
                    self.tcx.associated_items(inherent_impl_did).in_definition_order()
                {
                    if let Some(attr) =
                        self.tcx.get_attr(inherent_method.def_id, sym::rustc_confusables)
                        && let Some(candidates) = parse_confusables(attr)
                        && candidates.contains(&item_name.name)
                        && let ty::AssocKind::Fn = inherent_method.kind
                    {
                        let args =
                            ty::GenericArgs::identity_for_item(self.tcx, inherent_method.def_id)
                                .rebase_onto(
                                    self.tcx,
                                    inherent_method.container_id(self.tcx),
                                    adt_args,
                                );
                        let fn_sig =
                            self.tcx.fn_sig(inherent_method.def_id).instantiate(self.tcx, args);
                        let fn_sig = self.instantiate_binder_with_fresh_vars(
                            item_name.span,
                            infer::FnCall,
                            fn_sig,
                        );
                        if let Some(ref args) = call_args
                            && fn_sig.inputs()[1..]
                                .iter()
                                .zip(args.into_iter())
                                .all(|(expected, found)| self.can_coerce(*expected, *found))
                            && fn_sig.inputs()[1..].len() == args.len()
                        {
                            err.span_suggestion_verbose(
                                item_name.span,
                                format!("you might have meant to use `{}`", inherent_method.name),
                                inherent_method.name,
                                Applicability::MaybeIncorrect,
                            );
                            return Some(inherent_method.name);
                        } else if let None = call_args {
                            err.span_note(
                                self.tcx.def_span(inherent_method.def_id),
                                format!(
                                    "you might have meant to use method `{}`",
                                    inherent_method.name,
                                ),
                            );
                            return Some(inherent_method.name);
                        }
                    }
                }
            }
        }
        None
    }
    fn note_candidates_on_method_error(
        &self,
        rcvr_ty: Ty<'tcx>,
        item_name: Ident,
        self_source: SelfSource<'tcx>,
        args: Option<&'tcx [hir::Expr<'tcx>]>,
        span: Span,
        err: &mut Diag<'_>,
        sources: &mut Vec<CandidateSource>,
        sugg_span: Option<Span>,
    ) {
        sources.sort_by_key(|source| match source {
            CandidateSource::Trait(id) => (0, self.tcx.def_path_str(id)),
            CandidateSource::Impl(id) => (1, self.tcx.def_path_str(id)),
        });
        sources.dedup();
        // Dynamic limit to avoid hiding just one candidate, which is silly.
        let limit = if sources.len() == 5 { 5 } else { 4 };

        let mut suggs = vec![];
        for (idx, source) in sources.iter().take(limit).enumerate() {
            match *source {
                CandidateSource::Impl(impl_did) => {
                    // Provide the best span we can. Use the item, if local to crate, else
                    // the impl, if local to crate (item may be defaulted), else nothing.
                    let Some(item) = self.associated_value(impl_did, item_name).or_else(|| {
                        let impl_trait_ref = self.tcx.impl_trait_ref(impl_did)?;
                        self.associated_value(impl_trait_ref.skip_binder().def_id, item_name)
                    }) else {
                        continue;
                    };

                    let note_span = if item.def_id.is_local() {
                        Some(self.tcx.def_span(item.def_id))
                    } else if impl_did.is_local() {
                        Some(self.tcx.def_span(impl_did))
                    } else {
                        None
                    };

                    let impl_ty = self.tcx.at(span).type_of(impl_did).instantiate_identity();

                    let insertion = match self.tcx.impl_trait_ref(impl_did) {
                        None => String::new(),
                        Some(trait_ref) => {
                            format!(
                                " of the trait `{}`",
                                self.tcx.def_path_str(trait_ref.skip_binder().def_id)
                            )
                        }
                    };

                    let (note_str, idx) = if sources.len() > 1 {
                        (
                            format!(
                                "candidate #{} is defined in an impl{} for the type `{}`",
                                idx + 1,
                                insertion,
                                impl_ty,
                            ),
                            Some(idx + 1),
                        )
                    } else {
                        (
                            format!(
                                "the candidate is defined in an impl{insertion} for the type `{impl_ty}`",
                            ),
                            None,
                        )
                    };
                    if let Some(note_span) = note_span {
                        // We have a span pointing to the method. Show note with snippet.
                        err.span_note(note_span, note_str);
                    } else {
                        err.note(note_str);
                    }
                    if let Some(sugg_span) = sugg_span
                        && let Some(trait_ref) = self.tcx.impl_trait_ref(impl_did)
                        && let Some(sugg) = print_disambiguation_help(
                            self.tcx,
                            err,
                            self_source,
                            args,
                            trait_ref
                                .instantiate(
                                    self.tcx,
                                    self.fresh_args_for_item(sugg_span, impl_did),
                                )
                                .with_self_ty(self.tcx, rcvr_ty),
                            idx,
                            sugg_span,
                            item,
                        )
                    {
                        suggs.push(sugg);
                    }
                }
                CandidateSource::Trait(trait_did) => {
                    let Some(item) = self.associated_value(trait_did, item_name) else { continue };
                    let item_span = self.tcx.def_span(item.def_id);
                    let idx = if sources.len() > 1 {
                        let msg = format!(
                            "candidate #{} is defined in the trait `{}`",
                            idx + 1,
                            self.tcx.def_path_str(trait_did)
                        );
                        err.span_note(item_span, msg);
                        Some(idx + 1)
                    } else {
                        let msg = format!(
                            "the candidate is defined in the trait `{}`",
                            self.tcx.def_path_str(trait_did)
                        );
                        err.span_note(item_span, msg);
                        None
                    };
                    if let Some(sugg_span) = sugg_span
                        && let Some(sugg) = print_disambiguation_help(
                            self.tcx,
                            err,
                            self_source,
                            args,
                            ty::TraitRef::new_from_args(
                                self.tcx,
                                trait_did,
                                self.fresh_args_for_item(sugg_span, trait_did),
                            )
                            .with_self_ty(self.tcx, rcvr_ty),
                            idx,
                            sugg_span,
                            item,
                        )
                    {
                        suggs.push(sugg);
                    }
                }
            }
        }
        if !suggs.is_empty()
            && let Some(span) = sugg_span
        {
            suggs.sort();
            err.span_suggestions(
                span.with_hi(item_name.span.lo()),
                "use fully-qualified syntax to disambiguate",
                suggs,
                Applicability::MachineApplicable,
            );
        }
        if sources.len() > limit {
            err.note(format!("and {} others", sources.len() - limit));
        }
    }

    /// Look at all the associated functions without receivers in the type's inherent impls
    /// to look for builders that return `Self`, `Option<Self>` or `Result<Self, _>`.
    fn find_builder_fn(&self, err: &mut Diag<'_>, rcvr_ty: Ty<'tcx>, expr_id: hir::HirId) {
        let ty::Adt(adt_def, _) = rcvr_ty.kind() else {
            return;
        };
        // FIXME(oli-obk): try out bubbling this error up one level and cancelling the other error in that case.
        let Ok(impls) = self.tcx.inherent_impls(adt_def.did()) else { return };
        let mut items = impls
            .iter()
            .flat_map(|i| self.tcx.associated_items(i).in_definition_order())
            // Only assoc fn with no receivers and only if
            // they are resolvable
            .filter(|item| {
                matches!(item.kind, ty::AssocKind::Fn)
                    && !item.fn_has_self_parameter
                    && self
                        .probe_for_name(
                            Mode::Path,
                            item.ident(self.tcx),
                            None,
                            IsSuggestion(true),
                            rcvr_ty,
                            expr_id,
                            ProbeScope::TraitsInScope,
                        )
                        .is_ok()
            })
            .filter_map(|item| {
                // Only assoc fns that return `Self`, `Option<Self>` or `Result<Self, _>`.
                let ret_ty = self
                    .tcx
                    .fn_sig(item.def_id)
                    .instantiate(self.tcx, self.fresh_args_for_item(DUMMY_SP, item.def_id))
                    .output();
                let ret_ty = self.tcx.instantiate_bound_regions_with_erased(ret_ty);
                let ty::Adt(def, args) = ret_ty.kind() else {
                    return None;
                };
                // Check for `-> Self`
                if self.can_eq(self.param_env, ret_ty, rcvr_ty) {
                    return Some((item.def_id, ret_ty));
                }
                // Check for `-> Option<Self>` or `-> Result<Self, _>`
                if ![self.tcx.lang_items().option_type(), self.tcx.get_diagnostic_item(sym::Result)]
                    .contains(&Some(def.did()))
                {
                    return None;
                }
                let arg = args.get(0)?.expect_ty();
                if self.can_eq(self.param_env, rcvr_ty, arg) {
                    Some((item.def_id, ret_ty))
                } else {
                    None
                }
            })
            .collect::<Vec<_>>();
        let post = if items.len() > 5 {
            let items_len = items.len();
            items.truncate(4);
            format!("\nand {} others", items_len - 4)
        } else {
            String::new()
        };
        match &items[..] {
            [] => {}
            [(def_id, ret_ty)] => {
                err.span_note(
                    self.tcx.def_span(def_id),
                    format!(
                        "if you're trying to build a new `{rcvr_ty}`, consider using `{}` which \
                         returns `{ret_ty}`",
                        self.tcx.def_path_str(def_id),
                    ),
                );
            }
            _ => {
                let span: MultiSpan = items
                    .iter()
                    .map(|(def_id, _)| self.tcx.def_span(def_id))
                    .collect::<Vec<Span>>()
                    .into();
                err.span_note(
                    span,
                    format!(
                        "if you're trying to build a new `{rcvr_ty}` consider using one of the \
                         following associated functions:\n{}{post}",
                        items
                            .iter()
                            .map(|(def_id, _ret_ty)| self.tcx.def_path_str(def_id))
                            .collect::<Vec<String>>()
                            .join("\n")
                    ),
                );
            }
        }
    }

    /// Suggest calling `Ty::method` if `.method()` isn't found because the method
    /// doesn't take a `self` receiver.
    fn suggest_associated_call_syntax(
        &self,
        err: &mut Diag<'_>,
        static_candidates: &Vec<CandidateSource>,
        rcvr_ty: Ty<'tcx>,
        source: SelfSource<'tcx>,
        item_name: Ident,
        args: Option<&'tcx [hir::Expr<'tcx>]>,
        sugg_span: Span,
    ) {
        let mut has_unsuggestable_args = false;
        let ty_str = if let Some(CandidateSource::Impl(impl_did)) = static_candidates.get(0) {
            // When the "method" is resolved through dereferencing, we really want the
            // original type that has the associated function for accurate suggestions.
            // (#61411)
            let impl_ty = self.tcx.type_of(*impl_did).instantiate_identity();
            let target_ty = self
                .autoderef(sugg_span, rcvr_ty)
                .find(|(rcvr_ty, _)| {
                    DeepRejectCtxt::new(self.tcx, TreatParams::ForLookup)
                        .types_may_unify(*rcvr_ty, impl_ty)
                })
                .map_or(impl_ty, |(ty, _)| ty)
                .peel_refs();
            if let ty::Adt(def, args) = target_ty.kind() {
                // If there are any inferred arguments, (`{integer}`), we should replace
                // them with underscores to allow the compiler to infer them
                let infer_args = self.tcx.mk_args_from_iter(args.into_iter().map(|arg| {
                    if !arg.is_suggestable(self.tcx, true) {
                        has_unsuggestable_args = true;
                        match arg.unpack() {
                            GenericArgKind::Lifetime(_) => self
                                .next_region_var(RegionVariableOrigin::MiscVariable(DUMMY_SP))
                                .into(),
                            GenericArgKind::Type(_) => self.next_ty_var(DUMMY_SP).into(),
                            GenericArgKind::Const(_) => self.next_const_var(DUMMY_SP).into(),
                        }
                    } else {
                        arg
                    }
                }));

                self.tcx.value_path_str_with_args(def.did(), infer_args)
            } else {
                self.ty_to_value_string(target_ty)
            }
        } else {
            self.ty_to_value_string(rcvr_ty.peel_refs())
        };
        if let SelfSource::MethodCall(_) = source {
            let first_arg = static_candidates.get(0).and_then(|candidate_source| {
                let (assoc_did, self_ty) = match candidate_source {
                    CandidateSource::Impl(impl_did) => {
                        (*impl_did, self.tcx.type_of(*impl_did).instantiate_identity())
                    }
                    CandidateSource::Trait(trait_did) => (*trait_did, rcvr_ty),
                };

                let assoc = self.associated_value(assoc_did, item_name)?;
                if assoc.kind != ty::AssocKind::Fn {
                    return None;
                }

                // for CandidateSource::Impl, `Self` will be instantiated to a concrete type
                // but for CandidateSource::Trait, `Self` is still `Self`
                let sig = self.tcx.fn_sig(assoc.def_id).instantiate_identity();
                sig.inputs().skip_binder().get(0).and_then(|first| {
                    // if the type of first arg is the same as the current impl type, we should take the first arg into assoc function
                    let first_ty = first.peel_refs();
                    if first_ty == self_ty || first_ty == self.tcx.types.self_param {
                        Some(first.ref_mutability().map_or("", |mutbl| mutbl.ref_prefix_str()))
                    } else {
                        None
                    }
                })
            });

            let mut applicability = Applicability::MachineApplicable;
            let args = if let SelfSource::MethodCall(receiver) = source
                && let Some(args) = args
            {
                // The first arg is the same kind as the receiver
                let explicit_args = if first_arg.is_some() {
                    std::iter::once(receiver).chain(args.iter()).collect::<Vec<_>>()
                } else {
                    // There is no `Self` kind to infer the arguments from
                    if has_unsuggestable_args {
                        applicability = Applicability::HasPlaceholders;
                    }
                    args.iter().collect()
                };
                format!(
                    "({}{})",
                    first_arg.unwrap_or(""),
                    explicit_args
                        .iter()
                        .map(|arg| self
                            .tcx
                            .sess
                            .source_map()
                            .span_to_snippet(arg.span)
                            .unwrap_or_else(|_| {
                                applicability = Applicability::HasPlaceholders;
                                "_".to_owned()
                            }))
                        .collect::<Vec<_>>()
                        .join(", "),
                )
            } else {
                applicability = Applicability::HasPlaceholders;
                "(...)".to_owned()
            };
            err.span_suggestion(
                sugg_span,
                "use associated function syntax instead",
                format!("{ty_str}::{item_name}{args}"),
                applicability,
            );
        } else {
            err.help(format!("try with `{ty_str}::{item_name}`",));
        }
    }

    /// Suggest calling a field with a type that implements the `Fn*` traits instead of a method with
    /// the same name as the field i.e. `(a.my_fn_ptr)(10)` instead of `a.my_fn_ptr(10)`.
    fn suggest_calling_field_as_fn(
        &self,
        span: Span,
        rcvr_ty: Ty<'tcx>,
        expr: &hir::Expr<'_>,
        item_name: Ident,
        err: &mut Diag<'_>,
    ) -> bool {
        let tcx = self.tcx;
        let field_receiver = self.autoderef(span, rcvr_ty).find_map(|(ty, _)| match ty.kind() {
            ty::Adt(def, args) if !def.is_enum() => {
                let variant = &def.non_enum_variant();
                tcx.find_field_index(item_name, variant).map(|index| {
                    let field = &variant.fields[index];
                    let field_ty = field.ty(tcx, args);
                    (field, field_ty)
                })
            }
            _ => None,
        });
        if let Some((field, field_ty)) = field_receiver {
            let scope = tcx.parent_module_from_def_id(self.body_id);
            let is_accessible = field.vis.is_accessible_from(scope, tcx);

            if is_accessible {
                if self.is_fn_ty(field_ty, span) {
                    let expr_span = expr.span.to(item_name.span);
                    err.multipart_suggestion(
                        format!(
                            "to call the function stored in `{item_name}`, \
                                         surround the field access with parentheses",
                        ),
                        vec![
                            (expr_span.shrink_to_lo(), '('.to_string()),
                            (expr_span.shrink_to_hi(), ')'.to_string()),
                        ],
                        Applicability::MachineApplicable,
                    );
                } else {
                    let call_expr = tcx.hir().expect_expr(tcx.parent_hir_id(expr.hir_id));

                    if let Some(span) = call_expr.span.trim_start(item_name.span) {
                        err.span_suggestion(
                            span,
                            "remove the arguments",
                            "",
                            Applicability::MaybeIncorrect,
                        );
                    }
                }
            }

            let field_kind = if is_accessible { "field" } else { "private field" };
            err.span_label(item_name.span, format!("{field_kind}, not a method"));
            return true;
        }
        false
    }

    /// Suggest possible range with adding parentheses, for example:
    /// when encountering `0..1.map(|i| i + 1)` suggest `(0..1).map(|i| i + 1)`.
    fn report_failed_method_call_on_range_end(
        &self,
        tcx: TyCtxt<'tcx>,
        actual: Ty<'tcx>,
        source: SelfSource<'tcx>,
        span: Span,
        item_name: Ident,
        ty_str: &str,
    ) -> Result<(), ErrorGuaranteed> {
        if let SelfSource::MethodCall(expr) = source {
            for (_, parent) in tcx.hir().parent_iter(expr.hir_id).take(5) {
                if let Node::Expr(parent_expr) = parent {
                    let lang_item = match parent_expr.kind {
                        ExprKind::Struct(qpath, _, _) => match *qpath {
                            QPath::LangItem(LangItem::Range, ..) => Some(LangItem::Range),
                            QPath::LangItem(LangItem::RangeTo, ..) => Some(LangItem::RangeTo),
                            QPath::LangItem(LangItem::RangeToInclusive, ..) => {
                                Some(LangItem::RangeToInclusive)
                            }
                            _ => None,
                        },
                        ExprKind::Call(func, _) => match func.kind {
                            // `..=` desugars into `::std::ops::RangeInclusive::new(...)`.
                            ExprKind::Path(QPath::LangItem(LangItem::RangeInclusiveNew, ..)) => {
                                Some(LangItem::RangeInclusiveStruct)
                            }
                            _ => None,
                        },
                        _ => None,
                    };

                    if lang_item.is_none() {
                        continue;
                    }

                    let span_included = match parent_expr.kind {
                        hir::ExprKind::Struct(_, eps, _) => {
                            eps.len() > 0 && eps.last().is_some_and(|ep| ep.span.contains(span))
                        }
                        // `..=` desugars into `::std::ops::RangeInclusive::new(...)`.
                        hir::ExprKind::Call(func, ..) => func.span.contains(span),
                        _ => false,
                    };

                    if !span_included {
                        continue;
                    }

                    let Some(range_def_id) =
                        lang_item.and_then(|lang_item| self.tcx.lang_items().get(lang_item))
                    else {
                        continue;
                    };
                    let range_ty =
                        self.tcx.type_of(range_def_id).instantiate(self.tcx, &[actual.into()]);

                    let pick = self.lookup_probe_for_diagnostic(
                        item_name,
                        range_ty,
                        expr,
                        ProbeScope::AllTraits,
                        None,
                    );
                    if pick.is_ok() {
                        let range_span = parent_expr.span.with_hi(expr.span.hi());
                        return Err(self.dcx().emit_err(errors::MissingParenthesesInRange {
                            span,
                            ty_str: ty_str.to_string(),
                            method_name: item_name.as_str().to_string(),
                            add_missing_parentheses: Some(errors::AddMissingParenthesesInRange {
                                func_name: item_name.name.as_str().to_string(),
                                left: range_span.shrink_to_lo(),
                                right: range_span.shrink_to_hi(),
                            }),
                        }));
                    }
                }
            }
        }
        Ok(())
    }

    fn report_failed_method_call_on_numerical_infer_var(
        &self,
        tcx: TyCtxt<'tcx>,
        actual: Ty<'tcx>,
        source: SelfSource<'_>,
        span: Span,
        item_kind: &str,
        item_name: Ident,
        ty_str: &str,
    ) -> Result<(), ErrorGuaranteed> {
        let found_candidate = all_traits(self.tcx)
            .into_iter()
            .any(|info| self.associated_value(info.def_id, item_name).is_some());
        let found_assoc = |ty: Ty<'tcx>| {
            simplify_type(tcx, ty, TreatParams::AsCandidateKey)
                .and_then(|simp| {
                    tcx.incoherent_impls(simp)
                        .into_iter()
                        .flatten()
                        .find_map(|&id| self.associated_value(id, item_name))
                })
                .is_some()
        };
        let found_candidate = found_candidate
            || found_assoc(tcx.types.i8)
            || found_assoc(tcx.types.i16)
            || found_assoc(tcx.types.i32)
            || found_assoc(tcx.types.i64)
            || found_assoc(tcx.types.i128)
            || found_assoc(tcx.types.u8)
            || found_assoc(tcx.types.u16)
            || found_assoc(tcx.types.u32)
            || found_assoc(tcx.types.u64)
            || found_assoc(tcx.types.u128)
            || found_assoc(tcx.types.f32)
            || found_assoc(tcx.types.f64);
        if found_candidate
            && actual.is_numeric()
            && !actual.has_concrete_skeleton()
            && let SelfSource::MethodCall(expr) = source
        {
            let mut err = struct_span_code_err!(
                self.dcx(),
                span,
                E0689,
                "can't call {} `{}` on ambiguous numeric type `{}`",
                item_kind,
                item_name,
                ty_str
            );
            let concrete_type = if actual.is_integral() { "i32" } else { "f32" };
            match expr.kind {
                ExprKind::Lit(lit) => {
                    // numeric literal
                    let snippet = tcx
                        .sess
                        .source_map()
                        .span_to_snippet(lit.span)
                        .unwrap_or_else(|_| "<numeric literal>".to_owned());

                    // If this is a floating point literal that ends with '.',
                    // get rid of it to stop this from becoming a member access.
                    let snippet = snippet.strip_suffix('.').unwrap_or(&snippet);
                    err.span_suggestion(
                        lit.span,
                        format!(
                            "you must specify a concrete type for this numeric value, \
                                         like `{concrete_type}`"
                        ),
                        format!("{snippet}_{concrete_type}"),
                        Applicability::MaybeIncorrect,
                    );
                }
                ExprKind::Path(QPath::Resolved(_, path)) => {
                    // local binding
                    if let hir::def::Res::Local(hir_id) = path.res {
                        let span = tcx.hir().span(hir_id);
                        let filename = tcx.sess.source_map().span_to_filename(span);

                        let parent_node = self.tcx.parent_hir_node(hir_id);
                        let msg = format!(
                            "you must specify a type for this binding, like `{concrete_type}`",
                        );

                        match (filename, parent_node) {
                            (
                                FileName::Real(_),
                                Node::LetStmt(hir::LetStmt {
                                    source: hir::LocalSource::Normal,
                                    ty,
                                    ..
                                }),
                            ) => {
                                let type_span = ty
                                    .map(|ty| ty.span.with_lo(span.hi()))
                                    .unwrap_or(span.shrink_to_hi());
                                err.span_suggestion(
                                    // account for `let x: _ = 42;`
                                    //                   ^^^
                                    type_span,
                                    msg,
                                    format!(": {concrete_type}"),
                                    Applicability::MaybeIncorrect,
                                );
                            }
                            _ => {
                                err.span_label(span, msg);
                            }
                        }
                    }
                }
                _ => {}
            }
            return Err(err.emit());
        }
        Ok(())
    }

    /// For code `rect::area(...)`,
    /// if `rect` is a local variable and `area` is a valid assoc method for it,
    /// we try to suggest `rect.area()`
    pub(crate) fn suggest_assoc_method_call(&self, segs: &[PathSegment<'_>]) {
        debug!("suggest_assoc_method_call segs: {:?}", segs);
        let [seg1, seg2] = segs else {
            return;
        };
        self.dcx().try_steal_modify_and_emit_err(
            seg1.ident.span,
            StashKey::CallAssocMethod,
            |err| {
                let body = self.tcx.hir().body_owned_by(self.body_id);
                struct LetVisitor {
                    ident_name: Symbol,
                }

                // FIXME: This really should be taking scoping, etc into account.
                impl<'v> Visitor<'v> for LetVisitor {
                    type Result = ControlFlow<Option<&'v hir::Expr<'v>>>;
                    fn visit_stmt(&mut self, ex: &'v hir::Stmt<'v>) -> Self::Result {
                        if let hir::StmtKind::Let(&hir::LetStmt { pat, init, .. }) = ex.kind
                            && let hir::PatKind::Binding(_, _, ident, ..) = pat.kind
                            && ident.name == self.ident_name
                        {
                            ControlFlow::Break(init)
                        } else {
                            hir::intravisit::walk_stmt(self, ex)
                        }
                    }
                }

                if let Node::Expr(call_expr) = self.tcx.parent_hir_node(seg1.hir_id)
                    && let ControlFlow::Break(Some(expr)) =
                        (LetVisitor { ident_name: seg1.ident.name }).visit_body(&body)
                    && let Some(self_ty) = self.node_ty_opt(expr.hir_id)
                {
                    let probe = self.lookup_probe_for_diagnostic(
                        seg2.ident,
                        self_ty,
                        call_expr,
                        ProbeScope::TraitsInScope,
                        None,
                    );
                    if probe.is_ok() {
                        let sm = self.infcx.tcx.sess.source_map();
                        err.span_suggestion_verbose(
                            sm.span_extend_while(seg1.ident.span.shrink_to_hi(), |c| c == ':')
                                .unwrap(),
                            "you may have meant to call an instance method",
                            ".",
                            Applicability::MaybeIncorrect,
                        );
                    }
                }
            },
        );
    }

    /// Suggest calling a method on a field i.e. `a.field.bar()` instead of `a.bar()`
    fn suggest_calling_method_on_field(
        &self,
        err: &mut Diag<'_>,
        source: SelfSource<'tcx>,
        span: Span,
        actual: Ty<'tcx>,
        item_name: Ident,
        return_type: Option<Ty<'tcx>>,
    ) {
        if let SelfSource::MethodCall(expr) = source {
            let mod_id = self.tcx.parent_module(expr.hir_id).to_def_id();
            for (fields, args) in
                self.get_field_candidates_considering_privacy(span, actual, mod_id, expr.hir_id)
            {
                let call_expr = self.tcx.hir().expect_expr(self.tcx.parent_hir_id(expr.hir_id));

                let lang_items = self.tcx.lang_items();
                let never_mention_traits = [
                    lang_items.clone_trait(),
                    lang_items.deref_trait(),
                    lang_items.deref_mut_trait(),
                    self.tcx.get_diagnostic_item(sym::AsRef),
                    self.tcx.get_diagnostic_item(sym::AsMut),
                    self.tcx.get_diagnostic_item(sym::Borrow),
                    self.tcx.get_diagnostic_item(sym::BorrowMut),
                ];
                let mut candidate_fields: Vec<_> = fields
                    .into_iter()
                    .filter_map(|candidate_field| {
                        self.check_for_nested_field_satisfying(
                            span,
                            &|_, field_ty| {
                                self.lookup_probe_for_diagnostic(
                                    item_name,
                                    field_ty,
                                    call_expr,
                                    ProbeScope::TraitsInScope,
                                    return_type,
                                )
                                .is_ok_and(|pick| {
                                    !never_mention_traits
                                        .iter()
                                        .flatten()
                                        .any(|def_id| self.tcx.parent(pick.item.def_id) == *def_id)
                                })
                            },
                            candidate_field,
                            args,
                            vec![],
                            mod_id,
                            expr.hir_id,
                        )
                    })
                    .map(|field_path| {
                        field_path
                            .iter()
                            .map(|id| id.name.to_ident_string())
                            .collect::<Vec<String>>()
                            .join(".")
                    })
                    .collect();
                candidate_fields.sort();

                let len = candidate_fields.len();
                if len > 0 {
                    err.span_suggestions(
                        item_name.span.shrink_to_lo(),
                        format!(
                            "{} of the expressions' fields {} a method of the same name",
                            if len > 1 { "some" } else { "one" },
                            if len > 1 { "have" } else { "has" },
                        ),
                        candidate_fields.iter().map(|path| format!("{path}.")),
                        Applicability::MaybeIncorrect,
                    );
                }
            }
        }
    }

    fn suggest_unwrapping_inner_self(
        &self,
        err: &mut Diag<'_>,
        source: SelfSource<'tcx>,
        actual: Ty<'tcx>,
        item_name: Ident,
    ) {
        let tcx = self.tcx;
        let SelfSource::MethodCall(expr) = source else {
            return;
        };
        let call_expr = tcx.hir().expect_expr(tcx.parent_hir_id(expr.hir_id));

        let ty::Adt(kind, args) = actual.kind() else {
            return;
        };
        match kind.adt_kind() {
            ty::AdtKind::Enum => {
                let matching_variants: Vec<_> = kind
                    .variants()
                    .iter()
                    .flat_map(|variant| {
                        let [field] = &variant.fields.raw[..] else {
                            return None;
                        };
                        let field_ty = field.ty(tcx, args);

                        // Skip `_`, since that'll just lead to ambiguity.
                        if self.resolve_vars_if_possible(field_ty).is_ty_var() {
                            return None;
                        }

                        self.lookup_probe_for_diagnostic(
                            item_name,
                            field_ty,
                            call_expr,
                            ProbeScope::TraitsInScope,
                            None,
                        )
                        .ok()
                        .map(|pick| (variant, field, pick))
                    })
                    .collect();

                let ret_ty_matches = |diagnostic_item| {
                    if let Some(ret_ty) = self
                        .ret_coercion
                        .as_ref()
                        .map(|c| self.resolve_vars_if_possible(c.borrow().expected_ty()))
                        && let ty::Adt(kind, _) = ret_ty.kind()
                        && tcx.get_diagnostic_item(diagnostic_item) == Some(kind.did())
                    {
                        true
                    } else {
                        false
                    }
                };

                match &matching_variants[..] {
                    [(_, field, pick)] => {
                        let self_ty = field.ty(tcx, args);
                        err.span_note(
                            tcx.def_span(pick.item.def_id),
                            format!("the method `{item_name}` exists on the type `{self_ty}`"),
                        );
                        let (article, kind, variant, question) =
                            if tcx.is_diagnostic_item(sym::Result, kind.did()) {
                                ("a", "Result", "Err", ret_ty_matches(sym::Result))
                            } else if tcx.is_diagnostic_item(sym::Option, kind.did()) {
                                ("an", "Option", "None", ret_ty_matches(sym::Option))
                            } else {
                                return;
                            };
                        if question {
                            err.span_suggestion_verbose(
                                expr.span.shrink_to_hi(),
                                format!(
                                    "use the `?` operator to extract the `{self_ty}` value, propagating \
                                    {article} `{kind}::{variant}` value to the caller"
                                ),
                                "?",
                                Applicability::MachineApplicable,
                            );
                        } else {
                            err.span_suggestion_verbose(
                                expr.span.shrink_to_hi(),
                                format!(
                                    "consider using `{kind}::expect` to unwrap the `{self_ty}` value, \
                                    panicking if the value is {article} `{kind}::{variant}`"
                                ),
                                ".expect(\"REASON\")",
                                Applicability::HasPlaceholders,
                            );
                        }
                    }
                    // FIXME(compiler-errors): Support suggestions for other matching enum variants
                    _ => {}
                }
            }
            // Target wrapper types - types that wrap or pretend to wrap another type,
            // perhaps this inner type is meant to be called?
            ty::AdtKind::Struct | ty::AdtKind::Union => {
                let [first] = ***args else {
                    return;
                };
                let ty::GenericArgKind::Type(ty) = first.unpack() else {
                    return;
                };
                let Ok(pick) = self.lookup_probe_for_diagnostic(
                    item_name,
                    ty,
                    call_expr,
                    ProbeScope::TraitsInScope,
                    None,
                ) else {
                    return;
                };

                let name = self.ty_to_value_string(actual);
                let inner_id = kind.did();
                let mutable = if let Some(AutorefOrPtrAdjustment::Autoref { mutbl, .. }) =
                    pick.autoref_or_ptr_adjustment
                {
                    Some(mutbl)
                } else {
                    None
                };

                if tcx.is_diagnostic_item(sym::LocalKey, inner_id) {
                    err.help("use `with` or `try_with` to access thread local storage");
                } else if tcx.is_lang_item(kind.did(), LangItem::MaybeUninit) {
                    err.help(format!(
                        "if this `{name}` has been initialized, \
                        use one of the `assume_init` methods to access the inner value"
                    ));
                } else if tcx.is_diagnostic_item(sym::RefCell, inner_id) {
                    let (suggestion, borrow_kind, panic_if) = match mutable {
                        Some(Mutability::Not) => (".borrow()", "borrow", "a mutable borrow exists"),
                        Some(Mutability::Mut) => {
                            (".borrow_mut()", "mutably borrow", "any borrows exist")
                        }
                        None => return,
                    };
                    err.span_suggestion_verbose(
                        expr.span.shrink_to_hi(),
                        format!(
                            "use `{suggestion}` to {borrow_kind} the `{ty}`, \
                            panicking if {panic_if}"
                        ),
                        suggestion,
                        Applicability::MaybeIncorrect,
                    );
                } else if tcx.is_diagnostic_item(sym::Mutex, inner_id) {
                    err.span_suggestion_verbose(
                        expr.span.shrink_to_hi(),
                        format!(
                            "use `.lock().unwrap()` to borrow the `{ty}`, \
                            blocking the current thread until it can be acquired"
                        ),
                        ".lock().unwrap()",
                        Applicability::MaybeIncorrect,
                    );
                } else if tcx.is_diagnostic_item(sym::RwLock, inner_id) {
                    let (suggestion, borrow_kind) = match mutable {
                        Some(Mutability::Not) => (".read().unwrap()", "borrow"),
                        Some(Mutability::Mut) => (".write().unwrap()", "mutably borrow"),
                        None => return,
                    };
                    err.span_suggestion_verbose(
                        expr.span.shrink_to_hi(),
                        format!(
                            "use `{suggestion}` to {borrow_kind} the `{ty}`, \
                            blocking the current thread until it can be acquired"
                        ),
                        suggestion,
                        Applicability::MaybeIncorrect,
                    );
                } else {
                    return;
                };

                err.span_note(
                    tcx.def_span(pick.item.def_id),
                    format!("the method `{item_name}` exists on the type `{ty}`"),
                );
            }
        }
    }

    pub(crate) fn note_unmet_impls_on_type(
        &self,
        err: &mut Diag<'_>,
        errors: Vec<FulfillmentError<'tcx>>,
        suggest_derive: bool,
    ) {
        let preds: Vec<_> = errors
            .iter()
            .filter_map(|e| match e.obligation.predicate.kind().skip_binder() {
                ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred)) => {
                    match pred.self_ty().kind() {
                        ty::Adt(_, _) => Some(pred),
                        _ => None,
                    }
                }
                _ => None,
            })
            .collect();

        // Note for local items and foreign items respectively.
        let (mut local_preds, mut foreign_preds): (Vec<_>, Vec<_>) =
            preds.iter().partition(|&pred| {
                if let ty::Adt(def, _) = pred.self_ty().kind() {
                    def.did().is_local()
                } else {
                    false
                }
            });

        local_preds.sort_by_key(|pred: &&ty::TraitPredicate<'_>| pred.trait_ref.to_string());
        let local_def_ids = local_preds
            .iter()
            .filter_map(|pred| match pred.self_ty().kind() {
                ty::Adt(def, _) => Some(def.did()),
                _ => None,
            })
            .collect::<FxIndexSet<_>>();
        let mut local_spans: MultiSpan = local_def_ids
            .iter()
            .filter_map(|def_id| {
                let span = self.tcx.def_span(*def_id);
                if span.is_dummy() { None } else { Some(span) }
            })
            .collect::<Vec<_>>()
            .into();
        for pred in &local_preds {
            match pred.self_ty().kind() {
                ty::Adt(def, _) => {
                    local_spans.push_span_label(
                        self.tcx.def_span(def.did()),
                        format!("must implement `{}`", pred.trait_ref.print_trait_sugared()),
                    );
                }
                _ => {}
            }
        }
        if local_spans.primary_span().is_some() {
            let msg = if let [local_pred] = local_preds.as_slice() {
                format!(
                    "an implementation of `{}` might be missing for `{}`",
                    local_pred.trait_ref.print_trait_sugared(),
                    local_pred.self_ty()
                )
            } else {
                format!(
                    "the following type{} would have to `impl` {} required trait{} for this \
                     operation to be valid",
                    pluralize!(local_def_ids.len()),
                    if local_def_ids.len() == 1 { "its" } else { "their" },
                    pluralize!(local_preds.len()),
                )
            };
            err.span_note(local_spans, msg);
        }

        foreign_preds.sort_by_key(|pred: &&ty::TraitPredicate<'_>| pred.trait_ref.to_string());
        let foreign_def_ids = foreign_preds
            .iter()
            .filter_map(|pred| match pred.self_ty().kind() {
                ty::Adt(def, _) => Some(def.did()),
                _ => None,
            })
            .collect::<FxIndexSet<_>>();
        let mut foreign_spans: MultiSpan = foreign_def_ids
            .iter()
            .filter_map(|def_id| {
                let span = self.tcx.def_span(*def_id);
                if span.is_dummy() { None } else { Some(span) }
            })
            .collect::<Vec<_>>()
            .into();
        for pred in &foreign_preds {
            match pred.self_ty().kind() {
                ty::Adt(def, _) => {
                    foreign_spans.push_span_label(
                        self.tcx.def_span(def.did()),
                        format!("not implement `{}`", pred.trait_ref.print_trait_sugared()),
                    );
                }
                _ => {}
            }
        }
        if foreign_spans.primary_span().is_some() {
            let msg = if let [foreign_pred] = foreign_preds.as_slice() {
                format!(
                    "the foreign item type `{}` doesn't implement `{}`",
                    foreign_pred.self_ty(),
                    foreign_pred.trait_ref.print_trait_sugared()
                )
            } else {
                format!(
                    "the foreign item type{} {} implement required trait{} for this \
                     operation to be valid",
                    pluralize!(foreign_def_ids.len()),
                    if foreign_def_ids.len() > 1 { "don't" } else { "doesn't" },
                    pluralize!(foreign_preds.len()),
                )
            };
            err.span_note(foreign_spans, msg);
        }

        let preds: Vec<_> = errors
            .iter()
            .map(|e| (e.obligation.predicate, None, Some(e.obligation.cause.clone())))
            .collect();
        if suggest_derive {
            self.suggest_derive(err, &preds);
        } else {
            // The predicate comes from a binop where the lhs and rhs have different types.
            let _ = self.note_predicate_source_and_get_derives(err, &preds);
        }
    }

    fn note_predicate_source_and_get_derives(
        &self,
        err: &mut Diag<'_>,
        unsatisfied_predicates: &[(
            ty::Predicate<'tcx>,
            Option<ty::Predicate<'tcx>>,
            Option<ObligationCause<'tcx>>,
        )],
    ) -> Vec<(String, Span, Symbol)> {
        let mut derives = Vec::<(String, Span, Symbol)>::new();
        let mut traits = Vec::new();
        for (pred, _, _) in unsatisfied_predicates {
            let Some(ty::PredicateKind::Clause(ty::ClauseKind::Trait(trait_pred))) =
                pred.kind().no_bound_vars()
            else {
                continue;
            };
            let adt = match trait_pred.self_ty().ty_adt_def() {
                Some(adt) if adt.did().is_local() => adt,
                _ => continue,
            };
            if let Some(diagnostic_name) = self.tcx.get_diagnostic_name(trait_pred.def_id()) {
                let can_derive = match diagnostic_name {
                    sym::Default => !adt.is_enum(),
                    sym::Eq
                    | sym::PartialEq
                    | sym::Ord
                    | sym::PartialOrd
                    | sym::Clone
                    | sym::Copy
                    | sym::Hash
                    | sym::Debug => true,
                    _ => false,
                };
                if can_derive {
                    let self_name = trait_pred.self_ty().to_string();
                    let self_span = self.tcx.def_span(adt.did());
                    for super_trait in
                        supertraits(self.tcx, ty::Binder::dummy(trait_pred.trait_ref))
                    {
                        if let Some(parent_diagnostic_name) =
                            self.tcx.get_diagnostic_name(super_trait.def_id())
                        {
                            derives.push((self_name.clone(), self_span, parent_diagnostic_name));
                        }
                    }
                    derives.push((self_name, self_span, diagnostic_name));
                } else {
                    traits.push(trait_pred.def_id());
                }
            } else {
                traits.push(trait_pred.def_id());
            }
        }
        traits.sort_by_key(|id| self.tcx.def_path_str(id));
        traits.dedup();

        let len = traits.len();
        if len > 0 {
            let span =
                MultiSpan::from_spans(traits.iter().map(|&did| self.tcx.def_span(did)).collect());
            let mut names = format!("`{}`", self.tcx.def_path_str(traits[0]));
            for (i, &did) in traits.iter().enumerate().skip(1) {
                if len > 2 {
                    names.push_str(", ");
                }
                if i == len - 1 {
                    names.push_str(" and ");
                }
                names.push('`');
                names.push_str(&self.tcx.def_path_str(did));
                names.push('`');
            }
            err.span_note(
                span,
                format!("the trait{} {} must be implemented", pluralize!(len), names),
            );
        }

        derives
    }

    pub(crate) fn suggest_derive(
        &self,
        err: &mut Diag<'_>,
        unsatisfied_predicates: &[(
            ty::Predicate<'tcx>,
            Option<ty::Predicate<'tcx>>,
            Option<ObligationCause<'tcx>>,
        )],
    ) -> bool {
        let mut derives = self.note_predicate_source_and_get_derives(err, unsatisfied_predicates);
        derives.sort();
        derives.dedup();

        let mut derives_grouped = Vec::<(String, Span, String)>::new();
        for (self_name, self_span, trait_name) in derives.into_iter() {
            if let Some((last_self_name, _, ref mut last_trait_names)) = derives_grouped.last_mut()
            {
                if last_self_name == &self_name {
                    last_trait_names.push_str(format!(", {trait_name}").as_str());
                    continue;
                }
            }
            derives_grouped.push((self_name, self_span, trait_name.to_string()));
        }

        for (self_name, self_span, traits) in &derives_grouped {
            err.span_suggestion_verbose(
                self_span.shrink_to_lo(),
                format!("consider annotating `{self_name}` with `#[derive({traits})]`"),
                format!("#[derive({traits})]\n"),
                Applicability::MaybeIncorrect,
            );
        }
        !derives_grouped.is_empty()
    }

    fn note_derefed_ty_has_method(
        &self,
        err: &mut Diag<'_>,
        self_source: SelfSource<'tcx>,
        rcvr_ty: Ty<'tcx>,
        item_name: Ident,
        expected: Expectation<'tcx>,
    ) {
        let SelfSource::QPath(ty) = self_source else {
            return;
        };
        for (deref_ty, _) in self.autoderef(DUMMY_SP, rcvr_ty).skip(1) {
            if let Ok(pick) = self.probe_for_name(
                Mode::Path,
                item_name,
                expected.only_has_type(self),
                IsSuggestion(true),
                deref_ty,
                ty.hir_id,
                ProbeScope::TraitsInScope,
            ) {
                if deref_ty.is_suggestable(self.tcx, true)
                    // If this method receives `&self`, then the provided
                    // argument _should_ coerce, so it's valid to suggest
                    // just changing the path.
                    && pick.item.fn_has_self_parameter
                    && let Some(self_ty) =
                        self.tcx.fn_sig(pick.item.def_id).instantiate_identity().inputs().skip_binder().get(0)
                    && self_ty.is_ref()
                {
                    let suggested_path = match deref_ty.kind() {
                        ty::Bool
                        | ty::Char
                        | ty::Int(_)
                        | ty::Uint(_)
                        | ty::Float(_)
                        | ty::Adt(_, _)
                        | ty::Str
                        | ty::Alias(ty::Projection | ty::Inherent, _)
                        | ty::Param(_) => format!("{deref_ty}"),
                        // we need to test something like  <&[_]>::len or <(&[u32])>::len
                        // and Vec::function();
                        // <&[_]>::len or <&[u32]>::len doesn't need an extra "<>" between
                        // but for Adt type like Vec::function()
                        // we would suggest <[_]>::function();
                        _ if self
                            .tcx
                            .sess
                            .source_map()
                            .span_wrapped_by_angle_or_parentheses(ty.span) =>
                        {
                            format!("{deref_ty}")
                        }
                        _ => format!("<{deref_ty}>"),
                    };
                    err.span_suggestion_verbose(
                        ty.span,
                        format!("the function `{item_name}` is implemented on `{deref_ty}`"),
                        suggested_path,
                        Applicability::MaybeIncorrect,
                    );
                } else {
                    err.span_note(
                        ty.span,
                        format!("the function `{item_name}` is implemented on `{deref_ty}`"),
                    );
                }
                return;
            }
        }
    }

    /// Print out the type for use in value namespace.
    fn ty_to_value_string(&self, ty: Ty<'tcx>) -> String {
        match ty.kind() {
            ty::Adt(def, args) => self.tcx.def_path_str_with_args(def.did(), args),
            _ => self.ty_to_string(ty),
        }
    }

    fn suggest_await_before_method(
        &self,
        err: &mut Diag<'_>,
        item_name: Ident,
        ty: Ty<'tcx>,
        call: &hir::Expr<'_>,
        span: Span,
        return_type: Option<Ty<'tcx>>,
    ) {
        let output_ty = match self.err_ctxt().get_impl_future_output_ty(ty) {
            Some(output_ty) => self.resolve_vars_if_possible(output_ty),
            _ => return,
        };
        let method_exists =
            self.method_exists_for_diagnostic(item_name, output_ty, call.hir_id, return_type);
        debug!("suggest_await_before_method: is_method_exist={}", method_exists);
        if method_exists {
            err.span_suggestion_verbose(
                span.shrink_to_lo(),
                "consider `await`ing on the `Future` and calling the method on its `Output`",
                "await.",
                Applicability::MaybeIncorrect,
            );
        }
    }

    fn suggest_use_candidates<F>(&self, candidates: Vec<DefId>, handle_candidates: F)
    where
        F: FnOnce(Vec<String>, Vec<String>, Span),
    {
        let parent_map = self.tcx.visible_parent_map(());

        let scope = self.tcx.parent_module_from_def_id(self.body_id);
        let (accessible_candidates, inaccessible_candidates): (Vec<_>, Vec<_>) =
            candidates.into_iter().partition(|id| {
                let vis = self.tcx.visibility(*id);
                vis.is_accessible_from(scope, self.tcx)
            });

        let sugg = |candidates: Vec<_>, visible| {
            // Separate out candidates that must be imported with a glob, because they are named `_`
            // and cannot be referred with their identifier.
            let (candidates, globs): (Vec<_>, Vec<_>) =
                candidates.into_iter().partition(|trait_did| {
                    if let Some(parent_did) = parent_map.get(trait_did) {
                        // If the item is re-exported as `_`, we should suggest a glob-import instead.
                        if *parent_did != self.tcx.parent(*trait_did)
                            && self
                                .tcx
                                .module_children(*parent_did)
                                .iter()
                                .filter(|child| child.res.opt_def_id() == Some(*trait_did))
                                .all(|child| child.ident.name == kw::Underscore)
                        {
                            return false;
                        }
                    }

                    true
                });

            let prefix = if visible { "use " } else { "" };
            let postfix = if visible { ";" } else { "" };
            let path_strings = candidates.iter().map(|trait_did| {
                format!(
                    "{prefix}{}{postfix}\n",
                    with_crate_prefix!(self.tcx.def_path_str(*trait_did)),
                )
            });

            let glob_path_strings = globs.iter().map(|trait_did| {
                let parent_did = parent_map.get(trait_did).unwrap();
                format!(
                    "{prefix}{}::*{postfix} // trait {}\n",
                    with_crate_prefix!(self.tcx.def_path_str(*parent_did)),
                    self.tcx.item_name(*trait_did),
                )
            });
            let mut sugg: Vec<_> = path_strings.chain(glob_path_strings).collect();
            sugg.sort();
            sugg
        };

        let accessible_sugg = sugg(accessible_candidates, true);
        let inaccessible_sugg = sugg(inaccessible_candidates, false);

        let (module, _, _) = self.tcx.hir().get_module(scope);
        let span = module.spans.inject_use_span;
        handle_candidates(accessible_sugg, inaccessible_sugg, span);
    }

    fn suggest_valid_traits(
        &self,
        err: &mut Diag<'_>,
        item_name: Ident,
        valid_out_of_scope_traits: Vec<DefId>,
        explain: bool,
    ) -> bool {
        if !valid_out_of_scope_traits.is_empty() {
            let mut candidates = valid_out_of_scope_traits;
            candidates.sort_by_key(|id| self.tcx.def_path_str(id));
            candidates.dedup();

            // `TryFrom` and `FromIterator` have no methods
            let edition_fix = candidates
                .iter()
                .find(|did| self.tcx.is_diagnostic_item(sym::TryInto, **did))
                .copied();

            if explain {
                err.help("items from traits can only be used if the trait is in scope");
            }

            let msg = format!(
                "{this_trait_is} implemented but not in scope",
                this_trait_is = if candidates.len() == 1 {
                    format!(
                        "trait `{}` which provides `{item_name}` is",
                        self.tcx.item_name(candidates[0]),
                    )
                } else {
                    format!("the following traits which provide `{item_name}` are")
                }
            );

            self.suggest_use_candidates(candidates, |accessible_sugg, inaccessible_sugg, span| {
                let suggest_for_access = |err: &mut Diag<'_>, mut msg: String, suggs: Vec<_>| {
                    msg += &format!(
                        "; perhaps you want to import {one_of}",
                        one_of = if suggs.len() == 1 { "it" } else { "one of them" },
                    );
                    err.span_suggestions(span, msg, suggs, Applicability::MaybeIncorrect);
                };
                let suggest_for_privacy = |err: &mut Diag<'_>, suggs: Vec<String>| {
                    let msg = format!(
                        "{this_trait_is} implemented but not reachable",
                        this_trait_is = if let [sugg] = suggs.as_slice() {
                            format!("trait `{}` which provides `{item_name}` is", sugg.trim())
                        } else {
                            format!("the following traits which provide `{item_name}` are")
                        }
                    );
                    if suggs.len() == 1 {
                        err.help(msg);
                    } else {
                        err.span_suggestions(span, msg, suggs, Applicability::MaybeIncorrect);
                    }
                };
                if accessible_sugg.is_empty() {
                    // `inaccessible_sugg` must not be empty
                    suggest_for_privacy(err, inaccessible_sugg);
                } else if inaccessible_sugg.is_empty() {
                    suggest_for_access(err, msg, accessible_sugg);
                } else {
                    suggest_for_access(err, msg, accessible_sugg);
                    suggest_for_privacy(err, inaccessible_sugg);
                }
            });

            if let Some(did) = edition_fix {
                err.note(format!(
                    "'{}' is included in the prelude starting in Edition 2021",
                    with_crate_prefix!(self.tcx.def_path_str(did))
                ));
            }

            true
        } else {
            false
        }
    }

    fn suggest_traits_to_import(
        &self,
        err: &mut Diag<'_>,
        span: Span,
        rcvr_ty: Ty<'tcx>,
        item_name: Ident,
        inputs_len: Option<usize>,
        source: SelfSource<'tcx>,
        valid_out_of_scope_traits: Vec<DefId>,
        static_candidates: &[CandidateSource],
        unsatisfied_bounds: bool,
        return_type: Option<Ty<'tcx>>,
        trait_missing_method: bool,
    ) {
        let mut alt_rcvr_sugg = false;
        let mut trait_in_other_version_found = false;
        if let (SelfSource::MethodCall(rcvr), false) = (source, unsatisfied_bounds) {
            debug!(
                "suggest_traits_to_import: span={:?}, item_name={:?}, rcvr_ty={:?}, rcvr={:?}",
                span, item_name, rcvr_ty, rcvr
            );
            let skippable = [
                self.tcx.lang_items().clone_trait(),
                self.tcx.lang_items().deref_trait(),
                self.tcx.lang_items().deref_mut_trait(),
                self.tcx.lang_items().drop_trait(),
                self.tcx.get_diagnostic_item(sym::AsRef),
            ];
            // Try alternative arbitrary self types that could fulfill this call.
            // FIXME: probe for all types that *could* be arbitrary self-types, not
            // just this list.
            for (rcvr_ty, post, pin_call) in &[
                (rcvr_ty, "", None),
                (
                    Ty::new_mut_ref(self.tcx, self.tcx.lifetimes.re_erased, rcvr_ty),
                    "&mut ",
                    Some("as_mut"),
                ),
                (
                    Ty::new_imm_ref(self.tcx, self.tcx.lifetimes.re_erased, rcvr_ty),
                    "&",
                    Some("as_ref"),
                ),
            ] {
                match self.lookup_probe_for_diagnostic(
                    item_name,
                    *rcvr_ty,
                    rcvr,
                    ProbeScope::AllTraits,
                    return_type,
                ) {
                    Ok(pick) => {
                        // If the method is defined for the receiver we have, it likely wasn't `use`d.
                        // We point at the method, but we just skip the rest of the check for arbitrary
                        // self types and rely on the suggestion to `use` the trait from
                        // `suggest_valid_traits`.
                        let did = Some(pick.item.container_id(self.tcx));
                        if skippable.contains(&did) {
                            continue;
                        }
                        trait_in_other_version_found = self
                            .detect_and_explain_multiple_crate_versions(
                                err,
                                pick.item.def_id,
                                rcvr.hir_id,
                                Some(*rcvr_ty),
                            );
                        if pick.autoderefs == 0 && !trait_in_other_version_found {
                            err.span_label(
                                pick.item.ident(self.tcx).span,
                                format!("the method is available for `{rcvr_ty}` here"),
                            );
                        }
                        break;
                    }
                    Err(MethodError::Ambiguity(_)) => {
                        // If the method is defined (but ambiguous) for the receiver we have, it is also
                        // likely we haven't `use`d it. It may be possible that if we `Box`/`Pin`/etc.
                        // the receiver, then it might disambiguate this method, but I think these
                        // suggestions are generally misleading (see #94218).
                        break;
                    }
                    Err(_) => (),
                }

                let Some(unpin_trait) = self.tcx.lang_items().unpin_trait() else {
                    return;
                };
                let pred = ty::TraitRef::new(self.tcx, unpin_trait, [*rcvr_ty]);
                let unpin = self.predicate_must_hold_considering_regions(&Obligation::new(
                    self.tcx,
                    ObligationCause::misc(rcvr.span, self.body_id),
                    self.param_env,
                    pred,
                ));
                for (rcvr_ty, pre) in &[
                    (Ty::new_lang_item(self.tcx, *rcvr_ty, LangItem::OwnedBox), "Box::new"),
                    (Ty::new_lang_item(self.tcx, *rcvr_ty, LangItem::Pin), "Pin::new"),
                    (Ty::new_diagnostic_item(self.tcx, *rcvr_ty, sym::Arc), "Arc::new"),
                    (Ty::new_diagnostic_item(self.tcx, *rcvr_ty, sym::Rc), "Rc::new"),
                ] {
                    if let Some(new_rcvr_t) = *rcvr_ty
                        && let Ok(pick) = self.lookup_probe_for_diagnostic(
                            item_name,
                            new_rcvr_t,
                            rcvr,
                            ProbeScope::AllTraits,
                            return_type,
                        )
                    {
                        debug!("try_alt_rcvr: pick candidate {:?}", pick);
                        let did = Some(pick.item.container_id(self.tcx));
                        // We don't want to suggest a container type when the missing
                        // method is `.clone()` or `.deref()` otherwise we'd suggest
                        // `Arc::new(foo).clone()`, which is far from what the user wants.
                        // Explicitly ignore the `Pin::as_ref()` method as `Pin` does not
                        // implement the `AsRef` trait.
                        let skip = skippable.contains(&did)
                            || (("Pin::new" == *pre)
                                && ((sym::as_ref == item_name.name) || !unpin))
                            || inputs_len.is_some_and(|inputs_len| {
                                pick.item.kind == ty::AssocKind::Fn
                                    && self
                                        .tcx
                                        .fn_sig(pick.item.def_id)
                                        .skip_binder()
                                        .skip_binder()
                                        .inputs()
                                        .len()
                                        != inputs_len
                            });
                        // Make sure the method is defined for the *actual* receiver: we don't
                        // want to treat `Box<Self>` as a receiver if it only works because of
                        // an autoderef to `&self`
                        if pick.autoderefs == 0 && !skip {
                            err.span_label(
                                pick.item.ident(self.tcx).span,
                                format!("the method is available for `{new_rcvr_t}` here"),
                            );
                            err.multipart_suggestion(
                                "consider wrapping the receiver expression with the \
                                 appropriate type",
                                vec![
                                    (rcvr.span.shrink_to_lo(), format!("{pre}({post}")),
                                    (rcvr.span.shrink_to_hi(), ")".to_string()),
                                ],
                                Applicability::MaybeIncorrect,
                            );
                            // We don't care about the other suggestions.
                            alt_rcvr_sugg = true;
                        }
                    }
                }
                // We special case the situation where `Pin::new` wouldn't work, and instead
                // suggest using the `pin!()` macro instead.
                if let Some(new_rcvr_t) = Ty::new_lang_item(self.tcx, *rcvr_ty, LangItem::Pin)
                    // We didn't find an alternative receiver for the method.
                    && !alt_rcvr_sugg
                    // `T: !Unpin`
                    && !unpin
                    // The method isn't `as_ref`, as it would provide a wrong suggestion for `Pin`.
                    && sym::as_ref != item_name.name
                    // Either `Pin::as_ref` or `Pin::as_mut`.
                    && let Some(pin_call) = pin_call
                    // Search for `item_name` as a method accessible on `Pin<T>`.
                    && let Ok(pick) = self.lookup_probe_for_diagnostic(
                        item_name,
                        new_rcvr_t,
                        rcvr,
                        ProbeScope::AllTraits,
                        return_type,
                    )
                    // We skip some common traits that we don't want to consider because autoderefs
                    // would take care of them.
                    && !skippable.contains(&Some(pick.item.container_id(self.tcx)))
                    // We don't want to go through derefs.
                    && pick.autoderefs == 0
                    // Check that the method of the same name that was found on the new `Pin<T>`
                    // receiver has the same number of arguments that appear in the user's code.
                    && inputs_len.is_some_and(|inputs_len| pick.item.kind == ty::AssocKind::Fn && self.tcx.fn_sig(pick.item.def_id).skip_binder().skip_binder().inputs().len() == inputs_len)
                {
                    let indent = self
                        .tcx
                        .sess
                        .source_map()
                        .indentation_before(rcvr.span)
                        .unwrap_or_else(|| " ".to_string());
                    let mut expr = rcvr;
                    while let Node::Expr(call_expr) = self.tcx.parent_hir_node(expr.hir_id)
                        && let hir::ExprKind::MethodCall(hir::PathSegment { .. }, ..) =
                            call_expr.kind
                    {
                        expr = call_expr;
                    }
                    match self.tcx.parent_hir_node(expr.hir_id) {
                        Node::LetStmt(stmt)
                            if let Some(init) = stmt.init
                                && let Ok(code) =
                                    self.tcx.sess.source_map().span_to_snippet(rcvr.span) =>
                        {
                            // We need to take care to account for the existing binding when we
                            // suggest the code.
                            err.multipart_suggestion(
                                "consider pinning the expression",
                                vec![
                                    (
                                        stmt.span.shrink_to_lo(),
                                        format!(
                                            "let mut pinned = std::pin::pin!({code});\n{indent}"
                                        ),
                                    ),
                                    (
                                        init.span.until(rcvr.span.shrink_to_hi()),
                                        format!("pinned.{pin_call}()"),
                                    ),
                                ],
                                Applicability::MaybeIncorrect,
                            );
                        }
                        Node::Block(_) | Node::Stmt(_) => {
                            // There's no binding, so we can provide a slightly nicer looking
                            // suggestion.
                            err.multipart_suggestion(
                                "consider pinning the expression",
                                vec![
                                    (
                                        rcvr.span.shrink_to_lo(),
                                        format!("let mut pinned = std::pin::pin!("),
                                    ),
                                    (
                                        rcvr.span.shrink_to_hi(),
                                        format!(");\n{indent}pinned.{pin_call}()"),
                                    ),
                                ],
                                Applicability::MaybeIncorrect,
                            );
                        }
                        _ => {
                            // We don't quite know what the users' code looks like, so we don't
                            // provide a pinning suggestion.
                            err.span_help(
                                rcvr.span,
                                "consider pinning the expression with `std::pin::pin!()` and \
                                 assigning that to a new binding",
                            );
                        }
                    }
                    // We don't care about the other suggestions.
                    alt_rcvr_sugg = true;
                }
            }
        }

        if let SelfSource::QPath(ty) = source
            && !valid_out_of_scope_traits.is_empty()
            && let hir::TyKind::Path(path) = ty.kind
            && let hir::QPath::Resolved(..) = path
            && let Some(assoc) = self
                .tcx
                .associated_items(valid_out_of_scope_traits[0])
                .filter_by_name_unhygienic(item_name.name)
                .next()
        {
            // See if the `Type::function(val)` where `function` wasn't found corresponds to a
            // `Trait` that is imported directly, but `Type` came from a different version of the
            // same crate.

            let rcvr_ty = self.node_ty_opt(ty.hir_id);
            trait_in_other_version_found = self.detect_and_explain_multiple_crate_versions(
                err,
                assoc.def_id,
                ty.hir_id,
                rcvr_ty,
            );
        }
        if !trait_in_other_version_found
            && self.suggest_valid_traits(err, item_name, valid_out_of_scope_traits, true)
        {
            return;
        }

        let type_is_local = self.type_derefs_to_local(span, rcvr_ty, source);

        let mut arbitrary_rcvr = vec![];
        // There are no traits implemented, so lets suggest some traits to
        // implement, by finding ones that have the item name, and are
        // legal to implement.
        let mut candidates = all_traits(self.tcx)
            .into_iter()
            // Don't issue suggestions for unstable traits since they're
            // unlikely to be implementable anyway
            .filter(|info| match self.tcx.lookup_stability(info.def_id) {
                Some(attr) => attr.level.is_stable(),
                None => true,
            })
            .filter(|info| {
                // Static candidates are already implemented, and known not to work
                // Do not suggest them again
                static_candidates.iter().all(|sc| match *sc {
                    CandidateSource::Trait(def_id) => def_id != info.def_id,
                    CandidateSource::Impl(def_id) => {
                        self.tcx.trait_id_of_impl(def_id) != Some(info.def_id)
                    }
                })
            })
            .filter(|info| {
                // We approximate the coherence rules to only suggest
                // traits that are legal to implement by requiring that
                // either the type or trait is local. Multi-dispatch means
                // this isn't perfect (that is, there are cases when
                // implementing a trait would be legal but is rejected
                // here).
                (type_is_local || info.def_id.is_local())
                    && !self.tcx.trait_is_auto(info.def_id)
                    && self
                        .associated_value(info.def_id, item_name)
                        .filter(|item| {
                            if let ty::AssocKind::Fn = item.kind {
                                let id = item
                                    .def_id
                                    .as_local()
                                    .map(|def_id| self.tcx.hir_node_by_def_id(def_id));
                                if let Some(hir::Node::TraitItem(hir::TraitItem {
                                    kind: hir::TraitItemKind::Fn(fn_sig, method),
                                    ..
                                })) = id
                                {
                                    let self_first_arg = match method {
                                        hir::TraitFn::Required([ident, ..]) => {
                                            ident.name == kw::SelfLower
                                        }
                                        hir::TraitFn::Provided(body_id) => {
                                            self.tcx.hir().body(*body_id).params.first().map_or(
                                                false,
                                                |param| {
                                                    matches!(
                                                        param.pat.kind,
                                                        hir::PatKind::Binding(_, _, ident, _)
                                                            if ident.name == kw::SelfLower
                                                    )
                                                },
                                            )
                                        }
                                        _ => false,
                                    };

                                    if !fn_sig.decl.implicit_self.has_implicit_self()
                                        && self_first_arg
                                    {
                                        if let Some(ty) = fn_sig.decl.inputs.get(0) {
                                            arbitrary_rcvr.push(ty.span);
                                        }
                                        return false;
                                    }
                                }
                            }
                            // We only want to suggest public or local traits (#45781).
                            item.visibility(self.tcx).is_public() || info.def_id.is_local()
                        })
                        .is_some()
            })
            .collect::<Vec<_>>();
        for span in &arbitrary_rcvr {
            err.span_label(
                *span,
                "the method might not be found because of this arbitrary self type",
            );
        }
        if alt_rcvr_sugg {
            return;
        }

        if !candidates.is_empty() {
            // Sort local crate results before others
            candidates
                .sort_by_key(|&info| (!info.def_id.is_local(), self.tcx.def_path_str(info.def_id)));
            candidates.dedup();

            let param_type = match *rcvr_ty.kind() {
                ty::Param(param) => Some(param),
                ty::Ref(_, ty, _) => match *ty.kind() {
                    ty::Param(param) => Some(param),
                    _ => None,
                },
                _ => None,
            };
            if !trait_missing_method {
                err.help(if param_type.is_some() {
                    "items from traits can only be used if the type parameter is bounded by the trait"
                } else {
                    "items from traits can only be used if the trait is implemented and in scope"
                });
            }

            let candidates_len = candidates.len();
            let message = |action| {
                format!(
                    "the following {traits_define} an item `{name}`, perhaps you need to {action} \
                     {one_of_them}:",
                    traits_define =
                        if candidates_len == 1 { "trait defines" } else { "traits define" },
                    action = action,
                    one_of_them = if candidates_len == 1 { "it" } else { "one of them" },
                    name = item_name,
                )
            };
            // Obtain the span for `param` and use it for a structured suggestion.
            if let Some(param) = param_type {
                let generics = self.tcx.generics_of(self.body_id.to_def_id());
                let type_param = generics.type_param(param, self.tcx);
                let hir = self.tcx.hir();
                if let Some(def_id) = type_param.def_id.as_local() {
                    let id = self.tcx.local_def_id_to_hir_id(def_id);
                    // Get the `hir::Param` to verify whether it already has any bounds.
                    // We do this to avoid suggesting code that ends up as `T: FooBar`,
                    // instead we suggest `T: Foo + Bar` in that case.
                    match self.tcx.hir_node(id) {
                        Node::GenericParam(param) => {
                            enum Introducer {
                                Plus,
                                Colon,
                                Nothing,
                            }
                            let hir_generics = hir.get_generics(id.owner.def_id).unwrap();
                            let trait_def_ids: DefIdSet = hir_generics
                                .bounds_for_param(def_id)
                                .flat_map(|bp| bp.bounds.iter())
                                .filter_map(|bound| bound.trait_ref()?.trait_def_id())
                                .collect();
                            if candidates.iter().any(|t| trait_def_ids.contains(&t.def_id)) {
                                return;
                            }
                            let msg = message(format!(
                                "restrict type parameter `{}` with",
                                param.name.ident(),
                            ));
                            let bounds_span = hir_generics.bounds_span_for_suggestions(def_id);
                            if rcvr_ty.is_ref()
                                && param.is_impl_trait()
                                && let Some((bounds_span, _)) = bounds_span
                            {
                                err.multipart_suggestions(
                                    msg,
                                    candidates.iter().map(|t| {
                                        vec![
                                            (param.span.shrink_to_lo(), "(".to_string()),
                                            (
                                                bounds_span,
                                                format!(" + {})", self.tcx.def_path_str(t.def_id)),
                                            ),
                                        ]
                                    }),
                                    Applicability::MaybeIncorrect,
                                );
                                return;
                            }

                            let (sp, introducer, open_paren_sp) =
                                if let Some((span, open_paren_sp)) = bounds_span {
                                    (span, Introducer::Plus, open_paren_sp)
                                } else if let Some(colon_span) = param.colon_span {
                                    (colon_span.shrink_to_hi(), Introducer::Nothing, None)
                                } else if param.is_impl_trait() {
                                    (param.span.shrink_to_hi(), Introducer::Plus, None)
                                } else {
                                    (param.span.shrink_to_hi(), Introducer::Colon, None)
                                };

                            let all_suggs = candidates.iter().map(|cand| {
                                let suggestion = format!(
                                    "{} {}",
                                    match introducer {
                                        Introducer::Plus => " +",
                                        Introducer::Colon => ":",
                                        Introducer::Nothing => "",
                                    },
                                    self.tcx.def_path_str(cand.def_id)
                                );

                                let mut suggs = vec![];

                                if let Some(open_paren_sp) = open_paren_sp {
                                    suggs.push((open_paren_sp, "(".to_string()));
                                    suggs.push((sp, format!("){suggestion}")));
                                } else {
                                    suggs.push((sp, suggestion));
                                }

                                suggs
                            });

                            err.multipart_suggestions(
                                msg,
                                all_suggs,
                                Applicability::MaybeIncorrect,
                            );

                            return;
                        }
                        Node::Item(hir::Item {
                            kind: hir::ItemKind::Trait(.., bounds, _),
                            ident,
                            ..
                        }) => {
                            let (sp, sep, article) = if bounds.is_empty() {
                                (ident.span.shrink_to_hi(), ":", "a")
                            } else {
                                (bounds.last().unwrap().span().shrink_to_hi(), " +", "another")
                            };
                            err.span_suggestions(
                                sp,
                                message(format!("add {article} supertrait for")),
                                candidates.iter().map(|t| {
                                    format!("{} {}", sep, self.tcx.def_path_str(t.def_id),)
                                }),
                                Applicability::MaybeIncorrect,
                            );
                            return;
                        }
                        _ => {}
                    }
                }
            }

            let (potential_candidates, explicitly_negative) = if param_type.is_some() {
                // FIXME: Even though negative bounds are not implemented, we could maybe handle
                // cases where a positive bound implies a negative impl.
                (candidates, Vec::new())
            } else if let Some(simp_rcvr_ty) =
                simplify_type(self.tcx, rcvr_ty, TreatParams::ForLookup)
            {
                let mut potential_candidates = Vec::new();
                let mut explicitly_negative = Vec::new();
                for candidate in candidates {
                    // Check if there's a negative impl of `candidate` for `rcvr_ty`
                    if self
                        .tcx
                        .all_impls(candidate.def_id)
                        .map(|imp_did| {
                            self.tcx.impl_trait_header(imp_did).expect(
                                "inherent impls can't be candidates, only trait impls can be",
                            )
                        })
                        .filter(|header| header.polarity != ty::ImplPolarity::Positive)
                        .any(|header| {
                            let imp = header.trait_ref.instantiate_identity();
                            let imp_simp =
                                simplify_type(self.tcx, imp.self_ty(), TreatParams::ForLookup);
                            imp_simp.is_some_and(|s| s == simp_rcvr_ty)
                        })
                    {
                        explicitly_negative.push(candidate);
                    } else {
                        potential_candidates.push(candidate);
                    }
                }
                (potential_candidates, explicitly_negative)
            } else {
                // We don't know enough about `recv_ty` to make proper suggestions.
                (candidates, Vec::new())
            };

            let impls_trait = |def_id: DefId| {
                let args = ty::GenericArgs::for_item(self.tcx, def_id, |param, _| {
                    if param.index == 0 {
                        rcvr_ty.into()
                    } else {
                        self.infcx.var_for_def(span, param)
                    }
                });
                self.infcx
                    .type_implements_trait(def_id, args, self.param_env)
                    .must_apply_modulo_regions()
                    && param_type.is_none()
            };
            match &potential_candidates[..] {
                [] => {}
                [trait_info] if trait_info.def_id.is_local() => {
                    if impls_trait(trait_info.def_id) {
                        self.suggest_valid_traits(err, item_name, vec![trait_info.def_id], false);
                    } else {
                        err.subdiagnostic(CandidateTraitNote {
                            span: self.tcx.def_span(trait_info.def_id),
                            trait_name: self.tcx.def_path_str(trait_info.def_id),
                            item_name,
                            action_or_ty: if trait_missing_method {
                                "NONE".to_string()
                            } else {
                                param_type.map_or_else(
                                    || "implement".to_string(), // FIXME: it might only need to be imported into scope, not implemented.
                                    |p| p.to_string(),
                                )
                            },
                        });
                    }
                }
                trait_infos => {
                    let mut msg = message(param_type.map_or_else(
                        || "implement".to_string(), // FIXME: it might only need to be imported into scope, not implemented.
                        |param| format!("restrict type parameter `{param}` with"),
                    ));
                    for (i, trait_info) in trait_infos.iter().enumerate() {
                        if impls_trait(trait_info.def_id) {
                            self.suggest_valid_traits(
                                err,
                                item_name,
                                vec![trait_info.def_id],
                                false,
                            );
                        }
                        msg.push_str(&format!(
                            "\ncandidate #{}: `{}`",
                            i + 1,
                            self.tcx.def_path_str(trait_info.def_id),
                        ));
                    }
                    err.note(msg);
                }
            }
            match &explicitly_negative[..] {
                [] => {}
                [trait_info] => {
                    let msg = format!(
                        "the trait `{}` defines an item `{}`, but is explicitly unimplemented",
                        self.tcx.def_path_str(trait_info.def_id),
                        item_name
                    );
                    err.note(msg);
                }
                trait_infos => {
                    let mut msg = format!(
                        "the following traits define an item `{item_name}`, but are explicitly unimplemented:"
                    );
                    for trait_info in trait_infos {
                        msg.push_str(&format!("\n{}", self.tcx.def_path_str(trait_info.def_id)));
                    }
                    err.note(msg);
                }
            }
        }
    }

    fn detect_and_explain_multiple_crate_versions(
        &self,
        err: &mut Diag<'_>,
        item_def_id: DefId,
        hir_id: hir::HirId,
        rcvr_ty: Option<Ty<'_>>,
    ) -> bool {
        let hir_id = self.tcx.parent_hir_id(hir_id);
        let Some(traits) = self.tcx.in_scope_traits(hir_id) else { return false };
        if traits.is_empty() {
            return false;
        }
        let trait_def_id = self.tcx.parent(item_def_id);
        let krate = self.tcx.crate_name(trait_def_id.krate);
        let name = self.tcx.item_name(trait_def_id);
        let candidates: Vec<_> = traits
            .iter()
            .filter(|c| {
                c.def_id.krate != trait_def_id.krate
                    && self.tcx.crate_name(c.def_id.krate) == krate
                    && self.tcx.item_name(c.def_id) == name
            })
            .map(|c| (c.def_id, c.import_ids.get(0).cloned()))
            .collect();
        if candidates.is_empty() {
            return false;
        }
        let item_span = self.tcx.def_span(item_def_id);
        let msg = format!(
            "there are multiple different versions of crate `{krate}` in the dependency graph",
        );
        let trait_span = self.tcx.def_span(trait_def_id);
        let mut multi_span: MultiSpan = trait_span.into();
        multi_span.push_span_label(trait_span, format!("this is the trait that is needed"));
        let descr = self.tcx.associated_item(item_def_id).descr();
        let rcvr_ty =
            rcvr_ty.map(|t| format!("`{t}`")).unwrap_or_else(|| "the receiver".to_string());
        multi_span
            .push_span_label(item_span, format!("the {descr} is available for {rcvr_ty} here"));
        for (def_id, import_def_id) in candidates {
            if let Some(import_def_id) = import_def_id {
                multi_span.push_span_label(
                    self.tcx.def_span(import_def_id),
                    format!(
                        "`{name}` imported here doesn't correspond to the right version of crate \
                         `{krate}`",
                    ),
                );
            }
            multi_span.push_span_label(
                self.tcx.def_span(def_id),
                format!("this is the trait that was imported"),
            );
        }
        err.span_note(multi_span, msg);
        true
    }

    /// issue #102320, for `unwrap_or` with closure as argument, suggest `unwrap_or_else`
    /// FIXME: currently not working for suggesting `map_or_else`, see #102408
    pub(crate) fn suggest_else_fn_with_closure(
        &self,
        err: &mut Diag<'_>,
        expr: &hir::Expr<'_>,
        found: Ty<'tcx>,
        expected: Ty<'tcx>,
    ) -> bool {
        let Some((_def_id_or_name, output, _inputs)) = self.extract_callable_info(found) else {
            return false;
        };

        if !self.can_coerce(output, expected) {
            return false;
        }

        if let Node::Expr(call_expr) = self.tcx.parent_hir_node(expr.hir_id)
            && let hir::ExprKind::MethodCall(
                hir::PathSegment { ident: method_name, .. },
                self_expr,
                args,
                ..,
            ) = call_expr.kind
            && let Some(self_ty) = self.typeck_results.borrow().expr_ty_opt(self_expr)
        {
            let new_name = Ident {
                name: Symbol::intern(&format!("{}_else", method_name.as_str())),
                span: method_name.span,
            };
            let probe = self.lookup_probe_for_diagnostic(
                new_name,
                self_ty,
                self_expr,
                ProbeScope::TraitsInScope,
                Some(expected),
            );

            // check the method arguments number
            if let Ok(pick) = probe
                && let fn_sig = self.tcx.fn_sig(pick.item.def_id)
                && let fn_args = fn_sig.skip_binder().skip_binder().inputs()
                && fn_args.len() == args.len() + 1
            {
                err.span_suggestion_verbose(
                    method_name.span.shrink_to_hi(),
                    format!("try calling `{}` instead", new_name.name.as_str()),
                    "_else",
                    Applicability::MaybeIncorrect,
                );
                return true;
            }
        }
        false
    }

    /// Checks whether there is a local type somewhere in the chain of
    /// autoderefs of `rcvr_ty`.
    fn type_derefs_to_local(
        &self,
        span: Span,
        rcvr_ty: Ty<'tcx>,
        source: SelfSource<'tcx>,
    ) -> bool {
        fn is_local(ty: Ty<'_>) -> bool {
            match ty.kind() {
                ty::Adt(def, _) => def.did().is_local(),
                ty::Foreign(did) => did.is_local(),
                ty::Dynamic(tr, ..) => tr.principal().is_some_and(|d| d.def_id().is_local()),
                ty::Param(_) => true,

                // Everything else (primitive types, etc.) is effectively
                // non-local (there are "edge" cases, e.g., `(LocalType,)`, but
                // the noise from these sort of types is usually just really
                // annoying, rather than any sort of help).
                _ => false,
            }
        }

        // This occurs for UFCS desugaring of `T::method`, where there is no
        // receiver expression for the method call, and thus no autoderef.
        if let SelfSource::QPath(_) = source {
            return is_local(rcvr_ty);
        }

        self.autoderef(span, rcvr_ty).any(|(ty, _)| is_local(ty))
    }
}

#[derive(Copy, Clone, Debug)]
enum SelfSource<'a> {
    QPath(&'a hir::Ty<'a>),
    MethodCall(&'a hir::Expr<'a> /* rcvr */),
}

#[derive(Copy, Clone, PartialEq, Eq)]
pub(crate) struct TraitInfo {
    pub def_id: DefId,
}

/// Retrieves all traits in this crate and any dependent crates,
/// and wraps them into `TraitInfo` for custom sorting.
pub(crate) fn all_traits(tcx: TyCtxt<'_>) -> Vec<TraitInfo> {
    tcx.all_traits().map(|def_id| TraitInfo { def_id }).collect()
}

fn print_disambiguation_help<'tcx>(
    tcx: TyCtxt<'tcx>,
    err: &mut Diag<'_>,
    source: SelfSource<'tcx>,
    args: Option<&'tcx [hir::Expr<'tcx>]>,
    trait_ref: ty::TraitRef<'tcx>,
    candidate_idx: Option<usize>,
    span: Span,
    item: ty::AssocItem,
) -> Option<String> {
    let trait_impl_type = trait_ref.self_ty().peel_refs();
    let trait_ref = if item.fn_has_self_parameter {
        trait_ref.print_only_trait_name().to_string()
    } else {
        format!("<{} as {}>", trait_ref.args[0], trait_ref.print_only_trait_name())
    };
    Some(
        if matches!(item.kind, ty::AssocKind::Fn)
            && let SelfSource::MethodCall(receiver) = source
            && let Some(args) = args
        {
            let def_kind_descr = tcx.def_kind_descr(item.kind.as_def_kind(), item.def_id);
            let item_name = item.ident(tcx);
            let first_input =
                tcx.fn_sig(item.def_id).instantiate_identity().skip_binder().inputs().get(0);
            let (first_arg_type, rcvr_ref) = (
                first_input.map(|first| first.peel_refs()),
                first_input
                    .and_then(|ty| ty.ref_mutability())
                    .map_or("", |mutbl| mutbl.ref_prefix_str()),
            );

            // If the type of first arg of this assoc function is `Self` or current trait impl type or `arbitrary_self_types`, we need to take the receiver as args. Otherwise, we don't.
            let args = if let Some(first_arg_type) = first_arg_type
                && (first_arg_type == tcx.types.self_param
                    || first_arg_type == trait_impl_type
                    || item.fn_has_self_parameter)
            {
                Some(receiver)
            } else {
                None
            }
            .into_iter()
            .chain(args)
            .map(|arg| {
                tcx.sess.source_map().span_to_snippet(arg.span).unwrap_or_else(|_| "_".to_owned())
            })
            .collect::<Vec<_>>()
            .join(", ");

            let args = format!("({}{})", rcvr_ref, args);
            err.span_suggestion_verbose(
                span,
                format!(
                    "disambiguate the {def_kind_descr} for {}",
                    if let Some(candidate) = candidate_idx {
                        format!("candidate #{candidate}")
                    } else {
                        "the candidate".to_string()
                    },
                ),
                format!("{trait_ref}::{item_name}{args}"),
                Applicability::HasPlaceholders,
            );
            return None;
        } else {
            format!("{trait_ref}::")
        },
    )
}