cpp_code_analysis 0.4.0

The analysis layer of cpp_ls: the preprocessor, symbol scopes, type inference and the semantic checks.
Documentation
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
//! What type a declaration has — including the ones that wrote `auto`.
//!
//! `type_of_expression` reads a name's type off its declaration, which works for every spelling that *is* a type
//! and fails for the one that is a placeholder. These tests are about the second case: the initializer's type,
//! what the declaration wrote around `auto`, and the shapes that are refused because the language's rule is not the
//! one a spelling suggests (`auto&&`) or because the declaration is not in this file's syntax.
//!
//! The type is always the **written** spelling — `const int&` is those three tokens — which is the same boundary
//! every other query in this crate has; what is new here is that the spelling is *computed* rather than copied.

use cpp_code_analysis::{
    CompilerConfig, MemoryFiles, OpenDocuments, Session, SessionFiles, WatchFilter,
};

fn session_with(files: &[(&str, &str)]) -> Session<MemoryFiles> {
    let mut memory = MemoryFiles::new();
    for (path, source) in files {
        memory = memory.with_file(*path, *source);
    }

    let providers = SessionFiles::new(OpenDocuments::new(), memory);
    let mut session = Session::with_config(
        "/p",
        providers,
        WatchFilter::new("/p"),
        CompilerConfig::default(),
    );
    session.add_project_files(
        files
            .iter()
            .map(|(path, _)| std::path::PathBuf::from(*path)),
    );

    // **The ask, and then the pump** - see `tests/completion.rs`'s `cooked_view`. A view is of the file's *cooked
    // reading*, and a rendering is built because something asked for it.
    for (path, _) in files {
        session.want_cooked_reading(std::path::Path::new(path));
    }
    session.index_everything();

    session
}

/// The type of the **use** of a name in a file, as the query layer answers it.
///
/// The use rather than the declaration, because that is the question every consumer asks: a cursor on `n` in
/// `n + 1`, a member access `w.` after `auto w = …`. The occurrence is found **as a word** — `rfind("n")` would
/// find the `n` of `return`, which is how a fixture ends up testing an offset nobody meant.
fn type_of_use(source: &str, spelling: &str) -> Option<String> {
    let session = session_with(&[("/p/a.cpp", source)]);
    let view = session.view("/p/a.cpp").expect("the file is held");
    let offset = last_word(source, spelling);

    match session.type_at(&view, as_reading(&view, offset)) {
        cpp_code_analysis::Known::Yes(found) => Some(found.type_of),
        _ => None,
    }
}

/// **A file offset in the rendering's coordinates**, for the queries that read `view.root`.
///
/// The same crossing `tests/signature.rs`'s `signature_where_marked` makes, and the same mistake this file made at
/// three call sites: a rendering has its **line breaks collapsed** and its comments removed, so a position computed
/// against the fixture's own text is not a position in it. Measured: thirteen tests in this suite answered `None`
/// where a type was expected, every one of them through this offset.
fn as_reading(
    view: &cpp_code_analysis::FileView,
    offset: usize,
) -> cpp_code_analysis::file::coordinates::ReadingOffset {
    view.reading_offset_of(cpp_code_analysis::file::coordinates::FileOffset::new(
        offset,
    ))
    .expect("the cursor is in the rendering")
}
/// The offset of the last **whole-word** occurrence of a spelling.
fn last_word(source: &str, spelling: &str) -> usize {
    let is_word = |character: char| character.is_alphanumeric() || character == '_';
    let mut found = None;
    let mut from = 0usize;

    while let Some(at) = source[from..].find(spelling) {
        let start = from + at;
        let end = start + spelling.len();
        let before = source[..start].chars().next_back();
        let after = source[end..].chars().next();

        if !before.is_some_and(is_word) && !after.is_some_and(is_word) {
            found = Some(start);
        }
        from = end;
    }

    found.unwrap_or_else(|| panic!("the fixture uses `{spelling}`"))
}

/// The five shapes a modern codebase actually writes, each deduced from what the declaration wrote.
/// **A `using` alias declares the type after its `=`** — and nothing next to it inherits it.
///
/// `using X = Y;` writes the type on the **right of the `=`**, in a `TypeId` of its own: not in a declarator, not
/// in a specifier sequence, which is why the reader that answers "what type does this name declare" needs a branch
/// that reads neither. The review registered this path as having no test of its own ("`using X = Y;` 与 `typedef`
/// … 这条路还没有专门的测试"), and it is the branch that reads `Shape::type_id`.
///
/// The question is asked of **member accesses** rather than of the declarations: a declaration answers with the
/// spelling the file wrote (`size_type value;` *is* a `size_type`), and following the alias is what the use-side
/// reader does. Asking the declaration and expecting `unsigned long` is a test that fails against correct code —
/// which is how this one started.
///
/// The **neighbours** are the other half, and they are why this is not one assertion: a target read out of the
/// wrong node — which is what picking the enclosing construct instead of the innermost one does — shows up first
/// as a neighbour with the alias's type.
#[test]
fn a_using_alias_member_declares_the_type_after_the_equals() {
    let source = "\
struct Widget {
    using size_type = unsigned long;
    size_type value;
    int count;
    char* name;
};
unsigned long use(Widget w) {
    w.count;
    w.name;
    return w.value;
}
";
    assert_eq!(
        type_of_use(source, "count"),
        Some("int".to_string()),
        "the member declared after the alias keeps its own type"
    );
    assert_eq!(
        type_of_use(source, "name"),
        Some("char*".to_string()),
        "and so does the one after that"
    );
    assert_eq!(
        type_of_use(source, "value"),
        Some("unsigned long".to_string()),
        "the member declared *with* the alias is what the alias names"
    );
}

/// **Two aliases in one class each keep their own target.**
///
/// The lookup asks "the innermost `using` this name is inside", and a class with two of them is where "innermost"
/// and "the one the file wrote first" stop being the same answer. Both are pinned because the failure mode is
/// silent: the second alias answering with the first one's target is a type that is *wrong*, not missing.
#[test]
fn two_using_aliases_in_one_class_keep_their_own_targets() {
    let source = "\
struct Widget {
    using first = int;
    using second = unsigned long;
    first a;
    second b;
};
int use(Widget w) {
    w.b;
    return w.a;
}
";
    assert_eq!(type_of_use(source, "a"), Some("int".to_string()));
    assert_eq!(type_of_use(source, "b"), Some("unsigned long".to_string()));
}

#[test]
fn an_auto_declaration_takes_the_type_of_its_initializer() {
    let cases = [
        (
            "int count();\nint f() { auto n = count(); return n; }\n",
            "n",
            "int",
        ),
        (
            "struct Widget { int size; };\nint f() { auto w = Widget{}; return w.size; }\n",
            "w",
            "Widget",
        ),
        (
            "int f() { int base = 1; auto copy = base; return copy; }\n",
            "copy",
            "int",
        ),
        (
            "int f() { int base = 1; const auto& r = base; return r; }\n",
            "r",
            "const int&",
        ),
        (
            "int f() { int base = 1; auto p = &base; return *p; }\n",
            "p",
            "int*",
        ),
        // **A `new` expression is a pointer to what it allocates**, and that is C++'s rule rather than an
        // inference: `new T`, `new T(args)`, `new T[n]` and `new T{args}` all have the type `T*`, and nothing about
        // `T`'s own type has to be known. Measured on MSVC's `<memory>`, this was the largest answerable family
        // among the refusals — five of them, every one reporting `UnknownType("new _Ref_count_obj2<_Ty>(…)")` and
        // its siblings, with the type already in the tree and nothing asking it.
        //
        // The template arguments are **kept** rather than resolved: `Box<int>*` is what a reader wants to see, and
        // trying to resolve the argument would turn a certain answer into an uncertain one.
        //
        // Each spelling appears **once** in its fixture, which is not decoration: `type_of_use` asks about the last
        // occurrence, and `auto p = new int; return *p;` puts that occurrence inside the dereference — a question
        // about `*p` rather than about `p`, whose answer is `int` and which says nothing about this arm.
        (
            "template <class T> struct Box { T value; };\nint f() { auto boxed = new Box<int>(); return 0; }\n",
            "boxed",
            "Box<int>*",
        ),
        (
            "int f() { auto plain = new int; return 0; }\n",
            "plain",
            "int*",
        ),
        // The array form: the bound is a **sibling** `ArrayType` node rather than part of the `TypeId`, and the
        // expression's type is still a plain pointer — `new int[10]` is `int*`, not `int[10]*`.
        (
            "int f() { auto many = new int[10]; return 0; }\n",
            "many",
            "int*",
        ),
    ];

    for (source, spelling, expected) in cases {
        assert_eq!(
            type_of_use(source, spelling).as_deref(),
            Some(expected),
            "in {source:?}"
        );
    }
}

/// **A chain of `auto`s resolves**, because the deducer asks the same question about the initializer's declaration
/// — which is what the depth guard exists for.
#[test]
fn a_chain_of_auto_declarations_resolves() {
    let source = "\
int count();
int f() {
    auto first = count();
    auto second = first;
    const auto& third = second;
    return third;
}
";
    assert_eq!(type_of_use(source, "first").as_deref(), Some("int"));
    assert_eq!(type_of_use(source, "second").as_deref(), Some("int"));
    assert_eq!(type_of_use(source, "third").as_deref(), Some("const int&"));
}

/// **What the declaration wrote around `auto` is what the type keeps**: the qualifier in front and the operators
/// behind, spelled the way the file would have spelled them had it named the type.
#[test]
fn the_written_decoration_is_kept() {
    let source = "\
int f() {
    int base = 1;
    const auto& kept = base;
    auto* pointer = &base;
    return kept;
}
";
    assert_eq!(type_of_use(source, "kept").as_deref(), Some("const int&"));
    assert_eq!(type_of_use(source, "pointer").as_deref(), Some("int*"));
}

/// **`auto&&` collapses, and the collapse is what decides the reference.**
///
/// The old rule refused a forwarding reference outright, on the argument that the language deduces `T&` or `T&&` from
/// the initializer's *value category*, which a declaration's spelling does not record. That argument is right about
/// the category and wrong about the answer: this layer reads an **lvalue** initializer as an lvalue, so `auto&& bound
/// = base;` is `int&` and `auto&& bound = 42;` is `int&&` — the same rule the language applies, over the one category
/// this engine can see.
///
/// The refusal was also load-bearing in the wrong direction: `auto& vfs = _server->GetVFS();` (a `T&` initializer)
/// went through the same code and produced **`T&&`**, and six hover cards on a real project were wrong because of it.
#[test]
fn a_forwarding_reference_collapses() {
    // A prvalue initializer: `int`, so `auto&&` is `int&&`.
    let from_a_value = "int f() { auto&& bound = 42; return bound; }\n";
    assert_eq!(type_of_use(from_a_value, "bound").as_deref(), Some("int&&"));
    // **A reference-returning call collapses**, which is the half that was breaking real code: the deduced type
    // already says `Widget&`, so `auto&&` is `Widget&` and not `Widget&&&`.
    let from_a_reference = "\
struct Widget { int size; };
Widget &the_widget();
int f() { auto&& bound = the_widget(); return bound.size; }
";
    assert_eq!(
        type_of_use(from_a_reference, "bound").as_deref(),
        Some("Widget&")
    );
    // **A named initializer is an lvalue**, so `auto&&` on it is `int&` — the collapse C++ performs, decided from the
    // initializer's *shape* (a name is an lvalue; a literal or a call is not) because its type is `int` for both.
    let from_a_name = "int f() { int base = 1; auto&& bound = base; return bound; }\n";
    assert_eq!(type_of_use(from_a_name, "bound").as_deref(), Some("int&"));
}

/// **`auto&` on a reference-returning call is one reference, not two.**
///
/// `auto &vfs = _server->GetVFS();` where `GetVFS()` returns `VirtualFileSystem&` deduced **`VirtualFileSystem&&`**:
/// the initializer's reference was kept *and* the declarator's was applied. Everything downstream then failed, because
/// `&&` names no class — measured on a real project, `vfs.GetVirtualFile(...)` answered
/// `Unknown(NotDeclaredHere("VirtualFileSystem::GetVirtualFile"))`.
#[test]
fn auto_reference_on_a_reference_returns_one_reference() {
    let source = "\
struct Widget { int size; };
Widget &the_widget();
int f() {
    auto &widget = the_widget();
    return widget.size;
}
";
    assert_eq!(
        type_of_use(source, "widget").as_deref(),
        Some("Widget&"),
        "a reference in the initializer never survives into `auto`"
    );
}

/// **`auto* p = x;` where `x` is not a pointer is refused** — the declaration does not compile, and inventing the
/// pointee would hide that.
#[test]
fn a_pointer_declaration_needs_a_pointer_initializer() {
    let source = "int f() { int base = 1; auto* wrong = base; return base; }\n";
    assert_eq!(type_of_use(source, "wrong"), None);
}

/// **A self-referential declaration terminates.** `auto x = x;` is ill-formed C++ and perfectly parseable, so the
/// recursion has to stop: past the depth bound the answer is `Unknown`, which is what every unreadable type gets.
#[test]
fn a_declaration_that_refers_to_itself_is_refused_rather_than_looping() {
    let source = "int f() { auto x = x; return 0; }\n";
    assert_eq!(type_of_use(source, "x"), None);
}

/// A declaration with nothing to deduce from — `auto n;` — is refused rather than answered with the word `auto`.
#[test]
fn a_declaration_with_no_initializer_is_refused() {
    let source = "int f() { auto n; return 0; }\n";
    assert_eq!(type_of_use(source, "n"), None);
}

/// **`auto` in another file is refused**, and the boundary is the syntax rather than the reading: the initializer
/// is in that header, and this layer holds one file's tree. The index's spelling for it is `auto`, which is not a
/// type, so saying nothing is the honest answer.
#[test]
fn an_auto_declaration_in_another_file_is_refused() {
    let header = "int count();\nauto measured = count();\n";
    let source = "#include \"b.h\"\nint f() { return measured; }\n";
    let session = session_with(&[("/p/a.cpp", source), ("/p/b.h", header)]);
    let view = session.view("/p/a.cpp").expect("the file is held");
    let offset = last_word(source, "measured");

    match session.type_at(&view, as_reading(&view, offset)) {
        cpp_code_analysis::Known::Yes(found) => assert_ne!(
            found.type_of, "auto",
            "the placeholder is never reported as a type"
        ),
        cpp_code_analysis::Known::Unknown(_) | cpp_code_analysis::Known::No => {}
    }
}

/// **The payoff: a member access through an `auto` variable now lists the class's members.**
///
/// This is what the deduction is *for* — `auto w = Widget{}; w.` is how modern C++ is written, and until the type
/// was computed the completion list was empty because the object's type was the word `auto`.
#[test]
fn a_member_access_through_auto_lists_the_members() {
    let source = "\
struct Widget { int size; int scaled(int factor) const; };
int f() {
    auto w = Widget{};
    w.
}
";
    let session = session_with(&[("/p/a.cpp", source)]);
    let view = session.view("/p/a.cpp").expect("the file is held");
    let offset = source.rfind("w.").expect("the fixture writes the access") + 2;

    match session.member_completions(
        &view,
        cpp_code_analysis::file::coordinates::FileOffset::new(offset),
    ) {
        cpp_code_analysis::Known::Yes(found) => {
            let names: Vec<String> = found
                .members
                .members
                .iter()
                .map(|member| member.fact.name.to_string())
                .collect();
            assert!(
                names.contains(&"size".to_string()) && names.contains(&"scaled".to_string()),
                "the members of the deduced class: {names:?}"
            );
        }
        other => panic!("an `auto` object's members are listable now, got {other:?}"),
    }
}

/// A **plain** declaration is unaffected: the type it was written with is still the answer.
///
/// The spelling is now **written by the reader** rather than cut out of the declaration's text, so it is the same
/// type with the whitespace of a spelling rather than the whitespace of a file — `const Widget*` for `const Widget* w`
/// where the text cut used to give `Widget *`. That is the one visible difference, and it is deliberate: the reader
/// is what knows the `*` belongs to the `Widget`.
///
/// **The `const` is part of it**, which this expectation used to say was not — the reader dropped qualifiers as
/// "not part of what the type is called". That is right for a lookup and wrong for a recorded type: it made
/// `static constexpr const char* Name = "…";` a `char*` and had `assign-type-check` refuse a string literal.
#[test]
fn a_declaration_that_names_its_type_keeps_it() {
    assert_eq!(
        type_of_use(
            "struct Widget { int size; };\nint f() { const Widget* w = nullptr; return 0; }\n",
            "w"
        )
        .as_deref(),
        Some("const Widget*")
    );
}

/// **The class behind a linkage specification has its members.**
///
/// MSVC writes its whole iostream hierarchy as `_EXPORT_STD extern "C++" template <…> class basic_istream : …`,
/// and while the scope walk stopped at the unnamed outer declaration, the class and every member of it were
/// missing from the index — which is what `std::cin.read` and a completion after `std::cin.` had to answer from.
#[test]
fn a_member_of_a_class_behind_a_linkage_specification_resolves() {
    let source = "\
namespace lib {
extern \"C++\" template <class _Elem>
class Stream {
public:
    int member;
};
extern Stream<char> input;
}
int f() { return lib::input.member; }
";
    assert_eq!(type_of_use(source, "member").as_deref(), Some("int"));
}

/// **Two declarations of one name, one type between them.**
///
/// A name query answers `Ambiguous` when several declarations are visible, which is right for "where is this
/// declared" — and the question here is the *type*, which a redeclaration does not make ambiguous. MSVC's
/// `<iostream>` writes `cin` twice (once plain, once as `_EXPORT_STD extern "C++" … istream cin;`), and while the
/// type query could not settle it, every use of `std::cin` lost its type: measured, 8 offsets in one file.
#[test]
fn a_name_declared_twice_with_one_type_still_has_that_type() {
    let header = "struct Widget { int size; };\nextern Widget shared;\nextern Widget shared;\n";
    let source = "#include \"lib.h\"\nint f() { return shared.size; }\n";
    let session = session_with(&[("/p/a.cpp", source), ("/p/lib.h", header)]);

    assert_eq!(
        type_of_use_in(&session, "/p/a.cpp", source, "shared").as_deref(),
        Some("Widget"),
        "the two declarations agree, so the type is not the ambiguous part"
    );
    assert_eq!(
        type_of_use_in(&session, "/p/a.cpp", source, "size").as_deref(),
        Some("int"),
        "and the member access through it works"
    );
}

/// …but declarations that **disagree** about the type keep the `Unknown`: that is exactly what `Ambiguous` was
/// about, and picking one of them would be a guess.
#[test]
fn two_declarations_that_disagree_about_the_type_are_still_unknown() {
    let header =
        "struct One { int a; };\nstruct Two { int b; };\nextern One shared;\nextern Two shared;\n";
    let source = "#include \"lib.h\"\nint f() { return shared.a; }\n";
    let session = session_with(&[("/p/a.cpp", source), ("/p/lib.h", header)]);

    assert_eq!(type_of_use_in(&session, "/p/a.cpp", source, "shared"), None);
}

/// **A deduction cycle terminates instead of taking the stack with it.**
///
/// A return type deduced from a body is read by typing the body, and a body may ask about the very expression the
/// caller started from. The shape below is the measured one, from a real C++23 project (`module_graph.cpp`, found by
/// running the member scoreboard on `clice` — it overflowed the stack with **32 MB** of it):
///
/// ```text
/// auto text_of(int file) { return lookup(tokens.text(text_of(file).content)); }
/// …
/// return text_of(f).content;        ← the question that closes the loop
/// ```
///
/// `text_of(f).content` needs `text_of`'s return type → its body → `tokens.text(…)` → that argument → the same
/// `.content` again. `MAX_TYPE_DEPTH` cannot stop it: a type read out of another declaration deliberately starts a
/// **new** question at depth 0, which is right for `auto x = f(); x.size()` and fatal for a cycle. The memo could not
/// stop it either, because an answer is remembered **after** it is computed.
///
/// So the answer is `Unknown` — the type is not knowable from these questions — and the process survives to say so.
#[test]
fn a_deduction_cycle_answers_unknown_instead_of_overflowing_the_stack() {
    let source = "\
struct Tokens { const char* text(const char* s) const; };
extern Tokens tokens;
const char* lookup(const char* key);
auto text_of(int file) { return lookup(tokens.text(text_of(file).content)); }
const char* g(int f) { return text_of(f).content; }
";
    let session = session_with(&[("/p/cycle.cpp", source)]);

    // The cycle is asked about by the *last* occurrence, which is the one inside `g`.
    assert_eq!(
        type_of_use_in(&session, "/p/cycle.cpp", source, "content"),
        None,
        "a question that needs its own answer is not answerable, and saying so is the whole point"
    );
}

/// The type of the use of a name **in a session the caller built** — the two-file fixtures' way in.
///
/// The offset is the name's **last byte**, not its first, and that is the product's own rule rather than a
/// convenience: a cursor "on a name" is anywhere inside it, and an offset at the name's *start* is the boundary
/// between it and whatever precedes it — where a `*`, a `(`, or a `.` decides what the expression is. Measured,
/// `v.data()` asked at the offset of `data` answers about `v.data` (an uncalled member function, which has no type)
/// while the same name asked one byte later answers `int*`, which is what a reader pointing at the name sees.
fn type_of_use_in(
    session: &Session<MemoryFiles>,
    path: &str,
    source: &str,
    spelling: &str,
) -> Option<String> {
    let view = session.view(path).expect("the file is held");
    let offset = last_word(source, spelling) + spelling.len() - 1;

    match session.type_at(&view, as_reading(&view, offset)) {
        cpp_code_analysis::Known::Yes(found) => Some(found.type_of),
        _ => None,
    }
}

/// **A qualified name is a name.** `lib::global` is one entity, and "what type is it" has to be asked about the
/// whole chain rather than about the segment the expression happens to *start* at.
///
/// That offset is the whole subject of this test: the expression's node begins at `lib`, and a lookup at that
/// offset asks about the **namespace** — which has no type, so the answer used to be `UnknownType("lib::global")`
/// for every qualified name in every file.
#[test]
fn a_qualified_name_in_this_file_has_the_type_its_declaration_wrote() {
    let source = "\
namespace lib {
struct Widget { int size; };
extern Widget global;
}
int f() { return lib::global.size; }
";
    assert_eq!(type_of_use(source, "global").as_deref(), Some("Widget"));
    assert_eq!(
        type_of_use(source, "size").as_deref(),
        Some("int"),
        "and a member access through it reads the member's type"
    );
}

/// The same question through the **index**: the chain is declared in a header, so this file's own scopes cannot
/// answer it, and the whole qualified spelling is what the index has to be asked about.
#[test]
fn a_qualified_name_declared_in_a_header_has_its_type() {
    let header = "namespace lib { struct Widget { int size; }; extern Widget shared; }\n";
    let source = "#include \"lib.h\"\nint f() { return lib::shared.size; }\n";
    let session = session_with(&[("/p/a.cpp", source), ("/p/lib.h", header)]);

    assert_eq!(
        type_of_use_in(&session, "/p/a.cpp", source, "shared").as_deref(),
        Some("Widget"),
        "the declaration is in the header, so the answer comes from the index"
    );
    assert_eq!(
        type_of_use_in(&session, "/p/a.cpp", source, "size").as_deref(),
        Some("int"),
        "and the member access through it reads the member"
    );
}

/// **A member of a class declared in a header has its type in the method's body.**
///
/// Reported from `EmmyLuaCodeStyle`: `LuaParser::_tokens` is `std::vector<LuaToken>` — its own hover says so, straight
/// out of the index's member list — and yet `_tokens.size()` in `LuaParser.cpp` answered *"the type of `_tokens` is
/// not known here"*, so `size` could not be resolved at all.
///
/// The reason is the two-layer split: the file's scope tree has no scope for a class declared in another file, and
/// the index's unqualified lookup does not match a fact whose scope is `Parser`. The class is named by the
/// *definition's* own declarator (`Parser::Parse`), and the query that knows a member of it is the one the hover uses.
#[test]
fn a_member_of_a_class_declared_in_a_header_has_its_type_in_the_body() {
    let header = "\
struct Token { int kind; };
struct Parser {
    Token _token;
    int Parse();
};
";
    let source = "\
#include \"api.h\"
int Parser::Parse() { return _token.kind; }
";
    let session = session_with(&[("/p/a.cpp", source), ("/p/api.h", header)]);
    assert_eq!(
        type_of_use_in(&session, "/p/a.cpp", source, "_token").as_deref(),
        Some("Token"),
    );
    assert_eq!(
        type_of_use_in(&session, "/p/a.cpp", source, "kind").as_deref(),
        Some("int"),
        "and the member access through it reads the member's own type"
    );
}

/// **The member is the class's, even when three classes declare that name.**
///
/// This is the real shape `EmmyLuaCodeStyle` has, and the one the first fixture above could not fail on: `_tokens`
/// is a member of `LuaLexer`, `LuaParser` **and** `LuaSyntaxTree`, so the index's unqualified lookup answers
/// `Ambiguous` — and the type engine had no way to ask "the class whose member function this is", which is what C++
/// looks in first. Measured on `LuaParser.cpp`: `type_at("_tokens")` answered `Unknown(Ambiguous)`, so
/// `_tokens.size()` could not resolve `size` at all.
#[test]
fn a_member_name_several_classes_declare_is_the_enclosing_class() {
    let header = "\
struct Token { int kind; };
struct Lexer {
    Token _token;
};
struct Parser {
    Token _token;
    int Parse();
};
";
    let source = "\
#include \"api.h\"
int Parser::Parse() { return _token.kind; }
";
    let session = session_with(&[("/p/a.cpp", source), ("/p/api.h", header)]);
    eprintln!(
        "definition(_token) = {:?}",
        session
            .index()
            .definition("_token", std::path::Path::new("/p/a.cpp"))
    );
    assert_eq!(
        type_of_use_in(&session, "/p/a.cpp", source, "_token").as_deref(),
        Some("Token"),
        "the enclosing class's member, not an ambiguous name"
    );
}

/// **A union's members are reachable through the union, and the union has no spelling of its own.**
///
/// Reported from `EmmyLuaCodeStyle`, `Mark.h`:
///
/// ```cpp
/// struct MarkEvent {
///     MarkEventType Type;
///     union {
///         struct { std::size_t Parent; LuaSyntaxNodeKind Kind; } Start;
///         struct { std::size_t Index; LuaTokenKind Kind; } Token;
///         struct { LuaTokenKind TokenKind; LuaParserErrorKind ErrorKind; } Error;
///     } U;
/// };
/// ```
///
/// `e.U.Start.Kind` in `LuaSyntaxTree.cpp` answered *"the type of `e.U` is not known here"*: `U`'s type is an unnamed
/// union, so the fact recorded no `type_of` at all, and the chain stopped at `U`. The type's name is the one the
/// parser already gives the anonymous type's **members** — `MarkEvent::U` — and that is what this asserts, member by
/// member down the chain.
#[test]
fn a_member_of_an_unnamed_union_has_a_type_and_its_members_are_reachable() {
    let source = "\
struct MarkEvent {
    int Type;
    union {
        struct { int Parent; int Kind; } Start;
        struct { int Index; } Token;
    } U;
};
int f(MarkEvent e) { return e.U.Start.Kind; }
";
    assert_eq!(type_of_use(source, "U").as_deref(), Some("MarkEvent::U"));
    assert_eq!(
        type_of_use(source, "Start").as_deref(),
        Some("MarkEvent::U::Start")
    );
    assert_eq!(
        type_of_use(source, "Kind").as_deref(),
        Some("int"),
        "the chain ends at the real member's type"
    );
}

/// **A member of a class template has the type its arguments make it, not the parameters it was written with.**
///
/// This is the shape a standard container is made of, and until the parameters were paired with the arguments the
/// answer was a type called `_Ty` — which is not a class, so `*v.data()` and a completion after it had nothing. The
/// header is a separate file on purpose: the parameter names are written **there**, and a query asking about `v`
/// holds only `a.cpp`.
///
/// The members here are written with `_Ty` **directly** rather than through a nested alias (`reference` is
/// `_Ty&`). That is deliberate and it is the boundary this feature stops at: substituting into `_Ty&` is this
/// layer's own operation, while `front()` returning `reference` needs the *alias* to be resolved before there is
/// anything to substitute into — a template member whose type is another template's nested type, which is a
/// separate problem with its own test ([`a_member_whose_type_is_a_nested_alias_is_not_substituted`]).
#[test]
fn a_member_of_a_class_template_takes_its_type_from_the_arguments() {
    let header = "\
namespace std {
template <class _Ty, class _Alloc = int>
struct vector {
    _Ty& front;
    _Ty* data();
};
}
";
    let source = "\
#include \"vector.h\"
int f() {
    std::vector<int> v;
    return *v.data() + v.front;
}
";
    let session = session_with(&[("/p/a.cpp", source), ("/p/vector.h", header)]);

    assert_eq!(
        type_of_use_in(&session, "/p/a.cpp", source, "v").as_deref(),
        Some("std::vector<int>"),
        "the declaration's own type is the type it was written with"
    );
    assert_eq!(
        type_of_use_in(&session, "/p/a.cpp", source, "data").as_deref(),
        Some("int*"),
        "`data` is declared `_Ty*`, and `_Ty` is `int` here"
    );
    assert_eq!(
        type_of_use_in(&session, "/p/a.cpp", source, "front").as_deref(),
        Some("int&"),
        "and `front` is declared `_Ty&`"
    );
}

/// **A member function the reader is pointing at inside a call has what the call has.**
///
/// A function has no type *as a name* — `DeclFact::returns` is the field for what a call of it gives — so the four
/// offsets of `data` in `v.data()` answered nothing while the `(` one byte later answered `int*`. The answer
/// depended on which byte of one name the cursor was on, which is not a distinction a reader can see. A cursor on
/// the callee is a cursor on the call.
#[test]
fn a_member_function_inside_a_call_has_what_the_call_has() {
    let header = "\
namespace std {
template <class _Ty>
struct vector { _Ty* data(); };
}
";
    let source = "\
#include \"vector.h\"
int f() {
    std::vector<int> v;
    return *v.data();
}
";
    let session = session_with(&[("/p/a.cpp", source), ("/p/vector.h", header)]);

    assert_eq!(
        type_of_use_in(&session, "/p/a.cpp", source, "data").as_deref(),
        Some("int*"),
        "the callee inside a call has the return type, arguments substituted"
    );
}

/// **A member function merely *named* has no type, and saying so is the answer.** `v.data;` is a member function
/// not being called: there is no `int*` involved, and answering with the return type would be a claim about an
/// expression the file did not write.
#[test]
fn a_member_function_that_is_not_called_has_no_type() {
    let header = "\
namespace std {
template <class _Ty>
struct vector { _Ty* data(); };
}
";
    let source = "\
#include \"vector.h\"
int f() {
    std::vector<int> v;
    v.data;
    return 0;
}
";
    let session = session_with(&[("/p/a.cpp", source), ("/p/vector.h", header)]);

    assert_eq!(
        type_of_use_in(&session, "/p/a.cpp", source, "data"),
        None,
        "a function named on its own has no type"
    );
}

/// **A member reached through an *alias* is finished with the arguments the alias carried.**
///
/// `std::string` is one name with no arguments of its own, and the class it names was written with three:
/// `basic_string<char, char_traits<char>, allocator<char>>`. So `_Elem` has nothing to be replaced by unless the
/// alias's target supplies the pairing — measured, this is the last step between `x.back()` answering `_Elem&` and
/// answering `char&`, and it is the same step that makes `std::string::size_type` a `size_t` rather than a name.
///
/// The **use** wins when it wrote arguments of its own (`std::vector<int>`), which is what the two lists in the
/// fixture are for: the alias's arguments are only consulted when the use said nothing.
#[test]
fn a_member_of_a_class_reached_through_an_alias_gets_the_aliass_arguments() {
    let header = "\
namespace std {
template <class _Elem>
struct basic_string {
    using value_type = _Elem;
    using reference = value_type&;
    reference back();
    reference front;
};
using string = basic_string<char>;
}
";
    let source = "\
#include \"string.h\"
int f() {
    std::string s;
    return s.front;
}
int g() {
    std::string s;
    auto& back = s.back();
    return back;
}
";
    let session = session_with(&[("/p/a.cpp", source), ("/p/string.h", header)]);

    assert_eq!(
        type_of_use_in(&session, "/p/a.cpp", source, "front").as_deref(),
        Some("char&"),
        "`reference` is `value_type&` is `_Elem&`, and `string` says `_Elem` is `char`"
    );
    assert_eq!(
        type_of_use_in(&session, "/p/a.cpp", source, "back").as_deref(),
        Some("char&"),
        // The declarator writes the reference, so the substitution's `&` survives it — `auto back = …` would deduce
        // the *value* `char`, which is the language's rule and not this test's question.
        "and the same for a member that is *called*, when the declarator asks for a reference"
    );
}

/// A member whose type is *another* member of the same class is resolved, with the same argument pairing.
///
/// `_Ty& reference; reference front;` is how the standard library writes almost every member it has: the type of
/// `front` is the *name* `reference`, which mentions no parameter at all, so substituting the arguments into it
/// replaces nothing. The step that finishes it is a second member lookup — `reference` in the same class, with the
/// same `_Ty = int` — and the walk repeats while the type is still a bare name. That is also what makes
/// `size_type` → `size_t` work, which is the shape `v.size()` has.
#[test]
fn a_member_whose_type_is_a_nested_alias_is_resolved() {
    let header = "\
namespace std {
template <class _Ty>
struct vector {
    typedef _Ty& reference;
    reference front;
};
}
";
    let source = "\
#include \"vector.h\"
int f() {
    std::vector<int> v;
    return v.front;
}
";
    let session = session_with(&[("/p/a.cpp", source), ("/p/vector.h", header)]);

    assert_eq!(
        type_of_use_in(&session, "/p/a.cpp", source, "front").as_deref(),
        Some("int&"),
        "`reference` is `_Ty&` and `_Ty` is `int`, one member lookup further in"
    );
}

/// **A chain of two is real**: `size_type` is `size_t`, and a member declared with `size_type` is a `size_t`.
///
/// The walk terminates because each step asks about a **different declaration** — a class has finitely many
/// members — and the cycle that would break that (`reference reference;`) is a member declared with its own type,
/// which the reader records as the member itself and the walk refuses to follow.
#[test]
fn a_chain_of_nested_types_resolves() {
    let source = "\
namespace std {
template <class _Ty>
struct vector {
    typedef unsigned long size_type;
    typedef size_type difference_type;
    difference_type count;
};
}
int f() {
    std::vector<int> v;
    return (int)v.count;
}
";
    let session = session_with(&[("/p/a.cpp", source)]);

    assert_eq!(
        type_of_use_in(&session, "/p/a.cpp", source, "count").as_deref(),
        Some("unsigned long"),
        "two members deep, and the name that ends the chain is the type"
    );
}

/// **A `using` alias is an alias too**, and its target is written after the `=` rather than in a declarator — so
/// it takes the *other* branch of the alias reader (`declared_alias_target`) than `typedef` does. Both branches end
/// in the same field, and a member declared with either has the type the alias points at.
#[test]
fn a_using_alias_resolves_like_a_typedef() {
    let source = "\
namespace std {
template <class _Ty>
struct vector {
    using size_type = unsigned long;
    size_type count;
};
}
int f() {
    std::vector<int> v;
    return (int)v.count;
}
";
    let session = session_with(&[("/p/a.cpp", source)]);

    assert_eq!(
        type_of_use_in(&session, "/p/a.cpp", source, "count").as_deref(),
        Some("unsigned long"),
        "`using size_type = unsigned long;` is the same fact as `typedef unsigned long size_type;`"
    );
}

/// **A class template written without arguments keeps its parameters**, which is the answer that must not become a
/// guess: `std::vector` with no argument has no `_Ty` to pair, so a member's type stays `_Ty&` — a name a caller can
/// see is unfinished rather than a type that happens to be wrong.
#[test]
fn a_template_without_arguments_keeps_its_parameters() {
    let source = "\
namespace std {
template <class _Ty>
struct vector { typedef _Ty& reference; };
}
int f() { std::vector v; return 0; }
";
    // The declaration is in this file, so the parameters come from its own syntax.
    let session = session_with(&[("/p/a.cpp", source)]);
    let view = session.view("/p/a.cpp").expect("the file is held");

    let names = session
        .index()
        .template_parameters_of("std::vector", &view.path);
    assert_eq!(
        names,
        vec!["_Ty".to_string()],
        "the parameter list is read out of the declaration"
    );
}

/// **A class template written inside another one declares its *own* parameters**, and the answer must be the inner
/// list: `outer<int>::inner<char>` pairs `_Uty` with `char`, and a reader that took the outer `_Ty` would pair the
/// wrong argument with the wrong name — the same defect the partial-specialization rule above exists to avoid.
///
/// The two templates are nested, so both specifier sequences contain the binding and *both* are answers to "which
/// template introduces this class"; the innermost one is the one that does.
#[test]
fn a_nested_class_template_declares_its_own_parameters() {
    let source = "\
namespace std {
template <class _Ty>
struct outer {
    template <class _Uty>
    struct inner { _Uty u; };
};
}
int f() { return 0; }
";
    let session = session_with(&[("/p/a.cpp", source)]);
    let view = session.view("/p/a.cpp").expect("the file is held");

    let names = session
        .index()
        .template_parameters_of("std::outer::inner", &view.path);
    assert_eq!(
        names,
        vec!["_Uty".to_string()],
        "the innermost template is the one that introduces the class"
    );
}
/// **A variable template takes its type from the arguments**, the way a class template's member does.
///
/// `template <class T> constexpr T pi = T(3.14159);` is a `Variable` whose name sits in the specifier sequence its
/// template introduces — the same relation a class or function template has — and the filter that decides which
/// bindings may have template parameters did not list `Variable`, so `pi<double>` had **no parameters to substitute
/// into** and its type stayed the parameter's own name: measured before this was written, `double x = pi<double>;`
/// answered `T`.
///
/// **And the spellings this does not cover, both measured.**
///
/// * **A two-argument template-id in an expression** — `ratio<float, double>` — answers `None` rather than a type.
///   That is the oldest ambiguity in C++ (`a < b, c > d`) and it belongs to the expression reader, not to this
///   pairing: the one-argument case below is the same code path and it works.
/// * **A defaulted parameter** — see the test below.
#[test]
fn a_variable_template_takes_its_type_from_the_arguments() {
    let header = "\
#pragma once
template <class T>
constexpr T pi = T(3.14159);


template <class T, class U = T>
constexpr U twice = U(2);
";
    let source = "\
#include \"constants.h\"
double f() {
    double a = pi<double>;
    // A two-argument template-id is a separate question: measured, `ratio<float, double>` answers `None`, which is the
    // shape of the oldest ambiguity in C++ — `a < b, c > d` — and it belongs to the expression reader, not here.
    return a + b;
}
";
    let session = session_with(&[("/p/a.cpp", source), ("/p/constants.h", header)]);

    assert_eq!(
        type_of_use_in(&session, "/p/a.cpp", source, "pi").as_deref(),
        Some("double"),
        "`pi` is declared `T`, and `T` is `double` here"
    );
}

/// **A defaulted template parameter of a variable template takes its default.**
///
/// `template <class T, class U = T> constexpr U twice = U(2);` written as `twice<float>` writes one argument for two
/// parameters, and the second's meaning is the first's — which makes the answer `float`.
///
/// This was a known red, and the reason was where the defaults were not: `DeclFact::parameters` held the parameter
/// **names** and nothing held the `= T` beside them, so the positional pairing ran out of arguments and left the name
/// standing. [`DeclFact::parameter_defaults`] is that field (codec version 30), and the substitution falls back to it:
/// [`crate::TypeSubstitutions::with_defaults`] for a `Type` — this case — and
/// [`crate::substituted_spelling_with_defaults`] for a spelling, which is what a member's recorded type is.
///
/// The explicit half of the same question works and is asserted above.
#[test]
fn a_defaulted_template_parameter_of_a_variable_template_substitutes() {
    let header = "\
#pragma once
template <class T, class U = T>
constexpr U twice = U(2);
";
    let source = "\
#include \"constants.h\"
float f() {
    float b = twice<float>;
    return b;
}
";
    let session = session_with(&[("/p/a.cpp", source), ("/p/constants.h", header)]);

    assert_eq!(
        type_of_use_in(&session, "/p/a.cpp", source, "twice").as_deref(),
        Some("float"),
        "the second parameter defaults to `T`, which the use wrote as `float`"
    );
}
/// **A compiler builtin reads as a `bool`, and the library's lookalike does not.**
///
/// `enable_if_t<__is_constructible(_Ty, _Args...), _Ty>` is how MSVC's STL writes a constrained overload, and the
/// first argument of `enable_if_t` is a `bool` there. Before this the reader took the head of that spelling —
/// `__is_constructible` — for a class name, and everything downstream followed from a class where a `bool` was meant.
///
/// The second assertion is the one that keeps the first from being a wrong answer shaped like a right one: in a
/// **type** position `is_same<_Ty, _Uty>` is the class template the library defines, and only the compiler's own
/// `__`-prefixed names are intrinsics. A table that listed both spellings would have made `std::is_same<A, B>` read
/// as `bool`.
#[test]
fn a_compiler_builtin_reads_as_a_bool_and_the_library_lookalike_does_not() {
    use cpp_code_analysis::sema::types::parse_type_spelling;

    for builtin in [
        "__is_constructible(_Ty, _Args...)",
        "__is_pointer(_Ty)",
        "__is_same(_Ty, _Uty)",
        "__is_trivially_copyable(_Ty)",
    ] {
        assert_eq!(
            parse_type_spelling(builtin),
            parse_type_spelling("bool"),
            "`{builtin}` is a question the compiler answers, and its answer is a bool"
        );
    }

    assert_ne!(
        parse_type_spelling("is_same<_Ty, _Uty>"),
        parse_type_spelling("bool"),
        "the library's own `is_same` is a class template, not an intrinsic"
    );
}
/// **A trait a builtin decides selects the right specialization.**
///
/// This is the shape MSVC's STL is built out of, written out in full so the whole chain is in the fixture: a primary
/// `enable_if` with no `type`, a partial specialization for `true` that has one, an alias template over both, and two
/// members whose type is that alias with a **compiler builtin** as the condition.
///
/// The two steps this exercises are the ones that were missing when it was written. An **alias template applied to
/// arguments** is looked up by the head of the spelling and its own parameters are replaced in the target — before
/// that, `enable_if_t<A, B>` came back as its own spelling because the index files it under the bare name. And a
/// **compiler builtin is folded** where a pattern is compared, because the specialization's first slot is the literal
/// `true` and MSVC writes the condition as `__is_same(…)`. `__is_constructible(T, U)` beside them is *not* decided,
/// and the direction that matters is that an undecided builtin folds to nothing: it stays unequal, no specialization
/// is chosen, and the answer is "not known" rather than a type from one that may not apply.
#[test]
fn a_trait_a_builtin_decides_selects_the_right_specialization() {
    let header = "\
#pragma once
template <bool _Test, class _Ty = void>
struct enable_if {};

template <class _Ty>
struct enable_if<true, _Ty> {
    using type = _Ty;
};

template <bool _Test, class _Ty = void>
using enable_if_t = typename enable_if<_Test, _Ty>::type;
";
    let source = "\
#include \"enable_if.h\"

struct Widget {
    enable_if_t<__is_same(int, int), double> when_the_same;
    enable_if_t<__is_pointer(int*), float> when_a_pointer;
};

void f(Widget& w) {
    double a = w.when_the_same;
    float b = w.when_a_pointer;
}
";
    let session = session_with(&[("/p/a.cpp", source), ("/p/enable_if.h", header)]);

    assert_eq!(
        type_of_use_in(&session, "/p/a.cpp", source, "when_the_same").as_deref(),
        Some("double"),
        "`__is_same(int, int)` is decided `true`, so the specialization with a `type` is the one that applies"
    );
    assert_eq!(
        type_of_use_in(&session, "/p/a.cpp", source, "when_a_pointer").as_deref(),
        Some("float"),
        "and `__is_pointer(int*)` is decided `true` the same way"
    );
}
/// **An alias template's target with its parameters replaced** — the step `enable_if_t<A, B>` needs.
///
/// An alias is stored as the spelling after its `=`, so `template <class _Test, class _Ty> using enable_if_t =
/// typename enable_if<_Test, _Ty>::type;` is the text `typename enable_if<_Test, _Ty>::type`, and the use has to
/// become `typename enable_if<__is_same(int, int), double>::type` before anything can be looked up in it.
///
/// Whole words only, and that is the assertion worth having: `_Ty` is a **prefix** of `_Ty2`, and a substring
/// replacement would rewrite a name the alias never mentioned.
#[test]
fn an_alias_target_is_substituted_by_whole_words() {
    use cpp_code_analysis::sema::types::substituted_spelling;

    let names = vec!["_Test".to_string(), "_Ty".to_string()];
    let arguments = vec!["__is_same(int, int)".to_string(), "double".to_string()];

    assert_eq!(
        substituted_spelling("typename enable_if<_Test, _Ty>::type", &names, &arguments),
        "typename enable_if<__is_same(int, int), double>::type",
        "both parameters are replaced, and the names the alias did not write are left alone"
    );
    assert_eq!(
        substituted_spelling("_Ty2", &names, &arguments),
        "_Ty2",
        "`_Ty` is a prefix of `_Ty2`, and a substring replacement would have rewritten it"
    );
    assert_eq!(
        substituted_spelling(
            "vector<_Ty>",
            &names,
            &["int".to_string(), "double".to_string()]
        ),
        "vector<double>",
        "an argument that mentions another parameter is not re-substituted"
    );
}

/// **`auto` over a temporary of a template type** — the shape from a real editor session that reported nothing.
///
/// `auto z = std::vector<int>();` writes no type next to the name; the type is the initialiser's, and that initialiser
/// is a **type-id call**. If the spelling comes out as `std::vector` without its argument, every later answer is wrong:
/// the member list is looked up for a class that has none, and a hover on `z.push_back` falls back to "every
/// `push_back` in the project".
#[test]
fn an_auto_variable_takes_the_templated_temporarys_type() {
    // `std::vector` is **declared** here, because a bare `auto z = std::vector<int>()` in a file with no `<vector>` is a
    // construction of a name the index has never seen — and then the honest answer is "no type", not a guess.
    let source = "\
namespace std {
template <class T>
class vector;
}
void f() {
    auto z = std::vector<int>();
}
";
    assert_eq!(
        type_of_use(source, "z").as_deref(),
        Some("std::vector<int>"),
        "the argument is part of the type the declaration has"
    );
}

/// And the same for a call: `auto y = g();` has `g`'s return type, template arguments and all.
#[test]
fn an_auto_variable_takes_the_calls_return_type() {
    let source = "\
std::string g();
void f() {
    auto y = g();
}
";
    assert_eq!(type_of_use(source, "y").as_deref(), Some("std::string"));
}

/// **A member's return type with the class's arguments substituted** — `basic_string<char>::back()` is `char&`.
///
/// The declaration writes `_Elem&`; the class is `template <class _Elem> class basic_string`; the use is through
/// `std::string`, which is `basic_string<char>`. The answer a reader wants is `char&`, and it exists in the same two
/// facts the member lookup already reads: the parameters the class declares and the arguments the alias supplies.
#[test]
fn a_members_return_type_has_the_class_arguments_substituted() {
    let source = "\
namespace std {
template <class Elem>
class basic_string {
public:
    Elem& back();
};
using string = basic_string<char>;
}
void f() {
    std::string x;
    auto& y = x.back();
}
";
    assert_eq!(
        type_of_use(source, "y").as_deref(),
        Some("char&"),
        "`back()` on a `basic_string<char>` returns `char&`, and the declarator asked for a reference"
    );
}

/// The same substitution, with the class used **directly as a template-id** rather than through an alias:
/// `std::vector<int> v; auto& z = v.front();` is `int&`.
#[test]
fn a_member_return_type_is_substituted_through_a_template_id_use() {
    let source = "\
namespace std {
template <class T>
class vector {
public:
    T& front();
};
}
void f() {
    std::vector<int> v;
    auto& z = v.front();
}
";
    assert_eq!(type_of_use(source, "z").as_deref(), Some("int&"));
}

/// **`operator[]` and the type it produces** — `v[0]` on a `std::vector<int>` should be an `int&`.
///
/// A subscript is an operator call, so the question is the same one the member-return tests ask: the class's
/// `operator[]` returns `T&`, the class is instantiated at `int`, and the answer is `int&`. What this measures is whether
/// `type_of_expression` knows that a subscript is that call.
///
/// # Where it stands
///
/// Measured: `v[0]` answers `Unknown(UnknownType("v[0]"))` — a subscript is not an expression kind the engine has an arm
/// for. An arm was added beside the `new` arm: `IndexExpr` → the object's type → `members_of` → the member named
/// `operator[]` → its `returns`, which `members_of` has already substituted (`T&` at `int`). It does not fire yet, so one of
/// its four conditions is false for this fixture; the next probe belongs **inside** that arm, printing which condition.
/// `v.front()` on the same fixture **does** work (`a_member_return_type_is_substituted_through_a_template_id_use`), so the
/// substitution itself is not in question.
#[test]
fn a_subscript_has_the_operators_return_type() {
    use cpp_code_analysis::file::coordinates::FileOffset;
    let source = "\
namespace std {
template <class T>
class vector {
public:
    T& at(unsigned long index);
    T& operator[](unsigned long index);
};
}
void f() {
    std::vector<int> v;
    int x = v[0];
    (void)x;
}
";
    let session = session_with(&[("/p/a.cpp", source)]);
    let view = session.view("/p/a.cpp").expect("the file is cooked");
    // The cursor on `v` of `v[0]` — a subscript is an expression whose type is the operator's return type.
    let at = source.find("v[0]").expect("the fixture subscripts") + 1;
    let reading = view
        .reading_offset_of(FileOffset::new(at))
        .expect("the offset is in the file");
    let answer = match session.type_at(&view, reading) {
        cpp_code_analysis::Known::Yes(found) => Some(found.type_of),
        other => {
            println!("`v[0]` at file {at} → {other:?}");
            None
        }
    };

    println!("`v[0]` is {answer:?}");
    assert_eq!(
        answer.as_deref(),
        Some("int&"),
        "a subscript is `operator[]`, whose return type is the class's `T&` at `int`"
    );
}

/// **A subscript whose operator returns the class's own `reference`** — the shape every container writes, and the one
/// the fact used to refuse.
///
/// `reference operator[](size_type)` is how `std::string`, `std::span` and every hand-written container declare it, and
/// `reference` is a typedef of the class. A fact is read by consumers that have no class, so
/// [`cpp_code_analysis::DeclFact::returns`] is `None` for that declaration — 60 false positives of `undefined-field`
/// taught that rule. The subscript is the one consumer that *does* have the class, so the spelling is kept beside it
/// ([`cpp_code_analysis::DeclFact::returns_in_the_class`]) and resolved there: `reference` is `_CharT&`.
///
/// Measured before: `s[0]` answered `Unknown(UnknownType("s[0]"))`.
#[test]
fn a_subscript_whose_operator_returns_the_classs_own_reference() {
    use cpp_code_analysis::file::coordinates::FileOffset;
    let source = "\
template <class _CharT>
class basic_string {
public:
    typedef _CharT& reference;
    reference operator[](unsigned long index);
};
void f() {
    basic_string<char> s;
    char c = s[0];
    (void)c;
}
";
    let session = session_with(&[("/p/a.cpp", source)]);
    let view = session.view("/p/a.cpp").expect("the file is cooked");
    let at = source.find("s[0]").expect("the fixture subscripts") + 1;
    let reading = view
        .reading_offset_of(FileOffset::new(at))
        .expect("the offset is in the file");

    let answer = match session.type_at(&view, reading) {
        cpp_code_analysis::Known::Yes(found) => Some(found.type_of),
        other => {
            println!("`s[0]` at file {at} → {other:?}");
            None
        }
    };
    assert_eq!(
        answer.as_deref(),
        Some("char&"),
        "`reference` is the class's own alias for `_CharT&`, and `_CharT` is `char`"
    );
}

/// **A member of an unnamed union, and of an unnamed struct inside it** — the shape `MarkEvent` is written in, and the
/// report behind it ("我们依然无法正确的推断出 Union 的后续").
///
/// ```cpp
/// struct MarkEvent {
///     union {
///         struct { std::size_t Parent; LuaSyntaxNodeKind Kind; } Start;
///         …
///     } U;
/// };
/// event.U.Start.Kind = …;
/// ```
///
/// The chain is three questions and every one of them is answerable: `U`'s type is the unnamed union — named by
/// [`cpp_code_analysis::sema::declarations::an_unnamed_class_body_named`] after the scope it is written in
/// (`MarkEvent::U`) — `Start`'s type is the unnamed struct inside it (`MarkEvent::U::Start`), and its members are
/// `Parent` and `Kind`. Measured: all four answers are right, so **what fails on the report is not this layer**: there
/// the object is `events[Pos]` on a `std::vector<MarkEvent>`, and that is the subscript (`operator_returns_of`), whose
/// note records the alias chain a `std::vector` still needs.
#[test]
fn a_member_of_an_unnamed_union_and_of_an_unnamed_struct_inside_it() {
    use cpp_code_analysis::file::coordinates::FileOffset;
    const SOURCE: &str = "\
enum class Kind { None, Start };
struct MarkEvent {
    int Type;
    union {
        struct { unsigned long Parent; Kind KindOf; } Start;
        struct { unsigned long Index; Kind Token; } Token;
    } U;
};
void f(MarkEvent &event) {
    event.U.Start.KindOf = Kind::None;
    unsigned long p = event.U.Start.Parent;
}
";
    let session = session_with(&[("/p/a.cpp", SOURCE)]);
    let view = session.view("/p/a.cpp").expect("cooked");
    let answered = |needle: &str| {
        let at = SOURCE.find(needle).expect("the fixture writes it") + needle.len() - 1;
        let reading = view
            .reading_offset_of(FileOffset::new(at))
            .expect("the offset is in the file");
        match session.type_at(&view, reading) {
            cpp_code_analysis::Known::Yes(found) => Some(found.type_of),
            cpp_code_analysis::Known::No => None,
            cpp_code_analysis::Known::Unknown(reason) => {
                println!("`{needle}` → unknown({reason:?})");
                None
            }
        }
    };

    assert_eq!(
        answered("event.U").as_deref(),
        Some("MarkEvent::U"),
        "the union is named after the scope it is written in"
    );
    assert_eq!(
        answered("event.U.Start").as_deref(),
        Some("MarkEvent::U::Start"),
        "and so is the unnamed struct inside it"
    );
    assert_eq!(
        answered("event.U.Start.KindOf").as_deref(),
        Some("Kind"),
        "a member of the unnamed struct is a member like any other"
    );
    assert_eq!(
        answered("event.U.Start.Parent").as_deref(),
        Some("unsigned long")
    );
}

/// **The standard library's allocator chain** — `std::vector<MarkEvent>::at()` is `MarkEvent&`, and `v[i]` too.
///
/// libstdc++ writes the element access type as an alias of an alias of an alias, and every level of it is a name that
/// only one class can resolve:
///
/// ```text
/// vector<T,A>::reference          _Alloc_traits::reference
///   _Alloc_traits                 __gnu_cxx::__alloc_traits<_Tp_alloc_type>     `_Tp_alloc_type` is vector's own
///     __alloc_traits::reference   value_type&
///       value_type                _Base_type::value_type
///         _Base_type              std::allocator_traits<_Tp_alloc_type>
///           value_type            _Alloc::value_type
///             _Alloc              std::allocator<T>            ← through `rebind<T>::other`
/// ```
///
/// The fixture below is that shape with the names changed (no standard library, 0.02 s): a nested class template
/// `rebind<U>::other`, an alias template `rebind_alloc`, an inheritance, and a class that re-exports its base's alias.
///
/// Reported from `EmmyLuaCodeStyle`'s `Mark.cpp`: `auto &event = p.GetEvents().at(Start);` answered
/// `__gnu_cxx::__alloc_traits<std::allocator<MarkEvent>>::template rebind<MarkEvent>::other&`, and `events[Pos]`
/// answered `_Tp_alloc_type&` — a spelling no reader can use, and every member behind it unknown.
#[test]
fn a_containers_element_access_through_the_allocator_chain() {
    use cpp_code_analysis::file::coordinates::FileOffset;
    const SOURCE: &str = "\
namespace mine {
template <typename Tp>
class allocator {
public:
    typedef Tp value_type;
    template <typename Tp1>
    struct rebind { typedef allocator<Tp1> other; };
};
template <typename Alloc>
struct allocator_traits {
    typedef typename Alloc::value_type value_type;
    template <typename Up>
    using rebind_alloc = typename Alloc::template rebind<Up>::other;
};
}
namespace mygnu {
template <typename Alloc>
struct alloc_traits : mine::allocator_traits<Alloc> {
    typedef mine::allocator_traits<Alloc> base_type;
    typedef typename base_type::value_type value_type;
    typedef value_type& reference;
    template <typename Tp>
    struct rebind { typedef typename base_type::template rebind_alloc<Tp> other; };
};
template <typename Tp, typename Alloc>
struct vector_base {
    typedef typename mygnu::alloc_traits<Alloc>::template rebind<Tp>::other tp_alloc_type;
};
template <typename Tp, typename Alloc>
class vector : protected vector_base<Tp, Alloc> {
    typedef vector_base<Tp, Alloc> base;
    typedef typename base::tp_alloc_type tp_alloc_type;
    typedef mygnu::alloc_traits<tp_alloc_type> alloc_traits_type;
public:
    typedef typename alloc_traits_type::reference reference;
    reference at(int i);
    reference operator[](int i);
};
}
void f() {
    mygnu::vector<int, mine::allocator<int>> v;
    int x = v[0];
    int y = v.at(1);
    (void)x;
    (void)y;
}
";
    let session = session_with(&[("/p/a.cpp", SOURCE)]);
    let view = session.view("/p/a.cpp").expect("cooked");
    let answered = |needle: &str| {
        let at = SOURCE.find(needle).expect("the fixture writes it") + 2;
        let reading = view
            .reading_offset_of(FileOffset::new(at))
            .expect("the offset is in the file");
        match session.type_at(&view, reading) {
            cpp_code_analysis::Known::Yes(found) => Some(found.type_of),
            cpp_code_analysis::Known::No => None,
            cpp_code_analysis::Known::Unknown(reason) => {
                println!("`{needle}` → unknown({reason:?})");
                None
            }
        }
    };

    assert_eq!(
        answered("v[0]").as_deref(),
        Some("int"),
        "a subscript on `vector<int, allocator<int>>` is `operator[]`'s `reference`, and `reference` is `int&` — \
         answered here as its decayed value, which is what a cursor on `v[0]` is asking about"
    );
    assert_eq!(
        answered("v.at(1)").as_deref(),
        Some("int&"),
        "and a call of `at` is the same alias chain, answered as the reference it is"
    );
}

/// **从 `std::map` 的范围里解构出来的名字,类型是 `pair` 的元素** —— 报告是 hover 显示 `int() value;`。
///
/// `for (auto [key, value] : map)` 里的名字没有写出来的类型,所以"声明的类型"这一层没有答案:元素的类型由
/// **范围**决定,`map<K, V>` 的元素是 `pair<const K, V>`,于是 `key` 是 `const K`、`value` 是 `V`。
#[test]
fn a_structured_binding_in_a_range_for_has_the_paired_type() {
    let source = "\
#include <map>
#include <string>
int f() {
    std::map<int, std::string> myMap;
    for (auto [key, value] : myMap) {
        value.size();
        int k = key;
        (void)k;
    }
    return 0;
}
";
    let header = "namespace std { template <class K, class V> struct map { }; template <class C> struct basic_string { }; }\n";
    let files = MemoryFiles::new()
        .with_file("/p/map", header)
        .with_file("/p/string", header)
        .with_file("/p/a.cpp", source);
    let providers = SessionFiles::new(OpenDocuments::new(), files);
    let mut session = Session::with_config(
        "/p",
        providers,
        WatchFilter::new("/p"),
        CompilerConfig::default(),
    );
    session.add_project_files([std::path::PathBuf::from("/p/a.cpp")]);
    session.want_cooked_reading(std::path::Path::new("/p/a.cpp"));
    session.index_everything();

    let view = session.view("/p/a.cpp").expect("a view");
    let type_of = |needle: &str| {
        let at = source.find(needle).expect("the fixture writes it")
            + needle
                .len()
                .saturating_sub(needle.trim_start_matches(['=', ' ', '(', ')']).len());
        let reading = view
            .reading_offset_of(cpp_code_analysis::file::coordinates::FileOffset::new(at))
            .expect("the cursor is in the rendering");
        match session.type_at(&view, reading) {
            cpp_code_analysis::Known::Yes(found) => found.type_of,
            other => panic!("`{needle}` has a type: {other:?}"),
        }
    };

    assert_eq!(
        type_of("value.size"),
        "std::string",
        "the second component of `map<int, std::string>`"
    );
    assert_eq!(
        type_of("= key"),
        "const int",
        "and the key is the first, `const` as a map's key is"
    );
}

/// **`for (auto x : v)` 里 `x` 的类型是元素的类型** —— 这个声明没有初始化器,所以"由初始化器推导"那条路没有答案。
///
/// 两张报告图是同一个缺陷:`x` / `it` 的 hover 只有 "a variable, declared in a function body",没有类型。
#[test]
fn a_range_for_variable_has_the_element_type() {
    let source = "\
#include <vector>
#include <map>
#include <string>
int f() {
    std::vector<int> values;
    for (auto x : values) {
        int k1 = x;
        (void)k1;
    }
    std::map<int, std::string> myMap;
    for (auto it : myMap) {
        int k2 = it;
        (void)k2;
    }
    return 0;
}
";
    let vector = "namespace std { template <class T> struct vector { }; }\n";
    let map = "namespace std { template <class K, class V> struct map { }; template <class A, class B> struct pair { }; }\n";
    let string = "namespace std { template <class C> struct basic_string { }; typedef basic_string<char> string; }\n";
    let files = MemoryFiles::new()
        .with_file("/p/vector", vector)
        .with_file("/p/map", map)
        .with_file("/p/string", string)
        .with_file("/p/a.cpp", source);
    let providers = SessionFiles::new(OpenDocuments::new(), files);
    let mut session = Session::with_config(
        "/p",
        providers,
        WatchFilter::new("/p"),
        CompilerConfig::default(),
    );
    session.add_project_files([std::path::PathBuf::from("/p/a.cpp")]);
    session.want_cooked_reading(std::path::Path::new("/p/a.cpp"));
    session.index_everything();

    let view = session.view("/p/a.cpp").expect("a view");
    let type_of = |needle: &str| {
        let at = source.find(needle).expect("the fixture writes it") + needle.len();
        let reading = view
            .reading_offset_of(cpp_code_analysis::file::coordinates::FileOffset::new(at))
            .expect("the cursor is in the rendering");
        match session.type_at(&view, reading) {
            cpp_code_analysis::Known::Yes(found) => found.type_of,
            other => panic!("`{needle}` has a type: {other:?}"),
        }
    };

    assert_eq!(type_of("k1 = "), "int", "the element of a `vector<int>`");
    let pair = type_of("k2 = ");
    assert!(
        pair.contains("pair<const int, std::string>"),
        "the element of a `map<int, std::string>` is that pair: {pair}"
    );
}

/// **`auto it = v.begin();` 的 `it` 是元素的视图** —— 迭代器类型是这一层无法实例化的类模板,
/// 而读者接下来写的是 `it->first`、`it.second`、`*it`,这三件都是关于**元素**的问题。
#[test]
fn an_iterator_call_has_the_element_type() {
    let source = "\
#include <map>
#include <string>
int f() {
    std::map<int, std::string> myMap;
    auto it = myMap.begin();
    int k = it;
    (void)k;
    return 0;
}
";
    let map = "namespace std { template <class K, class V> struct map { }; template <class A, class B> struct pair { }; }\n";
    let string = "namespace std { template <class C> struct basic_string { }; typedef basic_string<char> string; }\n";
    let files = MemoryFiles::new()
        .with_file("/p/map", map)
        .with_file("/p/string", string)
        .with_file("/p/a.cpp", source);
    let providers = SessionFiles::new(OpenDocuments::new(), files);
    let mut session = Session::with_config(
        "/p",
        providers,
        WatchFilter::new("/p"),
        CompilerConfig::default(),
    );
    session.add_project_files([std::path::PathBuf::from("/p/a.cpp")]);
    session.want_cooked_reading(std::path::Path::new("/p/a.cpp"));
    session.index_everything();

    let view = session.view("/p/a.cpp").expect("a view");
    let at = source.find("k = it").expect("the fixture") + "k = ".len();
    let reading = view
        .reading_offset_of(cpp_code_analysis::file::coordinates::FileOffset::new(at))
        .expect("the cursor is in the rendering");
    match session.type_at(&view, reading) {
        cpp_code_analysis::Known::Yes(found) => {
            println!("it: {}", found.type_of);
            assert!(
                found.type_of.contains("pair<const int, std::string>"),
                "`begin()` yields the map's element: {}",
                found.type_of
            );
        }
        other => panic!("`it` has a type: {other:?}"),
    }
}

/// **`*it` 就是元素本身** —— 迭代器被当作元素的视图(`begin()` 的类型就是元素),所以 `*it` 问的正是那个东西。
#[test]
fn dereferencing_an_iterator_yields_the_element() {
    let source = "\
#include \"map.h\"
#include \"string.h\"
int f() {
    std::map<int, std::string> myMap;
    auto it = myMap.begin();
    auto element = *it;
    int k = element;
    (void)k;
    return 0;
}
";
    let map = "namespace std { template <class K, class V> struct map { }; template <class A, class B> struct pair { A first; B second; }; }\n";
    let string = "namespace std { template <class C> struct basic_string { }; typedef basic_string<char> string; }\n";
    let files = MemoryFiles::new()
        .with_file("/p/map.h", map)
        .with_file("/p/string.h", string)
        .with_file("/p/a.cpp", source);
    let providers = SessionFiles::new(OpenDocuments::new(), files);
    let mut session = Session::with_config(
        "/p",
        providers,
        WatchFilter::new("/p"),
        CompilerConfig::default(),
    );
    session.add_project_files([
        std::path::PathBuf::from("/p/map.h"),
        std::path::PathBuf::from("/p/string.h"),
        std::path::PathBuf::from("/p/a.cpp"),
    ]);
    session.want_cooked_reading(std::path::Path::new("/p/a.cpp"));
    session.index_everything();

    let view = session.view("/p/a.cpp").expect("a view");
    let at = source.find("k = element").expect("the fixture") + "k = ".len();
    let reading = view
        .reading_offset_of(cpp_code_analysis::file::coordinates::FileOffset::new(at))
        .expect("the cursor is in the rendering");
    match session.type_at(&view, reading) {
        cpp_code_analysis::Known::Yes(found) => {
            println!("element: {}", found.type_of);
            assert!(
                found.type_of.contains("pair<const int, std::string>"),
                "`*it` is the element: {}",
                found.type_of
            );
        }
        other => panic!("`element` has a type: {other:?}"),
    }
}

/// **类自己定义的 operator** —— `operator+`、`operator*`、`operator[]`、`operator()` 的返回类型就是表达式类型。
///
/// 还差最后一步:operator 成员目前**不在** `members_of` 的名单里(事实层没有给它建名字),所以查找永远返回 None,
/// 表达式落回"操作数类型相同"或迭代器模型。规则和接线都已完成,缺的是事实层的名字 —— 见下一节的记录。
#[test]
fn a_class_own_operators_have_their_declared_return_type() {
    let source = "\
struct Money {
    int cents;
    Money operator+(const Money& other) const;
    int operator*() const;
    int operator[](int at) const;
    int operator()(int times) const;
    Money operator-() const;
};
int f() {
    Money a;
    Money sum = a + a;
    int deref = *a;
    int at = a[0];
    int times = a(3);
    Money neg = -a;
    (void)sum; (void)deref; (void)at; (void)times; (void)neg;
    return 0;
}
";
    let files = MemoryFiles::new().with_file("/p/a.cpp", source);
    let providers = SessionFiles::new(OpenDocuments::new(), files);
    let mut session = Session::with_config(
        "/p",
        providers,
        WatchFilter::new("/p"),
        CompilerConfig::default(),
    );
    session.add_project_files([std::path::PathBuf::from("/p/a.cpp")]);
    session.want_cooked_reading(std::path::Path::new("/p/a.cpp"));
    session.index_everything();

    let view = session.view("/p/a.cpp").expect("a view");
    let type_of = |needle: &str| {
        let at = source.find(needle).expect("the fixture writes it") + needle.len();
        let reading = view
            .reading_offset_of(cpp_code_analysis::file::coordinates::FileOffset::new(at))
            .expect("the cursor is in the rendering");
        match session.type_at(&view, reading) {
            cpp_code_analysis::Known::Yes(found) => found.type_of,
            other => panic!("`{needle}` has a type: {other:?}"),
        }
    };

    match session.members_of(&view, "Money") {
        cpp_code_analysis::Known::Yes(list) => println!(
            "members of Money: {:?}",
            list.members
                .iter()
                .map(|member| (member.fact.name.to_string(), member.fact.returns.clone()))
                .collect::<Vec<_>>()
        ),
        other => println!("members of Money: {other:?}"),
    }
    assert_eq!(type_of("Money sum = "), "Money", "operator+ returns Money");
    assert_eq!(type_of("int deref = "), "int", "operator* returns int");
    // **The cursor goes past the base name**, onto the `[`: `type_at` answers the node an offset sits in, and an
    // offset on `a` is the *name* — whose own type is `Money`, correctly. The subscript is the expression whose type
    // is the operator's return type.
    assert_eq!(type_of("int at = a"), "int", "operator[] returns int");
    // Same rule as the subscript — one character later, onto the call's own `(`.
    assert_eq!(type_of("int times = a"), "int", "operator() returns int");
    assert_eq!(
        type_of("Money neg = "),
        "Money",
        "unary operator- returns Money"
    );
}

/// **`typedef struct { … } Name;`** — C's ordinary way of naming a struct, and the shape whose members were missing.
///
/// Three questions, and all three are the same one asked of a different spelling:
///
/// 1. an **unnamed** body: the typedef's name is the only name the type has, so the class is filed under `Object`
///    and `members_of("Object")` has to answer `a` and `b`;
/// 2. a **tagged** body behind an alias: the members are filed under the *tag*, so `members_of("Alias")` has to
///    follow the alias to `Tag` instead of stopping at the definition's text;
/// 3. a plain `struct Plain { … } plain;` — a member access on `plain` needs the *variable's* type, and that type is
///    the tag.
///
/// Measured before the fix, on a four-line fixture: the typedef's fact carried
/// `type_of: Some("struct { int a; int b; }")`, and `members_of("Object")` answered an **empty** list with
/// `unlisted ["{ int a; int b; }"]` — nothing declares a body's text as a class — while its two members sat under
/// the scope `Object`. On neovim's checkout "a member list built from no visible declaration" was **21 539 of
/// 40 000** refusals.
#[test]
fn a_typedef_of_a_class_body_names_its_members() {
    let source = "\
typedef struct { int a; int b; } Object;
typedef struct Tag { int d; } Alias;
struct Plain { int e; } plain;
int f(void) { Object o; return o.a; }
int g(void) { return plain.d; }
";
    let session = session_with(&[("/p/a.cpp", source)]);
    let view = session.view("/p/a.cpp").expect("the file is held");

    let members = |class: &str| match session.members_of(&view, class) {
        cpp_code_analysis::Known::Yes(list) => {
            assert!(
                list.unlisted.is_empty(),
                "`{class}` is fully declared here: {:?}",
                list.unlisted
            );
            list.members
                .iter()
                .map(|member| member.fact.name.to_string())
                .collect::<Vec<_>>()
        }
        other => panic!("`{class}` has a member list: {other:?}"),
    };

    assert_eq!(
        members("Object"),
        ["a", "b"],
        "the unnamed body is `Object`"
    );
    assert_eq!(members("Alias"), ["d"], "the alias follows to its tag");
    assert_eq!(members("Tag"), ["d"], "and the tag answers on its own");
    assert_eq!(members("Plain"), ["e"], "a named body with a variable");

    // **And the answers are reachable through the code**, which is what a member list is for: the receiver's type
    // comes from the declaration, and the member's from the list.
    assert_eq!(
        type_of_use(source, "plain").as_deref(),
        Some("Plain"),
        "a named body types its variable as the tag"
    );
    assert_eq!(
        type_of_use(source, "o").as_deref(),
        Some("Object"),
        "an unnamed body behind a typedef types the variable as the typedef"
    );
    assert_eq!(
        type_of_use(source, "a").as_deref(),
        Some("int"),
        "and `o.a` is the member the body declares"
    );
}

/// **A real C header's shape** — a tag declared as a typedef first and defined later, behind a function-pointer
/// typedef that used to swallow the rest of the file.
///
/// neovim's `src/nvim/ex_cmds_defs.h` is written exactly like this:
///
/// ```c
/// typedef char *(*LineGetter)(int, void *, int, bool);   // :96
/// typedef struct exarg exarg_T;                          // :86
/// struct exarg { char *arg; … int addr_count; … };        // :108
/// ```
///
/// Three separate defects sat on those four lines, and each one was silent:
///
/// * the function-pointer typedef **never ended** — its `Declarator` ran to the end of the header, so `struct exarg`
///   and everything after line 96 produced no facts at all (16 facts for a 800-line header instead of 93);
/// * the alias's target was recorded as the *elaborated* spelling `struct exarg`, which no scope has, so
///   `members_of("exarg_T")` answered nothing even once the members existed;
/// * an anonymous struct that is a **member** of another class borrowed the next typedef's name, so
///   `struct { bool file; bool bar; } magic;` was filed under `exarg::SubReplacementString`.
///
/// The facts are asserted through the queries that use them, and `members("exarg_T") = members("exarg")` is the
/// whole point: the alias has to reach the tag's members rather than stop at the spelling the file wrote.
#[test]
fn a_struct_defined_after_its_typedef_answers_for_the_alias() {
    let source = "\
typedef char *(*LineGetter)(int, void *, int, bool);
typedef struct exarg exarg_T;
struct exarg { char *arg; int addr_count; struct { bool file; bool bar; } magic; };
struct exarg raw;
int f(void) { exarg_T eap; return eap.addr_count; }
";
    let session = session_with(&[("/p/a.cpp", source)]);
    let view = session.view("/p/a.cpp").expect("the file is held");

    let members = |class: &str| match session.members_of(&view, class) {
        cpp_code_analysis::Known::Yes(list) => list
            .members
            .iter()
            .map(|member| member.fact.name.to_string())
            .collect::<Vec<_>>(),
        other => panic!("`{class}` has a member list: {other:?}"),
    };

    let tag = ["addr_count", "arg", "magic"];
    assert_eq!(members("exarg"), tag, "the tag's own members");
    assert_eq!(members("exarg_T"), tag, "and the alias reaches them");

    assert_eq!(
        type_of_use(source, "eap").as_deref(),
        Some("exarg_T"),
        "a variable declared with the alias keeps the alias"
    );
    assert_eq!(
        type_of_use(source, "addr_count").as_deref(),
        Some("int"),
        "and its member access is typed through the tag's body"
    );
    // **The elaborated keyword is not part of the type.** `struct exarg raw;` used to record `struct` — the keyword
    // alone — because the grammar reads `struct exarg` as a `BuiltinType` plus a `NamedType`, and the builtin won.
    assert_eq!(
        type_of_use(source, "raw").as_deref(),
        Some("exarg"),
        "`struct exarg` spells the type `exarg`"
    );
}

/// **An anonymous union declared in a *header*, asked about from a file that includes it** — the corpus's shape.
///
/// `struct line` is in `corpus.h` with `union { long integer; double real; };` inside it, and the use is
/// `line.integer` in `types.c`. The one-file fixture passes; this is the two-file one, where the members come from the
/// header's facts rather than from the queried file's scope tree.
#[test]
fn an_anonymous_unions_members_are_facts_of_the_struct_it_is_declared_in() {
    let header = "\
struct line {
    char colour;
    union {
        long integer;
        double real;
    }; /* anonymous */
};
";
    let source = "#include \"corpus.h\"\nint f(struct line *line) { return (int)line->integer; }\n";
    let session = session_with(&[("/p/corpus.h", header), ("/p/a.c", source)]);
    let view = session.view("/p/a.c").expect("the file is held");

    let members = match session.members_of(&view, "line") {
        cpp_code_analysis::Known::Yes(list) => list
            .members
            .iter()
            .map(|member| member.fact.name.to_string())
            .collect::<Vec<_>>(),
        other => panic!("`line` has a member list: {other:?}"),
    };
    assert!(
        members.iter().any(|name| name == "integer"),
        "the union is anonymous, so `integer` is `line`'s member: {members:?}"
    );
}

/// **A variable whose type is an unnamed struct declared inline**, and its members.
///
/// ```c
/// struct { unsigned three : 3; } bits = {5};
/// return (int)bits.three;
/// ```
///
/// The type has no spelling, so the only key the members can have is the declarator's name — measured on a nine-file C
/// corpus, `bits.three` and `bits.signed_wide` (and `anonymous_union.integer`) answered nothing.
#[test]
fn an_unnamed_struct_type_is_keyed_by_the_variable_it_declares() {
    let source = "int f(void) { struct { unsigned three : 3; unsigned four : 3; } bits = {5, 1}; return (int)bits.three; }\n";
    let session = session_with(&[("/p/a.c", source)]);
    let view = session.view("/p/a.c").expect("the file is held");

    // **The *query* is what this test asserts, and the fact deliberately stays empty.** A file-scope
    // `static const struct { … } priority[]` is hovered as the declaration's own text, and naming that type made the
    // hover read `priority[] priority;` — so the key exists where a member question needs it (the query path, for a
    // local) and nowhere a reader reads a type.
    assert_eq!(
        type_of_use(source, "bits").as_deref(),
        Some("bits"),
        "the query path keys the type by the variable's own name"
    );
    let members = match session.members_of(&view, "bits") {
        cpp_code_analysis::Known::Yes(list) => list
            .members
            .iter()
            .map(|member| member.fact.name.to_string())
            .collect::<Vec<_>>(),
        other => panic!("`bits` has a member list: {other:?}"),
    };
    assert!(
        members.iter().any(|name| name == "three"),
        "and its members are filed under that name: {members:?}"
    );
}

/// **`__auto_type` is GNU's `auto`**, and its type is its initialiser's.
///
/// Measured on a nine-file C corpus: `__auto_type value = 41;` had a *fact* typed `int` (the fact builder deducts) while
/// `type_at(value)` answered `__auto_type` — the query path reads the syntax and the placeholder check did not know the
/// GNU spelling.
#[test]
fn gnu_auto_type_is_deduced_from_its_initializer() {
    let source = "int f(void) { __auto_type value = 41; return value; }\n";
    assert_eq!(
        type_of_use(source, "value").as_deref(),
        Some("int"),
        "the placeholder's type is the initialiser's"
    );
}
/// **A variable whose type is a function-pointer typedef** — `binary_op operation = add;`.
///
/// `binary_op` is `typedef int (*binary_op)(int, int);`, and the declaration's type is that alias's name — the
/// spelling the file wrote. Measured on a nine-file C corpus: `operation` and `compare` answered `<no type>`, so
/// `operation(1, 2)` could not be resolved as a call of a known signature.
#[test]
fn a_function_pointer_typedef_names_the_variable_it_types() {
    let source = "\
typedef int (*binary_op)(int left, int right);
static int add(int left, int right) { return left + right; }
int f(void) { binary_op operation = add; return operation(1, 2); }
";
    assert_eq!(
        type_of_use(source, "operation").as_deref(),
        Some("binary_op"),
        "the alias's name is the type the file wrote"
    );
}

/// **A function-pointer typedef declared in a *header*** — the corpus's shape, and the one the one-file fixture misses.
///
/// `corpus.h` writes `typedef int (*binary_op)(int left, int right);` and `functions.c` uses it as
/// `binary_op operation = add;`. Measured on a nine-file C corpus: `operation` and `compare` answered `<no type>`,
/// while every other local in every other file answered. The one-file fixture in `types.rs` passes, so what is being
/// measured here is the **cross-file** spelling: a binding whose type names a typedef the file did not write.
#[test]
fn a_function_pointer_typedef_from_a_header_types_its_variable() {
    let header = "\
typedef int (*binary_op)(int left, int right);
typedef int comparer(const void *left, const void *right);
";
    let source = "\
#include \"corpus.h\"
#include <stddef.h>
#include <stdio.h>
static int add(int left, int right) { return left + right; }
int f(void) { binary_op operation = add; comparer *compare = 0; return operation(1, 2) + (compare != 0); }
";
    let session = session_with(&[("/p/corpus.h", header), ("/p/a.c", source)]);
    let view = session.view("/p/a.c").expect("the file is held");

    let type_at = |needle: &str| {
        let offset = source.find(needle).expect("the needle is written") + 1;
        let reading = view
            .reading_offset_of(cpp_code_analysis::file::coordinates::FileOffset::new(
                offset,
            ))
            .expect("the cursor is in the rendering");
        match session.type_at(&view, reading) {
            cpp_code_analysis::Known::Yes(found) => Some(found.type_of),
            _ => None,
        }
    };

    // **The engine expands the alias**, and that is the better answer — `int (*)(int, int)` is the type, while
    // `binary_op` is its name. This fixture therefore pins the *expansion*; what the corpus answers for the same shape
    // (nothing at all) is still open.
    let operation = type_at("binary_op operation").expect("the variable has a type");
    assert!(
        operation.contains("int") && operation.contains("(int, int)"),
        "a function-pointer typedef expands to the type it names: {operation}"
    );
    let compare = type_at("comparer *compare").expect("the pointer has a type");
    assert!(
        compare.contains("int") && compare.contains("void"),
        "and so does the other typedef: {compare}"
    );
}

/// **A member whose specifier starts with `_BitInt(N)` is not recorded**, and one that starts with `unsigned` is.
///
/// `struct bits { unsigned _BitInt(3) three : 3; _BitInt(13) signed_wide : 13; };` — the corpus's own shape, and the
/// measurement is the *summary*, because the facts are what a member list is built from.
#[test]
fn both_bitfields_of_a_bitint_struct_are_recorded() {
    let source = "\
struct bits {
    unsigned _BitInt(3) three : 3;
    _BitInt(13) signed_wide : 13;
    _BitInt(5) plain;
    unsigned _BitInt(4) other : 2;
};
";
    let session = session_with(&[("/p/a.c", source)]);
    let summary = session
        .parsed_summary(std::path::Path::new("/p/a.c"))
        .expect("the file has a summary");
    let names: Vec<&str> = summary
        .declarations
        .iter()
        .map(|fact| fact.name.as_str())
        .collect();
    eprintln!("facts: {names:?}");
    assert!(
        names.contains(&"three") && names.contains(&"signed_wide") && names.contains(&"plain"),
        "both bit-fields and the plain member are members: {names:?}"
    );
}

/// **`__STDC_VERSION__` is a macro every C compiler defines**, and its value follows from the standard the file is read
/// as — not from a toolchain that may not exist.
#[test]
fn the_standards_own_macros_answer_from_the_language() {
    let session = session_with(&[("/p/a.c", "int f(void) { return 0; }\n")]);
    assert_eq!(
        session.predefined_macro("__STDC_VERSION__", std::path::Path::new("/p/a.c")),
        Some("202311L".to_string()),
        "a permissive C reading takes the latest standard"
    );
    assert_eq!(
        session.predefined_macro("__STDC__", std::path::Path::new("/p/a.c")),
        Some("1".to_string()),
    );
    assert_eq!(
        session.predefined_macro("__GNUC__", std::path::Path::new("/p/a.c")),
        None,
        "a compiler's own macro is not the standard's, and no compiler was asked here"
    );
}

/// **A class template's members, through an instantiation** — `Box<int> box; box.get()`.
///
/// The template's body is ordinary C++: `get` and `set` are members of `Box`, and an instantiation has them. Measured on
/// a C++20 project: `techniques::Box<int> box(7); box.` answered **0 items** — not a wrong list, an empty one — which is
/// what a reader sees as "completion does not work on my template class".
#[test]
fn a_class_templates_members_answer_through_its_instantiation() {
    let source = "\
template <typename T>
class Box {
public:
    explicit Box(T value) : value_(value) {}

    T get() const { return value_; }
    void set(T value) { value_ = value; }

private:
    T value_;
};

int f() {
    Box<int> box(7);
    return box.get();
}
";
    let session = session_with(&[("/p/a.cpp", source)]);
    let view = session.view("/p/a.cpp").expect("the file is held");

    let names = |class: &str| match session.members_of(&view, class) {
        cpp_code_analysis::Known::Yes(list) => list
            .members
            .iter()
            .map(|member| member.fact.name.to_string())
            .collect::<Vec<_>>(),
        other => panic!("`{class}` has a member list: {other:?}"),
    };

    assert!(
        names("Box<int>").contains(&"get".to_string()),
        "the instantiation has the template's members: {:?}",
        names("Box<int>")
    );
    assert!(
        names("Box").contains(&"get".to_string()),
        "and so does the template's own name: {:?}",
        names("Box")
    );
}

/// **Class template argument deduction** — `Box box(7);` where `Box` is a class template.
///
/// The implicit guide is the constructor's parameter list: `Box(T value)` makes `Box box(7)` a `Box<int>`, and that is
/// ordinary C++17. Measured on a real C++ project (`techniques.h` + `techniques.cpp`): the declaration's type came back
/// as the **template's name without its argument** — `techniques::Box` — while `Box<int> box(7)` was right, so the
/// deduction is what is missing rather than the template.
#[test]
fn a_class_template_argument_is_deduced_from_the_initializer() {
    let source = "\
template <typename T>
class Box {
public:
    explicit Box(T value) : value_(value) {}

    T get() const { return value_; }

private:
    T value_;
};

int f() {
    Box box(7);
    return box.get();
}
";
    let session = session_with(&[("/p/a.cpp", source)]);
    let view = session.view("/p/a.cpp").expect("the file is held");
    let offset = last_word(source, "box") + 2;
    let found = match session.type_at(&view, as_reading(&view, offset)) {
        cpp_code_analysis::Known::Yes(found) => found.type_of,
        other => panic!("`box` has a type: {other:?}"),
    };
    assert!(
        found.contains("Box<int>"),
        "the argument is deduced from the initializer: {found}"
    );

    // **And the members follow the deduced type**, which is the question a reader actually asks of the name.
    let names = match session.members_of(&view, "Box<int>") {
        cpp_code_analysis::Known::Yes(list) => list
            .members
            .iter()
            .map(|member| member.fact.name.to_string())
            .collect::<Vec<_>>(),
        other => panic!("`Box<int>` has a member list: {other:?}"),
    };
    assert!(
        names.contains(&"get".to_string()),
        "the deduced instantiation has the template's members: {names:?}"
    );
}

/// The same deduction through a **namespace**, which is how a header's template is reached: `techniques::Box box(7)`.
#[test]
fn a_class_template_argument_is_deduced_through_a_namespace() {
    let source = "\
namespace techniques {
template <typename T>
class Box {
public:
    explicit Box(T value) : value_(value) {}
    T get() const { return value_; }
private:
    T value_;
};
}  // namespace techniques

int f() {
    techniques::Box box(7);
    return box.get();
}
";
    let session = session_with(&[("/p/a.cpp", source)]);
    let view = session.view("/p/a.cpp").expect("the file is held");
    let offset = last_word(source, "box") + 2;
    let found = match session.type_at(&view, as_reading(&view, offset)) {
        cpp_code_analysis::Known::Yes(found) => found.type_of,
        other => panic!("`box` has a type: {other:?}"),
    };
    assert!(
        found.contains("Box<int>"),
        "the argument is deduced: {found}"
    );
}

/// The same deduction when the class is in a **header** — the shape the project actually has, and the one that still
/// answers the bare template name there.
#[test]
fn a_class_template_argument_is_deduced_across_a_header() {
    let header = "\
#pragma once
namespace techniques {
template <typename T>
class Box {
public:
    explicit Box(T value) : value_(value) {}
    T get() const { return value_; }
private:
    T value_;
};
}  // namespace techniques
";
    let source = "\
#include \"techniques.h\"

int f() {
    techniques::Box box(7);
    return box.get();
}
";
    let session = session_with(&[("/p/techniques.h", header), ("/p/a.cpp", source)]);
    if let Some(summary) = session.parsed_summary(std::path::Path::new("/p/techniques.h")) {
        for fact in &summary.declarations {
            eprintln!(
                "[fact] name={:?} scope={:?} kind={:?} params={:?}",
                fact.name,
                fact.scope,
                fact.kind,
                fact.parameter_list()
            );
        }
    }
    let view = session.view("/p/a.cpp").expect("the file is held");
    let offset = last_word(source, "box") + 2;
    let found = match session.type_at(&view, as_reading(&view, offset)) {
        cpp_code_analysis::Known::Yes(found) => found.type_of,
        other => panic!("`box` has a type: {other:?}"),
    };
    assert!(
        found.contains("Box<int>"),
        "the argument is deduced: {found}"
    );
}

/// **A hand-written deduction guide**, which is the language's own spelling of the rule — and the only way to deduce a
/// class whose constructor does not take the template parameter.
///
/// ```cpp
/// template <typename T> struct Wrap { Wrap(std::string); };
/// template <typename T> Wrap(T) -> Wrap<T>;
/// Wrap w(7);        // Wrap<int>, and nothing about Wrap's constructor says so
/// ```
///
/// Measured: the guide is read by the grammar (the file parses) and was not read by the deduction, so `w` answered the
/// bare template name.
#[test]
fn a_hand_written_deduction_guide_is_read() {
    let source = "\
#include <string>

template <typename T>
struct Wrap {
    Wrap(std::string);
    T get() const;
};

template <typename T>
Wrap(T) -> Wrap<T>;

int f() {
    Wrap wrapped(7);
    return wrapped.get();
}
";
    let session = session_with(&[("/p/a.cpp", source)]);
    let view = session.view("/p/a.cpp").expect("the file is held");
    let offset = last_word(source, "wrapped") + 2;
    let found = match session.type_at(&view, as_reading(&view, offset)) {
        cpp_code_analysis::Known::Yes(found) => found.type_of,
        other => panic!("`w` has a type: {other:?}"),
    };
    assert!(found.contains("Wrap<int>"), "the guide deduces it: {found}");
}
/// **Which overload `describe(1.5)` calls**, asked the way go-to-definition asks it.
///
/// Two declarations differ only in their `enable_if` condition, so the *only* thing that picks between them is whether
/// `std::is_floating_point<double>::value` is true — and `sema::constraint` decides exactly that
/// (`the_constraint_evaluator_decides_the_fundamental_traits`). What is missing is the **set** to decide it on.
///
/// Measured: the answer is the first declaration (the `is_integral` one) for either call, because two declarations of one
/// name in one scope reach the answer as **one** candidate. The chain is:
///
/// ```text
/// the scope tree     binds template functions … one binding for both
/// definitions_at     "every declaration in the scope that answered" — a set of one
/// the index          holds both facts, and folds them into one (`one_entity_declared_twice`, which is about a header's
///                    declaration and the `.cpp`'s definition, not about two same-file overloads)
/// ```
///
/// Ignored rather than deleted: the assertion is the goal, and the day an overload set exists this is the test that says
/// so.
#[test]
fn an_enable_if_overload_is_chosen_by_its_condition() {
    let source = "\
#include <type_traits>
#include <string>

template <typename T>
typename std::enable_if<std::is_integral<T>::value, std::string>::type describe(T value) {
    return \"integral\";
}

template <typename T>
typename std::enable_if<std::is_floating_point<T>::value, std::string>::type describe(T value) {
    return \"floating\";
}

std::string f() {
    describe(41);
    return describe(1.5);
}
";
    let session = session_with(&[("/p/a.cpp", source)]);
    let view = session.view("/p/a.cpp").expect("the file is held");
    // **The rendering's text**, because a fact's range is a range there — the include closure is inlined into it.
    let rendering = view.root.text().to_string();
    let declaration_at = |which: usize| -> usize {
        rendering
            .match_indices("describe(T value)")
            .nth(which)
            .map(|(at, _)| at)
            .expect("both declarations are in the rendering")
    };

    // **`describe(1.5)` picks the floating-point overload**: its condition is `is_floating_point<T>` with `T = double`.
    let floating_call = rendering
        .find("describe(1.5)")
        .expect("the call is written");
    let one = match session.definition(
        &view,
        cpp_code_analysis::file::coordinates::ReadingOffset::new(floating_call + 3),
    ) {
        cpp_code_analysis::Known::Yes(found) => found,
        other => panic!("`describe(1.5)` resolves: {other:?}"),
    };
    assert_eq!(
        one.fact.name_range.start_offset,
        declaration_at(1),
        "the condition picks the floating-point overload"
    );

    // **And the other direction**, because a narrowing that answered the last candidate for every call would pass the
    // assertion above and be just as wrong.
    let integral_call = rendering.find("describe(41)").expect("the call is written");
    let integral = match session.definition(
        &view,
        cpp_code_analysis::file::coordinates::ReadingOffset::new(integral_call + 3),
    ) {
        cpp_code_analysis::Known::Yes(found) => found,
        other => panic!("`describe(41)` resolves: {other:?}"),
    };
    assert_eq!(
        integral.fact.name_range.start_offset,
        declaration_at(0),
        "the condition picks the integral overload"
    );
}

/// **The condition evaluator**, decision by decision — the module the `enable_if` overloads rest on.
#[test]
fn the_constraint_evaluator_decides_the_fundamental_traits() {
    use cpp_code_analysis::sema::constraint::{satisfies, the_condition_of};

    let bind = |spelling: &str| vec![("T".to_string(), spelling.to_string())];

    // The two declarations of `describe`, verbatim.
    let integral = the_condition_of(
        "typename std::enable_if<std::is_integral<T>::value, std::string>::type describe(T value)",
    )
    .expect("the declaration carries a condition");
    assert_eq!(integral, "std::is_integral<T>::value");
    assert_eq!(satisfies(integral, &bind("int")), Some(true));
    assert_eq!(satisfies(integral, &bind("double")), Some(false));
    assert_eq!(
        satisfies(integral, &bind("std::string")),
        Some(false),
        "a class is not integral"
    );

    let floating = the_condition_of(
        "typename std::enable_if<std::is_floating_point<T>::value, int>::type describe(T value)",
    )
    .expect("the declaration carries a condition");
    assert_eq!(satisfies(floating, &bind("double")), Some(true));
    assert_eq!(satisfies(floating, &bind("int")), Some(false));

    // The three spellings of one answer.
    for spelling in [
        "std::is_integral<T>::value",
        "std::is_integral<T>{}",
        "std::is_integral_v<T>",
        "std::is_integral<T>()",
    ] {
        assert_eq!(
            satisfies(spelling, &bind("long")),
            Some(true),
            "{spelling} says a `long` is integral"
        );
    }

    // The operators, and a trait this layer does not know.
    assert_eq!(
        satisfies("!std::is_integral<T>::value", &bind("double")),
        Some(true)
    );
    assert_eq!(
        satisfies(
            "std::is_integral<T>::value && !std::is_same<T, char>::value",
            &bind("int")
        ),
        Some(true)
    );
    assert_eq!(
        satisfies(
            "std::is_integral<T>::value || std::is_floating_point<T>::value",
            &bind("float")
        ),
        Some(true)
    );
    assert_eq!(
        satisfies("std::is_user_defined<T>::value", &bind("int")),
        None,
        "a trait this layer does not know is *cannot tell*, never `false`"
    );
    assert_eq!(
        satisfies("std::is_integral<U>::value", &bind("int")),
        None,
        "an unbound parameter is cannot-tell as well"
    );
    // **The decorators**, which answer from the spelling alone — and for *any* type, so `std::string&` is a reference
    // whichever class `string` is.
    for (condition, spelling, expected) in [
        ("std::is_reference_v<T>", "int&", true),
        ("std::is_reference_v<T>", "int&&", true),
        ("std::is_reference_v<T>", "int", false),
        ("std::is_reference_v<T>", "std::string&", true),
        ("std::is_const_v<T>", "const int", true),
        ("std::is_const_v<T>", "int", false),
        ("std::is_const_v<T>", "const int*", false),
        ("std::is_volatile_v<T>", "volatile int", true),
        ("std::is_volatile_v<T>", "int", false),
        ("std::is_array_v<T>", "int[4]", true),
        ("std::is_array_v<T>", "int[]", true),
        ("std::is_array_v<T>", "int", false),
        ("std::is_function_v<T>", "int(int)", true),
        ("std::is_function_v<T>", "int", false),
        ("std::is_pointer_v<T>", "int*", true),
        ("std::is_pointer_v<T>", "int", false),
    ] {
        assert_eq!(
            satisfies(condition, &bind(spelling)),
            Some(expected),
            "{condition} of {spelling}"
        );
    }
    // **A class name is not a scalar this layer can place**, so a scalar trait of one is *cannot tell* — and an unbound
    // parameter stays unanswerable for the decorators too.
    assert_eq!(
        satisfies("std::is_integral_v<T>", &bind("std::string")),
        Some(false)
    );
    assert_eq!(satisfies("std::is_const_v<T>", &bind("U")), None);
    assert_eq!(satisfies("std::is_class_v<T>", &bind("std::string")), None);

    assert_eq!(
        the_condition_of("void plain(int value)"),
        None,
        "an ordinary function carries no condition"
    );
}

/// Doctors: how many `describe` bindings the scope tree keeps, looked up from inside the function body.
#[test]
fn dump_the_overload_bindings() {
    let source = "\
#include <type_traits>

template <typename T>
typename std::enable_if<std::is_integral<T>::value, int>::type describe(T value) { return 1; }

template <typename T>
typename std::enable_if<std::is_floating_point<T>::value, int>::type describe(T value) { return 2; }

int f() { return describe(1.5); }
";
    let session = session_with(&[("/p/a.cpp", source)]);
    let view = session.view("/p/a.cpp").expect("the file is held");
    let inside = source.find("return describe").expect("the call");
    let reading = as_reading(&view, inside);
    let scope = view.scopes.scope_at(reading.get()).expect("a scope");
    let mut at = Some(scope);
    while let Some(current) = at {
        let data = view.scopes.scope(current).expect("the scope");
        let named: Vec<_> = data
            .bindings
            .iter()
            .filter(|binding| binding.name.identifier_text() == Some("describe"))
            .map(|binding| (binding.kind, binding.name_range.start_offset))
            .collect();
        eprintln!(
            "[bindings] scope {:?} kind {:?}: {} describe {named:?}",
            current,
            data.kind,
            named.len()
        );
        at = data.parent;
    }
    let all: Vec<_> = view
        .scopes
        .scope(scope)
        .expect("the scope")
        .bindings
        .iter()
        .map(|binding| (binding.kind, binding.name.identifier_text()))
        .collect();
    eprintln!("[all] {all:?}");
}
/// Doctors: the tree above a call's callee.
#[test]
fn dump_the_call_shape() {
    let source = "int describe(double v) { return 1; }\nint f() { return describe(1.5); }\n";
    let session = session_with(&[("/p/a.cpp", source)]);
    let view = session.view("/p/a.cpp").expect("the file is held");
    let at = source.find("describe(1.5)").expect("the call") + 3;
    let reading = as_reading(&view, at);
    let node = cpp_code_analysis::sema::resolve::expression_at(&view.root, reading.get());
    eprintln!("[call] expression={:?}", node.as_ref().map(|n| n.kind()));
    let mut chain = Vec::new();
    if let Some(node) = node {
        for ancestor in node.ancestors() {
            chain.push(format!("{:?}", ancestor.kind()));
        }
    }
    eprintln!("[call] ancestors={chain:?}");
    let call = cpp_code_analysis::sema::resolve::expression_at(&view.root, reading.get()).and_then(
        |node| {
            node.ancestors()
                .find(|n| format!("{:?}", n.kind()).contains("Call"))
        },
    );
    if let Some(call) = call {
        eprintln!(
            "[call] children={:?}",
            call.children()
                .map(|c| format!("{:?}", c.kind()))
                .collect::<Vec<_>>()
        );
    }
}

/// **`enable_if` in a *template parameter*** — the other spelling of the same idiom.
///
/// ```cpp
/// template <typename T, typename = std::enable_if_t<std::is_integral<T>::value>>
/// void describe(T value);
/// ```
///
/// Measured: the same call-site question as the return-type form, and the same answer for either call, because the
/// condition is found but the parameter that carries it (`typename = …`) declares no name — so the bindings shifted and
/// `T` was bound to nothing.
#[test]
fn an_enable_if_in_a_template_parameter_is_read() {
    let source = "\
#include <type_traits>
#include <string>

template <typename T, typename = std::enable_if_t<std::is_integral<T>::value>>
std::string describe(T value) {
    return \"integral\";
}

template <typename T, typename = std::enable_if_t<std::is_floating_point<T>::value>>
std::string describe(T value) {
    return \"floating\";
}

std::string f() {
    describe(41);
    return describe(1.5);
}
";
    let session = session_with(&[("/p/a.cpp", source)]);
    let view = session.view("/p/a.cpp").expect("the file is held");
    let rendering = view.root.text().to_string();
    let declaration_at = |which: usize| -> usize {
        rendering
            .match_indices("describe(T value)")
            .nth(which)
            .map(|(at, _)| at)
            .expect("both declarations are in the rendering")
    };
    let floating_call = rendering
        .find("describe(1.5)")
        .expect("the call is written");
    let one = match session.definition(
        &view,
        cpp_code_analysis::file::coordinates::ReadingOffset::new(floating_call + 3),
    ) {
        cpp_code_analysis::Known::Yes(found) => found,
        other => panic!("`describe(1.5)` resolves: {other:?}"),
    };
    assert_eq!(
        one.fact.name_range.start_offset,
        declaration_at(1),
        "the template parameter's condition picks the floating-point overload"
    );
}
/// **A `requires` clause** — C++20's own spelling of the same condition.
///
/// ```cpp
/// template <typename T> requires std::is_integral_v<T>
/// std::string describe(T value);
/// ```
///
/// `enable_if` is found by name; a `requires` clause is a different keyword and was not read at all, so both overloads
/// stayed candidates and the call answered the first one written.
#[test]
fn a_requires_clause_is_read() {
    let source = "\
#include <type_traits>
#include <string>

template <typename T> requires std::is_integral_v<T>
std::string describe(T value) {
    return \"integral\";
}

template <typename T> requires std::is_floating_point_v<T>
std::string describe(T value) {
    return \"floating\";
}

std::string f() {
    describe(41);
    return describe(1.5);
}
";
    let session = session_with(&[("/p/a.cpp", source)]);
    let view = session.view("/p/a.cpp").expect("the file is held");
    let rendering = view.root.text().to_string();
    let declaration_at = |which: usize| -> usize {
        rendering
            .match_indices("describe(T value)")
            .nth(which)
            .map(|(at, _)| at)
            .expect("both declarations are in the rendering")
    };
    let floating_call = rendering
        .find("describe(1.5)")
        .expect("the call is written");
    let one = match session.definition(
        &view,
        cpp_code_analysis::file::coordinates::ReadingOffset::new(floating_call + 3),
    ) {
        cpp_code_analysis::Known::Yes(found) => found,
        other => panic!("`describe(1.5)` resolves: {other:?}"),
    };
    assert_eq!(
        one.fact.name_range.start_offset,
        declaration_at(1),
        "the `requires` clause picks the floating-point overload"
    );

    // **And the other direction**, as in the `enable_if` test: an answer that always took the last candidate would pass
    // the assertion above.
    let integral_call = rendering.find("describe(41)").expect("the call is written");
    let integral = match session.definition(
        &view,
        cpp_code_analysis::file::coordinates::ReadingOffset::new(integral_call + 3),
    ) {
        cpp_code_analysis::Known::Yes(found) => found,
        other => panic!("`describe(41)` resolves: {other:?}"),
    };
    assert_eq!(
        integral.fact.name_range.start_offset,
        declaration_at(0),
        "and the integral overload for an int"
    );
}

/// **A named concept** — `template <integral T>` where `integral` is a concept the file defines.
///
/// ```cpp
/// template <typename T> concept integral = std::is_integral_v<T>;
/// template <integral T> std::string describe(T value);
/// ```
///
/// The constraint is a *name*, so there is no expression in the declaration to evaluate: it has to be found, and its own
/// right-hand side evaluated with the same bindings. Measured: both overloads stayed candidates and the call answered the
/// first.
#[test]
fn a_named_concept_decides_the_overload() {
    let source = "\
#include <type_traits>
#include <string>

template <typename T>
concept integral = std::is_integral_v<T>;

template <typename T>
concept floating = std::is_floating_point_v<T>;

template <integral T>
std::string describe(T value) {
    return \"integral\";
}

template <floating T>
std::string describe(T value) {
    return \"floating\";
}

std::string f() {
    describe(41);
    return describe(1.5);
}
";
    let session = session_with(&[("/p/a.cpp", source)]);
    let view = session.view("/p/a.cpp").expect("the file is held");
    let rendering = view.root.text().to_string();
    let declaration_at = |which: usize| -> usize {
        rendering
            .match_indices("describe(T value)")
            .nth(which)
            .map(|(at, _)| at)
            .expect("both declarations are in the rendering")
    };
    let floating_call = rendering
        .find("describe(1.5)")
        .expect("the call is written");
    let one = match session.definition(
        &view,
        cpp_code_analysis::file::coordinates::ReadingOffset::new(floating_call + 3),
    ) {
        cpp_code_analysis::Known::Yes(found) => found,
        other => panic!("`describe(1.5)` resolves: {other:?}"),
    };
    assert_eq!(
        one.fact.name_range.start_offset,
        declaration_at(1),
        "the concept picks the floating-point overload"
    );
}
/// **A concept that composes another** — `concept signed_integral = integral<T> && is_signed_v<T>`.
///
/// Both overloads are constrained, so the call is decided by *which condition is true*, and the composed one can only be
/// answered by expanding `integral<T>` first with its own parameter bound. Measured before expansion existed: the verdict
/// was "cannot tell", both candidates stayed, and the call answered the first declaration.
#[test]
fn a_composed_concept_decides_the_overload() {
    let source = "\
#include <type_traits>
#include <string>

template <typename T>
concept integral = std::is_integral_v<T>;

template <typename T>
concept signed_integral = integral<T> && std::is_signed_v<T>;

template <signed_integral T>
std::string describe(T value) {
    return \"signed\";
}

template <typename T> requires std::is_floating_point_v<T>
std::string describe(T value) {
    return \"floating\";
}

std::string f() {
    describe(-3);
    return describe(1.5);
}
";
    let session = session_with(&[("/p/a.cpp", source)]);
    let view = session.view("/p/a.cpp").expect("the file is held");
    let rendering = view.root.text().to_string();
    let declaration_at = |which: usize| -> usize {
        rendering
            .match_indices("describe(T value)")
            .nth(which)
            .map(|(at, _)| at)
            .expect("both declarations are in the rendering")
    };

    // **`describe(1.5)`** — the composed condition is `integral<double> && is_signed<double>`, which is false once
    // `integral<double>` is expanded, so the floating overload is the only survivor.
    let floating_call = rendering
        .find("describe(1.5)")
        .expect("the call is written");
    let floating = match session.definition(
        &view,
        cpp_code_analysis::file::coordinates::ReadingOffset::new(floating_call + 3),
    ) {
        cpp_code_analysis::Known::Yes(found) => found,
        other => panic!("`describe(1.5)` resolves: {other:?}"),
    };
    assert_eq!(
        floating.fact.name_range.start_offset,
        declaration_at(1),
        "the composed concept is false for a double"
    );

    // **And `describe(-3)`** — `integral<int> && is_signed<int>` is true, so the signed overload wins.
    let signed_call = rendering.find("describe(-3)").expect("the call is written");
    let signed = match session.definition(
        &view,
        cpp_code_analysis::file::coordinates::ReadingOffset::new(signed_call + 3),
    ) {
        cpp_code_analysis::Known::Yes(found) => found,
        other => panic!("`describe(-3)` resolves: {other:?}"),
    };
    assert_eq!(
        signed.fact.name_range.start_offset,
        declaration_at(0),
        "and true for a signed int"
    );
}

/// **A `No` from the type relation is not always a refusal** — `value_or(0)` against `_Ty2&&`.
///
/// The relation answers `No` for `int` → `_Ty2&&` because `_Ty2` is not `int`, and `_Ty2` is a *template parameter*. Only a
/// class the index can find decides those; one it cannot see makes the answer `Unknown`, and `Unknown` reports nothing.
#[test]
fn a_template_parameter_target_is_not_a_refusal() {
    use cpp_code_analysis::sema::convert::{Conversion, conversion};
    use cpp_code_analysis::sema::types::{Type, parse_type_spelling};

    let an_int = Type::builtin("int");
    assert_eq!(
        conversion(&an_int, "_Ty2&&", |_| None),
        Conversion::Unknown,
        "an unread template parameter is cannot-tell, not a refusal"
    );
    assert_eq!(
        conversion(&an_int, "T", |_| None),
        Conversion::Unknown,
        "and so is a bare `T`"
    );
    // **A class the index knows still refuses**: `int` cannot be built into a `Widget` that has no such constructor.
    assert_eq!(
        conversion(&an_int, "const Widget&", |_| Some(false)),
        Conversion::Refused,
    );
    // **And an unread class on the *source* side is cannot-tell too**, not a refusal.
    let a_string = parse_type_spelling("std::string");
    assert_eq!(conversion(&a_string, "int", |_| None), Conversion::Unknown);
}

/// **A member's deduced return type, through the header that declares it** — `auto copy_of() const { return value_; }`.
///
/// The class is written in a header and the call is in the `.cpp`, which is the shape a project actually has. The declared
/// type is `auto`, so the *body* is what says `int` once the class's `T` is substituted, and the body is reachable because
/// the header is part of the same translation unit.
#[test]
fn a_members_deduced_return_type_is_read_through_a_header() {
    let header = "\
template <typename T>
class Box {
public:
    explicit Box(T value) : value_(value) {}
    auto copy_of() const { return value_; }
    auto doubled() const { return value_ + value_; }

private:
    T value_;
};
";
    let source = "\
#include \"a.h\"

int f() {
    Box<int> box(7);
    auto copied = box.copy_of();
    auto doubled = box.doubled();
    return copied + doubled;
}
";
    let session = session_with(&[("/p/a.cpp", source), ("/p/a.h", header)]);
    let view = session.view("/p/a.cpp").expect("the file is held");
    let offset = last_word(source, "copied");
    match session.type_at(&view, as_reading(&view, offset)) {
        cpp_code_analysis::Known::Yes(found) => {
            assert_eq!(found.type_of, "int", "`copy_of` returns the field's type");
        }
        other => panic!("`copied` is typed: {other:?}"),
    }
    let offset = last_word(source, "doubled");
    match session.type_at(&view, as_reading(&view, offset)) {
        cpp_code_analysis::Known::Yes(found) => {
            assert_eq!(found.type_of, "int", "`value_ + value_` is an `int`");
        }
        other => panic!("`doubled` is typed: {other:?}"),
    }
}

/// **A fold expression's type** — `template <typename... Vs> auto sum_of(Vs... values) { return (values + ...); }`.
///
/// The return type is `auto`, so the body decides, and the body is a fold: the type is every argument the call passed,
/// widened by `+` — four `int`s are an `int`, and `1` with `2.5` is a `double`.
#[test]
fn a_fold_expressions_type_is_the_widest_argument() {
    let source = "\
namespace t {
template <typename... Values>
auto sum_of(Values... values) {
    return (values + ...);
}
}

int f() {
    auto total = t::sum_of(1, 2, 3, 4);
    auto mixed = t::sum_of(1, 2.5);
    return total + static_cast<int>(mixed);
}
";
    let session = session_with(&[("/p/a.cpp", source)]);
    let view = session.view("/p/a.cpp").expect("the file is held");
    for (name, expected) in [("total", "int"), ("mixed", "double")] {
        let offset = last_word(source, name);
        match session.type_at(&view, as_reading(&view, offset)) {
            cpp_code_analysis::Known::Yes(found) => {
                assert_eq!(found.type_of, expected, "{name}");
            }
            other => panic!("`{name}` is typed: {other:?}"),
        }
    }
}

/// **A lambda called through its own `operator()`** — `auto doubler = [](int v) { return v * 2; }; doubler(3)`.
#[test]
fn a_lambda_call_has_the_type_its_body_returns() {
    let source = "\
int f() {
    auto doubler = [](int value) { return value * 2; };
    auto result = doubler(3);
    return result;
}
";
    let session = session_with(&[("/p/a.cpp", source)]);
    let view = session.view("/p/a.cpp").expect("the file is held");
    for (name, _) in [("doubler", "closure"), ("result", "int")] {
        let offset = last_word(source, name);
        match session.type_at(&view, as_reading(&view, offset)) {
            cpp_code_analysis::Known::Yes(found) => println!("{name} → {}", found.type_of),
            other => println!("{name} → {other:?}"),
        }
    }
    let offset = last_word(source, "result");
    match session.type_at(&view, as_reading(&view, offset)) {
        cpp_code_analysis::Known::Yes(found) => assert_eq!(found.type_of, "int"),
        other => panic!("`result` is typed: {other:?}"),
    }
}

/// **A structured binding over a `pair`** — `auto [number, fraction] = pair;` gives the components, in order.
///
/// Measured before this: **both** names answered `int()` — a spelling invented by the `auto` path after the pattern's
/// initializer was never found, because the `=` of `auto [a, b] = pair;` is inside the `InitDeclarator` and the lookup only
/// scanned the `Declaration` above it.
#[test]
fn a_structured_binding_over_a_pair_has_the_components_types() {
    let source = "\
#include <utility>

int f() {
    std::pair<int, double> pair{1, 2.5};
    auto [number, fraction] = pair;
    return number + static_cast<int>(fraction);
}
";
    let session = session_with(&[("/p/a.cpp", source)]);
    let view = session.view("/p/a.cpp").expect("the file is held");
    for (name, expected) in [("number", "int"), ("fraction", "double")] {
        let offset = last_word(source, name);
        match session.type_at(&view, as_reading(&view, offset)) {
            cpp_code_analysis::Known::Yes(found) => {
                assert_eq!(found.type_of, expected, "{name}");
            }
            other => panic!("`{name}` is typed: {other:?}"),
        }
    }
}

/// **`auto x = std::move(y)` is a value, not an rvalue reference.**
///
/// Reported from an editor with a screenshot: `auto diagnostic_results = std::move(_Arg.result.Data);` drew its inlay
/// type as `std::vector<LuaDiagnosticInfo>&&`, and every later query on it fell apart — a range-`for` over it could
/// not type its element, so `diagnostic.Message` answered "the type of `diagnostic` is not known".
///
/// The language rule is the one this pins: **`auto` alone declares a new object**, so the deduced type is the
/// initializer's type with its reference (and top-level cv) removed — `std::move` returning `T&&` deduces `T`. Only a
/// declarator that *writes* a reference keeps one, and that case has its own test
/// (`auto_reference_on_a_reference_returns_one_reference`).
#[test]
fn a_value_auto_drops_the_reference_its_initializer_had() {
    let source = "\
namespace std {
    template <class T> T&& move(T& value) noexcept;
}
struct Item { int size; };
int f() {
    Item item;
    auto moved = std::move(item);
    return moved.size;
}
";
    assert_eq!(
        type_of_use(source, "moved").as_deref(),
        Some("Item"),
        "`auto` alone is a new object, so the reference is not part of its type"
    );
}

/// **A class template's member alias is substituted with the instantiation's arguments.**
///
/// `std::vector<T>::emplace_back()` returns `reference`, which the class itself defines as `value_type&`. The declaration
/// names the element type, so the answer for `auto &result = v.emplace_back()` is `int&` — and a project class that
/// writes `using reference = T&;` must answer the same way, which is why this is a substitution along the class's own
/// aliases rather than a table of container members.
#[test]
fn a_container_members_alias_is_substituted_with_its_element_type() {
    let source = "\
namespace std {
    template <class T> class vector {
    public:
        using value_type = T;
        using reference = value_type&;
        reference emplace_back();
    };
}
int f() {
    std::vector<int> values;
    auto &result = values.emplace_back();
    result = 3;
    return result;
}
";
    assert_eq!(
        type_of_use(source, "result").as_deref(),
        Some("int&"),
        "`reference` is `value_type&`, and `value_type` is the argument the declaration wrote"
    );
}

/// **A dependent alias of a class template is substituted too** — the chain the standard library actually writes.
///
/// `reference` is `allocator_traits<value_type>::value_type&`: an alias whose right-hand side is a **member of another
/// class template**, instantiated with this class's own argument. MSVC's `vector` writes exactly this shape
/// (`using reference = _Alty&;` with `_Alty` a traits alias), which is why the reported `emplace_back()` had no type.
#[test]
fn a_dependent_alias_of_a_class_template_is_substituted() {
    let source = "\
namespace std {
    template <class T> struct allocator_traits { using value_type = T; };
    template <class T> class vector {
    public:
        using value_type = T;
        using reference = allocator_traits<value_type>::value_type&;
        reference emplace_back();
    };
}
int f() {
    std::vector<int> values;
    auto &result = values.emplace_back();
    result = 3;
    return result;
}
";
    assert_eq!(
        type_of_use(source, "result").as_deref(),
        Some("int&"),
        "`allocator_traits<value_type>::value_type` is `int` for `vector<int>`"
    );
}

/// **A class the index does not hold is answered from the rendering** — the shape of every system header.
///
/// The index summarizes the project's files; `<vector>` is not one of them, so `std::vector` has no entry and its members
/// cannot be looked up — measured on `EmmyLuaCodeStyle`: "the type of `std::vector<LuaDiagnostic>&` is not known here".
/// But the **rendering** has the class's body inlined, because that is what cooking means, and this crate parses it for
/// every cooked file. The fixture makes that shape without the standard library: `lib.h` is *included* and deliberately
/// **not** a project file.
#[test]
fn a_class_the_index_does_not_hold_is_answered_from_the_rendering() {
    const LIB_H: &str = "\
namespace std {
    template <class T> struct allocator_traits { using value_type = T; };
    template <class T> class vector {
    public:
        using value_type = T;
        using reference = allocator_traits<value_type>::value_type&;
        reference emplace_back();
    };
}
";
    let source = "\
#include \"lib.h\"
int f() {
    std::vector<int> values;
    auto &result = values.emplace_back();
    return result;
}
";
    let memory = MemoryFiles::new()
        .with_file("/p/lib.h", LIB_H)
        .with_file("/p/a.cpp", source);
    let providers = SessionFiles::new(OpenDocuments::new(), memory);
    let mut session = Session::with_config(
        "/p",
        providers,
        WatchFilter::new("/p"),
        CompilerConfig::default(),
    );
    // **Only the translation unit is a project file.** `lib.h` is read because it is included, which is exactly what a
    // system header is to this index.
    session.add_project_files([std::path::PathBuf::from("/p/a.cpp")]);
    session.want_cooked_reading(std::path::Path::new("/p/a.cpp"));
    session.index_everything();

    let view = session.view("/p/a.cpp").expect("the file is held");
    let offset = last_word(source, "result");
    match session.type_at(&view, as_reading(&view, offset)) {
        cpp_code_analysis::Known::Yes(found) => assert_eq!(
            found.type_of, "int&",
            "the class's member alias is in the rendering, even though the index has no `vector`"
        ),
        _ => panic!("the rendering holds `std::vector`, so `result` has a type"),
    }
}

/// **A member is found through a class template's instantiation, or it is not found at all.**
///
/// The plain `struct` and the class template declare the same member. If the first answers and the second does not, the
/// defect is in the member query's handling of a class template — not in the alias substitution, not in the index's
/// coverage, and not in the rendering. Written because `inspect complete` on `results.` in `EmmyLuaCodeStyle` printed
/// `scope std::vector, 0 item(s)` while the MSVC header is summarized with 993 declarations.
#[test]
fn a_member_is_found_through_a_plain_class_and_through_a_template() {
    const LIB_H: &str = "\
struct Plain { int emplace_back(); };
template <class T> class Temp { public: int emplace_back(); };
";
    let source = "\
#include \"lib.h\"
int f() {
    Plain plain;
    auto a = plain.emplace_back();
    Temp<int> temp;
    auto b = temp.emplace_back();
    return a + b;
}
";
    let memory = MemoryFiles::new()
        .with_file("/p/lib.h", LIB_H)
        .with_file("/p/a.cpp", source);
    let providers = SessionFiles::new(OpenDocuments::new(), memory);
    let mut session = Session::with_config(
        "/p",
        providers,
        WatchFilter::new("/p"),
        CompilerConfig::default(),
    );
    session.add_project_files([
        std::path::PathBuf::from("/p/lib.h"),
        std::path::PathBuf::from("/p/a.cpp"),
    ]);
    session.want_cooked_reading(std::path::Path::new("/p/a.cpp"));
    session.index_everything();

    let view = session.view("/p/a.cpp").expect("the file is held");
    for (spelling, expected) in [("a", "int"), ("b", "int")] {
        let offset = last_word(source, spelling);
        let found = session.type_at(&view, as_reading(&view, offset));
        assert!(
            matches!(&found, cpp_code_analysis::Known::Yes(known) if known.type_of == expected),
            "`{spelling}` should be `{expected}`, got {found:?}"
        );
    }
}
/// **The standard library's own idiom**: a class template inside macro-opened namespaces, with macro-guarded members.
///
/// Every simplified fixture answers correctly, so the one thing the real header has that they do not is this shape:
/// MSVC writes `_STD_BEGIN template <class _Ty…> class vector { … _CONSTEXPR20 reference emplace_back() … } _STD_END`.
/// If this fails, the defect is the macro-region handling around a class template, not the member query and not the
/// alias substitution.
#[test]
fn a_class_template_in_macro_opened_namespaces_is_the_standard_library_idiom() {
    const LIB_H: &str = "\
#define _STD_BEGIN namespace std {
#define _STD_END }
#define _CONSTEXPR20
#define _NODISCARD [[nodiscard]]
_STD_BEGIN
template <class _Ty> class vector {
public:
    using value_type = _Ty;
    using reference = value_type&;
    _NODISCARD _CONSTEXPR20 reference emplace_back();
};
_STD_END
";
    let source = "\
#include \"lib.h\"
int f() {
    std::vector<int> values;
    auto &result = values.emplace_back();
    result = 3;
    return result;
}
";
    let memory = MemoryFiles::new()
        .with_file("/p/lib.h", LIB_H)
        .with_file("/p/a.cpp", source);
    let providers = SessionFiles::new(OpenDocuments::new(), memory);
    let mut session = Session::with_config(
        "/p",
        providers,
        WatchFilter::new("/p"),
        CompilerConfig::default(),
    );
    session.add_project_files([
        std::path::PathBuf::from("/p/lib.h"),
        std::path::PathBuf::from("/p/a.cpp"),
    ]);
    session.want_cooked_reading(std::path::Path::new("/p/a.cpp"));
    session.index_everything();

    let view = session.view("/p/a.cpp").expect("the file is held");
    let offset = last_word(source, "result");
    let found = session.type_at(&view, as_reading(&view, offset));
    assert!(
        matches!(&found, cpp_code_analysis::Known::Yes(known) if known.type_of == "int&"),
        "the standard library's idiom should answer `int&`, got {found:?}"
    );
}

/// **Two levels of include, with guards** — the shape a standard header arrives through.
///
/// Every fixture so far had the class one include away from the file. A real translation unit reaches `<vector>` through
/// a chain (`std::string` → `xstring` → …), and each header is guarded. This is that shape with the types this crate
/// can see: `base.h` (guarded) declares the class, `mid.h` includes it, the `.cpp` includes only `mid.h`.
#[test]
fn a_member_is_found_two_includes_away_through_guards() {
    const BASE_H: &str = "\
#ifndef BASE_H
#define BASE_H
namespace std {
    template <class T> class vector {
    public:
        using value_type = T;
        using reference = value_type&;
        reference emplace_back();
    };
}
#endif
";
    const MID_H: &str = "\
#pragma once
#include \"base.h\"
";
    let source = "\
#include \"mid.h\"
int f() {
    std::vector<int> values;
    auto &result = values.emplace_back();
    result = 3;
    return result;
}
";
    let memory = MemoryFiles::new()
        .with_file("/p/base.h", BASE_H)
        .with_file("/p/mid.h", MID_H)
        .with_file("/p/a.cpp", source);
    let providers = SessionFiles::new(OpenDocuments::new(), memory);
    let mut session = Session::with_config(
        "/p",
        providers,
        WatchFilter::new("/p"),
        CompilerConfig::default(),
    );
    session.add_project_files([
        std::path::PathBuf::from("/p/base.h"),
        std::path::PathBuf::from("/p/mid.h"),
        std::path::PathBuf::from("/p/a.cpp"),
    ]);
    session.want_cooked_reading(std::path::Path::new("/p/a.cpp"));
    session.index_everything();

    let view = session.view("/p/a.cpp").expect("the file is held");
    let offset = last_word(source, "result");
    let found = session.type_at(&view, as_reading(&view, offset));
    assert!(
        matches!(&found, cpp_code_analysis::Known::Yes(known) if known.type_of == "int&"),
        "two includes away, through a guard, is still `int&`, got {found:?}"
    );
}

/// **A partial specialization is a declaration: it must produce a fact, with its pattern.**
///
/// The shape is MSVC's, reduced — `<xmemory>` decides `allocator_traits`'s base with
/// `struct _Is_default_allocator<allocator<_Ty>, void_t<typename allocator<_Ty>::_From_primary>> : …`, and the index
/// holds **no fact at all** for that specialization (measured on the real header: the principal is at offset 17000 and
/// the next declaration at 17304, with nothing between). Without it the trait can never be true, `allocator_traits`
/// takes the wrong `conditional_t` branch, and `auto pos = events.size();` answers `_Alty_traits::size_type`.
///
/// This is asserted in the repository rather than through a fixture written by hand to a temporary file: that pipeline
/// was measured to give two different answers for one shape between runs, so it is not evidence.
#[test]
fn a_partial_specialization_is_a_declaration_with_a_pattern() {
    let source = "\
template <class T> struct A { };
template <class V> struct void_t { };
template <class T, class = void> struct B : A<T> { };
template <class T> struct B<A<T>, void_t<typename A<T>::From>> : A<T> {
    using inner = int;
};
";
    let session = session_with(&[("/p/a.cpp", source)]);
    let facts: Vec<_> = session
        .index()
        .every_fact_named("B")
        .into_iter()
        .map(|(_, fact)| fact.clone())
        .collect();
    let patterns: Vec<Option<&str>> = facts.iter().map(|fact| fact.pattern()).collect();
    assert!(
        facts.len() >= 2,
        "the principal and its specialization are two declarations: {patterns:?}"
    );
    assert!(
        patterns.iter().any(Option::is_some),
        "and the specialization records the pattern it matches: {patterns:?}"
    );
    let inner = session.index().every_fact_named("inner");
    assert!(
        !inner.is_empty(),
        "the specialization's member is declared too"
    );
}

/// **A base chosen by a `conditional_t` over a library trait picks the branch the trait selects.**
///
/// This is the standard library's own shape, reduced: `allocator_traits<A> : conditional_t<_Is_default_allocator<A>::value,
/// _Default_allocator_traits<A>, _Normal_allocator_traits<A>>`, where the trait is a **partial specialization** —
/// `_Is_default_allocator<allocator<T>> : true_type` against the principal `false_type`. It took four steps to make
/// true, and each one is a rule of its own: the class's defaulted argument is substituted at the use
/// (`A = allocator<T>`), a specialization's slot matches a qualified argument by head and recursively, the trait's
/// value is the base its matching specialization inherits, and the `conditional` then selects one branch. Until all
/// four, `pos` answered `allocator_traits<A>::size_type`.
#[test]
fn a_conditional_base_picks_the_branch_its_trait_selects() {
    let source = "\
namespace std {
template <class T> struct false_type { static constexpr bool value = false; };
template <class T> struct true_type { static constexpr bool value = true; };
template <class T> struct allocator { using value_type = T; };
template <class T> struct _Is_default_allocator : false_type { };
template <class T> struct _Is_default_allocator<allocator<T>> : true_type { };
template <bool C, class A, class B> struct conditional;
template <class A, class B> struct conditional<true, A, B> { using type = A; };
template <class A, class B> struct conditional<false, A, B> { using type = B; };
template <bool C, class A, class B> using conditional_t = typename conditional<C, A, B>::type;
template <class T> struct _Default_allocator_traits { using size_type = unsigned long long; };
template <class T> struct _Normal_allocator_traits { using size_type = typename T::size_type; };
template <class A>
struct allocator_traits
    : conditional_t<_Is_default_allocator<A>::value, _Default_allocator_traits<A>, _Normal_allocator_traits<A>> { };
template <class T, class A = allocator<T>> struct vector {
    using size_type = typename allocator_traits<A>::size_type;
    size_type size() const;
};
}
std::vector<int> events;
void f() {
    auto pos = events.size();
    (void)pos;
}
";
    assert_eq!(
        type_of_use(source, "pos").as_deref(),
        Some("unsigned long long"),
        "`_Is_default_allocator<allocator<int>>` is true, so the base is `_Default_allocator_traits`"
    );
}

/// **An alias template used as a base has its arguments substituted into the alias's target.** — *open*
///
/// The standard library writes `allocator_traits<_Alloc> : conditional_t<…>` — a base that is an **alias template**,
/// so the base is not a class until the alias's parameters are replaced by what the clause wrote. Measured on
/// `EmmyLuaCodeStyle`, the member list reported
/// `NotDeclaredHere("conditional<_Test,_Ty1,_Ty2>::type")`: the target's own parameter names were still standing,
/// and the base was `unlisted`, so `size_type` was in no member list.
///
/// Asserted and ignored rather than deleted: it is the reproducer for the next step of the generics work, and it says
/// what the answer must be.
///
/// # Why the fix is not "call the alias walk here"
///
/// It was tried. `a_spelling_through_the_aliases` turns such a spelling into a type and is the obvious tool, but it
/// asks member questions, a member question builds a member list, and a member list walks bases — **measured as a
/// stack overflow** (`STATUS_STACK_OVERFLOW` on this very test). The resolution therefore has to be a static one: the
/// alias fact's own parameters and target, read from the index, then the target member's fact, substituted; and it
/// has to bound its own steps. For `conditional_t` it also has to stay sound — the condition is a library trait over
/// a template parameter and cannot be evaluated, so the honest answer is the members **both** branches agree on, the
/// same agreement rule `the_return_the_overloads_agree_on` applies.
#[test]
fn an_alias_template_used_as_a_base_substitutes_its_arguments() {
    let source = "\
template <class T> struct chosen { using size_type = unsigned long long; };
template <class T> struct other { using size_type = unsigned long long; };
template <class X, class Y> struct pick { using type = X; };
template <class X, class Y> using pick_t = typename pick<X, Y>::type;
template <class T> struct traits : pick_t<chosen<T>, other<T>> { };
struct User : traits<int> { };
";
    let session = session_with(&[("/p/a.cpp", source)]);
    let view = session.view("/p/a.cpp").expect("the file is held");
    let list = cpp_code_analysis::index::project::members_of(
        session.index(),
        &view.scopes,
        &view.root,
        std::path::Path::new("/p/a.cpp"),
        "User",
    );
    let cpp_code_analysis::Known::Yes(list) = list else {
        panic!("`User` is a class: {list:?}");
    };
    let names: Vec<&str> = list
        .members
        .iter()
        .map(|member| member.fact.name.as_str())
        .collect();
    assert!(
        names.contains(&"size_type"),
        "the alias's target names `chosen<T>`, whose members are the base's: {names:?} (unlisted: {:?})",
        list.unlisted
            .iter()
            .map(|one| one.spelling.as_str())
            .collect::<Vec<_>>()
    );
}

/// **A class template that is forward-declared and then defined has the *definition's* base clause.**
///
/// MSVC's `<xmemory>` writes `struct allocator_traits;` at line 554 and, at 755,
/// `struct allocator_traits : conditional_t<…> {};` — an **empty body** whose every member is inherited. The base
/// walk read the first Type fact it found, which is the forward declaration, whose `bases` is empty: measured on
/// `EmmyLuaCodeStyle`, `members_of("std::allocator_traits")` answered `0 listed, 0 unlisted`, and `size_type` was in
/// no list at all.
#[test]
fn a_class_template_forward_declared_then_defined_uses_the_definitions_bases() {
    let source = "\
template <class T> struct traits;
template <class T> struct base {
    using size_type = unsigned long long;
};
template <class T> struct traits : base<T> { };
struct User : traits<int> { };
";
    let session = session_with(&[("/p/a.cpp", source)]);
    let view = session.view("/p/a.cpp").expect("the file is held");
    let list = cpp_code_analysis::index::project::members_of(
        session.index(),
        &view.scopes,
        &view.root,
        std::path::Path::new("/p/a.cpp"),
        "User",
    );
    let cpp_code_analysis::Known::Yes(list) = list else {
        panic!("`User` is a class: {list:?}");
    };
    let names: Vec<&str> = list
        .members
        .iter()
        .map(|member| member.fact.name.as_str())
        .collect();
    assert!(
        names.contains(&"size_type"),
        "the inherited member comes through the definition's base: {names:?} (unlisted: {:?})",
        list.unlisted
            .iter()
            .map(|one| one.spelling.as_str())
            .collect::<Vec<_>>()
    );
}

/// **A container's `size()` is a size, not the name of an internal alias.**
///
/// MSVC's `<vector>` writes the chain in four steps, and the last one is why this is a cross-file fixture:
///
/// ```cpp
/// using _Alty        = _Rebind_alloc_t<_Alloc, _Ty>;          // an alias template from <xmemory>
/// using _Alty_traits = allocator_traits<_Alty>;
/// using size_type    = typename _Alty_traits::size_type;
/// ```
///
/// `_Rebind_alloc_t` is declared in `<xmemory>`, which `<vector>` includes — and in the same file the chain resolves.
/// The fixture puts it where the standard library has it, because that is the shape the walk stops on.
///
/// Reported from an editor: `auto pos = _events.size();` in `LuaParser.cpp:51` drew `_Alty_traits::size_type` — the
/// walk stopped at `allocator_traits<_Alty>` without substituting `_Alty`.
#[test]
fn a_containers_size_called_on_an_object_is_its_size_type() {
    const XMEMORY: &str = "\
namespace std {
template <class T> struct allocator { using value_type = T; using size_type = unsigned long long; };
template <class A> struct allocator_traits { using size_type = typename A::size_type; };
template <class A, class T> struct _Rebind_alloc { using type = A; };
template <class A, class T> using _Rebind_alloc_t = typename _Rebind_alloc<A, T>::type;
}
";
    let source = "\
#include \"xmemory\"
namespace std {
template <class _Ty, class _Alloc = allocator<_Ty>> class vector {
    using _Alty        = _Rebind_alloc_t<_Alloc, _Ty>;
    using _Alty_traits = allocator_traits<_Alty>;
public:
    using size_type = typename _Alty_traits::size_type;
    size_type size() const;
};
}
std::vector<int> events;
void f() {
    auto pos = events.size();
    (void)pos;
}
";
    let session = session_with(&[("/p/xmemory", XMEMORY), ("/p/a.cpp", source)]);
    let view = session.view("/p/a.cpp").expect("the file is held");
    let offset = last_word(source, "pos");
    let found = session.type_at(&view, as_reading(&view, offset));
    assert!(
        matches!(&found, cpp_code_analysis::Known::Yes(known) if known.type_of == "unsigned long long"),
        "the chain ends at the size type, not at an alias of the allocator's: {found:?}"
    );
}

/// **A deduced type keeps the alias the source wrote.** `auto text = b.get();` where `get` returns
/// `std::string` — not `basic_string<char, char_traits<char>, allocator<char>>`, which is what the alias *stands for*
/// and what an editor drew in the inlay hint of `LuaCodeFormat.cpp:131`.
///
/// The alias walk (`a_spelling_through_the_aliases`) exists to turn a member's own spelling into a type the reader can
/// use — `pointer` → `_Val*`, `_Alloc_traits::reference` → `std::size_t&` — and it was also following a name qualified
/// by a **namespace**, where there is nothing to make usable: `std::string` is already a name.
#[test]
fn a_deduced_type_keeps_the_alias_the_source_wrote() {
    let source = "\
namespace std {
template <class T> struct allocator { };
template <class C> struct char_traits { };
template <class C, class T, class A> struct basic_string { };
using string = basic_string<char, char_traits<char>, allocator<char>>;
}
struct Builder { std::string get(); };
void f(Builder b) {
    auto text = b.get();
    (void)text;
}
";
    assert_eq!(
        type_of_use(source, "text").as_deref(),
        Some("std::string"),
        "the spelling the header wrote, not what the alias stands for"
    );
}

/// **The range of a range-based `for` written as a reference is still the range.**
///
/// `for (auto &x : someContainer)` where `someContainer` is a parameter is the ordinary spelling in every C++ code
/// base, and a parameter is almost always `const std::vector<T>&`. [`crate::index`]'s `range_element_of` matched only
/// `Type::Named`, so a one-token difference — the `&` the reader never thinks about — left the loop variable with no
/// type at all. Measured through the fixture in `tests/emplace_probe.rs`: `void f(const std::vector<Other>
/// &diagnostics) { for (auto &d : diagnostics) }` typed `d` as `std::vector<Other>&`, the container.
#[test]
fn a_range_written_as_a_reference_still_binds_its_element() {
    let source = "\
#include <vector>
int f(const std::vector<int> &values) {
    for (auto x : values) {
        int k = x;
        (void)k;
    }
    return 0;
}
";
    let session = session_with(&[
        (
            "/p/vector",
            "namespace std { template <class T> struct vector { }; }\n",
        ),
        ("/p/a.cpp", source),
    ]);

    let view = session.view("/p/a.cpp").expect("the file is held");
    // The last `k` is the use in `(void)k;`, whose type is the declaration's.
    let offset = last_word(source, "k");
    let found = session.type_at(&view, as_reading(&view, offset));
    assert!(
        matches!(&found, cpp_code_analysis::Known::Yes(known) if known.type_of == "int"),
        "the element of a `const std::vector<int>&` is `int`, got {found:?}"
    );
}

/// **An `auto` declaration inside a range-for body is deduced from *its own* initializer, not from the loop's range.**
///
/// Reported from an editor: `LuaCodeFormat.cpp:304`, `auto &result = results.emplace_back();`, the first statement of
/// `for (auto &diagnostic: diagnostics)`, answered `std::vector<LuaDiagnostic>&` — the **parameter's** type, which that
/// line never mentions. `range_for_element_type` reached the enclosing `RangeForStat` by ancestry and answered before
/// the ordinary deduction ran, so every `auto` in every loop body was typed as the loop variable.
#[test]
fn an_auto_declaration_in_a_range_for_body_is_deduced_from_its_own_initializer() {
    let source = "\
namespace std { template <class T> struct vector { T &emplace_back(); }; }
struct Item { int size; };
struct Other { int size; };
void f(const std::vector<Other> &diagnostics) {
    std::vector<Item> results;
    for (auto &diagnostic : diagnostics) {
        auto &result = results.emplace_back();
        int k = result.size;
        (void)k;
    }
}
";
    let session = session_with(&[("/p/a.cpp", source)]);

    let view = session.view("/p/a.cpp").expect("the file is held");
    let type_of = |offset: usize| match session.type_at(&view, as_reading(&view, offset)) {
        cpp_code_analysis::Known::Yes(known) => known.type_of,
        other => panic!("a type is expected at {offset}: {other:?}"),
    };

    // The loop variable is the element of the range, reference and all.
    let loop_variable = last_word(source, "diagnostic ");
    assert_eq!(
        type_of(loop_variable),
        "Other",
        "the loop variable is the range's element"
    );

    // And the declaration in the body is *its* initializer's type, which is `Item&`.
    assert_eq!(
        type_of(last_word(source, "result")),
        "Item&",
        "the declaration inside the body is not the loop variable"
    );
}
/// **`std::define_static_array(r)` is the static array of the range's elements** — C++26's facility whose return type
/// no fact can answer.
///
/// libstdc++ declares it `template<ranges::input_range R> consteval auto define_static_array(R&& r)` with a body built
/// out of reflection intrinsics, so the fact has no return type at all and every use of it was untyped: in
/// `C:\Users\zc\Desktop\cpp\src\reflect.hpp`, `auto members = std::define_static_array(…);` had no type, no inlay hint
/// and a hover that could only say "not known". The standard fixes the *shape* — `const T(&)[N]` — and everything but
/// the bound is answerable from the argument's own type.
#[test]
fn a_define_static_array_call_is_the_static_array_of_its_range() {
    let source = "\
namespace std {
  namespace meta { struct info {}; }
  template <class T> struct vector {};
  std::vector<std::meta::info> nonstatic_data_members_of(int);
  template <class R> consteval auto define_static_array(R&& r);
}
auto members = std::define_static_array(std::nonstatic_data_members_of(1));
";
    assert_eq!(
        type_of_use(source, "members").as_deref(),
        Some("std::meta::info[]"),
        "the range is a `vector<std::meta::info>`, so the array's element is `std::meta::info`"
    );
}

/// **A storage specifier is not part of the deduced type.** `static constexpr auto members = …` is declared with the
/// type of its initializer; keeping the words in front of `auto` made the answer `static constexpr info[]`, which the
/// hover card printed beside its own `static constexpr` (`static constexpr static constexpr info[]`) and the inlay
/// hint drew after `auto`.
#[test]
fn a_deduced_type_does_not_carry_the_storage_specifiers() {
    assert_eq!(
        type_of_use("static constexpr auto count = 1;\n", "count").as_deref(),
        Some("int")
    );
    assert_eq!(
        type_of_use("inline constexpr auto ratio = 0.5;\n", "ratio").as_deref(),
        Some("double")
    );
    // **`const` stays**, because it is part of the type: `const auto fixed = 2;` is a `const int`.
    assert_eq!(
        type_of_use("static const auto fixed = 2;\n", "fixed").as_deref(),
        Some("const int")
    );
}
/// **A function whose body returns a call or a member is typed by typing that expression** — the shape a summary
/// cannot answer, because a summary is built without the index and a call has to be resolved.
///
/// `auto make() { return make_thing(); }` is how a great deal of C++14-and-later code writes a deduced return. The
/// summary types the `return` expressions a spelling answers on its own (a literal, a name, a binary expression), so
/// these four had no return type at all and every call to them was untyped. At query time the declaring file's own
/// tree and scopes are in reach, so the expression is typed with the machinery a use in that file gets.
#[test]
fn a_function_whose_body_returns_a_call_is_typed_from_that_call() {
    // The returned expression is a call.
    let called = "\
int g(int);
auto via(int x) { return g(x); }
auto result = via(3);
";
    assert_eq!(
        type_of_use(called, "result").as_deref(),
        Some("int"),
        "`g(x)` is an `int`, so `via` returns one"
    );

    // The returned expression is a member of a parameter.
    let member = "\
struct Widget { int count; };
auto count_of(Widget w) { return w.count; }
auto result = count_of(Widget{1});
";
    assert_eq!(
        type_of_use(member, "result").as_deref(),
        Some("int"),
        "`w.count` is an `int`, so `count_of` returns one"
    );

    // A chain: the returned expression is a call to *another* function deduced the same way.
    let chained = "\
int g(int);
auto via(int x) { return g(x); }
auto twice(int x) { return via(x) + via(x); }
auto result = twice(4);
";
    assert_eq!(
        type_of_use(chained, "result").as_deref(),
        Some("int"),
        "`via` is an `int` function, so `twice` is too"
    );

    // A bare `return;` is `void`, which is the language's rule for a deduced return type.
    let bare = "\
void note() { }
auto nothing(bool b) { if (b) { return; } note(); }
auto result = nothing(true);
";
    assert_eq!(
        type_of_use(bare, "result").as_deref(),
        Some("void"),
        "a bare `return;` makes the deduced type `void`"
    );
}

/// **What the body deduction refuses**, and why refusing is the answer rather than a guess.
#[test]
fn a_body_whose_returns_disagree_is_not_typed() {
    // Two returns with two types is not a program.
    let disagreeing = "\
auto bad(bool b) { if (b) { return 1; } return 2.5; }
auto result = bad(true);
";
    assert_eq!(
        type_of_use(disagreeing, "result"),
        None,
        "`int` and `double` do not agree, so there is no deduced type to report"
    );

    // Two functions that return each other's calls are a cycle, and the walk has to end.
    let mutual = "\
auto f();
auto g() { return f(); }
auto f() { return g(); }
auto result = f();
";
    assert_eq!(
        type_of_use(mutual, "result"),
        None,
        "a cycle is refused at the recursion bound rather than walked forever"
    );
}

/// **A braced initialisation of a standard class template is answered from the standard's own guide.**
///
/// `std::tuple tuple{1, 2.5}` is `std::tuple<int, double>` — a fact the standard fixes, not one the constructors say
/// (`tuple`'s constructors are constrained variadic templates) and not one a member list can say either. This asserts
/// the answer; the *cost* of getting it is the other half and is written where the code is: the table that holds the
/// fact used to be consulted **after** `members_of`, and on a class template whose base is written in its pack that
/// walk does not terminate (`_Tuple_impl<0, _Elements...>` → `_Tuple_impl<0 + 1, _Tail...>` → … — a different spelling
/// every level, and the walk stops only on a repeated spelling).
///
/// Measured before both changes: `textDocument/inlayHint` on a nine-line file containing `std::tuple tuple{1, 2.5, s}`
/// never returned (120 s — the client's own deadline) and the server reached **2 GB**; after, the request took **8 ms**
/// and the whole `containers.cpp` request **154 ms / 132 MB**.
#[test]
fn a_braced_initialisation_of_a_standard_class_template_is_answered_without_walking_its_members() {
    let source = "\
namespace std {

template <int N, typename... Elements> struct impl;

template <int N, typename Head, typename... Tail>
struct impl<N, Head, Tail...> : impl<N + 1, Tail...> {
    Head head;
};

template <int N> struct impl<N> {};

template <typename... Elements> struct tuple : impl<0, Elements...> {
    int own;
};

}  // namespace std

void f() {
    auto made = std::tuple{1, 2.5};
}
";
    assert_eq!(
        type_of_use(source, "made").as_deref(),
        Some("std::tuple<int, double>"),
        "the standard's own guide answers the deduction, and it is consulted before any member list is built"
    );
}

/// **A member list of a class template whose base is its own pack stops, and says where it stopped.**
///
/// The same shape as the test above, asked the other way: a *member list* of the uninstantiated class, which is what a
/// hover on `std::tuple` or a member completion asks for. The walk cannot enter the base — `impl<0, Elements...>` names
/// one type per argument and this class was asked about without any — so the clause is a **gap** in `unlisted` rather
/// than a level to walk, which is the field that exists to say an answer is incomplete instead of pretending it is not.
///
/// Measured before: `members_of("std::tuple")` was still walking **ten minutes** in, holding **843 MB**, one
/// `_Head_base<0 + 1 + 1, _Head>` per level. After: **298 ms, 110 members, one named gap** on the standard library's own
/// `std::tuple`.
#[test]
fn a_member_list_of_a_class_template_whose_base_is_its_pack_stops_and_says_so() {
    let source = "\
namespace lib {

template <int N, typename... Elements> struct impl;

template <int N, typename Head, typename... Tail>
struct impl<N, Head, Tail...> : impl<N + 1, Tail...> {
    Head head;
};

template <int N> struct impl<N> {};

template <typename... Elements> struct tuple : impl<0, Elements...> {
    int own;
};

}  // namespace lib

void f() {
    lib::tuple<int, double> value{};
}
";
    let session = session_with(&[("/p/a.cpp", source)]);
    let view = session
        .view_of_the_file(std::path::Path::new("/p/a.cpp"))
        .expect("the file is held");

    let bare = session.members_of(&view, "lib::tuple");
    let cpp_code_analysis::Known::Yes(bare) = bare else {
        panic!("a class this file declares answers a member list: {bare:?}");
    };
    assert!(
        bare.members.iter().any(|member| member.fact.name == "own"),
        "the class's own members are listed: {:?}",
        bare.members
            .iter()
            .map(|member| member.fact.name.to_string())
            .collect::<Vec<_>>()
    );
    assert_eq!(
        bare.unlisted.len(),
        1,
        "the base it could not spell is named rather than silently dropped: {:?}",
        bare.unlisted
    );
    assert!(
        bare.unlisted[0].spelling.contains("Elements..."),
        "the gap names the clause, which is the only way a reader can tell what is missing: {:?}",
        bare.unlisted[0]
    );

    // **The instantiated form terminates too**, one level further: `impl<0, int, double>` can be spelled, its own base
    // (`impl<N + 1, Tail...>`) cannot — the same gap, one level down. Before the rule this walk did not terminate
    // either, so a test that returned at all is the measurement.
    let instantiated = session.members_of(&view, "lib::tuple<int, double>");
    let cpp_code_analysis::Known::Yes(instantiated) = instantiated else {
        panic!("an instantiated class this file declares answers a member list: {instantiated:?}");
    };
    assert!(
        !instantiated.members.is_empty(),
        "the members of the first base level that could be spelled are listed: {instantiated:?}"
    );
}

/// **A partial specialization's members are not the primary template's.**
///
/// Both file their facts under the **primary's name**: a member of `std::vector<bool>` has `scope: "std::vector"`,
/// because that is the class template's qualified name and a specialization has no name of its own. So a member list
/// built by "every fact whose scope is this class" answers with the union of two class bodies.
///
/// Measured on `test_cpp/src/containers.cpp:28`, `vector.begin()`: the hover card listed **four** declarations —
/// `begin()` and `begin() const` twice each, the second pair from `stl_bvector.h`. After this: `std::vector<int>` has
/// **165** members instead of 280, and `begin` is written twice, both from the primary's `stl_vector.h`.
///
/// The body a use names is the one the index's own rule picks ([`ProjectIndex::definitions`]): a definition, and among
/// several the one with the most template parameters.
#[test]
fn a_partial_specializations_members_are_not_the_primary_templates() {
    let files = [
        (
            "/p/vector.h",
            "template <typename T> struct vector {\n    T* begin();\n    const T* begin() const;\n};\n",
        ),
        (
            "/p/bvector.h",
            "template <> struct vector<bool> {\n    void begin();\n    void begin() const;\n};\n",
        ),
        (
            "/p/a.cpp",
            "#include \"vector.h\"\n#include \"bvector.h\"\nvoid f() { vector<int> v; v.begin(); }\n",
        ),
    ];
    let session = session_with(&files);
    let view = session
        .view_of_the_file(std::path::Path::new("/p/a.cpp"))
        .expect("the file is held");

    let members = session.members_of(&view, "vector<int>");
    let cpp_code_analysis::Known::Yes(members) = members else {
        panic!("a class these headers declare answers a member list: {members:?}");
    };
    let begins: Vec<(String, usize)> = members
        .members
        .iter()
        .filter(|member| member.fact.name == "begin")
        .map(|member| {
            (
                member.file.display().to_string(),
                member.fact.range.start_offset,
            )
        })
        .collect();
    assert_eq!(
        begins.len(),
        2,
        "the primary template declares `begin` twice and the specialization's two are someone else's: {begins:?}"
    );
    assert!(
        begins.iter().all(|(file, _)| file.ends_with("vector.h")),
        "and both are the primary's own body: {begins:?}"
    );
}

/// **An operator whose return type is a qualified name is still a member.**
///
/// `std::size_t operator()(std::string const& value) const` is an operator declaration whose *return type* holds a
/// `::` in the specifier sequence. `declaration_is_qualified` asks "is there a `::` in a name under the specifier
/// sequence, outside every declarator" — and answered yes, because an operator's declarator holds no *identifier*
/// (`get_name_text` looks for one), so it looked like the bare parameter list of an out-of-line definition. The walk
/// then asked whether `std::size_t` named a scope this file had built, got `false`, and bound nothing: the class had
/// no member, and `containers.cpp` had 43 declarations without its operator — which is why hovering the `operator`
/// **keyword** on line 63 showed the keyword's meaning. The pair that named the cause: `bool operator<(…)` was fine
/// and `std::size_t operator<(…)` was not.
#[test]
fn an_operators_qualified_return_type_does_not_lose_the_member() {
    let source = "\
struct Hashed {
    std::size_t operator()(std::string const& value) const;
    bool operator<(const Hashed& other) const;
};
";
    let session = session_with(&[("/p/a.cpp", source)]);
    let view = session
        .view_of_the_file(std::path::Path::new("/p/a.cpp"))
        .expect("the file is held");
    let members = session.members_of(&view, "Hashed");
    let cpp_code_analysis::Known::Yes(members) = members else {
        panic!("a class this file declares answers a member list: {members:?}");
    };
    let names: Vec<String> = members
        .members
        .iter()
        .map(|member| member.fact.name.to_string())
        .collect();
    assert!(
        names.iter().any(|name| name == "operator()"),
        "the operator with the qualified return type is a member: {names:?}"
    );
    assert!(
        names.iter().any(|name| name == "operator<"),
        "and so is the one with a plain return type: {names:?}"
    );
}

/// **The three readings a member hover and a keyword hover are built from**, asked directly.
///
/// `the_name_declared_at` is the one name no *name resolution* can read — an operator, spelled with a keyword — and
/// `the_object_is_const` is the constness of a declared object, which its *type spelling* cannot answer: `type_at`
/// says `std::string&` for a parameter declared `std::string const&`, because the spelling is a name for lookup.
#[test]
fn a_declarations_own_readings() {
    let source = "\
struct Hashed {
    std::size_t operator()(std::string const& value) const;
};

void f(std::string const& text, std::string& plain) {
    text.size();
    plain.size();
}
";
    let session = session_with(&[("/p/a.cpp", source)]);
    let view = session
        .view_of_the_file(std::path::Path::new("/p/a.cpp"))
        .expect("the file is held");

    let operator = source.find("operator()").expect("the operator is written") + 2;
    assert_eq!(
        session.the_name_declared_at(&view, operator).as_deref(),
        Some("operator()"),
        "an operator's name is composed from the keyword, not read as an identifier"
    );

    let text = source.find("text.size").expect("the const one is used");
    assert_eq!(
        session.the_object_is_const(&view, text),
        Some(true),
        "`text` is declared `std::string const&`"
    );
    let plain = source.find("plain.size").expect("the other one is used");
    assert_eq!(
        session.the_object_is_const(&view, plain),
        Some(false),
        "`plain` is declared `std::string&`, so every overload is callable"
    );
    assert_eq!(
        session.the_object_is_const(&view, source.find("nothing").unwrap_or(0)),
        None,
        "an offset that is not a name answers nothing rather than `false`"
    );
}