go-rs 0.1.2

Go on the fusevm bytecode VM — compiles to fusevm::Chunk, runs on a shared three-tier Cranelift JIT
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
//! A recursive-descent parser for the slice-1 Go grammar.
//!
//! Consumes the [`crate::lexer`] token stream — already carrying Go's
//! automatically-inserted semicolons — and builds a [`crate::ast::Program`].
//! The grammar covers a single-file `package main`: the package clause, imports,
//! `type T struct` declarations, and top-level `func` / method declarations whose
//! bodies use `:=`/`var`, assignment to lvalues (ident / index / field),
//! `if`/`else`, three-clause/condition/infinite `for`, `for … range`,
//! `return`/`break`/`continue`, and the usual expression forms including
//! composite literals (`[]T{…}`, `map[K]V{…}`, `T{…}`), indexing, `make`, and
//! `fmt.Println`-style selector calls.

use crate::ast::*;
use crate::lexer::{lex, Tok, Token};
use std::collections::{HashMap, HashSet};

/// The two kinds of `type` declaration the parser produces.
enum TypeDecl {
    Struct(StructDecl),
    Interface(InterfaceDecl),
}

/// Parse Go source into a [`Program`].
pub fn parse(src: &str) -> Result<Program, String> {
    let tokens = lex(src)?;
    // Pre-scan `type <IDENT> struct` so a `T{…}` composite literal can be told
    // apart from an identifier `T` followed by a block `{`.
    let struct_names = scan_struct_names(&tokens);
    // Pre-scan generic declarations — a `func Name[…]` / `type Name[…]` (Go 1.18
    // type parameters). go-rs is dynamically typed on the fusevm value model, so
    // generics are handled by *erasure*: the type-parameter and type-argument
    // brackets are consumed and dropped. Recording the generic names lets a use
    // site (`Name[int](…)`) tell an instantiation apart from an index `xs[i]`.
    let generic_names = scan_generic_names(&tokens);
    let mut p = Parser {
        tokens,
        pos: 0,
        struct_names,
        generic_names,
        no_composite: false,
        anon_structs: HashMap::new(),
        anon_interfaces: HashMap::new(),
        local_types: Vec::new(),
        local_interfaces: Vec::new(),
        defined: HashMap::new(),
    };
    // Pre-scan `type Name <base>` over a non-struct base, with the real type
    // grammar rather than a token pattern — the base can be any type expression
    // (`map[string][]int`, `func(int) (int, error)`). A use site can precede the
    // declaration, and `Name{…}` only parses as a composite literal if the base
    // is one, so the names have to be known before the program is walked.
    p.scan_defined_types();
    p.program()
}

struct Parser {
    tokens: Vec<Token>,
    pos: usize,
    /// Names declared as `type T struct` — enables `T{…}` composite literals.
    struct_names: HashSet<String>,
    /// Names declared generic (`func F[…]` / `type T[…]`) — enables telling an
    /// instantiation `F[int](…)` apart from an index expression `xs[i]`.
    generic_names: HashSet<String>,
    /// True while parsing a control-clause header (`if`/`for`/`switch` init and
    /// condition), where a `{` starts the body — so a bare `T{…}` / `pkg.T{…}`
    /// composite literal is suppressed (Go's `exprLev` rule; use parens to force
    /// one). Reset to `false` inside parentheses and the composite's own braces.
    no_composite: bool,
    /// Anonymous struct types encountered (`struct{…}` in a type or literal
    /// position), keyed by a canonical name synthesized from their fields, so an
    /// identical shape shares one type. Merged into the program's declared types.
    anon_structs: HashMap<String, Vec<Param>>,
    /// Anonymous interface types with a method set (`interface{ Unwrap() error }`
    /// in a type-assertion or type-switch position), keyed by the canonical name
    /// [`iface_name`] builds, so an identical method set shares one type. Merged
    /// into the program's interface declarations.
    anon_interfaces: HashMap<String, Vec<String>>,
    /// `type T struct{…}` / `type I interface{…}` written inside a function
    /// body, hoisted to the program's declarations — which is where the compiler
    /// reads every type from — as they are parsed.
    local_types: Vec<StructDecl>,
    local_interfaces: Vec<InterfaceDecl>,
    /// Defined types over a non-struct, non-interface base — `type Weekday int`,
    /// `type mySlice []int` — as name → the base type as written. A defined type
    /// is a *distinct* type in Go with the same representation as its base, so
    /// go-rs carries the name only where it is observable: `%T`, `%#v` and method
    /// dispatch.
    defined: HashMap<String, String>,
}

/// The canonical name of an interface type with method set `methods`:
/// `interface{Is;Unwrap}`, methods sorted so the written order does not matter
/// (Go's interface identity ignores it too). An empty method set is the opaque
/// `interface{}` every interface value already erases to.
fn iface_name(methods: &mut Vec<String>) -> String {
    if methods.is_empty() {
        return "interface{}".to_string();
    }
    methods.sort();
    methods.dedup();
    format!("interface{{{}}}", methods.join(";"))
}

/// The token index of the *name* in every type spec, whether written singly
/// (`type T …`) or inside a group (`type ( T … ; U … )`).
///
/// The pre-scans need both forms, and a group's later specs are not preceded by
/// the `type` keyword, so they cannot be found by looking for one.
fn type_spec_positions(tokens: &[Token]) -> Vec<usize> {
    let mut out = Vec::new();
    let mut i = 0;
    while i < tokens.len() {
        if !matches!(tokens[i].kind, Tok::Type) {
            i += 1;
            continue;
        }
        if !matches!(tokens.get(i + 1).map(|t| &t.kind), Some(Tok::LParen)) {
            if matches!(tokens.get(i + 1).map(|t| &t.kind), Some(Tok::Ident(_))) {
                out.push(i + 1);
            }
            i += 1;
            continue;
        }
        // A group: a spec starts at the first token after the `(` or after the
        // `;` that ended the previous one.
        let mut j = i + 2;
        let mut depth = 1usize;
        let mut at_spec_start = true;
        while j < tokens.len() && depth > 0 {
            match &tokens[j].kind {
                Tok::LParen | Tok::LBracket | Tok::LBrace => {
                    depth += 1;
                    at_spec_start = false;
                }
                Tok::RParen | Tok::RBracket | Tok::RBrace => {
                    depth -= 1;
                    at_spec_start = false;
                }
                Tok::Semi if depth == 1 => at_spec_start = true,
                Tok::Ident(_) if at_spec_start => {
                    out.push(j);
                    at_spec_start = false;
                }
                _ => at_spec_start = false,
            }
            j += 1;
        }
        i = j;
    }
    out
}

/// The token index just past an optional `[ … ]` generic type-parameter list at
/// `j`, or `j` itself when there is none.
fn skip_type_param_tokens(tokens: &[Token], j: usize) -> usize {
    if !matches!(tokens.get(j).map(|t| &t.kind), Some(Tok::LBracket)) {
        return j;
    }
    let mut j = j;
    let mut depth = 0usize;
    while j < tokens.len() {
        match &tokens[j].kind {
            Tok::LBracket => depth += 1,
            Tok::RBracket => {
                depth -= 1;
                if depth == 0 {
                    return j + 1;
                }
            }
            _ => {}
        }
        j += 1;
    }
    j
}

/// Collect names declared as `type Name struct` — including generic structs
/// `type Name[T any] struct`, where an optional `[ … ]` type-parameter list sits
/// between the name and `struct`. Enables `Name{…}` composite-literal parsing.
fn scan_struct_names(tokens: &[Token]) -> HashSet<String> {
    let mut names = HashSet::new();
    for p in type_spec_positions(tokens) {
        let Tok::Ident(n) = &tokens[p].kind else {
            continue;
        };
        let j = skip_type_param_tokens(tokens, p + 1);
        if matches!(tokens.get(j).map(|t| &t.kind), Some(Tok::Struct)) {
            names.insert(n.clone());
        }
    }
    names
}

/// Collect the names of generic declarations: `func Name[…]` (with or without a
/// method receiver) and `type Name[…]`. Used to erase type arguments at use
/// sites without mistaking them for indexing.
fn scan_generic_names(tokens: &[Token]) -> HashSet<String> {
    let mut names = HashSet::new();
    let kind = |i: usize| tokens.get(i).map(|t| &t.kind);
    let mut i = 0;
    while i < tokens.len() {
        match &tokens[i].kind {
            Tok::Type => {
                // `type Name [` …
                if let (Some(Tok::Ident(n)), Some(Tok::LBracket)) = (kind(i + 1), kind(i + 2)) {
                    names.insert(n.clone());
                }
            }
            Tok::Func => {
                // Skip an optional method receiver `( … )` to reach the name.
                let mut j = i + 1;
                if matches!(kind(j), Some(Tok::LParen)) {
                    let mut depth = 0;
                    while j < tokens.len() {
                        match &tokens[j].kind {
                            Tok::LParen => depth += 1,
                            Tok::RParen => {
                                depth -= 1;
                                if depth == 0 {
                                    j += 1;
                                    break;
                                }
                            }
                            _ => {}
                        }
                        j += 1;
                    }
                }
                if let (Some(Tok::Ident(n)), Some(Tok::LBracket)) = (kind(j), kind(j + 1)) {
                    names.insert(n.clone());
                }
            }
            _ => {}
        }
        i += 1;
    }
    names
}

impl Parser {
    // ── token cursor ───────────────────────────────────────────────────────

    fn peek(&self) -> &Tok {
        &self.tokens[self.pos].kind
    }

    /// The token `n` positions ahead (`0` is the current one).
    fn peek_at(&self, n: usize) -> &Tok {
        self.tokens
            .get(self.pos + n)
            .map(|t| &t.kind)
            .unwrap_or(&Tok::Eof)
    }

    fn peek2(&self) -> &Tok {
        self.tokens
            .get(self.pos + 1)
            .map(|t| &t.kind)
            .unwrap_or(&Tok::Eof)
    }

    fn line(&self) -> u32 {
        self.tokens[self.pos].line
    }

    /// The label of a `break label` / `continue label`, when one follows on the
    /// same line. Go's semicolon insertion ends the statement at the newline, so
    /// an identifier on the *next* line is a new statement and not a label.
    fn opt_label(&mut self) -> Option<String> {
        let Tok::Ident(name) = self.peek().clone() else {
            return None;
        };
        if self.tokens[self.pos].line != self.tokens[self.pos.saturating_sub(1)].line {
            return None;
        }
        self.advance();
        Some(name)
    }

    /// Whether the cursor sits on `label:` introducing a labeled statement — an
    /// identifier followed by a colon. Distinguished from `x := …` by the colon
    /// standing alone, and from a `case`/`default` label by those being their own
    /// tokens.
    fn at_label(&self) -> bool {
        matches!(self.peek(), Tok::Ident(_))
            && matches!(
                self.tokens.get(self.pos + 1).map(|t| &t.kind),
                Some(Tok::Colon)
            )
    }

    fn advance(&mut self) -> Tok {
        let t = self.tokens[self.pos].kind.clone();
        if self.pos + 1 < self.tokens.len() {
            self.pos += 1;
        }
        t
    }

    /// Consume the token if it matches `kind`; return whether it did.
    fn eat(&mut self, kind: &Tok) -> bool {
        if self.peek() == kind {
            self.advance();
            true
        } else {
            false
        }
    }

    fn expect(&mut self, kind: &Tok) -> Result<(), String> {
        if self.peek() == kind {
            self.advance();
            Ok(())
        } else {
            Err(format!(
                "go-rs: expected `{kind}`, found `{}` on line {}",
                self.peek(),
                self.line()
            ))
        }
    }

    fn ident(&mut self) -> Result<String, String> {
        match self.advance() {
            Tok::Ident(s) => Ok(s),
            other => Err(format!(
                "go-rs: expected identifier, found `{other}` on line {}",
                self.line()
            )),
        }
    }

    /// Skip any run of statement-terminating semicolons (ASI-inserted or explicit).
    fn skip_semis(&mut self) {
        while matches!(self.peek(), Tok::Semi) {
            self.advance();
        }
    }

    // ── top level ──────────────────────────────────────────────────────────

    fn program(&mut self) -> Result<Program, String> {
        self.skip_semis();
        self.expect(&Tok::Package)?;
        let package = self.ident()?;
        self.skip_semis();

        let mut imports = Vec::new();
        while matches!(self.peek(), Tok::Import) {
            self.advance();
            self.parse_import(&mut imports)?;
            self.skip_semis();
        }

        let mut types = Vec::new();
        let mut interfaces = Vec::new();
        let mut main = Vec::new();
        // Package-level `var`/`const` declarations, collected separately so
        // `func main` (which sets the main body) does not overwrite them; they
        // are prepended to the main body to run first (no separate init phase).
        let mut globals = Vec::new();
        let mut funcs = Vec::new();
        while !matches!(self.peek(), Tok::Eof) {
            match self.peek() {
                Tok::Func => {
                    let f = self.func_decl()?;
                    if f.name == "main" && f.receiver.is_none() {
                        main = f.body;
                    } else {
                        funcs.push(f);
                    }
                }
                Tok::Type => {
                    for td in self.type_decl()? {
                        match td {
                            TypeDecl::Struct(s) => types.push(s),
                            TypeDecl::Interface(i) => interfaces.push(i),
                        }
                    }
                }
                Tok::Var | Tok::Const => globals.push(self.stmt()?),
                other => {
                    return Err(format!(
                        "go-rs: expected `func` or `type` at top level, found `{other}` on line {}",
                        self.line()
                    ))
                }
            }
            self.skip_semis();
        }

        // Globals run before the main body.
        globals.extend(main);
        let main = globals;

        // Types declared inside a function body. Go scopes them to their block;
        // go-rs hoists them, which is right for everything the name is
        // observable through (`%T` prints `main.T` either way) and wrong only if
        // two functions declare *different* types under one name.
        types.append(&mut self.local_types);
        interfaces.append(&mut self.local_interfaces);
        // Register anonymous struct types (`struct{…}` used as a type or literal)
        // so the compiler can zero-fill and field-access them.
        for (name, fields) in std::mem::take(&mut self.anon_structs) {
            types.push(StructDecl { name, fields });
        }
        // Register anonymous interface types (`interface{ Unwrap() error }` in a
        // type-assertion or type-switch position) so the compiler can test a
        // value's method set against them.
        for (name, methods) in std::mem::take(&mut self.anon_interfaces) {
            interfaces.push(InterfaceDecl { name, methods });
        }

        let mut defined: Vec<(String, String)> = self.defined.drain().collect();
        defined.sort();
        Ok(Program {
            package,
            imports,
            types,
            interfaces,
            main,
            funcs,
            defined,
        })
    }

    /// Fill [`Parser::defined`] by parsing every `type Name <base>` declaration
    /// whose base is neither a struct nor an interface, ahead of the program
    /// walk. Each is parsed with [`Parser::type_decl`] itself, so the base is
    /// read by the real type grammar; a declaration that fails to parse here is
    /// left for the walk to report in source order. The position is restored,
    /// and struct/interface declarations are skipped rather than parsed twice.
    fn scan_defined_types(&mut self) {
        let saved = self.pos;
        for p in type_spec_positions(&self.tokens) {
            // `Name` then anything but `struct` / `interface`, looking past a
            // generic type-parameter list.
            let j = skip_type_param_tokens(&self.tokens, p + 1);
            if matches!(
                self.tokens.get(j).map(|t| &t.kind),
                Some(Tok::Struct) | Some(Tok::Interface) | None
            ) {
                continue;
            }
            self.pos = p;
            let _ = self.type_spec();
        }
        self.pos = saved;
    }

    /// Parse a `type` declaration: one spec, or a parenthesized group of them
    /// (`type ( A int; B struct{…} )`).
    fn type_decl(&mut self) -> Result<Vec<TypeDecl>, String> {
        self.expect(&Tok::Type)?;
        if !self.eat(&Tok::LParen) {
            return Ok(self.type_spec()?.into_iter().collect());
        }
        let mut out = Vec::new();
        self.skip_semis();
        while !matches!(self.peek(), Tok::RParen | Tok::Eof) {
            out.extend(self.type_spec()?);
            self.skip_semis();
        }
        self.expect(&Tok::RParen)?;
        Ok(out)
    }

    /// Parse one type spec — everything after the `type` keyword, or one entry
    /// of a group: `T struct { … }`, `I interface { … }`, or a defined type
    /// `T <base>`. Returns `None` for the defined type, which is recorded in
    /// [`Parser::defined`] rather than declared: it has the same representation
    /// as its base, and the name is carried only where it is observable (`%T`,
    /// `%#v`, method dispatch).
    fn type_spec(&mut self) -> Result<Option<TypeDecl>, String> {
        let name = self.ident()?;
        // Erase a generic type-parameter list: `type Stack[T any] struct{…}`.
        //
        // A `[` after the name is a type-parameter list only when what follows
        // is a parameter name. `type mySlice []int` and `type myArr [3]int`
        // open the *base* type with the same token, and eating those brackets
        // would leave the base as its element type — `mySlice` over `int`.
        if matches!(self.peek(), Tok::LBracket) && !self.brackets_open_a_type() {
            self.skip_type_brackets()?;
        }
        // A defined type over a non-struct base: `type Weekday int`,
        // `type Celsius float64`, `type ID string`. It has no fields and no
        // method set of its own, so it declares nothing the program needs beyond
        // the name — which `%T`, `%#v` and method dispatch do observe, so the
        // name and its base are recorded rather than dropped.
        if !matches!(self.peek(), Tok::Struct | Tok::Interface) {
            let base = self.type_name()?;
            self.defined.insert(name, base);
            return Ok(None);
        }
        match self.peek() {
            Tok::Interface => {
                self.advance();
                let methods = self.interface_body()?;
                Ok(Some(TypeDecl::Interface(InterfaceDecl { name, methods })))
            }
            _ => {
                self.expect(&Tok::Struct)?;
                let fields = self.struct_field_list()?;
                Ok(Some(TypeDecl::Struct(StructDecl { name, fields })))
            }
        }
    }

    /// Parse an interface body `{ … }` into its method set, each entry encoded by
    /// [`method_sig`]. The `interface` keyword is already consumed; the opening
    /// `{` is the current token.
    ///
    /// An element is either a method (`name(params) results`) or a generic
    /// type-constraint term (`~int | ~float64`, an embedded constraint name). A
    /// method is exactly `Ident (`; anything else is a constraint term, which
    /// go-rs erases — constraints contribute no methods.
    fn interface_body(&mut self) -> Result<Vec<String>, String> {
        self.expect(&Tok::LBrace)?;
        self.skip_semis();
        let mut methods = Vec::new();
        while !matches!(self.peek(), Tok::RBrace | Tok::Eof) {
            if matches!(self.peek(), Tok::Ident(_)) && matches!(self.peek2(), Tok::LParen) {
                let name = self.ident()?;
                self.expect(&Tok::LParen)?;
                let arity = self.skip_param_list()?;
                let results = self.result_types()?;
                methods.push(method_sig(&name, arity, &results));
            }
            // Skip to the end of the element (a constraint term including `~`,
            // `|`, and bracketed types — a method's results are already parsed).
            while !matches!(self.peek(), Tok::Semi | Tok::RBrace | Tok::Eof) {
                self.advance();
            }
            self.skip_semis();
        }
        self.expect(&Tok::RBrace)?;
        Ok(methods)
    }

    /// Consume an already-opened interface-method parameter list and return how
    /// many parameters it declares — the top-level comma count plus one, which is
    /// right whether the parameters are named (`a, b error`) or bare types
    /// (`error, error`). Their types are not recorded (see [`method_sig`]).
    fn skip_param_list(&mut self) -> Result<usize, String> {
        let mut depth = 1;
        let mut commas = 0;
        let mut empty = true;
        loop {
            match self.advance() {
                Tok::LParen | Tok::LBracket | Tok::LBrace => depth += 1,
                Tok::RBracket | Tok::RBrace => depth -= 1,
                Tok::RParen => {
                    depth -= 1;
                    if depth == 0 {
                        break;
                    }
                }
                Tok::Comma if depth == 1 => {
                    commas += 1;
                    empty = false;
                }
                Tok::Eof => return Err("go-rs: unterminated `(` in interface method".to_string()),
                _ => empty = false,
            }
        }
        Ok(if empty { 0 } else { commas + 1 })
    }

    /// Parse an interface method's result list: nothing, one type, or a
    /// parenthesized `(T, U)` list.
    fn result_types(&mut self) -> Result<Vec<String>, String> {
        if matches!(self.peek(), Tok::Semi | Tok::RBrace | Tok::Eof) {
            return Ok(Vec::new());
        }
        // An interface method's results are a function's results — including the
        // *named* form Go's own stdlib writes them in (`Write(p []byte) (n int,
        // err error)`). Reading them as bare types made the name a type and the
        // type a syntax error, which is what stopped `io` from being vendored.
        // The names carry no meaning in a method set, so they are dropped here.
        Ok(self.results()?.into_iter().map(|(_, ty)| ty).collect())
    }

    /// Parse a `{ field-decls }` struct body into its fields. The `struct`
    /// keyword is already consumed; the opening `{` is the current token.
    fn struct_field_list(&mut self) -> Result<Vec<Param>, String> {
        self.expect(&Tok::LBrace)?;
        self.skip_semis();
        let mut fields = Vec::new();
        while !matches!(self.peek(), Tok::RBrace | Tok::Eof) {
            // An embedded field is a bare type with no name: `struct { Base }`.
            // Its field name is the type's own name (the last component of a
            // qualified one), which is what promotion later reads through.
            if let Some(ty) = self.embedded_field()? {
                let name = ty.rsplit('.').next().unwrap_or(&ty).to_string();
                fields.push(Param { name, ty });
                self.skip_semis();
                continue;
            }
            // One or more field names sharing a type: `x, y int`.
            let mut names = vec![self.ident()?];
            while self.eat(&Tok::Comma) {
                names.push(self.ident()?);
            }
            let ty = self.type_name()?;
            for n in names {
                fields.push(Param {
                    name: n,
                    ty: ty.clone(),
                });
            }
            self.skip_semis();
        }
        self.expect(&Tok::RBrace)?;
        Ok(fields)
    }

    /// An embedded field at the current position — `Base`, `pkg.Base`, or
    /// `*Base` with no field name — consumed and returned as its type text.
    /// Returns `None` (consuming nothing) if this is an ordinary named field.
    fn embedded_field(&mut self) -> Result<Option<String>, String> {
        // `*T` can only be an embedded pointer here; a named field would have
        // its name first.
        let star = matches!(self.peek(), Tok::Star);
        let base = if star { 1 } else { 0 };
        let Tok::Ident(_) = self.peek_at(base) else {
            return Ok(None);
        };
        // A qualified type takes two more tokens (`.` `Ident`); the field ends
        // where the type ends, so what follows must close the declaration.
        let qualified = matches!(self.peek_at(base + 1), Tok::Dot);
        let after = if qualified { base + 3 } else { base + 1 };
        if !matches!(self.peek_at(after), Tok::Semi | Tok::RBrace | Tok::Str(_)) {
            return Ok(None);
        }
        if star {
            self.advance();
        }
        let mut ty = self.ident()?;
        if qualified {
            self.expect(&Tok::Dot)?;
            ty.push('.');
            ty.push_str(&self.ident()?);
        }
        // A field tag (`json:"…"`) is accepted and ignored.
        if matches!(self.peek(), Tok::Str(_)) {
            self.advance();
        }
        Ok(Some(ty))
    }

    /// Parse an anonymous `struct{ … }` type (the `struct` keyword is the current
    /// token), register it under a canonical name synthesized from its fields,
    /// and return that name. Identical shapes share one type.
    fn anon_struct_type(&mut self) -> Result<String, String> {
        self.expect(&Tok::Struct)?;
        let fields = self.struct_field_list()?;
        let inner = fields
            .iter()
            .map(|f| format!("{} {}", f.name, f.ty))
            .collect::<Vec<_>>()
            .join("; ");
        let name = format!("struct{{{inner}}}");
        self.anon_structs.entry(name.clone()).or_insert(fields);
        Ok(name)
    }

    /// Consume tokens through the closing `)` of an already-opened paren group.
    fn skip_balanced_parens(&mut self) -> Result<(), String> {
        let mut depth = 1;
        while depth > 0 {
            match self.advance() {
                Tok::LParen => depth += 1,
                Tok::RParen => depth -= 1,
                Tok::Eof => return Err("go-rs: unterminated `(` in interface method".to_string()),
                _ => {}
            }
        }
        Ok(())
    }

    /// Consume a `[ … ]` bracket group (type parameters `[T any, U comparable]`
    /// or type arguments `[int, string]`) and discard it — generics are erased.
    /// The opening `[` must be the current token.
    /// The token immediately after the `[ … ]` bracket group that starts at the
    /// current position (which must be `[`), or `None` if unbalanced. Used to
    /// tell a generic instantiation (`f[T](` / `T[U]{`) from an index (`xs[i]`).
    fn bracket_group_next(&self) -> Option<&Tok> {
        let mut i = self.pos;
        let mut depth = 0;
        loop {
            match self.tokens.get(i).map(|t| &t.kind)? {
                Tok::LBracket => depth += 1,
                Tok::RBracket => {
                    depth -= 1;
                    if depth == 0 {
                        return self.tokens.get(i + 1).map(|t| &t.kind);
                    }
                }
                Tok::Eof => return None,
                _ => {}
            }
            i += 1;
        }
    }

    fn skip_type_brackets(&mut self) -> Result<(), String> {
        self.expect(&Tok::LBracket)?;
        let mut depth = 1;
        while depth > 0 {
            match self.advance() {
                Tok::LBracket => depth += 1,
                Tok::RBracket => depth -= 1,
                Tok::Eof => return Err("go-rs: unterminated `[` in type parameters".to_string()),
                _ => {}
            }
        }
        Ok(())
    }

    fn parse_import(&mut self, imports: &mut Vec<String>) -> Result<(), String> {
        // `import "path"` or `import ( "a"; "b" )`.
        if self.eat(&Tok::LParen) {
            self.skip_semis();
            while !matches!(self.peek(), Tok::RParen) {
                imports.push(self.import_path()?);
                self.skip_semis();
            }
            self.expect(&Tok::RParen)?;
        } else {
            imports.push(self.import_path()?);
        }
        Ok(())
    }

    fn import_path(&mut self) -> Result<String, String> {
        // Slice 1 ignores import aliases; a leading alias ident is consumed.
        if matches!(self.peek(), Tok::Ident(_)) {
            self.advance();
        }
        match self.advance() {
            Tok::Str(s) => Ok(s),
            other => Err(format!(
                "go-rs: expected import path string, found `{other}` on line {}",
                self.line()
            )),
        }
    }

    fn func_decl(&mut self) -> Result<Func, String> {
        let line = self.line();
        self.expect(&Tok::Func)?;
        // Optional method receiver: `func (r T) name(...)`, or the unnamed form
        // `func (T) name(...)` / `func (*T) name(...)` a method that ignores its
        // receiver may write. An unnamed receiver gets a placeholder name the
        // body cannot refer to.
        let receiver = if matches!(self.peek(), Tok::LParen) {
            self.advance();
            // The receiver is unnamed when the type is all that stands before
            // the `)`: `*T`, `T`, or `pkg.T`.
            let unnamed = match self.peek() {
                Tok::Star => true,
                Tok::Ident(_) => match self.peek2() {
                    Tok::RParen => true,
                    Tok::Dot => matches!(self.peek_at(3), Tok::RParen),
                    _ => false,
                },
                _ => false,
            };
            let rname = if unnamed {
                "$recv".to_string()
            } else {
                self.ident()?
            };
            let rty = self.type_name()?;
            self.expect(&Tok::RParen)?;
            Some(Param {
                name: rname,
                ty: rty,
            })
        } else {
            None
        };
        let name = self.ident()?;
        // Erase a generic type-parameter list: `func F[T any](…)`.
        if matches!(self.peek(), Tok::LBracket) {
            self.skip_type_brackets()?;
        }
        self.expect(&Tok::LParen)?;
        let (params, variadic) = self.params()?;
        self.expect(&Tok::RParen)?;
        let (result_names, results): (Vec<String>, Vec<String>) =
            self.results()?.into_iter().unzip();
        let body = self.block()?;
        Ok(Func {
            name,
            receiver,
            params,
            variadic,
            results,
            result_names,
            body,
            line,
        })
    }

    /// Parse a parameter list. Supports grouped parameters that share a type
    /// (`a, b int`): names without a trailing type inherit the next typed
    /// group's type, matching Go's rule.
    fn params(&mut self) -> Result<(Vec<Param>, bool), String> {
        let mut params = Vec::new();
        let mut variadic = false;
        if matches!(self.peek(), Tok::RParen) {
            return Ok((params, variadic));
        }
        // Collect (name, optional-type) entries, then back-fill inherited types.
        let mut pending: Vec<(String, Option<String>)> = Vec::new();
        loop {
            let name = self.ident()?;
            // `name ...T` — a variadic parameter; `ty` is the element type.
            let ty = if matches!(self.peek(), Tok::Ellipsis) {
                self.advance();
                variadic = true;
                Some(self.type_name()?)
            } else if self.type_starts() {
                Some(self.type_name()?)
            } else {
                None
            };
            let had_type = ty.is_some();
            pending.push((name, ty));
            if had_type {
                // Flush this group: everyone pending shares the just-read type.
                let t = pending.last().unwrap().1.clone().unwrap();
                for (n, slot) in pending.drain(..) {
                    params.push(Param {
                        name: n,
                        ty: slot.unwrap_or_else(|| t.clone()),
                    });
                }
            }
            if !self.eat(&Tok::Comma) {
                break;
            }
        }
        // Any trailing un-typed names are an error in real Go; slice 1 defaults
        // them to a dynamic type rather than rejecting.
        for (n, slot) in pending {
            params.push(Param {
                name: n,
                ty: slot.unwrap_or_else(|| "any".to_string()),
            });
        }
        Ok((params, variadic))
    }

    /// Parse a function result signature: nothing, a single type, or a
    /// parenthesized list. Each result is `(name, type)`; `name` is `""` for an
    /// unnamed result. A parenthesized list is *named* iff any identifier in it is
    /// immediately followed by a type (Go requires all-or-none).
    fn results(&mut self) -> Result<Vec<(String, String)>, String> {
        if matches!(self.peek(), Tok::LBrace) {
            return Ok(Vec::new());
        }
        if self.eat(&Tok::LParen) {
            if self.paren_list_is_named() {
                // Named results share the grammar of a parameter list.
                let (params, _) = self.params()?;
                self.expect(&Tok::RParen)?;
                return Ok(params.into_iter().map(|p| (p.name, p.ty)).collect());
            }
            let mut out = Vec::new();
            while !matches!(self.peek(), Tok::RParen) {
                out.push((String::new(), self.type_name()?));
                if !self.eat(&Tok::Comma) {
                    break;
                }
            }
            self.expect(&Tok::RParen)?;
            Ok(out)
        } else {
            Ok(vec![(String::new(), self.type_name()?)])
        }
    }

    /// Whether the parenthesized list starting at the current position (just after
    /// its `(`) is a *named* list: some `Ident` is immediately followed by a token
    /// that begins a type (so the ident is a name, not a type).
    fn paren_list_is_named(&self) -> bool {
        let mut i = self.pos;
        let mut depth = 1;
        while i < self.tokens.len() {
            match &self.tokens[i].kind {
                Tok::LParen | Tok::LBracket => depth += 1,
                Tok::RParen if depth == 1 => break,
                Tok::RParen | Tok::RBracket => depth -= 1,
                // `map` is a predeclared identifier, not a keyword, so a
                // `map[K]V` *type* looks like a name followed by a type start.
                // Skipping it is what keeps `(map[string]int, error)` an
                // unnamed result list.
                Tok::Ident(n) if n == "map" => {}
                Tok::Ident(_) if depth == 1 && self.type_starts_at(i + 1) => return true,
                _ => {}
            }
            i += 1;
        }
        false
    }

    /// True if the current token can begin a type in a parameter/result position.
    fn type_starts(&self) -> bool {
        self.type_starts_at(self.pos)
    }

    fn type_starts_at(&self, pos: usize) -> bool {
        matches!(
            self.tokens.get(pos).map(|t| &t.kind),
            Some(Tok::Ident(_))
                | Some(Tok::LBracket)
                | Some(Tok::Star)
                | Some(Tok::Chan)
                | Some(Tok::Func)
                | Some(Tok::Struct)
                | Some(Tok::Interface)
        )
    }

    /// Parse a type name. Handles named types (`int`, `string`, `T`, …), slices
    /// (`[]T`), maps (`map[K]V`), and pointers (`*T`) as opaque strings for later
    /// typing.
    fn type_name(&mut self) -> Result<String, String> {
        match self.peek() {
            Tok::LBracket => {
                self.advance();
                // A fixed-size array type `[N]T` keeps its length: `[N]T` is a
                // *value* type in Go and `[]T` a reference one, and the written
                // length is the only thing that tells them apart downstream. A
                // length go-rs cannot fold to a constant (`[...]T` in a type
                // position, or `[n]T` for an `n` this parser does not evaluate)
                // has no name to keep, so it falls back to `[]T` — the older,
                // reference-typed behaviour rather than a wrong length.
                let mut len = None;
                if !matches!(self.peek(), Tok::RBracket) {
                    if matches!(self.peek(), Tok::Ellipsis) {
                        self.advance();
                    } else {
                        len = const_int_of(&self.expr()?).filter(|n| *n >= 0);
                    }
                }
                self.expect(&Tok::RBracket)?;
                let elem = self.type_name()?;
                Ok(match len {
                    Some(n) => format!("[{n}]{elem}"),
                    None => format!("[]{elem}"),
                })
            }
            Tok::Star => {
                self.advance();
                Ok(format!("*{}", self.type_name()?))
            }
            // `map[K]V` — `map` is a predeclared ident, not a keyword.
            Tok::Ident(n) if n == "map" => {
                self.advance();
                self.expect(&Tok::LBracket)?;
                let k = self.type_name()?;
                self.expect(&Tok::RBracket)?;
                let v = self.type_name()?;
                Ok(format!("map[{k}]{v}"))
            }
            // `chan T` — a channel type.
            Tok::Chan => {
                self.advance();
                Ok(format!("chan {}", self.type_name()?))
            }
            // `struct{ … }` — an anonymous struct type (a field type, a
            // `map[K]struct{…}` value, a `chan struct{}` element, …).
            Tok::Struct => self.anon_struct_type(),
            // An inline interface type — `interface{}` (the empty interface, so
            // `var x interface{}` / `[]interface{}` / an `interface{}`
            // parameter), or a method set (`interface{ Unwrap() error }`, how
            // `errors.Is`/`As` walk a wrap chain). An empty method set erases to
            // the one opaque `interface{}` name every interface value carries; a
            // non-empty one is registered under its canonical name so a type
            // assertion against it can test the value's method set.
            Tok::Interface => {
                self.advance();
                let mut methods = self.interface_body()?;
                let name = iface_name(&mut methods);
                if !methods.is_empty() {
                    self.anon_interfaces.entry(name.clone()).or_insert(methods);
                }
                Ok(name)
            }
            // `func(params) results` — a function type (for function-typed
            // parameters/fields). Consumed structurally; go-rs treats every
            // function value uniformly, so only the `func` tag is retained.
            Tok::Func => {
                self.advance();
                self.expect(&Tok::LParen)?;
                self.skip_balanced_parens()?;
                // Optional results: a single type or a `( … )` list, ending
                // before the next `,`/`)`/`{`/`;`.
                if matches!(self.peek(), Tok::LParen) {
                    self.advance();
                    self.skip_balanced_parens()?;
                } else if self.type_starts() {
                    let _ = self.type_name()?;
                }
                Ok("func".to_string())
            }
            Tok::Ident(_) => {
                let mut name = self.ident()?;
                // A qualified type from an imported package: `pkg.Type`. The
                // linker rewrites the reference to the merged `pkg.Type` name.
                if matches!(self.peek(), Tok::Dot) {
                    self.advance();
                    let field = self.ident()?;
                    name = format!("{name}.{field}");
                }
                // Erase type arguments on a generic type reference: the base name
                // of `Stack[int]` / `Pair[K, V]` is what go-rs types against.
                if matches!(self.peek(), Tok::LBracket)
                    && (self.generic_names.contains(&name) || name.contains('.'))
                {
                    self.skip_type_brackets()?;
                }
                Ok(name)
            }
            other => Err(format!(
                "go-rs: expected type, found `{other}` on line {}",
                self.line()
            )),
        }
    }

    // ── statements ─────────────────────────────────────────────────────────

    fn block(&mut self) -> Result<Vec<Stmt>, String> {
        self.expect(&Tok::LBrace)?;
        self.skip_semis();
        let mut stmts = Vec::new();
        while !matches!(self.peek(), Tok::RBrace | Tok::Eof) {
            stmts.push(self.stmt()?);
            self.skip_semis();
        }
        self.expect(&Tok::RBrace)?;
        Ok(stmts)
    }

    fn stmt(&mut self) -> Result<Stmt, String> {
        // `label:` before a loop or `switch` — the target a labeled
        // `break`/`continue` names. The label rides on the statement it
        // introduces rather than becoming a statement of its own, because that
        // is the only thing it can attach to.
        if self.at_label() {
            let Tok::Ident(label) = self.advance() else {
                unreachable!("at_label checked the token")
            };
            self.advance(); // `:`
            self.skip_semis();
            let mut inner = self.stmt()?;
            if !set_stmt_label(&mut inner, &label) {
                return Err(format!(
                    "go-rs: label `{label}` must introduce a `for` or `switch` (line {})",
                    self.line()
                ));
            }
            return Ok(inner);
        }
        match self.peek() {
            Tok::Var => self.var_stmt(),
            Tok::Const => self.const_stmt(),
            // `type T …` in a function body. It declares no run-time work, so it
            // lowers to nothing; the declaration itself is hoisted to the
            // program (a defined type goes into `defined` inside `type_decl`).
            Tok::Type => {
                for td in self.type_decl()? {
                    match td {
                        TypeDecl::Struct(s) => self.local_types.push(s),
                        TypeDecl::Interface(i) => self.local_interfaces.push(i),
                    }
                }
                Ok(Stmt::Block(Vec::new()))
            }
            Tok::Return => {
                let line = self.line();
                self.advance();
                let mut vals = Vec::new();
                if !matches!(self.peek(), Tok::Semi | Tok::RBrace) {
                    vals.push(self.expr()?);
                    while self.eat(&Tok::Comma) {
                        vals.push(self.expr()?);
                    }
                }
                Ok(Stmt::Return(vals, line))
            }
            Tok::Break => {
                let line = self.line();
                self.advance();
                Ok(Stmt::Break(line, self.opt_label()))
            }
            Tok::Continue => {
                let line = self.line();
                self.advance();
                Ok(Stmt::Continue(line, self.opt_label()))
            }
            Tok::Fallthrough => {
                let line = self.line();
                self.advance();
                Ok(Stmt::Fallthrough(line))
            }
            Tok::If => self.if_stmt(),
            Tok::For => self.for_stmt(),
            Tok::Go => {
                let line = self.line();
                self.advance();
                let call = self.expr()?;
                Ok(Stmt::Go { call, line })
            }
            Tok::Defer => {
                let line = self.line();
                self.advance();
                let call = self.expr()?;
                Ok(Stmt::Defer { call, line })
            }
            Tok::Select => self.select_stmt(),
            Tok::Switch => self.switch_stmt(),
            Tok::LBrace => Ok(Stmt::Block(self.block()?)),
            _ => self.simple_stmt(),
        }
    }

    fn var_stmt(&mut self) -> Result<Stmt, String> {
        let line = self.line();
        self.expect(&Tok::Var)?;
        // Grouped form `var ( … )` → a block of single declarations.
        if self.eat(&Tok::LParen) {
            self.skip_semis();
            let mut decls = Vec::new();
            while !matches!(self.peek(), Tok::RParen | Tok::Eof) {
                decls.push(self.var_spec(line)?);
                self.skip_semis();
            }
            self.expect(&Tok::RParen)?;
            return Ok(Stmt::Block(decls));
        }
        self.var_spec(line)
    }

    /// One `name {, name} [type] [= init {, init}]` variable specification.
    ///
    /// Go binds a whole name list per spec. A multi-name spec desugars to one
    /// `Stmt::Var` per name (so each keeps the written type, which drives the
    /// fixed-width narrowing and float-conversion paths), except for the
    /// `var a, b = f()` form — a single multi-value right-hand side — which
    /// reuses the `Stmt::Short` destructuring already built for `a, b := f()`.
    fn var_spec(&mut self, line: u32) -> Result<Stmt, String> {
        let names = self.ident_list()?;
        if names.len() > 1 {
            return self.var_spec_multi(names, line);
        }
        let name = names.into_iter().next().expect("ident_list is non-empty");
        let (ty, array_len) = self.var_decl_type()?;
        let init = if self.eat(&Tok::Assign) {
            Some(self.expr()?)
        } else {
            self.bare_array_init(&ty, array_len)
        };
        Ok(Stmt::Var {
            name,
            ty,
            init,
            line,
        })
    }

    /// The `[type]` of a variable specification. A fixed-size array type `[N]T`
    /// is captured specially — its length is returned alongside — so a bare
    /// `var x [N]T` (no initializer) can be zero-filled to N elements: go-rs
    /// models arrays as slices, and the erased size would otherwise be lost.
    fn var_decl_type(&mut self) -> Result<(Option<String>, Option<usize>), String> {
        if !self.type_starts() || matches!(self.peek(), Tok::Assign) {
            return Ok((None, None));
        }
        if matches!(self.peek(), Tok::LBracket) && !matches!(self.peek2(), Tok::RBracket) {
            self.advance(); // `[`
            let array_len = if matches!(self.peek(), Tok::Ellipsis) {
                self.advance();
                None
            } else {
                const_int_of(&self.expr()?)
                    .filter(|n| *n >= 0)
                    .map(|n| n as usize)
            };
            self.expect(&Tok::RBracket)?;
            let elem = self.type_name()?;
            // The written type keeps the length, so the variable's static type
            // is the array value type `[N]T` and every copy site fires for it.
            return Ok((
                Some(match array_len {
                    Some(n) => format!("[{n}]{elem}"),
                    None => format!("[]{elem}"),
                }),
                array_len,
            ));
        }
        Ok((Some(self.type_name()?), None))
    }

    /// Bare `var x [N]T` → an N-element array of the element zero value.
    fn bare_array_init(&self, ty: &Option<String>, array_len: Option<usize>) -> Option<Expr> {
        let (len, t) = (array_len?, ty.as_ref()?);
        let elem = array_elem_ty(t)
            .or_else(|| t.strip_prefix("[]"))
            .unwrap_or(t)
            .to_string();
        Some(Expr::Make {
            is_map: false,
            elem_zero: Box::new(self.zero_value_expr(&elem)),
            elem_ty: elem,
            len: Some(Box::new(Expr::Int(len as i64))),
            cap: None,
        })
    }

    /// The zero-value expression for a written Go type — the one place that
    /// decision is made, so a `make` fill, an array-literal gap and an elided
    /// composite all agree.
    ///
    /// A struct zero is a zero-valued struct (not `0`, which the first `s[i].f =
    /// v` would nil-dereference), a slice or map zero is that type's empty
    /// literal (which prints and measures as empty rather than as the scalar
    /// zero), a fixed-size array zero is an N-element array of its own element's
    /// zero, and everything else is the scalar zero.
    fn zero_value_expr(&self, ty: &str) -> Expr {
        // A defined type's zero value is its base's: `var s mySlice` is a nil
        // slice that prints `[]`, not the scalar zero. (`type` cycles are
        // rejected by Go, so following the chain terminates.)
        if let Some(base) = self.defined.get(ty) {
            if base != ty {
                return self.zero_value_expr(&base.clone());
            }
        }
        if let (Some(elem), Some(n)) = (array_elem_ty(ty), array_len_of(ty)) {
            return Expr::SliceLit {
                elems: (0..n).map(|_| self.zero_value_expr(elem)).collect(),
                elem_ty: elem.to_string(),
                array_len: Some(n),
            };
        }
        if let Some(inner) = ty.strip_prefix("[]") {
            return Expr::SliceLit {
                elem_ty: inner.to_string(),
                elems: Vec::new(),
                array_len: None,
            };
        }
        if let Some((key_ty, val_ty)) = split_map_type(ty) {
            return Expr::MapLit {
                key_ty,
                val_ty,
                pairs: Vec::new(),
            };
        }
        if self.struct_names.contains(ty) {
            return Expr::StructLit {
                type_name: ty.to_string(),
                fields: Vec::new(),
            };
        }
        zero_expr(ty)
    }

    /// `var a, b [T] [= e, f]` — a specification binding several names.
    fn var_spec_multi(&mut self, names: Vec<String>, line: u32) -> Result<Stmt, String> {
        let (ty, array_len) = self.var_decl_type()?;
        let mut values = Vec::new();
        if self.eat(&Tok::Assign) {
            values.push(self.expr()?);
            while self.eat(&Tok::Comma) {
                values.push(self.expr()?);
            }
        }
        // `var a, b = f()` — one multi-value right-hand side destructured over
        // the whole name list, exactly like `a, b := f()`.
        if values.len() == 1 && names.len() > 1 {
            return Ok(Stmt::Short {
                names,
                values,
                line,
            });
        }
        if !values.is_empty() && values.len() != names.len() {
            return Err(format!(
                "go-rs: assignment mismatch: {} variables but {} values on line {line}",
                names.len(),
                values.len()
            ));
        }
        let mut values = values.into_iter();
        let decls = names
            .into_iter()
            .map(|name| {
                let init = match values.next() {
                    Some(e) => Some(e),
                    None => self.bare_array_init(&ty, array_len),
                };
                Stmt::Var {
                    name,
                    ty: ty.clone(),
                    init,
                    line,
                }
            })
            .collect();
        Ok(Stmt::Block(decls))
    }

    /// `const NAME [type] = expr` or a grouped `const ( … )` block with `iota`.
    /// A constant is modeled as an (immutable-by-convention) `var`; `iota` is the
    /// spec's index within the block, substituted into the expression, and a spec
    /// with no `= …` inherits the previous spec's expression list.
    fn const_stmt(&mut self) -> Result<Stmt, String> {
        let line = self.line();
        self.expect(&Tok::Const)?;
        if self.eat(&Tok::LParen) {
            self.skip_semis();
            let mut decls = Vec::new();
            let mut prev: Option<(Option<String>, Vec<Expr>)> = None;
            let mut idx: i64 = 0;
            while !matches!(self.peek(), Tok::RParen | Tok::Eof) {
                let names = self.ident_list()?;
                let ty = if self.type_starts() && !matches!(self.peek(), Tok::Assign) {
                    Some(self.type_name()?)
                } else {
                    None
                };
                let (ty, exprs) = if self.eat(&Tok::Assign) {
                    let mut es = vec![self.expr()?];
                    while self.eat(&Tok::Comma) {
                        es.push(self.expr()?);
                    }
                    (ty, es)
                } else {
                    // Inherit the previous spec's type + expressions (Go's `iota`
                    // repetition rule); `iota` is re-substituted with this index.
                    match &prev {
                        Some((pty, pes)) => (pty.clone(), pes.clone()),
                        None => return Err(format!("go-rs: missing const value on line {line}")),
                    }
                };
                for (name, expr) in names.iter().zip(&exprs) {
                    decls.push(Stmt::Var {
                        name: name.clone(),
                        ty: ty.clone(),
                        init: Some(subst_iota(expr.clone(), idx)),
                        line,
                    });
                }
                prev = Some((ty, exprs));
                idx += 1;
                self.skip_semis();
            }
            self.expect(&Tok::RParen)?;
            return Ok(Stmt::Block(decls));
        }
        // Single const: `const NAME [type] = expr` (iota == 0).
        let name = self.ident()?;
        let ty = if self.type_starts() && !matches!(self.peek(), Tok::Assign) {
            Some(self.type_name()?)
        } else {
            None
        };
        self.expect(&Tok::Assign)?;
        let init = subst_iota(self.expr()?, 0);
        Ok(Stmt::Var {
            name,
            ty,
            init: Some(init),
            line,
        })
    }

    /// Parse `ident {, ident}` — a comma-separated name list.
    fn ident_list(&mut self) -> Result<Vec<String>, String> {
        let mut names = vec![self.ident()?];
        while self.eat(&Tok::Comma) {
            names.push(self.ident()?);
        }
        Ok(names)
    }

    fn if_stmt(&mut self) -> Result<Stmt, String> {
        let line = self.line();
        self.expect(&Tok::If)?;
        // A `{` in the header starts the body, so suppress bare composite
        // literals while parsing the init/condition.
        let saved = self.no_composite;
        self.no_composite = true;
        // Optional init clause: `if x := f(); cond { … }`.
        let mut init = None;
        let first = self.simple_stmt()?;
        let cond = if self.eat(&Tok::Semi) {
            init = Some(Box::new(first));
            self.expr()?
        } else {
            stmt_into_expr(first, line)?
        };
        self.no_composite = saved;
        let then = self.block()?;
        let els = if self.eat(&Tok::Else) {
            if matches!(self.peek(), Tok::If) {
                vec![self.if_stmt()?]
            } else {
                self.block()?
            }
        } else {
            Vec::new()
        };
        Ok(Stmt::If {
            init,
            cond,
            then,
            els,
            line,
        })
    }

    fn for_stmt(&mut self) -> Result<Stmt, String> {
        let line = self.line();
        self.expect(&Tok::For)?;

        // `for { … }` — infinite loop.
        if matches!(self.peek(), Tok::LBrace) {
            let body = self.block()?;
            return Ok(Stmt::For {
                label: None,
                init: None,
                cond: None,
                post: None,
                body,
                line,
            });
        }

        // `for [k[, v] :=|= ] range iter { … }` — detected by a `range` keyword
        // in the header (before the block `{`).
        if self.header_has_range() {
            return self.for_range(line);
        }

        // Otherwise a condition-only or three-clause header. Suppress bare
        // composite literals while parsing the header (a `{` starts the body).
        let saved = self.no_composite;
        self.no_composite = true;
        let first = if matches!(self.peek(), Tok::Semi) {
            None
        } else {
            Some(self.simple_stmt()?)
        };

        if self.eat(&Tok::Semi) {
            // Three-clause: `for init; cond; post { … }`.
            let cond = if matches!(self.peek(), Tok::Semi) {
                None
            } else {
                Some(self.expr()?)
            };
            self.expect(&Tok::Semi)?;
            let post = if matches!(self.peek(), Tok::LBrace) {
                None
            } else {
                Some(Box::new(self.simple_stmt()?))
            };
            self.no_composite = saved;
            let body = self.block()?;
            Ok(Stmt::For {
                label: None,
                init: first.map(Box::new),
                cond,
                post,
                body,
                line,
            })
        } else {
            // Condition-only: `for cond { … }`.
            let cond = match first {
                Some(s) => Some(stmt_into_expr(s, line)?),
                None => None,
            };
            self.no_composite = saved;
            let body = self.block()?;
            Ok(Stmt::For {
                label: None,
                init: None,
                cond,
                post: None,
                body,
                line,
            })
        }
    }

    /// True if a `range` keyword appears in the current `for` header (before the
    /// block-opening `{`).
    fn header_has_range(&self) -> bool {
        let mut i = self.pos;
        while let Some(t) = self.tokens.get(i) {
            match t.kind {
                Tok::Range => return true,
                Tok::LBrace | Tok::Semi | Tok::Eof => return false,
                _ => i += 1,
            }
        }
        false
    }

    /// Parse a `for … range` loop header and body.
    fn for_range(&mut self, line: u32) -> Result<Stmt, String> {
        let mut key = None;
        let mut val = None;
        let mut define = false;
        if !matches!(self.peek(), Tok::Range) {
            // `k` or `k, v` then `:=` or `=`.
            let mut names = vec![self.range_name()?];
            if self.eat(&Tok::Comma) {
                names.push(self.range_name()?);
            }
            if self.eat(&Tok::Define) {
                define = true;
            } else if self.eat(&Tok::Assign) {
                define = false;
            } else {
                return Err(format!(
                    "go-rs: expected `:=` or `=` in range clause on line {}",
                    self.line()
                ));
            }
            key = names.first().cloned().flatten();
            val = names.get(1).cloned().flatten();
        }
        self.expect(&Tok::Range)?;
        // The `{` after the range expression opens the body, so suppress bare
        // composite literals while parsing the iterated expression.
        let saved = self.no_composite;
        self.no_composite = true;
        let iter = self.expr()?;
        self.no_composite = saved;
        let body = self.block()?;
        Ok(Stmt::ForRange {
            label: None,
            key,
            val,
            define,
            iter,
            body,
            line,
        })
    }

    /// A range key/value name: an identifier, or `None` for the blank `_`.
    fn range_name(&mut self) -> Result<Option<String>, String> {
        let n = self.ident()?;
        Ok(if n == "_" { None } else { Some(n) })
    }

    /// Parse a `select { case …: …; default: … }` statement.
    fn select_stmt(&mut self) -> Result<Stmt, String> {
        let line = self.line();
        self.expect(&Tok::Select)?;
        self.expect(&Tok::LBrace)?;
        self.skip_semis();
        let mut cases = Vec::new();
        let mut default = None;
        while !matches!(self.peek(), Tok::RBrace | Tok::Eof) {
            let ident_kw = matches!(self.peek(), Tok::Ident(s) if s == "default");
            if ident_kw {
                self.advance();
                self.expect(&Tok::Colon)?;
                default = Some(self.case_body()?);
                continue;
            }
            // `case COMM:`
            if !matches!(self.peek(), Tok::Ident(s) if s == "case") {
                return Err(format!(
                    "go-rs: expected `case` or `default` in select on line {}",
                    self.line()
                ));
            }
            self.advance(); // `case`
            let comm = self.select_comm()?;
            self.expect(&Tok::Colon)?;
            let body = self.case_body()?;
            cases.push(SelectClause { comm, body });
        }
        self.expect(&Tok::RBrace)?;
        Ok(Stmt::Select {
            cases,
            default,
            line,
        })
    }

    fn switch_stmt(&mut self) -> Result<Stmt, String> {
        let line = self.line();
        self.expect(&Tok::Switch)?;

        // Optional `init;` then the guard: a tag expression, or a type-switch
        // guard `[v :=] x.(type)`. A `{` in the header opens the case block, so
        // suppress bare composite literals while parsing it.
        let saved = self.no_composite;
        self.no_composite = true;
        let mut init = None;
        let mut guard: Option<Stmt> = None;
        if !matches!(self.peek(), Tok::LBrace) {
            let first = self.simple_stmt()?;
            if self.eat(&Tok::Semi) {
                init = Some(Box::new(first));
                if !matches!(self.peek(), Tok::LBrace) {
                    guard = Some(self.simple_stmt()?);
                }
            } else {
                guard = Some(first);
            }
        }
        self.no_composite = saved;

        // A type switch: the guard is `x.(type)` or `v := x.(type)`.
        if let Some((bind, expr)) = guard.as_ref().and_then(type_switch_guard) {
            return self.type_switch_body(init, bind, expr, line);
        }
        let tag = match guard {
            Some(s) => Some(stmt_into_expr(s, line)?),
            None => None,
        };

        self.expect(&Tok::LBrace)?;
        self.skip_semis();
        let mut cases = Vec::new();
        let mut default = None;
        while !matches!(self.peek(), Tok::RBrace | Tok::Eof) {
            if matches!(self.peek(), Tok::Ident(s) if s == "default") {
                self.advance();
                self.expect(&Tok::Colon)?;
                default = Some(self.case_body()?);
                continue;
            }
            if !matches!(self.peek(), Tok::Ident(s) if s == "case") {
                return Err(format!(
                    "go-rs: expected `case` or `default` in switch on line {}",
                    self.line()
                ));
            }
            self.advance(); // `case`
            let mut exprs = vec![self.expr()?];
            while self.eat(&Tok::Comma) {
                exprs.push(self.expr()?);
            }
            self.expect(&Tok::Colon)?;
            let body = self.case_body()?;
            cases.push(SwitchCase { exprs, body });
        }
        self.expect(&Tok::RBrace)?;
        Ok(Stmt::Switch {
            label: None,
            init,
            tag,
            cases,
            default,
            line,
        })
    }

    /// Parse the body of a type switch (its guard already recognized): `{ case
    /// T1, T2: … ; default: … }`, where each case names types.
    fn type_switch_body(
        &mut self,
        init: Option<Box<Stmt>>,
        bind: Option<String>,
        expr: Expr,
        line: u32,
    ) -> Result<Stmt, String> {
        self.expect(&Tok::LBrace)?;
        self.skip_semis();
        let mut cases = Vec::new();
        let mut default = None;
        while !matches!(self.peek(), Tok::RBrace | Tok::Eof) {
            if matches!(self.peek(), Tok::Ident(s) if s == "default") {
                self.advance();
                self.expect(&Tok::Colon)?;
                default = Some(self.case_body()?);
                continue;
            }
            if !matches!(self.peek(), Tok::Ident(s) if s == "case") {
                return Err(format!(
                    "go-rs: expected `case` or `default` in type switch on line {}",
                    self.line()
                ));
            }
            self.advance(); // `case`
            let mut types = vec![self.type_name()?];
            while self.eat(&Tok::Comma) {
                types.push(self.type_name()?);
            }
            self.expect(&Tok::Colon)?;
            let body = self.case_body()?;
            cases.push(TypeSwitchCase { types, body });
        }
        self.expect(&Tok::RBrace)?;
        Ok(Stmt::TypeSwitch {
            init,
            bind,
            expr,
            cases,
            default,
            line,
        })
    }

    /// Parse the communication clause of a select case (before the `:`).
    fn select_comm(&mut self) -> Result<SelectComm, String> {
        // `<-ch` (receive, no bind).
        if matches!(self.peek(), Tok::Arrow) {
            self.advance();
            return Ok(SelectComm::Recv {
                bind: None,
                ok_bind: None,
                chan: self.expr()?,
            });
        }
        let first = self.expr()?;
        // `v, ok := <-ch` — the comma-ok receive, whose `ok` is false when the
        // channel is closed and drained (which makes the case *ready*, so this
        // form is how a select loop learns a channel is finished).
        if matches!(self.peek(), Tok::Comma) {
            self.advance();
            let second = self.expr()?;
            if !matches!(self.peek(), Tok::Define | Tok::Assign) {
                return Err(format!(
                    "go-rs: expected `:=` after `v, ok` in select case, found `{}`",
                    self.peek()
                ));
            }
            self.advance();
            self.expect(&Tok::Arrow)?;
            let chan = self.expr()?;
            let name = |e: Expr| match e {
                Expr::Ident(n) if n != "_" => Some(n),
                _ => None,
            };
            return Ok(SelectComm::Recv {
                bind: name(first),
                ok_bind: name(second),
                chan,
            });
        }
        match self.peek() {
            // `v := <-ch` / `v = <-ch` (receive with bind).
            Tok::Define | Tok::Assign => {
                self.advance();
                self.expect(&Tok::Arrow)?;
                let chan = self.expr()?;
                let bind = match first {
                    Expr::Ident(n) if n != "_" => Some(n),
                    _ => None,
                };
                Ok(SelectComm::Recv {
                    bind,
                    ok_bind: None,
                    chan,
                })
            }
            // `ch <- val` (send).
            Tok::Arrow => {
                self.advance();
                Ok(SelectComm::Send {
                    chan: first,
                    val: self.expr()?,
                })
            }
            other => Err(format!(
                "go-rs: expected a channel operation in select case, found `{other}`"
            )),
        }
    }

    /// Parse the statements of a select case/default body (up to the next
    /// `case`/`default`/`}`).
    fn case_body(&mut self) -> Result<Vec<Stmt>, String> {
        self.skip_semis();
        let mut body = Vec::new();
        while !matches!(self.peek(), Tok::RBrace | Tok::Eof)
            && !matches!(self.peek(), Tok::Ident(s) if s == "case" || s == "default")
        {
            body.push(self.stmt()?);
            self.skip_semis();
        }
        Ok(body)
    }

    /// Parse a simple statement: short decl, assignment, inc/dec, or a bare
    /// expression.
    fn simple_stmt(&mut self) -> Result<Stmt, String> {
        let line = self.line();

        // Short variable declaration: `a, b := …`. Look for an ident list
        // followed by `:=`.
        if let Some(names) = self.try_ident_list_define() {
            let mut values = vec![self.expr()?];
            while self.eat(&Tok::Comma) {
                values.push(self.expr()?);
            }
            return Ok(Stmt::Short {
                names,
                values,
                line,
            });
        }

        let target = self.expr()?;
        // Parallel assignment `t0, t1, … = v0, v1, …`.
        if matches!(self.peek(), Tok::Comma) {
            let mut targets = vec![target];
            while self.eat(&Tok::Comma) {
                targets.push(self.expr()?);
            }
            self.expect(&Tok::Assign)?;
            let mut values = vec![self.expr()?];
            while self.eat(&Tok::Comma) {
                values.push(self.expr()?);
            }
            return Ok(Stmt::AssignMulti {
                targets,
                values,
                line,
            });
        }
        match self.peek() {
            // `ch <- val` — channel send.
            Tok::Arrow => {
                self.advance();
                let val = self.expr()?;
                Ok(Stmt::Send {
                    chan: target,
                    val,
                    line,
                })
            }
            Tok::PlusPlus => {
                self.advance();
                Ok(Stmt::IncDec {
                    target,
                    inc: true,
                    line,
                })
            }
            Tok::MinusMinus => {
                self.advance();
                Ok(Stmt::IncDec {
                    target,
                    inc: false,
                    line,
                })
            }
            Tok::Assign
            | Tok::PlusAssign
            | Tok::MinusAssign
            | Tok::StarAssign
            | Tok::SlashAssign
            | Tok::PercentAssign
            | Tok::AmpAssign
            | Tok::PipeAssign
            | Tok::CaretAssign
            | Tok::ShlAssign
            | Tok::ShrAssign
            | Tok::AndNotAssign => {
                let op = match self.advance() {
                    Tok::Assign => AssignOp::Set,
                    Tok::PlusAssign => AssignOp::Add,
                    Tok::MinusAssign => AssignOp::Sub,
                    Tok::StarAssign => AssignOp::Mul,
                    Tok::SlashAssign => AssignOp::Div,
                    Tok::PercentAssign => AssignOp::Mod,
                    Tok::AmpAssign => AssignOp::BitAnd,
                    Tok::PipeAssign => AssignOp::BitOr,
                    Tok::CaretAssign => AssignOp::BitXor,
                    Tok::ShlAssign => AssignOp::Shl,
                    Tok::ShrAssign => AssignOp::Shr,
                    Tok::AndNotAssign => AssignOp::AndNot,
                    _ => unreachable!(),
                };
                let value = self.expr()?;
                Ok(Stmt::Assign {
                    target,
                    op,
                    value,
                    line,
                })
            }
            _ => Ok(Stmt::ExprStmt(target)),
        }
    }

    /// If the tokens at the cursor form `ident {, ident} :=`, consume them and
    /// return the names; otherwise leave the cursor unmoved and return `None`.
    fn try_ident_list_define(&mut self) -> Option<Vec<String>> {
        let start = self.pos;
        let mut names = Vec::new();
        loop {
            match self.peek() {
                Tok::Ident(s) => {
                    names.push(s.clone());
                    self.advance();
                }
                _ => {
                    self.pos = start;
                    return None;
                }
            }
            match self.peek() {
                Tok::Comma => {
                    self.advance();
                }
                Tok::Define => {
                    self.advance();
                    return Some(names);
                }
                _ => {
                    self.pos = start;
                    return None;
                }
            }
        }
    }

    // ── expressions (precedence climbing) ──────────────────────────────────

    fn expr(&mut self) -> Result<Expr, String> {
        self.binary(0)
    }

    fn binary(&mut self, min_prec: u8) -> Result<Expr, String> {
        let mut lhs = self.unary()?;
        while let Some((op, prec)) = binop_of(self.peek()) {
            if prec < min_prec {
                break;
            }
            self.advance();
            let rhs = self.binary(prec + 1)?;
            lhs = Expr::Binary {
                op,
                lhs: Box::new(lhs),
                rhs: Box::new(rhs),
            };
        }
        Ok(lhs)
    }

    fn unary(&mut self) -> Result<Expr, String> {
        match self.peek() {
            Tok::Minus => {
                self.advance();
                Ok(Expr::Unary {
                    op: UnOp::Neg,
                    rhs: Box::new(self.unary()?),
                })
            }
            Tok::Not => {
                self.advance();
                Ok(Expr::Unary {
                    op: UnOp::Not,
                    rhs: Box::new(self.unary()?),
                })
            }
            // Unary `+` is a no-op on numbers.
            Tok::Plus => {
                self.advance();
                self.unary()
            }
            // `<-ch` — channel receive.
            Tok::Arrow => {
                self.advance();
                Ok(Expr::Recv {
                    chan: Box::new(self.unary()?),
                })
            }
            // `&x` — address-of (a no-copy reference on go-rs handles).
            Tok::Amp => {
                self.advance();
                Ok(Expr::Unary {
                    op: UnOp::Addr,
                    rhs: Box::new(self.unary()?),
                })
            }
            // `^x` — bitwise complement.
            Tok::Caret => {
                self.advance();
                Ok(Expr::Unary {
                    op: UnOp::BitNot,
                    rhs: Box::new(self.unary()?),
                })
            }
            // `*p` — pointer dereference (identity on a go-rs handle).
            Tok::Star => {
                self.advance();
                Ok(Expr::Unary {
                    op: UnOp::Deref,
                    rhs: Box::new(self.unary()?),
                })
            }
            _ => self.postfix(),
        }
    }

    fn postfix(&mut self) -> Result<Expr, String> {
        let mut e = self.primary()?;
        loop {
            // Erase type arguments on a generic instantiation: `F[int](x)` and
            // `Stack[int]{…}`. When the base is a known generic name, the `[ … ]`
            // is type arguments (not an index), so consume and drop it, leaving
            // the bare name to be called or composite-literal'd below.
            if matches!(self.peek(), Tok::LBracket) {
                if let Expr::Ident(n) = &e {
                    if self.generic_names.contains(n) {
                        let base = n.clone();
                        self.skip_type_brackets()?;
                        // `Stack[int]{ … }` — a generic struct composite literal.
                        if matches!(self.peek(), Tok::LBrace) && self.struct_names.contains(&base) {
                            e = self.struct_literal(base)?;
                        }
                        continue;
                    }
                }
                // A package-qualified base with a bracket group immediately
                // followed by `(` or `{` is a generic instantiation imported from
                // another package — `pkg.Func[T](…)` / `pkg.Type[T]{…}` — which the
                // local `generic_names` set can't know. Erase the type arguments.
                // Restricted to a `pkg.Name` selector base so an index-then-call
                // `fns[i](x)` (plain-ident base) is unaffected.
                if matches!(&e, Expr::Selector { recv, .. } if matches!(recv.as_ref(), Expr::Ident(_)))
                {
                    let next = self.bracket_group_next();
                    // `pkg.Func[T](…)` — a generic call. The `(` is never a loop
                    // body, so this is unambiguous.
                    if matches!(next, Some(Tok::LParen)) {
                        self.skip_type_brackets()?;
                        continue;
                    }
                    // `pkg.Type[T]{ … }` — a generic composite. Ambiguous with an
                    // index in a control header (`range m[k] {`), so gated.
                    if matches!(next, Some(Tok::LBrace)) && !self.no_composite {
                        self.skip_type_brackets()?;
                        if let Expr::Selector { recv, field } = &e {
                            if let Expr::Ident(pkg) = recv.as_ref() {
                                e = self.struct_literal(format!("{pkg}.{field}"))?;
                            }
                        }
                        continue;
                    }
                }
            }
            match self.peek() {
                Tok::Dot => {
                    self.advance();
                    // Type assertion `x.(T)` / type-switch guard `x.(type)`.
                    if self.eat(&Tok::LParen) {
                        let ty = if self.eat(&Tok::Type) {
                            "type".to_string()
                        } else {
                            self.type_name()?
                        };
                        self.expect(&Tok::RParen)?;
                        e = Expr::TypeAssert {
                            expr: Box::new(e),
                            ty,
                        };
                    } else {
                        let field = self.ident()?;
                        // `pkg.Type{ … }` — a composite literal of a type imported
                        // from another package. Recognized when the receiver is a
                        // bare package identifier and a `{` follows (outside a
                        // control-clause header). The linker qualifies `pkg.Type`
                        // to the merged struct name the compiler then resolves.
                        if !self.no_composite
                            && matches!(self.peek(), Tok::LBrace)
                            && matches!(&e, Expr::Ident(_))
                        {
                            let Expr::Ident(pkg) = &e else { unreachable!() };
                            e = self.struct_literal(format!("{pkg}.{field}"))?;
                        } else {
                            e = Expr::Selector {
                                recv: Box::new(e),
                                field,
                            };
                        }
                    }
                }
                Tok::LBracket => {
                    self.advance();
                    // `recv[low:high]` slice expression, or `recv[index]`. Either
                    // bound of a slice may be omitted (`s[:hi]`, `s[lo:]`, `s[:]`).
                    let low = if matches!(self.peek(), Tok::Colon) {
                        None
                    } else {
                        Some(self.expr()?)
                    };
                    if self.eat(&Tok::Colon) {
                        let high = if matches!(self.peek(), Tok::RBracket | Tok::Colon) {
                            None
                        } else {
                            Some(self.expr()?)
                        };
                        // A three-index (full) slice `s[low:high:max]` — `max`
                        // caps the result's capacity at `max - low`.
                        let max = if self.eat(&Tok::Colon) {
                            Some(self.expr()?)
                        } else {
                            None
                        };
                        self.expect(&Tok::RBracket)?;
                        e = Expr::Slice {
                            recv: Box::new(e),
                            low: low.map(Box::new),
                            high: high.map(Box::new),
                            max: max.map(Box::new),
                        };
                    } else {
                        self.expect(&Tok::RBracket)?;
                        e = Expr::Index {
                            recv: Box::new(e),
                            index: Box::new(low.expect("index expr present")),
                        };
                    }
                }
                Tok::LParen => {
                    let line = self.line();
                    self.advance();
                    // Call-argument parentheses reset composite suppression.
                    let saved = self.no_composite;
                    self.no_composite = false;
                    let mut args = Vec::new();
                    let mut spread = false;
                    while !matches!(self.peek(), Tok::RParen) {
                        args.push(self.expr()?);
                        // `f(xs...)` — spread the last argument into a variadic call.
                        if matches!(self.peek(), Tok::Ellipsis) {
                            self.advance();
                            spread = true;
                        }
                        if !self.eat(&Tok::Comma) {
                            break;
                        }
                    }
                    self.expect(&Tok::RParen)?;
                    self.no_composite = saved;
                    e = Expr::Call {
                        func: Box::new(e),
                        args,
                        spread,
                        line,
                    };
                }
                _ => break,
            }
        }
        Ok(e)
    }

    fn primary(&mut self) -> Result<Expr, String> {
        let line = self.line();
        // `[]T{ … }` slice literal / `[]T(x)` conversion, or a fixed-size array
        // literal `[N]T{ … }` / `[...]T{ … }`.
        if matches!(self.peek(), Tok::LBracket) {
            // Peek past `[` to distinguish a slice (`[]`) from an array (`[N]`
            // or `[...]`).
            if matches!(self.peek2(), Tok::RBracket) {
                return self.slice_literal();
            }
            return self.array_literal();
        }
        // `struct{ … }{ … }` — an anonymous struct composite literal.
        if matches!(self.peek(), Tok::Struct) {
            let ty = self.anon_struct_type()?;
            return self.struct_literal(ty);
        }
        match self.advance() {
            Tok::Int(n) => Ok(Expr::Int(n)),
            Tok::Float(f, dec) => Ok(Expr::Float(f, dec)),
            Tok::Str(s) => Ok(Expr::Str(s)),
            Tok::True => Ok(Expr::Bool(true)),
            Tok::False => Ok(Expr::Bool(false)),
            Tok::Ident(s) => {
                match s.as_str() {
                    // `map[K]V{ … }` map composite literal.
                    "map" if matches!(self.peek(), Tok::LBracket) => self.map_literal(),
                    // `make([]T, n)` / `make(map[K]V)`.
                    "make" if matches!(self.peek(), Tok::LParen) => self.make_expr(),
                    // `new(T)` — a pointer to a zero value of T. Faithful lowering:
                    // `&T{}` for a struct (the compiler zero-fills missing fields),
                    // and address-of the scalar zero value otherwise.
                    "new" if matches!(self.peek(), Tok::LParen) => self.new_expr(),
                    // `T{ … }` struct composite literal (T declared as a struct).
                    _ if matches!(self.peek(), Tok::LBrace) && self.struct_names.contains(&s) => {
                        self.struct_literal(s)
                    }
                    // `T{ … }` where `T` is a defined type over a slice, array
                    // or map: the literal is the base's, and the defined name is
                    // kept as the conversion `T(<base literal>)` — which is what
                    // the type already means, and needs no new node.
                    _ if matches!(self.peek(), Tok::LBrace)
                        && self.defined_composite_base(&s).is_some() =>
                    {
                        let line = self.line();
                        let base = self.defined_composite_base(&s).unwrap_or_default();
                        let lit = self.elided_literal(&base)?;
                        Ok(Expr::Call {
                            func: Box::new(Expr::Ident(s)),
                            args: vec![lit],
                            spread: false,
                            line,
                        })
                    }
                    _ => Ok(Expr::Ident(s)),
                }
            }
            Tok::LParen => {
                // Parentheses reset the composite-literal suppression: `if
                // (Point{}).x > 0 { … }` is a valid composite inside a header.
                let saved = self.no_composite;
                self.no_composite = false;
                let e = self.expr()?;
                self.expect(&Tok::RParen)?;
                self.no_composite = saved;
                Ok(e)
            }
            // A function literal (closure): `func(params) results { body }`.
            Tok::Func => {
                self.expect(&Tok::LParen)?;
                // Variadic closures are uncommon; the flag is dropped for FuncLit.
                let (params, _) = self.params()?;
                self.expect(&Tok::RParen)?;
                // Closures keep only result types (named results on a func literal
                // are uncommon; the name is dropped).
                let results = self.results()?.into_iter().map(|(_, t)| t).collect();
                let body = self.block()?;
                Ok(Expr::FuncLit {
                    params,
                    results,
                    body,
                })
            }
            other => Err(format!(
                "go-rs: unexpected token `{other}` in expression on line {line}"
            )),
        }
    }

    /// `[]T{ e0, e1, … }` (slice composite literal) or `[]T(x)` (a slice
    /// conversion — `[]byte(s)` / `[]rune(s)`).
    fn slice_literal(&mut self) -> Result<Expr, String> {
        self.expect(&Tok::LBracket)?;
        self.expect(&Tok::RBracket)?;
        let elem_ty = self.type_name()?;
        // `[]byte(x)` / `[]rune(x)` — a conversion, not a composite literal.
        if matches!(self.peek(), Tok::LParen) {
            let line = self.line();
            self.expect(&Tok::LParen)?;
            let arg = self.expr()?;
            self.expect(&Tok::RParen)?;
            return Ok(Expr::Call {
                func: Box::new(Expr::Ident(format!("[]{elem_ty}"))),
                args: vec![arg],
                spread: false,
                line,
            });
        }
        // Each element may elide the type: `[]T{ {…}, {…} }` means
        // `[]T{ T{…}, T{…} }`, for a struct `T` and for a container `T` alike.
        let elems = self.brace_list(|p| p.elem_or_expr(&elem_ty))?;
        Ok(Expr::SliceLit {
            elem_ty,
            elems,
            array_len: None,
        })
    }

    /// `[N]T{ … }` / `[...]T{ … }` fixed-size array literal — a `SliceLit`
    /// carrying the array's length, which is what makes its static type the
    /// value type `[N]T` rather than the reference type `[]T`.
    /// Elements may be sequential (`v0, v1, …`) or index-keyed (`3: v`), the
    /// latter zero-filling any gaps; a `[...]` array is sized by its elements.
    fn array_literal(&mut self) -> Result<Expr, String> {
        self.expect(&Tok::LBracket)?;
        // Size: `...` (element-sized) or a constant expression `[N]`.
        let fixed_len: Option<usize> = if matches!(self.peek(), Tok::Ellipsis) {
            self.advance();
            None
        } else {
            let n = self.expr()?;
            const_int_of(&n).map(|n| n as usize)
        };
        self.expect(&Tok::RBracket)?;
        let elem_ty = self.type_name()?;
        self.expect(&Tok::LBrace)?;
        // Collect (index, value) placements; a sequential value takes the running
        // index, an index-keyed value resets it.
        let mut placed: Vec<(usize, Expr)> = Vec::new();
        let mut next_idx = 0usize;
        while !matches!(self.peek(), Tok::RBrace) {
            // A bare `{ … }` is an elided composite of the element type, and
            // cannot be an index key, so it is always sequential.
            if matches!(self.peek(), Tok::LBrace) {
                let v = self.elided_literal(&elem_ty)?;
                placed.push((next_idx, v));
                next_idx += 1;
            } else {
                let first = self.expr()?;
                if self.eat(&Tok::Colon) {
                    // `idx: value` — an index-keyed element.
                    let idx = const_int_of(&first).ok_or_else(|| {
                        format!(
                            "go-rs: array index in a composite literal must be a constant (line {})",
                            self.line()
                        )
                    })? as usize;
                    let v = self.elem_or_expr(&elem_ty)?;
                    placed.push((idx, v));
                    next_idx = idx + 1;
                } else {
                    placed.push((next_idx, first));
                    next_idx += 1;
                }
            }
            if !self.eat(&Tok::Comma) {
                break;
            }
        }
        self.expect(&Tok::RBrace)?;
        // Final length: the declared `[N]`, else one past the highest index.
        let len = fixed_len.unwrap_or_else(|| placed.iter().map(|(i, _)| i + 1).max().unwrap_or(0));
        let mut elems: Vec<Expr> = (0..len).map(|_| self.zero_value_expr(&elem_ty)).collect();
        for (idx, v) in placed {
            if idx < elems.len() {
                elems[idx] = v;
            }
        }
        Ok(Expr::SliceLit {
            elem_ty,
            elems,
            array_len: Some(len),
        })
    }

    /// `map[K]V{ k0: v0, … }` (already consumed the `map` ident).
    fn map_literal(&mut self) -> Result<Expr, String> {
        self.expect(&Tok::LBracket)?;
        let key_ty = self.type_name()?;
        self.expect(&Tok::RBracket)?;
        let val_ty = self.type_name()?;
        self.expect(&Tok::LBrace)?;
        let mut pairs = Vec::new();
        while !matches!(self.peek(), Tok::RBrace) {
            let k = self.elem_or_expr(&key_ty)?;
            self.expect(&Tok::Colon)?;
            let v = self.elem_or_expr(&val_ty)?;
            pairs.push((k, v));
            if !self.eat(&Tok::Comma) {
                break;
            }
        }
        self.expect(&Tok::RBrace)?;
        Ok(Expr::MapLit {
            key_ty,
            val_ty,
            pairs,
        })
    }

    /// One element of a composite literal whose element type is `ty`. A bare
    /// `{ … }` here is Go's elided element type — `[][]int{{1, 2}}` means
    /// `[][]int{[]int{1, 2}}` — so it is parsed as a literal of `ty` rather than
    /// as an expression. Anything else is an ordinary expression.
    fn elem_or_expr(&mut self, ty: &str) -> Result<Expr, String> {
        if !matches!(self.peek(), Tok::LBrace) {
            return self.expr();
        }
        self.elided_literal(ty)
    }

    /// Whether the `[` at the cursor opens a slice or array *type* rather than a
    /// generic type-parameter list: `[]T` has nothing between the brackets, and
    /// `[3]T` / `[...]T` a length. A parameter list always names a parameter
    /// first (`[T any]`).
    fn brackets_open_a_type(&self) -> bool {
        matches!(
            self.tokens.get(self.pos + 1).map(|t| &t.kind),
            Some(Tok::RBracket) | Some(Tok::Int(_)) | Some(Tok::Ellipsis)
        )
    }

    /// The base of a defined type whose base is a composite — a slice, an array
    /// or a map — so `Name{…}` can be parsed as that base's literal. `None` for
    /// an undeclared name or one whose base takes no composite literal (`int`,
    /// `func(…)`, `chan T`, a pointer).
    fn defined_composite_base(&self, name: &str) -> Option<String> {
        let base = self.defined.get(name)?;
        let composite = base.starts_with("[]")
            || base.starts_with("map[")
            || (array_elem_ty(base).is_some() && array_len_of(base).is_some());
        composite.then(|| base.clone())
    }

    /// A `{ … }` standing in for a literal of the known element type `ty`.
    /// Reconstructs which literal form `ty` calls for, since the type text was
    /// written once on the outer literal and elided on every element.
    fn elided_literal(&mut self, ty: &str) -> Result<Expr, String> {
        // `[][2]int{{1, 2}}` elides an *array* element, which keeps its length
        // (and so its value semantics); a gap left by a short `{…}` is the
        // element type's own zero, exactly as in the written `[N]T{…}` form.
        if let (Some(inner), Some(n)) = (array_elem_ty(ty), array_len_of(ty)) {
            let mut elems = self.brace_list(|p| p.elem_or_expr(inner))?;
            elems.truncate(n);
            while elems.len() < n {
                elems.push(self.zero_value_expr(inner));
            }
            return Ok(Expr::SliceLit {
                elem_ty: inner.to_string(),
                elems,
                array_len: Some(n),
            });
        }
        if let Some(inner) = ty.strip_prefix("[]") {
            let elems = self.brace_list(|p| p.elem_or_expr(inner))?;
            return Ok(Expr::SliceLit {
                elem_ty: inner.to_string(),
                elems,
                array_len: None,
            });
        }
        if let Some((key_ty, val_ty)) = split_map_type(ty) {
            self.expect(&Tok::LBrace)?;
            let mut pairs = Vec::new();
            while !matches!(self.peek(), Tok::RBrace) {
                let k = self.elem_or_expr(&key_ty)?;
                self.expect(&Tok::Colon)?;
                let v = self.elem_or_expr(&val_ty)?;
                pairs.push((k, v));
                if !self.eat(&Tok::Comma) {
                    break;
                }
            }
            self.expect(&Tok::RBrace)?;
            return Ok(Expr::MapLit {
                key_ty,
                val_ty,
                pairs,
            });
        }
        // Anything else names a struct — a declared one, an inline `struct{…}`,
        // or a qualified `pkg.T` the parser can't see the definition of.
        self.struct_literal(ty.to_string())
    }

    /// `{ a, b, … }` — a comma-separated brace list, each element parsed by `f`.
    fn brace_list<T>(
        &mut self,
        mut f: impl FnMut(&mut Self) -> Result<T, String>,
    ) -> Result<Vec<T>, String> {
        self.expect(&Tok::LBrace)?;
        let saved = self.no_composite;
        self.no_composite = false;
        let mut out = Vec::new();
        while !matches!(self.peek(), Tok::RBrace) {
            out.push(f(self)?);
            if !self.eat(&Tok::Comma) {
                break;
            }
        }
        self.no_composite = saved;
        self.expect(&Tok::RBrace)?;
        Ok(out)
    }

    /// `T{ … }` — positional `T{a, b}` or keyed `T{x: a, y: b}`.
    fn struct_literal(&mut self, type_name: String) -> Result<Expr, String> {
        self.expect(&Tok::LBrace)?;
        // Inside the composite's braces the `{`-ambiguity is over, so nested
        // composites in field values are allowed again.
        let saved = self.no_composite;
        self.no_composite = false;
        let mut fields = Vec::new();
        while !matches!(self.peek(), Tok::RBrace) {
            // Keyed element: `ident :` (but not `::`); otherwise positional.
            let key = if matches!(self.peek(), Tok::Ident(_)) && matches!(self.peek2(), Tok::Colon)
            {
                let n = self.ident()?;
                self.expect(&Tok::Colon)?;
                Some(n)
            } else {
                None
            };
            let val = self.expr()?;
            fields.push((key, val));
            if !self.eat(&Tok::Comma) {
                break;
            }
        }
        self.expect(&Tok::RBrace)?;
        self.no_composite = saved;
        Ok(Expr::StructLit { type_name, fields })
    }

    /// `new(T)` — allocate a zero value of `T` and return a pointer to it. A
    /// struct `T` lowers to `&T{}` (the compiler zero-fills missing fields); a
    /// scalar `T` lowers to the address of its zero value.
    fn new_expr(&mut self) -> Result<Expr, String> {
        self.expect(&Tok::LParen)?;
        let ty = self.type_name()?;
        self.expect(&Tok::RParen)?;
        let inner = if self.struct_names.contains(&ty) {
            Expr::StructLit {
                type_name: ty,
                fields: Vec::new(),
            }
        } else {
            zero_expr(&ty)
        };
        Ok(Expr::Unary {
            op: crate::ast::UnOp::Addr,
            rhs: Box::new(inner),
        })
    }

    /// `make([]T, len[, cap])` (slice) or `make(map[K]V)` (map). The first
    /// argument is a type; a slice may carry a length and a capacity.
    fn make_expr(&mut self) -> Result<Expr, String> {
        self.expect(&Tok::LParen)?;
        let ty = self.type_name()?;
        // `make(chan T, cap)` — a channel.
        if let Some(elem) = ty.strip_prefix("chan ") {
            let elem_ty = elem.trim().to_string();
            let cap = if self.eat(&Tok::Comma) {
                Some(Box::new(self.expr()?))
            } else {
                None
            };
            self.expect(&Tok::RParen)?;
            return Ok(Expr::MakeChan { cap, elem_ty });
        }
        let is_map = ty.starts_with("map[");
        let mut len = None;
        let mut cap = None;
        if self.eat(&Tok::Comma) {
            len = Some(Box::new(self.expr()?));
            if self.eat(&Tok::Comma) {
                cap = Some(Box::new(self.expr()?));
            }
        }
        self.expect(&Tok::RParen)?;
        // A slice records its element type (which fills `elem_zero`); a map has
        // no element zero to build, so it records the whole written `map[K]V` —
        // the only place that type survives, and what lets a `m[k]` element be
        // typed at a use site.
        let elem_ty = ty.strip_prefix("[]").unwrap_or("");
        Ok(Expr::Make {
            is_map,
            len,
            cap,
            elem_ty: if is_map {
                ty.clone()
            } else {
                elem_ty.to_string()
            },
            // Every element gets its own zero, built by the one rule in
            // `zero_value_expr` — without it `make([]T, n)` fills with integers
            // and the first `s[i].f = v` panics on a nil dereference.
            elem_zero: Box::new(self.zero_value_expr(elem_ty)),
        })
    }
}

/// The constant integer value of an expression, if it folds to one — used for
/// array sizes and index-keyed array-literal elements. Handles literals and the
/// simple unary/binary arithmetic that appears in array bounds.
fn const_int_of(e: &Expr) -> Option<i64> {
    match e {
        Expr::Int(n) => Some(*n),
        Expr::Unary {
            op: crate::ast::UnOp::Neg,
            rhs,
        } => const_int_of(rhs).map(|n| -n),
        Expr::Binary { op, lhs, rhs } => {
            use crate::ast::BinOp;
            let (a, b) = (const_int_of(lhs)?, const_int_of(rhs)?);
            match op {
                BinOp::Add => Some(a + b),
                BinOp::Sub => Some(a - b),
                BinOp::Mul => Some(a * b),
                BinOp::Div if b != 0 => Some(a / b),
                BinOp::Mod if b != 0 => Some(a % b),
                BinOp::Shl => Some(a << b),
                BinOp::Shr => Some(a >> b),
                _ => None,
            }
        }
        _ => None,
    }
}

/// The zero-value expression for a Go element type (drives `make([]T, n)` fill).
fn zero_expr(ty: &str) -> Expr {
    match ty {
        "float32" | "float64" => Expr::Float(0.0, Some((0, 0))),
        "string" => Expr::Str(String::new()),
        "bool" => Expr::Bool(false),
        _ => Expr::Int(0),
    }
}

/// The binary operator and its precedence for a token, or `None` if the token
/// is not a binary operator. Higher binds tighter (Go's five levels).
/// Replace every `iota` identifier in a constant expression with its integer
/// value `idx` (the const spec's index within its block).
fn subst_iota(e: Expr, idx: i64) -> Expr {
    let b = |e: Box<Expr>| Box::new(subst_iota(*e, idx));
    match e {
        Expr::Ident(n) if n == "iota" => Expr::Int(idx),
        Expr::Unary { op, rhs } => Expr::Unary { op, rhs: b(rhs) },
        Expr::Binary { op, lhs, rhs } => Expr::Binary {
            op,
            lhs: b(lhs),
            rhs: b(rhs),
        },
        Expr::Call {
            func,
            args,
            spread,
            line,
        } => Expr::Call {
            func: b(func),
            args: args.into_iter().map(|a| subst_iota(a, idx)).collect(),
            spread,
            line,
        },
        Expr::Index { recv, index } => Expr::Index {
            recv: b(recv),
            index: b(index),
        },
        Expr::Selector { recv, field } => Expr::Selector {
            recv: b(recv),
            field,
        },
        other => other,
    }
}

/// If `s` is a type-switch guard (`x.(type)` or `v := x.(type)`), return the
/// optional bound name and the asserted expression.
fn type_switch_guard(s: &Stmt) -> Option<(Option<String>, Expr)> {
    let is_type_assert = |e: &Expr| matches!(e, Expr::TypeAssert { ty, .. } if ty == "type");
    match s {
        Stmt::ExprStmt(e) if is_type_assert(e) => {
            if let Expr::TypeAssert { expr, .. } = e {
                Some((None, (**expr).clone()))
            } else {
                None
            }
        }
        Stmt::Short { names, values, .. } if names.len() == 1 && values.len() == 1 => {
            if let Expr::TypeAssert { expr, ty } = &values[0] {
                if ty == "type" {
                    return Some((Some(names[0].clone()), (**expr).clone()));
                }
            }
            None
        }
        _ => None,
    }
}

fn binop_of(t: &Tok) -> Option<(BinOp, u8)> {
    Some(match t {
        Tok::OrOr => (BinOp::Or, 1),
        Tok::AndAnd => (BinOp::And, 2),
        Tok::EqEq => (BinOp::Eq, 3),
        Tok::NotEq => (BinOp::Ne, 3),
        Tok::Lt => (BinOp::Lt, 3),
        Tok::Gt => (BinOp::Gt, 3),
        Tok::Le => (BinOp::Le, 3),
        Tok::Ge => (BinOp::Ge, 3),
        // Additive level (4): `+ - | ^`.
        Tok::Plus => (BinOp::Add, 4),
        Tok::Minus => (BinOp::Sub, 4),
        Tok::Pipe => (BinOp::BitOr, 4),
        Tok::Caret => (BinOp::BitXor, 4),
        // Multiplicative level (5): `* / % << >> & &^`.
        Tok::Star => (BinOp::Mul, 5),
        Tok::Slash => (BinOp::Div, 5),
        Tok::Percent => (BinOp::Mod, 5),
        Tok::Shl => (BinOp::Shl, 5),
        Tok::Shr => (BinOp::Shr, 5),
        Tok::Amp => (BinOp::BitAnd, 5),
        Tok::AndNot => (BinOp::AndNot, 5),
        _ => return None,
    })
}

/// Extract the expression from a statement that is required to be an expression
/// (a `for`/`if` condition parsed as a simple statement).
fn stmt_into_expr(s: Stmt, line: u32) -> Result<Expr, String> {
    match s {
        Stmt::ExprStmt(e) => Ok(e),
        _ => Err(format!(
            "go-rs: expected a boolean expression on line {line}"
        )),
    }
}

/// Split `map[K]V` into `(K, V)`, matching brackets so a map-keyed-by-map or a
/// slice value (`map[string][]int`) splits at the right `]`. Returns `None` for
/// any type that isn't a map.
fn split_map_type(ty: &str) -> Option<(String, String)> {
    let rest = ty.strip_prefix("map[")?;
    let mut depth = 1usize;
    for (i, c) in rest.char_indices() {
        match c {
            '[' => depth += 1,
            ']' => {
                depth -= 1;
                if depth == 0 {
                    return Some((rest[..i].to_string(), rest[i + 1..].to_string()));
                }
            }
            _ => {}
        }
    }
    None
}