bock-codegen 1.0.0

Multi-target code generation for Bock — JS, TS, Python, Rust, Go
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
12009
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049
12050
12051
12052
12053
12054
12055
12056
12057
12058
12059
12060
12061
12062
12063
12064
12065
12066
12067
12068
12069
12070
12071
12072
12073
12074
12075
12076
12077
12078
12079
12080
12081
12082
12083
12084
12085
12086
12087
12088
12089
12090
12091
12092
12093
12094
12095
12096
12097
12098
12099
12100
12101
12102
12103
12104
12105
12106
12107
12108
12109
12110
12111
12112
12113
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124
12125
12126
12127
12128
12129
12130
12131
12132
12133
12134
12135
12136
12137
12138
12139
12140
12141
12142
12143
12144
12145
12146
12147
12148
12149
12150
12151
12152
12153
12154
12155
12156
12157
12158
12159
12160
12161
12162
12163
12164
12165
12166
12167
12168
12169
12170
12171
12172
12173
12174
12175
12176
12177
12178
12179
12180
12181
12182
12183
12184
12185
12186
12187
12188
12189
12190
12191
12192
12193
12194
12195
12196
12197
12198
12199
12200
12201
12202
12203
12204
12205
12206
12207
12208
12209
12210
12211
12212
12213
12214
12215
12216
12217
12218
12219
12220
12221
12222
12223
12224
12225
12226
12227
12228
12229
12230
12231
12232
12233
12234
12235
12236
12237
12238
12239
12240
12241
12242
12243
12244
12245
12246
12247
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260
12261
12262
12263
12264
12265
12266
12267
12268
12269
12270
12271
12272
12273
12274
12275
12276
12277
12278
12279
12280
12281
12282
12283
12284
12285
12286
12287
12288
12289
12290
12291
12292
12293
12294
12295
12296
12297
12298
12299
12300
12301
12302
12303
12304
12305
12306
12307
12308
12309
12310
12311
12312
12313
12314
12315
12316
12317
12318
12319
12320
12321
12322
12323
12324
12325
12326
12327
12328
12329
12330
12331
12332
12333
12334
12335
12336
12337
12338
12339
12340
12341
12342
12343
12344
12345
12346
12347
12348
12349
12350
12351
12352
12353
12354
12355
12356
12357
12358
12359
12360
12361
12362
12363
12364
12365
12366
12367
12368
12369
12370
12371
12372
12373
12374
12375
12376
12377
12378
12379
12380
12381
12382
12383
12384
12385
12386
12387
12388
12389
12390
12391
12392
12393
12394
12395
12396
12397
12398
12399
12400
12401
12402
12403
12404
12405
12406
12407
12408
12409
12410
12411
12412
12413
12414
12415
12416
12417
12418
12419
12420
12421
12422
12423
12424
12425
12426
12427
12428
12429
12430
12431
12432
12433
12434
12435
12436
12437
12438
12439
12440
12441
12442
12443
12444
12445
12446
12447
12448
12449
12450
12451
12452
12453
12454
12455
12456
12457
12458
12459
12460
12461
12462
12463
12464
12465
12466
12467
12468
12469
12470
12471
12472
12473
12474
12475
12476
12477
12478
12479
12480
12481
12482
12483
12484
12485
12486
12487
12488
12489
12490
12491
12492
12493
12494
12495
12496
12497
12498
12499
12500
12501
12502
12503
12504
12505
12506
12507
12508
12509
12510
12511
12512
12513
12514
12515
12516
12517
12518
12519
12520
12521
12522
12523
12524
12525
12526
12527
12528
12529
12530
12531
12532
12533
12534
12535
12536
12537
12538
12539
12540
12541
12542
12543
12544
12545
12546
12547
12548
12549
12550
12551
12552
12553
12554
12555
12556
12557
12558
12559
12560
12561
12562
12563
12564
12565
12566
12567
12568
12569
12570
12571
12572
12573
12574
12575
12576
12577
12578
12579
12580
12581
12582
12583
12584
12585
12586
12587
12588
12589
12590
12591
12592
12593
12594
12595
12596
12597
12598
12599
12600
12601
12602
12603
12604
12605
12606
12607
12608
12609
12610
12611
12612
12613
12614
12615
12616
12617
12618
12619
12620
12621
12622
12623
12624
12625
12626
12627
12628
12629
12630
12631
12632
12633
12634
12635
12636
12637
12638
12639
12640
12641
12642
12643
12644
12645
12646
12647
12648
12649
12650
12651
12652
12653
12654
12655
12656
12657
12658
12659
12660
12661
12662
12663
12664
12665
12666
12667
12668
12669
12670
12671
12672
12673
12674
12675
12676
12677
12678
12679
12680
12681
12682
12683
12684
12685
12686
12687
12688
12689
12690
12691
12692
12693
12694
12695
12696
12697
12698
12699
12700
12701
12702
12703
12704
12705
12706
12707
12708
12709
12710
12711
12712
12713
12714
12715
12716
12717
12718
12719
12720
12721
12722
12723
12724
12725
12726
12727
12728
12729
12730
12731
12732
12733
12734
12735
12736
12737
12738
12739
12740
12741
12742
12743
12744
12745
12746
12747
12748
12749
12750
12751
12752
12753
12754
12755
12756
12757
12758
12759
12760
12761
12762
12763
12764
12765
12766
12767
12768
12769
12770
12771
12772
12773
12774
12775
12776
12777
12778
12779
12780
12781
12782
12783
12784
12785
12786
12787
12788
12789
12790
12791
12792
12793
12794
12795
12796
12797
12798
12799
12800
12801
12802
12803
12804
12805
12806
12807
12808
12809
12810
12811
12812
12813
12814
12815
12816
12817
12818
12819
12820
12821
12822
12823
12824
12825
12826
12827
12828
12829
12830
12831
12832
12833
12834
12835
12836
12837
12838
12839
12840
12841
12842
12843
12844
12845
12846
12847
12848
12849
12850
12851
12852
12853
12854
12855
12856
12857
12858
12859
12860
12861
12862
12863
12864
12865
12866
12867
12868
12869
12870
12871
12872
12873
12874
12875
12876
12877
12878
12879
12880
12881
12882
12883
12884
12885
12886
12887
12888
12889
12890
12891
12892
12893
12894
12895
12896
12897
12898
12899
12900
12901
12902
12903
12904
12905
12906
12907
12908
12909
12910
12911
12912
12913
12914
12915
12916
12917
12918
12919
12920
12921
12922
12923
12924
12925
12926
12927
12928
12929
12930
12931
12932
12933
12934
12935
12936
12937
12938
12939
12940
12941
12942
12943
12944
12945
12946
12947
12948
12949
12950
12951
12952
12953
12954
12955
12956
12957
12958
12959
12960
12961
12962
12963
12964
12965
12966
12967
12968
12969
12970
12971
12972
12973
12974
12975
12976
12977
12978
12979
12980
12981
12982
12983
12984
12985
12986
12987
12988
12989
12990
12991
12992
12993
12994
12995
12996
12997
12998
12999
13000
13001
13002
13003
13004
13005
13006
13007
13008
13009
13010
13011
13012
13013
13014
13015
13016
13017
13018
13019
13020
13021
13022
13023
13024
13025
13026
13027
13028
13029
13030
13031
13032
13033
13034
13035
13036
13037
13038
13039
13040
13041
13042
13043
13044
13045
13046
13047
13048
13049
13050
13051
13052
13053
13054
13055
13056
13057
13058
13059
13060
13061
13062
13063
13064
13065
13066
13067
13068
13069
13070
13071
13072
13073
13074
13075
13076
13077
13078
13079
13080
13081
13082
13083
13084
13085
13086
13087
13088
13089
13090
13091
13092
13093
13094
13095
13096
13097
13098
13099
13100
13101
13102
13103
13104
13105
13106
13107
13108
13109
13110
13111
13112
13113
13114
13115
13116
13117
13118
13119
13120
13121
13122
13123
13124
13125
13126
13127
13128
13129
13130
13131
13132
13133
13134
13135
13136
13137
13138
13139
13140
13141
13142
13143
13144
13145
13146
13147
13148
13149
13150
13151
13152
13153
13154
13155
13156
13157
13158
13159
13160
13161
13162
13163
13164
13165
13166
13167
13168
13169
13170
13171
13172
13173
13174
13175
13176
13177
13178
13179
13180
13181
13182
13183
13184
13185
13186
13187
13188
13189
13190
13191
13192
13193
13194
13195
13196
13197
13198
13199
13200
13201
13202
13203
13204
13205
13206
13207
13208
13209
13210
13211
13212
13213
13214
13215
13216
13217
13218
13219
13220
13221
13222
13223
13224
13225
13226
13227
13228
13229
13230
13231
13232
13233
13234
13235
13236
13237
13238
13239
13240
13241
13242
13243
13244
13245
13246
13247
13248
13249
13250
13251
13252
13253
13254
13255
13256
13257
13258
13259
13260
13261
13262
13263
13264
13265
13266
13267
13268
13269
13270
13271
13272
13273
13274
13275
13276
13277
13278
13279
13280
13281
13282
13283
13284
13285
13286
13287
13288
13289
13290
13291
13292
13293
13294
13295
13296
13297
13298
13299
13300
13301
13302
13303
13304
13305
13306
13307
13308
13309
13310
13311
13312
13313
13314
13315
13316
13317
13318
13319
13320
13321
13322
13323
13324
13325
13326
13327
13328
13329
13330
13331
13332
13333
13334
13335
13336
13337
13338
13339
13340
13341
13342
13343
13344
13345
13346
13347
13348
13349
13350
13351
13352
13353
13354
13355
13356
13357
13358
13359
13360
13361
13362
13363
13364
13365
13366
13367
13368
13369
13370
13371
13372
13373
13374
13375
13376
13377
13378
13379
13380
13381
13382
13383
13384
13385
13386
13387
13388
13389
13390
13391
13392
13393
13394
13395
13396
13397
13398
13399
13400
13401
13402
13403
13404
13405
13406
13407
13408
13409
13410
13411
13412
13413
13414
13415
13416
13417
13418
13419
13420
13421
13422
13423
13424
13425
13426
13427
13428
13429
13430
13431
13432
13433
13434
13435
13436
13437
13438
13439
13440
13441
13442
13443
13444
13445
13446
13447
13448
13449
13450
13451
13452
13453
13454
13455
13456
13457
13458
13459
13460
13461
13462
13463
13464
13465
13466
13467
13468
13469
13470
13471
13472
13473
13474
13475
13476
13477
13478
13479
13480
13481
13482
13483
13484
13485
13486
13487
13488
13489
13490
13491
13492
13493
13494
13495
13496
13497
13498
13499
13500
13501
13502
13503
13504
13505
13506
13507
13508
13509
13510
13511
13512
13513
13514
13515
13516
13517
13518
13519
13520
13521
13522
13523
13524
13525
13526
13527
13528
13529
13530
13531
13532
13533
13534
13535
13536
13537
13538
13539
13540
13541
13542
13543
13544
13545
13546
13547
13548
13549
13550
13551
13552
13553
13554
13555
13556
13557
13558
13559
13560
13561
13562
13563
13564
13565
13566
13567
13568
13569
13570
13571
13572
13573
13574
13575
13576
13577
13578
13579
13580
13581
13582
13583
13584
13585
13586
13587
13588
13589
13590
13591
13592
13593
13594
13595
13596
13597
13598
13599
13600
13601
13602
13603
13604
13605
13606
13607
13608
13609
13610
13611
13612
13613
13614
13615
13616
13617
13618
13619
13620
13621
13622
13623
13624
13625
13626
13627
13628
13629
13630
13631
13632
13633
13634
13635
13636
13637
13638
13639
13640
13641
13642
13643
13644
13645
13646
13647
13648
13649
13650
13651
13652
13653
13654
13655
13656
13657
13658
13659
13660
13661
13662
13663
13664
13665
13666
13667
13668
13669
13670
13671
13672
13673
13674
13675
13676
13677
13678
13679
13680
13681
13682
13683
13684
13685
13686
13687
13688
13689
13690
13691
13692
13693
13694
13695
13696
13697
13698
13699
13700
13701
13702
13703
13704
13705
13706
13707
13708
13709
13710
13711
13712
13713
13714
13715
13716
13717
13718
13719
13720
13721
13722
13723
13724
13725
13726
13727
13728
13729
13730
13731
13732
13733
13734
13735
13736
13737
13738
13739
13740
13741
13742
13743
13744
13745
13746
13747
13748
13749
13750
13751
13752
13753
13754
13755
13756
13757
13758
13759
13760
13761
13762
13763
13764
13765
13766
13767
13768
13769
13770
13771
13772
13773
13774
13775
13776
13777
13778
13779
13780
13781
13782
13783
13784
13785
13786
13787
13788
13789
13790
13791
13792
13793
13794
13795
13796
13797
13798
13799
13800
13801
13802
13803
13804
13805
13806
13807
13808
13809
13810
13811
13812
13813
13814
13815
13816
13817
13818
13819
13820
13821
13822
13823
13824
13825
13826
13827
13828
13829
13830
13831
13832
13833
13834
13835
13836
13837
13838
13839
13840
13841
13842
13843
13844
13845
13846
13847
13848
13849
13850
13851
13852
13853
13854
13855
13856
13857
13858
13859
13860
13861
13862
13863
13864
13865
13866
13867
13868
13869
13870
13871
13872
13873
13874
13875
13876
13877
13878
13879
13880
13881
13882
13883
13884
13885
13886
13887
13888
13889
13890
13891
13892
13893
13894
13895
13896
13897
13898
13899
13900
13901
13902
13903
13904
13905
13906
13907
13908
13909
13910
13911
13912
13913
13914
13915
13916
13917
13918
13919
13920
13921
13922
13923
13924
13925
13926
13927
13928
13929
13930
13931
13932
13933
13934
13935
13936
13937
13938
13939
13940
13941
13942
13943
13944
13945
13946
13947
13948
13949
13950
13951
13952
13953
13954
13955
13956
13957
13958
13959
13960
13961
13962
13963
13964
13965
13966
13967
13968
13969
13970
13971
13972
13973
13974
13975
13976
13977
13978
13979
13980
13981
13982
13983
13984
13985
13986
13987
13988
13989
13990
13991
13992
13993
13994
13995
13996
13997
13998
13999
14000
14001
14002
14003
14004
14005
14006
14007
14008
14009
14010
14011
14012
14013
14014
14015
14016
14017
14018
14019
14020
14021
14022
14023
14024
14025
14026
14027
14028
14029
14030
14031
14032
14033
14034
14035
14036
14037
14038
14039
14040
14041
14042
14043
14044
14045
14046
14047
14048
14049
14050
14051
14052
14053
14054
14055
14056
14057
14058
14059
14060
14061
14062
14063
14064
14065
14066
14067
14068
14069
14070
14071
14072
14073
14074
14075
14076
14077
14078
14079
14080
14081
14082
14083
14084
14085
14086
14087
14088
14089
14090
14091
14092
14093
14094
14095
14096
14097
14098
14099
14100
14101
14102
14103
14104
14105
14106
14107
14108
14109
14110
14111
14112
14113
14114
14115
14116
14117
14118
14119
14120
14121
14122
14123
14124
14125
14126
14127
14128
14129
14130
14131
14132
14133
14134
14135
14136
14137
14138
14139
14140
14141
14142
14143
14144
14145
14146
14147
14148
14149
14150
14151
14152
14153
14154
14155
14156
14157
14158
14159
14160
14161
14162
14163
14164
14165
14166
14167
14168
14169
14170
14171
14172
14173
14174
14175
14176
14177
14178
14179
14180
14181
14182
14183
14184
14185
14186
14187
14188
14189
14190
14191
14192
14193
14194
14195
14196
14197
14198
14199
14200
14201
14202
14203
14204
14205
14206
14207
14208
14209
14210
14211
14212
14213
14214
14215
14216
14217
14218
14219
14220
14221
14222
14223
14224
14225
14226
14227
14228
14229
14230
14231
14232
14233
14234
14235
14236
14237
14238
14239
14240
14241
14242
14243
14244
14245
14246
14247
14248
14249
14250
14251
14252
14253
14254
14255
14256
14257
14258
14259
14260
14261
14262
14263
14264
14265
14266
14267
14268
14269
14270
14271
14272
14273
14274
14275
14276
14277
14278
14279
14280
14281
14282
14283
14284
14285
14286
14287
14288
14289
14290
14291
14292
14293
14294
14295
14296
14297
14298
14299
14300
14301
14302
14303
14304
14305
14306
14307
14308
14309
14310
14311
14312
14313
14314
14315
14316
14317
14318
14319
14320
14321
14322
14323
14324
14325
14326
14327
14328
14329
14330
14331
14332
14333
14334
14335
14336
14337
14338
14339
14340
14341
14342
14343
14344
14345
14346
14347
14348
14349
14350
14351
14352
14353
14354
14355
14356
14357
14358
14359
14360
14361
14362
14363
14364
14365
14366
14367
14368
14369
14370
14371
14372
14373
14374
14375
14376
14377
14378
14379
14380
14381
14382
14383
14384
14385
14386
14387
14388
14389
14390
14391
14392
14393
14394
14395
14396
14397
14398
14399
14400
14401
14402
14403
14404
14405
14406
14407
14408
14409
14410
14411
14412
14413
14414
14415
14416
14417
14418
14419
14420
14421
14422
14423
14424
14425
14426
14427
14428
14429
14430
14431
14432
14433
14434
14435
14436
14437
14438
14439
14440
14441
14442
14443
14444
14445
14446
14447
14448
14449
14450
14451
14452
14453
14454
14455
14456
14457
14458
14459
14460
14461
14462
14463
14464
14465
14466
14467
14468
14469
14470
14471
14472
14473
14474
14475
14476
14477
14478
14479
14480
14481
14482
14483
14484
14485
14486
14487
14488
14489
14490
14491
14492
14493
14494
14495
14496
14497
14498
14499
14500
14501
14502
14503
14504
14505
14506
14507
14508
14509
14510
14511
14512
14513
14514
14515
14516
14517
14518
14519
14520
14521
14522
14523
14524
14525
14526
14527
14528
14529
14530
14531
14532
14533
14534
14535
14536
14537
14538
14539
14540
14541
14542
14543
14544
14545
14546
14547
14548
14549
14550
14551
14552
14553
14554
14555
14556
14557
14558
14559
14560
14561
14562
14563
14564
14565
14566
14567
14568
14569
14570
14571
14572
14573
14574
14575
14576
14577
14578
14579
14580
14581
14582
14583
14584
14585
14586
14587
14588
14589
14590
14591
14592
14593
14594
14595
14596
14597
14598
14599
14600
14601
14602
14603
14604
14605
14606
14607
14608
14609
14610
14611
14612
14613
14614
14615
14616
14617
14618
14619
14620
14621
14622
14623
14624
14625
14626
14627
14628
14629
14630
14631
14632
14633
14634
14635
14636
14637
14638
14639
14640
14641
14642
14643
14644
14645
14646
14647
14648
14649
14650
14651
14652
14653
14654
14655
14656
14657
14658
14659
14660
14661
14662
14663
14664
14665
14666
14667
14668
14669
14670
14671
14672
14673
14674
14675
14676
14677
14678
14679
14680
14681
14682
14683
14684
14685
14686
14687
14688
14689
14690
14691
14692
14693
14694
14695
14696
14697
14698
14699
14700
14701
14702
14703
14704
14705
14706
14707
14708
14709
14710
14711
14712
14713
14714
14715
14716
14717
14718
14719
14720
14721
14722
14723
14724
14725
14726
14727
14728
14729
14730
14731
14732
14733
14734
14735
14736
14737
14738
14739
14740
14741
14742
14743
14744
14745
14746
14747
14748
14749
14750
14751
14752
14753
14754
14755
14756
14757
14758
14759
14760
14761
14762
14763
14764
14765
14766
14767
14768
14769
14770
14771
14772
14773
14774
14775
14776
14777
14778
14779
14780
14781
14782
14783
14784
14785
14786
14787
14788
14789
14790
14791
14792
14793
14794
14795
14796
14797
14798
14799
14800
14801
14802
14803
14804
14805
14806
14807
14808
14809
14810
14811
14812
14813
14814
14815
14816
14817
14818
14819
14820
14821
14822
14823
14824
14825
14826
14827
14828
14829
14830
14831
14832
14833
14834
14835
14836
14837
14838
14839
14840
14841
14842
14843
14844
14845
14846
14847
14848
14849
14850
14851
14852
14853
14854
14855
14856
14857
14858
14859
14860
14861
14862
14863
14864
14865
14866
14867
14868
14869
14870
14871
14872
14873
14874
14875
14876
14877
14878
14879
14880
14881
14882
14883
14884
14885
14886
14887
14888
14889
14890
14891
14892
14893
14894
14895
14896
14897
14898
14899
14900
14901
14902
14903
14904
14905
14906
14907
14908
14909
14910
14911
14912
14913
14914
14915
14916
14917
14918
14919
14920
14921
14922
14923
14924
14925
14926
14927
14928
14929
14930
14931
14932
14933
14934
14935
14936
14937
14938
14939
14940
14941
14942
14943
14944
14945
14946
14947
14948
14949
14950
14951
14952
14953
14954
14955
14956
14957
14958
14959
14960
14961
14962
14963
14964
14965
14966
14967
14968
14969
14970
14971
14972
14973
14974
14975
14976
14977
14978
14979
14980
14981
14982
14983
14984
14985
14986
14987
14988
14989
14990
14991
14992
14993
14994
14995
14996
14997
14998
14999
15000
15001
15002
15003
15004
15005
15006
15007
15008
15009
15010
15011
15012
15013
15014
15015
15016
15017
15018
15019
15020
15021
15022
15023
15024
15025
15026
15027
15028
15029
15030
15031
15032
15033
15034
15035
15036
15037
15038
15039
15040
15041
15042
15043
15044
15045
15046
15047
15048
15049
15050
15051
15052
15053
15054
15055
15056
15057
15058
15059
15060
15061
15062
15063
15064
15065
15066
15067
15068
15069
15070
15071
15072
15073
15074
15075
15076
15077
15078
15079
15080
15081
15082
15083
15084
15085
15086
15087
15088
15089
15090
15091
15092
15093
15094
15095
15096
15097
15098
15099
15100
15101
15102
15103
15104
15105
15106
15107
15108
15109
15110
15111
15112
15113
15114
15115
15116
15117
15118
15119
15120
15121
15122
15123
15124
15125
15126
15127
15128
15129
15130
15131
15132
15133
15134
15135
15136
15137
15138
15139
15140
15141
15142
15143
15144
15145
15146
15147
15148
15149
15150
15151
15152
15153
15154
15155
15156
15157
15158
15159
15160
15161
15162
15163
15164
15165
15166
15167
15168
15169
15170
15171
15172
15173
15174
15175
15176
15177
15178
15179
15180
15181
15182
15183
15184
15185
15186
15187
15188
15189
15190
15191
15192
15193
15194
15195
15196
15197
15198
15199
15200
15201
15202
15203
15204
15205
15206
15207
15208
15209
15210
15211
15212
15213
15214
15215
15216
15217
15218
15219
15220
15221
15222
15223
15224
15225
15226
15227
15228
15229
15230
15231
15232
15233
15234
15235
15236
15237
15238
15239
15240
15241
15242
15243
15244
15245
15246
15247
15248
15249
15250
15251
15252
15253
15254
15255
15256
15257
15258
15259
15260
15261
15262
15263
15264
15265
15266
15267
15268
15269
15270
15271
15272
15273
15274
15275
15276
15277
15278
15279
15280
15281
15282
15283
15284
15285
15286
15287
15288
15289
15290
15291
15292
15293
15294
15295
15296
15297
15298
15299
15300
15301
15302
15303
15304
15305
15306
15307
15308
15309
15310
15311
15312
15313
15314
15315
15316
15317
15318
15319
15320
15321
15322
15323
15324
15325
15326
15327
15328
15329
15330
15331
15332
15333
15334
15335
15336
15337
15338
15339
15340
15341
15342
15343
15344
15345
15346
15347
15348
15349
15350
15351
15352
15353
15354
15355
15356
15357
15358
15359
15360
15361
15362
15363
15364
15365
15366
15367
15368
15369
15370
15371
15372
15373
15374
15375
15376
15377
15378
15379
15380
15381
15382
15383
15384
15385
15386
15387
15388
15389
15390
15391
15392
15393
15394
15395
15396
15397
15398
15399
15400
15401
15402
15403
15404
15405
15406
15407
15408
15409
15410
15411
15412
15413
15414
15415
15416
15417
15418
15419
15420
15421
15422
15423
15424
15425
15426
15427
15428
15429
15430
15431
15432
15433
15434
15435
15436
15437
15438
15439
15440
15441
15442
15443
15444
15445
15446
15447
15448
15449
15450
15451
15452
15453
15454
15455
15456
15457
15458
15459
15460
15461
15462
15463
15464
15465
15466
15467
15468
15469
15470
15471
15472
15473
15474
15475
15476
15477
15478
15479
15480
15481
15482
15483
15484
15485
15486
15487
15488
15489
15490
15491
15492
15493
15494
15495
15496
15497
15498
15499
15500
15501
15502
15503
15504
15505
15506
15507
15508
15509
15510
15511
15512
15513
15514
15515
15516
15517
15518
15519
15520
15521
15522
15523
15524
15525
15526
15527
15528
15529
15530
15531
15532
15533
15534
15535
15536
15537
15538
15539
15540
15541
15542
15543
15544
15545
15546
15547
15548
15549
15550
15551
15552
15553
15554
15555
15556
15557
15558
15559
15560
15561
15562
15563
15564
15565
15566
15567
15568
15569
15570
15571
15572
15573
15574
15575
15576
15577
15578
15579
15580
15581
15582
15583
15584
15585
15586
15587
15588
15589
15590
15591
15592
15593
15594
15595
15596
15597
15598
15599
15600
15601
15602
15603
15604
15605
15606
15607
15608
15609
15610
15611
15612
15613
15614
15615
15616
15617
15618
15619
15620
15621
15622
15623
15624
15625
15626
15627
15628
15629
15630
15631
15632
15633
15634
15635
15636
15637
15638
15639
15640
15641
15642
15643
15644
15645
15646
15647
15648
15649
15650
15651
15652
15653
15654
15655
15656
15657
15658
15659
15660
15661
15662
15663
15664
15665
15666
15667
15668
15669
15670
15671
15672
15673
15674
15675
15676
15677
15678
15679
15680
15681
15682
15683
15684
15685
15686
15687
15688
15689
15690
15691
15692
15693
15694
15695
15696
15697
15698
15699
15700
15701
15702
15703
15704
15705
15706
15707
15708
15709
15710
15711
15712
15713
15714
15715
15716
15717
15718
15719
15720
15721
15722
15723
15724
15725
15726
15727
15728
15729
15730
15731
15732
15733
15734
15735
15736
15737
15738
15739
15740
15741
15742
15743
15744
15745
15746
15747
15748
15749
15750
15751
15752
15753
15754
15755
15756
15757
15758
15759
15760
15761
15762
15763
15764
15765
15766
15767
15768
15769
15770
15771
15772
15773
15774
15775
15776
15777
15778
15779
15780
15781
15782
15783
15784
15785
15786
15787
15788
15789
15790
15791
15792
15793
15794
15795
15796
15797
15798
15799
15800
15801
15802
15803
15804
15805
15806
15807
15808
15809
15810
15811
15812
15813
15814
15815
15816
15817
15818
15819
15820
15821
15822
15823
15824
15825
15826
15827
15828
15829
15830
15831
15832
15833
15834
15835
15836
15837
15838
15839
15840
15841
15842
15843
15844
15845
15846
15847
15848
15849
15850
15851
15852
15853
15854
15855
15856
15857
15858
15859
15860
15861
15862
15863
15864
15865
15866
15867
15868
15869
15870
15871
15872
15873
15874
15875
15876
15877
15878
15879
15880
15881
15882
15883
15884
15885
15886
15887
15888
15889
15890
15891
15892
15893
15894
15895
15896
15897
15898
15899
15900
15901
15902
15903
15904
15905
15906
15907
15908
15909
15910
15911
15912
15913
15914
15915
15916
15917
15918
15919
15920
15921
15922
15923
15924
15925
15926
15927
15928
15929
15930
15931
15932
15933
15934
15935
15936
15937
15938
15939
15940
15941
15942
15943
15944
15945
15946
15947
15948
15949
15950
15951
15952
15953
15954
15955
15956
15957
15958
15959
15960
15961
15962
15963
15964
15965
15966
15967
15968
15969
15970
15971
15972
15973
15974
15975
15976
15977
15978
15979
15980
15981
15982
15983
15984
15985
15986
15987
15988
15989
15990
15991
15992
15993
15994
15995
15996
15997
15998
15999
16000
16001
16002
16003
16004
16005
16006
16007
16008
16009
16010
16011
16012
16013
16014
16015
16016
16017
16018
16019
16020
16021
16022
16023
16024
16025
16026
16027
16028
16029
16030
16031
16032
16033
16034
16035
16036
16037
16038
16039
16040
16041
16042
16043
16044
16045
16046
16047
16048
16049
16050
16051
16052
16053
16054
16055
16056
16057
16058
16059
16060
16061
16062
16063
16064
16065
16066
16067
16068
16069
16070
16071
16072
16073
16074
16075
16076
16077
16078
16079
16080
16081
16082
16083
16084
16085
16086
16087
16088
16089
16090
16091
16092
16093
16094
16095
16096
16097
16098
16099
16100
16101
16102
16103
16104
16105
16106
16107
16108
16109
16110
16111
16112
16113
16114
16115
16116
16117
16118
16119
16120
16121
16122
16123
16124
16125
16126
16127
16128
16129
16130
16131
16132
16133
16134
16135
16136
16137
16138
16139
16140
16141
16142
16143
16144
16145
16146
16147
16148
16149
16150
16151
16152
16153
16154
16155
16156
16157
16158
16159
16160
16161
16162
16163
16164
16165
16166
16167
16168
16169
16170
16171
16172
16173
16174
16175
16176
16177
16178
16179
16180
16181
16182
16183
16184
16185
16186
16187
16188
16189
16190
16191
16192
16193
16194
16195
16196
16197
16198
16199
16200
16201
16202
16203
16204
16205
16206
16207
16208
16209
16210
16211
16212
16213
16214
16215
16216
16217
16218
16219
16220
16221
16222
16223
16224
16225
16226
16227
16228
16229
16230
16231
16232
16233
16234
16235
16236
16237
16238
16239
16240
16241
16242
16243
16244
16245
16246
16247
16248
16249
16250
16251
16252
16253
16254
16255
16256
16257
16258
16259
16260
16261
16262
16263
16264
16265
16266
16267
16268
16269
16270
16271
16272
16273
16274
16275
16276
16277
16278
16279
16280
16281
16282
16283
16284
16285
16286
16287
16288
16289
16290
16291
16292
16293
16294
16295
16296
16297
16298
16299
16300
16301
16302
16303
16304
16305
16306
16307
16308
16309
16310
16311
16312
16313
16314
16315
16316
16317
16318
16319
16320
16321
16322
16323
16324
16325
16326
16327
16328
16329
16330
16331
16332
16333
16334
16335
16336
16337
16338
16339
16340
16341
16342
16343
16344
16345
16346
16347
16348
16349
16350
16351
16352
16353
16354
16355
16356
16357
16358
16359
16360
16361
16362
16363
16364
16365
16366
16367
16368
16369
16370
16371
16372
16373
16374
16375
16376
16377
16378
16379
16380
16381
16382
16383
16384
16385
16386
16387
16388
16389
16390
16391
16392
16393
16394
16395
16396
16397
16398
16399
16400
16401
16402
16403
16404
16405
16406
16407
16408
16409
16410
16411
16412
16413
16414
16415
16416
16417
16418
16419
16420
16421
16422
16423
16424
16425
16426
16427
16428
16429
16430
16431
16432
16433
16434
16435
16436
16437
16438
16439
16440
16441
16442
16443
16444
16445
16446
16447
16448
16449
16450
16451
16452
16453
16454
16455
16456
16457
16458
16459
16460
16461
16462
16463
16464
16465
16466
16467
16468
16469
16470
16471
16472
16473
16474
16475
16476
16477
16478
16479
16480
16481
16482
16483
16484
16485
16486
16487
16488
16489
16490
16491
16492
16493
16494
16495
16496
16497
16498
16499
16500
16501
16502
16503
16504
16505
16506
16507
16508
16509
16510
16511
16512
16513
16514
16515
16516
16517
16518
16519
16520
16521
16522
16523
16524
16525
16526
16527
16528
16529
16530
16531
16532
16533
16534
16535
16536
16537
16538
16539
16540
16541
16542
16543
16544
16545
16546
16547
16548
16549
16550
16551
16552
16553
16554
16555
16556
16557
16558
16559
16560
16561
16562
16563
16564
16565
16566
16567
16568
16569
16570
16571
16572
16573
16574
16575
16576
16577
16578
16579
16580
16581
16582
16583
16584
16585
16586
16587
16588
16589
16590
16591
16592
16593
16594
16595
16596
16597
16598
16599
16600
16601
16602
16603
16604
16605
16606
16607
16608
16609
16610
16611
16612
16613
16614
16615
16616
16617
16618
16619
16620
16621
16622
16623
16624
16625
16626
16627
16628
16629
16630
16631
16632
16633
16634
16635
16636
16637
16638
16639
16640
16641
16642
16643
16644
16645
16646
16647
16648
16649
16650
16651
16652
16653
16654
16655
16656
16657
16658
16659
16660
16661
16662
16663
16664
16665
16666
16667
16668
16669
16670
16671
16672
16673
16674
16675
16676
16677
16678
16679
16680
16681
16682
16683
16684
16685
16686
16687
16688
16689
16690
16691
16692
16693
16694
16695
16696
16697
16698
16699
16700
16701
16702
16703
16704
16705
16706
16707
16708
16709
16710
16711
16712
16713
16714
16715
16716
16717
16718
16719
16720
16721
16722
16723
16724
16725
16726
16727
16728
16729
16730
16731
16732
16733
16734
16735
16736
16737
16738
16739
16740
16741
16742
16743
16744
16745
16746
16747
16748
16749
16750
16751
16752
16753
16754
16755
16756
16757
16758
16759
16760
16761
16762
16763
16764
16765
16766
16767
16768
16769
16770
16771
16772
16773
16774
16775
16776
16777
16778
16779
16780
16781
16782
16783
16784
16785
16786
16787
16788
16789
16790
16791
16792
16793
16794
16795
16796
16797
16798
16799
16800
16801
16802
16803
16804
16805
16806
16807
16808
16809
16810
16811
16812
16813
16814
16815
16816
16817
16818
16819
16820
16821
16822
16823
16824
16825
16826
16827
16828
16829
16830
16831
16832
16833
16834
16835
16836
16837
16838
16839
16840
16841
16842
16843
16844
16845
16846
16847
16848
16849
16850
16851
16852
16853
16854
16855
16856
16857
16858
16859
16860
16861
16862
16863
16864
16865
16866
16867
16868
16869
16870
16871
16872
16873
16874
16875
16876
16877
16878
16879
16880
16881
16882
16883
16884
16885
16886
16887
16888
16889
16890
16891
16892
16893
16894
16895
16896
16897
16898
16899
16900
16901
16902
16903
16904
16905
16906
16907
16908
16909
16910
16911
16912
16913
16914
16915
16916
16917
16918
16919
16920
16921
16922
16923
16924
16925
16926
16927
16928
16929
16930
16931
16932
16933
16934
16935
16936
16937
16938
16939
16940
16941
16942
16943
16944
16945
16946
16947
16948
16949
16950
16951
16952
16953
16954
16955
16956
16957
16958
16959
16960
16961
16962
16963
16964
16965
16966
16967
16968
16969
16970
16971
16972
16973
16974
16975
16976
16977
16978
16979
16980
16981
16982
16983
16984
16985
16986
16987
16988
16989
16990
16991
16992
16993
16994
16995
16996
16997
16998
16999
17000
17001
17002
17003
17004
17005
17006
17007
17008
17009
17010
17011
17012
17013
17014
17015
17016
17017
17018
17019
17020
17021
17022
17023
17024
17025
17026
17027
17028
17029
17030
17031
17032
17033
17034
17035
17036
17037
17038
17039
17040
17041
17042
17043
17044
17045
17046
17047
17048
17049
17050
17051
17052
17053
17054
17055
17056
17057
17058
17059
17060
17061
17062
17063
17064
17065
17066
17067
17068
17069
17070
17071
17072
17073
17074
17075
17076
17077
17078
17079
17080
17081
17082
17083
17084
17085
17086
17087
17088
17089
17090
17091
17092
17093
17094
17095
17096
17097
17098
17099
17100
17101
17102
17103
17104
17105
17106
17107
17108
17109
17110
17111
17112
17113
17114
17115
17116
17117
17118
17119
17120
17121
17122
17123
17124
17125
17126
17127
17128
17129
17130
17131
17132
17133
17134
17135
17136
17137
17138
17139
17140
17141
17142
17143
17144
17145
17146
17147
17148
17149
17150
17151
17152
17153
17154
17155
17156
17157
17158
17159
17160
17161
17162
17163
17164
17165
17166
17167
17168
17169
17170
17171
17172
17173
17174
17175
17176
17177
17178
17179
17180
17181
17182
17183
17184
17185
17186
17187
17188
17189
17190
17191
17192
17193
17194
17195
17196
17197
17198
17199
17200
17201
17202
17203
17204
17205
17206
17207
17208
17209
17210
17211
17212
17213
17214
17215
17216
17217
17218
17219
17220
17221
17222
17223
17224
17225
17226
17227
17228
17229
17230
17231
17232
17233
17234
17235
17236
17237
17238
17239
17240
17241
17242
17243
17244
17245
17246
17247
17248
17249
17250
17251
17252
17253
17254
17255
17256
17257
17258
17259
17260
17261
17262
17263
17264
17265
17266
17267
17268
17269
17270
17271
17272
17273
17274
17275
17276
17277
17278
17279
17280
17281
17282
17283
17284
17285
17286
17287
17288
17289
17290
17291
17292
17293
17294
17295
17296
17297
17298
17299
17300
17301
17302
17303
17304
17305
17306
17307
17308
17309
17310
17311
17312
17313
17314
17315
17316
17317
17318
17319
17320
17321
17322
17323
17324
17325
17326
17327
17328
17329
17330
17331
17332
17333
17334
17335
17336
17337
17338
17339
17340
17341
17342
17343
17344
17345
17346
17347
17348
17349
17350
17351
17352
17353
17354
17355
17356
17357
17358
17359
17360
17361
17362
17363
17364
17365
17366
17367
17368
17369
17370
17371
17372
17373
17374
17375
17376
17377
17378
17379
17380
17381
17382
17383
17384
17385
17386
17387
17388
17389
17390
17391
17392
17393
17394
17395
17396
17397
17398
17399
17400
17401
17402
17403
17404
17405
17406
17407
17408
17409
17410
17411
17412
17413
17414
17415
17416
17417
17418
17419
17420
17421
17422
17423
17424
17425
17426
17427
17428
17429
17430
17431
17432
17433
17434
17435
17436
17437
17438
17439
17440
17441
17442
17443
17444
17445
17446
17447
17448
17449
17450
17451
17452
17453
17454
17455
17456
17457
17458
17459
17460
17461
17462
17463
17464
17465
17466
17467
17468
17469
17470
17471
17472
17473
17474
17475
17476
17477
17478
17479
17480
17481
17482
17483
17484
17485
17486
17487
17488
17489
17490
17491
17492
17493
17494
17495
17496
17497
17498
17499
17500
17501
17502
17503
17504
17505
17506
17507
17508
17509
17510
17511
17512
17513
17514
17515
17516
17517
17518
17519
17520
17521
17522
17523
17524
17525
17526
17527
17528
17529
17530
17531
17532
17533
17534
17535
17536
17537
17538
17539
17540
17541
17542
17543
17544
17545
17546
17547
17548
17549
17550
17551
17552
17553
17554
17555
17556
17557
17558
17559
17560
17561
17562
17563
17564
17565
17566
17567
17568
17569
17570
17571
17572
17573
17574
17575
17576
17577
17578
17579
17580
17581
17582
17583
17584
17585
17586
17587
17588
17589
17590
17591
17592
17593
17594
17595
17596
17597
17598
17599
17600
17601
17602
17603
17604
17605
17606
17607
17608
17609
17610
17611
17612
17613
17614
17615
17616
17617
17618
17619
17620
17621
17622
17623
17624
17625
17626
17627
17628
17629
17630
17631
17632
17633
17634
17635
17636
17637
17638
17639
17640
17641
17642
17643
17644
17645
17646
17647
17648
17649
17650
17651
17652
17653
17654
17655
17656
17657
17658
17659
17660
17661
17662
17663
17664
17665
17666
17667
17668
17669
17670
17671
17672
17673
17674
17675
17676
17677
17678
17679
17680
17681
17682
17683
17684
17685
17686
17687
17688
17689
17690
17691
17692
17693
17694
17695
17696
17697
17698
17699
17700
17701
17702
17703
17704
17705
17706
17707
17708
17709
17710
17711
17712
17713
17714
17715
17716
17717
17718
17719
17720
17721
17722
17723
17724
17725
17726
17727
17728
17729
17730
17731
17732
17733
17734
17735
17736
17737
17738
17739
17740
17741
17742
17743
17744
17745
17746
17747
17748
17749
17750
17751
17752
17753
17754
17755
17756
17757
17758
17759
17760
17761
17762
17763
17764
17765
17766
17767
17768
17769
17770
17771
17772
17773
17774
17775
17776
17777
17778
17779
17780
17781
17782
17783
17784
17785
17786
17787
17788
17789
17790
17791
17792
17793
17794
17795
17796
17797
17798
17799
17800
17801
17802
17803
17804
17805
17806
17807
17808
17809
17810
17811
17812
17813
17814
17815
17816
17817
17818
17819
17820
17821
17822
17823
17824
17825
17826
17827
17828
17829
17830
17831
17832
17833
17834
17835
17836
17837
17838
17839
17840
17841
17842
17843
17844
17845
17846
17847
17848
17849
17850
17851
17852
17853
17854
17855
17856
17857
17858
17859
17860
17861
17862
17863
17864
17865
17866
17867
17868
17869
17870
17871
17872
17873
17874
17875
17876
17877
17878
17879
17880
17881
17882
17883
17884
17885
17886
17887
17888
17889
17890
17891
17892
17893
17894
17895
17896
17897
17898
17899
17900
17901
17902
17903
17904
17905
17906
17907
17908
17909
17910
17911
17912
17913
17914
17915
17916
17917
17918
17919
17920
17921
17922
17923
17924
17925
17926
17927
17928
17929
17930
17931
17932
17933
17934
17935
17936
17937
17938
17939
17940
17941
17942
17943
17944
17945
17946
17947
17948
17949
17950
17951
17952
17953
17954
17955
17956
17957
17958
17959
17960
17961
17962
17963
17964
17965
17966
17967
17968
17969
17970
17971
17972
17973
17974
17975
17976
17977
17978
17979
17980
17981
17982
17983
17984
17985
17986
17987
17988
17989
17990
17991
17992
17993
17994
17995
17996
17997
17998
17999
18000
18001
18002
18003
18004
18005
18006
18007
18008
18009
18010
18011
18012
18013
18014
18015
18016
18017
18018
18019
18020
18021
18022
18023
18024
18025
18026
18027
18028
18029
18030
18031
18032
18033
18034
18035
18036
18037
18038
18039
18040
18041
18042
18043
18044
18045
18046
18047
18048
18049
18050
18051
18052
18053
18054
18055
18056
18057
18058
18059
18060
18061
18062
18063
18064
18065
18066
18067
18068
18069
18070
18071
18072
18073
18074
18075
18076
18077
18078
18079
18080
18081
18082
18083
18084
18085
18086
18087
18088
18089
18090
18091
18092
18093
18094
18095
18096
18097
18098
18099
18100
18101
18102
18103
18104
18105
18106
18107
18108
18109
18110
18111
18112
18113
18114
18115
18116
18117
18118
18119
18120
18121
18122
18123
18124
18125
18126
18127
18128
18129
18130
18131
18132
18133
18134
18135
18136
18137
18138
18139
18140
18141
18142
18143
18144
18145
18146
18147
18148
18149
18150
18151
18152
18153
18154
18155
18156
18157
18158
18159
18160
18161
18162
18163
18164
18165
18166
18167
18168
18169
18170
18171
18172
18173
18174
18175
18176
18177
18178
18179
18180
18181
18182
18183
18184
18185
18186
18187
18188
18189
18190
18191
18192
18193
18194
18195
18196
18197
18198
18199
18200
18201
18202
18203
18204
18205
18206
18207
18208
18209
18210
18211
18212
18213
18214
18215
18216
18217
18218
18219
18220
18221
18222
18223
18224
18225
18226
18227
18228
18229
18230
18231
18232
18233
18234
18235
18236
18237
18238
18239
18240
18241
18242
18243
18244
18245
18246
18247
18248
18249
18250
18251
18252
18253
18254
18255
18256
18257
18258
18259
18260
18261
18262
18263
18264
18265
18266
18267
18268
18269
18270
18271
18272
18273
18274
18275
18276
18277
18278
18279
18280
18281
18282
18283
18284
18285
18286
18287
18288
18289
18290
18291
18292
18293
18294
18295
18296
18297
18298
18299
18300
18301
18302
18303
18304
18305
18306
18307
18308
18309
18310
18311
18312
18313
18314
18315
18316
18317
18318
18319
18320
18321
18322
18323
18324
18325
18326
18327
18328
18329
18330
18331
18332
18333
18334
18335
18336
18337
18338
18339
18340
18341
18342
18343
18344
18345
18346
18347
18348
18349
18350
18351
18352
18353
18354
18355
18356
18357
18358
18359
18360
18361
18362
18363
18364
18365
18366
18367
18368
18369
18370
18371
18372
18373
18374
18375
18376
18377
18378
18379
18380
18381
18382
18383
18384
18385
18386
18387
18388
18389
18390
18391
18392
18393
18394
18395
18396
18397
18398
18399
18400
18401
18402
18403
18404
18405
18406
18407
18408
18409
18410
18411
18412
18413
18414
18415
18416
18417
18418
18419
18420
18421
18422
18423
18424
18425
18426
18427
18428
18429
18430
18431
18432
18433
18434
18435
18436
18437
18438
18439
18440
18441
18442
18443
18444
18445
18446
18447
18448
18449
18450
18451
18452
18453
18454
18455
18456
18457
18458
18459
18460
18461
18462
18463
18464
18465
18466
18467
18468
18469
18470
18471
18472
18473
18474
18475
18476
18477
18478
18479
18480
18481
18482
18483
18484
18485
18486
18487
18488
18489
18490
18491
18492
18493
18494
18495
18496
18497
18498
18499
18500
18501
18502
18503
18504
18505
18506
18507
18508
18509
18510
18511
18512
18513
18514
18515
18516
18517
18518
18519
18520
18521
18522
18523
18524
18525
18526
18527
18528
18529
18530
18531
18532
18533
18534
18535
18536
18537
18538
18539
18540
18541
18542
18543
18544
18545
18546
18547
18548
18549
18550
18551
18552
18553
18554
18555
18556
18557
18558
18559
18560
18561
18562
18563
18564
18565
18566
18567
18568
18569
18570
18571
18572
18573
18574
18575
18576
18577
18578
18579
18580
18581
18582
18583
18584
18585
18586
18587
18588
18589
18590
18591
18592
18593
18594
18595
18596
18597
18598
18599
18600
18601
18602
18603
18604
18605
18606
18607
18608
18609
18610
18611
18612
18613
18614
18615
18616
18617
18618
18619
18620
18621
18622
18623
18624
18625
18626
18627
18628
18629
18630
18631
18632
18633
18634
18635
18636
18637
18638
18639
18640
18641
18642
18643
18644
18645
18646
18647
18648
18649
18650
18651
18652
18653
18654
18655
18656
18657
18658
18659
18660
18661
18662
18663
18664
18665
18666
18667
18668
18669
18670
18671
18672
18673
18674
18675
18676
18677
18678
18679
18680
18681
18682
18683
18684
18685
18686
18687
18688
18689
18690
18691
18692
18693
18694
18695
18696
18697
18698
18699
18700
18701
18702
18703
18704
18705
18706
18707
18708
18709
18710
18711
18712
18713
18714
18715
18716
18717
18718
18719
//! Go code generator — rule-based (Tier 2) transpilation from AIR to Go.
//!
//! Handles all capability gaps:
//! - Records → structs
//! - Traits → interfaces
//! - Algebraic types → structs with tag field + type switch
//! - Pattern matching → switch/type-switch/if-else chains
//! - Effects → interface parameters
//! - Ownership → erased (Go is GC)
//! - Generics → Go type parameters (Go 1.18+)
//! - Concurrency → goroutines/channels
//! - Error handling → `(value, error)` return tuples
//! - String interpolation → `fmt.Sprintf`

use std::collections::{HashMap, HashSet};
use std::fmt::Write;
use std::path::PathBuf;

use bock_air::{
    AIRNode, AirInterpolationPart, EnumVariantPayload, NodeKind, ResultVariant, Visitor,
};
use bock_ast::{AssignOp, BinOp, Literal, TypeExpr, UnaryOp, Visibility};
use bock_types::AIRModule;

use crate::error::CodegenError;
use crate::generator::{CodeGenerator, GeneratedCode, OutputFile, SourceMap};
use crate::profile::TargetProfile;

/// Collects the value-identifier names *referenced* anywhere in a subtree.
///
/// Used by the Go emitter's unused-binding guard: Go rejects a `let`-bound local
/// that is never read (`declared and not used`), which Bock permits (a binding
/// kept only for its side effect, or shadowed later). After emitting such a
/// binding we append `_ = name` iff the name is not referenced in the rest of the
/// block — this collector computes that reference set.
///
/// Every [`NodeKind::Identifier`] is counted as a reference. Binding *patterns*
/// (`let x = …`'s `x`) are not identifiers, so they are correctly excluded — only
/// genuine *uses* land here. Conservative by construction: a name that appears in
/// any form (a nested closure, a struct-field value, an interpolation) is seen, so
/// the guard never silences a binding that is actually read.
struct IdentUseCollector {
    used: HashSet<String>,
}

impl Visitor for IdentUseCollector {
    fn visit_node(&mut self, node: &AIRNode) {
        if let NodeKind::Identifier { name } = &node.kind {
            self.used.insert(name.name.clone());
        }
        bock_air::visitor::walk_node(self, node);
    }
}

/// The set of value-identifier names referenced anywhere in `node` (see
/// [`IdentUseCollector`]).
fn collect_used_idents(node: &AIRNode) -> HashSet<String> {
    let mut c = IdentUseCollector {
        used: HashSet::new(),
    };
    c.visit_node(node);
    c.used
}

/// Conservative module scan for `Channel` / `spawn` references.
fn go_module_uses_concurrency(items: &[AIRNode]) -> bool {
    items.iter().any(|n| {
        let s = format!("{n:?}");
        s.contains("\"Channel\"") || s.contains("\"spawn\"")
    })
}

/// Whether a Go loop needs a label so a statement-arm `match`'s `break`/
/// `continue` can target the loop instead of the inner `switch`.
///
/// A label is only required when the jumping `match` lowers to a Go `switch`
/// (where a bare `break` would exit the switch, not the loop). An `Optional`
/// `match` lowers to an `if __opt.tag == "Some" { ... } else { ... }` chain
/// instead — a bare `break`/`continue` there already targets the enclosing
/// `for`, so labelling it produces a *defined-and-not-used* label that Go
/// rejects. This refines the shared [`crate::generator::loop_needs_break_label`]
/// for Go's lowering: it returns true only when a non-Optional statement-arm
/// `match` with a `break`/`continue` is present (not nested under another loop).
fn go_loop_needs_label(body: &AIRNode) -> bool {
    /// Does `node` perform a loop `break`/`continue` reachable from a match arm
    /// without crossing into a nested loop or function?
    fn arm_has_jump(node: &AIRNode) -> bool {
        match &node.kind {
            NodeKind::Break { .. } | NodeKind::Continue => true,
            NodeKind::For { .. }
            | NodeKind::While { .. }
            | NodeKind::Loop { .. }
            | NodeKind::FnDecl { .. }
            | NodeKind::Lambda { .. } => false,
            NodeKind::Block { stmts, tail } => {
                stmts.iter().any(arm_has_jump) || tail.as_deref().is_some_and(arm_has_jump)
            }
            NodeKind::If {
                then_block,
                else_block,
                ..
            } => arm_has_jump(then_block) || else_block.as_deref().is_some_and(arm_has_jump),
            NodeKind::Match { arms, .. } => arms
                .iter()
                .any(|a| matches!(&a.kind, NodeKind::MatchArm { body, .. } if arm_has_jump(body))),
            NodeKind::Guard { else_block, .. } => arm_has_jump(else_block),
            _ => false,
        }
    }
    /// Find a *switch*-lowered (non-Optional) statement-arm match that jumps the
    /// loop, not crossing into a nested loop or function.
    fn find(node: &AIRNode) -> bool {
        match &node.kind {
            NodeKind::For { .. }
            | NodeKind::While { .. }
            | NodeKind::Loop { .. }
            | NodeKind::FnDecl { .. }
            | NodeKind::Lambda { .. } => false,
            NodeKind::Match { arms, .. } => {
                // Optional/Result matches lower to if/else where bare
                // break/continue already target the loop — no label needed for
                // *this* match.
                let this_needs_label = !go_match_is_optional(arms)
                    && !go_match_is_result(arms)
                    && crate::generator::match_has_statement_arm(arms)
                    && arms.iter().any(|a| {
                        matches!(&a.kind, NodeKind::MatchArm { body, .. } if arm_has_jump(body))
                    });
                // Even a non-jumping (or Optional) match may *contain* a nested
                // switch-lowered match that jumps the loop, so always recurse
                // into the arms.
                this_needs_label
                    || arms
                        .iter()
                        .any(|a| matches!(&a.kind, NodeKind::MatchArm { body, .. } if find(body)))
            }
            NodeKind::Block { stmts, tail } => {
                stmts.iter().any(find) || tail.as_deref().is_some_and(find)
            }
            NodeKind::If {
                then_block,
                else_block,
                ..
            } => find(then_block) || else_block.as_deref().is_some_and(find),
            NodeKind::Guard { else_block, .. } => find(else_block),
            _ => false,
        }
    }
    find(body)
}

/// Decide whether a Go `match` should lower to a *type*-switch
/// (`switch v := s.(type) { case T: }`) rather than a *value*-switch
/// (`switch s { case 5: }`).
///
/// Constructor and record patterns dispatch on the scrutinee's dynamic type
/// (enum variants are distinct Go structs), so any such pattern forces a
/// type-switch. Literal and bind patterns dispatch on value. A match whose
/// arms are only wildcard/bind patterns defaults to a value-switch.
fn go_match_is_type_switch(arms: &[AIRNode]) -> bool {
    arms.iter().any(|arm| {
        matches!(
            &arm.kind,
            NodeKind::MatchArm { pattern, .. }
                if matches!(
                    pattern.kind,
                    NodeKind::ConstructorPat { .. } | NodeKind::RecordPat { .. }
                )
        )
    })
}

/// True if any arm is a catch-all (`_` or a bind pattern), which lowers to a Go
/// `default:` case.
fn go_match_has_default_arm(arms: &[AIRNode]) -> bool {
    arms.iter().any(|arm| {
        matches!(
            &arm.kind,
            NodeKind::MatchArm { pattern, .. }
                if matches!(pattern.kind, NodeKind::WildcardPat | NodeKind::BindPat { .. })
        )
    })
}

/// Runtime helpers for Bock concurrency in Go. A Channel is a wrapper
/// over `chan interface{}` so the generic shape is simple; `spawn`
/// launches a goroutine whose result is piped through a 1-element
/// buffered channel (matching the existing Go async-fn wrapper
/// convention — cf. F.4.3).
const CONCURRENCY_RUNTIME_GO: &str = "\
// ── Bock concurrency runtime ──
type __bockChannel struct {
\tq chan interface{}
}

func __bockChannelNew() (*__bockChannel, *__bockChannel) {
\tc := &__bockChannel{q: make(chan interface{}, 1024)}
\treturn c, c
}
func (c *__bockChannel) send(v interface{}) { c.q <- v }
func (c *__bockChannel) recv() interface{}  { return <-c.q }
func (c *__bockChannel) close()              {}

// __bockSpawn launches the passed channel-returning async computation.
// In practice the Go async-fn lowerer already wraps bodies in goroutines,
// so this is the identity on a receive channel.
func __bockSpawn(ch interface{}) interface{} { return ch }
";

/// Runtime helpers for Bock `Optional[T]` in Go. Go has no sum type, so an
/// optional is a tagged struct: `tag` is `"Some"` or `"None"`, `v` carries the
/// payload for `Some`. `__bockSome`/`__bockNone` are the constructors; matches
/// dispatch on `.tag` and read `.v` for the bound value.
///
/// `__bockAsInt64` / `__bockAsFloat64` recover a numeric payload from the
/// `interface{}` box. Bock's `Int`/`Float` are Go `int64`/`float64`, but a
/// payload constructed from an *untyped Go constant* — e.g. `Some(10)` →
/// `__bockSome(10)` — boxes a Go `int` (the default type of an untyped integer
/// constant), not an `int64`. A hard `.(int64)` assertion on that box panics
/// (`interface {} is int, not int64`). These helpers widen the common numeric
/// boxings instead, so a `Some(x)` payload bound for typed use works whether it
/// came from a literal, a typed variable, or arithmetic.
const OPTIONAL_RUNTIME_GO: &str = "// ── Bock Optional runtime ──
type __bockOption struct {
	tag string
	v   interface{}
}

func __bockSome(v interface{}) __bockOption { return __bockOption{tag: \"Some\", v: v} }

var __bockNone = __bockOption{tag: \"None\"}
";

/// Shared numeric-widening helpers used by both the `Optional` and `Result`
/// runtimes to recover an `int64`/`float64` payload from the `interface{}` box.
///
/// A payload constructed from an *untyped Go constant* — e.g. `Some(10)` /
/// `Ok(10)` → `__bockSome(10)` / `__bockOk(10)` — boxes a Go `int` (the default
/// type of an untyped integer constant), not an `int64`. A hard `.(int64)`
/// assertion on that box panics (`interface {} is int, not int64`). These helpers
/// widen the common numeric boxings instead. Emitted once if *either* container
/// runtime is used (its own emit flag), so the two runtimes never redeclare them.
const NUMERIC_RUNTIME_GO: &str = "// ── Bock numeric payload helpers ──
func __bockAsInt64(v interface{}) int64 {
	switch n := v.(type) {
	case int64:
		return n
	case int:
		return int64(n)
	case int32:
		return int64(n)
	case float64:
		return int64(n)
	default:
		return 0
	}
}

func __bockAsFloat64(v interface{}) float64 {
	switch n := v.(type) {
	case float64:
		return n
	case float32:
		return float64(n)
	case int64:
		return float64(n)
	case int:
		return float64(n)
	default:
		return 0
	}
}
";

/// Runtime for Bock `Result[T, E]` in Go. Mirrors `OPTIONAL_RUNTIME_GO`: a
/// tagged struct (`tag` is `"Ok"`/`"Err"`, `v` carries the payload), with
/// `__bockOk`/`__bockErr` constructors. A `match r { Ok(v) => …; Err(e) => … }`
/// dispatches on `.tag` and reads `.v` for the bound value — the same tag-switch
/// the Optional match uses, not the user-enum type-switch (`case Ok:` against an
/// undefined Go type) the broken codegen produced.
const RESULT_RUNTIME_GO: &str = "// ── Bock Result runtime ──
type __bockResult struct {
	tag string
	v   interface{}
}

func __bockOk(v interface{}) __bockResult { return __bockResult{tag: \"Ok\", v: v} }

func __bockErr(v interface{}) __bockResult { return __bockResult{tag: \"Err\", v: v} }
";

/// Runtime helper for Bock range expressions (`0..n` / `0..=n`) in Go. Go has
/// no native range *value*, so `for i in 0..n` lowers to
/// `for _, i := range __bockRange(0, n, false)`; this builds the `[]int64`
/// slice with half-open (`inclusive=false`) or inclusive (`inclusive=true`)
/// bounds, matching Python's `range(lo, hi)` / `range(lo, hi + 1)` and Rust's
/// `lo..hi` / `lo..=hi`. Emitted once into the shared `bock_runtime.go`
/// (per-module path) or inlined at most once (single-module path), gated on a
/// ctx flag (mirrors `OPTIONAL_RUNTIME_GO`).
const RANGE_RUNTIME_GO: &str = "// ── Bock range runtime ──
func __bockRange(lo int64, hi int64, inclusive bool) []int64 {
	end := hi
	if inclusive {
		end = hi + 1
	}
	r := make([]int64, 0)
	for i := lo; i < end; i++ {
		r = append(r, i)
	}
	return r
}
";

/// Integer exponentiation helper for the `**` operator on integer operands.
/// Go has no `**` and `math.Pow` returns `float64` (losing integer precision and
/// type), so an `Int ** Int` lowers to a call to this helper, which does
/// fast exponentiation-by-squaring and stays in `int64`. A negative exponent
/// yields `0` (an integer power with a negative exponent has no `int64` value;
/// Bock callers using fractional results use `Float ** Float`, which routes to
/// `math.Pow`). Gated by [`go_module_uses_int_pow`] so it is emitted only when a
/// `**` with non-float operands is present.
const INT_POW_RUNTIME_GO: &str = "// ── Bock integer-power runtime ──
func __bockIntPow(base int64, exp int64) int64 {
	if exp < 0 {
		return 0
	}
	result := int64(1)
	for exp > 0 {
		if exp&1 == 1 {
			result *= base
		}
		base *= base
		exp >>= 1
	}
	return result
}
";

/// True if the module references a `Range` node anywhere (so the range runtime
/// helper must be emitted). Mirrors [`go_module_uses_optional`]. `RangePat`
/// (a match-arm range pattern) does not contain the `Range {` substring, so it
/// is not matched — the helper is only needed for range *values*.
fn go_module_uses_range(items: &[AIRNode]) -> bool {
    items.iter().any(|n| format!("{n:?}").contains("Range {"))
}

/// True if the module contains any `**` (`BinOp::Pow`) operator (so the
/// integer-power runtime helper must be emitted). The float path lowers to
/// `math.Pow`; the int path calls [`INT_POW_RUNTIME_GO`]'s `__bockIntPow`. We
/// emit the helper whenever *any* `**` is present (Go tolerates an unused
/// package-level func, so a float-only program harmlessly carries it), rather
/// than re-deriving operand types here. Mirrors [`go_module_uses_range`]'s
/// structural debug scan: a `BinaryOp { op: Pow` renders that substring.
fn go_module_uses_int_pow(items: &[AIRNode]) -> bool {
    items.iter().any(|n| format!("{n:?}").contains("op: Pow"))
}

/// Runtime helper for the DQ29 `"deep"` equality lane: `==`/`!=` whose operand
/// (transitively) involves a `List`/`Map`/`Set` — Go has no `==` for slices or
/// maps ("can only be compared to nil", a compile error). `reflect.DeepEqual`
/// gives exactly the §18.5 semantics: element-wise for slices, content-based
/// and ORDER-INDEPENDENT for maps (Bock `Map` and `Set` both lower to Go
/// maps), recursive through struct fields, and IEEE for floats (`NaN`-holding
/// values are not deeply equal — the DQ10 caveat). Needs `import "reflect"`
/// wherever it is emitted. The shallow lanes never route here: Go struct /
/// interface `==` is already field-wise.
const DEEP_EQ_RUNTIME_GO: &str = "// ── Bock structural equality runtime ──
func __bockDeepEq(a any, b any) bool {
	return reflect.DeepEqual(a, b)
}

// DQ31 (§18.5 container equality defers to element conformance): the
// custom-element deep-equality lane. A container whose element tree carries an
// explicit `impl Equatable` (the `\"deep_custom\"` lane) must compare elements
// — and match Map keys / Set members — through the element's `Eq` method, NOT
// `reflect.DeepEqual` (which is field-wise and would silently ignore the
// custom equality, diverging from the other targets). Falls back to
// `reflect.DeepEqual` for any value lacking a custom `Eq`, so all-structural
// sub-trees behave identically to the `\"deep\"` lane.
func __bockEqCustom(a any, b any) bool {
	// Optional/Result runtime wrappers carry their payload in an unexported
	// `v interface{}` field, so the generic `reflect.Struct` arm below cannot
	// reach it (`CanInterface()` is false) and bails to `reflect.DeepEqual`,
	// which would IGNORE a custom `impl Equatable` on the payload (DQ31). Match
	// the variant tag, then recurse on the payload through `__bockEqCustom` so a
	// custom-`Eq` element inside the wrapper defers to its `Eq`.
	if oa, ok := a.(__bockOption); ok {
		ob, ok := b.(__bockOption)
		if !ok || oa.tag != ob.tag {
			return false
		}
		if oa.tag == \"None\" {
			return true
		}
		return __bockEqCustom(oa.v, ob.v)
	}
	if ra, ok := a.(__bockResult); ok {
		rb, ok := b.(__bockResult)
		if !ok || ra.tag != rb.tag {
			return false
		}
		return __bockEqCustom(ra.v, rb.v)
	}
	va := reflect.ValueOf(a)
	vb := reflect.ValueOf(b)
	// A value with a custom `Eq(Self) bool` method defines its own equality.
	if eq := va.MethodByName(\"Eq\"); eq.IsValid() && va.Type() == vb.Type() {
		mt := eq.Type()
		if mt.NumIn() == 1 && mt.NumOut() == 1 && mt.Out(0).Kind() == reflect.Bool {
			out := eq.Call([]reflect.Value{vb})
			return out[0].Bool()
		}
	}
	if !va.IsValid() || !vb.IsValid() || va.Kind() != vb.Kind() {
		return reflect.DeepEqual(a, b)
	}
	switch va.Kind() {
	case reflect.Slice, reflect.Array:
		if va.Len() != vb.Len() {
			return false
		}
		for i := 0; i < va.Len(); i++ {
			if !__bockEqCustom(va.Index(i).Interface(), vb.Index(i).Interface()) {
				return false
			}
		}
		return true
	case reflect.Map:
		if va.Len() != vb.Len() {
			return false
		}
		// Order-independent: every (k, v) in a must match some (bk, bv) in b
		// under custom element equality (the key's `Eq` governs key-matching,
		// the value's `Eq` the value comparison). A Set lowers to a Go
		// map[T]struct{}, so membership is the same scan with a trivial value.
		bKeys := vb.MapKeys()
		for _, ka := range va.MapKeys() {
			found := false
			for _, kb := range bKeys {
				if __bockEqCustom(ka.Interface(), kb.Interface()) {
					if __bockEqCustom(va.MapIndex(ka).Interface(), vb.MapIndex(kb).Interface()) {
						found = true
						break
					}
				}
			}
			if !found {
				return false
			}
		}
		return true
	case reflect.Ptr, reflect.Interface:
		if va.IsNil() || vb.IsNil() {
			return va.IsNil() == vb.IsNil()
		}
		return __bockEqCustom(va.Elem().Interface(), vb.Elem().Interface())
	case reflect.Struct:
		// Records and tuples (struct{ Field0 …; Field1 … }): recurse field-wise
		// so a custom-`Eq` element nested in a struct still defers to its `Eq`.
		// Codegen emits exported (PascalCased / `Field0`-style) fields, all
		// reachable through reflection. If any field is unexported (a runtime
		// wrapper this helper does not special-case above), fall back to
		// `reflect.DeepEqual` for the whole value rather than ignore it.
		for i := 0; i < va.NumField(); i++ {
			if !va.Field(i).CanInterface() {
				return reflect.DeepEqual(a, b)
			}
			if !__bockEqCustom(va.Field(i).Interface(), vb.Field(i).Interface()) {
				return false
			}
		}
		return true
	default:
		return reflect.DeepEqual(a, b)
	}
}
";

/// True if the module contains a `"deep"`- or `"deep_custom"`-lane equality (so
/// [`DEEP_EQ_RUNTIME_GO`] must be emitted and `\"reflect\"` imported). Mirrors
/// [`go_module_uses_range`]'s structural debug scan over the checker's
/// `user_eq` metadata stamp. The `"deep_custom"` lane (DQ31) routes through the
/// same runtime block, which defines both `__bockDeepEq` and `__bockEqCustom`.
fn go_module_uses_deep_eq(items: &[AIRNode]) -> bool {
    items.iter().any(|n| {
        let dbg = format!("{n:?}");
        dbg.contains("\"user_eq\": String(\"deep\")")
            || dbg.contains("\"user_eq\": String(\"deep_custom\")")
    })
}

/// True if the module references `Optional`, `Some`, or `None` anywhere — or
/// calls `pop`, whose DQ30 lowering builds the tagged `__bockOption` runtime
/// (`__bockSome(v)` / `__bockNone`) — so the Optional runtime prelude must be
/// emitted. A cheap structural scan over the debug rendering, mirroring
/// `go_module_uses_concurrency`. Over-matching (a user method named `pop`)
/// only emits the unused runtime struct, which compiles fine.
fn go_module_uses_optional(items: &[AIRNode]) -> bool {
    items.iter().any(|n| {
        let s = format!("{n:?}");
        s.contains("\"Optional\"")
            || s.contains("TypeOptional")
            || s.contains("\"Some\"")
            || s.contains("\"None\"")
            || s.contains("\"pop\"")
    })
}

/// True if the module references `Result`, `Ok`, or `Err` anywhere (so the
/// `Result` runtime prelude must be emitted). Mirrors [`go_module_uses_optional`].
fn go_module_uses_result(items: &[AIRNode]) -> bool {
    items.iter().any(|n| {
        let s = format!("{n:?}");
        s.contains("\"Result\"")
            || s.contains("ResultConstruct")
            || s.contains("\"Ok\"")
            || s.contains("\"Err\"")
    })
}

/// The prelude `Ordering` runtime: a small enum type with the three variants as
/// package-level constants, plus a generic `compare` helper the primitive bridge
/// calls. Mirrors `OPTIONAL_RUNTIME_GO` — when the `core.compare` enum decl is
/// not among the reached modules, `Ordering`/`Less`/`Equal`/`Greater` and
/// `(x).compare(y)` need this self-contained representation. A value-switch
/// `case Less:` (the existing Go match lowering for these arms) matches a
/// `__bockOrdering` constant directly.
const ORDERING_RUNTIME_GO: &str = "// ── Bock Ordering runtime ──
type __bockOrdering int

const (
	Less __bockOrdering = iota - 1
	Equal
	Greater
)

func __bockCompare[T int64 | float64 | string | rune | int | uint64 | float32](a, b T) __bockOrdering {
	if a < b {
		return Less
	}
	if a == b {
		return Equal
	}
	return Greater
}
";

/// Runtime for a `${expr}` interpolation part: render a value whose Bock type
/// has a `Displayable` impl through its `ToString` method (the user
/// `to_string` lowers to a `ToString() string` value-receiver method on Go)
/// rather than the struct default (`fmt.Sprintf("%v", p)` → `{3 7}`). The
/// `__bockStringer` interface is satisfied by exactly those types; every other
/// value falls through to `%v`. Gated by [`go_module_uses_str`]; its own `fmt`
/// import is emitted into the runtime file. (Q-displayable-interpolation-dispatch.)
const STR_RUNTIME_GO: &str = "// ── Bock display-string runtime ──
type __bockStringer interface {
	ToString() string
}

func __bockStr(x interface{}) string {
	if s, ok := x.(__bockStringer); ok {
		return s.ToString()
	}
	return fmt.Sprintf(\"%v\", x)
}
";

/// The `__bockOrdered` constraint a `[T: Comparable]` sealed-core bound lowers to
/// (GAP-C): the ordered primitive type-set, so a generic fn's `a.compare(b)` /
/// `a > b` can use `<`/`==`/`>`. Self-contained (no `cmp` import), matching
/// `__bockCompare`'s set. Emitted independently of the rest of the Ordering
/// runtime: a `[T: Comparable]`-bounded fn (`max_of[T: Comparable]`) needs the
/// constraint even when the module never references `Ordering`/`compare` (which
/// is what gates [`ORDERING_RUNTIME_GO`]). Deduped against that block so the type
/// is never defined twice.
const ORDERED_CONSTRAINT_GO: &str = "// ── Bock ordered constraint ──
type __bockOrdered interface {
	~int64 | ~float64 | ~string | ~rune | ~int | ~uint64 | ~float32
}
";

/// True if the module references the prelude `Ordering` enum, any of its
/// variants, or a `compare` method call (lowered to an `Ordering` runtime
/// value). Gates emission of [`ORDERING_RUNTIME_GO`], mirroring
/// [`go_module_uses_optional`].
fn go_module_uses_ordering(items: &[AIRNode]) -> bool {
    items.iter().any(|n| {
        let s = format!("{n:?}");
        s.contains("\"Ordering\"")
            || s.contains("\"Less\"")
            || s.contains("\"Equal\"")
            || s.contains("\"Greater\"")
            || s.contains("\"compare\"")
    })
}

/// True if any module contains a string interpolation (`${expr}`), so the
/// [`STR_RUNTIME_GO`] `__bockStr` helper must be emitted into the runtime file.
/// Mirrors [`go_module_uses_optional`]. (Q-displayable-interpolation-dispatch.)
fn go_module_uses_str(items: &[AIRNode]) -> bool {
    items
        .iter()
        .any(|n| format!("{n:?}").contains("Interpolation"))
}

/// True if a `match`\'s arms dispatch on the prelude `Ordering` variants
/// (`Less`/`Equal`/`Greater`), so the Go backend emits a *value*-switch over the
/// `__bockOrdering` constants rather than the type-switch it uses for user
/// enums. Recognised by any constructor pattern whose final segment is an
/// `Ordering` variant.
fn go_match_is_ordering(arms: &[AIRNode]) -> bool {
    arms.iter().any(|arm| {
        if let NodeKind::MatchArm { pattern, .. } = &arm.kind {
            if let NodeKind::ConstructorPat { path, .. } = &pattern.kind {
                return path
                    .segments
                    .last()
                    .and_then(|s| crate::generator::ordering_variant(&s.name))
                    .is_some();
            }
        }
        false
    })
}

/// True if a `match`\'s arms dispatch on the `Optional` constructors
/// `Some`/`None` (so the Go backend emits a tag-based switch over
/// `__bockOption`). Recognised by a constructor pattern whose final path
/// segment is `Some` or `None`.
fn go_match_is_optional(arms: &[AIRNode]) -> bool {
    arms.iter().any(|arm| {
        if let NodeKind::MatchArm { pattern, .. } = &arm.kind {
            if let NodeKind::ConstructorPat { path, .. } = &pattern.kind {
                return path
                    .segments
                    .last()
                    .is_some_and(|seg| matches!(seg.name.as_str(), "Some" | "None"));
            }
        }
        false
    })
}

/// True if a `match`'s arms dispatch on the `Result` constructors `Ok`/`Err`
/// (so the Go backend emits a tag-based switch over `__bockResult`, mirroring
/// [`go_match_is_optional`]). Without this, an `Ok`/`Err` constructor pattern
/// would route to the user-enum type-switch (`case Ok:` against an undefined Go
/// type) — the defect this fixes.
fn go_match_is_result(arms: &[AIRNode]) -> bool {
    arms.iter().any(|arm| {
        if let NodeKind::MatchArm { pattern, .. } = &arm.kind {
            if let NodeKind::ConstructorPat { path, .. } = &pattern.kind {
                return path
                    .segments
                    .last()
                    .is_some_and(|seg| matches!(seg.name.as_str(), "Ok" | "Err"));
            }
        }
        false
    })
}

/// Go code generator implementing the `CodeGenerator` trait.
#[derive(Debug)]
pub struct GoGenerator {
    profile: TargetProfile,
}

impl GoGenerator {
    /// Creates a new Go code generator.
    #[must_use]
    pub fn new() -> Self {
        Self {
            profile: TargetProfile::go(),
        }
    }
}

impl Default for GoGenerator {
    fn default() -> Self {
        Self::new()
    }
}

impl CodeGenerator for GoGenerator {
    fn target(&self) -> &TargetProfile {
        &self.profile
    }

    fn generate_module(&self, module: &AIRModule) -> Result<GeneratedCode, CodegenError> {
        // Shared pre-pass: hoist value-position diverging control flow (see
        // `hoist_value_cf`) into declare-then-assign temp blocks.
        let module =
            &crate::generator::hoist_value_cf(crate::generator::lower_blanket_into(module.clone()));
        let mut ctx = GoEmitCtx::new();
        ctx.enum_variants =
            crate::generator::collect_enum_variants(&[(module, std::path::Path::new(""))]);
        ctx.generic_decls =
            crate::generator::collect_generic_decls(&[(module, std::path::Path::new(""))]);
        ctx.collect_record_param_fields(module);
        ctx.collect_async_fns(module);
        ctx.collect_methods(module);
        ctx.collect_type_aliases(module);
        ctx.collect_optional_returns(module);
        ctx.collect_method_optional_returns(module);
        // `trait_decls` must precede `collect_fn_and_type_names` so the latter can
        // record which generic fns carry a *sealed-core* bound lowered to a Go
        // built-in constraint (GAP-C — `fn_sealed_bound`).
        ctx.trait_decls =
            crate::generator::collect_trait_decls(&[(module, std::path::Path::new(""))]);
        ctx.const_names =
            crate::generator::collect_const_names(&[(module, std::path::Path::new(""))]);
        ctx.collect_fn_and_type_names(module);
        ctx.derive_self_param_traits();
        ctx.emit_node(module)?;
        let content = ctx.finish();
        let source_map = SourceMap {
            generated_file: String::new(),
            ..Default::default()
        };
        Ok(GeneratedCode {
            files: vec![OutputFile {
                path: PathBuf::new(),
                content,
                source_map: Some(source_map),
            }],
        })
    }

    /// Emit a per-module **native Go package tree** (spec §20.6.1; DQ19
    /// resolved): each module the entry program reaches through a real `use` is
    /// emitted to its **own** `.go` file under `build/go/`, all in one
    /// `package main`.
    ///
    /// ## Package model (flat, single `package main`)
    ///
    /// Go requires exactly one package per directory, and same-package symbols
    /// are visible across files **without** any import. So the cleanest model
    /// that is genuinely per-file and runs via `go run .` keeps every emitted
    /// file in `build/go/` as `package main`: a function/record/enum declared
    /// in `core.option`'s file is referenced directly from `main`'s file, no
    /// inter-file import. The flat layout (filenames flatten the dotted module
    /// path — `module core.option` ⇒ `core.option.go`) avoids the subdirectory
    /// that would make Go treat a module as a *separate* package. §20.6.1 allows
    /// "the target ecosystem's conventions," and one package across files is
    /// Go's. (Project mode — S6 — may refine this toward real subpackages with
    /// capitalized exports.)
    ///
    /// `ImportDecl`s therefore emit nothing (same package). The runtime preludes
    /// (Optional / Result / numeric / Ordering / concurrency / range) are
    /// emitted **once** into a shared `bock_runtime.go`; consuming files use the
    /// runtime symbols directly (same package). The minimal `go.mod` (module
    /// name + go version) run affordance is emitted by the **scaffolder** in
    /// project mode (S6a / DV18), not by codegen, so `go run .` resolves the
    /// package. Go uses a native `func main`, so no entry invocation is appended.
    fn generate_project(
        &self,
        modules: &[(&AIRModule, &std::path::Path)],
    ) -> Result<GeneratedCode, CodegenError> {
        // Shared pre-pass: hoist value-position diverging control flow on every
        // module before registry collection or emission (see `hoist_value_cf`).
        let hoisted: Vec<(AIRModule, &std::path::Path)> = modules
            .iter()
            .map(|(m, p)| {
                (
                    crate::generator::hoist_value_cf(crate::generator::lower_blanket_into(
                        (*m).clone(),
                    )),
                    *p,
                )
            })
            .collect();
        let modules: Vec<(&AIRModule, &std::path::Path)> =
            hoisted.iter().map(|(m, p)| (m, *p)).collect();
        let modules = modules.as_slice();
        // Emit only modules the entry program actually `use`s (plus the entry
        // itself), dependency-ordered — never the prelude-only stdlib.
        let reachable = crate::generator::reachable_modules(modules);
        let modules = reachable.as_slice();
        if modules.is_empty() {
            return Ok(GeneratedCode { files: vec![] });
        }

        let entry_idx = modules
            .iter()
            .position(|(m, _)| crate::generator::module_declares_main_fn(m))
            .unwrap_or(modules.len() - 1);

        // Pre-scan async fns across ALL modules so cross-module calls between
        // async functions route through the Async-suffix wrappers.
        let mut global_async_fns: HashSet<String> = HashSet::new();
        for (module, _) in modules {
            if let NodeKind::Module { items, .. } = &module.kind {
                for item in items {
                    if let NodeKind::FnDecl {
                        is_async: true,
                        name,
                        ..
                    } = &item.kind
                    {
                        global_async_fns.insert(name.name.clone());
                    }
                }
            }
        }

        // A template ctx carries the program-wide analysis (enum variants,
        // generics, trait/method/Optional-return metadata) collected across the
        // whole reachable set so a reference in one file to a symbol declared in
        // another lowers identically to the bundling path. Each per-module ctx
        // is forked from it.
        let mut template = GoEmitCtx::new();
        template.async_fns = global_async_fns;
        template.enum_variants = crate::generator::collect_enum_variants(modules);
        template.generic_decls = crate::generator::collect_generic_decls(modules);
        template.trait_decls = crate::generator::collect_trait_decls(modules);
        template.const_names = crate::generator::collect_const_names(modules);
        template.derive_self_param_traits();
        // Aliases first across the whole reachable set: a fn in one module may
        // return a `type` alias declared in another, and the Optional/Result
        // return scan below must see through it.
        for (module, _) in modules {
            template.collect_type_aliases(module);
        }
        for (module, _) in modules {
            template.collect_methods(module);
            template.collect_optional_returns(module);
            template.collect_method_optional_returns(module);
            template.collect_record_param_fields(module);
            template.collect_fn_and_type_names(module);
        }
        // Effect-op resolution needs the whole reachable set: a bare op in one
        // module may belong to an effect declared in another (§10 + DV13).
        template.seed_effect_registries(modules);

        let mut files: Vec<OutputFile> = Vec::with_capacity(modules.len() + 2);
        for (i, (module, source_path)) in modules.iter().enumerate() {
            let mut ctx = template.fork();
            ctx.per_module = true;
            ctx.emit_node(module)?;
            let (body, needs) = ctx.into_parts();

            // Each per-module file is `package main` with its own per-file
            // `import (...)` block (Go imports are per-file).
            let mut content = "package main\n".to_string();
            content.push_str(&needs.render_block());
            content.push('\n');
            content.push_str(&body);

            let rel = if i == entry_idx {
                std::path::PathBuf::from("main.go")
            } else {
                go_module_filename(module, source_path, self.target())
            };
            let generated_file = rel
                .file_name()
                .and_then(|s| s.to_str())
                .unwrap_or("")
                .to_string();
            files.push(OutputFile {
                path: rel,
                content,
                source_map: Some(SourceMap {
                    generated_file,
                    ..Default::default()
                }),
            });
        }

        // Shared runtime file: emit exactly the preludes the whole program uses,
        // once, in their own `package main` file (same package → visible to all).
        if let Some(runtime) = self.build_runtime_file(modules, &template) {
            files.push(OutputFile {
                path: std::path::PathBuf::from("bock_runtime.go"),
                content: runtime,
                source_map: None,
            });
        }

        // Manifest emission moved to the project-mode scaffolder (S6a / DV18):
        // codegen emits only the per-module *source* package in all modes; the
        // `go.mod` run affordance is emitted by `GoScaffolder` in project mode
        // only (never under `--source-only`). See `scaffold.rs`.

        Ok(GeneratedCode { files })
    }

    /// Transpile `@test` functions into a `bock_test.go` file (S7).
    ///
    /// `go test` runs `func TestXxx(t *testing.T)` in `package main` (same
    /// package → the test can call the program's unexported functions). Each
    /// Bock `@test` becomes one such function, with `expect(...)` assertion
    /// chains lowered to `if <neg> { t.Errorf(...) }`. `framework` is ignored:
    /// `go test` (stdlib `testing`) is the universal Go framework (§20.6.2).
    fn generate_tests(
        &self,
        modules: &[(&AIRModule, &std::path::Path)],
        _framework: &str,
    ) -> Result<crate::generator::TestArtifacts, CodegenError> {
        let reachable = crate::generator::reachable_modules(modules);
        let modules = reachable.as_slice();
        let tests = crate::generator::collect_test_fns(modules);
        if tests.is_empty() {
            return Ok(crate::generator::TestArtifacts::default());
        }

        // Same program-wide analysis `generate_project` builds, so test bodies
        // lower references (function casing, enum variants, Optional returns)
        // identically to the runtime package.
        let mut global_async_fns: HashSet<String> = HashSet::new();
        for (module, _) in modules {
            if let NodeKind::Module { items, .. } = &module.kind {
                for item in items {
                    if let NodeKind::FnDecl {
                        is_async: true,
                        name,
                        ..
                    } = &item.kind
                    {
                        global_async_fns.insert(name.name.clone());
                    }
                }
            }
        }
        let mut template = GoEmitCtx::new();
        template.async_fns = global_async_fns;
        template.enum_variants = crate::generator::collect_enum_variants(modules);
        template.generic_decls = crate::generator::collect_generic_decls(modules);
        template.trait_decls = crate::generator::collect_trait_decls(modules);
        template.const_names = crate::generator::collect_const_names(modules);
        template.derive_self_param_traits();
        // Aliases first across the whole reachable set: a fn in one module may
        // return a `type` alias declared in another, and the Optional/Result
        // return scan below must see through it.
        for (module, _) in modules {
            template.collect_type_aliases(module);
        }
        for (module, _) in modules {
            template.collect_methods(module);
            template.collect_optional_returns(module);
            template.collect_method_optional_returns(module);
            template.collect_record_param_fields(module);
            template.collect_fn_and_type_names(module);
        }
        template.seed_effect_registries(modules);

        let mut ctx = template.fork();
        ctx.per_module = true;
        for (test_fn, _module_path) in &tests {
            let NodeKind::FnDecl { name, body, .. } = &test_fn.kind else {
                continue;
            };
            let go_name = go_test_fn_name(&name.name);
            ctx.buf.push('\n');
            ctx.writeln(&format!("func {go_name}(t *testing.T) {{"));
            ctx.indent += 1;
            ctx.emit_go_test_body(body)?;
            ctx.indent -= 1;
            ctx.writeln("}");
        }

        let (body, needs) = ctx.into_parts();
        // The test file is `package main`; build its import block (testing plus
        // whatever the test bodies pull in — fmt/strings/…). gofmt-sorted order.
        let mut imports: Vec<&str> = vec!["\"testing\""];
        if needs.fmt {
            imports.push("\"fmt\"");
        }
        if needs.strconv {
            imports.push("\"strconv\"");
        }
        if needs.strings {
            imports.push("\"strings\"");
        }
        if needs.sync {
            imports.push("\"sync\"");
        }
        if needs.time {
            imports.push("\"time\"");
        }
        if needs.utf8 {
            imports.push("\"unicode/utf8\"");
        }
        imports.sort_unstable();
        let mut content = String::from("package main\n\nimport (\n");
        for imp in &imports {
            content.push_str(&format!("\t{imp}\n"));
        }
        content.push_str(")\n");
        content.push_str(&body);

        Ok(crate::generator::TestArtifacts {
            files: vec![OutputFile {
                path: std::path::PathBuf::from("bock_test.go"),
                content,
                source_map: None,
            }],
            entry_append: None,
        })
    }
}

/// The flat output filename for one non-entry module in the per-module Go
/// package: the declared dotted module-path kept verbatim (`module core.option`
/// ⇒ `core.option.go`), so every emitted file lives directly in `build/go/`
/// (one package per directory — no subdirectory, which Go would treat as a
/// separate package). A module with no declared path falls back to its
/// source-mirrored file name.
///
/// The dots are **kept** (not flattened to `_`) deliberately: Go reserves the
/// `_test.go` filename suffix for test files (excluded from a normal `go build`
/// / `go run .`), so `module core.test` flattened to `core_test.go` would
/// silently vanish from the build. `core.test.go` does not match `_test.go` and
/// compiles as an ordinary package file. (Go also reserves `_GOOS.go` /
/// `_GOARCH.go` suffixes, which the dot form likewise avoids.)
fn go_module_filename(
    module: &AIRModule,
    source_path: &std::path::Path,
    target: &TargetProfile,
) -> std::path::PathBuf {
    match crate::generator::module_path_string(module) {
        Some(path) if !path.is_empty() => std::path::PathBuf::from(format!("{path}.go")),
        _ => crate::generator::derive_output_path(source_path, target)
            .file_name()
            .map(std::path::PathBuf::from)
            .unwrap_or_else(|| std::path::PathBuf::from("module.go")),
    }
}

impl GoGenerator {
    /// Build the shared `bock_runtime.go` for the per-module path: `package main`
    /// plus exactly the runtime preludes any reached module references, emitted
    /// once (a duplicate `type __bockOption` / `__bockChannel` across files would
    /// not compile). Returns `None` when no prelude is needed.
    ///
    /// The selection mirrors the bundling path's per-prelude gating:
    /// numeric helpers are emitted when either container runtime is present
    /// (both use them); the bespoke int-`Ordering` runtime is emitted only when
    /// the real `core.compare.Ordering` enum is NOT reachable (otherwise that
    /// user enum is authoritative and the int runtime would be dead + shadow it).
    fn build_runtime_file(
        &self,
        modules: &[(&AIRModule, &std::path::Path)],
        template: &GoEmitCtx,
    ) -> Option<String> {
        let mut uses_concurrency = false;
        let mut uses_optional = false;
        let mut uses_result = false;
        let mut uses_ordering = false;
        let mut uses_range = false;
        let mut uses_int_pow = false;
        let mut uses_deep_eq = false;
        let mut uses_str = false;
        for (module, _) in modules {
            if let NodeKind::Module { items, .. } = &module.kind {
                uses_concurrency |= go_module_uses_concurrency(items);
                uses_optional |= go_module_uses_optional(items);
                uses_result |= go_module_uses_result(items);
                uses_ordering |= go_module_uses_ordering(items);
                uses_range |= go_module_uses_range(items);
                uses_int_pow |= go_module_uses_int_pow(items);
                uses_deep_eq |= go_module_uses_deep_eq(items);
                uses_str |= go_module_uses_str(items);
            }
        }
        // The real `core.compare.Ordering` enum is authoritative when reachable
        // (its `Less` is a registered user variant in the shared registry).
        let ordering_enum_reachable = template
            .enum_variants
            .get("Less")
            .is_some_and(|info| info.enum_name == "Ordering");

        let emit_ordering = uses_ordering && !ordering_enum_reachable;
        // A `[T: Comparable]`-bounded generic fn lowers `T` to `T __bockOrdered`
        // (GAP-C). That constraint type must be defined even when the program
        // never references `Ordering`/`compare` (which gates the rest of the
        // Ordering runtime). `fn_sealed_bound` is populated on the template's
        // program-wide pre-scan.
        let emit_ordered_constraint = !template.fn_sealed_bound.is_empty();
        if !(uses_concurrency
            || uses_optional
            || uses_result
            || uses_range
            || uses_int_pow
            || uses_deep_eq
            || uses_str
            || emit_ordering
            || emit_ordered_constraint)
        {
            return None;
        }

        let mut content = String::from("package main\n\n");
        // `__bockDeepEq` is the only runtime helper with a stdlib dependency;
        // its import lives here (Go imports are per-file, and the consuming
        // modules only *call* the helper).
        if uses_deep_eq {
            content.push_str("import \"reflect\"\n\n");
        }
        if uses_str {
            // `__bockStr` calls `fmt.Sprintf`; Go imports are per-file, and the
            // runtime file is its own `package main` source.
            content.push_str("import \"fmt\"\n\n");
        }
        if uses_concurrency {
            content.push_str(CONCURRENCY_RUNTIME_GO);
            content.push('\n');
        }
        // The deep-eq runtime's `__bockEqCustom` type-asserts the Optional/Result
        // wrapper structs to defer their payload to the element's `Eq`
        // (Q-py-go-wrapper-structural-eq), so those type definitions must be in
        // scope wherever `__bockEqCustom` is. Emit them when `uses_deep_eq` even
        // if the program never names `Optional`/`Result` directly; the
        // (then-unused) constructors are harmless package-level decls in Go.
        if uses_optional || uses_deep_eq {
            content.push_str(OPTIONAL_RUNTIME_GO);
            content.push('\n');
        }
        if uses_result || uses_deep_eq {
            content.push_str(RESULT_RUNTIME_GO);
            content.push('\n');
        }
        if uses_optional || uses_result {
            content.push_str(NUMERIC_RUNTIME_GO);
            content.push('\n');
        }
        if emit_ordering {
            content.push_str(ORDERING_RUNTIME_GO);
            content.push('\n');
        }
        // The `__bockOrdered` constraint: needed by a sealed-bound generic fn,
        // and (since it was split out of the Ordering block) also whenever the
        // Ordering runtime itself is emitted, so a `compare`-using generic still
        // resolves it. Emitted once — `emit_ordering` no longer carries it.
        if emit_ordered_constraint || emit_ordering {
            content.push_str(ORDERED_CONSTRAINT_GO);
            content.push('\n');
        }
        if uses_range {
            content.push_str(RANGE_RUNTIME_GO);
            content.push('\n');
        }
        if uses_int_pow {
            content.push_str(INT_POW_RUNTIME_GO);
            content.push('\n');
        }
        if uses_deep_eq {
            content.push_str(DEEP_EQ_RUNTIME_GO);
            content.push('\n');
        }
        if uses_str {
            content.push_str(STR_RUNTIME_GO);
            content.push('\n');
        }
        // Each runtime block is joined with a trailing `\n`, which leaves a blank
        // line at EOF; gofmt wants exactly one terminating newline (§20.6.2
        // codegen-formatter agreement — the output must be gofmt-clean).
        let content = format!("{}\n", content.trim_end());
        Some(content)
    }
}

// ─── Emission context ────────────────────────────────────────────────────────

/// Internal state for Go emission.
///
/// `Clone` is derived so the per-module path ([`GoGenerator::generate_project`])
/// can pre-scan the whole program's cross-module analysis once into a template
/// ctx and [`GoEmitCtx::fork`] it per module file (resetting only the per-file
/// emission state). Every field is itself `Clone`.
#[derive(Clone)]
struct GoEmitCtx {
    buf: String,
    indent: usize,
    /// Track whether we need `"fmt"` import.
    needs_fmt_import: bool,
    /// Track whether we need `"sync"` import.
    needs_sync_import: bool,
    /// Track whether we need `"time"` import.
    needs_time_import: bool,
    /// Track whether we need `"strings"` import (String built-in methods).
    needs_strings_import: bool,
    /// Track whether we need `"unicode/utf8"` import (`String.len` scalar count).
    needs_utf8_import: bool,
    /// Track whether we need `"math"` import (numeric `Float` math methods).
    needs_math_import: bool,
    /// Track whether we need `"unicode"` import (`Char`/`trim_start`/`trim_end`
    /// predicates via `unicode.IsSpace`/`IsLetter`/`IsDigit`).
    needs_unicode_import: bool,
    /// Track whether we need `"strconv"` import (`Int.try_from`/`Float.try_from`
    /// string parsing via `strconv.ParseInt`/`strconv.ParseFloat`).
    needs_strconv_import: bool,
    /// Track whether we need `"reflect"` import (the DQ29 `__bockDeepEq`
    /// structural-equality helper, single-module path only — the per-module
    /// path imports it inside the shared `bock_runtime.go` instead).
    needs_reflect_import: bool,
    /// Package name (defaults to "main").
    package_name: String,
    /// Maps effect operation name → effect type name (e.g., "log" → "Logger").
    effect_ops: HashMap<String, String>,
    /// Maps effect type name → current handler variable name in scope.
    current_handler_vars: HashMap<String, String>,
    /// Maps function name → effect type names from its `with` clause.
    fn_effects: HashMap<String, Vec<String>>,
    /// Maps composite effect name → component effect names.
    composite_effects: HashMap<String, Vec<String>>,
    /// Names of public (exported) functions — emitted as PascalCase at call sites.
    public_fns: HashSet<String>,
    /// Names of effect operations that return Void — emitted without a `return` prefix.
    void_effect_ops: HashSet<String>,
    /// Bock names of top-level async functions. Call-site identifiers in this
    /// set are rewritten to `fnNameAsync` so callers receive the channel form
    /// of the function (goroutine started, `<-chan T` returned). Without this,
    /// `await task()` would try to receive from a `T`, not `chan T`.
    async_fns: HashSet<String>,
    /// Names of `public` methods (declared in impl/class/trait blocks). Used at
    /// desugared method-call sites to pick PascalCase (public) vs camelCase
    /// (private) so the call matches the method definition's Go casing.
    public_methods: HashSet<String>,
    /// `(target type name, method name)` pairs that have an *inherent* (`impl
    /// Type { ... }`, no `trait_path`) or *class* method definition. A trait
    /// impl (`impl Trait for Type`) whose method merely forwards to the
    /// same-named inherent method (`fn render(self) { self.render() }`) is a
    /// redundant self-recursive forwarder in Go once the inherent method is
    /// exported to satisfy the interface directly — both would emit the same
    /// PascalCase Go name on the receiver, and the forwarder body's
    /// `self.render()` would resolve back to itself. Such a trait-impl method is
    /// skipped when an inherent definition already covers it. Keyed on the
    /// PascalCased Go method name (the trait-side casing) so a private inherent
    /// method exported via `public_methods` still matches.
    inherent_methods: HashSet<(String, String)>,
    /// PascalCased names of every record/class field declared in the program.
    /// Go forbids a struct having a field and a method with the same name, so a
    /// public method whose PascalCased Go name collides with a field name
    /// (e.g. `core.error`'s `SimpleError { message }` + `fn message(self)`) is
    /// suffixed `Method` by [`Self::go_method_name`] at the declaration (trait
    /// interface + receiver) and every call site so they agree.
    record_field_names: HashSet<String>,
    /// Loop-label stack. In Go, `break` inside a `switch` exits the switch, not
    /// an enclosing `for`. When a statement-arm `match` (lowered to a `switch`)
    /// contains a `break`/`continue` meant for the loop, the loop is given a
    /// label and the jump is emitted as `break <label>` / `continue <label>`.
    /// An entry is pushed for every active loop; `Some` once a label has been
    /// allocated for it. Only allocated labels are emitted (Go errors on an
    /// unused label).
    loop_labels: Vec<Option<String>>,
    /// When > 0, `break`/`continue` are being emitted inside a `switch` arm and
    /// must target the innermost labelled loop rather than the switch.
    switch_label_depth: usize,
    /// Monotonic counter for unique loop-label names.
    loop_label_counter: usize,
    /// Monotonic counter for unique guard-let discriminant temp names
    /// (`__guard0`, `__guard1`, …), so two `guard (let …)` statements in the same
    /// block do not collide.
    guard_counter: usize,
    /// Monotonic counter for unique `?`-propagation temp names (`__try0`,
    /// `__try1`, …). Go has no native `?`; each propagate hoists the operand into
    /// a `__tryN` local before its unwrap-or-early-return lowering.
    try_counter: usize,
    /// Monotonic counter for unique tuple-destructuring-`let` temp names
    /// (`__tup0`, `__tup1`, …). Go has no tuple destructuring; a
    /// `let (a, b) = expr` hoists `expr` into a `__tupN` struct local and binds
    /// each name off its `.Field{i}`, so two such lets in one block do not collide.
    let_tuple_counter: usize,
    /// Depth of enclosing *expression-position* `loop` IIFEs (`let r = loop { …
    /// break <v> }`). Bock's `loop` is a value-producing expression whose
    /// `break <v>` yields the loop's value; Go's `for`+`break` carries no value.
    /// When this is > 0 the innermost loop body is the IIFE's body, so a
    /// `break <v>` lowers to `return <v>` (out of the IIFE), not a value-dropping
    /// `break`. Saved/restored around nested statement-position loops, whose
    /// `break <v>` is a different (still value-dropping) case.
    loop_expr_depth: usize,
    /// Maps a function name → the Go element type of its `Optional[T]` return
    /// (`int64` for `-> Int?`). Pre-scanned across the module so a `match`
    /// whose scrutinee is a call (`match next(it) { Some(x) => ... }`) can
    /// type-assert the bound payload. Functions not returning an Optional are
    /// absent.
    fn_optional_ret_elem: HashMap<String, String>,
    /// Maps an in-scope variable name → the Go element type of its `Optional[T]`
    /// (e.g. an `o: Int?` parameter or a `let o: Int? = ...` binding maps to
    /// `int64`). Lets a `match o { Some(x) => ... }` type-assert `__opt.v` to
    /// the concrete element type instead of leaving it `interface{}`. The Go
    /// Optional runtime stores the payload as `interface{}`, so without this
    /// assertion any typed use of the bound value (`x + 10`) fails Go
    /// compilation. Scoped per function body and restored on exit.
    var_optional_elem: HashMap<String, String>,
    /// Maps an in-scope variable name → its declared type-expression AIR node
    /// (an `Optional[Result[(Int, Int), String]]` param maps to that
    /// `TypeOptional`/`TypeNamed` node). The single-element `var_*_elem` maps
    /// only record the *one-level* peeled Go type, which is not enough to
    /// type-assert the payload of a *nested* constructor pattern: a
    /// `match v { Some(Ok((a, b))) => … }` must peel Optional → Result → Tuple
    /// to assert the boxed `interface{}` payload to its concrete tuple struct
    /// (`struct{ Field0 int64; Field1 int64 }`) before `.Field0` reads.
    /// Threaded through the pattern-bind/test recursion (peeling Optional on
    /// `Some`, Result on `Ok`/`Err`) so a nested tuple pattern lands on the
    /// concrete struct type. Scoped per function body and restored on exit.
    var_decl_type_node: HashMap<String, AIRNode>,
    /// Maps a *method* name → the Go element type of its `Optional[T]` return
    /// (`int64` for `fn next(self) -> Int?`). Pre-scanned across every
    /// impl/class/trait block so a `match` whose scrutinee is a method call
    /// (`match it.next() { Some(x) => ... }`, the shape `for x in <Iterable>`
    /// desugars to) can type-assert the bound payload. This is the method-call
    /// analogue of [`Self::fn_optional_ret_elem`]. Keyed by method name only
    /// (Go codegen sees the AIR, not the checker's per-type `method_types`); if
    /// two methods share a name but return different Optional element types, the
    /// entry is poisoned (left absent) so the payload falls back to the runtime
    /// `interface{}` — conservative, never wrong, only un-type-asserted.
    method_optional_ret_elem: HashMap<String, String>,
    /// Maps a method name → the concrete generic-record instantiation it returns
    /// (`("ListIterator", ["int64"])` for `Bag.iter() -> ListIterator[Int]`),
    /// for methods whose declared return type is a concrete generic-record
    /// apply (no remaining type params). Lets an *untyped* binding of such a
    /// call (`__it := bag.Iter()`, the `for x in <Iterable>` desugar) record the
    /// binding's record args ([`Self::var_record_type_args`]) so the
    /// subsequent `match __it.next() { Some(x) => ... }` resolves the generic
    /// `Optional[T]` payload to the concrete arg (`int64`) — `T` is undefined in
    /// the calling fn (`main`). Keyed by method name only; poisoned (left
    /// absent) on a name clash with disagreeing args, as
    /// [`Self::method_optional_ret_elem`].
    method_ret_record_args: HashMap<String, (String, Vec<String>)>,
    /// Maps a method name → its declared return type rendered as Go (`stock_value
    /// → "float64"`). Lets `infer_go_expr_type` resolve a `recv.method()` call's
    /// type so a `.map((p) => p.stock_value())` combinator sizes its result slice
    /// as `[]float64` (not the erased `[]interface{}` whose elements a later
    /// `fold`'s `acc + v` can't add). Keyed by method name only; poisoned (left
    /// absent) on a name clash with disagreeing Go return types — mirrors
    /// [`Self::method_optional_ret_elem`]. A return type still naming an in-scope
    /// generic param is skipped (it is the generic signature, not concrete).
    method_return_go_types: HashMap<String, String>,
    /// Maps an in-scope variable name bound to a lambda → that lambda's inferred
    /// Go return type (`clip_fn → "[]float64"` for `let clip_fn = (d) => clip(d,
    /// ..)`). Lets a compose desugar `normalize >> clip_fn` (lowered to
    /// `(__compose_x) => clip_fn(normalize(__compose_x))`) resolve its own return
    /// type from the outer local lambda `clip_fn`, so the emitted closure is
    /// `func(x []float64) []float64` rather than `func(x []float64) interface{}`
    /// (the latter not assignable to a `Fn(List[Float]) -> List[Float]` callee).
    /// Function-scoped, restored on body exit alongside `var_go_type`.
    var_lambda_ret: HashMap<String, String>,
    /// Maps an in-scope variable name → its concrete generic record
    /// instantiation `(base record name, concrete Go type-args)` — e.g. a `let
    /// c: ListIter[Int]` binding or a `c: Counter[Int]` parameter maps to
    /// `("ListIter", ["int64"])`. Used to resolve a method-call scrutinee's
    /// `Optional[T]` payload at a CONCRETE call site: `method_optional_ret_elem`
    /// stores the *generic* element (`"T"`, the record's type param), undefined
    /// in the concrete caller (`main`); this lets `match c.next() { Some(x) =>
    /// ... }` assert the payload to the instantiation's arg (`int64`) instead of
    /// the bare `T`. Scoped per function/method body and restored on exit.
    var_record_type_args: HashMap<String, (String, Vec<String>)>,
    /// Maps an in-scope variable name → the Go element type of its `List[T]`
    /// (e.g. a `let nums: List[Int] = ...` binding maps to `int64`). The
    /// read-only `List` built-ins `get`/`first`/`last` return `Optional[T]`
    /// whose payload is the list element; this lets a `match nums.get(i) {
    /// Some(x) => ... }` type-assert the `interface{}` payload to the element
    /// type, the same way [`Self::var_optional_elem`] handles direct
    /// `Optional[T]` bindings. Scoped per function body and restored on exit.
    var_list_elem: HashMap<String, String>,
    /// Maps an in-scope variable name → `(key_go_type, val_go_type)` of its
    /// `Map[K, V]` (e.g. a `let m: Map[String, Int] = ...` binding maps to
    /// `("string", "int64")`). The built-in `Map` methods lower to inline
    /// `func(__m map[K]V, …) …` closures whose parameter type must match the
    /// concretely-typed receiver `map[K]V`; this records the declared key/value
    /// Go types so the closure is well-typed (Go does not pass a `map[string]
    /// int64` where a `map[interface{}]interface{}` is expected). Scoped per
    /// function body and restored on exit (mirrors [`Self::var_list_elem`]).
    var_map_kv: HashMap<String, (String, String)>,
    /// Maps an in-scope variable name → the Go element type of its `Set[E]`
    /// (e.g. a `let s: Set[Int] = ...` binding maps to `int64`). The Set
    /// analogue of [`Self::var_map_kv`]: the built-in `Set` methods lower to
    /// inline closures over `map[E]struct{}`, so the element type must match the
    /// concretely-typed receiver. Scoped per function body and restored on exit.
    var_set_elem: HashMap<String, String>,
    /// Maps an in-scope variable name → `(ok_go_type, err_go_type)` of its
    /// `Result[T, E]` (e.g. an `r: Result[Int, String]` param maps to
    /// `("int64", "string")`). The Result analogue of [`Self::var_optional_elem`]:
    /// a `match r { Ok(v) => ...; Err(e) => ... }` type-asserts the `interface{}`
    /// payload to the concrete Ok/Err type rather than leaving it `interface{}`.
    /// Scoped per function body and restored on exit.
    var_result_elem: HashMap<String, (String, String)>,
    /// Maps a free-function name → `(ok_go_type, err_go_type)` of its
    /// `Result[T, E]` return, so a `match parse(s) { Ok(n) => ... }` on a call
    /// scrutinee type-asserts the bound payload. The Result analogue of
    /// [`Self::fn_optional_ret_elem`]; functions not returning a Result are absent.
    fn_result_ret_elem: HashMap<String, (String, String)>,
    /// Set once the concurrency runtime prelude has been emitted into `buf` in
    /// the single-module self-contained path ([`GoGenerator::generate_module`]),
    /// so a module referencing it more than once still inlines it at most once (a
    /// duplicate `type __bockChannel` would not compile). The per-module project
    /// path emits the runtime once into the shared `bock_runtime.go`.
    concurrency_runtime_emitted: bool,
    /// Set once the Optional runtime prelude has been emitted into `buf`;
    /// deduped exactly as [`Self::concurrency_runtime_emitted`].
    optional_runtime_emitted: bool,
    /// Set once the `Result` runtime prelude has been emitted; deduped exactly as
    /// [`Self::optional_runtime_emitted`].
    result_runtime_emitted: bool,
    /// Set once the shared numeric-payload helpers ([`NUMERIC_RUNTIME_GO`]) have
    /// been emitted. Emitted once if *either* the Optional or `Result` runtime is
    /// used, so the two never redeclare `__bockAsInt64`/`__bockAsFloat64`.
    numeric_runtime_emitted: bool,
    /// Set once the [`ORDERING_RUNTIME_GO`] prelude has been emitted; deduped
    /// exactly as [`Self::optional_runtime_emitted`].
    ordering_runtime_emitted: bool,
    /// Set once [`ORDERED_CONSTRAINT_GO`] (`__bockOrdered`) has been emitted in
    /// the single-file inline path; deduped exactly as
    /// [`Self::ordering_runtime_emitted`].
    ordered_constraint_emitted: bool,
    /// Set once the [`RANGE_RUNTIME_GO`] helper has been emitted; deduped exactly
    /// as [`Self::optional_runtime_emitted`] (a duplicate `func __bockRange`
    /// would not compile).
    range_runtime_emitted: bool,
    /// Set once the [`INT_POW_RUNTIME_GO`] helper (`__bockIntPow`) has been
    /// emitted; deduped exactly as [`Self::range_runtime_emitted`] (a duplicate
    /// `func __bockIntPow` would not compile).
    int_pow_runtime_emitted: bool,
    /// Set once the [`DEEP_EQ_RUNTIME_GO`] helper (`__bockDeepEq`) has been
    /// emitted; deduped exactly as [`Self::range_runtime_emitted`].
    deep_eq_runtime_emitted: bool,
    /// User-enum-variant registry (DV14). Go has no sum type, so a user enum is
    /// a sealed interface + per-variant structs named `{enum}{variant}`
    /// (e.g. `ShapeCircle`). The registry lets a construction emit the variant
    /// struct literal and a `match` emit a *type-switch* (`switch __v :=
    /// s.(type) { case ShapeCircle: … }`) with field extraction, rather than the
    /// broken value-switch on the unqualified variant name. Built-in
    /// Optional/Result pre-seeds are filtered out (Optional has its own
    /// `__bockOption` runtime). Pre-scanned across the reached modules.
    enum_variants: crate::generator::EnumVariantRegistry,
    /// Type-alias registry: alias name → its underlying type-expression AIR node
    /// (`type ParseResult = Result[MarkdownNode, ParseError]` →
    /// `ParseResult → TypeNamed(Result[...])`). Go has no transparent alias to a
    /// *runtime* type the way Bock does: a function returning the alias
    /// `ParseResult` must lower to the `__bockResult` runtime struct (so a `match`
    /// on its value dispatches on `.tag`), and `result_elem_go_types` /
    /// `collect_optional_returns` must see *through* the alias to record the
    /// Ok/Err payload types. The emitter resolves an alias name to its target via
    /// this map. Pre-scanned across the reached modules (mirrors
    /// [`Self::enum_variants`]).
    type_aliases: HashMap<String, AIRNode>,
    /// Declared names of module-scope `const`s, pre-scanned across the reachable
    /// program. Emitted verbatim at both declaration and use so the two agree —
    /// `to_pascal_case` (`FIZZ_NUM` → `FIZZNUM`) at the def and `go_fn_name`
    /// (`fizzNUM`) at the use otherwise disagree. `SCREAMING_SNAKE` is a valid,
    /// exported Go identifier. See [`crate::generator::collect_const_names`].
    const_names: std::collections::HashSet<String>,
    /// Generic-type declaration registry: a record/enum/class name → its
    /// declared generic params. Lets an `impl Box { ... }` block recover the
    /// `[T any]` declared on `record Box[T]` so a Go method receiver emits
    /// `func (self *Box[T]) ...` (Go requires the type-param list on the
    /// receiver) and a construction emits `Box[int64]{...}`. Pre-scanned across
    /// the reached modules (mirrors [`Self::enum_variants`]).
    generic_decls: crate::generator::GenericDeclRegistry,
    /// Method-level type-parameter lowering registry (DQ28). Go forbids type
    /// parameters on methods (`func (b Box[T]) Map[U](..)` is a syntax error),
    /// but Bock keeps the surface (`Box[T].map[U]`); the Go backend lowers such a
    /// method to a *free function* `func Box_Map[T, U](self Box[T], ..) ..`,
    /// keyed `<TypeName>_<MethodGoName>` for collision-free naming (free
    /// functions support multiple type params natively — no monomorphization).
    /// This map records, per Bock *method name*, the owning Go type name, so a
    /// call site `box.map(f)` can be rewritten to `Box_Map(box, f)`. Keyed by
    /// method name only (codegen sees the AIR, not the checker's per-type method
    /// table); if two distinct types declare a generic method of the same name
    /// the entry is *poisoned* (removed) — the call site then falls back to the
    /// ordinary method-dispatch form, which is at worst un-lowered, never wrong
    /// for the unambiguous types. Pre-scanned across the reached modules.
    method_freefn_lowered: HashMap<String, String>,
    /// Maps an in-scope variable name → its Go type, used to infer a lambda's
    /// return type. Go infers a bare `func(...) interface{}` for every lambda;
    /// when such a closure is passed to a typed `func(int64) int64` parameter
    /// the assignment fails to compile. Tracking param/binding Go types lets the
    /// lambda emitter recover a concrete return type structurally from the body.
    /// Scoped per function/lambda body and restored on exit.
    var_go_type: HashMap<String, String>,
    /// Stack of value names already declared in each *Go block scope* currently
    /// open, innermost frame last. Used to lower a shadowing `let` correctly:
    /// Bock permits re-binding the same name in one block (the immutable-update
    /// idiom, `let acc = …; let acc = f(acc)`), but Go's `:=` rejects a
    /// re-declaration with no new variable on the left side. When a `let`'s name
    /// is already in the *innermost* frame, the binding lowers to a plain
    /// assignment (`acc = …`) instead of a fresh declaration (`acc := …` /
    /// `var acc T = …`). A new frame is pushed on entry to each Go block body
    /// (see [`Self::emit_block_body_inner`]) and popped on exit; the function's
    /// body frame is pre-seeded with the parameter names (via
    /// [`Self::pending_scope_seed`]) so a `let` shadowing a parameter (same Go
    /// scope) also reassigns. A name first declared in a *nested* block is not in
    /// an outer frame, so it stays a `:=` declaration — Go permits that legal
    /// inner-scope shadow.
    go_declared_scopes: Vec<HashSet<String>>,
    /// Names to merge into the next Go block frame pushed by
    /// [`Self::emit_block_body_inner`]. Set at function/method entry to the
    /// parameter names so the function body's frame (which shares the function's
    /// single Go scope — there is no extra brace for the body) treats a `let`
    /// shadowing a parameter as a reassignment. Consumed (taken) by the next
    /// frame push so it never leaks into a nested block.
    pending_scope_seed: Option<Vec<String>>,
    /// Maps a declare-only temp name (from the shared value-CF hoist) → the Go
    /// type inferred for its `var __bock_cf_N T` declaration. Go has no
    /// deferred-init `var x` (it needs a type), so a block emitter pre-scans each
    /// declare-only `let` paired with its following relocated control-flow
    /// statement, infers the result type structurally, and records it here for
    /// the `LetBinding` emitter. See [`Self::seed_decl_only_types`].
    decl_only_types: HashMap<String, String>,
    /// Maps a generic record's name → for each generic param (in declaration
    /// order) the field name whose declared type is exactly that param. Lets a
    /// construction `Box { value: 42 }` emit the explicit instantiation
    /// `Box[int64]{...}` Go requires (Go does *not* infer struct type args from
    /// composite-literal field values). `None` for a param not directly named by
    /// any field's type (then the arg falls back to `any`). Pre-scanned.
    record_param_fields: HashMap<String, Vec<Option<String>>>,
    /// Maps a record name → (field name → the Go element type of that field's
    /// `List[...]` declared type). Lets a built-in list method on a `self.field`
    /// receiver inside a (generic) method type its inline closure's `[]<elem>`
    /// parameter correctly: inside `fn next(self)` of `record ListIter[T] { xs:
    /// List[T] }`, `self.xs.get(i)` must take `[]T` (T is in scope on the
    /// receiver), not `[]interface{}` (which a `[]T` argument does not satisfy).
    /// Only `List`-typed fields are recorded. Pre-scanned across the reached modules.
    record_field_list_elem: HashMap<String, HashMap<String, String>>,
    /// Maps a record name → (field name → the Go `(key, value)` types of that
    /// field's `Map[K, V]` declared type). The `Map` analogue of
    /// [`Self::record_field_list_elem`]: lets a built-in map method on a
    /// `record.field` receiver (`report.by_category.get(k)` for `by_category:
    /// Map[String, Float]`) type its inline closure's `map[K]V`/`K`/`V`
    /// parameters from the field's declared key/value types rather than the
    /// erased `map[interface{}]interface{}` Go rejects against the concrete
    /// struct field. Only `Map`-typed fields are recorded. Pre-scanned.
    record_field_map_kv: HashMap<String, HashMap<String, (String, String)>>,
    /// Maps a record name → (field name → the Go type of that field), for every
    /// field of every record. The general scalar analogue of
    /// [`Self::record_field_list_elem`]/[`Self::record_field_map_kv`]: lets
    /// [`Self::infer_go_expr_type`] resolve a bare `obj.field` access (e.g. a
    /// `.map((b) => b.id)` closure body where `b: Block` and `record Block { id:
    /// Int }`) to the field's concrete Go type (`int64`), so the result slice is
    /// sized `[]int64` rather than the erased `[]interface{}` Go rejects against
    /// a declared `[]int64` return. Pre-scanned across the reached modules.
    record_field_go_type: HashMap<String, HashMap<String, String>>,
    /// Maps a record name → its generic-param names in declaration order
    /// (`"SortedSet" → ["T"]`). Lets a construction site substitute a field's
    /// declared list-element type (`record SortedSet[T] { items: List[T] }` →
    /// elem `T`) with the construct's resolved concrete type args, so an empty
    /// `[]` field literal emits `[]Key{}` for `SortedSet[Key]{…}` (or `[]T{}`
    /// when the construct is itself generic) rather than the erased
    /// `[]interface{}{}` Go rejects against the `[]T` struct field. Pre-scanned.
    record_generic_param_names: HashMap<String, Vec<String>>,
    /// The base name of the record whose method body is currently being emitted
    /// (`"ListIter"` inside `impl ListIter`'s methods), so a `self.field` list
    /// receiver resolves through [`Self::record_field_list_elem`]. Set at method
    /// entry, restored on exit; `None` outside an impl method body.
    current_self_record: Option<String>,
    /// Trait-declaration registry. Used at each `impl Trait for Type` site to
    /// recover the trait's *default* methods (those carrying a body) so a
    /// receiver method is synthesized on the target — the Go interface declares
    /// only the signature, so a type relying on an inherited default would
    /// otherwise fail to satisfy the interface and have no such method. Pre-
    /// scanned across the reached modules (mirrors [`Self::enum_variants`]).
    trait_decls: crate::generator::TraitDeclRegistry,
    /// Names of all top-level types (records, enums, traits, classes). A public
    /// Bock function whose PascalCased Go name collides with one of these (e.g.
    /// `public fn key` → `Key`, colliding with `record Key`) is renamed via
    /// [`Self::go_fn_name`] — Go has one namespace for types and functions, and
    /// PascalCasing erases the `key`/`Key` case distinction Bock relies on.
    type_names: HashSet<String>,
    /// When `Some(target)`, a `Self` type (`TypeSelf`) renders as `target`
    /// rather than the `/* Self */` placeholder. Set while emitting a trait-impl
    /// method on a concrete target — most relevant for a *synthesized default
    /// method* whose source uses `Self` (e.g. `other: Self`), which must become
    /// the concrete receiver type so the Go method signature is valid. Cleared
    /// everywhere else.
    go_self_subst: Option<String>,
    /// Trait names whose methods take a `Self`-typed operand (e.g.
    /// `Comparable`/`Equatable`, whose `compare`/`eq` take `other: Self`). Such
    /// traits are encoded as F-bounded *generic* interfaces in Go (`type
    /// Comparable[T any] interface { Compare(T) Ordering }`) and a bound `[T:
    /// Comparable]` lowers to `[T Comparable[T]]` — a plain Go interface cannot
    /// name the implementing type. Derived from [`Self::trait_decls`].
    self_param_traits: HashSet<String>,
    /// The Go return type of the function/method whose body is currently being
    /// emitted in *return position*. An `if`/`match` in expression position
    /// lowers to an IIFE; typing that IIFE with this concrete return type
    /// (`func() Ordering { … }`) rather than `func() interface{}` makes its
    /// result assignable where the concrete type is required (e.g. a user-enum
    /// `Ordering` return — `interface{}` does not satisfy a named interface).
    /// `None` outside a typed return body. The match/if IIFE also emits a
    /// trailing `panic("unreachable")` instead of `return nil` when typed, since
    /// a concrete (non-interface) return type has no `nil`.
    current_fn_ret_type: Option<String>,
    /// The enclosing function's declared return type *node*, kept when it is a
    /// function type (`Fn(Int) -> Int`). A lambda in tail-return position
    /// (`fn compose(...) -> Fn(Int) -> Int { (x) => f(g(x)) }`) is otherwise
    /// emitted with `interface{}` params and an `interface{}` return — not
    /// assignable to the declared `func(int64) int64` return. This lets the
    /// return-position emitter type that lambda's params/return from the declared
    /// function type. `None` outside a typed return body (or when the return type
    /// is not a function type). Saved/restored alongside `current_fn_ret_type`.
    current_fn_ret_type_node: Option<AIRNode>,
    /// The Go type a value-position expression is being assigned *into*, when
    /// known and distinct from the enclosing function's return type. Set around a
    /// `let x: T = <value>`'s value emit. An expression-position `match` lowers
    /// to an IIFE whose return type must be the *binding*'s declared `T`, not the
    /// function's return type (`current_fn_ret_type`) — a `let x: T = match …`
    /// where `T` ≠ the fn return otherwise emits `func() <RetType> { … }()` whose
    /// result is not assignable to `T`. When set (and not `interface{}`), the
    /// match/if IIFE prefers this over `current_fn_ret_type`. `None` outside a
    /// typed binding context; consumed (taken) around the value emit so it never
    /// leaks to a sibling/outer expression. Additive: when absent the IIFE keeps
    /// using `current_fn_ret_type`, preserving the working Optional/Result/enum
    /// return-position behavior.
    current_expected_type: Option<String>,
    /// The concrete Go type-argument suffix (`[int64]`) of the user enum whose
    /// `match` is currently being lowered to a type-switch / type-assert chain,
    /// or `""` when none / the enum is non-generic. A generic user enum's variant
    /// structs carry the enum's params (`type BoxFull[T any] struct{…}`), so a
    /// `case BoxFull[int64]:` / `_, ok := __v.(BoxFull[int64])` must spell the
    /// concrete instantiation — Go rejects a bare generic type without
    /// instantiation. Set from the scrutinee's inferred Go type before a match's
    /// arms are emitted and restored afterwards (matches nest). See
    /// [`Self::enum_variant_type_arg_suffix`].
    current_match_enum_type_args: String,
    /// Expected collection element Go types for a collection literal emitted in
    /// a *typed context* (a `let x: List[T] = [...]`). A collection literal
    /// infers its element type from its elements, but an EMPTY literal (`[]`)
    /// or one whose elements infer looser than the declaration cannot — and the
    /// `interface{}` fallback then mismatches the declared `[]T`. When set, a
    /// `List`/`Set` literal uses `.0` as its element type and a `Map` literal
    /// uses `(.0, .1)` as `(key, value)`, so the literal matches the declared
    /// container. `None` outside a typed-collection binding context. Consumed
    /// (taken) at the literal so it never leaks to a nested/sibling literal.
    expected_collection_elem: Option<(String, Option<String>)>,
    /// The enclosing function's *return* collection element Go types, when its
    /// return type is a `List[T]` / `Set[T]` / `Map[K, V]`. A collection literal
    /// in `return` position adopts these so a generic `fn single[T](x: T) ->
    /// List[T] { return [x] }` emits `[]T{x}` rather than the `[]interface{}{x}`
    /// the bare-literal inference falls back to (which is not assignable to the
    /// `[]T` return). Set at fn/method entry from the return type, restored on
    /// exit; `None` for a non-collection or absent return type.
    current_fn_ret_collection_elem: Option<(String, Option<String>)>,
    /// Signatures of top-level generic functions, keyed by fn name: the declared
    /// generic-param names and each value param's declared type node. Used at a
    /// call site to type an *untyped lambda argument* (`Filter(it, (x) => x >
    /// 2)`): the non-lambda arguments bind the fn's type params to concrete Go
    /// types, and the lambda's `Fn(T) -> U` parameter type is then specialised
    /// (`func(int64) bool`) so the emitted closure's param is `x int64`, not the
    /// `interface{}` default an unannotated param falls back to (which both
    /// breaks `x > 2` arithmetic and mismatches the `func(int64) bool`
    /// parameter). Only generic fns are recorded (a non-generic fn's lambda arg
    /// already types correctly). Pre-scanned across the reached modules.
    fn_signatures: HashMap<String, GoFnSig>,
    /// Names of generic fns whose bound was lowered to a Go built-in constraint
    /// from a sealed-core trait (`[T: Comparable]` → `[T __bockOrdered]`, GAP-C).
    /// Under such a constraint Go infers `T` from an *untyped* constant arg as the
    /// default type (`int`, not `int64`), mismatching an `int64`-typed
    /// destination, so the call site must synthesise an explicit type arg
    /// (`max2[int64](9, 7)`) even though the signature touches no container. Set
    /// during the same pre-scan as [`Self::fn_signatures`].
    fn_sealed_bound: std::collections::HashSet<String>,
    /// Maps a *non-generic* top-level fn name → its rendered Go return type
    /// (`"key" → "Key"`). Lets [`Self::infer_go_expr_type`] type a call to a
    /// concrete constructor/helper so a list literal of such calls (`[key(3),
    /// key(1)]`) infers the homogeneous element type `Key` and emits `[]Key{…}`
    /// — which in turn lets a generic callee taking that slice (`from_list`,
    /// `max_of`) infer its element type rather than collapsing to
    /// `[]interface{}` / `[any]`. Generic fns live in [`Self::fn_signatures`].
    fn_return_go_types: HashMap<String, String>,
    /// Maps a *non-generic* top-level fn name → its value params' declared type
    /// nodes (the same `Vec<Option<AIRNode>>` shape [`Self::fn_signatures`] holds
    /// for generic fns). A non-generic fn taking a concrete `Fn(Todo) -> Bool`
    /// parameter — e.g. `count_where(todos, pred)` — must still pin an untyped
    /// `(t) => t.done` lambda argument to `func(t Todo) bool`; without this map
    /// the lambda erases to `func(t interface{}) bool` and `t.Done` fails (Go has
    /// no field on `interface{}`). Pre-scanned alongside `fn_signatures`; consumed
    /// at the call site as the lambda-specialisation fallback when the callee is
    /// not generic.
    fn_param_types: HashMap<String, Vec<Option<AIRNode>>>,
    /// The concrete Go parameter types an *untyped lambda argument* should adopt
    /// at its current call site, derived from the callee's generic signature
    /// specialised by the other arguments ([`Self::fn_signatures`]). A lambda's
    /// own params carry no source annotation (`(x) => x > 2`), so without this
    /// they default to `interface{}` — which both breaks the body's arithmetic
    /// and mismatches the typed `func(int64) bool` callee parameter. Set just
    /// before emitting such an argument, consumed (taken) by the lambda emit so
    /// it never leaks to a nested lambda. `None` for an ordinarily-typed lambda.
    expected_lambda_param_types: Option<Vec<String>>,
    /// A forced Go return type for the *next* lambda emitted, consumed (taken) by
    /// the `Lambda` arm. A predicate combinator (`filter`/`find`/`any`/`all`)
    /// always takes a `Bool`-returning closure, but the body may be a method call
    /// (`(p) => p.in_stock()`) or a `match` whose Go return type
    /// [`Self::infer_go_expr_type`] cannot recover — leaving the closure typed
    /// `func(T) interface{}`, which then fails `if __f(__x)` (`non-boolean
    /// condition`). Setting this to `Some("bool")` pins the predicate's return so
    /// the closure type-checks. `None` for an ordinarily-inferred lambda.
    forced_lambda_ret: Option<String>,
    /// True in the **per-module native-package** emission path
    /// ([`GoGenerator::generate_project`], the sole real-build path). When set,
    /// the `Module` arm does **not** inline the runtime preludes (they are
    /// emitted once into the shared `bock_runtime.go` by `generate_project`) —
    /// each module is its own `package main` file and same-package symbols are
    /// visible without an import. When clear, the module is emitted as a single
    /// self-contained file with its runtime preludes inlined — the
    /// [`GoGenerator::generate_module`] path used by unit tests.
    per_module: bool,
}

/// The set of Go stdlib packages the emitted body needs imported, gathered as
/// it is generated. Rendered into a single deduped `import (...)` block by
/// [`GoImportNeeds::render_block`]. Shared by the two emission entry points
/// ([`GoEmitCtx::into_parts`] for the per-module path and
/// [`GoEmitCtx::finish`] for the single-module path) so the import logic lives
/// in one place.
#[derive(Default, Clone, Copy)]
struct GoImportNeeds {
    fmt: bool,
    sync: bool,
    time: bool,
    strings: bool,
    utf8: bool,
    math: bool,
    unicode: bool,
    strconv: bool,
    reflect: bool,
}

impl GoImportNeeds {
    /// Render the needed packages as a Go `import` clause (`import "x"` for a
    /// single package, an `import (...)` block for several), or the empty string
    /// when nothing is needed. The order matches `gofmt`'s lexical sort.
    fn render_block(self) -> String {
        // Packages are pushed in `gofmt`'s lexical sort order.
        let mut imports = Vec::new();
        if self.fmt {
            imports.push("\"fmt\"");
        }
        if self.math {
            imports.push("\"math\"");
        }
        if self.reflect {
            imports.push("\"reflect\"");
        }
        if self.strconv {
            imports.push("\"strconv\"");
        }
        if self.strings {
            imports.push("\"strings\"");
        }
        if self.sync {
            imports.push("\"sync\"");
        }
        if self.time {
            imports.push("\"time\"");
        }
        if self.unicode {
            imports.push("\"unicode\"");
        }
        if self.utf8 {
            imports.push("\"unicode/utf8\"");
        }
        if imports.is_empty() {
            return String::new();
        }
        if imports.len() == 1 {
            return format!("\nimport {}\n", imports[0]);
        }
        let mut block = String::from("\nimport (\n");
        for imp in &imports {
            block.push_str(&format!("\t{imp}\n"));
        }
        block.push_str(")\n");
        block
    }
}

/// A recorded generic-function signature ([`GoEmitCtx::fn_signatures`]): the
/// declared generic-param names, each value param's declared type node, and the
/// return type node. Used to specialise an untyped lambda argument at a call
/// site (bind the type params from the non-lambda args, substitute into the
/// lambda's `Fn(...)` param type) and to infer a generic call's result type.
type GoFnSig = (Vec<String>, Vec<Option<AIRNode>>, Option<AIRNode>);

impl GoEmitCtx {
    fn new() -> Self {
        Self {
            buf: String::with_capacity(4096),
            indent: 0,
            needs_fmt_import: false,
            needs_sync_import: false,
            needs_time_import: false,
            needs_strings_import: false,
            needs_utf8_import: false,
            needs_math_import: false,
            needs_unicode_import: false,
            needs_strconv_import: false,
            needs_reflect_import: false,
            package_name: "main".into(),
            effect_ops: HashMap::new(),
            current_handler_vars: HashMap::new(),
            fn_effects: HashMap::new(),
            composite_effects: HashMap::new(),
            public_fns: HashSet::new(),
            void_effect_ops: HashSet::new(),
            async_fns: HashSet::new(),
            public_methods: HashSet::new(),
            inherent_methods: HashSet::new(),
            record_field_names: HashSet::new(),
            loop_labels: Vec::new(),
            switch_label_depth: 0,
            loop_label_counter: 0,
            guard_counter: 0,
            try_counter: 0,
            let_tuple_counter: 0,
            loop_expr_depth: 0,
            fn_optional_ret_elem: HashMap::new(),
            var_optional_elem: HashMap::new(),
            var_decl_type_node: HashMap::new(),
            method_optional_ret_elem: HashMap::new(),
            method_ret_record_args: HashMap::new(),
            method_return_go_types: HashMap::new(),
            var_lambda_ret: HashMap::new(),
            var_record_type_args: HashMap::new(),
            var_list_elem: HashMap::new(),
            var_map_kv: HashMap::new(),
            var_set_elem: HashMap::new(),
            var_result_elem: HashMap::new(),
            fn_result_ret_elem: HashMap::new(),
            concurrency_runtime_emitted: false,
            optional_runtime_emitted: false,
            result_runtime_emitted: false,
            numeric_runtime_emitted: false,
            ordering_runtime_emitted: false,
            ordered_constraint_emitted: false,
            range_runtime_emitted: false,
            int_pow_runtime_emitted: false,
            deep_eq_runtime_emitted: false,
            enum_variants: crate::generator::EnumVariantRegistry::new(),
            type_aliases: HashMap::new(),
            const_names: std::collections::HashSet::new(),
            generic_decls: crate::generator::GenericDeclRegistry::new(),
            method_freefn_lowered: HashMap::new(),
            var_go_type: HashMap::new(),
            go_declared_scopes: Vec::new(),
            pending_scope_seed: None,
            decl_only_types: HashMap::new(),
            record_param_fields: HashMap::new(),
            record_field_list_elem: HashMap::new(),
            record_field_map_kv: HashMap::new(),
            record_field_go_type: HashMap::new(),
            record_generic_param_names: HashMap::new(),
            current_self_record: None,
            trait_decls: crate::generator::TraitDeclRegistry::new(),
            type_names: HashSet::new(),
            go_self_subst: None,
            self_param_traits: HashSet::new(),
            current_fn_ret_type: None,
            current_fn_ret_type_node: None,
            current_expected_type: None,
            current_match_enum_type_args: String::new(),
            current_fn_ret_collection_elem: None,
            fn_signatures: HashMap::new(),
            fn_sealed_bound: std::collections::HashSet::new(),
            fn_return_go_types: HashMap::new(),
            fn_param_types: HashMap::new(),
            expected_lambda_param_types: None,
            forced_lambda_ret: None,
            expected_collection_elem: None,
            per_module: false,
        }
    }

    /// Clone the program-wide cross-module *analysis* state into a fresh
    /// emission context for one file of the per-module tree, resetting only the
    /// per-file emission state (output buffer + indent, the `needs_*` per-file
    /// import flags, and the runtime-once flags). The analysis registries
    /// (`enum_variants`, `trait_decls`, method/Optional-return metadata, …) are
    /// carried so a reference in one file to a symbol declared in another
    /// resolves correctly across the per-module tree.
    fn fork(&self) -> Self {
        let mut c = self.clone();
        c.buf = String::with_capacity(4096);
        c.indent = 0;
        c.needs_fmt_import = false;
        c.needs_sync_import = false;
        c.needs_time_import = false;
        c.needs_strings_import = false;
        c.needs_utf8_import = false;
        c.needs_math_import = false;
        c.needs_unicode_import = false;
        c.needs_strconv_import = false;
        c.needs_reflect_import = false;
        c.concurrency_runtime_emitted = false;
        c.optional_runtime_emitted = false;
        c.result_runtime_emitted = false;
        c.numeric_runtime_emitted = false;
        c.ordering_runtime_emitted = false;
        c.ordered_constraint_emitted = false;
        c.range_runtime_emitted = false;
        c.int_pow_runtime_emitted = false;
        c.deep_eq_runtime_emitted = false;
        c.per_module = false;
        c
    }

    /// Pre-seed the effect registries (`effect_ops`, `composite_effects`,
    /// `void_effect_ops`) from every module's top-level `EffectDecl`s. In the
    /// per-module path each module is emitted by its own forked context, so a
    /// bare op `log(...)` used in `main` whose effect `Log` is declared in
    /// another module must be recognised without having emitted the declaring
    /// module first (cross-module effects, §10). Mirrors the Python / JS / TS /
    /// Rust backends' equivalents.
    fn seed_effect_registries(&mut self, modules: &[(&AIRModule, &std::path::Path)]) {
        for (module, _) in modules {
            let NodeKind::Module { items, .. } = &module.kind else {
                continue;
            };
            for item in items {
                let NodeKind::EffectDecl {
                    name,
                    components,
                    operations,
                    ..
                } = &item.kind
                else {
                    continue;
                };
                if !components.is_empty() {
                    let comp_names: Vec<String> = components
                        .iter()
                        .map(|tp| {
                            tp.segments
                                .last()
                                .map_or("effect".to_string(), |s| s.name.clone())
                        })
                        .collect();
                    self.composite_effects.insert(name.name.clone(), comp_names);
                    continue;
                }
                for op in operations {
                    if let NodeKind::FnDecl {
                        name: op_name,
                        return_type,
                        ..
                    } = &op.kind
                    {
                        self.effect_ops
                            .insert(op_name.name.clone(), name.name.clone());
                        if return_type.as_deref().is_some_and(Self::is_void_type) {
                            self.void_effect_ops.insert(op_name.name.clone());
                        }
                    }
                }
            }
        }
    }

    /// The Go type to use for an expression-position `if`/`match` IIFE return.
    ///
    /// Prefers the binding's *expected* type ([`Self::current_expected_type`],
    /// set around a `let x: T = …` value emit) when known and concrete, so a
    /// value-position `let x: T = match …` produces `func() T { … }()` —
    /// assignable to `T` even when `T` differs from the enclosing function's
    /// return type. An `interface{}` expected type is ignored (it carries no more
    /// information than the untyped fallback and would suppress a more specific
    /// `current_fn_ret_type`). Falls back to the function's return type
    /// ([`Self::current_fn_ret_type`]) for the return-position case
    /// (`return match …`). `None` ⇒ the caller emits the `interface{}` fallback.
    fn expected_iife_type(&self) -> Option<String> {
        match self.current_expected_type.as_deref() {
            Some(t) if t != "interface{}" => Some(t.to_string()),
            _ => self.current_fn_ret_type.clone(),
        }
    }

    /// Populate [`Self::self_param_traits`] from the already-built
    /// [`Self::trait_decls`] registry. Call after `trait_decls` is set.
    fn derive_self_param_traits(&mut self) {
        for (name, info) in &self.trait_decls {
            if crate::generator::trait_uses_self_operand(info) {
                self.self_param_traits.insert(name.clone());
            }
        }
    }

    /// Whether `name` is bound as a *local value* (parameter, `let`, match /
    /// guard bind, typed loop var) at the current emission point, in which case
    /// it must shadow a same-named public module function
    /// (Q-go-runtime-helper-shadowing). When any `core.string` item is imported
    /// the whole module is reached and its public fns (`lines`, `repeat`, …)
    /// enter [`Self::public_fns`]; without this check the identifier emitter
    /// PascalCased EVERY bare reference, so a `lines: List[String]` parameter
    /// used in `for line in lines` emitted `range Lines` — the helper
    /// *function* — which Go rejects. Bock scoping says the local wins; the
    /// checker already resolved it that way, so the Go spelling must too.
    ///
    /// Locals are tracked in two places, both keyed by the Go-escaped name and
    /// both populated only as emission *reaches* the binding (so a reference
    /// preceding a later same-named `let` still resolves to the module fn):
    /// [`Self::var_go_type`] (params on fn/method/lambda entry, typed loop vars,
    /// typed binds — saved/restored per scope) and the
    /// [`Self::go_declared_scopes`] frames (every `let`/bind the open blocks
    /// declared, seeded with the parameter names). Checked across *all* open
    /// frames — an outer fn-scope binding shadows inside nested blocks too.
    fn local_shadows_public_fn(&self, name: &str) -> bool {
        if !self.public_fns.contains(name) {
            return false;
        }
        let key = go_value_ident(name);
        self.var_go_type.contains_key(&key)
            || self
                .go_declared_scopes
                .iter()
                .any(|frame| frame.contains(&key))
    }

    /// The Go identifier for a top-level Bock function reference, applying the
    /// public/private PascalCase/camelCase rule and then disambiguating a public
    /// name that collides with a top-level type. Go has a single namespace for
    /// types and functions, and PascalCasing collapses Bock's `key`/`Key` case
    /// distinction onto one identifier; when a public function's Go name equals a
    /// declared type name (`func Key` vs `type Key`), the function is suffixed
    /// with `Fn` (`KeyFn`). The same rule is applied at the declaration site and
    /// every call/reference site so they always agree.
    fn go_fn_name(&self, name: &str) -> String {
        if self.public_fns.contains(name) {
            let pascal = to_pascal_case(name);
            if self.type_names.contains(&pascal) {
                format!("{pascal}Fn")
            } else {
                pascal
            }
        } else {
            // Private fns and bare value references both route here; escape so a
            // `camelCase` name colliding with a Go keyword (`default`, `range`,
            // `type`, …) is mangled identically at the declaration and every
            // reference, keeping them in sync.
            go_value_ident(name)
        }
    }

    /// Pre-scan every module's top-level type declarations (records, enums,
    /// traits, classes) into [`Self::type_names`], and every `public` top-level
    /// function name into [`Self::public_fns`], so [`Self::go_fn_name`] can
    /// detect a function-name/type-name collision at *any* call site — including
    /// a call that precedes the function's declaration in emission order.
    /// Mirrors the other pre-scans.
    fn collect_fn_and_type_names(&mut self, module: &AIRNode) {
        if let NodeKind::Module { items, .. } = &module.kind {
            for item in items {
                match &item.kind {
                    NodeKind::RecordDecl { name, .. }
                    | NodeKind::EnumDecl { name, .. }
                    | NodeKind::TraitDecl { name, .. }
                    | NodeKind::ClassDecl { name, .. } => {
                        self.type_names.insert(name.name.clone());
                    }
                    NodeKind::FnDecl {
                        visibility, name, ..
                    } if matches!(visibility, Visibility::Public) && name.name != "main" => {
                        self.public_fns.insert(name.name.clone());
                    }
                    _ => {}
                }
                // Record every *generic* top-level fn's signature (generic-param
                // names + each value param's declared type) so a call site can
                // specialise an untyped lambda argument to the concrete Go type
                // (see `fn_signatures`). Recorded regardless of visibility — the
                // embedded `core.iter` combinators are public, but a user's
                // private generic fn taking a lambda needs the same treatment.
                if let NodeKind::FnDecl {
                    name,
                    generic_params,
                    params,
                    ..
                } = &item.kind
                {
                    if !generic_params.is_empty() {
                        let gp_names: Vec<String> =
                            generic_params.iter().map(|p| p.name.name.clone()).collect();
                        let param_tys: Vec<Option<AIRNode>> = params
                            .iter()
                            .map(|p| match &p.kind {
                                NodeKind::Param { ty, .. } => ty.as_deref().cloned(),
                                _ => None,
                            })
                            .collect();
                        let ret_ty = if let NodeKind::FnDecl { return_type, .. } = &item.kind {
                            return_type.as_deref().cloned()
                        } else {
                            None
                        };
                        // A generic param whose sealed-core bound was lowered to a
                        // Go built-in constraint defeats Go's untyped-constant
                        // inference (GAP-C), so the call site must synthesise an
                        // explicit type arg — record the fn.
                        let has_sealed_bound = generic_params.iter().any(|p| {
                            p.bounds.iter().any(|b| {
                                let bn = b
                                    .segments
                                    .iter()
                                    .map(|s| s.name.as_str())
                                    .collect::<Vec<_>>()
                                    .join(".");
                                crate::generator::is_unimplemented_sealed_core_trait(
                                    &bn,
                                    &self.trait_decls,
                                )
                            })
                        });
                        if has_sealed_bound {
                            self.fn_sealed_bound.insert(name.name.clone());
                        }
                        self.fn_signatures
                            .insert(name.name.clone(), (gp_names, param_tys, ret_ty));
                    } else if let NodeKind::FnDecl { return_type, .. } = &item.kind {
                        // A *non-generic* fn: record its rendered Go return type so
                        // a call (`key(3)`) can be typed for homogeneous list-elem
                        // inference. Skip `Void`/`Unit` returns (no usable type).
                        if let Some(ret) = return_type.as_deref() {
                            if !Self::is_void_type(ret) {
                                self.fn_return_go_types
                                    .insert(name.name.clone(), self.type_to_go(ret));
                            }
                        }
                        // Record the value params' declared type nodes so an
                        // untyped lambda argument to a concrete `Fn(...)` param
                        // (`count_where(todos, (t) => t.done)`) can be specialised
                        // to `func(t Todo) bool` rather than `func(t interface{})`.
                        let param_tys: Vec<Option<AIRNode>> = params
                            .iter()
                            .map(|p| match &p.kind {
                                NodeKind::Param { ty, .. } => ty.as_deref().cloned(),
                                _ => None,
                            })
                            .collect();
                        if param_tys.iter().any(Option::is_some) {
                            self.fn_param_types.insert(name.name.clone(), param_tys);
                        }
                    }
                }
            }
        }
    }

    /// Pre-scan a module's top-level `RecordDecl`s and, for each generic
    /// record, record which field's declared type is each generic param (in
    /// param order). A construction site then looks up the field value's Go
    /// type per param to emit the explicit `[arg, ...]` instantiation Go
    /// requires. Additive across the reached modules (mirrors the other `collect_*`).
    fn collect_record_param_fields(&mut self, module: &AIRModule) {
        let NodeKind::Module { items, .. } = &module.kind else {
            return;
        };
        for item in items {
            let NodeKind::RecordDecl {
                name,
                generic_params,
                fields,
                ..
            } = &item.kind
            else {
                continue;
            };
            // Record each `List[...]`-typed field's Go element type, keyed by
            // field name — used to type a `self.field.get(i)` list-method
            // receiver's closure inside the record's methods. Done for every
            // record (generic or not): a non-generic record may still hold a
            // `List[Int]` field whose method-side receiver needs `[]int64`.
            let list_fields: HashMap<String, String> = fields
                .iter()
                .filter_map(|f| {
                    Self::list_field_elem_type(&f.ty)
                        .map(|elem_ty| (f.name.name.clone(), self.ast_type_to_go(elem_ty)))
                })
                .collect();
            if !list_fields.is_empty() {
                self.record_field_list_elem
                    .insert(name.name.clone(), list_fields);
            }
            // Record each `Map[K, V]`-typed field's Go key/value types, keyed by
            // field name — used to type a `record.field.get(k)` map-method
            // receiver's inline closure (`map[K]V` / `K` / `V`) from the field's
            // declared types rather than the erased `map[interface{}]interface{}`.
            let map_fields: HashMap<String, (String, String)> = fields
                .iter()
                .filter_map(|f| {
                    Self::map_field_kv_type(&f.ty).map(|(k, v)| {
                        (
                            f.name.name.clone(),
                            (self.ast_type_to_go(k), self.ast_type_to_go(v)),
                        )
                    })
                })
                .collect();
            if !map_fields.is_empty() {
                self.record_field_map_kv
                    .insert(name.name.clone(), map_fields);
            }
            // Record EVERY field's Go type, keyed by field name — lets
            // `infer_go_expr_type` resolve a bare `obj.field` access (a
            // `.map((b) => b.id)` closure body) to the field's concrete Go type,
            // sizing the result slice concretely rather than as `[]interface{}`.
            let field_go_types: HashMap<String, String> = fields
                .iter()
                .map(|f| (f.name.name.clone(), self.ast_type_to_go(&f.ty)))
                .collect();
            if !field_go_types.is_empty() {
                self.record_field_go_type
                    .insert(name.name.clone(), field_go_types);
            }
            if generic_params.is_empty() {
                continue;
            }
            self.record_generic_param_names.insert(
                name.name.clone(),
                generic_params.iter().map(|p| p.name.name.clone()).collect(),
            );
            let per_param: Vec<Option<String>> = generic_params
                .iter()
                .map(|gp| {
                    fields
                        .iter()
                        .find(|f| Self::ast_type_is_param(&f.ty, &gp.name.name))
                        .map(|f| f.name.name.clone())
                })
                .collect();
            self.record_param_fields
                .insert(name.name.clone(), per_param);
        }
    }

    /// If `ty` is a `List[Elem]` named type, return its element `TypeExpr`,
    /// else `None`. Used to record a record field's list element type for
    /// method-side receiver typing.
    fn list_field_elem_type(ty: &TypeExpr) -> Option<&TypeExpr> {
        match ty {
            TypeExpr::Named { path, args, .. }
                if args.len() == 1 && path.segments.last().is_some_and(|s| s.name == "List") =>
            {
                args.first()
            }
            _ => None,
        }
    }

    /// The `(key, value)` type expressions of a `Map[K, V]`-typed field, or
    /// `None` for any other type. The `Map` analogue of
    /// [`Self::list_field_elem_type`]; used to populate
    /// [`Self::record_field_map_kv`].
    fn map_field_kv_type(ty: &TypeExpr) -> Option<(&TypeExpr, &TypeExpr)> {
        match ty {
            TypeExpr::Named { path, args, .. }
                if args.len() == 2 && path.segments.last().is_some_and(|s| s.name == "Map") =>
            {
                Some((args.first()?, args.get(1)?))
            }
            _ => None,
        }
    }

    /// True when `ty` is a bare named type whose single segment is `param`
    /// (i.e. the field is declared with exactly the generic param `T`, not
    /// `List[T]` or some other composite).
    fn ast_type_is_param(ty: &TypeExpr, param: &str) -> bool {
        matches!(
            ty,
            TypeExpr::Named { path, args, .. }
                if args.is_empty()
                    && path.segments.len() == 1
                    && path.segments[0].name == param
        )
    }

    /// Variant info for `path` when its last segment is a registered *user*
    /// enum variant (built-in Optional/Result pre-seeds excluded — Optional has
    /// its own `__bockOption` runtime, handled by the bespoke `go_match_is_*`
    /// paths).
    fn user_variant_for_path(
        &self,
        path: &bock_ast::TypePath,
    ) -> Option<&crate::generator::EnumVariantInfo> {
        let info = crate::generator::registered_variant(&self.enum_variants, path)?;
        if matches!(info.enum_name.as_str(), "Optional" | "Result") {
            return None;
        }
        Some(info)
    }

    /// As [`Self::user_variant_for_path`] but keyed by a bare identifier name.
    fn user_variant_for_name(&self, name: &str) -> Option<&crate::generator::EnumVariantInfo> {
        let info = self.enum_variants.get(name)?;
        if matches!(info.enum_name.as_str(), "Optional" | "Result") {
            return None;
        }
        Some(info)
    }

    /// True when the real `core.compare.Ordering` enum is reachable in this
    /// program (its `Less` variant is a registered user enum variant). When
    /// `core.compare` is `use`d, the actual `enum Ordering` decl is emitted; its
    /// `Less`/`Equal`/`Greater` references and matches then use the user-enum
    /// representation (sealed-interface variant structs `OrderingLess{}`), not
    /// the prelude `__bockOrdering` value runtime used when the enum is *not*
    /// reachable (e.g. a bare primitive `compare`).
    fn ordering_enum_reachable(&self) -> bool {
        self.enum_variants
            .get("Less")
            .is_some_and(|info| info.enum_name == "Ordering")
    }

    /// True if every arm of a `match` is a registered user enum variant pattern
    /// (constructor / record / unit), so the match lowers to a Go *type-switch*
    /// over the sealed-interface concrete types with field extraction.
    fn go_match_is_user_enum(&self, arms: &[AIRNode]) -> bool {
        let mut saw_variant = false;
        for arm in arms {
            let NodeKind::MatchArm { pattern, .. } = &arm.kind else {
                continue;
            };
            match &pattern.kind {
                NodeKind::ConstructorPat { path, .. } | NodeKind::RecordPat { path, .. }
                    if self.user_variant_for_path(path).is_some() =>
                {
                    saw_variant = true;
                }
                // Any constructor / record pattern that is NOT a registered
                // user variant disqualifies the type-switch lowering.
                NodeKind::ConstructorPat { .. } | NodeKind::RecordPat { .. } => return false,
                // A trailing `_` / bind arm is a permissible default.
                NodeKind::WildcardPat | NodeKind::BindPat { .. } => {}
                _ => return false,
            }
        }
        saw_variant
    }

    /// The owning enum name of a user-enum match's arms, if any arm names a
    /// registered variant. Used to recover the enum's generic instantiation
    /// (`Box[int64]`) so a type-switch `case`/type-assert spells the concrete
    /// variant struct (`case BoxFull[int64]:`). All arms of one match share an
    /// enum (a type-switch on a single sealed-interface value), so the first
    /// variant-bearing arm decides.
    fn match_owner_enum_name(&self, arms: &[AIRNode]) -> Option<String> {
        for arm in arms {
            let NodeKind::MatchArm { pattern, .. } = &arm.kind else {
                continue;
            };
            let path = match &pattern.kind {
                NodeKind::ConstructorPat { path, .. } | NodeKind::RecordPat { path, .. } => path,
                _ => continue,
            };
            if let Some(info) = self.user_variant_for_path(path) {
                return Some(info.enum_name.clone());
            }
        }
        None
    }

    /// The concrete generic type-arg suffix (`[int64]`) to apply to the variant
    /// structs of a user-enum `match`, recovered from the scrutinee's inferred Go
    /// type (`Box[int64]`) and the match's owning enum. `""` when the scrutinee
    /// type is unknown, the enum is non-generic, or the inferred type is not
    /// exactly `<enum>[...]`. Drives [`Self::current_match_enum_type_args`].
    fn match_enum_type_arg_suffix(&self, scrutinee: &AIRNode, arms: &[AIRNode]) -> String {
        let Some(enum_name) = self.match_owner_enum_name(arms) else {
            return String::new();
        };
        match self.infer_go_expr_type(scrutinee) {
            Some(full) => self.enum_variant_type_arg_suffix(&enum_name, &full),
            None => String::new(),
        }
    }

    /// True if any arm of a user-enum type-switch binds a payload field from the
    /// concrete `__v` (a `ConstructorPat`/`RecordPat` with at least one non-`_`
    /// sub-pattern). When no arm does, the statement-position type-switch binds
    /// `__v` but never reads it — Go's "declared and not used" — unless a
    /// `default: panic(... __v)` consumes it. See [`Self::emit_match`].
    fn go_user_enum_match_binds_payload(arms: &[AIRNode]) -> bool {
        arms.iter().any(|arm| {
            let NodeKind::MatchArm { pattern, .. } = &arm.kind else {
                return false;
            };
            match &pattern.kind {
                NodeKind::ConstructorPat { fields, .. } => fields
                    .iter()
                    .any(|f| !matches!(f.kind, NodeKind::WildcardPat)),
                NodeKind::RecordPat { fields, .. } => fields.iter().any(|f| {
                    f.pattern
                        .as_ref()
                        .is_none_or(|p| !matches!(p.kind, NodeKind::WildcardPat))
                }),
                _ => false,
            }
        })
    }

    /// True if any arm's top-level pattern is a bare `BindPat` (`x => …`,
    /// `mut x => …`). Such a match cannot use the value-switch IIFE in expression
    /// position: a bind has no value to `case` on, so the switch lowering emits
    /// the broken `case interface{}:` and drops the bound name. The
    /// expression-position emitter routes these to the if-chain IIFE instead
    /// (which binds `x := root` in an unconditional `else`). Statement position is
    /// unaffected — `emit_match`'s `value_switch_binds` already handles it via the
    /// `switch __v := scrutinee; __v` form. Mirrors the `match_needs_ifchain`
    /// gate without touching that shared single-discriminant fast-path.
    fn go_value_match_has_bind_arm(arms: &[AIRNode]) -> bool {
        arms.iter().any(|arm| {
            matches!(
                &arm.kind,
                NodeKind::MatchArm { pattern, .. }
                    if matches!(pattern.kind, NodeKind::BindPat { .. })
            )
        })
    }

    /// True if any arm matches a *plain* (non-enum-variant) record with only
    /// bind/wildcard fields (`Point { x, .. }`, `Point { x, y }`). Such an arm is
    /// not "structured" by `match_needs_ifchain` (no nested sub-pattern), so a
    /// value-position match made solely of these stays on the value-switch
    /// fast-path — which emits the broken `case Point:` (a Go *type* in expression
    /// position) and drops the field binds (`undefined: x`). A plain record is a
    /// concrete struct, not a sealed-interface value, so it has no value/type to
    /// switch on at all; route the match to the if-chain IIFE, whose
    /// `pattern_test_go` / `collect_binds_go` read each field directly off
    /// `access.<Field>`. (A record arm with a *literal* field — `Point { x: 0 }` —
    /// is already structured, so `match_needs_ifchain` diverts it; this only
    /// covers the all-bind/wildcard plain-record arm.) Does not touch the shared
    /// `match_needs_ifchain`.
    fn go_value_match_has_plain_record_arm(&self, arms: &[AIRNode]) -> bool {
        arms.iter().any(|arm| {
            let NodeKind::MatchArm { pattern, .. } = &arm.kind else {
                return false;
            };
            matches!(&pattern.kind, NodeKind::RecordPat { path, .. }
                if self.user_variant_for_path(path).is_none())
        })
    }

    /// Pre-scan the module for top-level `async fn` names. Must be populated
    /// before any Call node is emitted so the Async-suffix rewrite at call
    /// sites covers both forward and backward references within the module.
    fn collect_async_fns(&mut self, module: &AIRNode) {
        if let NodeKind::Module { items, .. } = &module.kind {
            for item in items {
                if let NodeKind::FnDecl {
                    is_async: true,
                    name,
                    ..
                } = &item.kind
                {
                    self.async_fns.insert(name.name.clone());
                }
            }
        }
    }

    /// Pre-scan all impl/class/trait blocks for `public` method names so call
    /// sites can match the Go method casing (PascalCase public, camelCase
    /// private).
    ///
    /// Trait methods — both those declared in a `TraitDecl` and those of an
    /// `impl Trait for Type` block — are recorded *regardless of Bock
    /// visibility*: Go interface methods are always emitted exported
    /// (PascalCase, see the `TraitDecl` emission), so the method must be
    /// PascalCased everywhere (interface signature, receiver method, and call
    /// site) for the type to satisfy the interface. A `private` trait default
    /// method would otherwise be PascalCased in the interface but camelCased at
    /// the call site, and the call would not resolve. Inherent (`impl Type`)
    /// and class methods keep the public-only rule.
    fn collect_methods(&mut self, module: &AIRNode) {
        // Collect every record/class field's PascalCased Go name so
        // `go_method_name` can detect a field/method name collision Go
        // forbids on a struct (`SimpleError { message }` + a `message`
        // method). The shared collector (used identically by js/ts/py) walks
        // every record/class regardless of where the colliding method is
        // declared (a separate `impl` block). `collect_methods` is called once
        // per reachable module to build a *program-wide* set on the template
        // ctx (see `generate_project`), so we extend rather than replace.
        self.record_field_names
            .extend(crate::generator::collect_record_field_names(
                module,
                to_pascal_case,
            ));
        if let NodeKind::Module { items, .. } = &module.kind {
            for item in items {
                // `inherent_target` is `Some(type name)` for an inherent (`impl
                // Type`, no trait) or class block — used to record
                // `(type, method)` so a redundant same-named trait-impl forwarder
                // can be skipped. A trait impl (`impl Trait for Type`) is not an
                // inherent definition (it is the forwarder we may skip).
                let (methods, always_export, inherent_target) = match &item.kind {
                    NodeKind::ImplBlock {
                        methods,
                        trait_path,
                        target,
                        ..
                    } => {
                        let inherent = if trait_path.is_none() {
                            Some(self.type_expr_to_string(target))
                        } else {
                            None
                        };
                        (methods, trait_path.is_some(), inherent)
                    }
                    NodeKind::TraitDecl { methods, .. } => (methods, true, None),
                    NodeKind::ClassDecl { methods, name, .. } => {
                        (methods, false, Some(name.name.clone()))
                    }
                    _ => continue,
                };
                for m in methods {
                    if let NodeKind::FnDecl {
                        visibility,
                        name,
                        generic_params,
                        ..
                    } = &m.kind
                    {
                        if always_export || matches!(visibility, Visibility::Public) {
                            self.public_methods.insert(name.name.clone());
                        }
                        if let Some(ty) = &inherent_target {
                            // Key on the PascalCased Go method name: a trait
                            // declares its methods exported (PascalCase), so a
                            // skip check from the trait-impl side compares against
                            // that casing. The inherent method itself is exported
                            // to the same Go name when its name is in
                            // `public_methods` (see `emit_method_body`).
                            self.inherent_methods
                                .insert((ty.clone(), to_pascal_case(&name.name)));
                            // DQ28: an inherent/class method that declares its own
                            // type parameters (`Box[T].map[U]`) cannot be a Go
                            // method (Go forbids method type params). Record it for
                            // free-function lowering, keyed by the Bock method
                            // name → owning type. Poison the entry if a second type
                            // declares a generic method of the same name (ambiguous
                            // at the by-name call site): set the value to a sentinel
                            // so the lookup treats it as absent.
                            if !generic_params.is_empty() {
                                use std::collections::hash_map::Entry;
                                match self.method_freefn_lowered.entry(name.name.clone()) {
                                    Entry::Vacant(e) => {
                                        e.insert(ty.clone());
                                    }
                                    Entry::Occupied(mut e) => {
                                        if e.get() != ty {
                                            // Ambiguous: two types, same generic
                                            // method name. Poison with a sentinel
                                            // that names no real type.
                                            e.insert(String::new());
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    /// The Go method name for a Bock method, applying the public/private
    /// PascalCase/camelCase rule and then disambiguating a public method whose
    /// PascalCased name collides with a struct field name. Go forbids a struct
    /// having a field and a method with the same name, so when a public method's
    /// Go name (`Message`) equals a record/class field name (`SimpleError`'s
    /// `message` field), the method is suffixed `Method` (`MessageMethod`). The
    /// same rule is applied at the trait-interface declaration, the receiver
    /// method, and every call site so they always agree. Private methods are
    /// camelCased and never collide with a (PascalCased) field name.
    fn go_method_name(&self, name: &str, is_public: bool) -> String {
        if is_public {
            // Shared collision policy (js/ts/py route through the same helper):
            // a public method whose PascalCased name equals a field name gets a
            // `Method` suffix (`Message` → `MessageMethod`).
            crate::generator::disambiguate_method_name(
                to_pascal_case(name),
                &self.record_field_names,
                "Method",
            )
        } else {
            to_camel_case(name)
        }
    }

    /// The owning Go type name of a method that is *free-function-lowered* for
    /// DQ28 (a method with its own type parameters, e.g. `Box[T].map[U]`), or
    /// `None` when the method name is not lowered or is ambiguous (the poison
    /// sentinel — an empty type name — reads as absent). When `Some(ty)`, a call
    /// site `recv.method(args)` lowers to `<FreeFnName>(recv, args)` and the
    /// declaration emits a free function instead of a Go method.
    fn freefn_lowered_type(&self, method_name: &str) -> Option<&str> {
        self.method_freefn_lowered
            .get(method_name)
            .map(String::as_str)
            .filter(|ty| !ty.is_empty())
    }

    /// The Go free-function name a DQ28-lowered method lowers to:
    /// `<TypeName>_<MethodGoName>` (`Box` + `Map` → `Box_Map`). The method name
    /// is PascalCased via [`Self::go_method_name`] (a public method matches its
    /// every call site; a private one camelCases) so the declaration and call
    /// site always agree. The `<Type>_` prefix guarantees collision-free naming
    /// across types that share a method name.
    fn freefn_lowered_name(&self, type_name: &str, method_name: &str, is_public: bool) -> String {
        format!(
            "{type_name}_{}",
            self.go_method_name(method_name, is_public)
        )
    }

    /// Pre-scan top-level functions whose declared return type is `Optional[T]`,
    /// recording `fn name → Go element type` of `T`. This lets a `match` whose
    /// scrutinee is a call to such a function (`match next(it) { Some(x) => ...
    /// }`) type-assert the bound payload to its concrete type. Must run before
    /// any match is emitted, so it covers forward references within the module.
    /// Pre-scan every top-level `type X = …` alias, recording `X → underlying
    /// type AIR node`. Lets the emitter resolve an alias *name* to its target Go
    /// type ([`Self::resolve_type_alias`]) wherever it appears — a function
    /// return/param type, or a `Result`/`Optional` element scan — so an alias to a
    /// runtime container (`type ParseResult = Result[...]`) lowers identically to
    /// the inlined container. Only non-generic aliases are recorded (a generic
    /// alias `type Pair[A, B] = (A, B)` would need substitution the emitter does
    /// not perform; it is left to fall through to its existing rendering).
    fn collect_type_aliases(&mut self, module: &AIRNode) {
        if let NodeKind::Module { items, .. } = &module.kind {
            for item in items {
                if let NodeKind::TypeAlias {
                    name,
                    generic_params,
                    ty,
                    ..
                } = &item.kind
                {
                    if generic_params.is_empty() {
                        self.type_aliases
                            .entry(name.name.clone())
                            .or_insert_with(|| (**ty).clone());
                    }
                }
            }
        }
    }

    /// If `node` is a `TypeNamed` naming a recorded non-generic type alias, return
    /// its underlying type AIR node (following at most one level — Bock aliases do
    /// not chain in practice, and a bounded depth avoids any cycle). `None` for a
    /// non-alias or non-`TypeNamed` node.
    fn resolve_type_alias(&self, node: &AIRNode) -> Option<&AIRNode> {
        if let NodeKind::TypeNamed { path, args } = &node.kind {
            if args.is_empty() {
                if let Some(seg) = path.segments.last() {
                    return self.type_aliases.get(&seg.name);
                }
            }
        }
        None
    }

    fn collect_optional_returns(&mut self, module: &AIRNode) {
        if let NodeKind::Module { items, .. } = &module.kind {
            for item in items {
                // Effect operations are dispatched by bare name (`read(key)` with
                // the handler resolved implicitly), so the AIR lowers a call to one
                // as `Call(Identifier "read", ...)` — indistinguishable from a free
                // fn at the call site. Record each effect op's `Optional`/`Result`
                // return into the by-name maps so a `match` on such a call (or on a
                // binding of it) type-asserts the boxed payload concretely. Without
                // this, the bound `interface{}` payload fails a later concrete use
                // (effect-showcase: `raw := storage.read(key); match raw { Some(v)
                // => return v }`, `v` wanted as `string`).
                if let NodeKind::EffectDecl { operations, .. } = &item.kind {
                    for op in operations {
                        if let NodeKind::FnDecl {
                            name,
                            return_type: Some(rt),
                            ..
                        } = &op.kind
                        {
                            if let Some(elem) = self.optional_elem_go_type(rt) {
                                self.fn_optional_ret_elem
                                    .entry(name.name.clone())
                                    .or_insert(elem);
                            }
                            if let Some(elems) = self.result_elem_go_types(rt) {
                                self.fn_result_ret_elem
                                    .entry(name.name.clone())
                                    .or_insert(elems);
                            }
                        }
                    }
                }
                if let NodeKind::FnDecl {
                    name,
                    return_type: Some(rt),
                    ..
                } = &item.kind
                {
                    if let Some(elem) = self.optional_elem_go_type(rt) {
                        self.fn_optional_ret_elem.insert(name.name.clone(), elem);
                    }
                    // Same pre-scan for `Result[T, E]` returns, so a `match
                    // parse(s) { Ok(n) => ... }` on a call scrutinee asserts the
                    // bound payload's Ok/Err type (mirrors the Optional path).
                    if let Some(elems) = self.result_elem_go_types(rt) {
                        self.fn_result_ret_elem.insert(name.name.clone(), elems);
                    }
                }
            }
        }
    }

    /// Pre-scan every impl/class/trait block for methods whose declared return
    /// type is `Optional[T]`, recording `method name → Go element type` of `T`.
    /// This lets a `match` whose scrutinee is a method call
    /// (`match it.next() { Some(x) => ... }`) type-assert the bound payload to
    /// its concrete element type — the shape `for x in <user-Iterable>` desugars
    /// to (a `loop`/`while` over `it.next(): T?`). Without it the payload stays
    /// the runtime `interface{}` and any typed use (`sum + x`) fails `go build`.
    ///
    /// Keyed by method name only — the Go backend works from the AIR, not the
    /// checker's per-type `method_types`. If the same method name appears on two
    /// types with *different* Optional element types, the entry is poisoned (its
    /// value cleared and a sentinel recorded) so resolution returns `None` and
    /// the payload safely falls back to `interface{}`. Must run before any match
    /// is emitted so it covers forward references within the module.
    fn collect_method_optional_returns(&mut self, module: &AIRNode) {
        // Methods sharing a name but disagreeing on element type are ambiguous;
        // track them here so the final map omits them entirely.
        let mut poisoned: HashSet<String> = HashSet::new();
        let mut poisoned_record: HashSet<String> = HashSet::new();
        let mut poisoned_go: HashSet<String> = HashSet::new();
        if let NodeKind::Module { items, .. } = &module.kind {
            for item in items {
                // The item's in-scope generic-param names: an impl's own plus
                // the target record's (`impl ListIterator { ... }` inherits the
                // `[T]` from `record ListIterator[T]`); a trait's declared
                // params. A method whose record return names one of these is
                // *not* a concrete return (it is the generic declaration), so it
                // is excluded from `method_ret_record_args`.
                let (methods, item_params): (&Vec<AIRNode>, Vec<String>) = match &item.kind {
                    NodeKind::ImplBlock {
                        methods,
                        generic_params,
                        target,
                        ..
                    } => {
                        let mut ps: Vec<String> =
                            generic_params.iter().map(|p| p.name.name.clone()).collect();
                        let target_name = self.type_expr_to_string(target);
                        if let Some(decl) = self.generic_decls.get(&target_name) {
                            ps.extend(decl.iter().map(|p| p.name.name.clone()));
                        }
                        (methods, ps)
                    }
                    NodeKind::ClassDecl {
                        methods,
                        generic_params,
                        ..
                    }
                    | NodeKind::TraitDecl {
                        methods,
                        generic_params,
                        ..
                    } => (
                        methods,
                        generic_params.iter().map(|p| p.name.name.clone()).collect(),
                    ),
                    _ => continue,
                };
                for m in methods {
                    if let NodeKind::FnDecl {
                        name,
                        return_type: Some(rt),
                        ..
                    } = &m.kind
                    {
                        if let Some(elem) = self.optional_elem_go_type(rt) {
                            match self.method_optional_ret_elem.get(&name.name) {
                                Some(existing) if *existing != elem => {
                                    poisoned.insert(name.name.clone());
                                }
                                _ => {
                                    self.method_optional_ret_elem
                                        .insert(name.name.clone(), elem);
                                }
                            }
                        }
                        // A method returning a *concrete* generic-record apply
                        // (`iter() -> ListIterator[Int]`, no remaining param) is
                        // recorded so an untyped binding of its call resolves the
                        // record args (`for x in bag` → `__it := bag.Iter()`).
                        // A return still naming an in-scope generic param (the
                        // `Iterable[T]` trait decl's `iter() -> ListIterator[T]`)
                        // is skipped — it is the generic signature, not a
                        // concrete return, and would falsely poison the concrete
                        // impl's entry.
                        if let Some(args) = self.record_type_args(rt) {
                            let is_concrete =
                                !args.1.iter().any(|a| item_params.iter().any(|p| p == a));
                            if is_concrete {
                                match self.method_ret_record_args.get(&name.name) {
                                    Some(existing) if *existing != args => {
                                        poisoned_record.insert(name.name.clone());
                                    }
                                    _ => {
                                        self.method_ret_record_args.insert(name.name.clone(), args);
                                    }
                                }
                            }
                        }
                        // Record the method's concrete Go return type, so
                        // `infer_go_expr_type` can type a `recv.method()` call
                        // (chiefly a `.map`/`.filter` closure body). A return
                        // type that still names an in-scope generic param is
                        // skipped — it is the generic signature, not concrete, and
                        // the calling site (a different fn) has no such `T`.
                        if !Self::type_mentions_params(rt, &item_params) {
                            let go_ty = self.type_to_go(rt);
                            match self.method_return_go_types.get(&name.name) {
                                Some(existing) if *existing != go_ty => {
                                    poisoned_go.insert(name.name.clone());
                                }
                                _ => {
                                    self.method_return_go_types.insert(name.name.clone(), go_ty);
                                }
                            }
                        }
                    }
                }
            }
        }
        for name in &poisoned {
            self.method_optional_ret_elem.remove(name);
        }
        for name in &poisoned_record {
            self.method_ret_record_args.remove(name);
        }
        for name in &poisoned_go {
            self.method_return_go_types.remove(name);
        }
    }

    /// If `node` is an `Optional[T]` type expression, return the Go type of its
    /// element `T`; otherwise `None`. Used to type-assert the `interface{}`
    /// payload of the Go Optional runtime back to its concrete element type at
    /// `match` arms. The element type is reachable structurally here because it
    /// lives in the `TypeOptional`/`Optional`-named node, unlike at the
    /// scrutinee expression (whose carried `type_info` is a stub).
    fn optional_elem_go_type(&self, node: &AIRNode) -> Option<String> {
        // See through a `type X = Optional[T]` alias (mirrors
        // `result_elem_go_types`).
        if let Some(target) = self.resolve_type_alias(node) {
            return self.optional_elem_go_type(target);
        }
        match &node.kind {
            NodeKind::TypeOptional { inner } => Some(self.type_to_go(inner)),
            NodeKind::TypeNamed { path, args } => {
                let is_optional = path.segments.last().is_some_and(|s| s.name == "Optional");
                if is_optional {
                    Some(
                        args.first()
                            .map_or_else(|| "interface{}".to_string(), |a| self.type_to_go(a)),
                    )
                } else {
                    None
                }
            }
            _ => None,
        }
    }

    /// If `node` is a `Result[T, E]` type expression, return the Go types of its
    /// `Ok` and `Err` payloads `(T, E)`; otherwise `None`. The Result analogue of
    /// [`Self::optional_elem_go_type`]: used to type-assert the `interface{}`
    /// payload of the Go Result runtime back to its concrete Ok/Err type at a
    /// `match` arm. A missing arg defaults to `interface{}`.
    fn result_elem_go_types(&self, node: &AIRNode) -> Option<(String, String)> {
        // See through a `type X = Result[T, E]` alias so a fn declared to return
        // the alias still records its Ok/Err payload types.
        if let Some(target) = self.resolve_type_alias(node) {
            return self.result_elem_go_types(target);
        }
        if let NodeKind::TypeNamed { path, args } = &node.kind {
            if path.segments.last().is_some_and(|s| s.name == "Result") {
                let ok = args
                    .first()
                    .map_or_else(|| "interface{}".to_string(), |a| self.type_to_go(a));
                let err = args
                    .get(1)
                    .map_or_else(|| "interface{}".to_string(), |a| self.type_to_go(a));
                return Some((ok, err));
            }
        }
        None
    }

    /// If `node` is a `List[T]` type expression, return the Go type of its
    /// element `T`; otherwise `None`. The read-only `List` built-ins
    /// `get`/`first`/`last` return `Optional[T]` over the list element, so a
    /// `match` on such a call must type-assert the `interface{}` payload to this
    /// element type. Reached structurally from the receiver's declared
    /// `List[T]` type (its element is unrecoverable from the runtime
    /// `[]interface{}` value alone).
    fn list_elem_go_type(&self, node: &AIRNode) -> Option<String> {
        if let NodeKind::TypeNamed { path, args } = &node.kind {
            if path.segments.last().is_some_and(|s| s.name == "List") {
                return args.first().map(|a| self.type_to_go(a));
            }
        }
        None
    }

    /// If `node` is a `Map[K, V]` type expression, return the Go types of its
    /// key and value `(K, V)`; otherwise `None`. The `Map` analogue of
    /// [`Self::list_elem_go_type`]: the built-in `Map` methods lower to inline
    /// closures over the concretely-typed receiver `map[K]V`, so a typed `let m:
    /// Map[K, V]` binding records these into [`Self::var_map_kv`]. A missing arg
    /// defaults to `interface{}`.
    fn map_kv_go_types(&self, node: &AIRNode) -> Option<(String, String)> {
        if let NodeKind::TypeNamed { path, args } = &node.kind {
            if path.segments.last().is_some_and(|s| s.name == "Map") {
                let k = args
                    .first()
                    .map_or_else(|| "interface{}".to_string(), |a| self.type_to_go(a));
                let v = args
                    .get(1)
                    .map_or_else(|| "interface{}".to_string(), |a| self.type_to_go(a));
                return Some((k, v));
            }
        }
        None
    }

    /// If `node` is a `Set[E]` type expression, return the Go type of its
    /// element `E`; otherwise `None`. The `Set` analogue of
    /// [`Self::list_elem_go_type`], recorded into [`Self::var_set_elem`].
    fn set_elem_go_type(&self, node: &AIRNode) -> Option<String> {
        if let NodeKind::TypeNamed { path, args } = &node.kind {
            if path.segments.last().is_some_and(|s| s.name == "Set") {
                return Some(
                    args.first()
                        .map_or_else(|| "interface{}".to_string(), |a| self.type_to_go(a)),
                );
            }
        }
        None
    }

    /// If `node` is an `Optional[T]` (or `T?`) type expression, return its inner
    /// `T` type node. The *node*-returning analogue of
    /// [`Self::optional_elem_go_type`]: lets the pattern recursion peel one
    /// Optional layer and keep descending a nested constructor pattern
    /// (`Some(Ok((a, b)))`) so a leaf tuple pattern lands on a concrete tuple
    /// type node. Sees through a `type X = Optional[T]` alias.
    fn optional_inner_type_node<'a>(&'a self, node: &'a AIRNode) -> Option<&'a AIRNode> {
        if let Some(target) = self.resolve_type_alias(node) {
            return self.optional_inner_type_node(target);
        }
        match &node.kind {
            NodeKind::TypeOptional { inner } => Some(inner),
            NodeKind::TypeNamed { path, args }
                if path.segments.last().is_some_and(|s| s.name == "Optional") =>
            {
                args.first()
            }
            _ => None,
        }
    }

    /// Clone `ret`'s function-type node when the declared return type is a
    /// function type (`Fn(Int) -> Int`), else `None`. Kept on
    /// [`Self::current_fn_ret_type_node`] so a lambda in tail-return position can
    /// take its param/return Go types from the declared function type. (Does not
    /// peel a `type` alias to a function type — a function returning such an
    /// alias is vanishingly rare in the v1 examples and the lambda still emits
    /// correctly via inference, just un-typed.)
    fn fn_type_ret_node(ret: Option<&AIRNode>) -> Option<AIRNode> {
        let node = ret?;
        if matches!(node.kind, NodeKind::TypeFunction { .. }) {
            Some(node.clone())
        } else {
            None
        }
    }

    /// The `(param_go_types, return_go_type)` of a `TypeFunction` node, each
    /// rendered as Go. Used to type a lambda emitted in return position from the
    /// enclosing function's declared `Fn(...) -> ...` return type. Returns `None`
    /// when `node` is not a function type.
    fn fn_type_go_signature(&self, node: &AIRNode) -> Option<(Vec<String>, String)> {
        if let NodeKind::TypeFunction { params, ret, .. } = &node.kind {
            let param_tys = params.iter().map(|p| self.type_to_go(p)).collect();
            return Some((param_tys, self.type_to_go(ret)));
        }
        None
    }

    /// When `tail` is a bare lambda being emitted in *tail-return position* and
    /// the enclosing function's declared return type is a function type
    /// (`fn compose(...) -> Fn(Int) -> Int { (x) => f(g(x)) }`), pin the lambda's
    /// param/return Go types from that declared type, so the emitted closure is
    /// `func(x int64) int64` rather than the inference fallback
    /// `func(x interface{}) interface{}` (not assignable to the declared return).
    /// Returns the saved `(expected_lambda_param_types, forced_lambda_ret)` so the
    /// caller restores them after the emit; a no-op (saved state unchanged) when
    /// the tail is not a lambda or the return type is not a function type.
    #[allow(clippy::type_complexity)]
    fn pin_return_lambda_types(&mut self, tail: &AIRNode) -> (Option<Vec<String>>, Option<String>) {
        let saved = (
            self.expected_lambda_param_types.clone(),
            self.forced_lambda_ret.clone(),
        );
        if !matches!(tail.kind, NodeKind::Lambda { .. }) {
            return saved;
        }
        if let Some(node) = &self.current_fn_ret_type_node {
            if let Some((param_tys, ret_ty)) = self.fn_type_go_signature(node) {
                self.expected_lambda_param_types = Some(param_tys);
                self.forced_lambda_ret = Some(ret_ty);
            }
        }
        saved
    }

    /// If `node` is a `Result[T, E]` type expression, return its `(T, E)` inner
    /// type nodes. The *node*-returning analogue of
    /// [`Self::result_elem_go_types`], used to peel a Result layer while
    /// descending a nested constructor pattern. Sees through a
    /// `type X = Result[T, E]` alias.
    fn result_inner_type_nodes<'a>(
        &'a self,
        node: &'a AIRNode,
    ) -> Option<(&'a AIRNode, Option<&'a AIRNode>)> {
        if let Some(target) = self.resolve_type_alias(node) {
            return self.result_inner_type_nodes(target);
        }
        if let NodeKind::TypeNamed { path, args } = &node.kind {
            if path.segments.last().is_some_and(|s| s.name == "Result") {
                return args.first().map(|ok| (ok, args.get(1)));
            }
        }
        None
    }

    /// The `(K, V)` Go types of a `Map` *value* expression used as the receiver
    /// of a built-in map method. Recovered from a declared `Map[K, V]`
    /// identifier (via [`Self::var_map_kv`]) or a homogeneously-typed map
    /// literal. `None` ⇒ the caller falls back to `interface{}` (never a wrong
    /// type).
    /// The Go type of a compose-desugar lambda's sole parameter.
    ///
    /// `f >> g` lowers (in shared AIR) to `(__compose_x) => g(f(__compose_x))`
    /// with an *untyped* `__compose_x`. The composed value's input type is the
    /// input type of the inner function `f`, so recover `f`'s first declared
    /// parameter type (via [`Self::fn_param_types`]) and render it as Go. Returns
    /// `None` for any non-compose lambda, or when `f`'s param type can't be
    /// resolved (then the param stays `interface{}`, never a wrong type).
    fn compose_lambda_param_go_type(&self, params: &[AIRNode], body: &AIRNode) -> Option<String> {
        // Exactly one param, a `BindPat` (the synthetic `__compose_x`).
        let [param] = params else {
            return None;
        };
        let NodeKind::Param {
            pattern, ty: None, ..
        } = &param.kind
        else {
            return None;
        };
        let NodeKind::BindPat { name, .. } = &pattern.kind else {
            return None;
        };
        if name.name != "__compose_x" {
            return None;
        }
        // Body is `g(f(__compose_x))`; reach the inner call `f(__compose_x)`.
        let NodeKind::Call {
            args: outer_args, ..
        } = &body.kind
        else {
            return None;
        };
        let inner = &outer_args.first()?.value;
        let NodeKind::Call { callee: f, .. } = &inner.kind else {
            return None;
        };
        self.compose_input_go_type(f)
    }

    /// The Go type of the *input* a composed callee `f` accepts — used to type a
    /// `>>`-compose lambda's synthetic `__compose_x` parameter.
    ///
    /// A named function (`Identifier`) yields its first declared parameter type.
    /// A *nested* compose (chained `f >> g >> h` desugars to a `(__compose_x) =>
    /// h(g(f(__compose_x)))` whose innermost callee `f` is itself a compose
    /// lambda) recurses through that lambda's own desugared shape to the
    /// innermost named function. Without the recursion an `f >> g >> h` chain's
    /// *outer* compose param fell back to `interface{}` on Go (only the innermost
    /// compose's param was typed), so passing `composeX` into the typed inner
    /// closure needed a type assertion Go rejected (Q-nested-compose-jstsgo, Go
    /// portion). Mirrors py's `emit_callee`/rust's `emit_callee_rs` parens
    /// strategy at the *typing* level.
    fn compose_input_go_type(&self, callee: &AIRNode) -> Option<String> {
        match &callee.kind {
            NodeKind::Identifier { name } => {
                let first_param = self.fn_param_types.get(&name.name)?.first()?.as_ref()?;
                Some(self.type_to_go(first_param))
            }
            // A nested compose lambda (`(__compose_x) => g(f(__compose_x))`): its
            // own input type is whatever its innermost composed callee `f`
            // accepts. Recurse through the same desugared shape.
            NodeKind::Lambda { params, body } => self.compose_lambda_param_go_type(params, body),
            _ => None,
        }
    }

    fn map_receiver_kv_go_types(&self, recv: &AIRNode) -> Option<(String, String)> {
        match &recv.kind {
            NodeKind::Identifier { name } => {
                self.var_map_kv.get(&go_value_ident(&name.name)).cloned()
            }
            NodeKind::MapLiteral { entries } => {
                let keys: Vec<&AIRNode> = entries.iter().map(|e| &e.key).collect();
                let vals: Vec<&AIRNode> = entries.iter().map(|e| &e.value).collect();
                match (
                    self.infer_homogeneous_elem_type_refs(&keys),
                    self.infer_homogeneous_elem_type_refs(&vals),
                ) {
                    (Some(k), Some(v)) => Some((k, v)),
                    _ => None,
                }
            }
            // A `self.field` map receiver inside an impl method: the field's
            // `Map[K, V]` types are recorded per record (mirrors the `List`
            // case in `list_receiver_elem_go_type`).
            NodeKind::FieldAccess { object, field } if matches!(&object.kind, NodeKind::Identifier { name } if name.name == "self") =>
            {
                let record = self.current_self_record.as_ref()?;
                self.record_field_map_kv
                    .get(record)
                    .and_then(|m| m.get(&field.name))
                    .cloned()
            }
            // A `value.field` map receiver where `value` is a variable of a known
            // record type (`report.by_category.get(k)` for `report: Report`,
            // `record Report { by_category: Map[String, Float] }`). The variable's
            // Go type names the record; the field's recorded `Map[K, V]` types
            // give the closure's `map[K]V` rather than the erased
            // `map[interface{}]interface{}` Go rejects against the concrete field.
            NodeKind::FieldAccess { object, field } => {
                let NodeKind::Identifier { name } = &object.kind else {
                    return None;
                };
                let obj_go_ty = self.var_go_type.get(&go_value_ident(&name.name))?;
                let record = Self::go_type_record_head(obj_go_ty);
                self.record_field_map_kv
                    .get(record)
                    .and_then(|m| m.get(&field.name))
                    .cloned()
            }
            _ => None,
        }
    }

    /// The Go element type of a `List` *value* expression, so an untyped binding
    /// to it (`let updated = items.map(..)`, `let evens = xs.filter(..)`) records
    /// its element type — letting a *chained* combinator on the binding
    /// (`updated.map((it) => it.title)`) type its closure param and a later use as
    /// a typed call argument keep `[]T` rather than erasing to `[]interface{}`.
    ///
    /// Handles a homogeneous list literal directly, and the closure-taking
    /// combinators `filter`/`map`/`flat_map` whose *receiver* element type is
    /// recoverable: `filter` preserves the element; `map` yields the closure's
    /// inferred return type (with the receiver element pinned as the closure
    /// param type); `flat_map` yields that return type's slice element. `None`
    /// when the element can't be recovered (the caller leaves the binding untyped
    /// — never a wrong type).
    fn value_list_elem_go_type(&mut self, value: &AIRNode) -> Option<String> {
        // A homogeneous list literal / typed `List[T]` identifier directly.
        if let Some(slice) = self.infer_go_expr_type(value) {
            if let Some(elem) = slice.strip_prefix("[]") {
                return Some(elem.to_string());
            }
        }
        // A builtin String-method call returning a concretely-typed list
        // (`s.split(..)` → `[]string`): the binding's element is known without
        // any combinator analysis (Q-go-split-combinator-typing — lets a `let
        // raw = s.split(..)` record `string` so a chain on the binding types).
        if let Some(elem) = Self::string_list_builtin_elem(value) {
            return Some(elem);
        }
        // A list combinator (`xs.map(..)`, `xs.filter(..)`) reaches codegen as the
        // desugared `Call(FieldAccess(xs, "map"), [cb])`, not a `MethodCall`;
        // recognise it through the shared desugar resolver.
        let NodeKind::Call { callee, args, .. } = &value.kind else {
            return None;
        };
        let (recv, method, rest) =
            crate::generator::desugared_list_functional_method(value, callee, args)?;
        // The cheap `&self` receiver resolver covers bindings/literals and the
        // element-preserving chained combinators; recurse through this *value*
        // resolver otherwise so a `map`/`flat_map` link in the chain (whose
        // element is its closure's return type) doesn't sever element recovery
        // for everything chained after it (`split(..).map(..).filter(..)`,
        // Q-go-split-combinator-typing).
        let recv_elem = self
            .list_receiver_elem_go_type(recv)
            .or_else(|| self.value_list_elem_go_type(recv))?;
        match method {
            "filter" => Some(recv_elem),
            "map" | "flat_map" => {
                let cb = rest.first()?;
                let NodeKind::Lambda { params, body } = &cb.value.kind else {
                    return None;
                };
                let saved = self.enter_param_go_types_with_expected(params, Some(&[recv_elem]));
                let ret = self.infer_block_tail_type(body);
                self.var_go_type = saved;
                let ret = ret?;
                if method == "flat_map" {
                    ret.strip_prefix("[]").map(str::to_string)
                } else {
                    Some(ret)
                }
            }
            _ => None,
        }
    }

    /// The element Go type of a `Set` *value* expression used as the receiver of
    /// a built-in set method. Recovered from a declared `Set[E]` identifier (via
    /// [`Self::var_set_elem`]) or a homogeneously-typed set literal. `None` ⇒
    /// `interface{}` fallback.
    fn set_receiver_elem_go_type(&self, recv: &AIRNode) -> Option<String> {
        match &recv.kind {
            NodeKind::Identifier { name } => {
                self.var_set_elem.get(&go_value_ident(&name.name)).cloned()
            }
            NodeKind::SetLiteral { elems } => self.infer_homogeneous_elem_type(elems),
            _ => None,
        }
    }

    /// Infer the `(K, V)` Go types of a `Map`-typed *value* expression — a map
    /// literal, a known `Map` identifier, or a `Map` built-in method that
    /// returns the receiver map (`set`/`delete`/`merge`/`filter`). Lets an
    /// untyped `let m2 = base.set(k, v)` propagate `base`'s key/value types onto
    /// `m2` so a subsequent `m2.get(k)` closure is well-typed. `None` ⇒
    /// `interface{}` fallback.
    fn value_map_kv_go_types(&self, value: &AIRNode) -> Option<(String, String)> {
        if let Some(kv) = self.map_receiver_kv_go_types(value) {
            return Some(kv);
        }
        if let NodeKind::Call { callee, args, .. } = &value.kind {
            if let Some((recv, method, _)) =
                crate::generator::desugared_map_method(value, callee, args)
            {
                if matches!(method, "set" | "delete" | "merge" | "filter") {
                    return self.value_map_kv_go_types(recv);
                }
            }
        }
        None
    }

    /// Infer the element Go type of a `Set`-typed *value* expression — a set
    /// literal, a known `Set` identifier, or a `Set` built-in returning the
    /// receiver set (`add`/`remove`/`union`/`intersection`/`difference`/
    /// `filter`/`map`). The `Set` analogue of [`Self::value_map_kv_go_types`].
    fn value_set_elem_go_type(&self, value: &AIRNode) -> Option<String> {
        if let Some(elem) = self.set_receiver_elem_go_type(value) {
            return Some(elem);
        }
        if let NodeKind::Call { callee, args, .. } = &value.kind {
            if let Some((recv, method, _)) =
                crate::generator::desugared_set_method(value, callee, args)
            {
                if matches!(
                    method,
                    "add" | "remove" | "union" | "intersection" | "difference" | "filter" | "map"
                ) {
                    return self.value_set_elem_go_type(recv);
                }
            }
        }
        None
    }

    /// Infer the concrete generic-record instantiation a *value* expression
    /// produces — `("ListIterator", ["int64"])` for `list_iter([1, 2, 3])` or
    /// `bag.iter()`. Resolved for: a call to a generic fn whose return type is a
    /// generic record ([`Self::infer_go_expr_type`]'s `Call` arm), and a method
    /// call (direct or the desugared `Call(FieldAccess(recv, m), [recv, ..])`
    /// shape) whose method has a recorded concrete record return
    /// ([`Self::method_ret_record_args`]). Used to record an untyped binding's
    /// record args so a later `match binding.next() { Some(x) => ... }` asserts
    /// the payload concretely. `None` when not structurally determinable.
    fn value_record_type_args(&self, value: &AIRNode) -> Option<(String, Vec<String>)> {
        // A method whose declared return is a concrete generic record.
        match &value.kind {
            NodeKind::MethodCall { method, .. } => {
                if let Some(args) = self.method_ret_record_args.get(&method.name) {
                    return Some(args.clone());
                }
            }
            NodeKind::Call { callee, args, .. } => {
                // The AIR also lowers `recv.m(rest)` to `Call(FieldAccess(recv,
                // m), [recv, ...])`.
                if let Some((_recv, method, _)) =
                    crate::generator::desugared_self_call(callee, args)
                {
                    if let Some(ra) = self.method_ret_record_args.get(&method.name) {
                        return Some(ra.clone());
                    }
                }
            }
            _ => {}
        }
        // A free generic-fn call resolving to a concrete record return
        // (`list_iter([...])` → `ListIterator[int64]`): parse the rendered type.
        let go_ty = self.infer_go_expr_type(value)?;
        let open = go_ty.find('[')?;
        if !go_ty.ends_with(']') {
            return None;
        }
        let base = go_ty[..open].to_string();
        if self.generic_decls.get(&base).is_none_or(|p| p.is_empty()) {
            return None;
        }
        let arg_str = &go_ty[open + 1..go_ty.len() - 1];
        let args = Self::split_top_level_commas(arg_str);
        if args.is_empty() {
            return None;
        }
        Some((base, args))
    }

    /// Infer the `(ok, err)` Go payload types a `Result`-typed *value* expression
    /// produces. The `Result` analogue of [`Self::value_map_kv_go_types`]:
    /// resolves a bare `Ok(v)` / `Err(e)` constructor's present arm from the
    /// payload expression (the absent arm stays `interface{}`), and a call to a
    /// function whose declared return is a `Result` via the existing
    /// [`Self::scrutinee_result_elems`] (fn-return / variable maps). Used to
    /// record an *untyped* binding's `(ok, err)` into [`Self::var_result_elem`]
    /// (`step1 := eval(...)`, with no `: Result[..]` annotation), so a later
    /// `match step1 { Ok(v) => ...; Err(e) => ... }` type-asserts the
    /// `interface{}` payload concretely rather than binding it bare (which leaves
    /// `v` as `interface{}` and fails a later use expecting the concrete type).
    fn value_result_elem_go_types(&self, value: &AIRNode) -> Option<(String, String)> {
        if let NodeKind::Call { callee, args, .. } = &value.kind {
            if let NodeKind::Identifier { name } = &callee.kind {
                match name.name.as_str() {
                    "Ok" => {
                        let ok = args
                            .first()
                            .and_then(|a| self.infer_go_expr_type(&a.value))
                            .unwrap_or_else(|| "interface{}".to_string());
                        return Some((ok, "interface{}".to_string()));
                    }
                    "Err" => {
                        let err = args
                            .first()
                            .and_then(|a| self.infer_go_expr_type(&a.value))
                            .unwrap_or_else(|| "interface{}".to_string());
                        return Some(("interface{}".to_string(), err));
                    }
                    _ => {}
                }
            }
        }
        self.scrutinee_result_elems(value)
    }

    /// For a `List[T]` / `Set[T]` / `Map[K, V]` type expression, the declared
    /// Go element types as `(elem_or_key, value)`: `List`/`Set` yield
    /// `(T, None)`; `Map` yields `(K, Some(V))`. A missing type arg defaults to
    /// `interface{}`. `None` for any non-collection type. Used to set
    /// [`Self::expected_collection_elem`] so a literal in a typed binding adopts
    /// the declared element type(s).
    /// If `node` is a *generic record* instantiation (`ListIter[Int]`), return
    /// its base name and the Go-rendered concrete type-args (`("ListIter",
    /// ["int64"])`). `None` for a non-record type, a non-generic record, or a
    /// record with no type-args. Used to record [`Self::var_record_type_args`]
    /// so a method-call scrutinee's generic `Optional[T]` payload can be resolved
    /// to the concrete instantiation at the call site.
    fn record_type_args(&self, node: &AIRNode) -> Option<(String, Vec<String>)> {
        let NodeKind::TypeNamed { path, args } = &node.kind else {
            return None;
        };
        if args.is_empty() {
            return None;
        }
        let base = path.segments.last().map(|s| s.name.clone())?;
        // Only generic records (those with a declared param list) qualify; this
        // keeps the map free of `List`/`Map`/etc. and other non-record applies.
        let params = self.generic_decls.get(&base)?;
        if params.is_empty() {
            return None;
        }
        let arg_strs: Vec<String> = args.iter().map(|a| self.type_to_go(a)).collect();
        Some((base, arg_strs))
    }

    fn collection_elem_go_types(&self, node: &AIRNode) -> Option<(String, Option<String>)> {
        let NodeKind::TypeNamed { path, args } = &node.kind else {
            return None;
        };
        let name = path.segments.last().map(|s| s.name.as_str())?;
        let arg = |i: usize| {
            args.get(i)
                .map_or_else(|| "interface{}".to_string(), |a| self.type_to_go(a))
        };
        match name {
            "List" | "Set" => Some((arg(0), None)),
            "Map" => Some((arg(0), Some(arg(1)))),
            _ => None,
        }
    }

    /// The Go element type a `for x in <iterable>` loop binds, when
    /// structurally recoverable:
    /// - an identifier whose declared `List[T]` element type is in
    ///   [`Self::var_list_elem`] (a typed `let` / parameter),
    /// - a list literal whose elements infer to one homogeneous Go type,
    /// - a range (`a..b` / `a..=b`), which yields `int64`.
    ///
    /// Returns `None` otherwise; the loop variable is then left out of the type
    /// scope and inference falls back to `interface{}` — never a wrong type.
    fn for_loop_elem_go_type(&self, iterable: &AIRNode) -> Option<String> {
        match &iterable.kind {
            NodeKind::Identifier { name } => {
                self.var_list_elem.get(&go_value_ident(&name.name)).cloned()
            }
            NodeKind::ListLiteral { elems } => self.infer_homogeneous_elem_type(elems),
            NodeKind::Range { .. } => Some("int64".to_string()),
            // `for p in s.split(",")`: the builtin lowers to `strings.Split`,
            // a concrete `[]string` (Q-go-split-combinator-typing).
            _ => Self::string_list_builtin_elem(iterable),
        }
    }

    /// The concrete Go type a builtin *String-method* call lowers to, keyed off
    /// the same checker receiver-kind annotation [`Self::try_emit_string_method`]
    /// dispatches on (so a user method named `trim` on a non-String receiver is
    /// never mistaken for the builtin). This table mirrors that emitter's
    /// lowerings exactly: `split` → `strings.Split` (`[]string`), the
    /// string-transforming methods → `string`, the length queries → `int64`, the
    /// predicates → `bool`, and the optional-returning lookups → the
    /// `__bockOption` runtime struct. Previously codegen had no return type for
    /// any of these, so a combinator chained onto `split()` — or a `.map((p) =>
    /// p.trim())` link feeding a chain — erased to `interface{}`, which Go
    /// rejects against the lowering's concrete types
    /// (Q-go-split-combinator-typing — the builtin-method sibling of the #256
    /// chained-combinator fix).
    fn string_builtin_return_go_type(
        node: &AIRNode,
        callee: &AIRNode,
        args: &[bock_air::AirArg],
    ) -> Option<String> {
        if crate::generator::primitive_recv_kind(node) != Some("String") {
            return None;
        }
        let (_, field, _) = crate::generator::desugared_self_call(callee, args)?;
        let ty = match field.name.as_str() {
            "to_upper" | "to_lower" | "trim" | "trim_start" | "trim_end" | "reverse" | "repeat"
            | "replace" | "slice" | "substring" | "to_string" | "display" => "string",
            "split" => "[]string",
            "len" | "length" | "count" | "byte_len" => "int64",
            "is_empty" | "contains" | "starts_with" | "ends_with" => "bool",
            "char_at" | "index_of" => "__bockOption",
            _ => return None,
        };
        Some(ty.to_string())
    }

    /// The Go element type of a builtin *String-method* call returning a
    /// `List` — today only `split`, whose lowering is a concrete `[]string`
    /// (Q-go-split-combinator-typing). The slice-element view of
    /// [`Self::string_builtin_return_go_type`] for the list-element resolvers.
    fn string_list_builtin_elem(recv: &AIRNode) -> Option<String> {
        let NodeKind::Call { callee, args, .. } = &recv.kind else {
            return None;
        };
        Self::string_builtin_return_go_type(recv, callee, args)?
            .strip_prefix("[]")
            .map(str::to_string)
    }

    /// The Go slice element type of a `List` *value* expression used as the
    /// receiver of a built-in list method (`get`/`concat`/…). The list-method
    /// closures take a `[]<elem>` parameter that must match the receiver's now
    /// concretely-typed slice. Recovered from a declared `List[T]` identifier
    /// (via [`Self::var_list_elem`]) or a homogeneously-typed list literal;
    /// `None` otherwise, in which case the receiver is `[]interface{}` and the
    /// `interface{}` element default matches.
    fn list_receiver_elem_go_type(&self, recv: &AIRNode) -> Option<String> {
        match &recv.kind {
            NodeKind::Identifier { name } => {
                let key = go_value_ident(&name.name);
                // A `List[T]` binding records its element type directly. Fall back
                // to the variable's full Go type (`var_go_type`) when the
                // dedicated list-element map has no entry — a typed *lambda
                // parameter* (`(data) => data.filter(..)` for a `Fn(List[T]) ->
                // ..` return type) is recorded only there as `[]T`, so peeling the
                // `[]` prefix recovers the element. Without this the chained
                // `.filter`/`.map` closure stayed `func(x interface{})` and its
                // `[]interface{}` result did not satisfy the typed `Fn` return.
                self.var_list_elem.get(&key).cloned().or_else(|| {
                    self.var_go_type
                        .get(&key)
                        .and_then(|t| t.strip_prefix("[]"))
                        .map(str::to_string)
                })
            }
            NodeKind::ListLiteral { elems } => self.infer_homogeneous_elem_type(elems),
            // A *chained* combinator whose receiver is itself a closure-taking
            // list method (`numbers.filter(..).map(..)`): the outer method's
            // receiver is the desugared `Call(FieldAccess(numbers, "filter"),
            // [numbers, cb])`. `filter`/`find` preserve the element type, so the
            // element is recoverable from the inner receiver without inferring a
            // closure return type (which would need `&mut self`). This keeps a
            // `.filter(..).map(..)` chain's outer `.map` closure typed
            // `func(n int64)` and its result `[]int64` rather than the erased
            // `interface{}`/`[]interface{}` that Go rejects. `map`/`flat_map`
            // receivers (whose element is the closure's return type) are recovered
            // by the `&mut self` fallback in `try_emit_list_functional_method`.
            NodeKind::Call { callee, args, .. } => {
                if let Some((inner_recv, method, _)) =
                    crate::generator::desugared_list_functional_method(recv, callee, args)
                {
                    match method {
                        "filter" | "find" => self.list_receiver_elem_go_type(inner_recv),
                        _ => None,
                    }
                } else {
                    // Not a list combinator: a builtin String-method call that
                    // returns a concretely-typed list (`s.split(..)` →
                    // `strings.Split` → `[]string`) still carries a known
                    // element (Q-go-split-combinator-typing).
                    Self::string_list_builtin_elem(recv)
                }
            }
            // A `self.field` list receiver inside an impl method (`self.xs.get(i)`
            // in `record ListIter[T] { xs: List[T] }`): the field's `List[...]`
            // element type is recorded per record. `T` is in scope on the
            // method receiver, so the closure correctly takes `[]T`.
            NodeKind::FieldAccess { object, field } if matches!(&object.kind, NodeKind::Identifier { name } if name.name == "self") =>
            {
                let record = self.current_self_record.as_ref()?;
                self.record_field_list_elem
                    .get(record)
                    .and_then(|m| m.get(&field.name))
                    .cloned()
            }
            // A `value.field` list receiver where `value` is a variable of a known
            // record type (`b.items.get(i)` for `b: Box[T]`, `record Box[T] {
            // items: List[T] }`). The variable's Go type (`Box[T]`) names the
            // record; the field's recorded `List[...]` element type (`T`, in scope
            // as the enclosing generic fn's type param) gives the closure's `[]T`
            // element rather than the `[]interface{}` default — which a `[]T`
            // field-access argument does not satisfy under Go's type rules. (GAP-A:
            // a generic free fn reading `b.items.get(i)` previously emitted the
            // `.get` closure with a `[]interface{}` parameter and bound the `Some`
            // payload as `interface{}`, both rejected against `[]T`/`T`.)
            NodeKind::FieldAccess { object, field } => {
                let NodeKind::Identifier { name } = &object.kind else {
                    return None;
                };
                let obj_go_ty = self.var_go_type.get(&go_value_ident(&name.name))?;
                let record = Self::go_type_record_head(obj_go_ty);
                self.record_field_list_elem
                    .get(record)
                    .and_then(|m| m.get(&field.name))
                    .cloned()
            }
            _ => None,
        }
    }

    /// The record/type head of a Go type rendering: the identifier before any
    /// generic `[...]` arg list (`Box[T]` → `Box`, `Box[int64]` → `Box`, `Box` →
    /// `Box`). Used to key [`Self::record_field_list_elem`] from a variable's
    /// recorded Go type when resolving a `value.field` list receiver.
    fn go_type_record_head(go_ty: &str) -> &str {
        go_ty.split('[').next().unwrap_or(go_ty).trim()
    }

    /// Parse the per-field Go types out of a tuple struct rendering
    /// (`struct{ Field0 int64; Field1 string }` → `["int64", "string"]`). The
    /// inverse of `type_to_go`'s `TypeTuple` arm. Returns an empty vec for any
    /// non-tuple-struct string. Used to pin a tuple literal's field types from a
    /// declared tuple return/binding type when element inference falls short.
    fn parse_tuple_struct_field_types(go_ty: &str) -> Vec<String> {
        let inner = match go_ty
            .trim()
            .strip_prefix("struct{")
            .and_then(|s| s.strip_suffix('}'))
        {
            Some(s) => s.trim(),
            None => return Vec::new(),
        };
        let mut out = Vec::new();
        for field in inner.split(';') {
            let field = field.trim();
            if field.is_empty() {
                continue;
            }
            // Each field is `Field<N> <ty>`; the type is everything after the
            // first whitespace-separated token.
            match field.split_once(char::is_whitespace) {
                Some((name, ty)) if name.starts_with("Field") => out.push(ty.trim().to_string()),
                _ => return Vec::new(),
            }
        }
        out
    }

    /// True when `node` is (or contains, in operand position) an identifier
    /// whose Go type is not in `scope` — i.e. an `interface{}`-typed value an
    /// arithmetic operation cannot soundly operate on. Used to keep arithmetic
    /// type-inference conservative (untyped lambda params stay `interface{}`).
    fn has_unresolved_operand(node: &AIRNode, scope: &HashMap<String, String>) -> bool {
        match &node.kind {
            NodeKind::Identifier { name } => !scope.contains_key(&go_value_ident(&name.name)),
            NodeKind::UnaryOp { operand, .. } => Self::has_unresolved_operand(operand, scope),
            NodeKind::BinaryOp { left, right, .. } => {
                Self::has_unresolved_operand(left, scope)
                    || Self::has_unresolved_operand(right, scope)
            }
            _ => false,
        }
    }

    /// Best-effort structural inference of an expression's Go type. Reaches the
    /// cases needed to (a) instantiate a generic struct construction
    /// (`Box[int64]{...}`) and (b) give a lambda a concrete return type rather
    /// than `interface{}`. Handles literals, in-scope identifiers (via
    /// [`Self::var_go_type`]), arithmetic/comparison binary ops, and unary ops.
    /// Returns `None` when the type can't be determined structurally — callers
    /// fall back to `any`/`interface{}`, never a wrong type.
    /// Structurally unify a generic-param *pattern* go-type (a `type_to_go`
    /// rendering that still names the type params, e.g. `ListIterator[T]` or
    /// `[]T`) against a *concrete* go-type string, recording each param's
    /// concrete binding into `bindings`. A bare pattern that is exactly a
    /// generic-param name binds that param to the whole concrete string;
    /// otherwise the two must share a structural skeleton (same brackets/commas
    /// in the same places) and the unification recurses into the differing
    /// segments. Conservative: a structural mismatch simply records nothing
    /// (the caller then leaves the lambda param untyped — never wrong, only
    /// loose). Only the `[`/`]`/`,` skeleton is parsed; this covers the generic
    /// container / iterator shapes the combinators use.
    fn unify_go_pattern(
        pattern: &str,
        concrete: &str,
        gp_names: &[String],
        bindings: &mut HashMap<String, String>,
    ) {
        let pat = pattern.trim();
        let con = concrete.trim();
        // A bare param name binds to the entire concrete type.
        if gp_names.iter().any(|g| g == pat) {
            bindings
                .entry(pat.to_string())
                .or_insert_with(|| con.to_string());
            return;
        }
        // Split each into (head, bracketed-args) on the first top-level `[`.
        let split = |s: &str| -> Option<(String, String)> {
            let open = s.find('[')?;
            if !s.ends_with(']') {
                return None;
            }
            Some((s[..open].to_string(), s[open + 1..s.len() - 1].to_string()))
        };
        // A slice prefix `[]elem` — split into the `[]` marker and the element.
        if let (Some(pe), Some(ce)) = (pat.strip_prefix("[]"), con.strip_prefix("[]")) {
            Self::unify_go_pattern(pe, ce, gp_names, bindings);
            return;
        }
        match (split(pat), split(con)) {
            (Some((ph, pa)), Some((ch, ca))) if ph == ch => {
                let p_args = Self::split_top_level_commas(&pa);
                let c_args = Self::split_top_level_commas(&ca);
                if p_args.len() == c_args.len() {
                    for (pp, cc) in p_args.iter().zip(c_args.iter()) {
                        Self::unify_go_pattern(pp, cc, gp_names, bindings);
                    }
                }
            }
            _ => {}
        }
    }

    /// Split a go-type-arg list on top-level commas (commas not nested inside a
    /// `[...]`). `int64, []string` → `["int64", "[]string"]`.
    fn split_top_level_commas(s: &str) -> Vec<String> {
        let mut out = Vec::new();
        let mut depth = 0i32;
        let mut start = 0usize;
        for (i, ch) in s.char_indices() {
            match ch {
                '[' | '(' => depth += 1,
                ']' | ')' => depth -= 1,
                ',' if depth == 0 => {
                    out.push(s[start..i].trim().to_string());
                    start = i + 1;
                }
                _ => {}
            }
        }
        let last = s[start..].trim();
        if !last.is_empty() {
            out.push(last.to_string());
        }
        out
    }

    /// Bind a generic fn's type params to concrete go-types from its call's
    /// *non-lambda* arguments (a lambda argument is what we are specialising, so
    /// it can't drive the binding). Each argument whose Go type infers is
    /// unified against the matching declared param type. Returns the
    /// `param-name → go-type` bindings discovered.
    fn bind_fn_type_params(
        &self,
        gp_names: &[String],
        param_tys: &[Option<AIRNode>],
        args: &[bock_air::AirArg],
    ) -> HashMap<String, String> {
        let mut bindings: HashMap<String, String> = HashMap::new();
        for (i, arg) in args.iter().enumerate() {
            if matches!(arg.value.kind, NodeKind::Lambda { .. }) {
                continue;
            }
            let Some(pty) = param_tys.get(i).and_then(|p| p.as_ref()) else {
                continue;
            };
            let Some(arg_go) = self.infer_go_expr_type(&arg.value) else {
                continue;
            };
            let pattern = self.type_to_go(pty);
            Self::unify_go_pattern(&pattern, &arg_go, gp_names, &mut bindings);
        }
        bindings
    }

    /// Substitute the `param-name → go-type` bindings into a `Fn(...)` parameter
    /// type, returning the concrete Go parameter types (`[int64]` for
    /// `Fn(T) -> Bool` with `T → int64`). `None` when the param type is not a
    /// function type or a needed binding is missing (caller leaves the lambda
    /// param untyped).
    fn specialise_lambda_param_types(
        &self,
        fn_param_ty: &AIRNode,
        gp_names: &[String],
        bindings: &HashMap<String, String>,
    ) -> Option<Vec<String>> {
        // A callee param declared via a `type` alias to a function type
        // (`type Predicate = Fn(Int) -> Bool`) is a `TypeNamed`; see through it
        // to the underlying `TypeFunction` so the lambda argument still gets its
        // param types (`func(x int64) bool`, not the erased `interface{}`).
        if let Some(target) = self.resolve_type_alias(fn_param_ty) {
            return self.specialise_lambda_param_types(target, gp_names, bindings);
        }
        let NodeKind::TypeFunction { params, .. } = &fn_param_ty.kind else {
            return None;
        };
        let mut out = Vec::with_capacity(params.len());
        for p in params {
            let rendered = self.type_to_go(p);
            // Substitute each bound param name token-for-token. The rendered form
            // names params verbatim (`T`, `[]T`), so a binding maps them.
            let resolved = if let Some(b) = bindings.get(rendered.trim()) {
                b.clone()
            } else {
                let mut r = rendered.clone();
                for g in gp_names {
                    if let Some(b) = bindings.get(g) {
                        r = Self::replace_type_token(&r, g, b);
                    }
                }
                // If any generic param token remains unbound, give up (untyped).
                if gp_names.iter().any(|g| Self::contains_type_token(&r, g)) {
                    return None;
                }
                r
            };
            out.push(resolved);
        }
        Some(out)
    }

    /// Replace whole-identifier occurrences of `token` in a go-type string with
    /// `repl` (so `T` in `[]T` becomes the binding, without clobbering a `T`
    /// inside a longer identifier like `Tree`).
    fn replace_type_token(s: &str, token: &str, repl: &str) -> String {
        let bytes = s.as_bytes();
        let mut out = String::with_capacity(s.len());
        let mut i = 0;
        while i < s.len() {
            if s[i..].starts_with(token) {
                let before_ok = i == 0 || !Self::is_ident_byte(bytes[i - 1]);
                let after_idx = i + token.len();
                let after_ok = after_idx >= s.len() || !Self::is_ident_byte(bytes[after_idx]);
                if before_ok && after_ok {
                    out.push_str(repl);
                    i = after_idx;
                    continue;
                }
            }
            // Push one char (handle UTF-8 boundaries safely).
            let ch = s[i..].chars().next().unwrap_or(' ');
            out.push(ch);
            i += ch.len_utf8();
        }
        out
    }

    /// True when `s` contains `token` as a whole identifier (used to detect an
    /// unbound generic param remaining after substitution).
    fn contains_type_token(s: &str, token: &str) -> bool {
        Self::replace_type_token(s, token, "\0") != *s
    }

    fn is_ident_byte(b: u8) -> bool {
        b.is_ascii_alphanumeric() || b == b'_'
    }

    /// The Go type-name a `RecordConstruct` lowers its struct literal to: the
    /// record/struct-variant name (`Item`, or `ShapeCircle` for a struct-variant
    /// construction). Mirrors the `type_name` computation in the `RecordConstruct`
    /// emission so [`Self::infer_go_expr_type`] can type a list literal of
    /// record-constructs (`[Item{...}, Item{...}]` → `[]Item`) instead of erasing
    /// it to `[]interface{}` (the GAP-A defect: `infer_go_expr_type` had no
    /// `RecordConstruct` arm, so the homogeneous-element inference failed and the
    /// `Box[T] { items: List[T] }` field literal became `[]interface{}{…}`, which
    /// `go build` rejects against the struct's `[]Item` field).
    fn record_construct_go_type_name(&self, path: &bock_ast::TypePath) -> String {
        if let Some(info) = self.user_variant_for_path(path) {
            let variant = path.segments.last().map_or("", |s| s.name.as_str());
            format!("{}{variant}", info.enum_name)
        } else {
            path.segments
                .iter()
                .map(|s| s.name.as_str())
                .collect::<Vec<_>>()
                .join(".")
        }
    }

    /// Build a `param → concrete Go type` substitution for a record construction
    /// from the record's declared generic-param names and the resolved type-arg
    /// suffix (`"[Key]"`, `"[T]"`, `"[any]"`). A param with no positional arg
    /// (malformed/empty suffix) is omitted. Used to specialise a `List[T]` field
    /// literal's element type at the construction site.
    fn record_param_substitution(
        &self,
        record_name: &str,
        type_args: &str,
    ) -> HashMap<String, String> {
        let mut subst = HashMap::new();
        let Some(params) = self.record_generic_param_names.get(record_name) else {
            return subst;
        };
        let args = Self::split_type_arg_suffix(type_args);
        for (param, arg) in params.iter().zip(args.iter()) {
            subst.insert(param.clone(), arg.clone());
        }
        subst
    }

    /// Split a Go type-argument suffix (`"[A, B[C]]"`) into its top-level args
    /// (`["A", "B[C]"]`), respecting bracket nesting so a nested instantiation is
    /// not split at its inner comma. Returns `[]` for an empty/absent suffix.
    fn split_type_arg_suffix(suffix: &str) -> Vec<String> {
        let inner = suffix
            .strip_prefix('[')
            .and_then(|s| s.strip_suffix(']'))
            .unwrap_or("");
        let mut args = Vec::new();
        let mut depth = 0usize;
        let mut cur = String::new();
        for ch in inner.chars() {
            match ch {
                '[' => {
                    depth += 1;
                    cur.push(ch);
                }
                ']' => {
                    depth = depth.saturating_sub(1);
                    cur.push(ch);
                }
                ',' if depth == 0 => {
                    args.push(cur.trim().to_string());
                    cur.clear();
                }
                _ => cur.push(ch),
            }
        }
        if !cur.trim().is_empty() {
            args.push(cur.trim().to_string());
        }
        args
    }

    /// Substitute whole-token generic-param occurrences in a Go type string
    /// using `subst` (`"T"` with `{T: "Key"}` → `"Key"`; `"[]T"` is handled by
    /// the caller, which passes the bare element type). Only an exact match is
    /// substituted — composite element types are rare for record list fields, so
    /// this keeps the rewrite token-precise rather than risking a substring hit.
    fn apply_type_subst(elem: &str, subst: &HashMap<String, String>) -> String {
        subst.get(elem).cloned().unwrap_or_else(|| elem.to_string())
    }

    /// Pre-scan a block's statements for declare-only temps (from the shared
    /// value-CF hoist) and record the Go type each `var __bock_cf_N T` needs.
    /// A declare-only `let` is always immediately followed by its relocated
    /// control-flow statement, whose branch values (`temp = <value>` assignments)
    /// determine the temp's type; infer it via [`Self::infer_assigned_temp_type`].
    /// Idempotent — re-records on each block entry, scoped to the temps it sees.
    fn seed_decl_only_types(&mut self, stmts: &[AIRNode]) {
        for (i, s) in stmts.iter().enumerate() {
            let NodeKind::LetBinding { pattern, .. } = &s.kind else {
                continue;
            };
            if !s.metadata.contains_key(crate::generator::DECL_ONLY_META) {
                continue;
            }
            let name = self.pattern_to_go_binding(pattern);
            // The relocated CF is the next statement; its value arms were
            // rewritten to `name = <value>` assignments, so infer the temp's Go
            // type from the assigned values. Falls back to `interface{}` (still
            // valid Go) when no assignment's value type is determinable.
            if let Some(next) = stmts.get(i + 1) {
                if let Some(ty) = self.infer_assigned_temp_type(next, &name) {
                    self.decl_only_types.insert(name, ty);
                }
            }
        }
    }

    /// Infer the common Go type assigned to temp `name` anywhere within `node`
    /// (the relocated control-flow statement of a value-CF hoist). Scans every
    /// `name = <value>` assignment and unifies their value types; returns `None`
    /// when they disagree or none is determinable. Does not descend into nested
    /// functions/lambdas.
    fn infer_assigned_temp_type(&self, node: &AIRNode, name: &str) -> Option<String> {
        fn collect<'a>(node: &'a AIRNode, name: &str, out: &mut Vec<&'a AIRNode>) {
            match &node.kind {
                NodeKind::Assign { target, value, .. } => {
                    if matches!(&target.kind, NodeKind::Identifier { name: n } if go_value_ident(&n.name) == name)
                    {
                        out.push(value);
                    }
                }
                NodeKind::FnDecl { .. } | NodeKind::Lambda { .. } => {}
                NodeKind::Block { stmts, tail } => {
                    for s in stmts {
                        collect(s, name, out);
                    }
                    if let Some(t) = tail {
                        collect(t, name, out);
                    }
                }
                NodeKind::If {
                    then_block,
                    else_block,
                    ..
                } => {
                    collect(then_block, name, out);
                    if let Some(e) = else_block {
                        collect(e, name, out);
                    }
                }
                NodeKind::Match { arms, .. } => {
                    for arm in arms {
                        if let NodeKind::MatchArm { body, .. } = &arm.kind {
                            collect(body, name, out);
                        }
                    }
                }
                NodeKind::Loop { body } | NodeKind::While { body, .. } => {
                    collect(body, name, out);
                }
                _ => {}
            }
        }
        let mut values = Vec::new();
        collect(node, name, &mut values);
        // Unify only the values whose Go type is *determinable*. An assignment
        // whose value can't be typed here (e.g. a pattern-bound `v` not yet in
        // `var_go_type`) does not constrain — mirroring how `infer_branchy_expr_type`
        // skips value-less arms. This recovers `int64` from `bockCf0 = (0-1)` even
        // when a sibling `bockCf0 = v` is opaque, rather than collapsing to the
        // unassignable `interface{}`. If the determinable ones disagree, give up.
        let mut common: Option<String> = None;
        for v in values {
            let Some(ty) = self.infer_block_tail_type(v) else {
                continue;
            };
            match &common {
                Some(c) if *c != ty => return None,
                Some(_) => {}
                None => common = Some(ty),
            }
        }
        common
    }

    /// Infer the Go value type produced by an `if`/`match` expression by
    /// inferring the type of each branch/arm's *tail* (value) and requiring them
    /// to agree. Used to type an untyped `let m = if (..) { Text } else { Image }`
    /// binding's IIFE: the inferred enum (`MessageType`) becomes the IIFE return
    /// type so a variant value is assignable, rather than the `interface{}` /
    /// enclosing-fn-return fallback. Returns `None` when any branch's type can't
    /// be inferred or the branches disagree (the caller then leaves the binding
    /// untyped, preserving the prior behavior — never a wrong type). Branches
    /// that *only* early-return (no value tail, e.g. a `return Err(..)` arm) are
    /// skipped: they exit the enclosing function rather than contributing a value.
    fn infer_branchy_expr_type(&self, node: &AIRNode) -> Option<String> {
        match &node.kind {
            NodeKind::If {
                then_block,
                else_block,
                ..
            } => {
                let then_ty = self.infer_block_tail_type(then_block);
                let else_ty = else_block
                    .as_deref()
                    .and_then(|e| self.infer_block_tail_type(e));
                match (then_ty, else_ty) {
                    (Some(a), Some(b)) if a == b => Some(a),
                    // One branch only early-returns (no value) — adopt the other.
                    (Some(a), None) => Some(a),
                    (None, Some(b)) => Some(b),
                    _ => None,
                }
            }
            NodeKind::Match { arms, .. } => {
                let mut common: Option<String> = None;
                for arm in arms {
                    let NodeKind::MatchArm { body, .. } = &arm.kind else {
                        continue;
                    };
                    let Some(ty) = self.infer_block_tail_type(body) else {
                        // A value-less arm (early-return) does not constrain.
                        continue;
                    };
                    match &common {
                        Some(c) if *c != ty => return None,
                        Some(_) => {}
                        None => common = Some(ty),
                    }
                }
                common
            }
            _ => None,
        }
    }

    /// Infer the Go type of a block's value tail: for a `Block` the tail
    /// expression's type (a value-producing tail only — a statement tail like an
    /// early `return` yields `None`, as the block contributes no value), and for
    /// a bare expression body the expression's type. A nested `if`/`match` tail
    /// recurses through [`Self::infer_branchy_expr_type`]. `None` when no value
    /// type is determinable.
    fn infer_block_tail_type(&self, node: &AIRNode) -> Option<String> {
        match &node.kind {
            NodeKind::Block { tail, .. } => {
                let t = tail.as_deref()?;
                if crate::generator::node_is_statement(t) {
                    return None;
                }
                self.infer_block_tail_type(t)
            }
            NodeKind::If { .. } | NodeKind::Match { .. } => self.infer_branchy_expr_type(node),
            _ => self.infer_go_expr_type(node),
        }
    }

    fn infer_go_expr_type(&self, node: &AIRNode) -> Option<String> {
        match &node.kind {
            NodeKind::Literal { lit } => match lit {
                Literal::Int(_) => Some("int64".to_string()),
                Literal::Float(_) => Some("float64".to_string()),
                Literal::Bool(_) => Some("bool".to_string()),
                Literal::String(_) => Some("string".to_string()),
                Literal::Char(_) => Some("rune".to_string()),
                Literal::Unit => None,
            },
            NodeKind::Identifier { name } => {
                // A bare reference to a *unit* user-enum variant (`Text`,
                // `HealthCheck`) types to its owning sealed-interface enum
                // (`MessageType`, `Route`), so an untyped `let t = Text` — or an
                // `if`/`match` whose arms yield such variants — infers the enum
                // type rather than collapsing to `interface{}`/the enclosing fn's
                // return type. Bound locals/params still win (a variable shadowing
                // is impossible — variant names are PascalCase, value idents are
                // camelCase — but check the var map first for symmetry).
                if let Some(t) = self.var_go_type.get(&go_value_ident(&name.name)) {
                    return Some(t.clone());
                }
                // Bare `None` lowers to the runtime `__bockOption` (the nullary
                // Optional constructor); type it so a value-CF arm yielding `None`
                // unifies with a sibling `Some(x)` arm (both `__bockOption`).
                if name.name == "None" {
                    return Some("__bockOption".to_string());
                }
                self.user_variant_for_name(&name.name)
                    .map(|info| info.enum_name.clone())
            }
            NodeKind::Interpolation { .. } => Some("string".to_string()),
            NodeKind::UnaryOp { op, operand } => match op {
                UnaryOp::Not => Some("bool".to_string()),
                UnaryOp::Neg | UnaryOp::BitNot => self.infer_go_expr_type(operand),
            },
            NodeKind::BinaryOp { op, left, right } => match op {
                BinOp::Eq
                | BinOp::Ne
                | BinOp::Lt
                | BinOp::Le
                | BinOp::Gt
                | BinOp::Ge
                | BinOp::And
                | BinOp::Or
                | BinOp::Is => Some("bool".to_string()),
                BinOp::Add
                | BinOp::Sub
                | BinOp::Mul
                | BinOp::Div
                | BinOp::Rem
                | BinOp::Pow
                | BinOp::BitAnd
                | BinOp::BitOr
                | BinOp::BitXor => {
                    // An arithmetic op is only soundly typed when neither operand
                    // is an *unresolved* identifier: a `func(x interface{}) ...`
                    // body of `x * 2` would not type-check in Go regardless of
                    // the literal, so leave the return type as `interface{}`
                    // rather than inferring a type the operation can't satisfy.
                    if Self::has_unresolved_operand(left, &self.var_go_type)
                        || Self::has_unresolved_operand(right, &self.var_go_type)
                    {
                        return None;
                    }
                    self.infer_go_expr_type(left)
                        .or_else(|| self.infer_go_expr_type(right))
                }
                BinOp::Compose => None,
            },
            // Collection literals so a nested collection (`[[1], [2]]`,
            // `{"k": [1, 2]}`) types its element concretely. A literal whose
            // elements infer to a single homogeneous Go type yields that
            // container type; otherwise `None` (callers fall back to
            // `interface{}`, never a wrong type).
            NodeKind::ListLiteral { elems } => self
                .infer_homogeneous_elem_type(elems)
                .map(|e| format!("[]{e}")),
            NodeKind::SetLiteral { elems } => self
                .infer_homogeneous_elem_type(elems)
                .map(|e| format!("map[{e}]struct{{}}")),
            NodeKind::MapLiteral { entries } => {
                let keys: Vec<&AIRNode> = entries.iter().map(|e| &e.key).collect();
                let vals: Vec<&AIRNode> = entries.iter().map(|e| &e.value).collect();
                match (
                    self.infer_homogeneous_elem_type_refs(&keys),
                    self.infer_homogeneous_elem_type_refs(&vals),
                ) {
                    (Some(k), Some(v)) => Some(format!("map[{k}]{v}")),
                    _ => None,
                }
            }
            // A record/struct-variant construction (`Item { id: 1 }`,
            // `Box[Item] { items: … }`) types to its Go struct name plus the
            // explicit type-arg suffix the emission would write (`Item`, or
            // `Box[int64]` when a param is recoverable from a directly-typed
            // field). This lets a list literal of record-constructs
            // (`[Item{…}, Item{…}]`) infer the homogeneous element type `Item` so
            // the `Box[T] { items: List[T] }` field literal emits `[]Item{…}`
            // rather than the erased `[]interface{}{…}` (GAP-A). Type args are
            // inferred from field values only (the `current_expected_type` used by
            // `expected_construct_type_args` names the *outer* binding, not the
            // per-element record); a generic param not directly typed by a field
            // falls back to `any`, matching the emission's loose-but-valid form.
            NodeKind::RecordConstruct { path, fields, .. } => {
                // A struct-payload variant construction (`GetUser { id: id }`)
                // types to its owning sealed-interface enum (`Route`), not the
                // variant struct (`RouteGetUser`): the value is boxed into the
                // interface at its use site, so an untyped binding / `if`-`else`
                // branch infers the assignable enum type.
                if let Some(info) = self.user_variant_for_path(path) {
                    return Some(info.enum_name.clone());
                }
                let type_name = self.record_construct_go_type_name(path);
                let type_args = self.infer_construct_type_args(&type_name, fields);
                Some(format!("{type_name}{type_args}"))
            }
            // A call to a known generic fn (`list_iter([]int64{...})`) resolves
            // to its return type with the type params bound from the arguments
            // (`ListIterator[int64]`), so a downstream call (`filter(it, ..)`)
            // can in turn bind its own params and specialise its lambda arg.
            NodeKind::Call { callee, args, .. } => {
                // A builtin String-method call types to its lowering's concrete
                // Go type (`p.trim()` → `string`, `s.split(..)` → `[]string`),
                // so a closure body / binding built from one infers concretely
                // (Q-go-split-combinator-typing). Checked before the user-method
                // return lookup: the receiver-kind annotation proves this is the
                // builtin, not a same-named user method.
                if let Some(t) = Self::string_builtin_return_go_type(node, callee, args) {
                    return Some(t);
                }
                let name = match &callee.kind {
                    NodeKind::Identifier { name } => name,
                    // A method call `recv.method(...)` lowers to a `Call` whose
                    // callee is a `FieldAccess`. Resolve it to the method's
                    // recorded Go return type (keyed by method name; the pre-scan
                    // omits names shared by methods with disagreeing returns, so a
                    // present entry is unambiguous). This types a
                    // `.map((p) => p.stock_value())` closure body to `float64`,
                    // sizing the result slice as `[]float64` rather than the
                    // erased `[]interface{}` whose elements a later `fold`'s
                    // `acc + v` cannot add.
                    NodeKind::FieldAccess { field, .. } => {
                        return self.method_return_go_types.get(&field.name).cloned();
                    }
                    _ => return None,
                };
                // The Optional/Result constructors lower to the runtime tagged
                // structs `__bockOption` / `__bockResult`, so a value-position
                // `if`/`match`/`loop` whose arms yield `Some(x)`/`None` (or `Ok`/
                // `Err`) infers that runtime type — letting the shared value-CF
                // hoist's `var __bock_cf_N __bockOption` be assignable to an
                // `Optional[T]`-returning fn (else it falls back to `interface{}`,
                // which Go rejects assigning into the typed return).
                match name.name.as_str() {
                    "Some" | "None" => return Some("__bockOption".to_string()),
                    "Ok" | "Err" => return Some("__bockResult".to_string()),
                    _ => {}
                }
                // A call to a local variable bound to a lambda (`clip_fn(x)`)
                // resolves to that lambda's recorded return type.
                if let Some(r) = self.var_lambda_ret.get(&go_value_ident(&name.name)) {
                    return Some(r.clone());
                }
                // A tuple-payload variant construction (`Circle(10)`) types to its
                // owning sealed-interface enum (`Shape`), mirroring the unit /
                // struct-payload variant cases — the variant struct is boxed into
                // the interface at its use site.
                if let Some(info) = self.user_variant_for_name(&name.name) {
                    return Some(info.enum_name.clone());
                }
                // A non-generic fn (`key`) resolves directly to its recorded Go
                // return type — no type-param binding needed. This is what types a
                // `[key(3), key(1)]` literal as `[]Key`.
                let Some((gp_names, param_tys, ret_ty)) = self.fn_signatures.get(&name.name) else {
                    return self.fn_return_go_types.get(&name.name).cloned();
                };
                let ret = ret_ty.as_ref()?;
                let bindings = self.bind_fn_type_params(gp_names, param_tys, args);
                let mut rendered = self.type_to_go(ret);
                for g in gp_names {
                    if let Some(b) = bindings.get(g) {
                        rendered = Self::replace_type_token(&rendered, g, b);
                    }
                }
                // Only return a fully-resolved type (no generic param left).
                if gp_names
                    .iter()
                    .any(|g| Self::contains_type_token(&rendered, g))
                {
                    return None;
                }
                Some(rendered)
            }
            // A bare `obj.field` access where `obj` is a variable of a known
            // record type resolves to the field's recorded Go type. This types a
            // `.map((b) => b.id)` closure body to `int64` (for `b: Block`,
            // `record Block { id: Int }`), sizing the `map` result slice as
            // `[]int64` rather than the erased `[]interface{}` Go rejects against
            // a declared `[]int64` return. `self.field` resolves through the
            // current-impl record; a non-identifier object (a chained access) is
            // left unresolved (conservative — `interface{}`, never wrong).
            NodeKind::FieldAccess { object, field } => {
                let record = match &object.kind {
                    NodeKind::Identifier { name } if name.name == "self" => {
                        self.current_self_record.clone()?
                    }
                    NodeKind::Identifier { name } => {
                        let obj_go_ty = self.var_go_type.get(&go_value_ident(&name.name))?;
                        Self::go_type_record_head(obj_go_ty).to_string()
                    }
                    _ => return None,
                };
                self.record_field_go_type
                    .get(&record)
                    .and_then(|m| m.get(&field.name))
                    .cloned()
            }
            // A `recv.method()` call resolves to the method's recorded Go return
            // type. Keyed by method name only; the pre-scan poisons (omits) any
            // name shared by methods with disagreeing return types, so a present
            // entry is unambiguous. Lets a `.map((p) => p.stock_value())` closure
            // body type to `float64`, sizing the result slice as `[]float64`.
            // A `MethodCall` node (the non-desugared method-call form) resolves
            // the same way as the `Call`-with-`FieldAccess` form above.
            NodeKind::MethodCall { method, .. } => {
                self.method_return_go_types.get(&method.name).cloned()
            }
            _ => None,
        }
    }

    /// Infer a single homogeneous Go element type for a collection literal's
    /// elements: `Some(ty)` iff the literal is non-empty and EVERY element
    /// infers (via [`Self::infer_go_expr_type`]) to the *same* concrete Go type.
    /// An empty literal, an element whose type can't be inferred, or a mix of
    /// types yields `None` — the caller then emits `interface{}`, which is never
    /// wrong (only loose). The `has_unresolved_operand` guard inside
    /// `infer_go_expr_type` already keeps arithmetic over unresolved identifiers
    /// from inferring an unsound type.
    fn infer_homogeneous_elem_type(&self, elems: &[AIRNode]) -> Option<String> {
        let refs: Vec<&AIRNode> = elems.iter().collect();
        self.infer_homogeneous_elem_type_refs(&refs)
    }

    /// `&AIRNode`-slice variant of [`Self::infer_homogeneous_elem_type`] (used
    /// for `MapLiteral` keys/values, which are not stored as a contiguous
    /// `&[AIRNode]`).
    fn infer_homogeneous_elem_type_refs(&self, elems: &[&AIRNode]) -> Option<String> {
        let mut iter = elems.iter();
        let first = self.infer_go_expr_type(iter.next()?)?;
        for e in iter {
            if self.infer_go_expr_type(e)? != first {
                return None;
            }
        }
        Some(first)
    }

    /// Build the explicit type-argument suffix (`[int64]`, `[int64, string]`)
    /// for a generic struct construction. For each of the target record's
    /// generic params (in declaration order) it finds the field whose declared
    /// type is exactly that param, then infers that field value's Go type. A
    /// param with no directly-typed field, or a value whose type can't be
    /// inferred, falls back to `any` (still a valid, if loose, instantiation).
    /// Returns `""` for a non-generic / unregistered type.
    /// The explicit Go type-argument suffix (`[int64]`) for a generic struct
    /// construction, recovered from the *declared* binding/expected type when it
    /// names this exact record (`current_expected_type == "ListIter[int64]"` for
    /// a `ListIter { ... }` construction). Returns `Some("[int64]")` then,
    /// `None` when there is no expected type, it names a different type, or it
    /// carries no args. More robust than field-value inference: it works when a
    /// generic param appears only *nested* in a field type (`xs: List[T]`),
    /// where no field is typed exactly `T`.
    fn expected_construct_type_args(&self, type_name: &str) -> Option<String> {
        let expected = self.current_expected_type.as_deref()?;
        let rest = expected.strip_prefix(type_name)?;
        // The remainder must be exactly a `[...]` type-arg list (so `ListIter`
        // does not match a hypothetical `ListIterator`); reject an empty suffix
        // (`ListIter` with no args) and anything not enclosed in brackets.
        if rest.starts_with('[') && rest.ends_with(']') && rest.len() > 2 {
            Some(rest.to_string())
        } else {
            None
        }
    }

    /// The concrete Go type-argument suffix (`[int64]`) for a *generic user enum*
    /// instantiation, recovered from a fully-rendered enum type string such as
    /// `Box[int64]`. Go has no sum type, so a generic user enum lowers to a
    /// sealed interface plus per-variant structs that ALL carry the enum's
    /// type-param list (`type BoxFull[T any] struct{…}`); every *use* of a
    /// variant struct — a construction (`BoxFull[int64]{…}`) and a type-switch
    /// `case` (`case BoxFull[int64]:`) — must therefore spell the concrete args,
    /// because Go rejects a bare generic type without instantiation. The args are
    /// the same for every variant of one enum (they share the enum's params), so
    /// they are read off the enum instantiation rather than per-variant.
    ///
    /// Returns `""` (no suffix) when the enum is non-generic, unregistered, or
    /// `full_type` is not exactly `<enum_name>[...]` — keeping a non-generic enum
    /// emitting the bare `BoxEmpty{}` / `case ShapeCircle:` it always did.
    fn enum_variant_type_arg_suffix(&self, enum_name: &str, full_type: &str) -> String {
        // A non-generic enum carries no params: never append a suffix.
        if self
            .generic_decls
            .get(enum_name)
            .is_none_or(|p| p.is_empty())
        {
            return String::new();
        }
        let Some(rest) = full_type.strip_prefix(enum_name) else {
            return String::new();
        };
        // The remainder must be exactly a `[...]` arg list (so `Box` does not
        // match `Boxer[…]`); reject an empty / unbracketed suffix.
        if rest.starts_with('[') && rest.ends_with(']') && rest.len() > 2 {
            rest.to_string()
        } else {
            String::new()
        }
    }

    /// The concrete type-arg suffix (`[int64]`) for constructing a variant of the
    /// generic user enum `enum_name`, read from the binding's *expected* type
    /// (`current_expected_type`, e.g. `Box[int64]` for `let f: Box[Int] =
    /// Full(7)`). Returns `""` when there is no expected type, it names a
    /// different enum, or the enum is non-generic. The construction-site analogue
    /// of [`Self::enum_variant_type_arg_suffix`].
    fn expected_enum_variant_type_arg_suffix(&self, enum_name: &str) -> String {
        match self.current_expected_type.as_deref() {
            Some(expected) => self.enum_variant_type_arg_suffix(enum_name, expected),
            None => String::new(),
        }
    }

    fn infer_construct_type_args(
        &self,
        type_name: &str,
        fields: &[bock_air::AirRecordField],
    ) -> String {
        let Some(per_param) = self.record_param_fields.get(type_name) else {
            return String::new();
        };
        if per_param.is_empty() {
            return String::new();
        }
        let args: Vec<String> = per_param
            .iter()
            .map(|field_name| {
                field_name
                    .as_ref()
                    .and_then(|fname| {
                        fields
                            .iter()
                            .find(|f| &f.name.name == fname)
                            .and_then(|f| f.value.as_deref())
                            .and_then(|v| self.infer_go_expr_type(v))
                    })
                    .unwrap_or_else(|| "any".to_string())
            })
            .collect();
        format!("[{}]", args.join(", "))
    }

    /// Record the `Optional[T]`, `List[T]`, `Map[K, V]`, `Set[E]`, and
    /// `Result[T, E]` element Go types of a function/lambda's parameters into the
    /// variable scopes, so a `match param { Some(x) => ... }` (direct Optional),
    /// `match param.get(i) { Some(x) => ... }` (List/Map built-in), or a `Set`
    /// membership test inside the body type-checks against the concrete element
    /// type. Returns the previous `(var_optional_elem, var_list_elem,
    /// var_result_elem, var_map_kv, var_set_elem)` scopes so the caller can
    /// restore them on exit (Go has no block-scoped reset here).
    #[allow(clippy::type_complexity)]
    fn enter_param_optional_scope(
        &mut self,
        params: &[AIRNode],
    ) -> (
        HashMap<String, String>,
        HashMap<String, String>,
        HashMap<String, (String, String)>,
        HashMap<String, (String, String)>,
        HashMap<String, String>,
    ) {
        let saved_opt = self.var_optional_elem.clone();
        let saved_list = self.var_list_elem.clone();
        let saved_result = self.var_result_elem.clone();
        let saved_map = self.var_map_kv.clone();
        let saved_set = self.var_set_elem.clone();
        for p in params {
            if let NodeKind::Param {
                pattern,
                ty: Some(t),
                ..
            } = &p.kind
            {
                let name = self.pattern_to_binding_name(pattern);
                // Record the full declared type node so a `match` whose
                // scrutinee is this param can peel a *nested* Optional/Result to
                // assert a tuple payload to its concrete struct (see
                // `var_decl_type_node`).
                self.var_decl_type_node.insert(name.clone(), (**t).clone());
                if let Some(elem) = self.optional_elem_go_type(t) {
                    self.var_optional_elem.insert(name.clone(), elem);
                }
                if let Some(elem) = self.list_elem_go_type(t) {
                    self.var_list_elem.insert(name.clone(), elem);
                }
                if let Some(kv) = self.map_kv_go_types(t) {
                    self.var_map_kv.insert(name.clone(), kv);
                }
                if let Some(elem) = self.set_elem_go_type(t) {
                    self.var_set_elem.insert(name.clone(), elem);
                }
                if let Some(elems) = self.result_elem_go_types(t) {
                    self.var_result_elem.insert(name.clone(), elems);
                }
                // A generic-record-typed param (`c: Counter[Int]`) records its
                // concrete instantiation so a `match c.next() { Some(x) => ... }`
                // can resolve the generic `Optional[T]` payload to the concrete
                // arg (`int64`) — see `scrutinee_optional_elem`.
                if let Some(record_args) = self.record_type_args(t) {
                    self.var_record_type_args.insert(name, record_args);
                }
            }
        }
        (saved_opt, saved_list, saved_result, saved_map, saved_set)
    }

    /// Record each typed param's Go type into [`Self::var_go_type`] so the
    /// body's expression types can be inferred (chiefly to give a lambda a
    /// concrete return type). Returns the previous map so the caller can restore
    /// it on exit. Untyped params are skipped (left absent → inference yields
    /// the `interface{}` fallback, never a wrong type).
    /// Record each param's Go type into the variable scope so the body's
    /// `infer_go_expr_type` sees concrete param types. A param whose source type
    /// is absent (an untyped lambda param) takes its type from the positional
    /// `expected` entry (the callee-specialised type, e.g. `int64`) when
    /// present, so `x > 2` / `x * 2` type-check and the lambda's inferred return
    /// type is concrete. Returns the previous scope for restore on exit. Pass
    /// `None` for `expected` when there are no specialised types (an ordinary
    /// typed lambda / fn body).
    /// The emitted Go binding names of a function/method's value parameters,
    /// used to pre-seed the body's Go block frame for shadowing-`let` tracking.
    fn param_binding_names(&self, params: &[AIRNode]) -> Vec<String> {
        params
            .iter()
            .filter_map(|p| match &p.kind {
                NodeKind::Param { pattern, .. } => {
                    let n = self.pattern_to_binding_name(pattern);
                    (n != "_").then_some(n)
                }
                _ => None,
            })
            .collect()
    }

    fn enter_param_go_types_with_expected(
        &mut self,
        params: &[AIRNode],
        expected: Option<&[String]>,
    ) -> HashMap<String, String> {
        let saved = self.var_go_type.clone();
        for (i, p) in params.iter().enumerate() {
            if let NodeKind::Param { pattern, ty, .. } = &p.kind {
                let name = self.pattern_to_binding_name(pattern);
                let go_ty = ty
                    .as_deref()
                    .map(|t| self.type_to_go(t))
                    .or_else(|| expected.and_then(|e| e.get(i).cloned()));
                if let Some(g) = go_ty {
                    self.var_go_type.insert(name, g);
                }
            }
        }
        saved
    }

    /// Render lambda params with explicit Go types drawn from `types` (one per
    /// param, positionally) — used when a lambda argument is specialised to a
    /// callee's concrete parameter types. A param with its own source
    /// annotation keeps it; otherwise the positional `types` entry is used.
    fn collect_param_strs_with_types(&self, params: &[AIRNode], types: &[String]) -> Vec<String> {
        params
            .iter()
            .enumerate()
            .filter_map(|(i, p)| {
                if let NodeKind::Param { pattern, ty, .. } = &p.kind {
                    let name = self.pattern_to_binding_name(pattern);
                    let type_str = ty
                        .as_ref()
                        .map(|t| self.type_to_go(t))
                        .or_else(|| types.get(i).cloned())
                        .unwrap_or_else(|| "interface{}".into());
                    Some(format!("{name} {type_str}"))
                } else {
                    None
                }
            })
            .collect()
    }

    /// Resolve the Go element type to assert for the payload of a `Some` bound in
    /// a `match` on `scrutinee`. Reachable for the common, structurally
    /// determinable cases: an identifier (parameter or typed `let`), a call to a
    /// function with a known `Optional[T]` return, and a *method call* whose
    /// method has a known `Optional[T]` return (`match it.next() { Some(x) =>
    /// ... }`, the shape `for x in <Iterable>` desugars to). Returns `None` when
    /// the element type cannot be determined structurally, in which case the
    /// binding is left as the runtime `interface{}` (no regression: that is the
    /// prior behavior, and `${v}`-style interpolation still works).
    /// Resolve a method-call's `Optional[T]` payload element to its CONCRETE Go
    /// type at the call site. `method_optional_ret_elem` stores the *generic*
    /// element as written on the method (`"T"`, the record's type param), which
    /// is undefined in a concrete caller such as `main`. When `receiver` is a
    /// variable bound to a concrete generic-record instantiation (recorded in
    /// [`Self::var_record_type_args`], e.g. `c: ListIter[Int]` →
    /// `("ListIter", ["int64"])`), and `elem` names one of that record's generic
    /// params, substitute the param with the corresponding concrete arg
    /// (`"T"` → `"int64"`). Otherwise `elem` is already concrete (a non-generic
    /// method, or a param-less return) and is returned unchanged.
    fn resolve_concrete_method_elem(&self, receiver: &AIRNode, elem: &str) -> String {
        let NodeKind::Identifier { name } = &receiver.kind else {
            return elem.to_string();
        };
        let Some((base, args)) = self.var_record_type_args.get(&go_value_ident(&name.name)) else {
            return elem.to_string();
        };
        let Some(params) = self.generic_decls.get(base) else {
            return elem.to_string();
        };
        // Find the generic param whose name equals `elem`, then map to the arg.
        if let Some(idx) = params.iter().position(|p| p.name.name == elem) {
            if let Some(concrete) = args.get(idx) {
                return concrete.clone();
            }
        }
        elem.to_string()
    }

    fn scrutinee_optional_elem(&self, scrutinee: &AIRNode) -> Option<String> {
        match &scrutinee.kind {
            NodeKind::Identifier { name } => self
                .var_optional_elem
                .get(&go_value_ident(&name.name))
                .cloned(),
            // A direct method call (`it.next()`).
            NodeKind::MethodCall {
                receiver, method, ..
            } => {
                let elem = self.method_optional_ret_elem.get(&method.name).cloned()?;
                Some(self.resolve_concrete_method_elem(receiver, &elem))
            }
            NodeKind::Call { callee, args, .. } => {
                // The read-only `List` built-ins `get`/`first`/`last` return
                // `Optional[<elem>]`. When the receiver is a variable with a
                // known `List[T]` element type, that element type is the payload
                // type — resolve it from `var_list_elem` before the generic
                // method-call path (whose `method_optional_ret_elem` only knows
                // *user-defined* methods, never the List built-ins).
                if let Some((recv, method, _)) =
                    crate::generator::desugared_list_method(scrutinee, callee, args)
                {
                    if matches!(method, "get" | "first" | "last") {
                        // The same receiver-element resolver the `.get` closure
                        // uses: a `List[T]` identifier (via `var_list_elem`), a
                        // homogeneous list literal, `self.field`, or a generic
                        // record param's `value.field` (`b.items.get(i)` for
                        // `b: Box[T]`). Without the last case the `Some(x)` payload
                        // stayed `interface{}` and a `return x` of a `[]T`-typed
                        // field element failed `go build` (GAP-A).
                        if let Some(elem) = self.list_receiver_elem_go_type(recv) {
                            return Some(elem);
                        }
                    }
                }
                // DQ30: `pop` on a `List[T]` returns `Optional[T]` — resolve
                // the payload the same way `get`/`first`/`last` do, so
                // `match xs.pop() { Some(v) => … }` type-asserts `v` to the
                // element type rather than `interface{}`.
                if let Some((recv, "pop", _)) =
                    crate::generator::desugared_list_inplace_mutator(scrutinee, callee, args)
                {
                    if let Some(elem) = self.list_receiver_elem_go_type(recv) {
                        return Some(elem);
                    }
                }
                // `Map.get(k)` returns `Optional[V]`; resolve the payload to the
                // map's value Go type so `match m.get(k) { Some(x) => … }`
                // type-asserts `x` to `V` rather than `interface{}`.
                if let Some((recv, "get", _)) =
                    crate::generator::desugared_map_method(scrutinee, callee, args)
                {
                    if let Some((_k, v)) = self.map_receiver_kv_go_types(recv) {
                        return Some(v);
                    }
                }
                match &callee.kind {
                    // Free-function call (`firstPositive(a, b)`).
                    NodeKind::Identifier { name } => {
                        self.fn_optional_ret_elem.get(&name.name).cloned()
                    }
                    // The AIR also lowers `recv.method(rest)` into
                    // `Call(FieldAccess(recv, method), [recv, ...rest])`; resolve
                    // it the same way as a direct `MethodCall` so both desugar
                    // shapes get a type-asserted payload.
                    NodeKind::FieldAccess { object, field } => {
                        crate::generator::desugared_self_call(callee, args)?;
                        let elem = self.method_optional_ret_elem.get(&field.name).cloned()?;
                        Some(self.resolve_concrete_method_elem(object, &elem))
                    }
                    _ => None,
                }
            }
            _ => None,
        }
    }

    /// Resolve the `(ok_go_type, err_go_type)` to assert for the payload of an
    /// `Ok`/`Err` bound in a `match` on `scrutinee`. The Result analogue of
    /// [`Self::scrutinee_optional_elem`]: an identifier (parameter or typed
    /// `let`) or a call to a function with a known `Result[T, E]` return.
    /// Returns `None` when the types cannot be determined structurally, in which
    /// case the payload falls back to the runtime `interface{}` (never wrong,
    /// only un-asserted).
    fn scrutinee_result_elems(&self, scrutinee: &AIRNode) -> Option<(String, String)> {
        match &scrutinee.kind {
            NodeKind::Identifier { name } => self
                .var_result_elem
                .get(&go_value_ident(&name.name))
                .cloned(),
            NodeKind::Call { callee, args, .. } => match &callee.kind {
                NodeKind::Identifier { name } => self.fn_result_ret_elem.get(&name.name).cloned(),
                NodeKind::FieldAccess { .. }
                    if crate::generator::desugared_self_call(callee, args).is_some() =>
                {
                    None
                }
                _ => None,
            },
            _ => None,
        }
    }

    /// The declared type-expression AIR node of a match scrutinee, when it is a
    /// variable (parameter or typed `let`) recorded in [`Self::var_decl_type_node`].
    /// Returns `None` for any other scrutinee shape (a call, a method call, …) —
    /// the pattern recursion then leaves a nested tuple payload un-asserted (the
    /// prior `interface{}` behavior; never wrong, only un-typed). Used to seed
    /// the declared-type threading through the if-chain pattern lowering so a
    /// `match v { Some(Ok((a, b))) => … }` asserts its tuple payload.
    fn scrutinee_decl_type_node(&self, scrutinee: &AIRNode) -> Option<&AIRNode> {
        if let NodeKind::Identifier { name } = &scrutinee.kind {
            return self.var_decl_type_node.get(&go_value_ident(&name.name));
        }
        None
    }

    /// The Go payload type of an *argument expression* whose static type is
    /// `Optional[T]`, when structurally recoverable. Extends
    /// [`Self::scrutinee_optional_elem`] (identifiers via `var_optional_elem`,
    /// calls via the fn/method return-element maps) with the bare-constructor
    /// case `Some(<expr>)` / `None`, whose payload type is inferred from the
    /// payload expression. Used to pin a generic free-fn's `Optional[T]`
    /// type-parameter at the call site (Go cannot infer it: the runtime
    /// `__bockOption` struct carries no `[T]`).
    fn arg_optional_elem(&self, arg: &AIRNode) -> Option<String> {
        if let NodeKind::Call { callee, args, .. } = &arg.kind {
            if let NodeKind::Identifier { name } = &callee.kind {
                match name.name.as_str() {
                    "Some" => return args.first().and_then(|a| self.infer_go_expr_type(&a.value)),
                    // `None` carries no payload type; nothing to bind.
                    "None" => return None,
                    _ => {}
                }
            }
        }
        self.scrutinee_optional_elem(arg)
    }

    /// The `(ok, err)` Go payload types of an *argument expression* whose static
    /// type is `Result[T, E]`, when recoverable. The `Result` analogue of
    /// [`Self::arg_optional_elem`]: identifiers / calls via
    /// [`Self::scrutinee_result_elems`], plus the bare `Ok(<expr>)` / `Err(<expr>)`
    /// constructors (only the present arm's type is inferable from a bare
    /// constructor, so the other stays `None`).
    fn arg_result_elems(&self, arg: &AIRNode) -> (Option<String>, Option<String>) {
        if let NodeKind::Call { callee, args, .. } = &arg.kind {
            if let NodeKind::Identifier { name } = &callee.kind {
                match name.name.as_str() {
                    "Ok" => {
                        return (
                            args.first().and_then(|a| self.infer_go_expr_type(&a.value)),
                            None,
                        )
                    }
                    "Err" => {
                        return (
                            None,
                            args.first().and_then(|a| self.infer_go_expr_type(&a.value)),
                        )
                    }
                    _ => {}
                }
            }
        }
        match self.scrutinee_result_elems(arg) {
            Some((ok, err)) => (Some(ok), Some(err)),
            None => (None, None),
        }
    }

    /// The `Optional[T]` inner / `Result[T, E]` arg type-param names of a
    /// declared parameter (or return) AIR type, when the type is one of those
    /// containers. Returns the param-name tokens (`["T"]` for `Optional[T]`,
    /// `["T", "E"]` for `Result[T, E]`) so the caller can pair them with the
    /// argument's recovered element types. `None` for any other type.
    fn container_type_param_names(node: &AIRNode) -> Option<(&'static str, Vec<&str>)> {
        match &node.kind {
            NodeKind::TypeOptional { inner } => {
                Some(("Optional", vec![Self::type_param_token(inner)?]))
            }
            NodeKind::TypeNamed { path, args } => {
                let name = path.segments.last().map(|s| s.name.as_str())?;
                match name {
                    "Optional" => Some(("Optional", vec![Self::type_param_token(args.first()?)?])),
                    "Result" => {
                        let t = Self::type_param_token(args.first()?)?;
                        let e = Self::type_param_token(args.get(1)?)?;
                        Some(("Result", vec![t, e]))
                    }
                    _ => None,
                }
            }
            _ => None,
        }
    }

    /// The bare type-parameter name a type node names, if it is a single
    /// unparameterised `TypeNamed` segment (`T` → `Some("T")`). `None` for any
    /// composite or primitive type — only a bare name can be bound positionally
    /// from a container's recovered element type.
    fn type_param_token(node: &AIRNode) -> Option<&str> {
        if let NodeKind::TypeNamed { path, args } = &node.kind {
            if args.is_empty() && path.segments.len() == 1 {
                return path.segments.first().map(|s| s.name.as_str());
            }
        }
        None
    }

    /// Synthesise the explicit Go type-arguments (`[int64]`, `[int64, string]`)
    /// for a call to a generic free function whose source omits them, so a Go
    /// type-parameter that the runtime cannot infer is pinned without a
    /// turbofish at the Bock level.
    ///
    /// Go can only infer a type-parameter from a value argument's static type.
    /// A parameter declared `Optional[T]` / `Result[T, E]` lowers to the
    /// *monomorphic* runtime struct `__bockOption` / `__bockResult` (payload
    /// `interface{}`), so its `T`/`E` are invisible to Go inference and a call
    /// like `or_else(empty, Some(7))` fails `cannot infer T`. This recovers each
    /// type-parameter's concrete Go type from, in order:
    ///
    /// 1. an `Optional[T]` / `Result[T, E]` argument's recovered element type
    ///    ([`Self::arg_optional_elem`] / [`Self::arg_result_elems`]),
    /// 2. an ordinary (non-container, non-lambda) argument unified against its
    ///    declared param type ([`Self::bind_fn_type_params`] — pins e.g.
    ///    `get_or`'s `fallback: T`),
    /// 3. the call's *expected* result type ([`Self::current_expected_type`],
    ///    a typed `let x: Ty = …`) unified against the declared return type.
    ///
    /// Returns `Some([go-type, …])` in declaration order, with any
    /// type-parameter still unresolved after the three sources filled with `any`
    /// (such a parameter appears *only* behind the erased runtime, so `any` is
    /// its only consistent type — e.g. `and_then`'s mapped `U`). Returns `None`
    /// — emitting no turbofish, so ordinary Go inference runs — when the
    /// signature names no `Optional`/`Result` container at all (the `core.iter`
    /// generic-record combinators, which Go already infers and must not change).
    fn synthesize_go_type_args(
        &self,
        gp_names: &[String],
        param_tys: &[Option<AIRNode>],
        ret_ty: Option<&AIRNode>,
        args: &[bock_air::AirArg],
        force: bool,
    ) -> Option<Vec<String>> {
        if gp_names.is_empty() {
            return None;
        }
        // Only intervene for a fn whose signature involves the monomorphic
        // `Optional`/`Result` runtime — the case Go's own inference cannot
        // handle (`__bockOption`/`__bockResult` carry no `[T]`) — or one whose
        // sealed-core bound was lowered to a built-in constraint (`force`), under
        // which Go infers an untyped constant as the default type (`int`, not
        // `int64`). A purely record/collection-generic fn (`core.iter`'s
        // `ListIterator[T]` combinators) is left bare so Go infers it as before —
        // no regression.
        let touches_container = param_tys
            .iter()
            .flatten()
            .chain(ret_ty)
            .any(Self::type_mentions_container);
        // A type param that appears in *no* value parameter cannot be inferred by
        // Go from the arguments — it is pinned only by the return type (e.g.
        // `fn empty[T]() -> SortedSet[T]`, a zero-arg generic constructor, or any
        // return-only param). Such a call always needs an explicit turbofish,
        // synthesised below from the expected destination type. (A param that
        // *does* appear in a value parameter is left to Go's own inference unless
        // a container/sealed-bound reason forces the turbofish.)
        let param_go_types: Vec<String> = param_tys
            .iter()
            .flatten()
            .map(|p| self.type_to_go(p))
            .collect();
        let has_return_only_param = gp_names.iter().any(|g| {
            !param_go_types
                .iter()
                .any(|p| Self::contains_type_token(p, g))
        });
        if !touches_container && !force && !has_return_only_param {
            return None;
        }
        let mut bindings: HashMap<String, String> = HashMap::new();

        // (2) Ordinary argument unification (bare `T`, `List[T]`, etc.).
        for (k, v) in self.bind_fn_type_params(gp_names, param_tys, args) {
            bindings.entry(k).or_insert(v);
        }

        // (1) Container arguments: pair each declared `Optional[T]`/`Result[T,E]`
        // param's type-param tokens with the argument's recovered element types.
        for (i, arg) in args.iter().enumerate() {
            let Some(pty) = param_tys.get(i).and_then(|p| p.as_ref()) else {
                continue;
            };
            let Some((container, tokens)) = Self::container_type_param_names(pty) else {
                continue;
            };
            match container {
                "Optional" => {
                    if let (Some(token), Some(elem)) =
                        (tokens.first(), self.arg_optional_elem(&arg.value))
                    {
                        if gp_names.iter().any(|g| g == *token) {
                            bindings.entry((*token).to_string()).or_insert(elem);
                        }
                    }
                }
                "Result" => {
                    let (ok, err) = self.arg_result_elems(&arg.value);
                    if let (Some(token), Some(ty)) = (tokens.first(), ok) {
                        if gp_names.iter().any(|g| g == *token) {
                            bindings.entry((*token).to_string()).or_insert(ty);
                        }
                    }
                    if let (Some(token), Some(ty)) = (tokens.get(1), err) {
                        if gp_names.iter().any(|g| g == *token) {
                            bindings.entry((*token).to_string()).or_insert(ty);
                        }
                    }
                }
                _ => {}
            }
        }

        // (3) Expected result type unified against the declared return type. The
        // typed-`let` binding sets `current_expected_type` to the rendered Go
        // type of the destination, e.g. `[]int64` for `let xs: List[Int] =
        // to_list(...)`, which unifies against `List[T]` → `[]T` to pin `T`.
        if let (Some(ret), Some(expected)) = (ret_ty, self.current_expected_type.as_deref()) {
            if expected != "interface{}" {
                let pattern = self.type_to_go(ret);
                Self::unify_go_pattern(&pattern, expected, gp_names, &mut bindings);
            }
        }

        // Every type-parameter is filled: a pinned one with its concrete Go
        // type, an unresolved one with `any`. An unresolved param appears *only*
        // behind the erased `Optional`/`Result` runtime (a bare `T`, `List[T]`,
        // `Fn(T) -> …`, etc. would have been bound above from an argument or the
        // return), so `any` is the only type it can take and never conflicts —
        // e.g. `and_then`'s `U` (the mapped `Ok` type) is invisible to the call
        // and harmlessly erased, while its `E` is pinned from the `Result[T, E]`
        // argument so Go no longer fails `cannot infer E`.
        let out: Vec<String> = gp_names
            .iter()
            .map(|g| {
                bindings
                    .get(g)
                    .cloned()
                    .unwrap_or_else(|| "any".to_string())
            })
            .collect();
        Some(out)
    }

    /// True if a declared AIR type *mentions* the monomorphic `Optional` /
    /// `Result` runtime anywhere within it (directly, or nested inside a
    /// collection / function type). Gates [`Self::synthesize_go_type_args`] —
    /// only such a signature defeats Go's own type-parameter inference.
    fn type_mentions_container(node: &AIRNode) -> bool {
        match &node.kind {
            NodeKind::TypeOptional { .. } => true,
            NodeKind::TypeNamed { path, args } => {
                let is_container = path
                    .segments
                    .last()
                    .is_some_and(|s| matches!(s.name.as_str(), "Optional" | "Result"));
                is_container || args.iter().any(Self::type_mentions_container)
            }
            NodeKind::TypeFunction { params, ret, .. } => {
                params.iter().any(Self::type_mentions_container)
                    || Self::type_mentions_container(ret)
            }
            NodeKind::TypeTuple { elems } => elems.iter().any(Self::type_mentions_container),
            _ => false,
        }
    }

    /// Returns `true` if the AIR type node mentions any of the named generic
    /// params (a bare `T`, or `T` nested inside `List[T]` / `Optional[T]` /
    /// `(T, U)` / a function type). Used to skip recording a method's Go return
    /// type when it is still generic (the concrete caller has no such `T`).
    fn type_mentions_params(node: &AIRNode, params: &[String]) -> bool {
        match &node.kind {
            NodeKind::TypeNamed { path, args } => {
                let names_param = path
                    .segments
                    .last()
                    .is_some_and(|s| params.iter().any(|p| p == &s.name));
                names_param || args.iter().any(|a| Self::type_mentions_params(a, params))
            }
            NodeKind::TypeOptional { inner } => Self::type_mentions_params(inner, params),
            NodeKind::TypeFunction {
                params: ps, ret, ..
            } => {
                ps.iter().any(|p| Self::type_mentions_params(p, params))
                    || Self::type_mentions_params(ret, params)
            }
            NodeKind::TypeTuple { elems } => {
                elems.iter().any(|e| Self::type_mentions_params(e, params))
            }
            _ => false,
        }
    }

    /// Returns `true` if the AIR type node represents `Void` or `Unit`.
    fn is_void_type(node: &AIRNode) -> bool {
        if let NodeKind::TypeNamed { path, .. } = &node.kind {
            if let Some(last) = path.segments.last() {
                return last.name == "Void" || last.name == "Unit";
            }
        }
        if let NodeKind::TypeTuple { elems } = &node.kind {
            return elems.is_empty();
        }
        false
    }

    /// Returns `true` if the AST `TypeExpr` represents `Void` or `Unit` (the
    /// `TypeExpr` analogue of [`Self::is_void_type`]). Used by `ast_type_to_go`
    /// to render a `Fn(...) -> Void` as a Go `func(...)` with no result type.
    fn ast_type_is_void(ty: &TypeExpr) -> bool {
        match ty {
            TypeExpr::Named { path, args, .. } if args.is_empty() => path
                .segments
                .last()
                .is_some_and(|s| s.name == "Void" || s.name == "Unit"),
            TypeExpr::Tuple { elems, .. } => elems.is_empty(),
            _ => false,
        }
    }

    /// Returns the emitted body and import flags without building the preamble.
    fn into_parts(self) -> (String, GoImportNeeds) {
        (
            self.buf,
            GoImportNeeds {
                fmt: self.needs_fmt_import,
                sync: self.needs_sync_import,
                time: self.needs_time_import,
                strings: self.needs_strings_import,
                utf8: self.needs_utf8_import,
                math: self.needs_math_import,
                unicode: self.needs_unicode_import,
                strconv: self.needs_strconv_import,
                reflect: self.needs_reflect_import,
            },
        )
    }

    fn finish(self) -> String {
        let mut header = format!("package {}\n", self.package_name);
        let needs = GoImportNeeds {
            fmt: self.needs_fmt_import,
            sync: self.needs_sync_import,
            time: self.needs_time_import,
            strings: self.needs_strings_import,
            utf8: self.needs_utf8_import,
            math: self.needs_math_import,
            unicode: self.needs_unicode_import,
            strconv: self.needs_strconv_import,
            reflect: self.needs_reflect_import,
        };
        header.push_str(&needs.render_block());
        header.push('\n');
        header.push_str(&self.buf);
        header
    }

    fn indent_str(&self) -> String {
        "\t".repeat(self.indent)
    }

    fn write_indent(&mut self) {
        let indent = self.indent_str();
        self.buf.push_str(&indent);
    }

    fn writeln(&mut self, s: &str) {
        self.write_indent();
        self.buf.push_str(s);
        self.buf.push('\n');
    }

    // ── Prelude function mapping ──────────────────────────────────────────

    /// Emit an expression into a temporary buffer and return the string.
    fn expr_to_string(&mut self, node: &AIRNode) -> Result<String, CodegenError> {
        let start = self.buf.len();
        self.emit_expr(node)?;
        let s = self.buf[start..].to_string();
        self.buf.truncate(start);
        Ok(s)
    }

    /// The Go string for an `Optional`/`Result` constructor's payload, casting a
    /// *numeric literal* payload to its concrete Go type (`int64` / `float64`).
    ///
    /// The runtime boxes the payload as `interface{}`. A bare Go integer literal
    /// (`__bockSome(7)`) is an *untyped constant* whose default boxed dynamic
    /// type is Go `int`, not `int64` — so a later `.(int64)` payload assertion
    /// (or a generic `.(T)` with `T` instantiated as `int64`) panics
    /// `interface {} is int, not int64`. The read-side widening helpers
    /// (`__bockAsInt64`) mask this for *concrete* `int64` assertions, but a
    /// generic free fn's body asserts the bare type parameter `T`, which has no
    /// widening. Boxing the literal as `int64(7)` / `float64(..)` makes the
    /// dynamic type match the instantiation, so both assertion forms succeed.
    /// Non-literal and non-numeric payloads are passed through unchanged.
    fn box_payload_str(&self, arg: Option<&bock_air::AirArg>, arg_strs: &[String]) -> String {
        let rendered = arg_strs
            .first()
            .map_or_else(|| "nil".to_string(), |s| s.clone());
        let Some(arg) = arg else {
            return rendered;
        };
        match Self::numeric_literal_go_type(&arg.value) {
            Some(go_ty) => format!("{go_ty}({rendered})"),
            None => rendered,
        }
    }

    /// The Go numeric type (`int64`/`float64`) of an expression that is a numeric
    /// literal — directly, or under a unary negation (`-1`) — else `None`. Used
    /// to box an `Optional`/`Result` payload literal at its concrete dynamic type
    /// (see [`Self::box_payload_str`]).
    /// Decide whether a `**` (`BinOp::Pow`) should lower to the floating-point
    /// path (`math.Pow`) or the integer path (`__bockIntPow`). Returns `true` if
    /// either operand is statically float-typed (a `Float` literal, or a binding
    /// inferred to `float64`/`float32`). When neither operand resolves to a float
    /// — the common `2 ** 10` integer case, or an unresolved operand — the integer
    /// helper is chosen, which keeps exact integer precision. Both operands are
    /// coerced to the chosen numeric type at the call site, so a mixed
    /// `Int ** Float` still routes to `math.Pow` and type-checks.
    fn pow_is_float(&self, left: &AIRNode, right: &AIRNode) -> bool {
        let is_float = |this: &Self, n: &AIRNode| -> bool {
            matches!(
                this.infer_go_expr_type(n).as_deref(),
                Some("float64") | Some("float32")
            )
        };
        is_float(self, left) || is_float(self, right)
    }

    fn numeric_literal_go_type(node: &AIRNode) -> Option<&'static str> {
        match &node.kind {
            NodeKind::Literal { lit } => match lit {
                Literal::Int(_) => Some("int64"),
                Literal::Float(_) => Some("float64"),
                _ => None,
            },
            NodeKind::UnaryOp {
                op: UnaryOp::Neg,
                operand,
            } => Self::numeric_literal_go_type(operand),
            _ => None,
        }
    }

    /// Map Bock prelude functions to Go equivalents.
    fn map_prelude_call(
        &mut self,
        callee: &AIRNode,
        args: &[bock_air::AirArg],
    ) -> Result<Option<String>, CodegenError> {
        let name = match &callee.kind {
            NodeKind::Identifier { name } => name.name.as_str(),
            _ => return Ok(None),
        };
        let arg_strs: Vec<String> = args
            .iter()
            .map(|a| self.expr_to_string(&a.value))
            .collect::<Result<_, _>>()?;
        let code = match name {
            "println" => {
                self.needs_fmt_import = true;
                let a = arg_strs.first().map_or(String::new(), |s| s.clone());
                format!("fmt.Println({a})")
            }
            "print" => {
                self.needs_fmt_import = true;
                let a = arg_strs.first().map_or(String::new(), |s| s.clone());
                format!("fmt.Print({a})")
            }
            "debug" => {
                self.needs_fmt_import = true;
                let a = arg_strs.first().map_or(String::new(), |s| s.clone());
                format!("fmt.Printf(\"%+v\\n\", {a})")
            }
            "assert" => {
                let a = arg_strs.first().map_or(String::new(), |s| s.clone());
                format!("if !{a} {{ panic(\"assertion failed\") }}")
            }
            "todo" => "panic(\"not implemented\")".to_string(),
            "unreachable" => "panic(\"unreachable\")".to_string(),
            "sleep" => {
                let a = arg_strs.first().map_or(String::new(), |s| s.clone());
                // Route through an installed `Clock` handler if one is in scope;
                // otherwise fall through to the host primitive (default).
                if let Some(handler) = self.clock_handler_var() {
                    format!("{handler}.{}({a})", to_pascal_case("sleep"))
                } else {
                    // sleep(d) returns a chan struct{} so `await` (= `<-ch`)
                    // works uniformly. The goroutine holds for `d` nanos, then
                    // closes ch.
                    self.needs_time_import = true;
                    format!("(func() <-chan struct{{}} {{ __ch := make(chan struct{{}}); go func() {{ time.Sleep(time.Duration({a})); close(__ch) }}(); return __ch }})()")
                }
            }
            // Optional constructors → tagged runtime struct.
            "Some" => {
                let a = self.box_payload_str(args.first(), &arg_strs);
                format!("__bockSome({a})")
            }
            "None" => "__bockNone".to_string(),
            // Result constructors → tagged runtime struct (see
            // `RESULT_RUNTIME_GO`), mirroring `Some`/`None`.
            "Ok" => {
                let a = self.box_payload_str(args.first(), &arg_strs);
                format!("__bockOk({a})")
            }
            "Err" => {
                let a = self.box_payload_str(args.first(), &arg_strs);
                format!("__bockErr({a})")
            }
            _ => return Ok(None),
        };
        Ok(Some(code))
    }

    /// Emit a built-in `Optional`/`Result` method call to its Go form.
    ///
    /// Recognised via the checker's `recv_kind` annotation
    /// ([`crate::generator::desugared_optional_method`] /
    /// [`crate::generator::desugared_result_method`]). The tagged runtime structs
    /// (`__bockOption`/`__bockResult`) carry the payload as `interface{}` in `.v`
    /// and the tag in `.tag`, so a method lowers to a Go closure IIFE that tests
    /// `.tag` and recovers the payload. The payload Go type (for `unwrap`/
    /// `unwrap_or`) is resolved from the receiver's declared `Optional[T]` /
    /// `Result[T, E]` type; when unknown it stays `interface{}` (works for `%v`
    /// interpolation, the conservative fallback the Optional match also uses).
    /// Returns `true` if handled.
    fn try_emit_container_method(
        &mut self,
        node: &AIRNode,
        callee: &AIRNode,
        args: &[bock_air::AirArg],
    ) -> Result<bool, CodegenError> {
        if let Some((recv, method, rest)) =
            crate::generator::desugared_optional_method(node, callee, args)
        {
            let elem = self.scrutinee_optional_elem(recv);
            self.emit_tagged_container_method(
                recv,
                method,
                rest,
                "Some",
                "__bockSome",
                "__bockNone",
                elem.as_deref(),
            )?;
            return Ok(true);
        }
        if let Some((recv, method, rest)) =
            crate::generator::desugared_result_method(node, callee, args)
        {
            let elems = self.scrutinee_result_elems(recv);
            let ok = elems.as_ref().map(|(o, _)| o.as_str());
            self.emit_tagged_container_method(
                recv,
                method,
                rest,
                "Ok",
                "__bockOk",
                "__bockErr",
                ok,
            )?;
            return Ok(true);
        }
        Ok(false)
    }

    /// Lower a tagged-container method on `recv` to a Go closure IIFE.
    /// `present_tag` is the payload-carrying tag (`"Some"`/`"Ok"`);
    /// `present_ctor`/`other_ctor` are the runtime constructors; `payload_ty` is
    /// the Go type the payload is asserted to (`None` → bare `interface{}`).
    #[allow(clippy::too_many_arguments)]
    fn emit_tagged_container_method(
        &mut self,
        recv: &AIRNode,
        method: &str,
        rest: &[bock_air::AirArg],
        present_tag: &str,
        present_ctor: &str,
        other_ctor: &str,
        payload_ty: Option<&str>,
    ) -> Result<(), CodegenError> {
        // The closure binds the receiver once as `__c` (a tagged struct).
        // Tag tests: `is_some`/`is_ok` and `is_none`/`is_err`.
        match method {
            "is_some" | "is_ok" => {
                self.buf.push('(');
                self.emit_expr(recv)?;
                let _ = write!(self.buf, ".tag == \"{present_tag}\")");
                return Ok(());
            }
            "is_none" | "is_err" => {
                self.buf.push('(');
                self.emit_expr(recv)?;
                let _ = write!(self.buf, ".tag != \"{present_tag}\")");
                return Ok(());
            }
            _ => {}
        }
        // Recover the payload as its concrete type (numeric boxings widened via
        // the shared helpers; otherwise a type assertion; else bare `.v`).
        let payload_expr = |ty: Option<&str>| -> String {
            match ty {
                Some("int64") => "__bockAsInt64(__c.v)".to_string(),
                Some("float64") => "__bockAsFloat64(__c.v)".to_string(),
                Some(t) => format!("__c.v.({t})"),
                None => "__c.v".to_string(),
            }
        };
        match method {
            "unwrap" | "unwrap_or" => {
                let ret_ty = payload_ty.unwrap_or("interface{}");
                let payload = payload_expr(payload_ty);
                let _ = write!(
                    self.buf,
                    "func(__c {recv_ty}) {ret_ty} {{ if __c.tag == \"{present_tag}\" {{ return {payload} }}; return ",
                    recv_ty = self.container_runtime_ty(present_ctor),
                );
                if method == "unwrap_or" {
                    if let Some(d) = rest.first() {
                        self.emit_expr(&d.value)?;
                    } else {
                        // No default supplied — fall back to the zero value.
                        self.zero_value_for(ret_ty);
                    }
                } else {
                    // `unwrap` on the empty case panics (no default supplied).
                    self.zero_value_for(ret_ty);
                }
                self.buf.push_str(" }(");
                self.emit_expr(recv)?;
                self.buf.push(')');
            }
            "map" => {
                // Apply the callback to the payload and rewrap as the present
                // variant; the empty/other variant passes through unchanged.
                let recv_ty = self.container_runtime_ty(present_ctor);
                let payload = payload_expr(payload_ty);
                let _ = write!(
                    self.buf,
                    "func(__c {recv_ty}) {recv_ty} {{ if __c.tag == \"{present_tag}\" {{ return {present_ctor}("
                );
                if let Some(f) = rest.first() {
                    self.emit_expr(&f.value)?;
                } else {
                    self.buf
                        .push_str("func(x interface{}) interface{} { return x }");
                }
                let _ = write!(self.buf, "({payload})) }}; return __c }}(");
                self.emit_expr(recv)?;
                self.buf.push(')');
            }
            "flat_map" => {
                let recv_ty = self.container_runtime_ty(present_ctor);
                let payload = payload_expr(payload_ty);
                let _ = write!(
                    self.buf,
                    "func(__c {recv_ty}) {recv_ty} {{ if __c.tag == \"{present_tag}\" {{ return "
                );
                if let Some(f) = rest.first() {
                    self.emit_expr(&f.value)?;
                } else {
                    self.buf
                        .push_str("func(x interface{}) interface{} { return x }");
                }
                let _ = write!(self.buf, "({payload}).({recv_ty}) }}; return __c }}(");
                self.emit_expr(recv)?;
                self.buf.push(')');
            }
            "map_err" => {
                let recv_ty = self.container_runtime_ty(present_ctor);
                let _ = write!(
                    self.buf,
                    "func(__c {recv_ty}) {recv_ty} {{ if __c.tag != \"{present_tag}\" {{ return {other_ctor}("
                );
                if let Some(f) = rest.first() {
                    self.emit_expr(&f.value)?;
                } else {
                    self.buf
                        .push_str("func(x interface{}) interface{} { return x }");
                }
                self.buf.push_str("(__c.v)) }; return __c }(");
                self.emit_expr(recv)?;
                self.buf.push(')');
            }
            _ => {
                self.buf.push_str("nil");
            }
        }
        Ok(())
    }

    /// The Go runtime struct type for a container, keyed by its present-variant
    /// constructor (`__bockSome` → `__bockOption`, `__bockOk` → `__bockResult`).
    fn container_runtime_ty(&self, present_ctor: &str) -> &'static str {
        if present_ctor == "__bockOk" {
            "__bockResult"
        } else {
            "__bockOption"
        }
    }

    /// Emit a Go zero value for `ty` (used as the `unwrap`-on-empty fallback).
    fn zero_value_for(&mut self, ty: &str) {
        let zero = match ty {
            "int64" | "float64" | "int" | "float32" | "int32" => "0",
            "string" => "\"\"",
            "bool" => "false",
            _ => "nil",
        };
        self.buf.push_str(zero);
    }

    /// Emit a read-only `List` built-in method call to its Go form.
    ///
    /// Lists are `[]interface{}`. `len`/`length`/`count` wrap in `int64(...)`;
    /// `is_empty` compares the length. `Optional`-returning methods
    /// (`get`/`first`/`last`/`index_of`) build the tagged Optional runtime
    /// (`__bockSome(v)` / `__bockNone`) inside an immediately-called closure so
    /// the receiver is evaluated once and bounds are checked. `contains` /
    /// `index_of` / `concat` / `join` use inline closures (no top-level helper
    /// injection needed). The `__bockSome` payload is `interface{}`; a `match`
    /// arm binding it re-asserts the element type via the existing Optional
    /// resolver (`scrutinee_optional_elem`), which now resolves
    /// `get`/`first`/`last` on a typed `List[T]` receiver.
    fn try_emit_list_method(
        &mut self,
        node: &AIRNode,
        callee: &AIRNode,
        args: &[bock_air::AirArg],
    ) -> Result<bool, CodegenError> {
        let Some((recv, method, rest)) =
            crate::generator::desugared_list_method(node, callee, args)
        else {
            return Ok(false);
        };
        let recv_str = self.expr_to_string(recv)?;
        // The receiver's Go slice element type. Lists are now concretely typed
        // (`[]int64`, etc.), so the closure parameter type (`__r []<elem>`) must
        // match the receiver — a `[]int64` argument does NOT convert to a
        // `[]interface{}` parameter in Go. When the element type can't be
        // recovered the receiver is still `[]interface{}` (the literal/inference
        // fallback), so `interface{}` is the correct, matching default.
        let elem = self
            .list_receiver_elem_go_type(recv)
            .unwrap_or_else(|| "interface{}".to_string());
        let slice = format!("[]{elem}");
        let code = match method {
            "len" | "length" | "count" => format!("int64(len({recv_str}))"),
            "is_empty" => format!("(len({recv_str}) == 0)"),
            "get" => {
                let Some(idx) = rest.first() else {
                    return Ok(false);
                };
                let i = self.expr_to_string(&idx.value)?;
                format!(
                    "func(__r {slice}, __i int64) __bockOption {{ \
                     if __i >= 0 && __i < int64(len(__r)) {{ return __bockSome(__r[__i]) }}; \
                     return __bockNone }}({recv_str}, {i})"
                )
            }
            "first" => format!(
                "func(__r {slice}) __bockOption {{ \
                 if len(__r) > 0 {{ return __bockSome(__r[0]) }}; \
                 return __bockNone }}({recv_str})"
            ),
            "last" => format!(
                "func(__r {slice}) __bockOption {{ \
                 if len(__r) > 0 {{ return __bockSome(__r[len(__r)-1]) }}; \
                 return __bockNone }}({recv_str})"
            ),
            "contains" => {
                let Some(x) = rest.first() else {
                    return Ok(false);
                };
                self.needs_fmt_import = true;
                let x = self.expr_to_string(&x.value)?;
                // Compare on the `%v` string form, not raw `interface{}` `==`:
                // a list literal boxes Go `int`/`float64` while a typed `Int`
                // variable is `int64`, so `int(30) == int64(30)` is *false*
                // under Go's type-and-value interface equality. The checker
                // guarantees `contains(x: T)` on `List[T]` (same T), so the two
                // operands always denote the same Bock type — `%v` normalises
                // only the int/int64 boxing difference. `__x` stays
                // `interface{}` (a typed argument boxes into it).
                format!(
                    "func(__r {slice}, __x interface{{}}) bool {{ \
                     __xs := fmt.Sprintf(\"%v\", __x); \
                     for _, __e := range __r {{ if fmt.Sprintf(\"%v\", __e) == __xs {{ return true }} }}; \
                     return false }}({recv_str}, {x})"
                )
            }
            "index_of" => {
                let Some(x) = rest.first() else {
                    return Ok(false);
                };
                self.needs_fmt_import = true;
                let x = self.expr_to_string(&x.value)?;
                // See `contains` for why this compares `%v` forms, not `==`.
                format!(
                    "func(__r {slice}, __x interface{{}}) __bockOption {{ \
                     __xs := fmt.Sprintf(\"%v\", __x); \
                     for __i, __e := range __r {{ if fmt.Sprintf(\"%v\", __e) == __xs {{ return __bockSome(int64(__i)) }} }}; \
                     return __bockNone }}({recv_str}, {x})"
                )
            }
            "concat" => {
                let Some(o) = rest.first() else {
                    return Ok(false);
                };
                // The `__o` IIFE parameter is `[]elem` (the receiver's element
                // type), so the argument list literal must also be `[]elem{...}`
                // — a `[]interface{}{x}` argument is not assignable to a `[]T`
                // parameter in Go. Thread the receiver's element type into the
                // literal as its expected collection element (extends #144's
                // return-position typed-literal fix to argument position).
                let prev_expected = self.expected_collection_elem.take();
                if matches!(
                    o.value.kind,
                    NodeKind::ListLiteral { .. }
                        | NodeKind::MapLiteral { .. }
                        | NodeKind::SetLiteral { .. }
                ) {
                    self.expected_collection_elem = Some((elem.clone(), None));
                }
                let o = self.expr_to_string(&o.value)?;
                self.expected_collection_elem = prev_expected;
                format!(
                    "func(__r {slice}, __o {slice}) {slice} {{ \
                     __v := make({slice}, 0, len(__r)+len(__o)); \
                     __v = append(__v, __r...); __v = append(__v, __o...); \
                     return __v }}({recv_str}, {o})"
                )
            }
            "join" => {
                let Some(sep) = rest.first() else {
                    return Ok(false);
                };
                self.needs_fmt_import = true;
                let sep = self.expr_to_string(&sep.value)?;
                format!(
                    "func(__r {slice}, __sep string) string {{ \
                     __s := \"\"; \
                     for __i, __e := range __r {{ if __i > 0 {{ __s += __sep }}; \
                     __s += fmt.Sprintf(\"%v\", __e) }}; \
                     return __s }}({recv_str}, {sep})"
                )
            }
            _ => return Ok(false),
        };
        self.buf.push_str(&code);
        Ok(true)
    }

    /// Emit an in-place `List` mutator (`push`/`append`, DQ18) in **statement
    /// position** to its Go form.
    ///
    /// Recognised via [`crate::generator::desugared_list_mutating_method`]. Go
    /// grows a slice by *reassignment* — `recv = append(recv, x)` — so unlike the
    /// other backends (which emit a value-less `recv.push(x)`) this is an
    /// assignment statement, emitted only from `emit_stmt`. The checker types
    /// `push`/`append` as `Void`, so the call always appears in statement
    /// position, and the ownership pass guarantees the receiver is a `mut` lvalue
    /// (a `let mut` slice, a `mut` parameter, or a field reachable through a
    /// `mut` receiver), so the same place expression is a valid assignment
    /// target on the left of `=`. A field receiver lowers to its Go-cased place
    /// (`r.Items = append(r.Items, x)`) via `expr_to_string`.
    ///
    /// Returns `false` (no statement emitted) when the call is not a recognised
    /// in-place `List` mutator, so the caller falls back to the generic
    /// expression-statement path.
    fn try_emit_list_mutating_stmt(
        &mut self,
        node: &AIRNode,
        callee: &AIRNode,
        args: &[bock_air::AirArg],
    ) -> Result<bool, CodegenError> {
        let Some((recv, _method, rest)) =
            crate::generator::desugared_list_mutating_method(node, callee, args)
        else {
            return Ok(false);
        };
        let Some(x) = rest.first() else {
            return Ok(false);
        };
        let recv_str = self.expr_to_string(recv)?;
        let x = self.expr_to_string(&x.value)?;
        self.write_indent();
        let _ = write!(self.buf, "{recv_str} = append({recv_str}, {x})");
        self.buf.push('\n');
        Ok(true)
    }

    /// Emit a DQ30 in-place `List` mutator
    /// (`pop`/`remove_at`/`insert`/`reverse`/`set`) to its Go form.
    ///
    /// Recognised via [`crate::generator::desugared_list_inplace_mutator`]. Go
    /// slices are *values* (a length change must reassign the receiver place,
    /// the DQ18 `recv = append(recv, x)` pattern), but `pop`/`remove_at` also
    /// *return* a value, so they cannot be assignment statements. The two are
    /// reconciled by spelling the reassign through a **pointer**: each
    /// length-changing lowering is an immediately-invoked func literal taking
    /// `__r *[]T` and assigning `*__r = …` — the same receiver reassignment,
    /// composed into expression position. The ownership pass guarantees the
    /// receiver is a `mut` lvalue (an identifier / field / index place), so
    /// `&recv` is addressable. `reverse` and `set` never change the length, so
    /// they mutate the shared backing array directly (no pointer):
    ///
    /// - `pop` → len-check + last-element grab + `*__r = (*__r)[:len-1]`
    ///   shrink, wrapped into the tagged `__bockOption` runtime
    ///   (`__bockSome(v)` / `__bockNone`);
    /// - `remove_at(i)` → bounds-check + grab +
    ///   `*__r = append((*__r)[:i], (*__r)[i+1:]...)` reassign;
    /// - `insert(i, x)` → bounds-check (`0..=len`) + append-grow +
    ///   `copy` right-shift + write;
    /// - `reverse` → in-place two-index swap loop;
    /// - `set(i, x)` → bounds-check + `__r[i] = x`.
    ///
    /// The bounds checks `panic(fmt.Sprintf(...))` with the normalized abort
    /// message `List.<op>: index <i> out of bounds (len <n>)`, Go's idiomatic
    /// abort channel (the DQ23 convention — Go's native divide-by-zero panic
    /// was likewise kept).
    fn try_emit_list_inplace_mutator(
        &mut self,
        node: &AIRNode,
        callee: &AIRNode,
        args: &[bock_air::AirArg],
    ) -> Result<bool, CodegenError> {
        let Some((recv, method, rest)) =
            crate::generator::desugared_list_inplace_mutator(node, callee, args)
        else {
            return Ok(false);
        };
        let recv_str = self.expr_to_string(recv)?;
        let elem = self
            .list_receiver_elem_go_type(recv)
            .unwrap_or_else(|| "interface{}".to_string());
        let slice = format!("[]{elem}");
        let code = match method {
            "pop" => format!(
                "func(__r *{slice}) __bockOption {{ \
                 if len(*__r) == 0 {{ return __bockNone }}; \
                 __v := (*__r)[len(*__r)-1]; \
                 *__r = (*__r)[:len(*__r)-1]; \
                 return __bockSome(__v) }}(&{recv_str})"
            ),
            "remove_at" => {
                let Some(idx) = rest.first() else {
                    return Ok(false);
                };
                self.needs_fmt_import = true;
                let i = self.expr_to_string(&idx.value)?;
                format!(
                    "func(__r *{slice}, __i int64) {elem} {{ \
                     if __i < 0 || __i >= int64(len(*__r)) {{ \
                     panic(fmt.Sprintf(\"List.remove_at: index %d out of bounds (len %d)\", __i, len(*__r))) }}; \
                     __v := (*__r)[__i]; \
                     *__r = append((*__r)[:__i], (*__r)[__i+1:]...); \
                     return __v }}(&{recv_str}, {i})"
                )
            }
            "insert" => {
                let (Some(idx), Some(x)) = (rest.first(), rest.get(1)) else {
                    return Ok(false);
                };
                self.needs_fmt_import = true;
                let i = self.expr_to_string(&idx.value)?;
                let x = self.expr_to_string(&x.value)?;
                format!(
                    "func(__r *{slice}, __i int64, __x {elem}) {{ \
                     if __i < 0 || __i > int64(len(*__r)) {{ \
                     panic(fmt.Sprintf(\"List.insert: index %d out of bounds (len %d)\", __i, len(*__r))) }}; \
                     *__r = append(*__r, __x); \
                     copy((*__r)[__i+1:], (*__r)[__i:]); \
                     (*__r)[__i] = __x }}(&{recv_str}, {i}, {x})"
                )
            }
            "reverse" => format!(
                "func(__r {slice}) {{ \
                 for __i, __j := 0, len(__r)-1; __i < __j; __i, __j = __i+1, __j-1 {{ \
                 __r[__i], __r[__j] = __r[__j], __r[__i] }} }}({recv_str})"
            ),
            "set" => {
                let (Some(idx), Some(x)) = (rest.first(), rest.get(1)) else {
                    return Ok(false);
                };
                self.needs_fmt_import = true;
                let i = self.expr_to_string(&idx.value)?;
                let x = self.expr_to_string(&x.value)?;
                format!(
                    "func(__r {slice}, __i int64, __x {elem}) {{ \
                     if __i < 0 || __i >= int64(len(__r)) {{ \
                     panic(fmt.Sprintf(\"List.set: index %d out of bounds (len %d)\", __i, len(__r))) }}; \
                     __r[__i] = __x }}({recv_str}, {i}, {x})"
                )
            }
            _ => return Ok(false),
        };
        self.buf.push_str(&code);
        Ok(true)
    }

    /// Render a closure (lambda) argument as a typed Go func literal, with its
    /// parameter types pinned to `param_types`, and report the func literal's
    /// inferred return type.
    ///
    /// Used by [`Self::try_emit_list_functional_method`]: a bare `(x) => x * 2`
    /// argument would otherwise emit `func(x interface{}) interface{}`, whose
    /// return does not assign into a concrete `[]int64`. Pinning the params to the
    /// receiver's element type (`[]int64` → `int64`) lets the Go backend infer a
    /// concrete return type, which the caller uses both to size the result slice
    /// and to decide whether a type assertion is needed.
    fn render_typed_closure_go(
        &mut self,
        closure: &AIRNode,
        param_types: &[String],
    ) -> Result<(String, Option<String>), CodegenError> {
        self.render_typed_closure_go_ret(closure, param_types, None)
    }

    /// As [`Self::render_typed_closure_go`], but a `forced_ret` of `Some(ty)` pins
    /// the closure's Go return type (a predicate combinator's `bool`) rather than
    /// inferring it from the body. The returned `Option<String>` ret reflects the
    /// forced type when given, so the caller's result-elem logic agrees.
    fn render_typed_closure_go_ret(
        &mut self,
        closure: &AIRNode,
        param_types: &[String],
        forced_ret: Option<&str>,
    ) -> Result<(String, Option<String>), CodegenError> {
        let ret = if let Some(t) = forced_ret {
            Some(t.to_string())
        } else if let NodeKind::Lambda { params, body } = &closure.kind {
            let saved = self.enter_param_go_types_with_expected(params, Some(param_types));
            // Use the block-tail inference (not the bare-expr one): a `.map`
            // closure whose body is a block / `if` / `match` (e.g.
            // `(t) => { if (t.id == id) { t.complete() } else { t } }`) needs its
            // value tail typed so the result slice is `[]Todo`, not `[]interface{}`
            // (`infer_go_expr_type` alone returns `None` for a block/if body).
            let r = self.infer_block_tail_type(body);
            self.var_go_type = saved;
            r
        } else {
            None
        };
        let prev = self.expected_lambda_param_types.take();
        self.expected_lambda_param_types = Some(param_types.to_vec());
        let prev_forced = self.forced_lambda_ret.take();
        self.forced_lambda_ret = forced_ret.map(str::to_string);
        let code = self.expr_to_string(closure)?;
        self.forced_lambda_ret = prev_forced;
        self.expected_lambda_param_types = prev;
        Ok((code, ret))
    }

    /// The Go element type to use for a `List` combinator's *result* slice, drawn
    /// from the binding's expected type (`current_expected_type`, e.g. `[]string`
    /// → `string`) when it is a slice, else from the closure's inferred return
    /// `cb_ret`, else `interface{}`.
    fn list_result_elem_go(&self, cb_ret: Option<&str>) -> String {
        if let Some(t) = self.current_expected_type.as_deref() {
            if let Some(elem) = t.strip_prefix("[]") {
                return elem.to_string();
            }
        }
        cb_ret.unwrap_or("interface{}").to_string()
    }

    /// Emit a functional (closure-taking) `List` built-in method call to its Go
    /// form.
    ///
    /// Recognised via [`crate::generator::desugared_list_functional_method`]. Go
    /// has neither methods on slices nor a usable `map` selector (`map` is a
    /// keyword), so each combinator lowers to an immediately-invoked func literal
    /// that drives a `for _, __x := range __r` loop, applying the closure
    /// (rendered with its parameter types pinned to the receiver's element type,
    /// via [`Self::render_typed_closure_go`]). The closure is evaluated *once* —
    /// the desugared `recv.map(recv, cb)` shape the generic fall-through emits
    /// otherwise produces `expected selector …, found 'map'` (a parse error,
    /// `map` being reserved) or `.filter undefined`. `map`/`flat_map` size their
    /// result from the binding's expected element type; `fold`/`reduce` thread an
    /// accumulator; `find` returns the tagged Optional runtime; `any`/`all`
    /// short-circuit to `bool`; `for_each` returns nothing.
    fn try_emit_list_functional_method(
        &mut self,
        node: &AIRNode,
        callee: &AIRNode,
        args: &[bock_air::AirArg],
    ) -> Result<bool, CodegenError> {
        let Some((recv, method, rest)) =
            crate::generator::desugared_list_functional_method(node, callee, args)
        else {
            return Ok(false);
        };
        let recv_str = self.expr_to_string(recv)?;
        // Recover the receiver's element type. `list_receiver_elem_go_type`
        // handles direct bindings/literals and the element-preserving chained
        // combinators (`filter`/`find`). A chained `map`/`flat_map` receiver's
        // element is the *closure's return type*, which needs the `&mut self`
        // block-tail inference in `value_list_elem_go_type` — so fall back to it
        // when the cheap `&self` resolver yields nothing. This is the
        // `.filter(..).map(..).map(..)` (Q-go-chained-combinator-typing) case:
        // the outermost `.map`'s `interface{}` element flips to the concrete
        // element threaded through the whole chain.
        let elem = self
            .list_receiver_elem_go_type(recv)
            .or_else(|| self.value_list_elem_go_type(recv))
            .unwrap_or_else(|| "interface{}".to_string());
        let slice = format!("[]{elem}");
        let code = match method {
            "map" => {
                let Some(cb) = rest.first() else {
                    return Ok(false);
                };
                let (f, cb_ret) =
                    self.render_typed_closure_go(&cb.value, std::slice::from_ref(&elem))?;
                let out = self.list_result_elem_go(cb_ret.as_deref());
                // Assert the closure result into the declared element type unless
                // the closure's inferred return type *already* matches it exactly
                // (asserting a concrete value onto its own type is a Go compile
                // error; appending an `interface{}` to a concrete `[]T` without
                // an assertion is also an error). When `out` is `interface{}` no
                // assertion is needed (anything assigns to `interface{}`).
                let push = if out != "interface{}" && cb_ret.as_deref() != Some(out.as_str()) {
                    format!("__out = append(__out, __f(__x).({out}))")
                } else {
                    "__out = append(__out, __f(__x))".to_string()
                };
                format!(
                    "func(__r {slice}) []{out} {{ \
                     __f := {f}; \
                     __out := make([]{out}, 0, len(__r)); \
                     for _, __x := range __r {{ {push} }}; \
                     return __out }}({recv_str})"
                )
            }
            "flat_map" => {
                let Some(cb) = rest.first() else {
                    return Ok(false);
                };
                let (f, cb_ret) =
                    self.render_typed_closure_go(&cb.value, std::slice::from_ref(&elem))?;
                // The closure returns a slice; its element is the result element.
                let out = self.current_expected_type.as_deref().map_or_else(
                    || {
                        cb_ret
                            .as_deref()
                            .and_then(|r| r.strip_prefix("[]"))
                            .unwrap_or("interface{}")
                            .to_string()
                    },
                    |t| t.strip_prefix("[]").unwrap_or("interface{}").to_string(),
                );
                format!(
                    "func(__r {slice}) []{out} {{ \
                     __f := {f}; \
                     __out := make([]{out}, 0, len(__r)); \
                     for _, __x := range __r {{ __out = append(__out, __f(__x)...) }}; \
                     return __out }}({recv_str})"
                )
            }
            "filter" => {
                let Some(cb) = rest.first() else {
                    return Ok(false);
                };
                // The predicate returns `Bool`; pin it so `if __f(__x)` is a Go
                // boolean condition (the body may be a method call / `match` whose
                // Go return type is otherwise erased to `interface{}`).
                let (f, _) = self.render_typed_closure_go_ret(
                    &cb.value,
                    std::slice::from_ref(&elem),
                    Some("bool"),
                )?;
                format!(
                    "func(__r {slice}) {slice} {{ \
                     __f := {f}; \
                     __out := make({slice}, 0, len(__r)); \
                     for _, __x := range __r {{ if __f(__x) {{ __out = append(__out, __x) }} }}; \
                     return __out }}({recv_str})"
                )
            }
            "find" => {
                let Some(cb) = rest.first() else {
                    return Ok(false);
                };
                let (f, _) = self.render_typed_closure_go_ret(
                    &cb.value,
                    std::slice::from_ref(&elem),
                    Some("bool"),
                )?;
                format!(
                    "func(__r {slice}) __bockOption {{ \
                     __f := {f}; \
                     for _, __x := range __r {{ if __f(__x) {{ return __bockSome(__x) }} }}; \
                     return __bockNone }}({recv_str})"
                )
            }
            "any" | "all" => {
                let Some(cb) = rest.first() else {
                    return Ok(false);
                };
                let (f, _) = self.render_typed_closure_go_ret(
                    &cb.value,
                    std::slice::from_ref(&elem),
                    Some("bool"),
                )?;
                // `any`: true if some element matches; `all`: true unless one fails.
                if method == "any" {
                    format!(
                        "func(__r {slice}) bool {{ \
                         __f := {f}; \
                         for _, __x := range __r {{ if __f(__x) {{ return true }} }}; \
                         return false }}({recv_str})"
                    )
                } else {
                    format!(
                        "func(__r {slice}) bool {{ \
                         __f := {f}; \
                         for _, __x := range __r {{ if !__f(__x) {{ return false }} }}; \
                         return true }}({recv_str})"
                    )
                }
            }
            "reduce" => {
                let Some(cb) = rest.first() else {
                    return Ok(false);
                };
                // No seed: first element is the accumulator. Accumulator type is
                // the element type.
                let (f, _) =
                    self.render_typed_closure_go(&cb.value, &[elem.clone(), elem.clone()])?;
                format!(
                    "func(__r {slice}) {elem} {{ \
                     __f := {f}; \
                     __acc := __r[0]; \
                     for __i := 1; __i < len(__r); __i++ {{ __acc = __f(__acc, __r[__i]) }}; \
                     return __acc }}({recv_str})"
                )
            }
            "fold" => {
                let (Some(init), Some(cb)) = (rest.first(), rest.get(1)) else {
                    return Ok(false);
                };
                let acc_ty = self
                    .infer_go_expr_type(&init.value)
                    .or_else(|| self.current_expected_type.clone())
                    .unwrap_or_else(|| "interface{}".to_string());
                let init_str = self.expr_to_string(&init.value)?;
                let (f, _) =
                    self.render_typed_closure_go(&cb.value, &[acc_ty.clone(), elem.clone()])?;
                format!(
                    "func(__r {slice}, __acc {acc_ty}) {acc_ty} {{ \
                     __f := {f}; \
                     for _, __x := range __r {{ __acc = __f(__acc, __x) }}; \
                     return __acc }}({recv_str}, {init_str})"
                )
            }
            "for_each" => {
                let Some(cb) = rest.first() else {
                    return Ok(false);
                };
                let (f, _) =
                    self.render_typed_closure_go(&cb.value, std::slice::from_ref(&elem))?;
                format!(
                    "func(__r {slice}) {{ \
                     __f := {f}; \
                     for _, __x := range __r {{ __f(__x) }} }}({recv_str})"
                )
            }
            _ => return Ok(false),
        };
        self.buf.push_str(&code);
        Ok(true)
    }

    /// Emit a built-in `Map[K, V]` method call to its Go form (native
    /// `map[K]V`, building on P3-α's typed map literals/decls).
    ///
    /// Recognised via [`crate::generator::desugared_map_method`] (gated on
    /// `recv_kind = "Map"`) and wired *before* [`Self::try_emit_list_method`],
    /// so a `Map` receiver's `get`/`contains_key`/`len` no longer route through
    /// the `List` path (which passed the `map[K]V` where a `[]interface{}` slice
    /// closure expected, and cast the key to `int64`). `get` uses the Go
    /// comma-ok form (`__v, __ok := __m[__k]`) → the `__bockSome`/`__bockNone`
    /// Optional runtime. Mutating methods (`set`/`delete`/`merge`) copy then
    /// mutate and return the new map (Bock map value semantics). The inline
    /// closures are typed `map[K]V` from the receiver's declared element types
    /// (recovered from [`Self::map_receiver_kv_go_types`]; `interface{}`
    /// fallback when unknown). Returns `true` if handled.
    fn try_emit_map_method(
        &mut self,
        node: &AIRNode,
        callee: &AIRNode,
        args: &[bock_air::AirArg],
    ) -> Result<bool, CodegenError> {
        let Some((recv, method, rest)) = crate::generator::desugared_map_method(node, callee, args)
        else {
            return Ok(false);
        };
        let recv_str = self.expr_to_string(recv)?;
        let (k_ty, v_ty) = self
            .map_receiver_kv_go_types(recv)
            .unwrap_or_else(|| ("interface{}".to_string(), "interface{}".to_string()));
        let map_ty = format!("map[{k_ty}]{v_ty}");
        let code = match method {
            "len" | "length" | "count" => format!("int64(len({recv_str}))"),
            "is_empty" => format!("(len({recv_str}) == 0)"),
            "contains_key" => {
                let Some(k) = rest.first() else {
                    return Ok(false);
                };
                let k = self.expr_to_string(&k.value)?;
                format!(
                    "func(__m {map_ty}, __k {k_ty}) bool {{ _, __ok := __m[__k]; return __ok }}\
                     ({recv_str}, {k})"
                )
            }
            "get" => {
                let Some(k) = rest.first() else {
                    return Ok(false);
                };
                let k = self.expr_to_string(&k.value)?;
                format!(
                    "func(__m {map_ty}, __k {k_ty}) __bockOption {{ \
                     if __v, __ok := __m[__k]; __ok {{ return __bockSome(__v) }}; \
                     return __bockNone }}({recv_str}, {k})"
                )
            }
            "set" => {
                let (Some(k), Some(v)) = (rest.first(), rest.get(1)) else {
                    return Ok(false);
                };
                let k = self.expr_to_string(&k.value)?;
                let v = self.expr_to_string(&v.value)?;
                format!(
                    "func(__m {map_ty}, __k {k_ty}, __v {v_ty}) {map_ty} {{ \
                     __r := make({map_ty}, len(__m)+1); \
                     for __mk, __mv := range __m {{ __r[__mk] = __mv }}; \
                     __r[__k] = __v; return __r }}({recv_str}, {k}, {v})"
                )
            }
            "delete" => {
                let Some(k) = rest.first() else {
                    return Ok(false);
                };
                let k = self.expr_to_string(&k.value)?;
                format!(
                    "func(__m {map_ty}, __k {k_ty}) {map_ty} {{ \
                     __r := make({map_ty}, len(__m)); \
                     for __mk, __mv := range __m {{ __r[__mk] = __mv }}; \
                     delete(__r, __k); return __r }}({recv_str}, {k})"
                )
            }
            "merge" => {
                let Some(o) = rest.first() else {
                    return Ok(false);
                };
                let o = self.expr_to_string(&o.value)?;
                format!(
                    "func(__m {map_ty}, __o {map_ty}) {map_ty} {{ \
                     __r := make({map_ty}, len(__m)+len(__o)); \
                     for __mk, __mv := range __m {{ __r[__mk] = __mv }}; \
                     for __ok, __ov := range __o {{ __r[__ok] = __ov }}; \
                     return __r }}({recv_str}, {o})"
                )
            }
            "filter" => {
                let Some(f) = rest.first() else {
                    return Ok(false);
                };
                let f = self.expr_to_string(&f.value)?;
                format!(
                    "func(__m {map_ty}, __f func({k_ty}, {v_ty}) bool) {map_ty} {{ \
                     __r := make({map_ty}); \
                     for __mk, __mv := range __m {{ if __f(__mk, __mv) {{ __r[__mk] = __mv }} }}; \
                     return __r }}({recv_str}, {f})"
                )
            }
            "keys" => format!(
                "func(__m {map_ty}) []{k_ty} {{ \
                 __r := make([]{k_ty}, 0, len(__m)); \
                 for __mk := range __m {{ __r = append(__r, __mk) }}; \
                 return __r }}({recv_str})"
            ),
            "values" => format!(
                "func(__m {map_ty}) []{v_ty} {{ \
                 __r := make([]{v_ty}, 0, len(__m)); \
                 for _, __mv := range __m {{ __r = append(__r, __mv) }}; \
                 return __r }}({recv_str})"
            ),
            "entries" | "to_list" => format!(
                "func(__m {map_ty}) [][2]interface{{}} {{ \
                 __r := make([][2]interface{{}}, 0, len(__m)); \
                 for __mk, __mv := range __m {{ __r = append(__r, [2]interface{{}}{{__mk, __mv}}) }}; \
                 return __r }}({recv_str})"
            ),
            "for_each" => {
                let Some(f) = rest.first() else {
                    return Ok(false);
                };
                let f = self.expr_to_string(&f.value)?;
                format!(
                    "func(__m {map_ty}, __f func({k_ty}, {v_ty})) {{ \
                     for __mk, __mv := range __m {{ __f(__mk, __mv) }} }}({recv_str}, {f})"
                )
            }
            _ => return Ok(false),
        };
        self.buf.push_str(&code);
        Ok(true)
    }

    /// Emit a built-in `Set[E]` method call to its Go form (native
    /// `map[E]struct{}`, building on P3-α's typed set literals/decls).
    ///
    /// Recognised via [`crate::generator::desugared_set_method`] (gated on
    /// `recv_kind = "Set"`) and wired *before* [`Self::try_emit_list_method`].
    /// `contains` is a comma-ok membership test; the set algebra builds new
    /// `map[E]struct{}` values. Mutating methods (`add`/`remove`) copy then
    /// mutate and return the new set. The inline closures are typed `map[E]
    /// struct{}` from the receiver's declared element type
    /// ([`Self::set_receiver_elem_go_type`]; `interface{}` fallback). Returns
    /// `true` if handled.
    fn try_emit_set_method(
        &mut self,
        node: &AIRNode,
        callee: &AIRNode,
        args: &[bock_air::AirArg],
    ) -> Result<bool, CodegenError> {
        let Some((recv, method, rest)) = crate::generator::desugared_set_method(node, callee, args)
        else {
            return Ok(false);
        };
        let recv_str = self.expr_to_string(recv)?;
        let e_ty = self
            .set_receiver_elem_go_type(recv)
            .unwrap_or_else(|| "interface{}".to_string());
        let set_ty = format!("map[{e_ty}]struct{{}}");
        let code = match method {
            "len" | "length" | "count" => format!("int64(len({recv_str}))"),
            "is_empty" => format!("(len({recv_str}) == 0)"),
            "contains" => {
                let Some(x) = rest.first() else {
                    return Ok(false);
                };
                let x = self.expr_to_string(&x.value)?;
                format!(
                    "func(__s {set_ty}, __x {e_ty}) bool {{ _, __ok := __s[__x]; return __ok }}\
                     ({recv_str}, {x})"
                )
            }
            "add" => {
                let Some(x) = rest.first() else {
                    return Ok(false);
                };
                let x = self.expr_to_string(&x.value)?;
                format!(
                    "func(__s {set_ty}, __x {e_ty}) {set_ty} {{ \
                     __r := make({set_ty}, len(__s)+1); \
                     for __sk := range __s {{ __r[__sk] = struct{{}}{{}} }}; \
                     __r[__x] = struct{{}}{{}}; return __r }}({recv_str}, {x})"
                )
            }
            "remove" => {
                let Some(x) = rest.first() else {
                    return Ok(false);
                };
                let x = self.expr_to_string(&x.value)?;
                format!(
                    "func(__s {set_ty}, __x {e_ty}) {set_ty} {{ \
                     __r := make({set_ty}, len(__s)); \
                     for __sk := range __s {{ __r[__sk] = struct{{}}{{}} }}; \
                     delete(__r, __x); return __r }}({recv_str}, {x})"
                )
            }
            "union" => {
                let Some(o) = rest.first() else {
                    return Ok(false);
                };
                let o = self.expr_to_string(&o.value)?;
                format!(
                    "func(__a {set_ty}, __b {set_ty}) {set_ty} {{ \
                     __r := make({set_ty}, len(__a)+len(__b)); \
                     for __k := range __a {{ __r[__k] = struct{{}}{{}} }}; \
                     for __k := range __b {{ __r[__k] = struct{{}}{{}} }}; \
                     return __r }}({recv_str}, {o})"
                )
            }
            "intersection" => {
                let Some(o) = rest.first() else {
                    return Ok(false);
                };
                let o = self.expr_to_string(&o.value)?;
                format!(
                    "func(__a {set_ty}, __b {set_ty}) {set_ty} {{ \
                     __r := make({set_ty}); \
                     for __k := range __a {{ if _, __ok := __b[__k]; __ok {{ \
                     __r[__k] = struct{{}}{{}} }} }}; \
                     return __r }}({recv_str}, {o})"
                )
            }
            "difference" => {
                let Some(o) = rest.first() else {
                    return Ok(false);
                };
                let o = self.expr_to_string(&o.value)?;
                format!(
                    "func(__a {set_ty}, __b {set_ty}) {set_ty} {{ \
                     __r := make({set_ty}); \
                     for __k := range __a {{ if _, __ok := __b[__k]; !__ok {{ \
                     __r[__k] = struct{{}}{{}} }} }}; \
                     return __r }}({recv_str}, {o})"
                )
            }
            "is_subset" => {
                let Some(o) = rest.first() else {
                    return Ok(false);
                };
                let o = self.expr_to_string(&o.value)?;
                format!(
                    "func(__a {set_ty}, __b {set_ty}) bool {{ \
                     for __k := range __a {{ if _, __ok := __b[__k]; !__ok {{ return false }} }}; \
                     return true }}({recv_str}, {o})"
                )
            }
            "is_superset" => {
                let Some(o) = rest.first() else {
                    return Ok(false);
                };
                let o = self.expr_to_string(&o.value)?;
                format!(
                    "func(__a {set_ty}, __b {set_ty}) bool {{ \
                     for __k := range __b {{ if _, __ok := __a[__k]; !__ok {{ return false }} }}; \
                     return true }}({recv_str}, {o})"
                )
            }
            "filter" => {
                let Some(f) = rest.first() else {
                    return Ok(false);
                };
                let f = self.expr_to_string(&f.value)?;
                format!(
                    "func(__s {set_ty}, __f func({e_ty}) bool) {set_ty} {{ \
                     __r := make({set_ty}); \
                     for __k := range __s {{ if __f(__k) {{ __r[__k] = struct{{}}{{}} }} }}; \
                     return __r }}({recv_str}, {f})"
                )
            }
            "map" => {
                let Some(f) = rest.first() else {
                    return Ok(false);
                };
                let f = self.expr_to_string(&f.value)?;
                format!(
                    "func(__s {set_ty}, __f func({e_ty}) {e_ty}) {set_ty} {{ \
                     __r := make({set_ty}); \
                     for __k := range __s {{ __r[__f(__k)] = struct{{}}{{}} }}; \
                     return __r }}({recv_str}, {f})"
                )
            }
            "to_list" => format!(
                "func(__s {set_ty}) []{e_ty} {{ \
                 __r := make([]{e_ty}, 0, len(__s)); \
                 for __k := range __s {{ __r = append(__r, __k) }}; \
                 return __r }}({recv_str})"
            ),
            "for_each" => {
                let Some(f) = rest.first() else {
                    return Ok(false);
                };
                let f = self.expr_to_string(&f.value)?;
                format!(
                    "func(__s {set_ty}, __f func({e_ty})) {{ \
                     for __k := range __s {{ __f(__k) }} }}({recv_str}, {f})"
                )
            }
            _ => return Ok(false),
        };
        self.buf.push_str(&code);
        Ok(true)
    }

    /// Recognise `Duration.xxx(...)` / `Instant.xxx(...)` associated-function
    /// calls and emit inline Go code. Duration values are `int64` nanoseconds
    /// (matching `time.Duration`); Instants are `time.Time` (monotonic via
    /// `time.Now()`).
    fn try_emit_time_assoc_call(
        &mut self,
        callee: &AIRNode,
        args: &[bock_air::AirArg],
    ) -> Result<bool, CodegenError> {
        let NodeKind::FieldAccess { object, field } = &callee.kind else {
            return Ok(false);
        };
        let NodeKind::Identifier { name: type_name } = &object.kind else {
            return Ok(false);
        };
        let arg_strs: Vec<String> = args
            .iter()
            .map(|a| self.expr_to_string(&a.value))
            .collect::<Result<_, _>>()?;
        let arg0 = || arg_strs.first().cloned().unwrap_or_default();
        let code = match (type_name.name.as_str(), field.name.as_str()) {
            ("Duration", "zero") => "int64(0)".to_string(),
            ("Duration", "nanos") => format!("int64({})", arg0()),
            ("Duration", "micros") => format!("(int64({}) * 1000)", arg0()),
            ("Duration", "millis") => format!("(int64({}) * 1000000)", arg0()),
            ("Duration", "seconds") => format!("(int64({}) * 1000000000)", arg0()),
            ("Duration", "minutes") => format!("(int64({}) * 60000000000)", arg0()),
            ("Duration", "hours") => format!("(int64({}) * 3600000000000)", arg0()),
            ("Instant", "now") => {
                // Route through an installed `Clock` handler's `now_monotonic`
                // op if one is in scope; otherwise emit the host primitive.
                if let Some(handler) = self.clock_handler_var() {
                    format!("{handler}.{}()", to_pascal_case("now_monotonic"))
                } else {
                    self.needs_time_import = true;
                    "time.Now()".to_string()
                }
            }
            _ => return Ok(false),
        };
        self.buf.push_str(&code);
        Ok(true)
    }

    /// Lower a primitive trait-bridge method call (`compare`/`eq`/`to_string`/
    /// `display` on a primitive receiver) to its Go form.
    ///
    /// `(1).compare(2)` resolves to `Ordering`; this routes it through the
    /// generic Ordering-runtime helper `__bockCompare`, returning a
    /// `__bockOrdering` constant the value-switch / construction sides use. `eq`
    /// → `==`; `to_string`/`display` → `fmt.Sprintf("%v", x)`.
    /// Lower a desugared `String` built-in method call (`recv_kind =
    /// "Primitive:String"`) to its native Go string op. Wired into the `Call`
    /// arm *before* `try_emit_list_method` so a String receiver's
    /// `len`/`contains`/`is_empty` dispatch here, not through the List path —
    /// which is the misrouting that broke `String.contains` (the `[]interface{}`
    /// `fmt.Sprintf("%v", …)` linear scan failed to compile against a `string`).
    ///
    /// `len` is the Unicode SCALAR count (`utf8.RuneCountInString(s)`) per spec
    /// §18.3 — Go's `len(s)` is the BYTE length, so `byte_len` maps to it.
    /// `replace` replaces ALL occurrences (`strings.ReplaceAll`). `split` returns
    /// `[]string`, which the read-only `List` built-ins (`len`/…) accept.
    ///
    /// Gated on `recv_kind = "Primitive:String"` directly (not the cross-backend
    /// [`crate::generator::desugared_string_method`] subset) so Go can lower the
    /// wider resolved String surface — `slice`/`substring`/`char_at`/`index_of`/
    /// `repeat`/`reverse`/`trim_start`/`trim_end` — to native ops, matching the
    /// Rust backend. Indexing/`reverse` are **rune-aware** (`[]rune(s)`) so the
    /// scalar-index semantics of §18.3 hold for multibyte input — a plain `s[i]`
    /// would be a byte. `char_at`/`index_of` build the tagged `Optional` runtime
    /// (`__bockSome(v)` / `__bockNone`); `index_of` converts `strings.Index`'s
    /// byte offset to a rune index.
    fn try_emit_string_method(
        &mut self,
        node: &AIRNode,
        callee: &AIRNode,
        args: &[bock_air::AirArg],
    ) -> Result<bool, CodegenError> {
        if crate::generator::primitive_recv_kind(node) != Some("String") {
            return Ok(false);
        }
        let Some((recv, field, rest)) = crate::generator::desugared_self_call(callee, args) else {
            return Ok(false);
        };
        let method = field.name.as_str();
        let recv_str = self.expr_to_string(recv)?;
        let arg0 = |this: &mut Self| -> Result<Option<String>, CodegenError> {
            rest.first()
                .map(|a| this.expr_to_string(&a.value))
                .transpose()
        };
        let code = match method {
            "len" | "length" | "count" => {
                self.needs_utf8_import = true;
                format!("int64(utf8.RuneCountInString({recv_str}))")
            }
            "byte_len" => format!("int64(len({recv_str}))"),
            "is_empty" => format!("(len({recv_str}) == 0)"),
            "to_upper" => {
                self.needs_strings_import = true;
                format!("strings.ToUpper({recv_str})")
            }
            "to_lower" => {
                self.needs_strings_import = true;
                format!("strings.ToLower({recv_str})")
            }
            "trim" => {
                self.needs_strings_import = true;
                format!("strings.TrimSpace({recv_str})")
            }
            "trim_start" => {
                self.needs_strings_import = true;
                self.needs_unicode_import = true;
                format!("strings.TrimLeftFunc({recv_str}, unicode.IsSpace)")
            }
            "trim_end" => {
                self.needs_strings_import = true;
                self.needs_unicode_import = true;
                format!("strings.TrimRightFunc({recv_str}, unicode.IsSpace)")
            }
            // `reverse` reverses by Unicode scalar (rune), not byte.
            "reverse" => format!(
                "func(__r []rune) string {{ for __i, __j := 0, len(__r)-1; __i < __j; __i, __j = __i+1, __j-1 {{ __r[__i], __r[__j] = __r[__j], __r[__i] }}; return string(__r) }}([]rune({recv_str}))"
            ),
            "to_string" | "display" => format!("({recv_str})"),
            "repeat" => {
                let Some(n) = arg0(self)? else {
                    return Ok(false);
                };
                self.needs_strings_import = true;
                format!("strings.Repeat({recv_str}, int({n}))")
            }
            "contains" => {
                let Some(p) = arg0(self)? else {
                    return Ok(false);
                };
                self.needs_strings_import = true;
                format!("strings.Contains({recv_str}, {p})")
            }
            "starts_with" => {
                let Some(p) = arg0(self)? else {
                    return Ok(false);
                };
                self.needs_strings_import = true;
                format!("strings.HasPrefix({recv_str}, {p})")
            }
            "ends_with" => {
                let Some(p) = arg0(self)? else {
                    return Ok(false);
                };
                self.needs_strings_import = true;
                format!("strings.HasSuffix({recv_str}, {p})")
            }
            "replace" => {
                let Some(from) = arg0(self)? else {
                    return Ok(false);
                };
                let Some(to) = rest
                    .get(1)
                    .map(|a| self.expr_to_string(&a.value))
                    .transpose()?
                else {
                    return Ok(false);
                };
                self.needs_strings_import = true;
                format!("strings.ReplaceAll({recv_str}, {from}, {to})")
            }
            "split" => {
                let Some(sep) = arg0(self)? else {
                    return Ok(false);
                };
                self.needs_strings_import = true;
                format!("strings.Split({recv_str}, {sep})")
            }
            // `slice`/`substring(start, end)`: scalar-index half-open substring
            // (spec §18.3). Indexed over `[]rune` so the indices are scalar
            // positions; `start`/`end` are clamped into range so out-of-bounds
            // does not panic.
            "slice" | "substring" => {
                let Some(start) = arg0(self)? else {
                    return Ok(false);
                };
                let Some(end) = rest
                    .get(1)
                    .map(|a| self.expr_to_string(&a.value))
                    .transpose()?
                else {
                    return Ok(false);
                };
                format!(
                    "func(__r []rune, __a, __b int) string {{ if __a < 0 {{ __a = 0 }}; if __b > len(__r) {{ __b = len(__r) }}; if __a > __b {{ __a = __b }}; return string(__r[__a:__b]) }}([]rune({recv_str}), int({start}), int({end}))"
                )
            }
            // `char_at(i)` returns `Optional[Char]` — `None` when out of range. A
            // Bock `Char` is a Go `rune`.
            "char_at" => {
                let Some(i) = arg0(self)? else {
                    return Ok(false);
                };
                format!(
                    "func(__r []rune, __i int) __bockOption {{ if __i >= 0 && __i < len(__r) {{ return __bockSome(__r[__i]) }}; return __bockNone }}([]rune({recv_str}), int({i}))"
                )
            }
            // `index_of(needle)` returns `Optional[Int]` — the scalar index of the
            // first match, or `None`. `strings.Index` yields a *byte* offset, so
            // convert it to a rune index via the prefix rune count.
            "index_of" => {
                let Some(p) = arg0(self)? else {
                    return Ok(false);
                };
                self.needs_strings_import = true;
                self.needs_utf8_import = true;
                format!(
                    "func(__s, __p string) __bockOption {{ __b := strings.Index(__s, __p); if __b < 0 {{ return __bockNone }}; return __bockSome(int64(utf8.RuneCountInString(__s[:__b]))) }}({recv_str}, {p})"
                )
            }
            _ => return Ok(false),
        };
        self.buf.push_str(&code);
        Ok(true)
    }

    /// Q-prim-assoc: lower a primitive associated-conversion call
    /// (`Float.from(x)` / `Int.try_from(s)` / `String.from(c)`) to Go's native
    /// conversion. `from` is a Go type conversion (`float64(x)` / `int64(x)` /
    /// `string(x)`; a Bock `Char` is a `rune`, so `string(rune)` yields the
    /// single-character string). `try_from` parses via `strconv.Parse{Int,Float}`
    /// inside an IIFE returning the `__bockResult` runtime struct, the `Err`
    /// payload built with the in-package `ConvertErrorFn` constructor (Go emits
    /// everything into `package main`, so it is always visible). Returns `true`
    /// when handled.
    fn try_emit_primitive_conversion(
        &mut self,
        node: &AIRNode,
        callee: &AIRNode,
        args: &[bock_air::AirArg],
    ) -> Result<bool, CodegenError> {
        let Some((target, method, arg)) =
            crate::generator::primitive_conversion_call(node, callee, args)
        else {
            return Ok(false);
        };
        let arg_str = self.expr_to_string(arg)?;
        let code = match (target, method) {
            ("Float", "from") => format!("float64({arg_str})"),
            ("Int", "from") => format!("int64({arg_str})"),
            ("String", "from") => format!("string({arg_str})"),
            ("Int", "try_from") => {
                self.needs_strconv_import = true;
                self.needs_strings_import = true;
                format!(
                    "func(__s string) __bockResult {{ __v, __err := strconv.ParseInt(strings.TrimSpace(__s), 10, 64); \
                     if __err != nil {{ return __bockErr(ConvertErrorFn(\"cannot parse \\\"\" + __s + \"\\\" as Int\")) }}; \
                     return __bockOk(__v) }}({arg_str})"
                )
            }
            ("Float", "try_from") => {
                self.needs_strconv_import = true;
                self.needs_strings_import = true;
                format!(
                    "func(__s string) __bockResult {{ __v, __err := strconv.ParseFloat(strings.TrimSpace(__s), 64); \
                     if __err != nil {{ return __bockErr(ConvertErrorFn(\"cannot parse \\\"\" + __s + \"\\\" as Float\")) }}; \
                     return __bockOk(__v) }}({arg_str})"
                )
            }
            _ => return Ok(false),
        };
        self.buf.push_str(&code);
        Ok(true)
    }

    /// Lower a desugared numeric/`Char`/`Bool` primitive method (`recv_kind =
    /// "Primitive:Int" | "Primitive:Float" | "Primitive:Char" | "Primitive:Bool"`)
    /// to its native Go form. Covers the conversion and math methods the checker
    /// resolves on the scalar primitives — `to_float`/`to_int`/`abs`/`min`/`max`/
    /// `clamp`/`floor`/`ceil`/`round`/`sqrt`/… . Wired into the `Call` arm
    /// alongside [`Self::try_emit_string_method`], before the generic
    /// desugared-self-call fall-through (which would emit `n.toFloat(n)`).
    /// `math.*` operates on `float64`, so the `Float` math methods round-trip the
    /// `float64` receiver through `math`. `compare`/`eq`/`to_string`/`display`/
    /// `hash_code` stay on the primitive *bridge* path. A Bock `Int` is a Go
    /// `int64`, `Float` a `float64`, `Char` a `rune`.
    fn try_emit_numeric_method(
        &mut self,
        node: &AIRNode,
        callee: &AIRNode,
        args: &[bock_air::AirArg],
    ) -> Result<bool, CodegenError> {
        let prim = match crate::generator::primitive_recv_kind(node) {
            Some(p @ ("Int" | "Float" | "Char" | "Bool")) => p,
            _ => return Ok(false),
        };
        let Some((recv, field, rest)) = crate::generator::desugared_self_call(callee, args) else {
            return Ok(false);
        };
        let method = field.name.as_str();
        let recv_str = self.expr_to_string(recv)?;
        let arg = |this: &mut Self, i: usize| -> Result<Option<String>, CodegenError> {
            rest.get(i)
                .map(|a| this.expr_to_string(&a.value))
                .transpose()
        };
        let code = match (prim, method) {
            // Conversions. `Float.to_int` truncates toward zero via `math.Trunc`,
            // which also yields a *runtime* (non-constant) float64 — Go rejects a
            // direct `int64(3.9)` on a literal receiver (an untyped/typed float
            // *constant* not exactly representable as an int).
            ("Int", "to_float") => format!("float64({recv_str})"),
            ("Float", "to_int") => {
                self.needs_math_import = true;
                format!("int64(math.Trunc({recv_str}))")
            }
            ("Char", "to_int") => format!("int64({recv_str})"),
            ("Bool", "to_int") => {
                format!("func(__b bool) int64 {{ if __b {{ return 1 }}; return 0 }}({recv_str})")
            }
            // Int math. Go 1.21+ has builtin `min`/`max`; `abs` is via a cast to
            // float64 and back to keep it inline.
            ("Int", "abs") => {
                self.needs_math_import = true;
                format!("int64(math.Abs(float64({recv_str})))")
            }
            ("Int" | "Float", "min") => {
                let Some(o) = arg(self, 0)? else {
                    return Ok(false);
                };
                format!("min({recv_str}, {o})")
            }
            ("Int" | "Float", "max") => {
                let Some(o) = arg(self, 0)? else {
                    return Ok(false);
                };
                format!("max({recv_str}, {o})")
            }
            ("Int" | "Float", "clamp") => {
                let (Some(lo), Some(hi)) = (arg(self, 0)?, arg(self, 1)?) else {
                    return Ok(false);
                };
                format!("min(max({recv_str}, {lo}), {hi})")
            }
            ("Int", "shift_left") => {
                let Some(o) = arg(self, 0)? else {
                    return Ok(false);
                };
                format!("(({recv_str}) << ({o}))")
            }
            ("Int", "shift_right") => {
                let Some(o) = arg(self, 0)? else {
                    return Ok(false);
                };
                format!("(({recv_str}) >> ({o}))")
            }
            // Float math (`math.*` works on `float64`).
            ("Float", "abs") => {
                self.needs_math_import = true;
                format!("math.Abs({recv_str})")
            }
            ("Float", "floor") => {
                self.needs_math_import = true;
                format!("math.Floor({recv_str})")
            }
            ("Float", "ceil") => {
                self.needs_math_import = true;
                format!("math.Ceil({recv_str})")
            }
            ("Float", "round") => {
                self.needs_math_import = true;
                format!("math.Round({recv_str})")
            }
            ("Float", "sqrt") => {
                self.needs_math_import = true;
                format!("math.Sqrt({recv_str})")
            }
            ("Float", "is_nan") => {
                self.needs_math_import = true;
                format!("math.IsNaN({recv_str})")
            }
            ("Float", "is_infinite") => {
                self.needs_math_import = true;
                format!("math.IsInf({recv_str}, 0)")
            }
            // Bool.
            ("Bool", "negate") => format!("(!({recv_str}))"),
            // Char (a Go `rune`).
            ("Char", "to_upper") => {
                self.needs_unicode_import = true;
                format!("unicode.ToUpper({recv_str})")
            }
            ("Char", "to_lower") => {
                self.needs_unicode_import = true;
                format!("unicode.ToLower({recv_str})")
            }
            ("Char", "is_alpha") => {
                self.needs_unicode_import = true;
                format!("unicode.IsLetter({recv_str})")
            }
            ("Char", "is_digit") => {
                self.needs_unicode_import = true;
                format!("unicode.IsDigit({recv_str})")
            }
            ("Char", "is_whitespace") => {
                self.needs_unicode_import = true;
                format!("unicode.IsSpace({recv_str})")
            }
            _ => return Ok(false),
        };
        self.buf.push_str(&code);
        Ok(true)
    }

    fn try_emit_primitive_bridge(
        &mut self,
        node: &AIRNode,
        callee: &AIRNode,
        args: &[bock_air::AirArg],
    ) -> Result<bool, CodegenError> {
        let Some((recv, method, rest, prim)) =
            crate::generator::primitive_bridge_call(node, callee, args)
        else {
            return Ok(false);
        };
        // A concrete primitive receiver uses the typed `__bockCompare` helper.
        self.emit_bridge_method(recv, method, rest, false, Some(prim))
    }

    /// Lower a sealed-core-trait bridge method on a *bounded generic type
    /// variable* (`a.eq(b)` / `a.compare(b)` inside `eq_check[T: Equatable]`) to
    /// its Go form (GAP-C). The generic analogue of
    /// [`Self::try_emit_primitive_bridge`]: the `[T Equatable]` bound is rewritten
    /// to Go's built-in constraint (`comparable` / `__bockOrdered`) at the
    /// signature, so `==` and the inline ordering comparison type-check. `compare`
    /// uses an inline comparison (not the typed `__bockCompare` helper, whose
    /// named constraint a `T __bockOrdered` does not satisfy). Fires only when the
    /// bound trait is sealed-core and NOT a user-declared trait.
    fn try_emit_trait_bound_bridge(
        &mut self,
        node: &AIRNode,
        callee: &AIRNode,
        args: &[bock_air::AirArg],
    ) -> Result<bool, CodegenError> {
        let Some((recv, method, rest, _tr)) =
            crate::generator::trait_bound_bridge_call(node, callee, args, &self.trait_decls)
        else {
            return Ok(false);
        };
        // A bounded *generic* type var has no concrete primitive kind; the
        // Char-display special-case below does not apply.
        self.emit_bridge_method(recv, method, rest, true, None)
    }

    /// Shared body of the primitive / trait-bound bridges. `generic` selects the
    /// generic-bound lowering for `compare`: an inline `if a < b … ` expression
    /// producing an `__bockOrdering` (the typed `__bockCompare` helper's named
    /// constraint is not satisfied by a `T __bockOrdered`-bounded type var). `eq`
    /// (`==`) and `to_string`/`display` (`fmt.Sprintf`) are identical either way.
    fn emit_bridge_method(
        &mut self,
        recv: &AIRNode,
        method: &str,
        rest: &[bock_air::AirArg],
        generic: bool,
        recv_prim: Option<&str>,
    ) -> Result<bool, CodegenError> {
        let recv_str = self.expr_to_string(recv)?;
        let code = match method {
            "compare" => {
                let Some(other) = rest.first() else {
                    return Ok(false);
                };
                let other = self.expr_to_string(&other.value)?;
                if generic {
                    // The Ordering runtime (gated on `"compare"` appearing in the
                    // module AST) is already emitted at module top, so `Less`/
                    // `Equal`/`Greater`/`__bockOrdering` are in scope here.
                    format!(
                        "func() __bockOrdering {{ if ({recv_str}) < ({other}) {{ return Less }}; \
                         if ({recv_str}) == ({other}) {{ return Equal }}; return Greater }}()"
                    )
                } else {
                    format!("__bockCompare({recv_str}, {other})")
                }
            }
            "eq" => {
                let Some(other) = rest.first() else {
                    return Ok(false);
                };
                let other = self.expr_to_string(&other.value)?;
                format!("(({recv_str}) == ({other}))")
            }
            "to_string" | "display" => {
                // A `Char` lowers to Go `rune` (an alias of `int32`); `fmt.Sprintf
                // ("%v", r)` would print its integer code point ('A' → "65"), not
                // the character. `string(rune)` renders the scalar as its UTF-8
                // text. Every other primitive keeps the `%v` formatting (an int
                // prints as its digits, a float/bool/string as itself).
                if recv_prim == Some("Char") {
                    format!("string({recv_str})")
                } else {
                    self.needs_fmt_import = true;
                    format!("fmt.Sprintf(\"%v\", {recv_str})")
                }
            }
            _ => return Ok(false),
        };
        self.buf.push_str(&code);
        Ok(true)
    }

    /// Recognise desugared method calls on Duration/Instant values.
    ///
    /// `node` is the full `Call` AIR node, consulted only to *exclude* primitive
    /// receivers: [`is_time_method_name`] alone is ambiguous (`abs` is both
    /// `Duration.abs` and `Int.abs`/`Float.abs`), so when the checker has stamped
    /// `recv_kind = "Primitive:<Ty>"` on the call this is a numeric method, not a
    /// time method — bail so [`Self::try_emit_numeric_method`] handles it.
    fn try_emit_time_desugared_method(
        &mut self,
        node: &AIRNode,
        callee: &AIRNode,
        args: &[bock_air::AirArg],
    ) -> Result<bool, CodegenError> {
        if crate::generator::primitive_recv_kind(node).is_some() {
            return Ok(false);
        }
        let NodeKind::FieldAccess { object, field } = &callee.kind else {
            return Ok(false);
        };
        if let NodeKind::Identifier { name } = &object.kind {
            if matches!(name.name.as_str(), "Duration" | "Instant") {
                return Ok(false);
            }
        }
        if !is_time_method_name(&field.name) {
            return Ok(false);
        }
        let remaining: Vec<bock_air::AirArg> = args.iter().skip(1).cloned().collect();
        self.try_emit_time_method(object, &field.name, &remaining)
    }

    /// Recognise `Channel.new()`, `spawn(...)`, and method calls on a
    /// channel value. Emits calls into the Go runtime helper code
    /// (injected at top-of-module).
    fn try_emit_concurrency_call(
        &mut self,
        callee: &AIRNode,
        args: &[bock_air::AirArg],
    ) -> Result<bool, CodegenError> {
        if let NodeKind::Identifier { name } = &callee.kind {
            if name.name == "spawn" {
                self.buf.push_str("__bockSpawn(");
                for (i, arg) in args.iter().enumerate() {
                    if i > 0 {
                        self.buf.push_str(", ");
                    }
                    self.emit_expr(&arg.value)?;
                }
                self.buf.push(')');
                return Ok(true);
            }
        }
        let NodeKind::FieldAccess { object, field } = &callee.kind else {
            return Ok(false);
        };
        if let NodeKind::Identifier { name: type_name } = &object.kind {
            if type_name.name == "Channel" && field.name == "new" {
                self.buf.push_str("__bockChannelNew()");
                return Ok(true);
            }
        }
        if matches!(field.name.as_str(), "send" | "recv" | "close") {
            self.emit_expr(object)?;
            let _ = write!(self.buf, ".{}", field.name);
            self.buf.push('(');
            for (i, arg) in args.iter().skip(1).enumerate() {
                if i > 0 {
                    self.buf.push_str(", ");
                }
                self.emit_expr(&arg.value)?;
            }
            self.buf.push(')');
            return Ok(true);
        }
        Ok(false)
    }

    /// Recognise instance methods on Duration/Instant values.
    fn try_emit_time_method(
        &mut self,
        receiver: &AIRNode,
        method: &str,
        args: &[bock_air::AirArg],
    ) -> Result<bool, CodegenError> {
        let recv_str = self.expr_to_string(receiver)?;
        let arg_strs: Vec<String> = args
            .iter()
            .map(|a| self.expr_to_string(&a.value))
            .collect::<Result<_, _>>()?;
        let code = match method {
            "as_nanos" => format!("({recv_str})"),
            "as_millis" => format!("(({recv_str}) / 1000000)"),
            "as_seconds" => format!("(({recv_str}) / 1000000000)"),
            "is_zero" => format!("(({recv_str}) == 0)"),
            "is_negative" => format!("(({recv_str}) < 0)"),
            "abs" => {
                format!("(func(__d int64) int64 {{ if __d < 0 {{ return -__d }}; return __d }}({recv_str}))")
            }
            "elapsed" => {
                // `instant.elapsed()` is derived: time-since-`recv`. Route the
                // "now" read through an installed `Clock` handler if in scope —
                // `NowMonotonic()` yields a `time.Time`, so the span is
                // `now.Sub(recv)` as nanoseconds; otherwise read the host
                // monotonic clock via `time.Since(recv)` (default).
                if let Some(handler) = self.clock_handler_var() {
                    format!(
                        "int64({handler}.{}().Sub({recv_str}))",
                        to_pascal_case("now_monotonic")
                    )
                } else {
                    self.needs_time_import = true;
                    format!("int64(time.Since({recv_str}))")
                }
            }
            "duration_since" => {
                let other = arg_strs.first().cloned().unwrap_or_default();
                format!("int64(({recv_str}).Sub({other}))")
            }
            _ => return Ok(false),
        };
        self.buf.push_str(&code);
        Ok(true)
    }

    // ── Top-level dispatch ──────────────────────────────────────────────────

    fn emit_node(&mut self, node: &AIRNode) -> Result<(), CodegenError> {
        match &node.kind {
            NodeKind::Module { items, .. } => {
                if self.per_module {
                    // Per-module native-package path (S3): each module is its own
                    // `package main` file and the runtime preludes live once in
                    // the shared `bock_runtime.go` (same package → visible). So
                    // this file inlines NO prelude; it just emits its own items.
                    // `ImportDecl`s are a no-op (same package — no inter-file
                    // import). The per-file `import (...)` block (fmt/sync/…) is
                    // rendered by `into_parts` from the `needs_*` flags the body
                    // sets as it emits.
                    //
                    // `@test` functions are transpiled separately into `go test`
                    // files (project mode, §20.6.2 — see `generate_tests`), never
                    // into the runtime package: their `expect(...)` assertion DSL
                    // has no runtime definition in the emitted source.
                    let mut first = true;
                    for item in items.iter() {
                        if crate::generator::fn_is_test(item) {
                            continue;
                        }
                        if !first {
                            self.buf.push('\n');
                        }
                        first = false;
                        self.emit_node(item)?;
                    }
                    return Ok(());
                }
                // Single-module self-contained emit (`generate_module`, used by
                // unit tests): the module's runtime preludes are inlined into
                // this one file's buffer and `ImportDecl`s are dropped. Each
                // prelude is emitted at most once, gated on a ctx flag (a
                // duplicate `type __bockChannel`/`__bockOption` would not
                // compile). The per-module *project* path never takes this branch
                // (it sets `per_module` and emits preludes once into the shared
                // `bock_runtime.go`).
                if !self.concurrency_runtime_emitted && go_module_uses_concurrency(items) {
                    self.buf.push_str(CONCURRENCY_RUNTIME_GO);
                    self.buf.push('\n');
                    self.concurrency_runtime_emitted = true;
                }
                let uses_optional = go_module_uses_optional(items);
                let uses_result = go_module_uses_result(items);
                // The deep-eq runtime's `__bockEqCustom` type-asserts the
                // Optional/Result wrapper structs to defer their payload to the
                // element's `Eq` (Q-py-go-wrapper-structural-eq), so the wrapper
                // type definitions must be in scope wherever `__bockEqCustom` is.
                // Emit them when the module uses a deep-eq lane even if it never
                // names `Optional`/`Result`; the (then-unused) constructors are
                // harmless package-level decls in Go.
                let uses_deep_eq = go_module_uses_deep_eq(items);
                if !self.optional_runtime_emitted && (uses_optional || uses_deep_eq) {
                    self.buf.push_str(OPTIONAL_RUNTIME_GO);
                    self.buf.push('\n');
                    self.optional_runtime_emitted = true;
                }
                if !self.result_runtime_emitted && (uses_result || uses_deep_eq) {
                    self.buf.push_str(RESULT_RUNTIME_GO);
                    self.buf.push('\n');
                    self.result_runtime_emitted = true;
                }
                // Shared numeric-payload helpers: emit once if either container
                // runtime is present (both use them; emitting from each would
                // redeclare them).
                if !self.numeric_runtime_emitted && (uses_optional || uses_result) {
                    self.buf.push_str(NUMERIC_RUNTIME_GO);
                    self.buf.push('\n');
                    self.numeric_runtime_emitted = true;
                }
                // The bespoke `__bockOrdering` value runtime is emitted only when
                // the real `core.compare.Ordering` enum is NOT reachable — when
                // it is, that user enum is authoritative (its variants are the
                // sealed-interface structs `OrderingLess{}`, and `compare`
                // returns it), so the int runtime would be dead and its `Less`
                // constants would shadow nothing the program uses.
                let emit_ordering = !self.ordering_runtime_emitted
                    && go_module_uses_ordering(items)
                    && !self.ordering_enum_reachable();
                if emit_ordering {
                    self.buf.push_str(ORDERING_RUNTIME_GO);
                    self.buf.push('\n');
                    self.ordering_runtime_emitted = true;
                }
                // The `__bockOrdered` constraint: needed by a `[T: Comparable]`
                // sealed-bound generic fn, or whenever the Ordering runtime above
                // is emitted (the constraint was split out of that block). Deduped
                // with its own flag so it is defined at most once.
                if !self.ordered_constraint_emitted
                    && (emit_ordering || !self.fn_sealed_bound.is_empty())
                {
                    self.buf.push_str(ORDERED_CONSTRAINT_GO);
                    self.buf.push('\n');
                    self.ordered_constraint_emitted = true;
                }
                if !self.range_runtime_emitted && go_module_uses_range(items) {
                    self.buf.push_str(RANGE_RUNTIME_GO);
                    self.buf.push('\n');
                    self.range_runtime_emitted = true;
                }
                if !self.int_pow_runtime_emitted && go_module_uses_int_pow(items) {
                    self.buf.push_str(INT_POW_RUNTIME_GO);
                    self.buf.push('\n');
                    self.int_pow_runtime_emitted = true;
                }
                if !self.deep_eq_runtime_emitted && uses_deep_eq {
                    self.buf.push_str(DEEP_EQ_RUNTIME_GO);
                    self.buf.push('\n');
                    self.deep_eq_runtime_emitted = true;
                    self.needs_reflect_import = true;
                }
                // `@test` functions are transpiled separately into `go test` files
                // (project mode, §20.6.2 — see `generate_tests`), never into the
                // runtime package.
                let mut first = true;
                for item in items.iter() {
                    if crate::generator::fn_is_test(item) {
                        continue;
                    }
                    if !first {
                        self.buf.push('\n');
                    }
                    first = false;
                    self.emit_node(item)?;
                }
                Ok(())
            }
            NodeKind::ImportDecl { .. } => {
                // Single-module self-contained emit: a Bock `use` is a no-op (no
                // sibling file to import from). The per-module project path keeps
                // one `package main` across files, so a same-package symbol is
                // also visible without a Go `import` — the per-item visit here is
                // a no-op in both paths.
                Ok(())
            }
            NodeKind::FnDecl {
                visibility,
                is_async,
                name,
                generic_params,
                params,
                return_type,
                effect_clause,
                where_clause,
                body,
                ..
            } => {
                // Fold any `where`-clause trait bounds onto the generic params so
                // the `[T Constraint]` type-param constraint is emitted for a
                // `where`-bounded fn — local or imported (the imported fn is
                // emitted in its own module file with its reconstructed
                // `where`-clause, PR #286).
                let merged = crate::generator::merge_where_bounds_into_generics(
                    generic_params,
                    where_clause,
                );
                self.emit_fn_decl(
                    *visibility,
                    *is_async,
                    &name.name,
                    &merged,
                    params,
                    return_type.as_deref(),
                    effect_clause,
                    body,
                )
            }
            NodeKind::RecordDecl {
                name,
                generic_params,
                fields,
                ..
            } => {
                let type_params = self.format_generic_params(generic_params);
                self.writeln(&format!("type {}{type_params} struct {{", name.name));
                self.indent += 1;
                for f in fields {
                    let type_str = self.ast_type_to_go(&f.ty);
                    self.writeln(&format!("{}\t{type_str}", to_pascal_case(&f.name.name)));
                }
                self.indent -= 1;
                self.writeln("}");
                Ok(())
            }
            NodeKind::EnumDecl {
                name,
                generic_params,
                variants,
                ..
            } => {
                // Go doesn't have algebraic types; use interface + variant structs.
                let type_params = self.format_generic_params(generic_params);
                // Emit the interface (sealed by convention).
                self.writeln(&format!("type {}{type_params} interface {{", name.name));
                self.indent += 1;
                self.writeln(&format!("is{}()", name.name));
                self.indent -= 1;
                self.writeln("}");
                // Emit each variant as a struct implementing the interface.
                for variant in variants {
                    self.buf.push('\n');
                    self.emit_enum_variant(&name.name, generic_params, variant)?;
                }
                Ok(())
            }
            NodeKind::ClassDecl {
                name,
                generic_params,
                fields,
                methods,
                ..
            } => {
                // Emit struct.
                let type_params = self.format_generic_params(generic_params);
                self.writeln(&format!("type {}{type_params} struct {{", name.name));
                self.indent += 1;
                for f in fields {
                    let type_str = self.ast_type_to_go(&f.ty);
                    self.writeln(&format!("{}\t{type_str}", to_pascal_case(&f.name.name)));
                }
                self.indent -= 1;
                self.writeln("}");
                // Constructor function.
                if !fields.is_empty() {
                    self.buf.push('\n');
                    let params: Vec<String> = fields
                        .iter()
                        .map(|f| {
                            let fname = to_camel_case(&f.name.name);
                            let type_str = self.ast_type_to_go(&f.ty);
                            format!("{fname} {type_str}")
                        })
                        .collect();
                    self.writeln(&format!(
                        "func New{}({}) *{} {{",
                        name.name,
                        params.join(", "),
                        name.name
                    ));
                    self.indent += 1;
                    let field_inits: Vec<String> = fields
                        .iter()
                        .map(|f| {
                            format!(
                                "{}: {},",
                                to_pascal_case(&f.name.name),
                                to_camel_case(&f.name.name)
                            )
                        })
                        .collect();
                    self.writeln(&format!("return &{} {{", name.name));
                    self.indent += 1;
                    for init in &field_inits {
                        self.writeln(init);
                    }
                    self.indent -= 1;
                    self.writeln("}");
                    self.indent -= 1;
                    self.writeln("}");
                }
                // Methods.
                for method in methods {
                    self.buf.push('\n');
                    self.emit_method(&name.name, generic_params, method, false)?;
                }
                Ok(())
            }
            NodeKind::TraitDecl {
                name,
                methods,
                generic_params,
                ..
            } => {
                // Traits → Go interfaces. A trait whose methods take a
                // `Self`-typed operand is encoded as an F-bounded *generic*
                // interface — `type Comparable[__Self any] interface {
                // Compare(__Self) Ordering }` — so an impl `func (Key)
                // Compare(Key)` satisfies `Comparable[Key]` and a bound `[T:
                // Comparable]` lowers to `[T Comparable[T]]`. `Self` in the
                // method signatures then renders as the interface's type param.
                //
                // A trait that declares its own generic params
                // (`trait Iterable[T] { fn iter(self) -> ListIterator[T] }`)
                // must carry them on the interface header too, or the param
                // appears `undefined` in a method signature (`Iter()
                // ListIterator[T]` with no `[T any]` → Go `undefined: T`). The
                // declared params and the synthesized `__Self` (if any) are both
                // threaded into the header.
                let uses_self = self.self_param_traits.contains(&name.name);
                let prev_self_subst = self.go_self_subst.take();
                let mut header_params: Vec<String> = Vec::new();
                if uses_self {
                    self.go_self_subst = Some("__Self".to_string());
                    header_params.push("__Self any".to_string());
                }
                for p in generic_params {
                    header_params.push(format!("{} any", p.name.name));
                }
                let head = if header_params.is_empty() {
                    name.name.clone()
                } else {
                    format!("{}[{}]", name.name, header_params.join(", "))
                };
                self.writeln(&format!("type {head} interface {{"));
                self.indent += 1;
                for method in methods {
                    if let NodeKind::FnDecl {
                        name,
                        params,
                        return_type,
                        ..
                    } = &method.kind
                    {
                        // Drop the leading `self` receiver — a Go interface
                        // method's receiver is implicit, so only the remaining
                        // operands form the signature (the AIR keeps `self` as a
                        // real leading param, as for impl methods).
                        let rest = match params.first().map(crate::generator::param_binds_self) {
                            Some(Some(_)) => &params[1..],
                            _ => &params[..],
                        };
                        let param_strs = self.collect_param_type_strs(rest);
                        let is_void = return_type.as_deref().is_some_and(Self::is_void_type);
                        let ret = if is_void {
                            String::new()
                        } else {
                            return_type
                                .as_deref()
                                .map(|t| format!(" {}", self.type_to_go(t)))
                                .unwrap_or_default()
                        };
                        self.writeln(&format!(
                            "{}({}){ret}",
                            self.go_method_name(&name.name, true),
                            param_strs.join(", "),
                        ));
                    }
                }
                self.indent -= 1;
                self.writeln("}");
                self.go_self_subst = prev_self_subst;
                Ok(())
            }
            NodeKind::ImplBlock {
                generic_params,
                target,
                methods,
                trait_path,
                ..
            } => {
                let target_name = self.type_expr_to_string(target);
                // The receiver's type-param list. Go requires the parameters on a
                // generic type's method receiver: `func (self *Box[T]) ...`. The
                // params come from the impl's own list when present, else from the
                // record/enum decl (the common `impl Box { ... }` where `T` is
                // declared on `record Box[T]`, not the impl).
                let target_generics = self.impl_target_generics(generic_params, &target_name);
                // Value receivers for trait/effect impls so `Handler{}` satisfies
                // the interface; pointer receivers for inherent `impl T { ... }`.
                let use_value_receiver = trait_path.is_some();
                // Trait default methods (codegen-completeness P2): synthesize a
                // receiver method on the target for every default method this
                // impl does not override, so the type satisfies the interface
                // (which declares the default's signature) and a call resolves.
                // A default body calling another trait method (`self.other(..)`)
                // resolves through the same receiver methods.
                let default_methods: Vec<AIRNode> = trait_path
                    .as_ref()
                    .map(|tp| {
                        crate::generator::inherited_default_methods(&self.trait_decls, tp, methods)
                    })
                    .unwrap_or_default();
                let mut emitted_any = false;
                for method in methods.iter().chain(default_methods.iter()) {
                    // Skip a trait-impl method that duplicates an inherent
                    // (`impl Type`) / class method of the same Go name. The
                    // inherent method is the real implementation and — being in
                    // `public_methods` because the trait declares it — is now
                    // exported to the same PascalCase Go name, so it satisfies the
                    // interface directly. Emitting the trait-impl method too would
                    // be a duplicate-method Go error, and a forwarder body
                    // (`fn render(self) { self.render() }`) would resolve back to
                    // itself (`Render() { return self.Render() }`) — infinite
                    // recursion. Default methods (synthesized, real bodies) and
                    // non-duplicated trait methods are unaffected.
                    if trait_path.is_some() {
                        if let NodeKind::FnDecl { name, .. } = &method.kind {
                            let go_name = to_pascal_case(&name.name);
                            if self
                                .inherent_methods
                                .contains(&(target_name.clone(), go_name))
                            {
                                continue;
                            }
                        }
                    }
                    if emitted_any {
                        self.buf.push('\n');
                    }
                    self.emit_method(&target_name, &target_generics, method, use_value_receiver)?;
                    emitted_any = true;
                }
                Ok(())
            }
            NodeKind::EffectDecl {
                name,
                components,
                generic_params,
                operations,
                ..
            } => {
                if !components.is_empty() {
                    let comp_names: Vec<String> = components
                        .iter()
                        .map(|tp| {
                            tp.segments
                                .last()
                                .map_or("effect".to_string(), |s| s.name.clone())
                        })
                        .collect();
                    self.writeln(&format!(
                        "// composite effect {} = {}",
                        name.name,
                        comp_names.join(" + ")
                    ));
                    self.composite_effects.insert(name.name.clone(), comp_names);
                    return Ok(());
                }
                // Record effect operations for Call → handler.op rewriting.
                for op in operations {
                    if let NodeKind::FnDecl {
                        name: op_name,
                        return_type,
                        ..
                    } = &op.kind
                    {
                        self.effect_ops
                            .insert(op_name.name.clone(), name.name.clone());
                        if return_type.as_deref().is_some_and(Self::is_void_type) {
                            self.void_effect_ops.insert(op_name.name.clone());
                        }
                    }
                }
                // Effects → Go interfaces.
                let type_params = self.format_generic_params(generic_params);
                self.writeln(&format!("type {}{type_params} interface {{", name.name));
                self.indent += 1;
                for op in operations {
                    if let NodeKind::FnDecl {
                        name,
                        params,
                        return_type,
                        ..
                    } = &op.kind
                    {
                        let param_strs = self.collect_param_type_strs(params);
                        let is_void = return_type.as_deref().is_some_and(Self::is_void_type);
                        let ret = if is_void {
                            String::new()
                        } else {
                            return_type
                                .as_deref()
                                .map(|t| format!(" {}", self.type_to_go(t)))
                                .unwrap_or_default()
                        };
                        self.writeln(&format!(
                            "{}({}){ret}",
                            to_pascal_case(&name.name),
                            param_strs.join(", "),
                        ));
                    }
                }
                self.indent -= 1;
                self.writeln("}");
                Ok(())
            }
            NodeKind::TypeAlias {
                name,
                generic_params,
                ty,
                ..
            } => {
                // Render the alias to its underlying Go type so a value of the
                // alias type is the concrete container/struct (`type ParseResult =
                // Result[...]` → `type ParseResult = __bockResult`, whose `.tag`
                // a `match` reads). A *generic* alias keeps the prior `interface{}`
                // erasure (the emitter does not substitute its params, and the
                // alias registry skips it), so its param list is preserved.
                if generic_params.is_empty() {
                    let underlying = self.type_to_go(ty);
                    self.writeln(&format!("type {} = {underlying}", name.name));
                } else {
                    let type_params = self.format_generic_params(generic_params);
                    self.writeln(&format!("type {}{type_params} = interface{{}}", name.name));
                }
                Ok(())
            }
            NodeKind::ConstDecl {
                name, value, ty, ..
            } => {
                let type_str = format!(" {}", self.type_to_go(ty));
                let ind = self.indent_str();
                // Emit the const's declared name verbatim (not pascal-cased) so it
                // matches the verbatim spelling the `Identifier` use-site arm emits
                // for a known const — `to_pascal_case` strips underscores
                // (`FIZZ_NUM` → `FIZZNUM`) while the use site keeps `FIZZ_NUM`, an
                // undefined-identifier error. `SCREAMING_SNAKE` is a valid exported
                // Go identifier.
                let _ = write!(self.buf, "{ind}var {}{type_str} = ", name.name);
                self.emit_expr(value)?;
                self.buf.push('\n');
                Ok(())
            }
            NodeKind::ModuleHandle { effect, handler } => {
                let effect_name = effect.segments.last().map_or("effect", |s| s.name.as_str());
                let var_name = format!("__{}", to_camel_case(effect_name));
                let ind = self.indent_str();
                let _ = write!(self.buf, "{ind}var {var_name} {effect_name} = ");
                self.emit_expr(handler)?;
                self.buf.push('\n');
                // Register the module-scoped handler so effectful function
                // calls at module level pick it up.
                self.current_handler_vars
                    .insert(effect_name.to_string(), var_name);
                Ok(())
            }
            NodeKind::PropertyTest { name, .. } => {
                self.writeln(&format!("// property test: {name}"));
                Ok(())
            }
            // Statement / expression nodes at top level:
            NodeKind::LetBinding { .. }
            | NodeKind::If { .. }
            | NodeKind::For { .. }
            | NodeKind::While { .. }
            | NodeKind::Loop { .. }
            | NodeKind::Return { .. }
            | NodeKind::Break { .. }
            | NodeKind::Continue
            | NodeKind::Guard { .. }
            | NodeKind::Match { .. }
            | NodeKind::Block { .. }
            | NodeKind::HandlingBlock { .. }
            | NodeKind::Assign { .. } => self.emit_stmt(node),
            // A bare `expr?` statement (`save_task(task)?`): the success value is
            // discarded, but the failure path must still early-return the
            // propagated error/None from the enclosing function. Emit the unwrap
            // prelude only — the success payload is unused, and asserting it (e.g.
            // a `Result[Void, _]` whose `Ok(())` boxes `nil`) would panic.
            NodeKind::Propagate { expr: inner } => {
                let _ = self.emit_try_unwrap(inner)?;
                Ok(())
            }
            _ => {
                // DQ18: an in-place `List` mutator (`push`/`append`) in
                // statement position lowers to Go's slice-growth idiom
                // `recv = append(recv, x)` — an assignment statement, not the
                // value-less call the other backends emit. Intercept it here,
                // before the generic expression-statement fall-through (Go has no
                // expression form for in-place append).
                if let NodeKind::Call { callee, args, .. } = &node.kind {
                    if self.try_emit_list_mutating_stmt(node, callee, args)? {
                        return Ok(());
                    }
                }
                self.write_indent();
                self.emit_expr(node)?;
                self.buf.push('\n');
                Ok(())
            }
        }
    }

    // ── Generics ────────────────────────────────────────────────────────────

    /// Resolve the generic params that apply to an `impl` target. Prefers the
    /// impl's own params (`impl[T] Box[T] { ... }`); falls back to the generic
    /// params declared on the target record/enum (`impl Box { ... }` where `T`
    /// is declared on `record Box[T]`). Returns an empty slice for a
    /// non-generic target.
    fn impl_target_generics(
        &self,
        impl_params: &[bock_ast::GenericParam],
        target_name: &str,
    ) -> Vec<bock_ast::GenericParam> {
        if !impl_params.is_empty() {
            return impl_params.to_vec();
        }
        self.generic_decls
            .get(target_name)
            .cloned()
            .unwrap_or_default()
    }

    /// Render a *use-site* generic argument list (`[T]`) from generic params —
    /// the bare names only, never the `any`/bound clause. Used on a method
    /// receiver type (`*Box[T]`) where the params are already in scope.
    fn format_generic_param_args(&self, params: &[bock_ast::GenericParam]) -> String {
        if params.is_empty() {
            return String::new();
        }
        let names: Vec<&str> = params.iter().map(|p| p.name.name.as_str()).collect();
        format!("[{}]", names.join(", "))
    }

    fn format_generic_params(&self, params: &[bock_ast::GenericParam]) -> String {
        if params.is_empty() {
            return String::new();
        }
        let parts: Vec<String> = params
            .iter()
            .map(|p| {
                if p.bounds.is_empty() {
                    format!("{} any", p.name.name)
                } else {
                    let bound_strs: Vec<String> = p
                        .bounds
                        .iter()
                        .map(|b| {
                            let bound_name = b
                                .segments
                                .iter()
                                .map(|s| s.name.as_str())
                                .collect::<Vec<_>>()
                                .join(".");
                            // A compiler-provided sealed-core bound (`Equatable`/…)
                            // with no user `impl` maps to Go's built-in constraint
                            // (GAP-C): `comparable` for equality/hashing, the
                            // self-contained `__bockOrdered` set for ordering, `any`
                            // for stringable. There is no `Equatable` type in Go, so
                            // the verbatim bound would be `undefined`.
                            if crate::generator::is_unimplemented_sealed_core_trait(
                                &bound_name,
                                &self.trait_decls,
                            ) {
                                match bound_name.as_str() {
                                    "Equatable" | "Hashable" => "comparable".to_string(),
                                    "Comparable" => "__bockOrdered".to_string(),
                                    _ => "any".to_string(),
                                }
                            } else if self.self_param_traits.contains(&bound_name) {
                                // An F-bounded self-param trait constraint is applied
                                // to the type var itself: `[T Comparable[T]]`.
                                format!("{bound_name}[{}]", p.name.name)
                            } else {
                                bound_name
                            }
                        })
                        .collect();
                    format!("{} {}", p.name.name, bound_strs.join(" | "))
                }
            })
            .collect();
        format!("[{}]", parts.join(", "))
    }

    fn format_generic_args(&self, args: &[AIRNode]) -> String {
        if args.is_empty() {
            return String::new();
        }
        let parts: Vec<String> = args.iter().map(|a| self.type_to_go(a)).collect();
        format!("[{}]", parts.join(", "))
    }

    // ── Function declarations ───────────────────────────────────────────────

    #[allow(clippy::too_many_arguments)]
    fn emit_fn_decl(
        &mut self,
        visibility: Visibility,
        is_async: bool,
        name: &str,
        generic_params: &[bock_ast::GenericParam],
        params: &[AIRNode],
        return_type: Option<&AIRNode>,
        effect_clause: &[bock_ast::TypePath],
        body: &AIRNode,
    ) -> Result<(), CodegenError> {
        let is_public = matches!(visibility, Visibility::Public);
        // The program entry point is always Go's `func main()`, never the
        // exported `func Main()` that PascalCasing a `public fn main` would
        // produce (Go would then report "function main is undeclared").
        let is_entry_point = name == "main";
        if is_public && !is_entry_point {
            self.public_fns.insert(name.to_string());
        }
        // `main` stays Go's bare `func main`; every other function goes through
        // `go_fn_name`, which applies the public/private casing rule and renames
        // a public name colliding with a top-level type (`key` → `KeyFn` when a
        // `record Key` exists).
        let fn_name = if is_entry_point {
            to_camel_case(name)
        } else {
            self.go_fn_name(name)
        };
        let type_params = self.format_generic_params(generic_params);
        let param_strs = self.collect_param_strs(params);
        let effects = self.effects_params(effect_clause);
        let mut all_params = param_strs.clone();
        all_params.extend(effects.clone());
        let is_void = return_type.is_some_and(Self::is_void_type);
        let ret = if is_void {
            String::new()
        } else {
            return_type
                .map(|t| format!(" {}", self.type_to_go(t)))
                .unwrap_or_default()
        };
        if !effect_clause.is_empty() {
            let effect_names = self.expand_effect_names(effect_clause);
            self.fn_effects.insert(name.to_string(), effect_names);
        }
        self.writeln(&format!(
            "func {fn_name}{type_params}({}){ret} {{",
            all_params.join(", "),
        ));
        self.indent += 1;
        let old_handler_vars = self.current_handler_vars.clone();
        let expanded = self.expand_effect_names(effect_clause);
        for ename in &expanded {
            self.current_handler_vars
                .insert(ename.clone(), to_camel_case(ename));
        }
        let saved_record_args = self.var_record_type_args.clone();
        let saved_lambda_ret = self.var_lambda_ret.clone();
        let saved_decl_type = self.var_decl_type_node.clone();
        let (
            saved_opt_scope,
            saved_list_scope,
            saved_result_scope,
            saved_map_scope,
            saved_set_scope,
        ) = self.enter_param_optional_scope(params);
        // Record each typed param's Go type (`b: Box[T]` → `var_go_type["b"] =
        // "Box[T]"`) so a `value.field.get(i)` list receiver inside the body can
        // recover the field's `[]T` element type (GAP-A). `current_self_record`
        // already covers `self.field`; this covers a non-self generic-record
        // param. Restored alongside the other param scopes on exit.
        let saved_go_types = self.enter_param_go_types_with_expected(params, None);
        // Seed the function body's Go block frame with the parameter names so a
        // `let` that shadows a parameter (the same Go scope — the body gets no
        // extra brace) lowers to a reassignment, not a re-declaration.
        self.pending_scope_seed = Some(self.param_binding_names(params));
        if name == "main" || is_void {
            self.emit_block_body(body)?;
        } else {
            let prev_ret = self.current_fn_ret_type.take();
            let prev_ret_coll = self.current_fn_ret_collection_elem.take();
            let prev_ret_node = self.current_fn_ret_type_node.take();
            self.current_fn_ret_type = return_type.map(|t| self.type_to_go(t));
            self.current_fn_ret_collection_elem =
                return_type.and_then(|t| self.collection_elem_go_types(t));
            self.current_fn_ret_type_node = Self::fn_type_ret_node(return_type);
            self.emit_block_body_return(body)?;
            self.current_fn_ret_type = prev_ret;
            self.current_fn_ret_collection_elem = prev_ret_coll;
            self.current_fn_ret_type_node = prev_ret_node;
        }
        self.var_optional_elem = saved_opt_scope;
        self.var_list_elem = saved_list_scope;
        self.var_result_elem = saved_result_scope;
        self.var_map_kv = saved_map_scope;
        self.var_set_elem = saved_set_scope;
        self.var_go_type = saved_go_types;
        self.var_record_type_args = saved_record_args;
        self.var_lambda_ret = saved_lambda_ret;
        self.var_decl_type_node = saved_decl_type;
        self.current_handler_vars = old_handler_vars;
        self.indent -= 1;
        self.writeln("}");

        // Async wrapper: every `async fn` gets a companion `FnAsync` that
        // starts a goroutine and returns a buffered `<-chan T` (or
        // `<-chan struct{}` for void returns). `main` is skipped — Go's
        // entry point is always `func main()` and wrapping it would be dead
        // code the linker would complain about.
        if is_async && name != "main" {
            self.buf.push('\n');
            self.emit_async_wrapper(
                &fn_name,
                &type_params,
                params,
                return_type,
                is_void,
                &effects,
            )?;
        }
        Ok(())
    }

    /// Emit the `FnNameAsync` companion for an `async fn`. The wrapper starts
    /// a goroutine, invokes the sync body with the caller's arguments, and
    /// returns the result over a buffered channel. Callers `await`
    /// (= `<-chan T`) to observe completion.
    fn emit_async_wrapper(
        &mut self,
        sync_fn_name: &str,
        type_params: &str,
        params: &[AIRNode],
        return_type: Option<&AIRNode>,
        is_void: bool,
        effects: &[String],
    ) -> Result<(), CodegenError> {
        let async_fn_name = format!("{sync_fn_name}Async");
        let param_strs = self.collect_param_strs(params);
        let mut all_params = param_strs;
        all_params.extend(effects.iter().cloned());
        let chan_ty = if is_void {
            "struct{}".to_string()
        } else {
            return_type
                .map(|t| self.type_to_go(t))
                .unwrap_or_else(|| "interface{}".to_string())
        };
        self.writeln(&format!(
            "func {async_fn_name}{type_params}({}) <-chan {chan_ty} {{",
            all_params.join(", "),
        ));
        self.indent += 1;
        self.writeln(&format!("__ch := make(chan {chan_ty}, 1)"));
        self.writeln("go func() {");
        self.indent += 1;
        // Forward the sync function's arguments verbatim. Param names are
        // the camel-cased binding names the wrapper receives.
        let call_args: Vec<String> = params
            .iter()
            .filter_map(|p| {
                if let NodeKind::Param { pattern, .. } = &p.kind {
                    Some(self.pattern_to_binding_name(pattern))
                } else {
                    None
                }
            })
            .chain(effects.iter().map(|e| {
                // Effects params look like `name EffectType`; recover the
                // name before the first space.
                e.split_whitespace().next().unwrap_or("").to_string()
            }))
            .collect();
        let call_site = format!("{sync_fn_name}({})", call_args.join(", "));
        if is_void {
            self.writeln(&call_site);
            self.writeln("__ch <- struct{}{}");
        } else {
            self.writeln(&format!("__ch <- {call_site}"));
        }
        self.indent -= 1;
        self.writeln("}()");
        self.writeln("return __ch");
        self.indent -= 1;
        self.writeln("}");
        Ok(())
    }

    fn emit_method(
        &mut self,
        receiver_type: &str,
        target_generics: &[bock_ast::GenericParam],
        method: &AIRNode,
        use_value_receiver: bool,
    ) -> Result<(), CodegenError> {
        // Inside ANY impl method (trait or plain inherent), a `Self` type
        // resolves to the concrete target (the receiver type) — whether it
        // appears in a synthesized trait default (`other: Self`) or an inherent
        // method's own signature (`fn combine(self, ...) -> Self`). Previously
        // this was gated on `use_value_receiver` (trait impls only), so an
        // inherent-impl `Self` lowered to the `/* Self */` placeholder and
        // produced an invalid Go signature. `receiver_type` is in scope for both
        // method kinds. Saved/restored so the ctx-wide default of `/* Self */`
        // is unchanged outside impl methods.
        let prev_self_subst = self.go_self_subst.take();
        self.go_self_subst = Some(receiver_type.to_string());
        // The base record name (`ListIter` from `ListIter[T]` / `ListIter`) so a
        // `self.field` list receiver inside the body resolves its element type
        // via `record_field_list_elem`. Restored on exit.
        let prev_self_record = self.current_self_record.take();
        let base = receiver_type
            .split_once('[')
            .map_or(receiver_type, |(b, _)| b)
            .to_string();
        self.current_self_record = Some(base);
        let result =
            self.emit_method_body(receiver_type, target_generics, method, use_value_receiver);
        self.current_self_record = prev_self_record;
        self.go_self_subst = prev_self_subst;
        result
    }

    fn emit_method_body(
        &mut self,
        receiver_type: &str,
        target_generics: &[bock_ast::GenericParam],
        method: &AIRNode,
        use_value_receiver: bool,
    ) -> Result<(), CodegenError> {
        if let NodeKind::FnDecl {
            visibility,
            name,
            generic_params,
            params,
            return_type,
            effect_clause,
            body,
            ..
        } = &method.kind
        {
            // A trait-impl method (`use_value_receiver`) is PascalCased
            // regardless of Bock visibility: Go interface methods are always
            // exported (the `TraitDecl` emission PascalCases them), so the
            // receiver method and the call site must match. A `private` trait
            // default method (e.g. `not_equals`) would otherwise be camelCased
            // here while the interface declares it PascalCased. An inherent
            // method whose name a trait *also* declares (so it is in
            // `public_methods`) is exported too: it is the real implementation
            // that satisfies the interface (`impl Button { fn render }` →
            // `Render`), and every call site already PascalCases a
            // `public_methods` name — declaration and dispatch must agree.
            // Otherwise inherent methods keep the public/private casing rule.
            let is_public_method = use_value_receiver
                || matches!(visibility, Visibility::Public)
                || self.public_methods.contains(&name.name);
            let method_name = self.go_method_name(&name.name, is_public_method);
            // An associated function (no `self` receiver, e.g. a `From` impl's
            // `from`) has no Go-static equivalent: emit a free function named
            // `<Type>_<Method>` (reusing the DQ28 free-function naming) with no
            // receiver. The call site (`is_associated_call`) rewrites
            // `Type.method(args)` to the same `Type_Method(args)`.
            let receiver_base = receiver_type
                .split_once('[')
                .map_or(receiver_type, |(b, _)| b);
            if crate::generator::is_associated_impl_method(method, &self.effect_ops) {
                return self.emit_associated_fn(
                    receiver_base,
                    target_generics,
                    method,
                    is_public_method,
                );
            }
            // The AIR keeps `self` as a leading `Param` and method bodies refer
            // to `self.Field`. Name the Go receiver `self` and drop the leading
            // `self` param so the body resolves with no rewrite — otherwise the
            // receiver was `p` while the body referenced an undefined `self`,
            // and `self` also leaked in as a stray `interface{}` parameter.
            let (receiver_var, rest) = match params.first().map(crate::generator::param_binds_self)
            {
                Some(Some(_)) => ("self".to_string(), &params[1..]),
                _ => (
                    receiver_type
                        .chars()
                        .next()
                        .unwrap_or('r')
                        .to_lowercase()
                        .to_string(),
                    &params[..],
                ),
            };
            let param_strs = self.collect_param_strs(rest);
            let effects = self.effects_params(effect_clause);
            let mut all_params = param_strs;
            all_params.extend(effects);
            let is_void = return_type.as_deref().is_some_and(Self::is_void_type);
            let ret = if is_void {
                String::new()
            } else {
                return_type
                    .as_deref()
                    .map(|t| format!(" {}", self.type_to_go(t)))
                    .unwrap_or_default()
            };
            // DQ28: a method declaring its own type parameters (`Box[T].map[U]`)
            // is free-function-lowered — Go forbids method type params. Emit
            // `func Box_Map[T any, U any](self Box[T], ..) ..` (the receiver
            // becomes a leading `self` *parameter*; the receiver's and the
            // method's type params combine on the free function, which Go allows).
            // Every call site is rewritten to `Box_Map(box, ..)` by the call
            // emitter. A non-generic method keeps the idiomatic Go receiver form.
            let receiver_base = receiver_type
                .split_once('[')
                .map_or(receiver_type, |(b, _)| b);
            let freefn_lowered = !generic_params.is_empty()
                && self.freefn_lowered_type(&name.name) == Some(receiver_base);
            if freefn_lowered {
                // Combine receiver type params (`[T any]`) with the method's own
                // (`[U any]`) into one Go free-function type-param list.
                let mut combined = target_generics.to_vec();
                combined.extend(generic_params.iter().cloned());
                let type_params = self.format_generic_params(&combined);
                let receiver_args = self.format_generic_param_args(target_generics);
                let self_param = format!("{receiver_var} {receiver_type}{receiver_args}");
                let mut freefn_params = vec![self_param];
                freefn_params.extend(all_params);
                let fn_name = self.freefn_lowered_name(receiver_base, &name.name, is_public_method);
                self.writeln(&format!(
                    "func {fn_name}{type_params}({}){ret} {{",
                    freefn_params.join(", "),
                ));
            } else {
                let receiver_prefix = if use_value_receiver { "" } else { "*" };
                // Go binds a generic type's params on the receiver itself:
                // `func (self *Box[T]) ...`. The bare-name arg list (`[T]`) brings
                // `T` into scope for the receiver type, params, and body.
                let receiver_generics = self.format_generic_param_args(target_generics);
                self.writeln(&format!(
                    "func ({receiver_var} {receiver_prefix}{receiver_type}{receiver_generics}) \
                     {method_name}({}){ret} {{",
                    all_params.join(", "),
                ));
            }
            self.indent += 1;
            let old_handler_vars = self.current_handler_vars.clone();
            let expanded = self.expand_effect_names(effect_clause);
            for ename in &expanded {
                self.current_handler_vars
                    .insert(ename.clone(), to_camel_case(ename));
            }
            let saved_record_args = self.var_record_type_args.clone();
            let saved_decl_type = self.var_decl_type_node.clone();
            let (
                saved_opt_scope,
                saved_list_scope,
                saved_result_scope,
                saved_map_scope,
                saved_set_scope,
            ) = self.enter_param_optional_scope(rest);
            // Seed the method body's Go block frame with the receiver var and the
            // value parameter names (same Go scope as the body) so a shadowing
            // `let` reassigns rather than re-declares.
            let mut method_seed = self.param_binding_names(rest);
            method_seed.push(receiver_var.clone());
            self.pending_scope_seed = Some(method_seed);
            if return_type.is_some() && !is_void {
                let prev_ret = self.current_fn_ret_type.take();
                let prev_ret_coll = self.current_fn_ret_collection_elem.take();
                let prev_ret_node = self.current_fn_ret_type_node.take();
                self.current_fn_ret_type = return_type.as_deref().map(|t| self.type_to_go(t));
                self.current_fn_ret_collection_elem = return_type
                    .as_deref()
                    .and_then(|t| self.collection_elem_go_types(t));
                self.current_fn_ret_type_node = Self::fn_type_ret_node(return_type.as_deref());
                self.emit_block_body_return(body)?;
                self.current_fn_ret_type = prev_ret;
                self.current_fn_ret_collection_elem = prev_ret_coll;
                self.current_fn_ret_type_node = prev_ret_node;
            } else {
                self.emit_block_body(body)?;
            }
            self.var_optional_elem = saved_opt_scope;
            self.var_list_elem = saved_list_scope;
            self.var_result_elem = saved_result_scope;
            self.var_map_kv = saved_map_scope;
            self.var_set_elem = saved_set_scope;
            self.var_record_type_args = saved_record_args;
            self.var_decl_type_node = saved_decl_type;
            self.current_handler_vars = old_handler_vars;
            self.indent -= 1;
            self.writeln("}");
        }
        Ok(())
    }

    /// Emit an impl/trait **associated function** (no `self` receiver) as a Go
    /// free function `func <Type>_<Method>(params) ret { ... }`.
    ///
    /// Go has no static methods, so an associated function — e.g. a `From` impl's
    /// `from(value) -> Self` — cannot attach to the type. It is emitted as a free
    /// function whose name carries the type prefix (`Foot_From`), matching the
    /// `Type.method(args)` → `Type_Method(args)` rewrite at the call site
    /// (`is_associated_call`). The `<Type>_` prefix keeps the name collision-free
    /// across types sharing a method name.
    fn emit_associated_fn(
        &mut self,
        receiver_base: &str,
        target_generics: &[bock_ast::GenericParam],
        method: &AIRNode,
        is_public_method: bool,
    ) -> Result<(), CodegenError> {
        let NodeKind::FnDecl {
            name,
            generic_params,
            params,
            return_type,
            effect_clause,
            body,
            ..
        } = &method.kind
        else {
            return Ok(());
        };
        let fn_name = self.freefn_lowered_name(receiver_base, &name.name, is_public_method);
        // Combine the target's type params with the method's own onto the free
        // function (Go forbids method type params, but a free function may carry
        // both — mirrors the DQ28 free-function lowering).
        let mut combined = target_generics.to_vec();
        combined.extend(generic_params.iter().cloned());
        let type_params = self.format_generic_params(&combined);
        let param_strs = self.collect_param_strs(params);
        let effects = self.effects_params(effect_clause);
        let mut all_params = param_strs;
        all_params.extend(effects);
        let is_void = return_type.as_deref().is_some_and(Self::is_void_type);
        let ret = if is_void {
            String::new()
        } else {
            return_type
                .as_deref()
                .map(|t| format!(" {}", self.type_to_go(t)))
                .unwrap_or_default()
        };
        self.writeln(&format!(
            "func {fn_name}{type_params}({}){ret} {{",
            all_params.join(", "),
        ));
        self.indent += 1;
        let old_handler_vars = self.current_handler_vars.clone();
        let expanded = self.expand_effect_names(effect_clause);
        for ename in &expanded {
            self.current_handler_vars
                .insert(ename.clone(), to_camel_case(ename));
        }
        let saved_record_args = self.var_record_type_args.clone();
        let saved_decl_type = self.var_decl_type_node.clone();
        let (
            saved_opt_scope,
            saved_list_scope,
            saved_result_scope,
            saved_map_scope,
            saved_set_scope,
        ) = self.enter_param_optional_scope(params);
        self.pending_scope_seed = Some(self.param_binding_names(params));
        if return_type.is_some() && !is_void {
            let prev_ret = self.current_fn_ret_type.take();
            let prev_ret_coll = self.current_fn_ret_collection_elem.take();
            let prev_ret_node = self.current_fn_ret_type_node.take();
            self.current_fn_ret_type = return_type.as_deref().map(|t| self.type_to_go(t));
            self.current_fn_ret_collection_elem = return_type
                .as_deref()
                .and_then(|t| self.collection_elem_go_types(t));
            self.current_fn_ret_type_node = Self::fn_type_ret_node(return_type.as_deref());
            self.emit_block_body_return(body)?;
            self.current_fn_ret_type = prev_ret;
            self.current_fn_ret_collection_elem = prev_ret_coll;
            self.current_fn_ret_type_node = prev_ret_node;
        } else {
            self.emit_block_body(body)?;
        }
        self.var_optional_elem = saved_opt_scope;
        self.var_list_elem = saved_list_scope;
        self.var_result_elem = saved_result_scope;
        self.var_map_kv = saved_map_scope;
        self.var_set_elem = saved_set_scope;
        self.var_record_type_args = saved_record_args;
        self.var_decl_type_node = saved_decl_type;
        self.current_handler_vars = old_handler_vars;
        self.indent -= 1;
        self.writeln("}");
        Ok(())
    }

    fn collect_param_strs(&self, params: &[AIRNode]) -> Vec<String> {
        params
            .iter()
            .filter_map(|p| {
                if let NodeKind::Param { pattern, ty, .. } = &p.kind {
                    let name = self.pattern_to_binding_name(pattern);
                    let type_str = ty
                        .as_ref()
                        .map(|t| format!(" {}", self.type_to_go(t)))
                        .unwrap_or_else(|| " interface{}".into());
                    Some(format!("{name}{type_str}"))
                } else {
                    None
                }
            })
            .collect()
    }

    fn collect_param_type_strs(&self, params: &[AIRNode]) -> Vec<String> {
        params
            .iter()
            .filter_map(|p| {
                if let NodeKind::Param { ty, .. } = &p.kind {
                    let type_str = ty
                        .as_ref()
                        .map(|t| self.type_to_go(t))
                        .unwrap_or_else(|| "interface{}".into());
                    Some(type_str)
                } else {
                    None
                }
            })
            .collect()
    }

    /// Expand effect names, replacing composite effects with their components.
    fn expand_effect_names(&self, effects: &[bock_ast::TypePath]) -> Vec<String> {
        let mut result = Vec::new();
        for tp in effects {
            let name = tp
                .segments
                .last()
                .map_or("effect".to_string(), |s| s.name.clone());
            if let Some(components) = self.composite_effects.get(&name) {
                result.extend(components.iter().cloned());
            } else {
                result.push(name);
            }
        }
        result
    }

    /// The in-scope `Clock` effect handler variable, if one is installed.
    ///
    /// When `Some`, the `Clock` time operations (`Instant.now`, `sleep`,
    /// `elapsed`) are routed through the handler instead of inlining the host
    /// primitive (Q-clock-handler-routing, §18.3.1/§18.4); when `None`, no
    /// handler is in scope and the default host primitive is emitted.
    fn clock_handler_var(&self) -> Option<&str> {
        self.current_handler_vars.get("Clock").map(String::as_str)
    }

    /// Effects → interface parameters: `log Log, clock Clock`.
    fn effects_params(&self, effects: &[bock_ast::TypePath]) -> Vec<String> {
        let expanded = self.expand_effect_names(effects);
        expanded
            .iter()
            .map(|name| format!("{} {}", to_camel_case(name), name))
            .collect()
    }

    /// Build `handler_var, ...` arguments for calling an effectful function.
    fn build_effects_call_args_go(&self, fn_name: &str) -> Option<String> {
        let effects = self.fn_effects.get(fn_name)?;
        let entries: Vec<String> = effects
            .iter()
            .filter_map(|e| {
                let handler_var = self.current_handler_vars.get(e)?;
                Some(handler_var.clone())
            })
            .collect();
        if entries.is_empty() {
            return None;
        }
        Some(entries.join(", "))
    }

    // ── Enum variant structs ────────────────────────────────────────────────

    fn emit_enum_variant(
        &mut self,
        enum_name: &str,
        generic_params: &[bock_ast::GenericParam],
        variant: &AIRNode,
    ) -> Result<(), CodegenError> {
        if let NodeKind::EnumVariant { name, payload } = &variant.kind {
            let vname = &name.name;
            let type_params = self.format_generic_params(generic_params);
            // The marker-method *receiver* type-param list must NOT carry the
            // `any` constraint: a Go receiver writes the bare `[T]` form
            // (`func (BoxFull[T]) isBox()`), whereas the constrained `[T any]`
            // form is a syntax error in receiver position ("unexpected name any,
            // expected ]"). The type DECLARATION above keeps the constrained
            // `[T any]` form; the receiver uses the bare-name args.
            let recv_type_params = self.format_generic_param_args(generic_params);
            match payload {
                EnumVariantPayload::Unit => {
                    self.writeln(&format!("type {enum_name}{vname}{type_params} struct{{}}"));
                }
                EnumVariantPayload::Struct(fields) => {
                    self.writeln(&format!("type {enum_name}{vname}{type_params} struct {{"));
                    self.indent += 1;
                    for f in fields {
                        let type_str = self.ast_type_to_go(&f.ty);
                        self.writeln(&format!("{}\t{type_str}", to_pascal_case(&f.name.name)));
                    }
                    self.indent -= 1;
                    self.writeln("}");
                }
                EnumVariantPayload::Tuple(elems) => {
                    self.writeln(&format!("type {enum_name}{vname}{type_params} struct {{"));
                    self.indent += 1;
                    for (i, elem) in elems.iter().enumerate() {
                        let type_str = self.type_to_go(elem);
                        self.writeln(&format!("Field{i}\t{type_str}"));
                    }
                    self.indent -= 1;
                    self.writeln("}");
                }
            }
            // Implement the interface marker method. The receiver uses the
            // bare `[T]` type-param form (`recv_type_params`), never `[T any]`.
            self.buf.push('\n');
            self.writeln(&format!(
                "func ({enum_name}{vname}{recv_type_params}) is{enum_name}() {{}}"
            ));
        }
        Ok(())
    }

    // ── Statements ──────────────────────────────────────────────────────────

    fn emit_stmt(&mut self, node: &AIRNode) -> Result<(), CodegenError> {
        match &node.kind {
            NodeKind::LetBinding {
                pattern, value, ty, ..
            } => {
                // Declare-only temp from the shared value-CF hoist: Go needs a
                // type for `var x T`. The owning block pre-scanned this temp
                // against its following control-flow statement and recorded the
                // inferred Go type in `decl_only_types`; emit `var x T`. A typed
                // annotation, if present, wins. Falls back to `interface{}` when
                // the type cannot be inferred (the relocated CF still assigns it).
                if node.metadata.contains_key(crate::generator::DECL_ONLY_META) {
                    let binding = self.pattern_to_go_binding(pattern);
                    let type_str = ty
                        .as_ref()
                        .map(|t| self.type_to_go(t))
                        .or_else(|| self.decl_only_types.get(&binding).cloned())
                        .unwrap_or_else(|| "interface{}".to_string());
                    self.var_go_type.insert(binding.clone(), type_str.clone());
                    let ind = self.indent_str();
                    let _ = writeln!(self.buf, "{ind}var {binding} {type_str}");
                    return Ok(());
                }
                // `let x = expr?` — the `?` operand is unwrapped (or the enclosing
                // function returns early) into a temp, then bound to `x`. Handled
                // before the general `let` paths so the early-return statements are
                // emitted at statement position (Go has no expression-level
                // early-return). Only a plain `BindPat` target is handled here; a
                // destructuring `let (a, b) = expr?` is rare and falls through.
                if let NodeKind::Propagate { expr: inner } = &value.kind {
                    if matches!(pattern.kind, NodeKind::BindPat { .. }) {
                        let binding = self.pattern_to_go_binding(pattern);
                        let payload = self.emit_try_unwrap(inner)?;
                        let ind = self.indent_str();
                        let _ = writeln!(self.buf, "{ind}{binding} := {payload}");
                        self.writeln(&format!("_ = {binding}"));
                        // Record the Ok element type so a later typed use of the
                        // binding resolves, and track it for shadowing-`let`.
                        if let Some((ok, _)) = self.scrutinee_result_elems(inner) {
                            self.var_go_type.insert(binding.clone(), ok);
                        } else if let Some(elem) = self.scrutinee_optional_elem(inner) {
                            self.var_go_type.insert(binding.clone(), elem);
                        }
                        if binding != "_" {
                            self.go_record_declared(&binding);
                        }
                        return Ok(());
                    }
                }
                // Tuple-destructuring `let (a, b, …) = expr`. Go has no tuple
                // destructuring and `pattern_to_binding_name` collapses a tuple
                // pattern to its *first* element — so without this every later
                // name was dropped (`undefined: total`) and the first bound the
                // whole struct. Hoist the value into a `__tupN` struct local and
                // bind each element off its `.Field{i}` (recursing through the
                // shared bind emitter for a nested element pattern). A bare-`_`
                // tuple pattern still binds nothing.
                if let NodeKind::TuplePat { elems } = &pattern.kind {
                    let n = self.let_tuple_counter;
                    self.let_tuple_counter += 1;
                    let tmp = format!("__tup{n}");
                    let ind = self.indent_str();
                    let _ = write!(self.buf, "{ind}{tmp} := ");
                    // The declared tuple type (when annotated) types each field;
                    // otherwise the struct literal/return value carries its own
                    // Go types and the `.Field{i}` reads inherit them.
                    self.emit_expr(value)?;
                    self.buf.push('\n');
                    self.writeln(&format!("_ = {tmp}"));
                    let decl_ty = ty.as_deref();
                    let field_tys = self.tuple_field_decl_tys(decl_ty, elems.len());
                    for (i, e) in elems.iter().enumerate() {
                        let access = format!("{tmp}.Field{i}");
                        let mut binds = String::new();
                        self.collect_binds_go(
                            e,
                            &access,
                            field_tys.get(i).and_then(|t| *t),
                            &mut binds,
                        );
                        for stmt in binds.split("; ") {
                            let stmt = stmt.trim();
                            if stmt.is_empty() {
                                continue;
                            }
                            self.writeln(stmt);
                            if let Some(name) = stmt.split_whitespace().next() {
                                if name != "_" {
                                    self.go_record_declared(name);
                                }
                            }
                        }
                    }
                    return Ok(());
                }
                let binding = self.pattern_to_go_binding(pattern);
                // Shadowing re-bind of a name already declared in this Go block
                // (the immutable-update idiom, `let acc = …; let acc = f(acc)`,
                // and the todo-list example's `let list = list.add(…)`). Go's
                // `:=` / `var` reject a re-declaration ("no new variables on left
                // side of :="), so lower it to a plain assignment `acc = …`. Only
                // a simple `BindPat` participates — a tuple/record destructure or
                // `_` is not the rebind idiom and keeps its declaration. The
                // existing `var_*`-scope recording below is skipped for a reassign
                // (the name's type is fixed by its first declaration), but the
                // value's expected-type hint is preserved so a branchy RHS still
                // lowers to a correctly-typed IIFE.
                if matches!(pattern.kind, NodeKind::BindPat { .. })
                    && binding != "_"
                    && self.go_name_declared_in_block(&binding)
                {
                    let ind = self.indent_str();
                    let _ = write!(self.buf, "{ind}{binding} = ");
                    let prev_expected_type = self.current_expected_type.take();
                    if let Some(existing) = self.var_go_type.get(&binding) {
                        self.current_expected_type = Some(existing.clone());
                    }
                    let prev_expected = self.expected_collection_elem.take();
                    if matches!(
                        value.kind,
                        NodeKind::ListLiteral { .. }
                            | NodeKind::MapLiteral { .. }
                            | NodeKind::SetLiteral { .. }
                    ) {
                        if let Some(t) = ty {
                            self.expected_collection_elem = self.collection_elem_go_types(t);
                        }
                    }
                    self.emit_expr(value)?;
                    self.current_expected_type = prev_expected_type;
                    self.expected_collection_elem = prev_expected;
                    self.buf.push('\n');
                    return Ok(());
                }
                if let Some(t) = ty {
                    // Record the full declared type node so a later `match` on
                    // this binding can peel a *nested* Optional/Result to assert
                    // a tuple payload to its concrete struct (mirrors the param
                    // path; see `var_decl_type_node`).
                    self.var_decl_type_node
                        .insert(self.pattern_to_binding_name(pattern), (**t).clone());
                    // Record an `Optional[T]` binding's element type so a later
                    // `match binding { Some(x) => ... }` can type-assert `x`.
                    if let Some(elem) = self.optional_elem_go_type(t) {
                        self.var_optional_elem
                            .insert(self.pattern_to_binding_name(pattern), elem);
                    }
                    // Record a `List[T]` binding's element type so a later
                    // `match binding.get(i) { Some(x) => ... }` can type-assert
                    // the `interface{}` payload.
                    if let Some(elem) = self.list_elem_go_type(t) {
                        self.var_list_elem
                            .insert(self.pattern_to_binding_name(pattern), elem);
                    }
                    // Record a `Map[K, V]` / `Set[E]` binding's element Go types
                    // so a later built-in method (`m.get(k)`, `s.contains(x)`,
                    // …) lowers its inline closures over the concretely-typed
                    // receiver `map[K]V` / `map[E]struct{}`.
                    if let Some(kv) = self.map_kv_go_types(t) {
                        self.var_map_kv
                            .insert(self.pattern_to_binding_name(pattern), kv);
                    }
                    if let Some(elem) = self.set_elem_go_type(t) {
                        self.var_set_elem
                            .insert(self.pattern_to_binding_name(pattern), elem);
                    }
                    // Record a `Result[T, E]` binding's Ok/Err types so a later
                    // `match binding { Ok(v) => ...; Err(e) => ... }` can
                    // type-assert the bound payload.
                    if let Some(elems) = self.result_elem_go_types(t) {
                        self.var_result_elem
                            .insert(self.pattern_to_binding_name(pattern), elems);
                    }
                    // Record a generic-record binding's concrete instantiation
                    // (`let c: ListIter[Int]` → `("ListIter", ["int64"])`) so a
                    // later `match c.next() { Some(x) => ... }` resolves the
                    // generic `Optional[T]` payload to the concrete arg (`int64`)
                    // rather than the undefined-in-caller `T`.
                    if let Some(record_args) = self.record_type_args(t) {
                        self.var_record_type_args
                            .insert(self.pattern_to_binding_name(pattern), record_args);
                    }
                    let type_str = self.type_to_go(t);
                    // Record the binding's rendered Go type so a later use as a
                    // call argument (`max_of(noKeys)` where `noKeys: List[Key]`)
                    // resolves to `[]Key`, letting a generic callee bind its
                    // element type from the argument rather than collapsing to
                    // `[any]`. Function-scoped: params save/restore `var_go_type`.
                    if let NodeKind::BindPat { name, .. } = &pattern.kind {
                        self.var_go_type
                            .insert(go_value_ident(&name.name), type_str.clone());
                    }
                    let ind = self.indent_str();
                    let _ = write!(self.buf, "{ind}var {binding} {type_str} = ");
                    // When the binding *value* is itself a collection literal,
                    // it takes its element type(s) from the declared type, so an
                    // empty `[]` (or under-inferred literal) matches the declared
                    // `[]T` / `map[K]V` rather than falling back to
                    // `[]interface{}`. Guarded to the top-level literal so the
                    // hint never leaks to a nested/argument literal whose own
                    // type may differ.
                    let prev_expected = self.expected_collection_elem.take();
                    if matches!(
                        value.kind,
                        NodeKind::ListLiteral { .. }
                            | NodeKind::MapLiteral { .. }
                            | NodeKind::SetLiteral { .. }
                    ) {
                        self.expected_collection_elem = self.collection_elem_go_types(t);
                    }
                    // The binding's declared Go type is the expected type for the
                    // value expression. An expression-position `match`/`if` lowers
                    // to an IIFE whose return must be this `T` (not the enclosing
                    // function's return type), so `let x: T = match …` is
                    // assignable. Restored after the value so it never leaks.
                    let prev_expected_type = self.current_expected_type.take();
                    self.current_expected_type = Some(type_str.clone());
                    self.emit_expr(value)?;
                    self.current_expected_type = prev_expected_type;
                    self.expected_collection_elem = prev_expected;
                    self.buf.push('\n');
                } else {
                    // Propagate a `Map`/`Set` element type onto an untyped
                    // binding whose value returns a map/set (`let m2 =
                    // base.set(k, v)`, `let s2 = s.add(x)`), so a later built-in
                    // on `m2`/`s2` lowers its inline closure over the concrete
                    // `map[K]V` / `map[E]struct{}` rather than `interface{}`.
                    if let Some(kv) = self.value_map_kv_go_types(value) {
                        self.var_map_kv
                            .insert(self.pattern_to_binding_name(pattern), kv);
                    }
                    if let Some(elem) = self.value_set_elem_go_type(value) {
                        self.var_set_elem
                            .insert(self.pattern_to_binding_name(pattern), elem);
                    }
                    // Record an untyped binding's concrete generic-record args
                    // when its value is a call returning one (`__it := bag.Iter()`
                    // → `("ListIterator", ["int64"])`), so a later
                    // `match __it.next() { Some(x) => ... }` asserts the payload
                    // to the concrete arg. This is the `for x in <Iterable>`
                    // desugar case, whose gensym binding carries no annotation.
                    if let Some(record_args) = self.value_record_type_args(value) {
                        self.var_record_type_args
                            .insert(self.pattern_to_binding_name(pattern), record_args);
                    }
                    // Record an untyped `Result`-typed binding's `(ok, err)` Go
                    // payload types when its value is a call to a `Result`-returning
                    // fn (`step1 := eval(...)`) or a bare `Ok(v)`/`Err(e)`. Without
                    // this, a later `match step1 { Ok(v) => ... }` binds `v` from the
                    // boxed `interface{}` payload un-asserted, leaving `v` as
                    // `interface{}` and failing a downstream use that expects the
                    // concrete type (e.g. `eval(OpMul, v, 2.0)` wanting `float64`).
                    if let Some(elems) = self.value_result_elem_go_types(value) {
                        self.var_result_elem
                            .insert(self.pattern_to_binding_name(pattern), elems);
                    }
                    // Record an untyped `Optional`-typed binding's element Go type
                    // when its value is a call/method returning `Optional[T]`
                    // (`raw := storage.read(key)`). Reuses the scrutinee resolver,
                    // which already maps an effect-/user-method's `Optional[T]`
                    // return to its concrete element. Without this, a later
                    // `match raw { Some(v) => ... }` binds `v` from the boxed
                    // `interface{}` un-asserted, so a typed-IIFE arm (`return v`
                    // where `T` is `string`) fails the Go build.
                    if let Some(elem) = self.scrutinee_optional_elem(value) {
                        self.var_optional_elem
                            .insert(self.pattern_to_binding_name(pattern), elem);
                    }
                    // Record an untyped `List`-typed binding's Go element type — a
                    // homogeneous list literal (`let items = [Item{1}, Item{2}]` →
                    // `Item`) or a list-combinator result (`let updated =
                    // items.map(..)`, `let evens = xs.filter(..)`). Without this a
                    // later `updated.map((it) => …)` cannot type the closure param
                    // `it` to `Item` (it falls back to `interface{}`, so `it.title`
                    // and the `[]Item` result both fail), and a use of the binding
                    // as a typed call argument erases to `[]interface{}`.
                    if let Some(elem) = self.value_list_elem_go_type(value) {
                        let name = self.pattern_to_binding_name(pattern);
                        self.var_list_elem.insert(name.clone(), elem.clone());
                        self.var_go_type.insert(name, format!("[]{elem}"));
                    }
                    // Record an untyped binding to a record-returning value (`let
                    // form = create_form()` → `FormState`), so a later built-in
                    // collection method on one of its fields (`form.fields.keys()`,
                    // `form.fields.get(k)`) resolves the field's declared `Map[K,
                    // V]` / `List[T]` types through `map_receiver_kv_go_types` /
                    // the list analogue rather than erasing to the
                    // `map[interface{}]interface{}` / `[]interface{}` Go rejects
                    // against the concretely-typed struct field. Scoped to records
                    // that actually have such a field (the only consumers), so this
                    // never over-records a binding's Go type.
                    if !matches!(value.kind, NodeKind::Lambda { .. }) {
                        if let Some(go_ty) = self.infer_go_expr_type(value) {
                            let head = Self::go_type_record_head(&go_ty);
                            if self.record_field_map_kv.contains_key(head)
                                || self.record_field_list_elem.contains_key(head)
                            {
                                self.var_go_type
                                    .insert(self.pattern_to_binding_name(pattern), go_ty);
                            }
                        }
                    }
                    // Record an untyped binding to a lambda → the lambda's inferred
                    // Go return type, so a later compose `f >> binding` can resolve
                    // its own output type from `binding` (the outer local lambda).
                    if let NodeKind::Lambda { params, body } = &value.kind {
                        let saved = self.enter_param_go_types_with_expected(params, None);
                        let ret = self.infer_block_tail_type(body);
                        self.var_go_type = saved;
                        if let Some(r) = ret {
                            self.var_lambda_ret
                                .insert(self.pattern_to_binding_name(pattern), r);
                        }
                    }
                    // An untyped `let m = if (..) { Text } else { Image }` lowers
                    // its value to an expression IIFE. Without an expected type the
                    // IIFE falls back to the enclosing fn's return type
                    // (`current_fn_ret_type`, e.g. `__bockResult` inside a
                    // `Result`-returning fn), which a user-enum variant value
                    // (`MessageType`) is not assignable to. Infer the value's Go
                    // type structurally (the enum a variant branch/arm yields) and
                    // record it as the binding's expected type so the IIFE is typed
                    // `func() MessageType { … }`. Scoped to the value emit; never
                    // leaks. Only `if`/`match` values need this — a direct value
                    // emit is already concretely typed.
                    let prev_expected_type = self.current_expected_type.take();
                    if matches!(value.kind, NodeKind::If { .. } | NodeKind::Match { .. }) {
                        if let Some(inferred) = self.infer_branchy_expr_type(value) {
                            self.current_expected_type = Some(inferred.clone());
                            // Also record it for the binding so a later use of the
                            // binding (e.g. as a struct field) resolves to the enum.
                            self.var_go_type
                                .insert(self.pattern_to_binding_name(pattern), inferred);
                        }
                    }
                    let ind = self.indent_str();
                    // Bock `Int` is `int64`, but Go infers an *untyped integer
                    // constant* (`0`, `lo + 1`) as the default `int` under `:=`.
                    // A later mix with an `int64` value (`i >= int64(len(xs))`,
                    // `total + p` where `p: Int`, or a struct field `Level int64`)
                    // then fails `mismatched types int and int64`. When the value's
                    // structural Go type is `int64`, pin the binding with an explicit
                    // `var x int64 = …` and record it so downstream uses agree. Only
                    // `int64` needs this — `float64`/`bool`/`string` literals already
                    // infer to the matching Go type under `:=`. Skipped when the value
                    // is an `if`/`match`/`loop` (those lower to a typed IIFE) or a
                    // collection/record literal (handled above), to keep the targeted
                    // surface minimal.
                    //
                    // Restricted to value kinds whose `int64` Go type is *reliable*:
                    // an integer literal, integer arithmetic (`BinaryOp`/`UnaryOp`),
                    // or a known-`int64` identifier. A *call* is deliberately
                    // excluded — a generic fn (`firstOr[T](single(9), -1)`)
                    // monomorphizes `T` to Go's untyped-constant default `int`, so
                    // its *actual* return type is `int`, not the `int64`
                    // `infer_go_expr_type` predicts from the Bock `Int`; pinning
                    // `var x int64` there fails `cannot use … (int) as int64`.
                    let pin_int64_kind = matches!(
                        value.kind,
                        NodeKind::Literal { .. }
                            | NodeKind::BinaryOp { .. }
                            | NodeKind::UnaryOp { .. }
                            | NodeKind::Identifier { .. }
                    );
                    let pin_int64 = pin_int64_kind
                        && self.infer_go_expr_type(value).as_deref() == Some("int64");
                    if pin_int64 {
                        self.var_go_type
                            .insert(self.pattern_to_binding_name(pattern), "int64".to_string());
                        let _ = write!(self.buf, "{ind}var {binding} int64 = ");
                    } else {
                        let _ = write!(self.buf, "{ind}{binding} := ");
                    }
                    self.emit_expr(value)?;
                    self.current_expected_type = prev_expected_type;
                    self.buf.push('\n');
                }
                // Record this name as declared in the current Go block scope so a
                // later same-name `let` in the same block reassigns (see the
                // shadowing short-circuit above). Only a simple `BindPat`
                // introduces a tracked declaration.
                if matches!(pattern.kind, NodeKind::BindPat { .. }) && binding != "_" {
                    self.go_record_declared(&binding);
                }
                Ok(())
            }
            NodeKind::If {
                let_pattern,
                condition,
                then_block,
                else_block,
            } => {
                if let Some(pat) = let_pattern {
                    let binding = self.pattern_to_go_binding(pat);
                    let ind = self.indent_str();
                    let _ = write!(self.buf, "{ind}{binding} := ");
                    self.emit_expr(condition)?;
                    self.buf.push('\n');
                    self.writeln(&format!("if {binding} != nil {{"));
                    self.indent += 1;
                    self.emit_block_body(then_block)?;
                    self.indent -= 1;
                } else {
                    let ind = self.indent_str();
                    let _ = write!(self.buf, "{ind}if ");
                    self.emit_expr(condition)?;
                    self.buf.push_str(" {\n");
                    self.indent += 1;
                    self.emit_block_body(then_block)?;
                    self.indent -= 1;
                }
                if let Some(else_b) = else_block {
                    if matches!(else_b.kind, NodeKind::If { .. }) {
                        let ind = self.indent_str();
                        let _ = write!(self.buf, "{ind}}} else ");
                        // Emit the if without leading indent.
                        self.emit_if_continued(else_b)?;
                        return Ok(());
                    }
                    self.writeln("} else {");
                    self.indent += 1;
                    self.emit_block_body(else_b)?;
                    self.indent -= 1;
                }
                self.writeln("}");
                Ok(())
            }
            NodeKind::For {
                pattern,
                iterable,
                body,
            } => {
                let mut binding = self.pattern_to_go_binding(pattern);
                // Go rejects a `range` loop variable that is never read
                // (`declared and not used`), which Bock permits (`for x in data {
                // count = count + 1 }`). When the bound name is a plain
                // identifier not referenced in the body, emit `_` instead.
                if let NodeKind::BindPat { name, .. } = &pattern.kind {
                    let go_name = go_value_ident(&name.name);
                    if go_name == binding && !collect_used_idents(body).contains(&name.name) {
                        binding = "_".to_string();
                    }
                }
                self.emit_loop_label_prefix(body);
                let ind = self.indent_str();
                // `for _, _ := range x` is invalid Go ("no new variables on left
                // side of :="); when the value var is discarded too, drop the
                // assignment entirely (`for range x`).
                if binding == "_" {
                    let _ = write!(self.buf, "{ind}for range ");
                } else {
                    let _ = write!(self.buf, "{ind}for _, {binding} := range ");
                }
                self.emit_expr(iterable)?;
                self.buf.push_str(" {\n");
                self.indent += 1;
                // Record the loop variable's element Go type into the body scope
                // so element arithmetic / typed returns type-check (Go ranges
                // over a concretely-typed `[]T` / range yield a `T`, not
                // `interface{}`). Recoverable when the iterable is a known
                // `List[T]` identifier, a homogeneously-typed list literal, or a
                // range (`int64`). Saved/restored around the body — Go has no
                // block-scoped reset here. Unrecoverable ⇒ left absent, so
                // inference yields the `interface{}` fallback, never a wrong
                // type.
                let saved_go_types = self.var_go_type.clone();
                if let (NodeKind::BindPat { name, .. }, Some(elem)) =
                    (&pattern.kind, self.for_loop_elem_go_type(iterable))
                {
                    self.var_go_type.insert(go_value_ident(&name.name), elem);
                }
                self.emit_block_body(body)?;
                self.var_go_type = saved_go_types;
                self.indent -= 1;
                self.writeln("}");
                self.loop_labels.pop();
                Ok(())
            }
            NodeKind::While { condition, body } => {
                self.emit_loop_label_prefix(body);
                let ind = self.indent_str();
                let _ = write!(self.buf, "{ind}for ");
                self.emit_expr(condition)?;
                self.buf.push_str(" {\n");
                self.indent += 1;
                self.emit_block_body(body)?;
                self.indent -= 1;
                self.writeln("}");
                self.loop_labels.pop();
                Ok(())
            }
            NodeKind::Loop { body } => {
                // A statement-position `loop` is a plain `for {}`. Its body is no
                // longer the value-IIFE body of any enclosing expression-`loop`, so
                // reset `loop_expr_depth` for the body: a `break <v>` here is the
                // (value-dropping) statement case, not a `return`.
                let saved_loop_expr = self.loop_expr_depth;
                self.loop_expr_depth = 0;
                self.emit_loop_label_prefix(body);
                self.writeln("for {");
                self.indent += 1;
                self.emit_block_body(body)?;
                self.indent -= 1;
                self.writeln("}");
                self.loop_labels.pop();
                self.loop_expr_depth = saved_loop_expr;
                Ok(())
            }
            NodeKind::Return { value } => {
                if let Some(val) = value {
                    let ind = self.indent_str();
                    let _ = write!(self.buf, "{ind}return ");
                    // A collection literal in return position adopts the
                    // function's return collection element type(s), so `return
                    // [x]` in `fn single[T](x: T) -> List[T]` emits `[]T{x}` (not
                    // the `[]interface{}{x}` bare-literal inference falls back to,
                    // which is not assignable to the `[]T` return). Guarded to a
                    // top-level collection literal and consumed at the literal so
                    // it never leaks to a nested/argument literal.
                    let prev_expected = self.expected_collection_elem.take();
                    if matches!(
                        val.kind,
                        NodeKind::ListLiteral { .. }
                            | NodeKind::MapLiteral { .. }
                            | NodeKind::SetLiteral { .. }
                    ) {
                        self.expected_collection_elem = self.current_fn_ret_collection_elem.clone();
                    }
                    // A generic-record construction in explicit-`return` position
                    // adopts the function's return type for its args (see
                    // `emit_block_body_inner`'s tail-return arm).
                    let prev_expected_type = self.current_expected_type.take();
                    if matches!(
                        val.kind,
                        NodeKind::RecordConstruct { .. } | NodeKind::TupleLiteral { .. }
                    ) {
                        self.current_expected_type = self.current_fn_ret_type.clone();
                    }
                    self.emit_expr(val)?;
                    self.expected_collection_elem = prev_expected;
                    self.current_expected_type = prev_expected_type;
                    self.buf.push('\n');
                } else {
                    self.writeln("return");
                }
                Ok(())
            }
            NodeKind::Break { value } => {
                // Inside an expression-position `loop` IIFE, `break <v>` is the
                // loop's value: lower it to `return <v>` (out of the IIFE). The
                // value-dropping `// break value:` comment below would otherwise
                // discard it and leave the IIFE with no return at all.
                if self.loop_expr_depth > 0 {
                    if let Some(val) = value {
                        let ind = self.indent_str();
                        let _ = write!(self.buf, "{ind}return ");
                        self.emit_expr(val)?;
                        self.buf.push('\n');
                        return Ok(());
                    }
                    // A bare `break` inside a value `loop` cannot produce the
                    // loop's value; fall through to the ordinary `break` below
                    // (the IIFE's trailing `panic` covers the unreachable tail).
                }
                if let Some(val) = value {
                    let ind = self.indent_str();
                    let _ = write!(self.buf, "{ind}// break value: ");
                    self.emit_expr(val)?;
                    self.buf.push('\n');
                }
                // Inside a statement-arm `switch`, a bare `break` would exit
                // the switch; target the enclosing loop's label instead.
                if self.switch_label_depth > 0 {
                    if let Some(label) = self.innermost_loop_label() {
                        self.writeln(&format!("break {label}"));
                        return Ok(());
                    }
                }
                self.writeln("break");
                Ok(())
            }
            NodeKind::Continue => {
                // `continue` already targets the loop even from inside a switch,
                // but use the label when one is in scope for symmetry/clarity.
                if self.switch_label_depth > 0 {
                    if let Some(label) = self.innermost_loop_label() {
                        self.writeln(&format!("continue {label}"));
                        return Ok(());
                    }
                }
                self.writeln("continue");
                Ok(())
            }
            NodeKind::Guard {
                let_pattern,
                condition,
                else_block,
            } => {
                if let Some(pat) = let_pattern {
                    return self.emit_guard_let(pat, condition, else_block);
                }
                let ind = self.indent_str();
                let _ = write!(self.buf, "{ind}if !(");
                self.emit_expr(condition)?;
                self.buf.push_str(") {\n");
                self.indent += 1;
                self.emit_block_body(else_block)?;
                self.indent -= 1;
                self.writeln("}");
                Ok(())
            }
            NodeKind::Match { scrutinee, arms } => self.emit_match(scrutinee, arms),
            NodeKind::Block { stmts, tail } => {
                self.seed_decl_only_types(stmts);
                for s in stmts {
                    self.emit_node(s)?;
                }
                if let Some(t) = tail {
                    self.write_indent();
                    self.emit_expr(t)?;
                    self.buf.push('\n');
                }
                Ok(())
            }
            NodeKind::HandlingBlock { handlers, body } => {
                // handling block → scoped handler instantiation. The emitted
                // `{ … }` is its own Go block scope, so it gets a fresh
                // `go_declared_scopes` frame: a name first bound in one
                // `handling` block and re-bound in a *sibling* `handling` block
                // is two independent declarations (each block-scoped), not a
                // redeclaration. Without a fresh frame the redeclaration tracker
                // would carry the prior block's `declared` set into this one and
                // rewrite the second `let part = …` into a bare `part = …` — a
                // name that left scope when the first block closed (Go rejects it
                // as `undefined: part`). Mirrors the js/ts fix
                // (Q-js-handling-let-redeclaration, #371) on the Go backend
                // (Q-go-handling-let-redeclaration).
                self.writeln("{");
                self.indent += 1;
                self.go_declared_scopes.push(HashSet::new());
                let old_handler_vars = self.current_handler_vars.clone();
                let mut new_var_names = Vec::with_capacity(handlers.len());
                for h in handlers {
                    let effect_name = h
                        .effect
                        .segments
                        .last()
                        .map_or("effect", |s| s.name.as_str());
                    let var_name = format!("__{}", to_camel_case(effect_name));
                    let ind = self.indent_str();
                    let _ = write!(self.buf, "{ind}{var_name} := ");
                    self.emit_expr(&h.handler)?;
                    self.buf.push('\n');
                    self.current_handler_vars
                        .insert(effect_name.to_string(), var_name.clone());
                    new_var_names.push(var_name);
                }
                // Suppress Go's "declared but not used" error when a handler
                // is declared in an outer handling scope and only referenced
                // indirectly through inner handling blocks (or not at all).
                for v in &new_var_names {
                    self.writeln(&format!("_ = {v}"));
                }
                if let NodeKind::Block { stmts, tail } = &body.kind {
                    self.seed_decl_only_types(stmts);
                    for s in stmts {
                        self.emit_node(s)?;
                    }
                    if let Some(t) = tail {
                        self.write_indent();
                        self.emit_expr(t)?;
                        self.buf.push('\n');
                    }
                } else {
                    self.emit_stmt(body)?;
                }
                self.current_handler_vars = old_handler_vars;
                self.go_declared_scopes.pop();
                self.indent -= 1;
                self.writeln("}");
                Ok(())
            }
            NodeKind::Assign { op, target, value } => {
                let ind = self.indent_str();
                let _ = write!(self.buf, "{ind}");
                self.emit_expr(target)?;
                let op_str = match op {
                    AssignOp::Assign => " = ",
                    AssignOp::AddAssign => " += ",
                    AssignOp::SubAssign => " -= ",
                    AssignOp::MulAssign => " *= ",
                    AssignOp::DivAssign => " /= ",
                    AssignOp::RemAssign => " %= ",
                };
                self.buf.push_str(op_str);
                self.emit_expr(value)?;
                self.buf.push('\n');
                Ok(())
            }
            _ => {
                self.write_indent();
                self.emit_expr(node)?;
                self.buf.push('\n');
                Ok(())
            }
        }
    }

    /// Emit an if statement that continues after an `} else`.
    fn emit_if_continued(&mut self, node: &AIRNode) -> Result<(), CodegenError> {
        if let NodeKind::If {
            condition,
            then_block,
            else_block,
            ..
        } = &node.kind
        {
            let _ = write!(self.buf, "if ");
            self.emit_expr(condition)?;
            self.buf.push_str(" {\n");
            self.indent += 1;
            self.emit_block_body(then_block)?;
            self.indent -= 1;
            if let Some(else_b) = else_block {
                if matches!(else_b.kind, NodeKind::If { .. }) {
                    let ind = self.indent_str();
                    let _ = write!(self.buf, "{ind}}} else ");
                    return self.emit_if_continued(else_b);
                }
                self.writeln("} else {");
                self.indent += 1;
                self.emit_block_body(else_b)?;
                self.indent -= 1;
            }
            self.writeln("}");
        }
        Ok(())
    }

    // ── Expressions ─────────────────────────────────────────────────────────

    fn emit_expr(&mut self, node: &AIRNode) -> Result<(), CodegenError> {
        match &node.kind {
            NodeKind::Literal { lit } => {
                match lit {
                    Literal::Int(s) => self.buf.push_str(s),
                    Literal::Float(s) => self.buf.push_str(s),
                    Literal::Bool(b) => self.buf.push_str(if *b { "true" } else { "false" }),
                    Literal::Char(s) => {
                        self.buf.push('\'');
                        self.buf.push_str(s);
                        self.buf.push('\'');
                    }
                    Literal::String(s) => {
                        self.buf.push('"');
                        self.buf.push_str(&escape_go_string(s));
                        self.buf.push('"');
                    }
                    Literal::Unit => self.buf.push_str("nil"),
                }
                Ok(())
            }
            NodeKind::Identifier { name } => {
                if name.name == "None" {
                    self.buf.push_str("__bockNone");
                    return Ok(());
                }
                // Prelude `Ordering` variant → the bare `__bockOrdering` constant
                // (`Less`/`Equal`/`Greater`) of the Ordering runtime, which a
                // value-switch `case Less:` and the `compare` bridge also use —
                // UNLESS the real `core.compare.Ordering` enum is reachable, in
                // which case the reference is a user-enum variant-struct
                // construction (`OrderingLess{}`), handled by the path below.
                if crate::generator::ordering_variant(&name.name).is_some()
                    && !self.ordering_enum_reachable()
                {
                    self.buf.push_str(&name.name);
                    return Ok(());
                }
                // A unit-variant reference (`Empty`) → an empty variant-struct
                // literal `ShapeEmpty{}`. For a *generic* enum the variant struct
                // carries the enum's type params (`type BoxEmpty[T any] struct{}`),
                // so the literal must spell the concrete instantiation
                // (`BoxEmpty[int64]{}`) drawn from the binding's expected type —
                // Go rejects a bare generic struct literal.
                if let Some(enum_name) = self
                    .user_variant_for_name(&name.name)
                    .map(|i| i.enum_name.clone())
                {
                    let type_args = self.expected_enum_variant_type_arg_suffix(&enum_name);
                    let _ = write!(self.buf, "{enum_name}{}{type_args}{{}}", name.name);
                    return Ok(());
                }
                // A module-scope `const` is emitted verbatim at its declaration;
                // spell its use site identically (the `go_fn_name` transform would
                // camelCase a SCREAMING_SNAKE name, e.g. `FIZZ_NUM` → `fizzNUM`).
                if self.const_names.contains(&name.name) {
                    self.buf.push_str(&name.name);
                    return Ok(());
                }
                let emitted = if is_prelude_ctor(&name.name) {
                    name.name.clone()
                } else if self.local_shadows_public_fn(&name.name) {
                    // An in-scope local (param/`let`/bind) shadows a same-named
                    // public module fn — spell the *local*, not the PascalCased
                    // helper (Q-go-runtime-helper-shadowing).
                    go_value_ident(&name.name)
                } else {
                    // Routes a public name colliding with a type through the
                    // `Fn`-suffix rename (`key` → `KeyFn`); a private name is
                    // camelCased.
                    self.go_fn_name(&name.name)
                };
                self.buf.push_str(&emitted);
                Ok(())
            }
            NodeKind::BinaryOp { op, left, right } => {
                // `+` on two `List[T]` operands is concatenation. Go has no `+`
                // for slices (`operator + not defined on []T`), so build a fresh
                // slice with `append(append([]T{}, a...), b...)`. The element type
                // comes from a list-typed operand or the binding's expected type.
                if matches!(op, BinOp::Add) && crate::generator::is_list_concat(node, left, right) {
                    let elem = self
                        .list_receiver_elem_go_type(left)
                        .or_else(|| self.list_receiver_elem_go_type(right))
                        .or_else(|| {
                            self.current_expected_type
                                .as_deref()
                                .and_then(|t| t.strip_prefix("[]"))
                                .map(str::to_string)
                        })
                        .unwrap_or_else(|| "interface{}".to_string());
                    // A list-literal operand must adopt `elem` as its element type
                    // — a `[]interface{}{x}` literal is not assignable to the
                    // `[]elem` slice `append` builds. Thread the expected element
                    // into each literal operand (mirrors the `concat` method).
                    let emit_operand =
                        |this: &mut Self, n: &AIRNode| -> Result<String, CodegenError> {
                            let prev = this.expected_collection_elem.take();
                            if matches!(n.kind, NodeKind::ListLiteral { .. }) {
                                this.expected_collection_elem = Some((elem.clone(), None));
                            }
                            let s = this.expr_to_string(n);
                            this.expected_collection_elem = prev;
                            s
                        };
                    let l = emit_operand(self, left)?;
                    let r = emit_operand(self, right)?;
                    let _ = write!(self.buf, "append(append([]{elem}{{}}, {l}...), {r}...)");
                    return Ok(());
                }
                // `a ** b`: Go has no `**`. A *float* power lowers to `math.Pow`
                // (which takes and returns `float64`); an *integer* power lowers
                // to the `__bockIntPow` runtime helper (stays in `int64`, exact).
                // Operands are coerced to the chosen numeric type so a mixed
                // `2 ** f` (Int literal ** Float) still type-checks.
                if matches!(op, BinOp::Pow) {
                    let l = self.expr_to_string(left)?;
                    let r = self.expr_to_string(right)?;
                    if self.pow_is_float(left, right) {
                        self.needs_math_import = true;
                        let _ = write!(self.buf, "math.Pow(float64({l}), float64({r}))");
                    } else {
                        let _ = write!(self.buf, "__bockIntPow(int64({l}), int64({r}))");
                    }
                    return Ok(());
                }
                // Ordering operators on a user `Comparable` type lower through the
                // type's `Compare` (Go structs are not ordered, so native `<` is a
                // compile error). `Compare` returns the sealed `Ordering` interface;
                // a type assertion against the variant struct yields the boolean,
                // wrapped in an IIFE (Go's comma-ok assertion is statement-only):
                // `a < b` ⇒ `… .(OrderingLess); return __ok`, `a <= b` ⇒
                // `… .(OrderingGreater); return !__ok`, etc.
                if crate::generator::is_user_compare(node) {
                    if let Some((tag, is_eq)) = crate::generator::user_compare_variant(*op) {
                        let recv = self.expr_to_string(left)?;
                        let other = self.expr_to_string(right)?;
                        let method = self.go_method_name("compare", true);
                        let neg = if is_eq { "" } else { "!" };
                        let _ = write!(
                            self.buf,
                            "func() bool {{ _, __ok := ({recv}.{method}({other})).(Ordering{tag}); return {neg}__ok }}()"
                        );
                        return Ok(());
                    }
                }
                // DQ29 (§18.5 structural Equatable): a stamped `==`/`!=` whose
                // operand has an explicit `impl Equatable` dispatches through
                // its `Eq` method (Go's native struct `==` is field-wise and
                // would silently ignore the user's custom equality). The
                // `"deep"` lane — operands involving a `List`/`Map`/`Set`
                // (no native `==`: "slice/map can only be compared to nil") —
                // lowers through the `__bockDeepEq` runtime helper, whose
                // `reflect.DeepEqual` is element-wise for slices and
                // order-independent for maps (Bock `Map`/`Set`). The
                // `"deep_custom"` lane (DQ31, §18.5) — a container whose element
                // tree carries an explicit `impl Equatable` — routes through
                // `__bockEqCustom`, which dispatches element comparison and
                // Map-key / Set-member matching through the element's `Eq`. The
                // `"structural"` and `"generic"` lanes stay native: Go struct/
                // interface equality is already field-wise (tag-then-payload
                // for the enum interface form), and a `comparable`-constrained
                // type param compares natively.
                if matches!(op, BinOp::Eq | BinOp::Ne) {
                    match crate::generator::user_eq_kind(node) {
                        Some("impl") => {
                            let recv = self.expr_to_string(left)?;
                            let other = self.expr_to_string(right)?;
                            let method = self.go_method_name("eq", true);
                            let neg = if *op == BinOp::Ne { "!" } else { "" };
                            let _ = write!(self.buf, "{neg}({recv}).{method}({other})");
                            return Ok(());
                        }
                        Some("deep") => {
                            let recv = self.expr_to_string(left)?;
                            let other = self.expr_to_string(right)?;
                            let neg = if *op == BinOp::Ne { "!" } else { "" };
                            let _ = write!(self.buf, "{neg}__bockDeepEq({recv}, {other})");
                            return Ok(());
                        }
                        // DQ31: a container whose element tree carries a custom
                        // `impl Equatable` routes through `__bockEqCustom`,
                        // which dispatches element comparison (and Map-key /
                        // Set-member matching) through the element's `Eq`
                        // method rather than `reflect.DeepEqual`.
                        Some("deep_custom") => {
                            let recv = self.expr_to_string(left)?;
                            let other = self.expr_to_string(right)?;
                            let neg = if *op == BinOp::Ne { "!" } else { "" };
                            let _ = write!(self.buf, "{neg}__bockEqCustom({recv}, {other})");
                            return Ok(());
                        }
                        _ => {}
                    }
                }
                self.buf.push('(');
                self.emit_expr(left)?;
                let op_str = match op {
                    BinOp::Add => " + ",
                    BinOp::Sub => " - ",
                    BinOp::Mul => " * ",
                    BinOp::Div => " / ",
                    BinOp::Rem => " % ",
                    // `Pow` is lowered above (math.Pow / __bockIntPow) and never
                    // reaches this arm; kept for match exhaustiveness.
                    BinOp::Pow => " /* pow */ ",
                    BinOp::Eq => " == ",
                    BinOp::Ne => " != ",
                    BinOp::Lt => " < ",
                    BinOp::Le => " <= ",
                    BinOp::Gt => " > ",
                    BinOp::Ge => " >= ",
                    BinOp::And => " && ",
                    BinOp::Or => " || ",
                    BinOp::BitAnd => " & ",
                    BinOp::BitOr => " | ",
                    BinOp::BitXor => " ^ ",
                    BinOp::Compose => " /* compose */ ",
                    BinOp::Is => " == ",
                };
                self.buf.push_str(op_str);
                self.emit_expr(right)?;
                self.buf.push(')');
                Ok(())
            }
            NodeKind::UnaryOp { op, operand } => {
                let op_str = match op {
                    UnaryOp::Neg => "-",
                    UnaryOp::Not => "!",
                    UnaryOp::BitNot => "^",
                };
                self.buf.push_str(op_str);
                self.emit_expr(operand)?;
                Ok(())
            }
            NodeKind::Call {
                callee,
                args,
                type_args,
            } => {
                // Effect operation Call → handler.Op rewriting.
                if let NodeKind::Identifier { name } = &callee.kind {
                    if let Some(effect_name) = self.effect_ops.get(&name.name).cloned() {
                        if let Some(handler_var) =
                            self.current_handler_vars.get(&effect_name).cloned()
                        {
                            let _ =
                                write!(self.buf, "{}.{}", handler_var, to_pascal_case(&name.name));
                            self.buf.push('(');
                            for (i, arg) in args.iter().enumerate() {
                                if i > 0 {
                                    self.buf.push_str(", ");
                                }
                                self.emit_expr(&arg.value)?;
                            }
                            self.buf.push(')');
                            return Ok(());
                        }
                    }
                }
                if let Some(code) = self.map_prelude_call(callee, args)? {
                    self.buf.push_str(&code);
                    return Ok(());
                }
                // A call whose callee names a registered tuple variant is a
                // construction → the variant-struct literal
                // `ShapeRect{Field0: 3.0, Field1: 4.0}`. For a *generic* enum the
                // variant struct carries the enum's type params
                // (`type BoxFull[T any] struct{…}`), so the literal must spell the
                // concrete instantiation (`BoxFull[int64]{…}`) from the binding's
                // expected type — Go does not infer struct type args from the
                // field values, and rejects a bare generic struct literal.
                if let NodeKind::Identifier { name } = &callee.kind {
                    if let Some(enum_name) = self
                        .user_variant_for_name(&name.name)
                        .map(|i| i.enum_name.clone())
                    {
                        let type_args = self.expected_enum_variant_type_arg_suffix(&enum_name);
                        let _ = write!(self.buf, "{enum_name}{}{type_args}{{", name.name);
                        for (i, arg) in args.iter().enumerate() {
                            if i > 0 {
                                self.buf.push_str(", ");
                            }
                            let _ = write!(self.buf, "Field{i}: ");
                            self.emit_expr(&arg.value)?;
                        }
                        self.buf.push('}');
                        return Ok(());
                    }
                }
                if self.try_emit_time_assoc_call(callee, args)? {
                    return Ok(());
                }
                if self.try_emit_time_desugared_method(node, callee, args)? {
                    return Ok(());
                }
                if self.try_emit_concurrency_call(callee, args)? {
                    return Ok(());
                }
                // Map/Set dispatch precedes the List recogniser so the
                // overlapping method names route by `recv_kind`, not by name.
                if self.try_emit_map_method(node, callee, args)? {
                    return Ok(());
                }
                if self.try_emit_set_method(node, callee, args)? {
                    return Ok(());
                }
                // String method dispatch runs *before* the List recogniser so the
                // overlapping `len`/`contains`/`is_empty` names route by the
                // checker's `recv_kind = "Primitive:String"`, not by name alone —
                // the fix for `String.contains` being misrouted to the List scan.
                if self.try_emit_string_method(node, callee, args)? {
                    return Ok(());
                }
                // Numeric/Char/Bool primitive methods (`to_float`/`abs`/`sqrt`/…)
                // likewise route by the checker's `recv_kind = "Primitive:Int|…"`
                // before the generic fall-through, which would emit `n.toFloat(n)`.
                if self.try_emit_numeric_method(node, callee, args)? {
                    return Ok(());
                }
                if self.try_emit_list_method(node, callee, args)? {
                    return Ok(());
                }
                if self.try_emit_list_inplace_mutator(node, callee, args)? {
                    return Ok(());
                }
                if self.try_emit_list_functional_method(node, callee, args)? {
                    return Ok(());
                }
                if self.try_emit_primitive_bridge(node, callee, args)? {
                    return Ok(());
                }
                if self.try_emit_trait_bound_bridge(node, callee, args)? {
                    return Ok(());
                }
                if self.try_emit_container_method(node, callee, args)? {
                    return Ok(());
                }
                // Q-prim-assoc: a primitive associated-conversion call
                // (`Float.from(x)` / `Int.try_from(s)` / `String.from(c)`)
                // lowers to Go's native conversion, NOT the free-function form
                // below (`Float_from` is undefined).
                if self.try_emit_primitive_conversion(node, callee, args)? {
                    return Ok(());
                }
                // Associated-function call (`Type.method(args)` — stamped by the
                // lowerer, no `self` prepended). Go has no static methods, so the
                // definition is a free function `Type_Method(...)`
                // (`emit_associated_fn`); emit the matching free-function call.
                // The `public_methods` check picks the same Pascal/camel casing
                // the definition used (trait-impl associated fns are always
                // exported).
                if crate::generator::is_associated_call(node) {
                    if let NodeKind::FieldAccess { object, field } = &callee.kind {
                        if let NodeKind::Identifier { name: type_name } = &object.kind {
                            let is_public = self.public_methods.contains(&field.name);
                            let fn_name =
                                self.freefn_lowered_name(&type_name.name, &field.name, is_public);
                            let _ = write!(self.buf, "{fn_name}(");
                            for (i, arg) in args.iter().enumerate() {
                                if i > 0 {
                                    self.buf.push_str(", ");
                                }
                                self.emit_expr(&arg.value)?;
                            }
                            self.buf.push(')');
                            return Ok(());
                        }
                    }
                }
                // Desugared instance method call `Call(FieldAccess(recv, m),
                // [recv, ...rest])`: emit `recv.M(rest)` using Go method casing
                // so the receiver flows through the native `self` receiver
                // rather than as a duplicated `interface{}` argument.
                if let Some((recv, method, rest)) =
                    crate::generator::desugared_self_call(callee, args)
                {
                    // DQ28 free-function lowering: a generic method
                    // (`box.map(f)`) lowers to a free function call
                    // `Box_Map(box, f)` — the receiver leads as the first
                    // argument. The method name uniquely identifies the type
                    // (poisoned otherwise), so the rewrite is unambiguous.
                    if let Some(ty) = self.freefn_lowered_type(&method.name).map(str::to_string) {
                        let is_public = self.public_methods.contains(&method.name);
                        let fn_name = self.freefn_lowered_name(&ty, &method.name, is_public);
                        let _ = write!(self.buf, "{fn_name}(");
                        self.emit_expr(recv)?;
                        for arg in rest {
                            self.buf.push_str(", ");
                            self.emit_expr(&arg.value)?;
                        }
                        self.buf.push(')');
                        return Ok(());
                    }
                    self.emit_expr(recv)?;
                    let go_method = self
                        .go_method_name(&method.name, self.public_methods.contains(&method.name));
                    let _ = write!(self.buf, ".{go_method}(");
                    for (i, arg) in rest.iter().enumerate() {
                        if i > 0 {
                            self.buf.push_str(", ");
                        }
                        self.emit_expr(&arg.value)?;
                    }
                    self.buf.push(')');
                    return Ok(());
                }
                // Pass handler args to effectful function calls.
                let effects_args = if let NodeKind::Identifier { name } = &callee.kind {
                    self.build_effects_call_args_go(&name.name)
                } else {
                    None
                };
                // Route async-fn calls through their `Async`-suffix wrapper
                // so callers receive a `<-chan T` instead of `T` — the sync
                // body is only invoked from inside its own wrapper.
                if let NodeKind::Identifier { name } = &callee.kind {
                    if self.async_fns.contains(&name.name) {
                        let go_name = self.go_fn_name(&name.name);
                        self.buf.push_str(&format!("{go_name}Async"));
                    } else {
                        self.emit_expr(callee)?;
                    }
                } else {
                    self.emit_expr(callee)?;
                }
                // When the callee is a known generic fn, recover its signature so
                // we can (a) synthesise explicit Go type-arguments the source
                // omits but Go cannot infer (a `Optional[T]`/`Result[T, E]` param
                // erases `T`/`E` from the monomorphic runtime struct), and (b)
                // specialise each untyped lambda argument to the concrete Go param
                // type (`func(int64) bool` for `filter(it, (x) => x > 2)`).
                let fn_sig = if let NodeKind::Identifier { name } = &callee.kind {
                    self.fn_signatures.get(&name.name).cloned()
                } else {
                    None
                };
                // Synthesise the turbofish only when the source carried none.
                // An explicit source `f[Ty](..)` (`type_args`) always wins.
                let callee_sealed_bound = matches!(&callee.kind, NodeKind::Identifier { name }
                    if self.fn_sealed_bound.contains(&name.name));
                let synthesized_type_args = if type_args.is_empty() {
                    fn_sig.as_ref().and_then(|(gp, ptys, ret)| {
                        // A container-touching signature defeats Go's own inference
                        // (the `Optional`/`Result` runtime erases `T`); a
                        // sealed-core-bound fn defeats untyped-constant inference
                        // (GAP-C). Either forces explicit type args.
                        self.synthesize_go_type_args(
                            gp,
                            ptys,
                            ret.as_ref(),
                            args,
                            callee_sealed_bound,
                        )
                    })
                } else {
                    None
                };
                let type_arg_str = if let Some(syn) = &synthesized_type_args {
                    format!("[{}]", syn.join(", "))
                } else {
                    self.format_generic_args(type_args)
                };
                self.buf.push_str(&type_arg_str);
                self.buf.push('(');
                // Bind the callee's type params from the (synthesised args, when
                // available, else the non-lambda arguments) so each untyped lambda
                // argument can be specialised to the concrete Go param type rather
                // than the `interface{}` default that breaks the body and
                // mismatches the typed callee param. The synthesised binding is
                // strictly more complete than the arg-only one — it also pins a
                // `T` that only appears behind `Optional[T]`/`Result[T, E]`, which
                // is exactly the `filter`/`and_then` lambda case.
                let lambda_bindings = fn_sig
                    .as_ref()
                    .map(|(gp, ptys, _)| {
                        let binds = match (&synthesized_type_args, gp.len()) {
                            (Some(syn), n) if syn.len() == n => {
                                gp.iter().cloned().zip(syn.iter().cloned()).collect()
                            }
                            _ => self.bind_fn_type_params(gp, ptys, args),
                        };
                        (gp.clone(), ptys.clone(), binds)
                    })
                    .or_else(|| {
                        // Non-generic callee: no type params to bind, but its
                        // concrete `Fn(...)` param types still pin an untyped lambda
                        // argument (`count_where(todos, (t) => t.done)` →
                        // `func(t Todo) bool`). Empty gp/bindings means
                        // `specialise_lambda_param_types` renders the param types
                        // verbatim.
                        if let NodeKind::Identifier { name } = &callee.kind {
                            self.fn_param_types
                                .get(&name.name)
                                .map(|ptys| (Vec::new(), ptys.clone(), HashMap::new()))
                        } else {
                            None
                        }
                    });
                for (i, arg) in args.iter().enumerate() {
                    if i > 0 {
                        self.buf.push_str(", ");
                    }
                    let prev_lambda = self.expected_lambda_param_types.take();
                    let prev_forced_ret = self.forced_lambda_ret.take();
                    let prev_coll = self.expected_collection_elem.take();
                    if matches!(arg.value.kind, NodeKind::Lambda { .. }) {
                        if let Some((gp, ptys, binds)) = &lambda_bindings {
                            if let Some(pty) = ptys.get(i).and_then(|p| p.as_ref()) {
                                self.expected_lambda_param_types =
                                    self.specialise_lambda_param_types(pty, gp, binds);
                                // A `Fn(...) -> Void` callee parameter pins the
                                // lambda's return to the Void marker so the closure
                                // emits `func(...) { <stmts> }` (no result, no
                                // `return`). Without this, a `() => println(...)`
                                // call argument would emit `func() interface{} {
                                // return fmt.Println(...) }` — both a type mismatch
                                // against the `func()` parameter and a Go arity
                                // error (`fmt.Println` returns `(int, error)`). The
                                // parameter may name a `type Handler = Fn() -> Void`
                                // alias (react-components' `EventHandler`), so peel
                                // one alias layer first.
                                let resolved = self.resolve_type_alias(pty).unwrap_or(pty);
                                if let NodeKind::TypeFunction { ret, .. } = &resolved.kind {
                                    if Self::is_void_type(ret) {
                                        self.forced_lambda_ret = Some("struct{}".to_string());
                                    }
                                }
                            }
                        }
                    } else if matches!(
                        arg.value.kind,
                        NodeKind::ListLiteral { .. }
                            | NodeKind::MapLiteral { .. }
                            | NodeKind::SetLiteral { .. }
                    ) {
                        // A collection literal argument (most importantly an empty
                        // `[]` / `{}` whose own elements can't infer a type) adopts
                        // the callee's declared parameter element type, so it emits
                        // `[]int64{}` against a `List[Int]` param rather than the
                        // erased `[]interface{}{}` Go rejects. The param type comes
                        // from the non-generic `fn_param_types` record.
                        if let NodeKind::Identifier { name } = &callee.kind {
                            if let Some(pty) = self
                                .fn_param_types
                                .get(&name.name)
                                .and_then(|ptys| ptys.get(i))
                                .and_then(|p| p.as_ref())
                            {
                                self.expected_collection_elem = self.collection_elem_go_types(pty);
                            }
                        }
                    }
                    self.emit_expr(&arg.value)?;
                    self.expected_lambda_param_types = prev_lambda;
                    self.forced_lambda_ret = prev_forced_ret;
                    self.expected_collection_elem = prev_coll;
                }
                if let Some(ea) = effects_args {
                    if !args.is_empty() {
                        self.buf.push_str(", ");
                    }
                    self.buf.push_str(&ea);
                }
                self.buf.push(')');
                Ok(())
            }
            NodeKind::MethodCall {
                receiver,
                method,
                args,
                ..
            } => {
                if self.try_emit_time_method(receiver, &method.name, args)? {
                    return Ok(());
                }
                // DQ28 free-function lowering (the non-desugared `MethodCall`
                // shape): `box.map(f)` → `Box_Map(box, f)`, receiver-first.
                if let Some(ty) = self.freefn_lowered_type(&method.name).map(str::to_string) {
                    let is_public = self.public_methods.contains(&method.name);
                    let fn_name = self.freefn_lowered_name(&ty, &method.name, is_public);
                    let _ = write!(self.buf, "{fn_name}(");
                    self.emit_expr(receiver)?;
                    for arg in args {
                        self.buf.push_str(", ");
                        self.emit_expr(&arg.value)?;
                    }
                    self.buf.push(')');
                    return Ok(());
                }
                self.emit_expr(receiver)?;
                // `MethodCall` dispatches a method through Go method casing. A
                // method whose name collides with a struct field is suffixed
                // identically here and at the declaration (`go_method_name`).
                let go_method =
                    self.go_method_name(&method.name, self.public_methods.contains(&method.name));
                let _ = write!(self.buf, ".{go_method}");
                self.buf.push('(');
                for (i, arg) in args.iter().enumerate() {
                    if i > 0 {
                        self.buf.push_str(", ");
                    }
                    self.emit_expr(&arg.value)?;
                }
                self.buf.push(')');
                Ok(())
            }
            NodeKind::FieldAccess { object, field } => {
                self.emit_expr(object)?;
                let _ = write!(self.buf, ".{}", to_pascal_case(&field.name));
                Ok(())
            }
            NodeKind::Index { object, index } => {
                self.emit_expr(object)?;
                self.buf.push('[');
                self.emit_expr(index)?;
                self.buf.push(']');
                Ok(())
            }
            NodeKind::Lambda { params, body } => {
                // An untyped lambda argument adopts the callee's specialised
                // parameter types when known (`expected_lambda_param_types`,
                // e.g. `func(int64) bool` for `filter(it, (x) => x > 2)`), so
                // the body's arithmetic type-checks and the closure satisfies
                // the typed callee parameter. Consume the hint so it never
                // leaks to a nested lambda in the body.
                let expected_params = self.expected_lambda_param_types.take();
                // A `f >> g` compose desugars (in shared AIR) to `(__compose_x) =>
                // g(f(__compose_x))` with an *untyped* `__compose_x`. Recover its
                // Go type from `f`'s first declared parameter type so the emitted
                // closure is `func(x []float64) ...` rather than `func(x
                // interface{}) ...` — the latter is not assignable to a typed
                // `Fn(List[Float]) -> List[Float]` callee parameter.
                let compose_param = if expected_params.is_none() {
                    self.compose_lambda_param_go_type(params, body)
                } else {
                    None
                };
                let param_strs = match (&expected_params, &compose_param) {
                    (Some(tys), _) if tys.len() == params.len() => {
                        self.collect_param_strs_with_types(params, tys)
                    }
                    (None, Some(ty)) => {
                        self.collect_param_strs_with_types(params, std::slice::from_ref(ty))
                    }
                    _ => self.collect_param_strs(params),
                };
                // Record the lambda's typed params so the body's return type can
                // be inferred structurally. Without a concrete return type Go
                // infers `interface{}`, which fails to satisfy a typed
                // `func(int64) int64` parameter at the use site.
                let scope_expected = expected_params
                    .as_deref()
                    .or(compose_param.as_ref().map(std::slice::from_ref));
                let saved_go_types =
                    self.enter_param_go_types_with_expected(params, scope_expected);
                // A predicate combinator pins the return type to `bool` (consumed
                // here so it never leaks to a nested lambda); otherwise infer it.
                // Use the block-tail inference so a lambda whose body is a block /
                // `if` / `match` (`(t) => { if (..) { t.complete() } else { t } }`)
                // gets a concrete return type (`Todo`) rather than `interface{}` —
                // this must agree with the result-slice element type the
                // list-combinator emitter derives from the same inference, or
                // `append(__out, __f(__x))` mismatches `[]Todo` vs `interface{}`.
                let forced_ret = self.forced_lambda_ret.take();
                // A `Fn(...) -> Void` callee pins `forced_ret` to the Void value
                // type `struct{}` (and the *type* now renders `func(...)` with no
                // result, see `type_to_go`'s `TypeFunction` arm). Such a lambda is
                // void: its closure must be `func(...) { <stmts> }` — no result
                // type, no `return`. Emitting `func() struct{} { return <body> }`
                // is doubly wrong: it does not match the `func()` parameter type,
                // and a void-call body (`println(...)` → Go's `fmt.Println`, which
                // returns `(int, error)`) makes `return fmt.Println(...)` a Go
                // arity error. When `forced_ret` is unset, a lambda whose body tail
                // is a void call (a bare `() => println(...)`) is likewise void.
                let body_tail = match &body.kind {
                    NodeKind::Block { tail: Some(t), .. } => t.as_ref(),
                    NodeKind::Block { tail: None, .. } => body,
                    _ => body,
                };
                let is_void_lambda = forced_ret.as_deref() == Some("struct{}")
                    || (forced_ret.is_none()
                        && expected_params.is_none()
                        && compose_param.is_none()
                        && self.is_void_call(body_tail));
                if is_void_lambda {
                    // Statement-style void closure: `func(params) { <stmts> }`. The
                    // body's effective tail void call is emitted as a statement
                    // (never `return`d). Mirrors the function-body void path.
                    let _ = write!(self.buf, "func({}) {{ ", param_strs.join(", "));
                    let prev_ret = self.current_fn_ret_type.take();
                    let prev_expected = self.current_expected_type.take();
                    if let NodeKind::Block { stmts, .. } = &body.kind {
                        for s in stmts {
                            self.emit_node(s)?;
                            self.buf.push_str("; ");
                        }
                    }
                    self.emit_expr(body_tail)?;
                    self.current_fn_ret_type = prev_ret;
                    self.current_expected_type = prev_expected;
                    self.buf.push_str(" }");
                    self.var_go_type = saved_go_types;
                    return Ok(());
                }
                let ret_ty = forced_ret.unwrap_or_else(|| {
                    self.infer_block_tail_type(body)
                        .unwrap_or_else(|| "interface{}".to_string())
                });
                let _ = write!(
                    self.buf,
                    "func({}) {ret_ty} {{ return ",
                    param_strs.join(", ")
                );
                // The lambda body is a fresh return scope: a `match`/`if`/`loop`
                // in its tail lowers to an IIFE whose type is *this lambda's*
                // return type, not the enclosing function's. Without resetting
                // these, an inner match IIFE in a `filter`/`map` lambda inherited
                // the outer fn's return type (chat-protocol: a `bool` match body
                // typed `func() []Message`, the surrounding `filter`'s return).
                let prev_ret = self.current_fn_ret_type.take();
                let prev_expected = self.current_expected_type.take();
                self.current_fn_ret_type = (ret_ty != "interface{}").then(|| ret_ty.clone());
                self.emit_expr(body)?;
                self.current_fn_ret_type = prev_ret;
                self.current_expected_type = prev_expected;
                self.buf.push_str(" }");
                self.var_go_type = saved_go_types;
                Ok(())
            }
            NodeKind::Pipe { left, right } => self.emit_pipe(left, right),
            NodeKind::Compose { left, right } => {
                // `f >> g` → `func(x interface{}) interface{} { return g(f(x)) }`
                let _ = write!(self.buf, "func(x interface{{}}) interface{{}} {{ return ");
                self.emit_expr(right)?;
                self.buf.push('(');
                self.emit_expr(left)?;
                self.buf.push_str("(x)) }");
                Ok(())
            }
            NodeKind::Await { expr } => {
                // Go uses goroutines/channels; await maps to channel receive.
                self.buf.push_str("<-");
                self.emit_expr(expr)?;
                Ok(())
            }
            NodeKind::Propagate { expr } => {
                // Go error propagation would require special handling;
                // just emit the expression for now.
                self.emit_expr(expr)?;
                Ok(())
            }
            NodeKind::Range { lo, hi, inclusive } => {
                // Go has no native range *value*; lower to the injected
                // `__bockRange(lo, hi, inclusive)` helper (a `[]int64`), so
                // `for _, i := range <range>` iterates the materialised slice.
                // The runtime is emitted once at the Module arm
                // (`go_module_uses_range`).
                self.buf.push_str("__bockRange(");
                self.emit_expr(lo)?;
                self.buf.push_str(", ");
                self.emit_expr(hi)?;
                let _ = write!(self.buf, ", {inclusive})");
                Ok(())
            }
            NodeKind::RecordConstruct {
                path,
                fields,
                spread,
            } => {
                // A struct-variant construction (`Circle { radius: .. }`) → the
                // `{enum}{variant}` struct literal `ShapeCircle{Radius: ..}`
                // (field name `to_pascal_case`d). Plain records keep their path.
                let type_name = self.record_construct_go_type_name(path);
                // Go requires an explicit type-argument list on a generic
                // struct literal (`Box[int64]{...}`); it does NOT infer the args
                // from the field values. Prefer the declared/expected binding
                // type's concrete args (`let c: ListIter[Int] = ListIter { ... }`
                // → `[int64]`), which works even when a param appears only
                // *nested* in a field type (`record ListIter[T] { xs: List[T] }`,
                // where no field is typed exactly `T` so field-inference yields
                // `any`). Fall back to inferring each param's type from the field
                // value that names it directly.
                let type_args = self
                    .expected_construct_type_args(&type_name)
                    .unwrap_or_else(|| self.infer_construct_type_args(&type_name, fields));
                if let Some(sp) = spread {
                    // Go has no struct-spread syntax (`Point{..p}`), so a record
                    // spread lowers to an IIFE that copies the base value, then
                    // assigns each override field, then returns the copy:
                    //   func() T { __s := <base>; __s.Field = val; …; return __s }()
                    // The base is the spread expression; the overrides are the
                    // explicitly-given fields. (A struct copy in Go is a value
                    // copy, so this does not mutate the base.)
                    let _ = write!(self.buf, "func() {type_name}{type_args} {{ __s := ");
                    self.emit_expr(sp)?;
                    self.buf.push_str("; ");
                    for f in fields {
                        let _ = write!(self.buf, "__s.{} = ", to_pascal_case(&f.name.name));
                        if let Some(val) = &f.value {
                            self.emit_expr(val)?;
                        } else {
                            self.buf.push_str(&to_camel_case(&f.name.name));
                        }
                        self.buf.push_str("; ");
                    }
                    self.buf.push_str("return __s }()");
                } else {
                    // A field whose declared type is `List[..]` (registered in
                    // `record_field_list_elem`) supplies the expected element type
                    // for a list-literal value, so an empty `[]` / under-inferred
                    // literal field emits `[]<elem>{}` matching the struct's `[]T`
                    // field rather than the erased `[]interface{}{}` Go rejects.
                    // The element type is the field's declared param (`T`),
                    // substituted with the construct's resolved concrete args
                    // (`SortedSet[Key]` → `T` ↦ `Key`).
                    let record_name = path.segments.last().map(|s| s.name.clone());
                    let param_subst = record_name
                        .as_deref()
                        .map(|rn| self.record_param_substitution(rn, &type_args))
                        .unwrap_or_default();
                    self.buf.push_str(&format!("{type_name}{type_args}{{"));
                    for (i, f) in fields.iter().enumerate() {
                        if i > 0 {
                            self.buf.push_str(", ");
                        }
                        let _ = write!(self.buf, "{}: ", to_pascal_case(&f.name.name));
                        if let Some(val) = &f.value {
                            let field_elem = record_name.as_deref().and_then(|rn| {
                                self.record_field_list_elem
                                    .get(rn)
                                    .and_then(|m| m.get(&f.name.name))
                                    .map(|e| Self::apply_type_subst(e, &param_subst))
                            });
                            let prev_expected = self.expected_collection_elem.take();
                            if let (Some(elem), true) = (
                                field_elem,
                                matches!(
                                    val.kind,
                                    NodeKind::ListLiteral { .. } | NodeKind::SetLiteral { .. }
                                ),
                            ) {
                                self.expected_collection_elem = Some((elem, None));
                            }
                            self.emit_expr(val)?;
                            self.expected_collection_elem = prev_expected;
                        } else {
                            self.buf.push_str(&to_camel_case(&f.name.name));
                        }
                    }
                    self.buf.push('}');
                }
                Ok(())
            }
            NodeKind::ListLiteral { elems } => {
                // A declared binding type (`let x: List[T] = ...`) takes priority
                // so an empty `[]` matches the declared `[]T`; otherwise infer a
                // homogeneous element type so `[1, 2, 3]` emits `[]int64{...}`
                // (not `[]interface{}{...}`), letting element arithmetic / typed
                // iteration / typed returns compile. Falls back to `interface{}`
                // when neither is available (empty literal with no declared
                // type, mixed types, unresolved operands).
                let expected = self.expected_collection_elem.take();
                let elem_ty = expected
                    .map(|(e, _)| e)
                    .or_else(|| self.infer_homogeneous_elem_type(elems))
                    .unwrap_or_else(|| "interface{}".to_string());
                let _ = write!(self.buf, "[]{elem_ty}{{");
                for (i, e) in elems.iter().enumerate() {
                    if i > 0 {
                        self.buf.push_str(", ");
                    }
                    self.emit_expr(e)?;
                }
                self.buf.push('}');
                Ok(())
            }
            NodeKind::MapLiteral { entries } => {
                // A declared `Map[K, V]` binding type takes priority (so an
                // empty `{}` matches `map[K]V`); otherwise infer key and value
                // element types separately so `{"a": 1}` emits
                // `map[string]int64{...}`. Either falling back to `interface{}`.
                let expected = self.expected_collection_elem.take();
                let keys: Vec<&AIRNode> = entries.iter().map(|e| &e.key).collect();
                let vals: Vec<&AIRNode> = entries.iter().map(|e| &e.value).collect();
                let (exp_key, exp_val) = match expected {
                    Some((k, v)) => (Some(k), v),
                    None => (None, None),
                };
                let key_ty = exp_key
                    .or_else(|| self.infer_homogeneous_elem_type_refs(&keys))
                    .unwrap_or_else(|| "interface{}".to_string());
                let val_ty = exp_val
                    .or_else(|| self.infer_homogeneous_elem_type_refs(&vals))
                    .unwrap_or_else(|| "interface{}".to_string());
                let _ = write!(self.buf, "map[{key_ty}]{val_ty}{{");
                for (i, entry) in entries.iter().enumerate() {
                    if i > 0 {
                        self.buf.push_str(", ");
                    }
                    self.emit_expr(&entry.key)?;
                    self.buf.push_str(": ");
                    self.emit_expr(&entry.value)?;
                }
                self.buf.push('}');
                Ok(())
            }
            NodeKind::SetLiteral { elems } => {
                // Go doesn't have sets; use map[T]struct{}. A declared `Set[T]`
                // binding type takes priority (empty set matches); otherwise
                // infer a homogeneous element type so `#{1, 2}` emits
                // `map[int64]struct{}{...}`.
                let expected = self.expected_collection_elem.take();
                let elem_ty = expected
                    .map(|(e, _)| e)
                    .or_else(|| self.infer_homogeneous_elem_type(elems))
                    .unwrap_or_else(|| "interface{}".to_string());
                let _ = write!(self.buf, "map[{elem_ty}]struct{{}}{{");
                for (i, e) in elems.iter().enumerate() {
                    if i > 0 {
                        self.buf.push_str(", ");
                    }
                    self.emit_expr(e)?;
                    self.buf.push_str(": {}");
                }
                self.buf.push('}');
                Ok(())
            }
            NodeKind::TupleLiteral { elems } => {
                // Go has no tuples; a `(a, b)` value is a struct with numbered
                // fields — the SAME representation `type_to_go` gives a tuple
                // *type* (`struct{ Field0 T0; Field1 T1 }`) and the match
                // pattern reads (`.Field0`). Emitting a `[...]interface{}` array
                // here (the prior lowering) produced a value whose type did not
                // match the `struct{…}` parameter type, so a tuple argument
                // failed `go build`. Build the matching struct literal instead,
                // inferring each field's element type (falling back to
                // `interface{}` when it can't be inferred).
                // A declared tuple return / binding type (`-> (Int, Int)` →
                // `struct{ Field0 int64; Field1 int64 }`) pins each field's Go
                // type, so a `return (count, total)` whose elements only infer to
                // `interface{}` (an untyped `let` binding) still emits the
                // concrete struct the declared return type demands. Per field:
                // the expected field type wins, else structural inference, else
                // `interface{}`.
                let expected_fields = self
                    .current_expected_type
                    .as_deref()
                    .map(Self::parse_tuple_struct_field_types)
                    .unwrap_or_default();
                let field_types: Vec<String> = elems
                    .iter()
                    .enumerate()
                    .map(|(i, e)| {
                        expected_fields
                            .get(i)
                            .cloned()
                            .or_else(|| self.infer_go_expr_type(e))
                            .unwrap_or_else(|| "interface{}".to_string())
                    })
                    .collect();
                let fields: Vec<String> = field_types
                    .iter()
                    .enumerate()
                    .map(|(i, t)| format!("Field{i} {t}"))
                    .collect();
                let _ = write!(self.buf, "struct{{ {} }}{{", fields.join("; "));
                for (i, e) in elems.iter().enumerate() {
                    if i > 0 {
                        self.buf.push_str(", ");
                    }
                    self.emit_expr(e)?;
                }
                self.buf.push('}');
                Ok(())
            }
            NodeKind::Interpolation { parts } => {
                self.needs_fmt_import = true;
                self.buf.push_str("fmt.Sprintf(\"");
                let mut args = Vec::new();
                for part in parts {
                    match part {
                        AirInterpolationPart::Literal(s) => {
                            // This literal lands inside a `fmt.Sprintf` FORMAT
                            // string, so a literal `%` must be doubled to `%%` —
                            // unescaped it pairs with the following bytes as a
                            // verb (`"${n}% pass"` → format `"%v% pass"` →
                            // `95%!p(MISSING)ass`), a SILENT cross-target output
                            // divergence: the build stays green and only Go
                            // corrupts (Q-go-percent-interpolation).
                            // `escape_go_string` never emits `%`, so escaping
                            // before doubling cannot double an escape.
                            self.buf.push_str(&escape_go_string(s).replace('%', "%%"));
                        }
                        AirInterpolationPart::Expr(expr) => {
                            // Q-displayable-interpolation-dispatch: render through
                            // `__bockStr` (a `%s` verb over a `string`) so a user
                            // value whose type has a `Displayable` impl (its
                            // `ToString` method) shows via that method, not the
                            // struct default (`%v` → `{3 7}`). Every other value
                            // falls back to `fmt.Sprintf("%v", x)` inside the
                            // helper, matching the prior `%v` form.
                            self.buf.push_str("%s");
                            args.push(expr.clone());
                        }
                    }
                }
                self.buf.push('"');
                for arg in &args {
                    self.buf.push_str(", __bockStr(");
                    self.emit_expr(arg)?;
                    self.buf.push(')');
                }
                self.buf.push(')');
                Ok(())
            }
            NodeKind::Placeholder => {
                self.buf.push('_');
                Ok(())
            }
            NodeKind::Unreachable => {
                self.buf.push_str("panic(\"unreachable\")");
                Ok(())
            }
            NodeKind::ResultConstruct { variant, value } => {
                // Construct the tagged Result-runtime struct (`__bockOk`/
                // `__bockErr`) — the same shape the surface `Ok(..)`/`Err(..)`
                // construction emits and the `Result` match reads on `.tag`. The
                // old Go-idiomatic `v, nil` / `nil, err` multi-return shape
                // disagreed with the tag-dispatched match, so reconcile on the
                // tagged struct.
                let ctor = match variant {
                    ResultVariant::Ok => "__bockOk",
                    ResultVariant::Err => "__bockErr",
                };
                let _ = write!(self.buf, "{ctor}(");
                if let Some(v) = value {
                    // Box a numeric-literal payload at its concrete Go type (see
                    // `box_payload_str`) so a later `.(int64)` / generic `.(T)`
                    // payload assertion does not panic on the `int` default.
                    if let Some(go_ty) = Self::numeric_literal_go_type(v) {
                        let _ = write!(self.buf, "{go_ty}(");
                        self.emit_expr(v)?;
                        self.buf.push(')');
                    } else {
                        self.emit_expr(v)?;
                    }
                } else {
                    self.buf.push_str("nil");
                }
                self.buf.push(')');
                Ok(())
            }
            NodeKind::Assign { op, target, value } => {
                self.emit_expr(target)?;
                let op_str = match op {
                    AssignOp::Assign => " = ",
                    AssignOp::AddAssign => " += ",
                    AssignOp::SubAssign => " -= ",
                    AssignOp::MulAssign => " *= ",
                    AssignOp::DivAssign => " /= ",
                    AssignOp::RemAssign => " %= ",
                };
                self.buf.push_str(op_str);
                self.emit_expr(value)?;
                Ok(())
            }
            NodeKind::If {
                condition,
                then_block,
                else_block,
                ..
            } => {
                // If in expression position: Go doesn't have ternary; emit as
                // IIFE. Type it with the binding's expected type when known (a
                // `let x: T = if …`), else the enclosing function's return type
                // (`func() Ordering { … }`) so a named/concrete result is
                // assignable; the `else` falls back to a typed zero only for the
                // untyped form (a concrete return type always has both branches
                // in a Bock `if`-expression).
                let iife_ty = self
                    .expected_iife_type()
                    .unwrap_or_else(|| "interface{}".to_string());
                // Each branch of an `if`-expression produces the SAME type as the
                // whole `if`, so a nested `if`/`match` in a branch tail must
                // inherit this IIFE's concrete type — otherwise it falls back to
                // the enclosing fn's return type (e.g. a nested
                // `func() __bockResult` inside an `else` of a `MessageType` `if`).
                // Propagate the concrete type into the branch bodies (a nested
                // `let` save/restores it, so it never wrongly overrides a binding
                // type); the `interface{}` fallback is left absent so the branches
                // keep inferring their own types.
                let prev_expected = self.current_expected_type.take();
                let branch_expected = (iife_ty != "interface{}").then(|| iife_ty.clone());
                // A non-`interface{}` IIFE type is the typed-zero source for a
                // void-call branch tail (`emit_arm_body_return`).
                let iife_ret = (iife_ty != "interface{}").then(|| iife_ty.clone());
                let _ = write!(self.buf, "func() {iife_ty} {{ if ");
                self.emit_expr(condition)?;
                self.buf.push_str(" { ");
                self.current_expected_type = branch_expected.clone();
                self.emit_arm_body_return(then_block, iife_ret.as_deref())?;
                self.buf.push_str(" } else { ");
                if let Some(eb) = else_block {
                    self.current_expected_type = branch_expected;
                    self.emit_arm_body_return(eb, iife_ret.as_deref())?;
                } else {
                    self.buf.push_str("return nil");
                }
                self.buf.push_str(" } }()");
                self.current_expected_type = prev_expected;
                Ok(())
            }
            NodeKind::Block { stmts, tail } => {
                if stmts.is_empty() {
                    if let Some(t) = tail {
                        return self.emit_expr(t);
                    }
                }
                // A block with statements in expression position (e.g. an
                // `if`/`match` arm body `{ let id = ...; GetUser { id: id } }`)
                // lowers to an IIFE that runs the statements then returns the
                // tail. The earlier `func() interface{} { return <tail> }` form
                // both DROPPED the statements (so a `let` binding used by the tail
                // was `undefined`) and erased the result to `interface{}` (so a
                // variant struct returned as the tail was not assignable to the
                // enclosing sealed-interface IIFE type, e.g. `Route`). Type the
                // IIFE with the expected type — the binding's `current_expected_type`
                // or the enclosing fn's return type (`expected_iife_type`) — and
                // emit the full block body via `emit_block_body_return`, which runs
                // the statements and returns the (typed) tail. A typed IIFE closes
                // with `panic("unreachable")` (it always returns through the tail);
                // the untyped form keeps `return nil`.
                let iife_ret = self.expected_iife_type();
                let iife_ty = iife_ret.as_deref().unwrap_or("interface{}");
                // Consume the expected type for THIS IIFE's return only; the block
                // body re-types its own tail (and may re-set it via a nested
                // `let`), so it must not leak into the statements.
                let prev_expected = self.current_expected_type.take();
                // The body's tail terminates the IIFE when it is a value
                // expression (emitted as `return <tail>`) or a terminating
                // statement (`return`/`break`/`continue`). Only when the block can
                // fall through (no tail, or an assignment tail) do we need the
                // trailing fallback — emitting it unconditionally would leave dead
                // code after a `return`. (Go does not reject unreachable code, but
                // the conditional keeps the output clean.)
                let body_falls_through = match tail {
                    Some(t) => crate::generator::node_is_statement(t) && !Self::tail_terminates(t),
                    None => true,
                };
                let _ = writeln!(self.buf, "func() {iife_ty} {{");
                self.indent += 1;
                self.emit_block_body_return(node)?;
                if body_falls_through {
                    if iife_ty == "interface{}" {
                        self.writeln("return nil");
                    } else {
                        self.writeln("panic(\"unreachable\")");
                    }
                }
                self.indent -= 1;
                self.write_indent();
                self.buf.push_str("}()");
                self.current_expected_type = prev_expected;
                Ok(())
            }
            NodeKind::Match { scrutinee, arms } => {
                // Guards, or-/tuple/list/range patterns, and nested
                // constructor/record patterns cannot ride the value/type
                // `switch` IIFE below (its `case <cond>` form has no slot for a
                // relational/length test or a fall-through guard — they collapse
                // to a broken `case interface{}:`). Route them to the shared
                // if/else-if chain, wrapped in a typed IIFE whose arm bodies
                // `return` the match's value.
                //
                // This `match_needs_ifchain` check comes BEFORE the Optional/Result
                // tag-switch fast-paths (mirroring the statement-position
                // `emit_match`, which checks it first): a *nested* Optional/Result
                // pattern (`Some(Ok(n))`) is structured, so the tag-switch
                // `emit_optional_match_expr` cannot bind its nested payload
                // (`undefined: n`). The if-chain's `collect_binds_go` recurses
                // through the nested `Some`/`Ok` tags and binds `n` off the typed
                // `.v` payloads. A *flat* `Some(x)`/`Ok(v)` match is not structured,
                // so it still takes the tag-switch fast-path below.
                //
                // Two further go.rs-local diversions (NOT in the shared
                // `match_needs_ifchain`, which keeps these on the switch fast-path —
                // correct only for *statement* position, where `emit_match` binds
                // `__v`): a bare-bind arm (`x => …`, `mut x => …`) has no value to
                // switch on, so the value-switch IIFE emits `case interface{}:` (a
                // *type* in expression position) and drops the name; and a *plain*
                // record arm (`Point { x, .. }`) is a concrete struct, not a
                // sealed-interface value, so `case Point:` is likewise invalid. The
                // if-chain IIFE tests/binds every pattern kind (a bare bind → an
                // unconditional `else` with `x := root`; a plain record → field
                // reads off `root.X`).
                if crate::generator::match_needs_ifchain(arms)
                    || Self::go_value_match_has_bind_arm(arms)
                    || self.go_value_match_has_plain_record_arm(arms)
                {
                    let iife_ret = self.expected_iife_type();
                    let iife_ty = iife_ret.as_deref().unwrap_or("interface{}");
                    // Each arm yields the SAME type as the whole match, so a
                    // nested branchy tail inherits the concrete IIFE type; a
                    // nested `let` save/restores it. Mirrors the switch path.
                    let prev_expected = self.current_expected_type.take();
                    self.current_expected_type =
                        (iife_ty != "interface{}").then(|| iife_ty.to_string());
                    let _ = writeln!(self.buf, "func() {iife_ty} {{");
                    self.indent += 1;
                    let res =
                        self.emit_match_ifchain_inner(scrutinee, arms, /*emit_return=*/ true);
                    self.indent -= 1;
                    self.current_expected_type = prev_expected;
                    res?;
                    // A Bock match is exhaustive, but Go cannot prove the if-chain
                    // is total, so a trailing `panic` keeps the IIFE well-typed.
                    self.write_indent();
                    self.buf.push_str("panic(\"unreachable\")\n");
                    self.write_indent();
                    self.buf.push_str("}()");
                    return Ok(());
                }
                // Flat `Optional` / `Result` matches dispatch on the runtime tag
                // (`Some(x)`/`None`, `Ok(v)`/`Err(e)`). A *nested* such match
                // (`Some(Ok(n))`) was diverted to the if-chain above, so only the
                // single-level tag-switch reaches here.
                if go_match_is_optional(arms) {
                    return self.emit_optional_match_expr(scrutinee, arms);
                }
                if go_match_is_result(arms) {
                    return self.emit_result_match_expr(scrutinee, arms);
                }
                // A user-enum match (including a reachable `core.compare.Ordering`
                // enum) dispatches on the dynamic concrete-variant *type*
                // (`OrderingGreater`), so the IIFE must be a *type-switch* — the
                // variant names are Go struct types, not values, so a value-switch
                // (`case OrderingGreater:`) is a compile error. (The prelude
                // `__bockOrdering` value-enum, used when the real enum is NOT
                // reachable, stays a value-switch via the path below.)
                let is_user_enum = self.go_match_is_user_enum(arms);
                // Type the IIFE so its result is assignable where a
                // concrete/named type is required — `interface{}` does not
                // satisfy a named interface like the user `Ordering`. Prefer the
                // binding's *expected* type (`let x: T = match …`) when known and
                // concrete: a value-position match binds into `T`, which need not
                // equal the enclosing function's return type. Otherwise fall back
                // to the function's return type (the return-position case:
                // `return match …`), preserving the working Optional/Result/enum
                // behavior. A typed IIFE closes with `panic("unreachable")` (a
                // Bock match is exhaustive) rather than `return nil`, which has no
                // value for a concrete return type.
                let iife_ret = self.expected_iife_type();
                let iife_ty = iife_ret.as_deref().unwrap_or("interface{}");
                // Consume the expected type for THIS IIFE's return only; the
                // scrutinee is a different (matched) type and must not inherit it.
                // Each arm produces the SAME type as the whole match, so a nested
                // `if`/`match` in an arm tail DOES inherit the concrete IIFE type
                // (else it falls back to the enclosing fn's return type). A nested
                // `let` save/restores it, so it never wrongly overrides a binding.
                let prev_expected = self.current_expected_type.take();
                let arm_expected = (iife_ty != "interface{}").then(|| iife_ty.to_string());
                // A user-enum match whose arms extract payload fields
                // (`Heading { level, text } => …`) must narrow on `__v` so each
                // case can read `__v.Level` / `__v.Text`. A payload-less enum
                // match (the `core.compare.Ordering` unit variants) keeps the
                // non-binding `switch x.(type)` (binding `__v` would be Go's
                // "declared and not used").
                let user_enum_binds = is_user_enum && Self::go_user_enum_match_binds_payload(arms);
                let _ = write!(self.buf, "func() {iife_ty} {{ switch ");
                if user_enum_binds {
                    self.buf.push_str("__v := ");
                    self.emit_expr(scrutinee)?;
                    self.buf.push_str(".(type) { ");
                } else if is_user_enum {
                    // Non-binding type-switch (`switch x.(type)`): the
                    // `core.compare.Ordering` variants are unit (no payload), so
                    // no `__v` binding is needed, which also avoids Go's
                    // "declared and not used" on a payload-less match.
                    self.emit_expr(scrutinee)?;
                    self.buf.push_str(".(type) { ");
                } else {
                    self.emit_expr(scrutinee)?;
                    self.buf.push_str(" { ");
                }
                // Match in expression position: emit as IIFE with switch. Each
                // arm body is terminated with `;` so consecutive single-line
                // `case`/`default` clauses are separated — Go requires a
                // statement terminator between a `case` body's trailing `return`
                // and the next `case`/`default` keyword (a bare space is a
                // "unexpected keyword case" syntax error).
                for arm in arms {
                    if let NodeKind::MatchArm { pattern, body, .. } = &arm.kind {
                        if matches!(pattern.kind, NodeKind::WildcardPat) {
                            self.buf.push_str("default: ");
                        } else {
                            self.buf.push_str("case ");
                            self.emit_match_case_condition(pattern)?;
                            self.buf.push_str(": ");
                        }
                        // Bind the narrowed `__v`'s payload fields for this arm
                        // (`level := __v.Level; _ = level`) before the body.
                        if user_enum_binds {
                            self.emit_user_enum_arm_bindings(pattern)?;
                        }
                        self.current_expected_type = arm_expected.clone();
                        self.emit_arm_body_return(body, iife_ret.as_deref())?;
                        self.buf.push_str("; ");
                    }
                }
                // `}; <fallthrough>` — the switch's closing brace and the IIFE's
                // fallthrough are two statements on one line, so they need an
                // explicit separator (a bare `} return` is a Go syntax error:
                // "unexpected keyword return at end of statement"). A typed IIFE
                // uses `panic` (no `nil` for a concrete type); the untyped form
                // keeps `return nil`.
                if iife_ret.is_some() {
                    self.buf.push_str("}; panic(\"unreachable\") }()");
                } else {
                    self.buf.push_str("}; return nil }()");
                }
                self.current_expected_type = prev_expected;
                Ok(())
            }
            // Ownership nodes: erase in Go.
            NodeKind::Move { expr }
            | NodeKind::Borrow { expr }
            | NodeKind::MutableBorrow { expr } => self.emit_expr(expr),
            // Effect operation invocation.
            NodeKind::EffectOp {
                effect,
                operation,
                args,
            } => {
                let effect_name = effect.segments.last().map_or("effect", |s| s.name.as_str());
                let _ = write!(
                    self.buf,
                    "{}.{}",
                    to_camel_case(effect_name),
                    to_pascal_case(&operation.name)
                );
                self.buf.push('(');
                for (i, arg) in args.iter().enumerate() {
                    if i > 0 {
                        self.buf.push_str(", ");
                    }
                    self.emit_expr(&arg.value)?;
                }
                self.buf.push(')');
                Ok(())
            }
            // Type expressions: erased in Go expression context.
            NodeKind::TypeNamed { .. }
            | NodeKind::TypeTuple { .. }
            | NodeKind::TypeFunction { .. }
            | NodeKind::TypeOptional { .. }
            | NodeKind::TypeSelf => {
                self.buf.push_str("/* type */");
                Ok(())
            }
            NodeKind::EffectRef { path } => {
                let name = path
                    .segments
                    .iter()
                    .map(|s| s.name.as_str())
                    .collect::<Vec<_>>()
                    .join(".");
                self.buf.push_str(&name);
                Ok(())
            }
            NodeKind::Error => {
                self.buf.push_str("/* error */");
                Ok(())
            }
            // An expression-position `loop` (`let r = loop { … break <v> }`):
            // Bock's `loop` is a value-producing expression terminated by a
            // `break <v>`. Go's `for` carries no value, so lower it to an IIFE
            // `func() T { for { … } }()` whose body's `break <v>` becomes
            // `return <v>` (handled via `loop_expr_depth` in the `Break` arm). The
            // loop is infinite — it only leaves through a `break <v>` — so the
            // trailing `panic("unreachable")` after the `for` is never reached but
            // satisfies Go's "missing return" check.
            NodeKind::Loop { body } => {
                let iife_ret = self.expected_iife_type();
                let iife_ty = iife_ret.as_deref().unwrap_or("interface{}");
                // The IIFE consumes the expected type for its own return; the loop
                // body re-types its own `break` values, so it must not leak into
                // the statements within.
                let prev_expected = self.current_expected_type.take();
                let _ = writeln!(self.buf, "func() {iife_ty} {{");
                self.indent += 1;
                self.loop_expr_depth += 1;
                self.emit_loop_label_prefix(body);
                self.writeln("for {");
                self.indent += 1;
                self.emit_block_body(body)?;
                self.indent -= 1;
                self.writeln("}");
                self.loop_labels.pop();
                self.loop_expr_depth -= 1;
                if iife_ty == "interface{}" {
                    self.writeln("return nil");
                } else {
                    self.writeln("panic(\"unreachable\")");
                }
                self.indent -= 1;
                self.write_indent();
                self.buf.push_str("}()");
                self.current_expected_type = prev_expected;
                Ok(())
            }
            _ => {
                self.buf.push_str("/* unsupported */");
                Ok(())
            }
        }
    }

    // ── Match → switch/if-else ──────────────────────────────────────────────

    /// Push a loop scope, emitting a Go label before the loop iff a contained
    /// statement-arm `match` needs to `break`/`continue` the loop (Go's `break`
    /// otherwise exits the inner `switch`). Must be paired with a
    /// `self.loop_labels.pop()` after the loop body is emitted.
    fn emit_loop_label_prefix(&mut self, body: &AIRNode) {
        if go_loop_needs_label(body) {
            self.loop_label_counter += 1;
            let label = format!("__bockLoop{}", self.loop_label_counter);
            let ind = self.indent_str();
            // A Go label sits in column-0-ish; we keep current indent for
            // readability — gofmt would re-align but the program is valid.
            let _ = writeln!(self.buf, "{ind}{label}:");
            self.loop_labels.push(Some(label));
        } else {
            self.loop_labels.push(None);
        }
    }

    /// The label of the innermost loop, if one was allocated. Used by
    /// `break`/`continue` emitted inside a statement-arm `switch`.
    fn innermost_loop_label(&self) -> Option<&str> {
        self.loop_labels.last().and_then(|l| l.as_deref())
    }

    /// Emit an `Optional` `match` in expression position as an IIFE that
    /// dispatches on the runtime tag (`__bockOption.tag`). `Some(v)` binds
    /// `v` from `.v` (as `interface{}`); `None`/`_` is the fallthrough.
    fn emit_optional_match_expr(
        &mut self,
        scrutinee: &AIRNode,
        arms: &[AIRNode],
    ) -> Result<(), CodegenError> {
        let elem = self.scrutinee_optional_elem(scrutinee);
        // Type the IIFE with the binding's *expected* type (a typed `let x: T =
        // match …` or a function-return `return match …`, both surfaced through
        // `current_expected_type`) when concrete, so the IIFE result is
        // assignable to the destination — a bare `interface{}` IIFE is not
        // assignable to a named `__bockOption` / `[]T` / `int64` / `T` return.
        // When no concrete expected type is in scope (e.g. the match is nested
        // in a string interpolation, whose `%v` consumes `interface{}`), fall
        // back to the untyped IIFE, preserving the existing behavior.
        let iife_ret = self.typed_match_iife_type();
        let iife_ty = iife_ret.as_deref().unwrap_or("interface{}");
        let prev_expected = self.current_expected_type.take();
        // A collection-typed IIFE (`func() []T { … }`) propagates its element
        // type to the arm bodies' collection literals, so `to_list`'s `[x]`/`[]`
        // arms emit `[]T{x}` / `[]T{}` rather than the `[]interface{}` default
        // (which is not assignable to the `[]T` return). Re-applied per arm
        // inside the arms emitter (a literal `take()`s the hint, so a single
        // top-level set would only reach the first arm).
        let iife_coll = iife_ret.as_deref().and_then(Self::rendered_collection_elem);
        let _ = write!(self.buf, "func() {iife_ty} {{ __opt := ");
        self.emit_expr(scrutinee)?;
        self.buf.push_str("; ");
        self.emit_optional_match_arms(
            arms,
            /*as_expr=*/ true,
            elem.as_deref(),
            iife_ret.is_some(),
            iife_coll.as_ref(),
            iife_ret.as_deref(),
        )?;
        self.buf.push_str(" }()");
        self.current_expected_type = prev_expected;
        Ok(())
    }

    /// Parse a rendered Go collection type string into its element Go type(s) for
    /// [`Self::expected_collection_elem`]: `[]int64` → `("int64", None)`,
    /// `map[string]int64` → `("string", Some("int64"))`. `None` for a
    /// non-collection rendering. Used to propagate a collection-typed IIFE
    /// return into the arm bodies' literals.
    fn rendered_collection_elem(ty: &str) -> Option<(String, Option<String>)> {
        let ty = ty.trim();
        if let Some(elem) = ty.strip_prefix("[]") {
            return Some((elem.to_string(), None));
        }
        if let Some(rest) = ty.strip_prefix("map[") {
            // Split on the matching close bracket of the key type (top-level).
            let mut depth = 0i32;
            for (i, ch) in rest.char_indices() {
                match ch {
                    '[' => depth += 1,
                    ']' if depth == 0 => {
                        let key = rest[..i].to_string();
                        let val = rest[i + 1..].to_string();
                        return Some((key, Some(val)));
                    }
                    ']' => depth -= 1,
                    _ => {}
                }
            }
        }
        None
    }

    /// The Go type a typed expression-position `Optional`/`Result` match IIFE
    /// should return: the binding's *expected* type ([`Self::current_expected_type`],
    /// set around a typed `let x: T = …` value emit or a function-return tail)
    /// when known and concrete. Unlike [`Self::expected_iife_type`] this does
    /// **not** fall back to the enclosing function's return type: an
    /// `Optional`/`Result` match nested in a non-return position (a string
    /// interpolation argument, say) must stay the untyped `interface{}` IIFE its
    /// `%v` consumer expects, never adopt the function's unrelated return type.
    /// `None` ⇒ the caller emits the `interface{}` fallback.
    fn typed_match_iife_type(&self) -> Option<String> {
        match self.current_expected_type.as_deref() {
            Some(t) if t != "interface{}" => Some(t.to_string()),
            _ => None,
        }
    }

    /// True when `node` is an *expression-position* `Optional`/`Result` match —
    /// a `match` over `Some`/`None` or `Ok`/`Err` whose arms are all
    /// value-producing (no statement arm). Such a match lowers to a typed IIFE
    /// (`func() __bockOption { … }()`) whose return must be assignable to the
    /// enclosing return/binding type; the callers use this to propagate the
    /// expected type into [`Self::current_expected_type`], mirroring the
    /// generic-record-construction case.
    fn is_expr_optional_or_result_match(node: &AIRNode) -> bool {
        if let NodeKind::Match { arms, .. } = &node.kind {
            !crate::generator::match_has_statement_arm(arms)
                && (go_match_is_optional(arms) || go_match_is_result(arms))
        } else {
            false
        }
    }

    /// Emit an `Optional` `match` in statement position as an if/else chain on
    /// the runtime tag.
    fn emit_optional_match_stmt(
        &mut self,
        scrutinee: &AIRNode,
        arms: &[AIRNode],
    ) -> Result<(), CodegenError> {
        let elem = self.scrutinee_optional_elem(scrutinee);
        // Wrap the `__opt := …` binding + tag chain in a Go block so the scrutinee
        // temp is scoped to this match. Two sequential statement matches in one
        // function (`match a {…}` then `match b {…}`) otherwise both emit
        // `__opt := …` in the same scope → `no new variables on left side of :=`.
        let ind = self.indent_str();
        let _ = writeln!(self.buf, "{ind}{{");
        self.indent += 1;
        let ind2 = self.indent_str();
        let _ = write!(self.buf, "{ind2}__opt := ");
        self.emit_expr(scrutinee)?;
        self.buf.push('\n');
        self.write_indent();
        self.emit_optional_match_arms(
            arms,
            /*as_expr=*/ false,
            elem.as_deref(),
            false,
            None,
            None,
        )?;
        self.buf.push('\n');
        self.indent -= 1;
        self.writeln("}");
        Ok(())
    }

    /// Shared body for [`emit_optional_match_expr`] /
    /// [`emit_optional_match_stmt`]: an if/else chain on the option tag. In
    /// expression mode each arm body is `return`ed; in statement mode the arm
    /// body is emitted as a block.
    #[allow(clippy::too_many_arguments)]
    fn emit_optional_match_arms(
        &mut self,
        arms: &[AIRNode],
        as_expr: bool,
        some_elem_ty: Option<&str>,
        typed_iife: bool,
        iife_coll: Option<&(String, Option<String>)>,
        iife_ty: Option<&str>,
    ) -> Result<(), CodegenError> {
        // Save the caller's pending collection-element hint: the arm bodies
        // re-set it per-arm (`iife_coll`), so it must be restored on exit rather
        // than clobbered (a match-expr may itself be a collection-literal element).
        let saved_coll = self.expected_collection_elem.take();
        let mut first = true;
        let arm_count = arms.len();
        for (idx, arm) in arms.iter().enumerate() {
            let NodeKind::MatchArm { pattern, body, .. } = &arm.kind else {
                continue;
            };
            let is_last = idx + 1 == arm_count;
            // Determine the tag test and any bound name. The final arm is
            // rendered as a plain `else` so the if-chain is exhaustive from
            // Go\'s control-flow view (Bock matches are exhaustive). Its bound
            // name (e.g. the `Some(v)` value) is still extracted.
            // `bind` is the payload name (the `v` in `Some(v)`); `bind_is_payload`
            // is true only when it binds the `Some` payload (not a catch-all
            // binding of the whole option), so the `interface{}` payload type
            // assertion applies to exactly that case.
            let (cond, bind, bind_is_payload): (String, Option<String>, bool) = match &pattern.kind
            {
                NodeKind::ConstructorPat { path, fields } => {
                    let variant = path.segments.last().map_or("", |s| s.name.as_str());
                    let bind = fields.first().map(|f| self.pattern_to_binding_name(f));
                    let is_payload = bind.is_some() && variant == "Some";
                    if is_last {
                        (String::new(), bind, is_payload)
                    } else {
                        (format!("__opt.tag == \"{variant}\""), bind, is_payload)
                    }
                }
                // Wildcard / bind pattern → catch-all.
                _ => (String::new(), None, false),
            };
            if first {
                first = false;
                if cond.is_empty() {
                    self.buf.push('{');
                } else {
                    let _ = write!(self.buf, "if {cond} {{");
                }
            } else if cond.is_empty() {
                self.buf.push_str(" else {");
            } else {
                let _ = write!(self.buf, " else if {cond} {{");
            }
            self.buf.push(' ');
            if let Some(name) = &bind {
                if name != "_" {
                    // The runtime stores the payload as `interface{}`. Assert it
                    // back to the concrete element type so typed use of the bound
                    // value (`x + 10`, a typed call) compiles. The element type
                    // comes from the scrutinee's `Optional[T]` (resolved
                    // structurally by the caller); when unknown, fall back to the
                    // bare `interface{}` payload — no regression, but typed use
                    // would not compile, which only happens if the scrutinee's
                    // element type is not structurally determinable.
                    match (bind_is_payload, some_elem_ty) {
                        // Numeric element types are recovered through the
                        // widening helpers rather than a hard `.(int64)` /
                        // `.(float64)` assertion: a payload constructed from an
                        // untyped Go constant (`Some(10)` → `__bockSome(10)`)
                        // boxes a Go `int`/`float64`, on which `.(int64)` panics.
                        (true, Some("int64")) => {
                            let _ =
                                write!(self.buf, "{name} := __bockAsInt64(__opt.v); _ = {name}; ");
                        }
                        (true, Some("float64")) => {
                            let _ = write!(
                                self.buf,
                                "{name} := __bockAsFloat64(__opt.v); _ = {name}; "
                            );
                        }
                        (true, Some(ty)) => {
                            let _ = write!(self.buf, "{name} := __opt.v.({ty}); _ = {name}; ");
                        }
                        _ => {
                            let _ = write!(self.buf, "{name} := __opt.v; _ = {name}; ");
                        }
                    }
                }
            }
            if as_expr {
                // Re-apply the IIFE's collection element per arm: a list/map/set
                // literal `take()`s the hint, so without re-setting it only the
                // first arm's literal would adopt the `[]T` element (`to_list`'s
                // `Some` arm), leaving the `None` arm's `[]` as `[]interface{}`.
                self.expected_collection_elem = iife_coll.cloned();
                // A void-call arm tail emits as a statement + discarded zero
                // (`return println(..)` is a Go arity error).
                self.emit_arm_body_return(body, iife_ty)?;
                self.buf.push(' ');
            } else {
                self.buf.push('\n');
                self.indent += 1;
                self.emit_block_body(body)?;
                self.indent -= 1;
                self.write_indent();
            }
            self.buf.push('}');
        }
        self.expected_collection_elem = saved_coll;
        // Expression mode needs a trailing value if no arm matched. A `;`
        // separates it from the preceding `}` (Go requires a terminator). A
        // *typed* IIFE has no `nil` of its concrete return type, so it closes
        // with `panic` (a Bock match is exhaustive, so this is unreachable);
        // the untyped form keeps `return nil`.
        if as_expr {
            if typed_iife {
                self.buf.push_str("; panic(\"unreachable\")");
            } else {
                self.buf.push_str("; return nil");
            }
        }
        Ok(())
    }

    /// Emit a `Result` `match` in expression position as an IIFE that dispatches
    /// on the runtime tag (`__bockResult.tag`). Mirrors
    /// [`Self::emit_optional_match_expr`].
    fn emit_result_match_expr(
        &mut self,
        scrutinee: &AIRNode,
        arms: &[AIRNode],
    ) -> Result<(), CodegenError> {
        let elems = self.scrutinee_result_elems(scrutinee);
        // Type the IIFE with the expected destination type when concrete (see
        // [`Self::emit_optional_match_expr`]); else the untyped fallback.
        let iife_ret = self.typed_match_iife_type();
        let iife_ty = iife_ret.as_deref().unwrap_or("interface{}");
        let prev_expected = self.current_expected_type.take();
        let iife_coll = iife_ret.as_deref().and_then(Self::rendered_collection_elem);
        let _ = write!(self.buf, "func() {iife_ty} {{ __res := ");
        self.emit_expr(scrutinee)?;
        self.buf.push_str("; ");
        self.emit_result_match_arms(
            arms,
            /*as_expr=*/ true,
            elems.as_ref(),
            iife_ret.is_some(),
            iife_coll.as_ref(),
            iife_ret.as_deref(),
        )?;
        self.buf.push_str(" }()");
        self.current_expected_type = prev_expected;
        Ok(())
    }

    /// Emit a `Result` `match` in statement position as an if/else chain on the
    /// runtime tag. Mirrors [`Self::emit_optional_match_stmt`].
    fn emit_result_match_stmt(
        &mut self,
        scrutinee: &AIRNode,
        arms: &[AIRNode],
    ) -> Result<(), CodegenError> {
        let elems = self.scrutinee_result_elems(scrutinee);
        // Scope the `__res := …` temp to a Go block so sequential statement
        // matches in one function don't collide on `__res` (`no new variables on
        // left side of :=`). See `emit_optional_match_stmt`.
        let ind = self.indent_str();
        let _ = writeln!(self.buf, "{ind}{{");
        self.indent += 1;
        let ind2 = self.indent_str();
        let _ = write!(self.buf, "{ind2}__res := ");
        self.emit_expr(scrutinee)?;
        self.buf.push('\n');
        self.write_indent();
        self.emit_result_match_arms(
            arms,
            /*as_expr=*/ false,
            elems.as_ref(),
            false,
            None,
            None,
        )?;
        self.buf.push('\n');
        self.indent -= 1;
        self.writeln("}");
        Ok(())
    }

    /// Shared body for the `Result` match emitters: an if/else chain on the
    /// result tag. `Ok(v)` binds `v` from `__res.v` asserted to the Ok type;
    /// `Err(e)` binds `e` asserted to the Err type. Mirrors
    /// [`Self::emit_optional_match_arms`].
    #[allow(clippy::too_many_arguments)]
    fn emit_result_match_arms(
        &mut self,
        arms: &[AIRNode],
        as_expr: bool,
        elems: Option<&(String, String)>,
        typed_iife: bool,
        iife_coll: Option<&(String, Option<String>)>,
        iife_ty: Option<&str>,
    ) -> Result<(), CodegenError> {
        // See `emit_optional_match_arms`: preserve the caller's pending
        // collection-element hint across the per-arm re-application.
        let saved_coll = self.expected_collection_elem.take();
        let mut first = true;
        let arm_count = arms.len();
        for (idx, arm) in arms.iter().enumerate() {
            let NodeKind::MatchArm { pattern, body, .. } = &arm.kind else {
                continue;
            };
            let is_last = idx + 1 == arm_count;
            // `(cond, bind, variant)`: the final arm is a plain `else` (Bock
            // matches are exhaustive), but its payload bind is still extracted.
            let (cond, bind, variant): (String, Option<String>, Option<&str>) = match &pattern.kind
            {
                NodeKind::ConstructorPat { path, fields } => {
                    let variant = path.segments.last().map_or("", |s| s.name.as_str());
                    let bind = fields.first().map(|f| self.pattern_to_binding_name(f));
                    if is_last {
                        (String::new(), bind, Some(variant))
                    } else {
                        (format!("__res.tag == \"{variant}\""), bind, Some(variant))
                    }
                }
                _ => (String::new(), None, None),
            };
            if first {
                first = false;
                if cond.is_empty() {
                    self.buf.push('{');
                } else {
                    let _ = write!(self.buf, "if {cond} {{");
                }
            } else if cond.is_empty() {
                self.buf.push_str(" else {");
            } else {
                let _ = write!(self.buf, " else if {cond} {{");
            }
            self.buf.push(' ');
            if let Some(name) = &bind {
                if name != "_" {
                    // Assert the `interface{}` payload to the concrete Ok/Err
                    // type. Numeric payloads from untyped Go constants are widened
                    // via the shared helpers (a hard `.(int64)` would panic, see
                    // `NUMERIC_RUNTIME_GO`). When the type is unknown, bind the
                    // bare `interface{}` payload (never wrong, only un-asserted).
                    let payload_ty = match (variant, elems) {
                        (Some("Ok"), Some((ok, _))) => Some(ok.as_str()),
                        (Some("Err"), Some((_, err))) => Some(err.as_str()),
                        _ => None,
                    };
                    match payload_ty {
                        Some("int64") => {
                            let _ =
                                write!(self.buf, "{name} := __bockAsInt64(__res.v); _ = {name}; ");
                        }
                        Some("float64") => {
                            let _ = write!(
                                self.buf,
                                "{name} := __bockAsFloat64(__res.v); _ = {name}; "
                            );
                        }
                        Some(ty) => {
                            let _ = write!(self.buf, "{name} := __res.v.({ty}); _ = {name}; ");
                        }
                        None => {
                            let _ = write!(self.buf, "{name} := __res.v; _ = {name}; ");
                        }
                    }
                }
            }
            if as_expr {
                // See `emit_optional_match_arms`: re-apply the IIFE collection
                // element per arm so every arm's literal adopts it, not just the
                // first.
                self.expected_collection_elem = iife_coll.cloned();
                // A void-call arm tail (`Err(e) => println(..)`) must emit the
                // call as a statement + discarded zero, not `return println(..)`
                // (a Go arity error: `fmt.Println` returns `(int, error)`).
                self.emit_arm_body_return(body, iife_ty)?;
                self.buf.push(' ');
            } else {
                self.buf.push('\n');
                self.indent += 1;
                self.emit_block_body(body)?;
                self.indent -= 1;
                self.write_indent();
            }
            self.buf.push('}');
        }
        self.expected_collection_elem = saved_coll;
        if as_expr {
            if typed_iife {
                self.buf.push_str("; panic(\"unreachable\")");
            } else {
                self.buf.push_str("; return nil");
            }
        }
        Ok(())
    }

    fn emit_match(&mut self, scrutinee: &AIRNode, arms: &[AIRNode]) -> Result<(), CodegenError> {
        // Guards, or-patterns, tuple patterns, and nested constructor/record
        // patterns cannot be expressed by the value/type `switch` below (a
        // failed guard's `break` exits the switch; an or-pattern has no single
        // discriminant; a nested sub-pattern's bindings are lost). Lower those
        // to an if/else-if chain. This takes priority over the Optional/Result
        // fast-paths so e.g. `Some(Ok(v))` (an Optional-leaf match that is still
        // nested) routes here. Additive: everything else keeps its existing
        // switch / tag-chain lowering (see `match_needs_ifchain`).
        if crate::generator::match_needs_ifchain(arms) {
            return self.emit_match_ifchain(scrutinee, arms);
        }
        // A plain (non-enum-variant) record pattern with only bind/wildcard
        // fields (`Point { x, .. }`) is a concrete Go struct, not a
        // sealed-interface value: it has no type/value to `switch` on
        // (`switch __v.(type) { case Point: }` is invalid — `__v` is not an
        // interface). Route it to the if-chain, which reads each field directly
        // off `access.<Field>`. Mirrors the expression-position diversion. Does
        // not touch the shared `match_needs_ifchain`.
        if self.go_value_match_has_plain_record_arm(arms) {
            return self.emit_match_ifchain(scrutinee, arms);
        }
        // `Optional` / `Result` matches dispatch on the runtime tag, not a Go
        // type/value switch.
        if go_match_is_optional(arms) {
            return self.emit_optional_match_stmt(scrutinee, arms);
        }
        if go_match_is_result(arms) {
            return self.emit_result_match_stmt(scrutinee, arms);
        }
        // A user enum lowers to a type-switch over the sealed-interface concrete
        // variant structs, binding each arm's payload fields from `__v`.
        let user_enum = self.go_match_is_user_enum(arms);
        // The prelude `Ordering` is a `__bockOrdering` *value* enum (constants),
        // so its match is a value-switch (`switch o { case Less: }`), never the
        // type-switch user enums use — `Less` is a constant, not a Go type.
        let ordering = !user_enum && go_match_is_ordering(arms);
        // Choose value-switch (`switch v { case 5: }`) vs type-switch
        // (`switch v := s.(type) { case T: }`) by pattern kind: constructor /
        // record patterns dispatch on dynamic type; literal / bind patterns
        // dispatch on value. `Ordering` is forced to a value-switch.
        let type_switch = !ordering && (user_enum || go_match_is_type_switch(arms));
        // A value-switch arm may bind the whole scrutinee (`x => …`). The
        // scrutinee is bound into `__v` via the switch's init clause so the arm
        // can emit `x := __v` — without this the `default:` discarded the name
        // and the body referenced an undefined variable (the Go binding-drop
        // defect). Only needed for the value-switch path; the type-switches
        // already bind `__v`.
        let value_switch_binds = !user_enum
            && !type_switch
            && arms.iter().any(|arm| {
                matches!(
                    &arm.kind,
                    NodeKind::MatchArm { pattern, .. } if matches!(pattern.kind, NodeKind::BindPat { .. })
                )
            });
        // The user-enum type-switch binds `__v` only when something reads it:
        // an arm that extracts a payload field (`__v.Radius`) or the trailing
        // `default: panic(... __v)` added for a non-exhaustive (catch-all-free)
        // match. A payload-less, catch-all-bearing match (e.g. `match ord {
        // Greater => …  _ => {} }`) binds nothing — emit a non-binding
        // `switch s.(type)` so Go does not reject an unused `__v`. Mirrors the
        // expression-position IIFE path (see `emit_match` expr lowering).
        let user_enum_default_panic = user_enum && !go_match_has_default_arm(arms);
        let user_enum_binds_v =
            user_enum && (Self::go_user_enum_match_binds_payload(arms) || user_enum_default_panic);
        let ind = self.indent_str();
        if user_enum && !user_enum_binds_v {
            // Non-binding type-switch: no arm reads `__v` and no `__v`-consuming
            // default panic follows, so binding it would be "declared and not
            // used".
            let _ = write!(self.buf, "{ind}switch ");
            self.emit_expr(scrutinee)?;
            self.buf.push_str(".(type) {\n");
        } else if user_enum {
            // A *narrowing* type-switch: `switch __v := s.(type)` rebinds `__v`
            // to the concrete variant struct in each case, so the arm can read
            // its payload fields (`__v.Radius`). (The non-narrowing
            // `switch __v := s; __v.(type)` form does not give `__v` the
            // concrete type in the cases.)
            let _ = write!(self.buf, "{ind}switch __v := ");
            self.emit_expr(scrutinee)?;
            self.buf.push_str(".(type) {\n");
        } else if type_switch {
            let _ = write!(self.buf, "{ind}switch __v := ");
            self.emit_expr(scrutinee)?;
            self.buf.push_str("; __v.(type) {\n");
        } else if value_switch_binds {
            // `switch __v := <scrutinee>; __v { … }` — evaluate once, give the
            // value a name so a bind arm can alias it.
            let _ = write!(self.buf, "{ind}switch __v := ");
            self.emit_expr(scrutinee)?;
            self.buf.push_str("; __v {\n");
        } else {
            let _ = write!(self.buf, "{ind}switch ");
            self.emit_expr(scrutinee)?;
            self.buf.push_str(" {\n");
        }
        self.indent += 1;
        self.switch_label_depth += 1;
        // A generic user enum's variant structs carry the enum's type params, so
        // each `case BoxFull[int64]:` must spell the concrete instantiation
        // recovered from the scrutinee's type. Record it for the case emitter and
        // restore afterwards (matches nest). Empty for non-generic / non-user-enum
        // matches, leaving the bare `case ShapeCircle:` form unchanged.
        let new_match_args = if user_enum {
            self.match_enum_type_arg_suffix(scrutinee, arms)
        } else {
            String::new()
        };
        let prev_match_args =
            std::mem::replace(&mut self.current_match_enum_type_args, new_match_args);
        for arm in arms {
            self.emit_match_arm(arm, user_enum, value_switch_binds)?;
        }
        self.current_match_enum_type_args = prev_match_args;
        // Bock matches are exhaustive, but Go can't prove a type-switch covers
        // every implementor of a sealed interface (nor a value-switch every
        // `__bockOrdering` constant), so a function that returns a value after
        // the switch would fail to compile ("missing return"). When no arm is a
        // catch-all (`_` / bind), add a `default: panic(...)` so the switch is
        // total from Go's control-flow view.
        if (user_enum || ordering) && !go_match_has_default_arm(arms) {
            let di = self.indent_str();
            if user_enum {
                self.needs_fmt_import = true;
                let _ = write!(
                    self.buf,
                    "{di}default:\n{di}\tpanic(fmt.Sprintf(\"unreachable match arm: %v\", __v))\n"
                );
            } else {
                // Value-switch (`Ordering`): the scrutinee is not bound to a
                // local, so panic with a static message.
                let _ = write!(
                    self.buf,
                    "{di}default:\n{di}\tpanic(\"unreachable match arm\")\n"
                );
            }
        }
        self.switch_label_depth -= 1;
        self.indent -= 1;
        self.writeln("}");
        Ok(())
    }

    // ── Match → if/else-if chain (guards, or-/tuple/nested patterns) ──────────

    /// Lower a `match` whose arms cannot be expressed by a value/type `switch`
    /// (see [`crate::generator::match_needs_ifchain`]) to an `if <test> {
    /// <binds>; <body> } else if …` chain.
    ///
    /// The scrutinee is evaluated once into `__match` (a typed local), so nested
    /// tests/binds read off a single stable, typed value. Each arm contributes
    /// one `if`/`else if`; an unguarded catch-all (or the final unguarded arm,
    /// since Bock matches are exhaustive) becomes the unconditional `else`. A
    /// chain not closed by an `else` gets a trailing `else { panic(...) }` so a
    /// value-returning function still compiles (Go cannot prove exhaustiveness).
    ///
    /// Unlike the `switch` lowering, a bare `break`/`continue` in an arm body
    /// targets the enclosing `for` directly (there is no switch to escape), so
    /// `switch_label_depth` is deliberately left untouched.
    ///
    /// Statement position: arm bodies run as statements (`emit_return = false`).
    /// The expression-position caller (`func() T { … }()`) passes
    /// `emit_return = true` so each arm body's tail becomes a `return`, yielding
    /// the match's value.
    fn emit_match_ifchain(
        &mut self,
        scrutinee: &AIRNode,
        arms: &[AIRNode],
    ) -> Result<(), CodegenError> {
        self.emit_match_ifchain_inner(scrutinee, arms, /*emit_return=*/ false)
    }

    fn emit_match_ifchain_inner(
        &mut self,
        scrutinee: &AIRNode,
        arms: &[AIRNode],
        emit_return: bool,
    ) -> Result<(), CodegenError> {
        // Single-evaluation root. A bare identifier is already a stable, typed
        // reference (emit it through the normal expression path so its name
        // matches the rest of the program); anything else is hoisted into a
        // typed `__match` local. Either way, leave the cursor indented at the
        // chain's column so the leading `if` lines up.
        let ind = self.indent_str();
        let root: String = if matches!(scrutinee.kind, NodeKind::Identifier { .. }) {
            let r = self.expr_to_string(scrutinee)?;
            self.write_indent();
            r
        } else {
            let _ = write!(self.buf, "{ind}__match := ");
            self.emit_expr(scrutinee)?;
            self.buf.push('\n');
            self.write_indent();
            "__match".to_string()
        };

        // The scrutinee's declared type, cloned to an owned node so it survives
        // the `&mut self` bind emit below. Threads through the pattern lowering
        // so a *nested tuple* payload (`Some(Ok((a, b)))`) is asserted to its
        // concrete tuple struct rather than read off a bare `interface{}`.
        let decl_ty: Option<AIRNode> = self.scrutinee_decl_type_node(scrutinee).cloned();

        let arm_count = arms.len();
        let mut first = true;
        let mut closed = false;
        for (idx, arm) in arms.iter().enumerate() {
            let NodeKind::MatchArm {
                pattern,
                guard,
                body,
            } = &arm.kind
            else {
                continue;
            };
            let test = self.pattern_test_go(pattern, &root, decl_ty.as_ref());
            let is_catch_all = matches!(
                pattern.kind,
                NodeKind::WildcardPat | NodeKind::BindPat { .. }
            );
            let is_last = idx + 1 == arm_count;
            let unconditional = guard.is_none() && (is_catch_all || is_last);

            if unconditional {
                if first {
                    self.buf.push('{');
                } else {
                    self.buf.push_str(" else {");
                }
                closed = true;
            } else {
                let mut cond = if test.is_empty() {
                    "true".to_string()
                } else {
                    test
                };
                if let Some(g) = guard {
                    // The guard may reference the arm's pattern bindings; they
                    // are only introduced inside the arm body, so evaluate the
                    // guard in an anonymous func that re-introduces them. A
                    // failed guard then falls through to the next `else if` (the
                    // fall-through a `switch` could not express).
                    let g_str = self.expr_to_string(g)?;
                    let binds =
                        self.pattern_binds_to_string_go_typed(pattern, &root, decl_ty.as_ref());
                    let guard_test = if binds.is_empty() {
                        format!("({g_str})")
                    } else {
                        format!("func() bool {{ {binds}return ({g_str}) }}()")
                    };
                    if cond == "true" {
                        cond = guard_test;
                    } else {
                        cond = format!("{cond} && {guard_test}");
                    }
                }
                if first {
                    let _ = write!(self.buf, "if {cond} {{");
                } else {
                    let _ = write!(self.buf, " else if {cond} {{");
                }
            }
            first = false;
            self.buf.push('\n');
            self.indent += 1;
            self.pattern_binds_go_typed(pattern, &root, decl_ty.as_ref())?;
            if emit_return {
                self.emit_block_body_return(body)?;
            } else {
                self.emit_block_body(body)?;
            }
            self.indent -= 1;
            self.write_indent();
            self.buf.push('}');
        }
        // A chain with no unconditional arm (all guarded, or no catch-all) needs
        // a trailing panic so a value-returning function compiles and an
        // unmatched scrutinee fails loudly. Bock matches are exhaustive, so this
        // is only ever reached if a guard chain is non-total.
        if !closed && !first {
            self.buf.push_str(" else {\n");
            self.indent += 1;
            self.writeln("panic(\"non-exhaustive match\")");
            self.indent -= 1;
            self.write_indent();
            self.buf.push('}');
        }
        self.buf.push('\n');
        Ok(())
    }

    /// Lower `guard (let PAT = COND) else { … }` (Go has no `let-else`).
    ///
    /// The pattern's bindings must stay live for the rest of the enclosing block
    /// (the whole point of guard-let), and the else arm must diverge (Bock's
    /// guard semantics guarantee it — `return`/`break`/`continue`/`panic`). The
    /// prior boolean `if !(cond)` lowering both negated a non-bool discriminant
    /// (`!(__bockResult{…})` — a `go build` error) and dropped the bound names
    /// (`undefined: v`). This emits:
    ///
    /// ```text
    /// __guardN := <cond>
    /// if !(<discriminant test>) { <else, diverges> }
    /// <name> := <typed payload of __guardN>   // bindings live after the guard
    /// ```
    ///
    /// `Ok`/`Some` payloads are asserted to their concrete element type (recovered
    /// from the scrutinee's `Result`/`Optional` element), so typed downstream use
    /// (`compare(guess, target)`) compiles; other constructor / record / tuple
    /// patterns reuse the shared bind emitter. A bare-bind pattern (always
    /// matches) emits only the binding (the else is unreachable).
    fn emit_guard_let(
        &mut self,
        pat: &AIRNode,
        condition: &AIRNode,
        else_block: &AIRNode,
    ) -> Result<(), CodegenError> {
        let n = self.guard_counter;
        self.guard_counter += 1;
        let guard_tmp = format!("__guard{n}");

        // Evaluate the discriminant once into a typed local (`:=`), so the test
        // and the payload bindings read off one stable value.
        let ind = self.indent_str();
        let _ = write!(self.buf, "{ind}{guard_tmp} := ");
        self.emit_expr(condition)?;
        self.buf.push('\n');

        // The else arm runs when the pattern does NOT match; it diverges.
        let test = self.pattern_test_go(pat, &guard_tmp, None);
        if !test.is_empty() {
            self.writeln(&format!("if !({test}) {{"));
            self.indent += 1;
            self.emit_block_body(else_block)?;
            self.indent -= 1;
            self.writeln("}");
        } else {
            // A bare-bind/wildcard pattern always matches; bind `__guard` itself
            // so it is not "declared and not used", and drop the dead else arm.
            self.writeln(&format!("_ = {guard_tmp}"));
        }

        // Introduce the pattern's bindings into the *enclosing* scope (live after
        // the guard). Constructor payloads get a concrete-typed assertion.
        self.emit_guard_let_binds(pat, condition, &guard_tmp)?;
        Ok(())
    }

    /// Emit the bindings a guard-let pattern introduces, into the current
    /// (enclosing) Go block scope. Reuses the concrete payload typing the
    /// Optional/Result match arms apply (`__bockAsInt64`, `.v.(string)`, …) for an
    /// `Ok`/`Some` payload bind; everything else falls back to the shared
    /// [`Self::pattern_binds_go`] emitter. Each emitted name is recorded in the
    /// block's declared-name frame so a later same-name `let` reassigns.
    fn emit_guard_let_binds(
        &mut self,
        pat: &AIRNode,
        condition: &AIRNode,
        access: &str,
    ) -> Result<(), CodegenError> {
        if let NodeKind::ConstructorPat { path, fields } = &pat.kind {
            let leaf = path.segments.last().map_or("", |s| s.name.as_str());
            // Optional/Result single-payload constructor: assert the boxed
            // `interface{}` payload to its concrete element type so typed use of
            // the bound value compiles.
            if matches!(leaf, "Some" | "Ok" | "Err") {
                if let Some(f) = fields.first() {
                    if let NodeKind::BindPat { name, .. } = &f.kind {
                        let bind = go_value_ident(&name.name);
                        if bind != "_" {
                            let elem_ty = match leaf {
                                "Some" => self.scrutinee_optional_elem(condition),
                                "Ok" => self.scrutinee_result_elems(condition).map(|(ok, _)| ok),
                                _ => self.scrutinee_result_elems(condition).map(|(_, e)| e),
                            };
                            let rhs = match elem_ty.as_deref() {
                                Some("int64") => format!("__bockAsInt64({access}.v)"),
                                Some("float64") => format!("__bockAsFloat64({access}.v)"),
                                Some(ty) => format!("{access}.v.({ty})"),
                                None => format!("{access}.v"),
                            };
                            self.writeln(&format!("{bind} := {rhs}"));
                            self.writeln(&format!("_ = {bind}"));
                            self.go_record_declared(&bind);
                            return Ok(());
                        }
                    }
                    // Nested payload pattern (`Ok(Ok(v))`, `Some((a, b))`): the
                    // payload is re-asserted to its runtime struct where needed
                    // and the shared emitter recurses.
                    let child = go_typed_access(f, &format!("{access}.v"));
                    return self.emit_guard_let_binds_generic(f, &child);
                }
                return Ok(());
            }
        }
        // User-enum / record / tuple / bind patterns: the shared bind emitter
        // already extracts payloads off the asserted variant struct.
        self.emit_guard_let_binds_generic(pat, access)
    }

    /// Fallback guard-let binder: emit `pat`'s bindings off `access` via the
    /// shared [`Self::pattern_binds_go`] machinery, then record each bound name in
    /// the current Go block frame so a later same-name `let` reassigns.
    fn emit_guard_let_binds_generic(
        &mut self,
        pat: &AIRNode,
        access: &str,
    ) -> Result<(), CodegenError> {
        self.pattern_binds_go(pat, access)?;
        let mut binds = String::new();
        self.collect_binds_go(pat, access, None, &mut binds);
        for stmt in binds.split("; ") {
            // Each emitted bind is `name := …`; record the `name`.
            if let Some(name) = stmt.split_whitespace().next() {
                if name != "_" {
                    self.go_record_declared(name);
                }
            }
        }
        Ok(())
    }

    /// Lower a `?` propagation operand (Go has no native `?`).
    ///
    /// Emits, at statement position, the unwrap-or-early-return prelude for
    /// `<inner>?`:
    ///
    /// ```text
    /// __tryN := <inner>
    /// if __tryN.tag == "Err" { return __tryN }     // Result: propagate the Err
    /// // or, for an Optional operand:
    /// if __tryN.tag == "None" { return __bockNone() }
    /// ```
    ///
    /// and returns the Go expression that reads the success payload
    /// (`__tryN.v` asserted to the concrete `Ok`/`Some` element type when known).
    /// The enclosing function returns a `__bockResult` / `__bockOption`, so an
    /// `Err` operand re-propagates directly (`return __tryN`) and a `None` operand
    /// returns `__bockNone()`. Bock's type checker guarantees the operand's
    /// container and error type are compatible with the enclosing return type, so
    /// no zero-value reconstruction of a *different* error type is needed.
    ///
    /// `?` is only reached in statement-adjacent positions in practice (`let x =
    /// e?`, a bare `e?` statement, a tail `e?`); a `?` nested inside a larger
    /// expression (`Ok(f()? + 1)`) is not hoisted by this path — the inner emit
    /// goes through the normal expression emitter. None of the v1 examples nest
    /// `?` that way.
    fn emit_try_unwrap(&mut self, inner: &AIRNode) -> Result<String, CodegenError> {
        let n = self.try_counter;
        self.try_counter += 1;
        let tmp = format!("__try{n}");

        // Evaluate the operand once.
        let ind = self.indent_str();
        let _ = write!(self.buf, "{ind}{tmp} := ");
        self.emit_expr(inner)?;
        self.buf.push('\n');

        // Decide Result vs Optional. Both lower to a runtime-tag check, but the
        // failure tag (`Err` vs `None`) and propagation value differ. Preference:
        //   1. the operand's recoverable `Result`/`Optional` element type;
        //   2. otherwise the enclosing function's return container
        //      (`__bockResult` → Result, `__bockOption` → Optional);
        //   3. default to Result — the overwhelmingly common case, and
        //      `return __tryN` is always a valid `__bockResult` to propagate.
        let result_elems = self.scrutinee_result_elems(inner);
        let opt_elem = self.scrutinee_optional_elem(inner);
        let is_optional = if result_elems.is_some() {
            false
        } else if opt_elem.is_some() {
            true
        } else {
            self.current_fn_ret_type.as_deref() == Some("__bockOption")
        };
        if is_optional {
            // Optional operand: a `None` short-circuits to the enclosing fn's
            // `None`. `__bockNone` is the runtime `__bockOption` None value.
            self.writeln(&format!("if {tmp}.tag == \"None\" {{"));
            self.indent += 1;
            self.writeln("return __bockNone");
            self.indent -= 1;
            self.writeln("}");
            Ok(self.try_payload_access(&tmp, opt_elem.as_deref()))
        } else {
            self.writeln(&format!("if {tmp}.tag == \"Err\" {{"));
            self.indent += 1;
            self.writeln(&format!("return {tmp}"));
            self.indent -= 1;
            self.writeln("}");
            let ok_ty = result_elems.map(|(ok, _)| ok);
            Ok(self.try_payload_access(&tmp, ok_ty.as_deref()))
        }
    }

    /// The Go expression that reads a `?`-unwrapped payload from `tmp.v`, asserted
    /// to the concrete element type `elem` when known. Numeric payloads use the
    /// widening helpers (an untyped Go constant boxed as `int`/`float64` would
    /// panic on a hard `.(int64)` assertion); everything else uses a direct type
    /// assertion, falling back to the bare `interface{}` payload when the element
    /// type is not statically recoverable.
    fn try_payload_access(&self, tmp: &str, elem: Option<&str>) -> String {
        match elem {
            Some("int64") => format!("__bockAsInt64({tmp}.v)"),
            Some("float64") => format!("__bockAsFloat64({tmp}.v)"),
            // A `Void`/unit payload (`Result[Void, _]` → `Ok(())` boxes `nil`)
            // and the `interface{}` fallback both read the raw payload — a hard
            // `.(struct{})` / `.(interface{})` assertion on a boxed `nil` panics.
            Some("struct{}") | Some("interface{}") | Some("any") | None => format!("{tmp}.v"),
            Some(ty) => format!("{tmp}.v.({ty})"),
        }
    }

    /// The Go expression reading an Optional/Result *leaf* payload bind off the
    /// runtime container `access` (`access.v`), asserted to the concrete element
    /// `elem` when known. The pattern-match analogue of [`Self::try_payload_access`]
    /// (which takes a bare temp name): a `match v { Some(n) if (n > 0) => … }`
    /// binds `n := access.v.(int64)` so typed use of `n` compiles. Numeric
    /// payloads use the widening helpers (a boxed untyped Go constant would panic
    /// on a hard `.(int64)`); `Void`/unit/`interface{}` and the unknown-type
    /// fallback read the raw payload (a hard assertion on a boxed `nil` panics).
    fn payload_access_go(&self, access: &str, elem: Option<&str>) -> String {
        match elem {
            Some("int64") => format!("__bockAsInt64({access}.v)"),
            Some("float64") => format!("__bockAsFloat64({access}.v)"),
            Some("struct{}") | Some("interface{}") | Some("any") | None => {
                format!("{access}.v")
            }
            Some(ty) => format!("{access}.v.({ty})"),
        }
    }

    /// Peel one declared-type layer for an Optional/Result constructor tag,
    /// returning the inner type node carried by the matched payload: `Some`/`None`
    /// peel `Optional[T]` → `T`; `Ok` peel `Result[T, E]` → `T`; `Err` → `E`.
    /// Returns `None` when the declared type is unknown or does not match the
    /// tag's container (the payload then stays the runtime `interface{}` — never
    /// wrong, only un-asserted). The result is `cloned` so the borrow of
    /// `decl_ty` does not outlive the recursion step.
    fn peel_constructor_decl_ty(
        &self,
        leaf: &str,
        decl_ty: Option<&AIRNode>,
    ) -> Option<Box<AIRNode>> {
        let ty = decl_ty?;
        match leaf {
            "Some" | "None" => self
                .optional_inner_type_node(ty)
                .map(|n| Box::new(n.clone())),
            "Ok" => self
                .result_inner_type_nodes(ty)
                .map(|(ok, _)| Box::new(ok.clone())),
            "Err" => self
                .result_inner_type_nodes(ty)
                .and_then(|(_, err)| err)
                .map(|n| Box::new(n.clone())),
            _ => None,
        }
    }

    /// The Go access expression for the payload of an Optional/Result
    /// constructor pattern's sole field. `Some(Ok(…))` re-asserts the boxed
    /// `interface{}` payload to the inner container runtime struct
    /// (`.v.(__bockResult)`), via [`go_typed_access`]. A nested *tuple* payload
    /// (`Some(Ok((a, b)))`) instead asserts the payload to its concrete tuple
    /// struct (`.v.(struct{ Field0 int64; Field1 int64 })`) so the subsequent
    /// `.Field0`/`.Field1` reads type-check — without it the field access lands
    /// on a bare `interface{}` and fails `go build`. `child_ty` is the
    /// peeled declared type of the payload (the tuple type, when known).
    fn constructor_child_access_go(
        &self,
        child: &AIRNode,
        access: &str,
        child_ty: Option<&AIRNode>,
    ) -> String {
        if let NodeKind::TuplePat { .. } = &child.kind {
            if let Some(t) = child_ty {
                if let NodeKind::TypeTuple { .. } = &t.kind {
                    let struct_ty = self.type_to_go(t);
                    return format!("{access}.v.({struct_ty})");
                }
            }
        }
        go_typed_access(child, &format!("{access}.v"))
    }

    /// The per-field declared type nodes of a tuple pattern position, recovered
    /// from a declared `TypeTuple` of the expected arity. Returns a vector of
    /// `Option<&AIRNode>` (one per field, `None` where the declared type is
    /// unknown or not a matching tuple) so the caller can thread each field's
    /// type into the element sub-pattern.
    fn tuple_field_decl_tys<'a>(
        &self,
        decl_ty: Option<&'a AIRNode>,
        arity: usize,
    ) -> Vec<Option<&'a AIRNode>> {
        if let Some(t) = decl_ty {
            if let NodeKind::TypeTuple { elems } = &t.kind {
                if elems.len() == arity {
                    return elems.iter().map(Some).collect();
                }
            }
        }
        vec![None; arity]
    }

    /// Build the boolean test that selects `pat` against the Go expression
    /// `access` (a correctly-typed value at this pattern position). Returns the
    /// empty string for a pattern that always matches (wildcard / bare bind).
    ///
    /// `decl_ty`, when present, is the declared type-expression node of the
    /// value at this position (recovered from the match scrutinee's declared
    /// type and peeled as the recursion descends `Some`/`Ok`/`Err`). It is only
    /// needed to type-assert a *nested tuple* payload (`Some(Ok((a, b)))`) to its
    /// concrete tuple struct before reading `.Field0`; every other arm ignores
    /// it and passes `None` down (the value at that position is already concrete).
    fn pattern_test_go(&self, pat: &AIRNode, access: &str, decl_ty: Option<&AIRNode>) -> String {
        match &pat.kind {
            NodeKind::WildcardPat | NodeKind::BindPat { .. } => String::new(),
            NodeKind::LiteralPat { lit } => {
                format!("{access} == {}", go_literal(lit))
            }
            NodeKind::ConstructorPat { path, fields } => {
                let leaf = path.segments.last().map_or("", |s| s.name.as_str());
                // Optional / Result dispatch on the runtime `.tag`; the payload
                // is `<access>.v` (an `interface{}` the child must re-assert).
                if matches!(leaf, "Some" | "None" | "Ok" | "Err") {
                    let mut tests = vec![format!("{access}.tag == \"{leaf}\"")];
                    if let Some(f) = fields.first() {
                        // Peel one declared-type layer matching this tag so a
                        // nested tuple payload can be asserted to its struct.
                        let child_ty = self.peel_constructor_decl_ty(leaf, decl_ty);
                        let child =
                            self.constructor_child_access_go(f, access, child_ty.as_deref());
                        let sub = self.pattern_test_go(f, &child, child_ty.as_deref());
                        if !sub.is_empty() {
                            tests.push(sub);
                        }
                    }
                    return tests.join(" && ");
                }
                // User enum: a sealed-interface value; test via a comma-ok type
                // assertion to the concrete variant struct.
                let variant_ty = self.go_variant_struct(path);
                format!("func() bool {{ _, ok := {access}.({variant_ty}); return ok }}()")
            }
            NodeKind::RecordPat { path, fields, .. } => {
                if self.user_variant_for_path(path).is_some() {
                    let variant_ty = self.go_variant_struct(path);
                    // Field sub-tests would require binding the asserted struct;
                    // a struct-variant record pattern with nested field patterns
                    // is rare — test the variant type and let binds extract.
                    let _ = fields;
                    return format!(
                        "func() bool {{ _, ok := {access}.({variant_ty}); return ok }}()"
                    );
                }
                // A *plain* record (`Point { x: 0, y }`) is already the concrete
                // struct, so test its field sub-patterns directly off
                // `access.<Field>` (a literal field constrains the arm; a bind /
                // wildcard field adds no test). Without these tests every plain-
                // record arm matched unconditionally, so `Point { x: 0, y: 0 }`
                // shadowed every later `Point { … }` arm.
                let mut tests = Vec::new();
                for f in fields {
                    if let Some(p) = &f.pattern {
                        let go_field = to_pascal_case(&f.name.name);
                        let sub = self.pattern_test_go(p, &format!("{access}.{go_field}"), None);
                        if !sub.is_empty() {
                            tests.push(sub);
                        }
                    }
                }
                if tests.is_empty() {
                    String::new()
                } else {
                    tests.join(" && ")
                }
            }
            NodeKind::TuplePat { elems } => {
                // The access is already the concrete tuple struct (the parent
                // `Some`/`Ok` peel asserted it). Per-field types come from the
                // declared tuple type when known.
                let field_tys = self.tuple_field_decl_tys(decl_ty, elems.len());
                let mut tests = Vec::new();
                for (i, e) in elems.iter().enumerate() {
                    let sub = self.pattern_test_go(
                        e,
                        &format!("{access}.Field{i}"),
                        field_tys.get(i).and_then(|t| *t),
                    );
                    if !sub.is_empty() {
                        tests.push(sub);
                    }
                }
                if tests.is_empty() {
                    String::new()
                } else {
                    tests.join(" && ")
                }
            }
            NodeKind::ListPat { elems, rest } => {
                // Bock lists are Go slices (`[]T`). `[a, b]` requires an exact
                // length; `[a, ..rest]` requires at least len(elems). Element
                // sub-patterns are tested positionally (`access[i]`); the rest
                // binds the tail slice and adds no test. Mirrors `pattern_test_js`.
                let n = elems.len();
                let len_test = if rest.is_some() {
                    format!("len({access}) >= {n}")
                } else {
                    format!("len({access}) == {n}")
                };
                let mut tests = vec![len_test];
                for (i, e) in elems.iter().enumerate() {
                    let sub = self.pattern_test_go(e, &format!("{access}[{i}]"), None);
                    if !sub.is_empty() {
                        tests.push(sub);
                    }
                }
                tests.join(" && ")
            }
            NodeKind::RangePat { lo, hi, inclusive } => {
                // `lo..hi` → `access >= lo && access < hi`; `lo..=hi` uses `<=`.
                // Mirrors `pattern_test_js`.
                let lo_s = range_bound_to_go(lo);
                let hi_s = range_bound_to_go(hi);
                let upper = if *inclusive { "<=" } else { "<" };
                format!("{access} >= {lo_s} && {access} {upper} {hi_s}")
            }
            NodeKind::OrPat { alternatives } => {
                let alts: Vec<String> = alternatives
                    .iter()
                    .map(|a| {
                        let t = self.pattern_test_go(a, access, decl_ty);
                        if t.is_empty() {
                            "true".to_string()
                        } else {
                            format!("({t})")
                        }
                    })
                    .collect();
                alts.join(" || ")
            }
            _ => String::new(),
        }
    }

    /// Emit the `name := <access…>; _ = name` bindings introduced by `pat`,
    /// recursing into nested constructor / record / tuple sub-patterns. The
    /// trailing `_ = name` keeps an unused binding from failing `go build`.
    fn pattern_binds_go(&mut self, pat: &AIRNode, access: &str) -> Result<(), CodegenError> {
        self.pattern_binds_go_typed(pat, access, None)
    }

    /// As [`Self::pattern_binds_go`], but threading the scrutinee's declared type
    /// node so a nested tuple payload is bound off a struct-asserted access.
    fn pattern_binds_go_typed(
        &mut self,
        pat: &AIRNode,
        access: &str,
        decl_ty: Option<&AIRNode>,
    ) -> Result<(), CodegenError> {
        let binds = self.pattern_binds_to_string_go_typed(pat, access, decl_ty);
        if binds.is_empty() {
            return Ok(());
        }
        // `pattern_binds_to_string_go` emits each `name := …; _ = name; `
        // separated by `; `; split onto its own indented line for readability.
        for stmt in binds.split("; ") {
            let stmt = stmt.trim();
            if stmt.is_empty() {
                continue;
            }
            self.writeln(stmt);
        }
        Ok(())
    }

    /// Collect `pat`'s bindings as a single-line string of `name := …; _ = name;
    /// ` statements. Shared by [`Self::pattern_binds_go`] (statement position)
    /// and the guard-evaluating anonymous func in [`Self::emit_match_ifchain`].
    /// Threads the scrutinee's declared type node so a nested tuple payload
    /// (`Some(Ok((a, b)))`) is bound off a struct-asserted access rather than a
    /// bare `interface{}`.
    fn pattern_binds_to_string_go_typed(
        &self,
        pat: &AIRNode,
        access: &str,
        decl_ty: Option<&AIRNode>,
    ) -> String {
        let mut out = String::new();
        self.collect_binds_go(pat, access, decl_ty, &mut out);
        out
    }

    fn collect_binds_go(
        &self,
        pat: &AIRNode,
        access: &str,
        decl_ty: Option<&AIRNode>,
        out: &mut String,
    ) {
        match &pat.kind {
            NodeKind::BindPat { name, .. } => {
                let n = go_value_ident(&name.name);
                let _ = write!(out, "{n} := {access}; _ = {n}; ");
            }
            NodeKind::ConstructorPat { path, fields } => {
                let leaf = path.segments.last().map_or("", |s| s.name.as_str());
                if matches!(leaf, "Some" | "None" | "Ok" | "Err") {
                    if let Some(f) = fields.first() {
                        // Peel one declared-type layer for this tag so a nested
                        // tuple payload is asserted to its concrete struct before
                        // its `.Field0`/`.Field1` reads (mirrors `pattern_test_go`).
                        let child_ty = self.peel_constructor_decl_ty(leaf, decl_ty);
                        // A *leaf* payload bind (`Some(n)`, `Ok(v)`) with a known
                        // concrete element type asserts the boxed `interface{}`
                        // payload to that type, so typed use inside a guard or
                        // arm body (`n > 0`) type-checks — the same payload typing
                        // the guard-let binder and the tag-switch arms apply. A
                        // nested constructor/tuple/record payload re-asserts via
                        // `constructor_child_access_go` and recurses.
                        if let NodeKind::BindPat { name, .. } = &f.kind {
                            let n = go_value_ident(&name.name);
                            if n != "_" {
                                let elem = child_ty.as_deref().map(|t| self.type_to_go(t));
                                let rhs = self.payload_access_go(access, elem.as_deref());
                                let _ = write!(out, "{n} := {rhs}; _ = {n}; ");
                                return;
                            }
                        }
                        let child =
                            self.constructor_child_access_go(f, access, child_ty.as_deref());
                        self.collect_binds_go(f, &child, child_ty.as_deref(), out);
                    }
                } else {
                    // User-enum variant: bind payload fields off the asserted
                    // concrete struct.
                    let variant_ty = self.go_variant_struct(path);
                    for (i, f) in fields.iter().enumerate() {
                        let child = format!("{access}.({variant_ty}).Field{i}");
                        self.collect_binds_go(f, &child, None, out);
                    }
                }
            }
            NodeKind::RecordPat { path, fields, .. } => {
                // A registered enum *variant* record (`Shape::Rect { w, h }`) is a
                // sealed-interface value, so its fields are read off a concrete
                // type assertion (`access.(ShapeRect).W`). A *plain* record
                // (`Point { x, y }`) is already a concrete struct — asserting
                // `access.(Point)` is invalid Go ("p is not an interface"), so its
                // fields are read directly (`access.X`). Mirrors the
                // `user_variant_for_path` gate in `pattern_test_go`.
                let base = if self.user_variant_for_path(path).is_some() {
                    let variant_ty = self.go_variant_struct(path);
                    format!("{access}.({variant_ty})")
                } else {
                    access.to_string()
                };
                for f in fields {
                    let go_field = to_pascal_case(&f.name.name);
                    let child = format!("{base}.{go_field}");
                    match &f.pattern {
                        Some(p) => self.collect_binds_go(p, &child, None, out),
                        None => {
                            let n = to_camel_case(&f.name.name);
                            let _ = write!(out, "{n} := {child}; _ = {n}; ");
                        }
                    }
                }
            }
            NodeKind::TuplePat { elems } => {
                let field_tys = self.tuple_field_decl_tys(decl_ty, elems.len());
                for (i, e) in elems.iter().enumerate() {
                    self.collect_binds_go(
                        e,
                        &format!("{access}.Field{i}"),
                        field_tys.get(i).and_then(|t| *t),
                        out,
                    );
                }
            }
            NodeKind::ListPat { elems, rest } => {
                for (i, e) in elems.iter().enumerate() {
                    self.collect_binds_go(e, &format!("{access}[{i}]"), None, out);
                }
                // `..rest` binds the remaining elements as a tail slice
                // (`rest := access[n:]`); a bare `..` (RestPat) or absent rest
                // binds nothing. Mirrors `pattern_binds_js`.
                if let Some(r) = rest {
                    if let NodeKind::BindPat { name, .. } = &r.kind {
                        let nm = go_value_ident(&name.name);
                        let _ = write!(out, "{nm} := {access}[{}:]; _ = {nm}; ", elems.len());
                    }
                }
            }
            NodeKind::OrPat { alternatives } => {
                if let Some(first) = alternatives.first() {
                    self.collect_binds_go(first, access, decl_ty, out);
                }
            }
            _ => {}
        }
    }

    /// The Go struct type name for a user-enum variant path (`ShapeRect`), or the
    /// joined path as a fallback.
    fn go_variant_struct(&self, path: &bock_ast::TypePath) -> String {
        if let Some(info) = self.user_variant_for_path(path) {
            let variant = path.segments.last().map_or("", |s| s.name.as_str());
            format!("{}{variant}", info.enum_name)
        } else {
            path.segments
                .iter()
                .map(|s| s.name.as_str())
                .collect::<Vec<_>>()
                .join("")
        }
    }

    fn emit_match_arm(
        &mut self,
        arm: &AIRNode,
        user_enum: bool,
        value_switch_binds: bool,
    ) -> Result<(), CodegenError> {
        if let NodeKind::MatchArm {
            pattern,
            guard,
            body,
        } = &arm.kind
        {
            let ind = self.indent_str();
            match &pattern.kind {
                NodeKind::WildcardPat | NodeKind::BindPat { .. } => {
                    let _ = write!(self.buf, "{ind}default:");
                }
                _ => {
                    let _ = write!(self.buf, "{ind}case ");
                    self.emit_match_case_condition(pattern)?;
                    self.buf.push(':');
                }
            }
            self.buf.push('\n');
            self.indent += 1;
            // For a user enum type-switch, bind the arm's payload fields from
            // the concrete `__v` (`radius := __v.Radius`, `w := __v.Field0`).
            if user_enum {
                self.emit_user_enum_arm_bindings(pattern)?;
            }
            // Value-switch bind arm (`x => …`): alias the named scrutinee `__v`
            // so the body's references resolve (the Go binding-drop fix).
            if value_switch_binds {
                if let NodeKind::BindPat { name, .. } = &pattern.kind {
                    let n = go_value_ident(&name.name);
                    self.writeln(&format!("{n} := __v; _ = {n}"));
                }
            }
            if let Some(g) = guard {
                let gi = self.indent_str();
                let _ = write!(self.buf, "{gi}if ");
                self.emit_expr(g)?;
                self.buf.push_str(" {\n");
                self.indent += 1;
                self.emit_block_body(body)?;
                self.indent -= 1;
                self.writeln("}");
            } else {
                self.emit_block_body(body)?;
            }
        }
        Ok(())
    }

    /// Bind a user-enum arm's payload fields from the type-switched `__v`.
    ///
    /// Inside a Go type-switch case `case ShapeCircle:`, `__v` has the concrete
    /// variant-struct type, so each bound field reads directly off it:
    /// - struct variant (`Circle { radius }`): `radius := __v.Radius`
    ///   (the struct field is `to_pascal_case` of the Bock field name).
    /// - tuple variant (`Rect(w, h)`): `w := __v.Field0; h := __v.Field1`.
    /// - unit variant: nothing to bind.
    ///
    /// Each binding is followed by `_ = name` so an arm that does not use every
    /// payload field still compiles (Go errors on an unused local).
    fn emit_user_enum_arm_bindings(&mut self, pattern: &AIRNode) -> Result<(), CodegenError> {
        match &pattern.kind {
            NodeKind::ConstructorPat { fields, .. } => {
                for (i, field) in fields.iter().enumerate() {
                    let name = self.pattern_to_binding_name(field);
                    if name == "_" {
                        continue;
                    }
                    self.writeln(&format!("{name} := __v.Field{i}; _ = {name}"));
                }
            }
            NodeKind::RecordPat { fields, .. } => {
                for f in fields {
                    let go_field = to_pascal_case(&f.name.name);
                    let bind = match &f.pattern {
                        Some(p) => self.pattern_to_binding_name(p),
                        // Shorthand `{ radius }` binds a variable named `radius`.
                        None => to_camel_case(&f.name.name),
                    };
                    if bind == "_" {
                        continue;
                    }
                    self.writeln(&format!("{bind} := __v.{go_field}; _ = {bind}"));
                }
            }
            _ => {}
        }
        Ok(())
    }

    fn emit_match_case_condition(&mut self, pat: &AIRNode) -> Result<(), CodegenError> {
        match &pat.kind {
            NodeKind::WildcardPat => {
                self.buf.push('_');
            }
            NodeKind::BindPat { name, .. } => {
                let _ = name;
                self.buf.push_str("interface{}");
            }
            NodeKind::LiteralPat { lit } => match lit {
                Literal::Int(s) => self.buf.push_str(s),
                Literal::Float(s) => self.buf.push_str(s),
                Literal::Bool(b) => self.buf.push_str(if *b { "true" } else { "false" }),
                Literal::Char(s) => {
                    self.buf.push('\'');
                    self.buf.push_str(s);
                    self.buf.push('\'');
                }
                Literal::String(s) => {
                    self.buf.push('"');
                    self.buf.push_str(&escape_go_string(s));
                    self.buf.push('"');
                }
                Literal::Unit => self.buf.push_str("nil"),
            },
            NodeKind::ConstructorPat { path, .. } => {
                // A user enum variant is a `{enum}{variant}` struct type
                // (`ShapeRect`); fall back to the joined path otherwise. For a
                // generic enum the variant struct carries the enum's type params,
                // so a `case BoxFull[int64]:` spells the concrete instantiation
                // (`current_match_enum_type_args`, empty for a non-generic enum).
                let variant_name = if let Some(info) = self.user_variant_for_path(path) {
                    let variant = path.segments.last().map_or("", |s| s.name.as_str());
                    format!(
                        "{}{variant}{}",
                        info.enum_name, self.current_match_enum_type_args
                    )
                } else {
                    path.segments
                        .iter()
                        .map(|s| s.name.as_str())
                        .collect::<Vec<_>>()
                        .join("")
                };
                self.buf.push_str(&variant_name);
            }
            NodeKind::RecordPat { path, .. } => {
                let type_name = if let Some(info) = self.user_variant_for_path(path) {
                    let variant = path.segments.last().map_or("", |s| s.name.as_str());
                    format!(
                        "{}{variant}{}",
                        info.enum_name, self.current_match_enum_type_args
                    )
                } else {
                    path.segments
                        .iter()
                        .map(|s| s.name.as_str())
                        .collect::<Vec<_>>()
                        .join(".")
                };
                self.buf.push_str(&type_name);
            }
            NodeKind::TuplePat { .. } => {
                self.buf.push_str("interface{}");
            }
            _ => {
                self.buf.push_str("interface{}");
            }
        }
        Ok(())
    }

    // ── Pipe operator ───────────────────────────────────────────────────────

    fn emit_pipe(&mut self, left: &AIRNode, right: &AIRNode) -> Result<(), CodegenError> {
        if let NodeKind::Call { callee, args, .. } = &right.kind {
            let has_placeholder = args
                .iter()
                .any(|a| matches!(a.value.kind, NodeKind::Placeholder));
            if has_placeholder {
                self.emit_expr(callee)?;
                self.buf.push('(');
                for (i, arg) in args.iter().enumerate() {
                    if i > 0 {
                        self.buf.push_str(", ");
                    }
                    if matches!(arg.value.kind, NodeKind::Placeholder) {
                        self.emit_expr(left)?;
                    } else {
                        self.emit_expr(&arg.value)?;
                    }
                }
                self.buf.push(')');
                return Ok(());
            }
        }
        self.emit_expr(right)?;
        self.buf.push('(');
        self.emit_expr(left)?;
        self.buf.push(')');
        Ok(())
    }

    // ── Type emission ───────────────────────────────────────────────────────

    /// If `name` is one of the three collection types, emit the concrete Go
    /// container type with its element/key/value types recovered from `args`
    /// (each mapped to Go via `arg_to_go`), rather than the `interface{}`-erased
    /// `map_type_name` fallback:
    /// - `List[T]`  → `[]T`
    /// - `Set[T]`   → `map[T]struct{}`
    /// - `Map[K,V]` → `map[K]V`
    ///
    /// A missing arg defaults to `interface{}` (e.g. a bare `List` with no type
    /// argument), preserving the prior erased behavior for the untyped case.
    /// Returns `None` for any non-collection type so callers fall through to the
    /// `Optional`/`Result` runtime-struct and generic-struct paths unchanged.
    fn collection_type_to_go<T>(
        &self,
        name: &str,
        args: &[T],
        arg_to_go: impl Fn(&T) -> String,
    ) -> Option<String> {
        let elem = |i: usize| {
            args.get(i)
                .map_or_else(|| "interface{}".to_string(), &arg_to_go)
        };
        match name {
            "List" => Some(format!("[]{}", elem(0))),
            "Set" => Some(format!("map[{}]struct{{}}", elem(0))),
            "Map" => Some(format!("map[{}]{}", elem(0), elem(1))),
            _ => None,
        }
    }

    fn type_to_go(&self, node: &AIRNode) -> String {
        match &node.kind {
            NodeKind::TypeNamed { path, args } => {
                // A non-generic `type X = …` alias renders as its underlying Go
                // type (`type ParseResult = Result[...]` → `__bockResult`), so a
                // value of the alias type is the runtime container a `match`
                // dispatches on. Resolved before the collection/runtime mapping so
                // an alias to `List[T]`/`Result`/`Optional` lowers correctly.
                if let Some(target) = self.resolve_type_alias(node) {
                    return self.type_to_go(target);
                }
                let name = path
                    .segments
                    .iter()
                    .map(|s| s.name.as_str())
                    .collect::<Vec<_>>()
                    .join(".");
                // The three collection types are NOT erased to an `interface{}`
                // element: a declared `List[Int]` must emit `[]int64` (not
                // `[]interface{}`) so element arithmetic / iteration / typed
                // returns compile. Emit the concrete Go container recursively
                // over the type args, BEFORE the `map_type_name`
                // `is_mapped_runtime` fallback (which would erase them).
                if let Some(collection) =
                    self.collection_type_to_go(&name, args, |a| self.type_to_go(a))
                {
                    return collection;
                }
                let go_name = self.map_type_name(&name);
                // Runtime container types (`__bockOption`, `__bockResult`) carry
                // their payload as `interface{}`, not as a Go generic parameter;
                // never append `[T]` to such a mapped runtime type.
                let is_mapped_runtime = go_name != name;
                if args.is_empty() || is_mapped_runtime {
                    go_name
                } else {
                    let arg_strs: Vec<String> = args.iter().map(|a| self.type_to_go(a)).collect();
                    format!("{go_name}[{}]", arg_strs.join(", "))
                }
            }
            NodeKind::TypeTuple { elems } => {
                // Go doesn't have tuples; emit as struct with numbered fields.
                if elems.is_empty() {
                    "struct{}".into()
                } else {
                    let fields: Vec<String> = elems
                        .iter()
                        .enumerate()
                        .map(|(i, e)| format!("Field{i} {}", self.type_to_go(e)))
                        .collect();
                    format!("struct{{ {} }}", fields.join("; "))
                }
            }
            NodeKind::TypeFunction { params, ret, .. } => {
                let param_strs: Vec<String> = params.iter().map(|p| self.type_to_go(p)).collect();
                // A `Fn(...) -> Void` lowers to a Go `func(...)` with NO result
                // type. Rendering the Void return as `struct{}` (its value type)
                // would make `func() struct{}`, which is a function that must
                // `return struct{}{}` — but a Void-returning closure body emits
                // no return, so the signatures would not match. Drop the result.
                if Self::is_void_type(ret) {
                    format!("func({})", param_strs.join(", "))
                } else {
                    format!("func({}) {}", param_strs.join(", "), self.type_to_go(ret))
                }
            }
            NodeKind::TypeOptional { inner } => {
                // `T?` lowers to the tagged Optional runtime struct, not a Go
                // pointer — pointers can\'t represent `Some(nil-able-T)` vs
                // `None`, and the match dispatches on the tag.
                let _ = inner;
                "__bockOption".to_string()
            }
            NodeKind::TypeSelf => self
                .go_self_subst
                .clone()
                .unwrap_or_else(|| "/* Self */".into()),
            _ => "interface{}".into(),
        }
    }

    fn map_type_name(&self, name: &str) -> String {
        match name {
            "Int" => "int64".into(),
            "Float" => "float64".into(),
            "Bool" => "bool".into(),
            "String" => "string".into(),
            // Bock `Char` is a Unicode scalar; Go's `rune` (`int32`). A char
            // literal `'A'` already emits a Go rune literal (`go_literal`), so a
            // `let c: Char` annotation must render `rune`, not the undefined
            // identifier `Char`.
            "Char" => "rune".into(),
            "Void" | "Unit" => "struct{}".into(),
            "List" => "[]interface{}".into(),
            "Map" => "map[string]interface{}".into(),
            "Set" => "map[interface{}]struct{}".into(),
            "Any" => "interface{}".into(),
            "Never" => "interface{}".into(),
            "Channel" => "*__bockChannel".into(),
            "Optional" => "__bockOption".into(),
            // `Result[T, E]` lowers to the tagged Result-runtime struct (the
            // `[T, E]` args are dropped — `is_mapped_runtime` in the callers
            // suppresses the generic suffix), mirroring `Optional`.
            "Result" => "__bockResult".into(),
            // §18.3.1 builtin time types: a `Duration` value lowers to a
            // signed-nanosecond `int64`, and an `Instant` to `time.Time`
            // (`time.Now()`). They are NOT user-defined types, so as annotations
            // (e.g. on a `Clock` handler's `now_monotonic() -> Instant` /
            // `sleep(duration: Duration)`) they must render their concrete Go
            // forms, not the undefined identifiers. (The `time` import is driven
            // by the value sites — `time.Now()` / `time.Since(...)` — that any
            // `Instant`-typed program also exercises.)
            "Duration" => "int64".into(),
            "Instant" => "time.Time".into(),
            // The prelude `Ordering` enum: when the real `core.compare.Ordering`
            // is NOT reachable (no `use core.compare`), its variants lower to the
            // `__bockOrdering` value runtime, so a `-> Ordering` annotation must
            // render `__bockOrdering` to agree — the bare `Ordering` is an
            // undefined Go type. When the enum IS reachable the user type is in
            // scope and keeps its name. (Q-prelude-impl-missing-import.)
            "Ordering" if !self.ordering_enum_reachable() => "__bockOrdering".into(),
            other => other.into(),
        }
    }

    fn ast_type_to_go(&self, ty: &TypeExpr) -> String {
        match ty {
            TypeExpr::Named { path, args, .. } => {
                let name = path
                    .segments
                    .iter()
                    .map(|s| s.name.as_str())
                    .collect::<Vec<_>>()
                    .join(".");
                // See `type_to_go`: emit concrete `[]T` / `map[K]V` /
                // `map[T]struct{}` for the three collection types rather than
                // erasing their element type to `interface{}`.
                if let Some(collection) =
                    self.collection_type_to_go(&name, args, |a| self.ast_type_to_go(a))
                {
                    return collection;
                }
                let go_name = self.map_type_name(&name);
                let is_mapped_runtime = go_name != name;
                if args.is_empty() || is_mapped_runtime {
                    go_name
                } else {
                    let arg_strs: Vec<String> =
                        args.iter().map(|a| self.ast_type_to_go(a)).collect();
                    format!("{go_name}[{}]", arg_strs.join(", "))
                }
            }
            TypeExpr::Tuple { elems, .. } => {
                if elems.is_empty() {
                    "struct{}".into()
                } else {
                    let fields: Vec<String> = elems
                        .iter()
                        .enumerate()
                        .map(|(i, e)| format!("Field{i} {}", self.ast_type_to_go(e)))
                        .collect();
                    format!("struct{{ {} }}", fields.join("; "))
                }
            }
            TypeExpr::Function { params, ret, .. } => {
                let param_strs: Vec<String> =
                    params.iter().map(|p| self.ast_type_to_go(p)).collect();
                // `Fn(...) -> Void` → Go `func(...)` (no result type). See the
                // AIR `TypeFunction` arm in `type_to_go` for the rationale.
                if Self::ast_type_is_void(ret) {
                    format!("func({})", param_strs.join(", "))
                } else {
                    format!(
                        "func({}) {}",
                        param_strs.join(", "),
                        self.ast_type_to_go(ret)
                    )
                }
            }
            TypeExpr::Optional { inner, .. } => {
                let _ = inner;
                "__bockOption".to_string()
            }
            TypeExpr::SelfType { .. } => "/* Self */".into(),
        }
    }

    // ── Helpers ─────────────────────────────────────────────────────────────

    fn emit_block_body(&mut self, node: &AIRNode) -> Result<(), CodegenError> {
        self.emit_block_body_inner(node, false)
    }

    /// Emit a `@test` function body (S7), lowering `expect(...)` assertion chains
    /// to Go `if <neg> { t.Errorf(...) }` guards and falling back to the normal
    /// statement emitter for any other statement. Sets `needs_reflect` when an
    /// equality assertion (`reflect.DeepEqual`) is emitted.
    fn emit_go_test_body(&mut self, body: &AIRNode) -> Result<(), CodegenError> {
        let stmts_and_tail: Vec<&AIRNode> = match &body.kind {
            NodeKind::Block { stmts, tail } => stmts.iter().chain(tail.as_deref()).collect(),
            _ => vec![body],
        };
        for stmt in stmts_and_tail {
            if let Some((assertion, actual, expected)) = crate::generator::classify_assertion(stmt)
            {
                let a = self.expr_to_string(actual)?;
                use crate::generator::TestAssertion as T;
                match assertion {
                    T::Equal => {
                        // Go `==` follows the constant-conversion rules (an
                        // untyped literal `3` compares equal to an `int64`), so it
                        // handles ints/floats/strings/bools and comparable structs
                        // (the `__bockOption`/`__bockResult` value runtimes) without
                        // the type-mismatch `reflect.DeepEqual(int64, int)` pitfall.
                        // Slice/map equality (uncommon in `@test`) would need
                        // `reflect.DeepEqual`; that is a known follow-up.
                        let e = match expected {
                            Some(e) => self.expr_to_string(e)?,
                            None => "nil".to_string(),
                        };
                        self.writeln(&format!("if ({a}) != ({e}) {{"));
                        self.indent += 1;
                        self.writeln(&format!("t.Errorf(\"expected %v, got %v\", {e}, {a})"));
                        self.indent -= 1;
                        self.writeln("}");
                    }
                    T::BeTrue => {
                        self.writeln(&format!("if !({a}) {{"));
                        self.indent += 1;
                        self.writeln("t.Errorf(\"expected true, got false\")");
                        self.indent -= 1;
                        self.writeln("}");
                    }
                    T::BeFalse => {
                        self.writeln(&format!("if {a} {{"));
                        self.indent += 1;
                        self.writeln("t.Errorf(\"expected false, got true\")");
                        self.indent -= 1;
                        self.writeln("}");
                    }
                    T::BeSome | T::BeNone | T::BeOk | T::BeErr => {
                        let tag = match assertion {
                            T::BeSome => "Some",
                            T::BeNone => "None",
                            T::BeOk => "Ok",
                            _ => "Err",
                        };
                        self.writeln(&format!("if ({a}).tag != \"{tag}\" {{"));
                        self.indent += 1;
                        self.writeln(&format!("t.Errorf(\"expected {tag}, got %v\", ({a}).tag)"));
                        self.indent -= 1;
                        self.writeln("}");
                    }
                }
            } else {
                self.emit_node(stmt)?;
            }
        }
        Ok(())
    }

    fn emit_block_body_return(&mut self, node: &AIRNode) -> Result<(), CodegenError> {
        self.emit_block_body_inner(node, true)
    }

    fn emit_block_body_inner(
        &mut self,
        node: &AIRNode,
        emit_return: bool,
    ) -> Result<(), CodegenError> {
        // Open a fresh Go block scope for shadowing-`let` tracking. The frame is
        // pre-seeded with any pending parameter names (function/method entry), so
        // a `let` shadowing a parameter — which is the *same* Go scope, the body
        // gets no extra brace — reassigns rather than re-declares. Popped on exit
        // regardless of which return path the body takes.
        let mut frame = HashSet::new();
        if let Some(seed) = self.pending_scope_seed.take() {
            frame.extend(seed);
        }
        self.go_declared_scopes.push(frame);
        let result = self.emit_block_body_inner_scoped(node, emit_return);
        self.go_declared_scopes.pop();
        result
    }

    /// Whether `name` is already declared in the innermost open Go block scope.
    /// A shadowing `let` of such a name must lower to a plain assignment (`=`),
    /// not a fresh `:=` / `var` declaration (which Go rejects as "no new
    /// variables on left side of :=").
    fn go_name_declared_in_block(&self, name: &str) -> bool {
        self.go_declared_scopes
            .last()
            .is_some_and(|frame| frame.contains(name))
    }

    /// Record `name` as declared in the innermost open Go block scope.
    fn go_record_declared(&mut self, name: &str) {
        if let Some(frame) = self.go_declared_scopes.last_mut() {
            frame.insert(name.to_string());
        }
    }

    fn emit_block_body_inner_scoped(
        &mut self,
        node: &AIRNode,
        emit_return: bool,
    ) -> Result<(), CodegenError> {
        if let NodeKind::Block { stmts, tail } = &node.kind {
            if stmts.is_empty() && tail.is_none() {
                self.writeln("// empty");
                return Ok(());
            }
            // Type the declare-only temps this block introduces (value-CF hoist).
            self.seed_decl_only_types(stmts);
            for (i, s) in stmts.iter().enumerate() {
                self.emit_node(s)?;
                // Go rejects a `let`-bound local never read (`declared and not
                // used`); Bock permits it (a binding kept for its call's side
                // effect, e.g. context-audit's `let payment = process(...)`). When
                // a simple `let x = …` binding's name is referenced nowhere in the
                // rest of this block, emit `_ = x` to satisfy Go without dropping
                // the side-effecting initializer. Restricted to a plain `BindPat`
                // (not `_`, tuple/record patterns, or a `:=`-from-`if let` form):
                // the conservative reference scan over the *remaining* siblings and
                // tail never silences a name that is actually used.
                if let NodeKind::LetBinding { pattern, .. } = &s.kind {
                    if let NodeKind::BindPat { name, .. } = &pattern.kind {
                        let go_name = go_value_ident(&name.name);
                        if go_name != "_" {
                            let used_after = stmts[i + 1..]
                                .iter()
                                .chain(tail.as_deref())
                                .any(|n| collect_used_idents(n).contains(&name.name));
                            if !used_after {
                                self.writeln(&format!("_ = {go_name}"));
                            }
                        }
                    }
                }
            }
            if let Some(t) = tail {
                // A statement tail (`return`/`break`/`continue`/assignment) is
                // emitted as a statement, never via `emit_expr` (which would
                // fall through to `/* unsupported */` for control-flow nodes).
                if crate::generator::node_is_statement(t) {
                    self.emit_node(t)?;
                    return Ok(());
                }
                // DQ18: an in-place `List` mutator (`push`/`append`) in *tail*
                // position (a single-statement loop body `{ acc.push(x) }`) is a
                // `Void` call, so it carries no value to return/emit — lower it to
                // Go's slice-growth assignment statement, not the value-less call
                // form `emit_expr` would otherwise emit.
                if let NodeKind::Call { callee, args, .. } = &t.kind {
                    if self.try_emit_list_mutating_stmt(t, callee, args)? {
                        return Ok(());
                    }
                    // DQ30: the `Void` in-place mutators (`insert`/`reverse`/
                    // `set`) lower to valueless func-literal calls — valid Go
                    // *statements*, but not values, so a tail must emit them as
                    // a plain statement, never behind a `return`.
                    if let Some((_, m, _)) =
                        crate::generator::desugared_list_inplace_mutator(t, callee, args)
                    {
                        if matches!(m, "insert" | "reverse" | "set") {
                            self.write_indent();
                            self.emit_expr(t)?;
                            self.buf.push('\n');
                            return Ok(());
                        }
                    }
                }
                // A `match` whose value is not consumed must emit in statement
                // position (a Go `switch` / if-chain), never as an expression
                // IIFE. Two cases reach here:
                //
                //   1. A `match` with statement arms (`break`/`return`/an
                //      assignment) carries no value at all.
                //   2. A `match` in a *non-returning* tail position
                //      (`emit_return == false`: a `Void`/`main` body tail, an
                //      `if`/loop/arm statement block) — its result is discarded,
                //      so even value-shaped arms (a `print(...)`/void-call arm,
                //      classified as an expression, not a statement) yield
                //      nothing the caller reads.
                //
                // Routing both to `emit_match` avoids the
                // `func() interface{} { …arm…; panic("unreachable") }()` IIFE
                // form, whose trailing exhaustiveness guard runs *after* the
                // matched arm — producing correct output and THEN a spurious
                // `unreachable` panic (Q-go-tailmatch-unreachable-panic). The
                // statement-position lowering has no such guard for a total
                // record/plain match, so the arm is the terminal statement.
                if let NodeKind::Match { scrutinee, arms } = &t.kind {
                    if !emit_return || crate::generator::match_has_statement_arm(arms) {
                        self.emit_match(scrutinee, arms)?;
                        return Ok(());
                    }
                }
                let should_return = emit_return && !self.is_void_call(t);
                // A collection literal in *tail-return* position adopts the
                // function's return collection element type(s), mirroring the
                // explicit-`return` arm, so `fn single[T](x: T) -> List[T] { [x]
                // }` emits `[]T{x}` rather than `[]interface{}{x}`.
                let prev_expected = self.expected_collection_elem.take();
                if should_return
                    && matches!(
                        t.kind,
                        NodeKind::ListLiteral { .. }
                            | NodeKind::MapLiteral { .. }
                            | NodeKind::SetLiteral { .. }
                    )
                {
                    self.expected_collection_elem = self.current_fn_ret_collection_elem.clone();
                }
                // A generic-record construction in tail-return position adopts
                // the function's rendered return type as its expected type, so
                // `fn list_iter[T](xs: List[T]) -> ListIterator[T] {
                // ListIterator { xs: xs, cursor: 0 } }` emits `ListIterator[T]{
                // ... }` rather than the field-inference `[any]` fallback (Go
                // requires explicit type args on a generic struct literal).
                let prev_expected_type = self.current_expected_type.take();
                if should_return
                    && (matches!(
                        t.kind,
                        NodeKind::RecordConstruct { .. } | NodeKind::TupleLiteral { .. }
                    ) || Self::is_expr_optional_or_result_match(t))
                {
                    self.current_expected_type = self.current_fn_ret_type.clone();
                }
                // A lambda returned directly (`-> Fn(Int) -> Int { (x) => … }`)
                // takes its param/return types from the declared function type.
                let saved_lambda_hints = if should_return {
                    self.pin_return_lambda_types(t)
                } else {
                    (
                        self.expected_lambda_param_types.clone(),
                        self.forced_lambda_ret.clone(),
                    )
                };
                if should_return {
                    let ind = self.indent_str();
                    let _ = write!(self.buf, "{ind}return ");
                    self.emit_expr(t)?;
                    self.buf.push('\n');
                } else {
                    self.write_indent();
                    self.emit_expr(t)?;
                    self.buf.push('\n');
                }
                self.expected_lambda_param_types = saved_lambda_hints.0;
                self.forced_lambda_ret = saved_lambda_hints.1;
                self.expected_collection_elem = prev_expected;
                self.current_expected_type = prev_expected_type;
            }
        } else if crate::generator::node_is_statement(node) {
            // A bare statement body (`break`/`continue`/`return`/assignment):
            // emit through the statement path, never as an expression.
            self.emit_node(node)?;
        } else {
            // Single expression as body. A `match` whose value is not consumed
            // (statement arms, or any match in a non-returning `emit_return ==
            // false` body) emits in statement position, never as an IIFE — see
            // the block-tail arm above (Q-go-tailmatch-unreachable-panic).
            if let NodeKind::Match { scrutinee, arms } = &node.kind {
                if !emit_return || crate::generator::match_has_statement_arm(arms) {
                    self.emit_match(scrutinee, arms)?;
                    return Ok(());
                }
            }
            let should_return = emit_return && !self.is_void_call(node);
            let prev_expected = self.expected_collection_elem.take();
            if should_return
                && matches!(
                    node.kind,
                    NodeKind::ListLiteral { .. }
                        | NodeKind::MapLiteral { .. }
                        | NodeKind::SetLiteral { .. }
                )
            {
                self.expected_collection_elem = self.current_fn_ret_collection_elem.clone();
            }
            // See the block-tail arm: a generic-record construction — or an
            // expression-position `Optional`/`Result` match — as the sole body
            // expression adopts the function's return type, so its IIFE result
            // is assignable to the declared return type.
            let prev_expected_type = self.current_expected_type.take();
            if should_return
                && (matches!(
                    node.kind,
                    NodeKind::RecordConstruct { .. } | NodeKind::TupleLiteral { .. }
                ) || Self::is_expr_optional_or_result_match(node))
            {
                self.current_expected_type = self.current_fn_ret_type.clone();
            }
            // A lambda body returned directly (`-> Fn(Int) -> Int { (x) => … }`,
            // or the single-expression form `compose(...) { (x) => f(g(x)) }`)
            // takes its param/return types from the declared function type.
            let saved_lambda_hints = if should_return {
                self.pin_return_lambda_types(node)
            } else {
                (
                    self.expected_lambda_param_types.clone(),
                    self.forced_lambda_ret.clone(),
                )
            };
            if should_return {
                let ind = self.indent_str();
                let _ = write!(self.buf, "{ind}return ");
                self.emit_expr(node)?;
                self.buf.push('\n');
            } else {
                self.write_indent();
                self.emit_expr(node)?;
                self.buf.push('\n');
            }
            self.expected_lambda_param_types = saved_lambda_hints.0;
            self.forced_lambda_ret = saved_lambda_hints.1;
            self.expected_collection_elem = prev_expected;
            self.current_expected_type = prev_expected_type;
        }
        Ok(())
    }

    /// Whether a statement-position tail node *unconditionally terminates* its
    /// enclosing block — a `return`/`break`/`continue`. Used by the block
    /// expression-IIFE to decide whether a trailing fallback
    /// (`return nil` / `panic("unreachable")`) is reachable: when the tail
    /// terminates, the fallback would be dead code after a `return`. An
    /// assignment tail (the other statement-tail shape) does not terminate, so
    /// the fallback is still needed there.
    fn tail_terminates(node: &AIRNode) -> bool {
        matches!(
            node.kind,
            NodeKind::Return { .. } | NodeKind::Break { .. } | NodeKind::Continue
        )
    }

    /// Emit an `if`/`match` arm body as the value of its enclosing expression
    /// IIFE — i.e. as a `return <expr>`, but with a void-call tail handled
    /// correctly. A `println(...)` / void effect-op call returns `(int, error)`
    /// (Go's `fmt.Println`) or nothing, so `return println(...)` is a Go arity
    /// error (`too many return values have (int, error) want T`). When the body's
    /// effective tail is a void call, emit the call as a *statement* followed by
    /// `return <zero>` (the IIFE result is discarded — these arise only when the
    /// whole match/if is in statement position). Otherwise emit the normal
    /// `return <body>`. `iife_ty` is the enclosing IIFE's return type; a non-`None`
    /// value uses a typed zero, `None` uses `nil`.
    fn emit_arm_body_return(
        &mut self,
        body: &AIRNode,
        iife_ty: Option<&str>,
    ) -> Result<(), CodegenError> {
        // The effective tail is the block tail (when the body is a `{ ... }`
        // block) or the body itself (a bare expression arm).
        let tail = match &body.kind {
            NodeKind::Block { tail: Some(t), .. } => t.as_ref(),
            NodeKind::Block { tail: None, .. } => body, // emitted below as-is
            _ => body,
        };
        if self.is_void_call(tail) {
            // Emit any leading block statements, then the void call as a
            // statement, then a discarded zero return.
            if let NodeKind::Block { stmts, .. } = &body.kind {
                for s in stmts {
                    self.emit_node(s)?;
                    self.buf.push_str("; ");
                }
            }
            self.emit_expr(tail)?;
            self.buf.push_str("; return ");
            match iife_ty {
                Some(ty) => self.zero_value_for(ty),
                None => self.buf.push_str("nil"),
            }
            return Ok(());
        }
        self.buf.push_str("return ");
        self.emit_block_as_expr(body)
    }

    /// Returns `true` if the expression is a call to a known void function
    /// (prelude or a Void-returning effect operation).
    fn is_void_call(&self, node: &AIRNode) -> bool {
        if let NodeKind::Call { callee, .. } = &node.kind {
            if let NodeKind::Identifier { name } = &callee.kind {
                if matches!(
                    name.name.as_str(),
                    "println" | "print" | "debug" | "assert" | "todo" | "unreachable"
                ) {
                    return true;
                }
                if self.void_effect_ops.contains(&name.name) {
                    return true;
                }
            }
        }
        false
    }

    fn emit_block_as_expr(&mut self, node: &AIRNode) -> Result<(), CodegenError> {
        if let NodeKind::Block { stmts, tail } = &node.kind {
            if stmts.is_empty() {
                if let Some(t) = tail {
                    return self.emit_expr(t);
                }
            }
        }
        self.emit_expr(node)
    }

    fn pattern_to_binding_name(&self, pat: &AIRNode) -> String {
        match &pat.kind {
            // A bound value name, keyword-escaped. This is the single Go
            // value-binding funnel: params, `let` bindings, and the
            // scope-inference map keys all derive from it, so the escape lands
            // identically everywhere and never strips back to a bare keyword (the
            // outer callers no longer re-run `to_camel_case`, which would drop a
            // trailing escape `_`).
            NodeKind::BindPat { name, .. } => go_value_ident(&name.name),
            NodeKind::WildcardPat => "_".into(),
            NodeKind::TuplePat { elems } => {
                // Go doesn't have tuple destructuring; use first element.
                elems
                    .first()
                    .map(|e| self.pattern_to_binding_name(e))
                    .unwrap_or_else(|| "_".into())
            }
            NodeKind::RecordPat { fields, .. } => fields
                .first()
                .map(|f| to_camel_case(&f.name.name))
                .unwrap_or_else(|| "_".into()),
            _ => "_".into(),
        }
    }

    fn pattern_to_go_binding(&self, pat: &AIRNode) -> String {
        self.pattern_to_binding_name(pat)
    }

    fn type_expr_to_string(&self, node: &AIRNode) -> String {
        match &node.kind {
            NodeKind::TypeNamed { path, .. } => path
                .segments
                .iter()
                .map(|s| s.name.as_str())
                .collect::<Vec<_>>()
                .join("."),
            NodeKind::Identifier { name } => name.name.clone(),
            _ => "Unknown".into(),
        }
    }
}

// ─── Utility functions ───────────────────────────────────────────────────────

/// Map a Bock `@test` function name to a valid Go test function name
/// (`TestXxx`), as `go test` requires (S7).
///
/// A leading `test`/`test_` is stripped so the conventional `test_add` does not
/// become the stuttering `TestTestAdd`; the remainder is PascalCased and
/// `Test`-prefixed. A name that is *only* `test` (no suffix) keeps a stable
/// disambiguating form (`TestTest` → `Test_` is invalid, so it becomes
/// `TestCase`). Empty input falls back to `TestCase`.
fn go_test_fn_name(bock_name: &str) -> String {
    let stripped = bock_name
        .strip_prefix("test_")
        .or_else(|| bock_name.strip_prefix("test"))
        .unwrap_or(bock_name);
    let pascal = to_pascal_case(stripped);
    if pascal.is_empty() {
        "TestCase".to_string()
    } else {
        format!("Test{pascal}")
    }
}

/// True for Bock's built-in Optional/Result constructors, which must be
/// emitted verbatim (PascalCase preserved) so generated Go code can match
/// the runtime prelude's `Some`/`None`/`Ok`/`Err` types.
fn is_prelude_ctor(s: &str) -> bool {
    matches!(s, "Some" | "None" | "Ok" | "Err")
}

/// Convert a Bock *value* identifier (a param, local binding, or private
/// free-function name) to its Go form: `camelCase`, then escaped against the Go
/// keyword set so a binding named e.g. `default`/`range`/`type` emits
/// `default_`/`range_`/`type_` rather than the illegal bare keyword. Apply at
/// every value declaration and reference site **and** the type-inference
/// scope-map keys, so the escaped name is used uniformly and the maps stay
/// aligned with the emitted name. Member/field/method names and exported
/// (PascalCased) names use bare casing — a keyword is legal as a Go field name,
/// and PascalCasing already lifts a name out of the lowercase keyword set.
/// See [`crate::generator::escape_target_keyword`].
fn go_value_ident(name: &str) -> String {
    crate::generator::escape_target_keyword(
        &to_camel_case(name),
        crate::generator::KeywordTarget::Go,
    )
}

/// Convert a name to `camelCase` (Go unexported).
fn to_camel_case(s: &str) -> String {
    if s.is_empty() || s == "_" {
        return s.to_string();
    }
    // If already camelCase (starts lowercase, no underscores), return as-is.
    if !s.contains('_') && s.starts_with(|c: char| c.is_lowercase()) {
        return s.to_string();
    }
    // If it's snake_case, convert to camelCase.
    if s.contains('_') {
        let parts: Vec<&str> = s.split('_').filter(|p| !p.is_empty()).collect();
        if parts.is_empty() {
            return s.to_string();
        }
        let mut result = parts[0].to_lowercase();
        for part in &parts[1..] {
            let mut chars = part.chars();
            if let Some(first) = chars.next() {
                result.push(
                    first
                        .to_uppercase()
                        .next()
                        .expect("uppercase yields at least one char"),
                );
                result.extend(chars);
            }
        }
        return result;
    }
    // If PascalCase, lowercase first letter.
    let mut chars = s.chars();
    let first = chars.next().expect("non-empty string guaranteed by caller");
    let mut result = first.to_lowercase().to_string();
    result.extend(chars);
    result
}

/// Returns true if `name` is the identifier of a Duration or Instant instance
/// method. Used to recognise `d.as_millis()` / `i.elapsed()` calls during codegen.
fn is_time_method_name(name: &str) -> bool {
    matches!(
        name,
        "as_nanos"
            | "as_millis"
            | "as_seconds"
            | "is_zero"
            | "is_negative"
            | "abs"
            | "elapsed"
            | "duration_since"
    )
}

/// Convert a name to `PascalCase` (Go exported).
fn to_pascal_case(s: &str) -> String {
    if s.is_empty() || s == "_" {
        return s.to_string();
    }
    // If it's snake_case, convert to PascalCase.
    if s.contains('_') {
        let parts: Vec<&str> = s.split('_').filter(|p| !p.is_empty()).collect();
        let mut result = String::new();
        for part in &parts {
            let mut chars = part.chars();
            if let Some(first) = chars.next() {
                result.push(
                    first
                        .to_uppercase()
                        .next()
                        .expect("uppercase yields at least one char"),
                );
                result.extend(chars);
            }
        }
        return result;
    }
    // Already PascalCase or camelCase — uppercase first letter.
    let mut chars = s.chars();
    let first = chars.next().expect("non-empty string guaranteed by caller");
    let mut result = first.to_uppercase().to_string();
    result.extend(chars);
    result
}

/// Escape special characters in a Go string literal.
fn escape_go_string(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    for ch in s.chars() {
        match ch {
            '"' => out.push_str("\\\""),
            '\\' => out.push_str("\\\\"),
            '\n' => out.push_str("\\n"),
            '\r' => out.push_str("\\r"),
            '\t' => out.push_str("\\t"),
            _ => out.push(ch),
        }
    }
    out
}

/// Render a literal as a Go value expression — used by the if-chain match
/// lowering to compare a scrutinee against a literal pattern (`<access> == …`).
/// Render a `RangePat` bound (`lo`/`hi`) as a Go expression. Range bounds are
/// literals (`1..10`) or a const identifier (`MIN..MAX`); anything else falls
/// back to the wrapped literal/identifier text, or `0` for an unrecognised node.
/// Mirrors `range_bound_to_js`.
fn range_bound_to_go(node: &AIRNode) -> String {
    match &node.kind {
        NodeKind::LiteralPat { lit } => go_literal(lit),
        NodeKind::Literal { lit } => go_literal(lit),
        NodeKind::Identifier { name } => go_value_ident(&name.name),
        _ => "0".to_string(),
    }
}

fn go_literal(lit: &Literal) -> String {
    match lit {
        Literal::Int(s) | Literal::Float(s) => s.clone(),
        Literal::Bool(b) => {
            if *b {
                "true".to_string()
            } else {
                "false".to_string()
            }
        }
        Literal::Char(s) => format!("'{s}'"),
        Literal::String(s) => format!("\"{}\"", escape_go_string(s)),
        Literal::Unit => "nil".to_string(),
    }
}

/// Wrap a raw `interface{}` access (a container's `.v` payload) with the type
/// assertion the *child* pattern needs to read it as a typed value. An Optional
/// / Result child re-asserts to its runtime struct so `.tag`/`.v` are reachable;
/// everything else (bind / wildcard / literal / tuple) reads the raw value.
fn go_typed_access(child: &AIRNode, raw_access: &str) -> String {
    if let NodeKind::ConstructorPat { path, .. } = &child.kind {
        let leaf = path.segments.last().map_or("", |s| s.name.as_str());
        match leaf {
            "Some" | "None" => return format!("{raw_access}.(__bockOption)"),
            "Ok" | "Err" => return format!("{raw_access}.(__bockResult)"),
            _ => {}
        }
    }
    raw_access.to_string()
}

// ─── Tests ───────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use bock_air::{AirArg, AirMapEntry, AirRecordField};
    use bock_ast::{Ident, TypePath};
    use bock_errors::{FileId, Span};

    fn span() -> Span {
        Span {
            file: FileId(0),
            start: 0,
            end: 0,
        }
    }

    fn ident(name: &str) -> Ident {
        Ident {
            name: name.to_string(),
            span: span(),
        }
    }

    fn type_path(segments: &[&str]) -> TypePath {
        TypePath {
            segments: segments.iter().map(|s| ident(s)).collect(),
            span: span(),
        }
    }

    fn node(id: u32, kind: NodeKind) -> AIRNode {
        AIRNode::new(id, span(), kind)
    }

    fn int_lit(id: u32, val: &str) -> AIRNode {
        node(
            id,
            NodeKind::Literal {
                lit: Literal::Int(val.into()),
            },
        )
    }

    fn str_lit(id: u32, val: &str) -> AIRNode {
        node(
            id,
            NodeKind::Literal {
                lit: Literal::String(val.into()),
            },
        )
    }

    fn bool_lit(id: u32, val: bool) -> AIRNode {
        node(
            id,
            NodeKind::Literal {
                lit: Literal::Bool(val),
            },
        )
    }

    fn id_node(id: u32, name: &str) -> AIRNode {
        node(id, NodeKind::Identifier { name: ident(name) })
    }

    fn bind_pat(id: u32, name: &str) -> AIRNode {
        node(
            id,
            NodeKind::BindPat {
                name: ident(name),
                is_mut: false,
            },
        )
    }

    fn param_node(id: u32, name: &str) -> AIRNode {
        node(
            id,
            NodeKind::Param {
                pattern: Box::new(bind_pat(id + 100, name)),
                ty: None,
                default: None,
            },
        )
    }

    fn typed_param_node(id: u32, name: &str, ty_name: &str) -> AIRNode {
        node(
            id,
            NodeKind::Param {
                pattern: Box::new(bind_pat(id + 100, name)),
                ty: Some(Box::new(node(
                    id + 200,
                    NodeKind::TypeNamed {
                        path: type_path(&[ty_name]),
                        args: vec![],
                    },
                ))),
                default: None,
            },
        )
    }

    fn block(id: u32, stmts: Vec<AIRNode>, tail: Option<AIRNode>) -> AIRNode {
        node(
            id,
            NodeKind::Block {
                stmts,
                tail: tail.map(Box::new),
            },
        )
    }

    fn module(imports: Vec<AIRNode>, items: Vec<AIRNode>) -> AIRNode {
        node(
            0,
            NodeKind::Module {
                path: None,
                annotations: vec![],
                imports,
                items,
            },
        )
    }

    fn gen(module: &AIRNode) -> String {
        let gen = GoGenerator::new();
        let result = gen.generate_module(module).unwrap();
        result.files[0].content.clone()
    }

    // ── Basic tests ─────────────────────────────────────────────────────────

    #[test]
    fn implements_code_generator_trait() {
        let gen = GoGenerator::new();
        assert_eq!(gen.target().id, "go");
    }

    #[test]
    fn empty_module() {
        let m = module(vec![], vec![]);
        let out = gen(&m);
        assert!(out.contains("package main"), "got: {out}");
    }

    #[test]
    fn simple_function() {
        let body = block(2, vec![], Some(int_lit(3, "42")));
        let f = node(
            1,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Private,
                is_async: false,
                name: ident("answer"),
                generic_params: vec![],
                params: vec![],
                return_type: None,
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(body),
            },
        );
        let out = gen(&module(vec![], vec![f]));
        assert!(out.contains("func answer()"), "got: {out}");
        assert!(out.contains("return 42"), "got: {out}");
    }

    #[test]
    fn public_function_is_pascal_case() {
        let body = block(2, vec![], Some(int_lit(3, "42")));
        let f = node(
            1,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Public,
                is_async: false,
                name: ident("getAnswer"),
                generic_params: vec![],
                params: vec![],
                return_type: None,
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(body),
            },
        );
        let out = gen(&module(vec![], vec![f]));
        assert!(out.contains("func GetAnswer()"), "got: {out}");
    }

    #[test]
    fn function_with_params_and_types() {
        let body = block(
            5,
            vec![],
            Some(node(
                6,
                NodeKind::BinaryOp {
                    op: BinOp::Add,
                    left: Box::new(id_node(7, "a")),
                    right: Box::new(id_node(8, "b")),
                },
            )),
        );
        let f = node(
            1,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Public,
                is_async: false,
                name: ident("add"),
                generic_params: vec![],
                params: vec![
                    typed_param_node(2, "a", "Int"),
                    typed_param_node(3, "b", "Int"),
                ],
                return_type: Some(Box::new(node(
                    4,
                    NodeKind::TypeNamed {
                        path: type_path(&["Int"]),
                        args: vec![],
                    },
                ))),
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(body),
            },
        );
        let out = gen(&module(vec![], vec![f]));
        assert!(
            out.contains("func Add(a int64, b int64) int64 {"),
            "got: {out}"
        );
        assert!(out.contains("(a + b)"), "got: {out}");
    }

    #[test]
    fn record_to_struct() {
        let rec = node(
            1,
            NodeKind::RecordDecl {
                annotations: vec![],
                visibility: Visibility::Public,
                name: ident("Point"),
                generic_params: vec![],
                fields: vec![
                    bock_ast::RecordDeclField {
                        id: 0,
                        span: span(),
                        name: ident("x"),
                        ty: TypeExpr::Named {
                            id: 0,
                            span: span(),
                            path: type_path(&["Float"]),
                            args: vec![],
                        },
                        default: None,
                    },
                    bock_ast::RecordDeclField {
                        id: 1,
                        span: span(),
                        name: ident("y"),
                        ty: TypeExpr::Named {
                            id: 1,
                            span: span(),
                            path: type_path(&["Float"]),
                            args: vec![],
                        },
                        default: None,
                    },
                ],
            },
        );
        let out = gen(&module(vec![], vec![rec]));
        assert!(out.contains("type Point struct {"), "got: {out}");
        assert!(out.contains("X\tfloat64"), "got: {out}");
        assert!(out.contains("Y\tfloat64"), "got: {out}");
    }

    #[test]
    fn trait_to_interface() {
        let t = node(
            1,
            NodeKind::TraitDecl {
                annotations: vec![],
                visibility: Visibility::Public,
                is_platform: false,
                name: ident("Drawable"),
                generic_params: vec![],
                associated_types: vec![],
                methods: vec![node(
                    2,
                    NodeKind::FnDecl {
                        annotations: vec![],
                        visibility: Visibility::Public,
                        is_async: false,
                        name: ident("draw"),
                        generic_params: vec![],
                        params: vec![],
                        return_type: None,
                        effect_clause: vec![],
                        where_clause: vec![],
                        body: Box::new(block(3, vec![], None)),
                    },
                )],
            },
        );
        let out = gen(&module(vec![], vec![t]));
        assert!(out.contains("type Drawable interface {"), "got: {out}");
        assert!(out.contains("Draw()"), "got: {out}");
    }

    #[test]
    fn self_operand_trait_becomes_f_bounded_generic_interface() {
        // P2 item 4: a trait whose method takes a `Self` operand
        // (`compare(self, other: Self)`) is encoded as an F-bounded generic
        // interface so an impl `func (Key) Compare(Key)` can satisfy it and a
        // bound `[T: Comparable]` lowers to `[T Comparable[T]]`. The leading
        // `self` receiver is dropped (implicit in a Go interface method); `Self`
        // renders as the interface's `__Self` type param.
        let self_param = node(
            10,
            NodeKind::Param {
                pattern: Box::new(bind_pat(11, "self")),
                ty: None,
                default: None,
            },
        );
        let other_param = node(
            12,
            NodeKind::Param {
                pattern: Box::new(bind_pat(13, "other")),
                ty: Some(Box::new(node(14, NodeKind::TypeSelf))),
                default: None,
            },
        );
        let method = node(
            2,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Public,
                is_async: false,
                name: ident("compare"),
                generic_params: vec![],
                params: vec![self_param, other_param],
                return_type: Some(Box::new(node(
                    20,
                    NodeKind::TypeNamed {
                        path: type_path(&["Bool"]),
                        args: vec![],
                    },
                ))),
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(block(3, vec![], None)),
            },
        );
        let t = node(
            1,
            NodeKind::TraitDecl {
                annotations: vec![],
                visibility: Visibility::Public,
                is_platform: false,
                name: ident("Comparable"),
                generic_params: vec![],
                associated_types: vec![],
                methods: vec![method],
            },
        );
        let out = gen(&module(vec![], vec![t]));
        assert!(
            out.contains("type Comparable[__Self any] interface {"),
            "self-operand trait should be an F-bounded generic interface, got: {out}"
        );
        assert!(
            out.contains("Compare(__Self)"),
            "the `self` receiver is dropped and `Self` renders as `__Self`, got: {out}"
        );
    }

    #[test]
    fn enum_to_interface_and_structs() {
        let e = node(
            1,
            NodeKind::EnumDecl {
                annotations: vec![],
                visibility: Visibility::Public,
                name: ident("Shape"),
                generic_params: vec![],
                variants: vec![
                    node(
                        2,
                        NodeKind::EnumVariant {
                            name: ident("Circle"),
                            payload: EnumVariantPayload::Struct(vec![bock_ast::RecordDeclField {
                                id: 0,
                                span: span(),
                                name: ident("radius"),
                                ty: TypeExpr::Named {
                                    id: 0,
                                    span: span(),
                                    path: type_path(&["Float"]),
                                    args: vec![],
                                },
                                default: None,
                            }]),
                        },
                    ),
                    node(
                        3,
                        NodeKind::EnumVariant {
                            name: ident("None"),
                            payload: EnumVariantPayload::Unit,
                        },
                    ),
                ],
            },
        );
        let out = gen(&module(vec![], vec![e]));
        assert!(out.contains("type Shape interface {"), "got: {out}");
        assert!(out.contains("isShape()"), "got: {out}");
        assert!(out.contains("type ShapeCircle struct {"), "got: {out}");
        assert!(out.contains("Radius\tfloat64"), "got: {out}");
        assert!(out.contains("type ShapeNone struct{}"), "got: {out}");
        assert!(
            out.contains("func (ShapeCircle) isShape() {}"),
            "got: {out}"
        );
        assert!(out.contains("func (ShapeNone) isShape() {}"), "got: {out}");
    }

    /// Q-go-generic-enum-codegen: a *generic* user enum (`enum Box[T] { Full(T),
    /// Empty }`) must emit Go that actually compiles. The per-variant structs and
    /// the sealed interface all carry the enum's `[T any]` params, so:
    ///   - the marker-method *receiver* uses the bare `[T]` form
    ///     (`func (BoxFull[T]) isBox()`) — the constrained `[T any]` receiver is a
    ///     Go syntax error ("unexpected name any, expected ]");
    ///   - a construction spells the concrete instantiation (`BoxFull[int64]{…}`,
    ///     `BoxEmpty[int64]{}`) recovered from the binding's expected type — Go
    ///     rejects a bare generic struct literal;
    ///   - a type-switch `case` spells it too (`case BoxFull[int64]:`).
    #[test]
    fn generic_user_enum_emits_valid_go() {
        // enum Box[T] { Full(T), Empty }
        let e = node(
            1,
            NodeKind::EnumDecl {
                annotations: vec![],
                visibility: Visibility::Public,
                name: ident("Box"),
                generic_params: vec![generic_param(2, "T")],
                variants: vec![
                    node(
                        3,
                        NodeKind::EnumVariant {
                            name: ident("Full"),
                            payload: EnumVariantPayload::Tuple(vec![named_type(4, "T")]),
                        },
                    ),
                    node(
                        5,
                        NodeKind::EnumVariant {
                            name: ident("Empty"),
                            payload: EnumVariantPayload::Unit,
                        },
                    ),
                ],
            },
        );
        // `b: Box[Int]` parameter (a generic instantiation, args = [Int]).
        let box_int = |id: u32| {
            node(
                id,
                NodeKind::TypeNamed {
                    path: type_path(&["Box"]),
                    args: vec![named_type(id + 1, "Int")],
                },
            )
        };
        let b_param = node(
            20,
            NodeKind::Param {
                pattern: Box::new(bind_pat(21, "b")),
                ty: Some(Box::new(box_int(22))),
                default: None,
            },
        );
        // match b { Full(x) => return x  Empty => return 0 }
        let full_arm = node(
            30,
            NodeKind::MatchArm {
                pattern: Box::new(node(
                    31,
                    NodeKind::ConstructorPat {
                        path: type_path(&["Full"]),
                        fields: vec![bind_pat(32, "x")],
                    },
                )),
                guard: None,
                body: Box::new(block(
                    33,
                    vec![],
                    Some(node(
                        34,
                        NodeKind::Return {
                            value: Some(Box::new(id_node(35, "x"))),
                        },
                    )),
                )),
            },
        );
        let empty_arm = node(
            36,
            NodeKind::MatchArm {
                pattern: Box::new(node(
                    37,
                    NodeKind::ConstructorPat {
                        path: type_path(&["Empty"]),
                        fields: vec![],
                    },
                )),
                guard: None,
                body: Box::new(block(
                    38,
                    vec![],
                    Some(node(
                        39,
                        NodeKind::Return {
                            value: Some(Box::new(int_lit(40, "0"))),
                        },
                    )),
                )),
            },
        );
        let match_expr = node(
            41,
            NodeKind::Match {
                scrutinee: Box::new(id_node(42, "b")),
                arms: vec![full_arm, empty_arm],
            },
        );
        let unwrap_fn = node(
            50,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Private,
                is_async: false,
                name: ident("unwrap_or_zero"),
                generic_params: vec![],
                params: vec![b_param],
                return_type: Some(Box::new(named_type(51, "Int"))),
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(block(52, vec![match_expr], None)),
            },
        );
        // fn build() -> Void { let f: Box[Int] = Full(7); let e: Box[Int] = Empty }
        let let_f = node(
            60,
            NodeKind::LetBinding {
                is_mut: false,
                pattern: Box::new(bind_pat(61, "f")),
                ty: Some(Box::new(box_int(62))),
                value: Box::new(node(
                    63,
                    NodeKind::Call {
                        callee: Box::new(id_node(64, "Full")),
                        type_args: vec![],
                        args: vec![AirArg {
                            label: None,
                            value: int_lit(65, "7"),
                        }],
                    },
                )),
            },
        );
        let let_e = node(
            66,
            NodeKind::LetBinding {
                is_mut: false,
                pattern: Box::new(bind_pat(67, "e")),
                ty: Some(Box::new(box_int(68))),
                value: Box::new(id_node(69, "Empty")),
            },
        );
        let build_fn = node(
            70,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Private,
                is_async: false,
                name: ident("build"),
                generic_params: vec![],
                params: vec![],
                return_type: None,
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(block(71, vec![let_f, let_e], None)),
            },
        );
        let out = gen(&module(vec![], vec![e, unwrap_fn, build_fn]));
        // Marker-method receivers use the bare `[T]` form, never `[T any]`.
        assert!(
            out.contains("func (BoxFull[T]) isBox() {}"),
            "marker receiver must be bare `[T]`, got: {out}"
        );
        assert!(
            out.contains("func (BoxEmpty[T]) isBox() {}"),
            "marker receiver must be bare `[T]`, got: {out}"
        );
        assert!(
            !out.contains("[T any])"),
            "no receiver may carry the `[T any]` constraint, got: {out}"
        );
        // The type DECLARATIONS keep the constrained `[T any]` form.
        assert!(
            out.contains("type BoxFull[T any] struct {"),
            "variant struct decl keeps `[T any]`, got: {out}"
        );
        // Type-switch cases spell the concrete instantiation.
        assert!(
            out.contains("case BoxFull[int64]:"),
            "type-switch case must instantiate, got: {out}"
        );
        assert!(
            out.contains("case BoxEmpty[int64]:"),
            "type-switch case must instantiate, got: {out}"
        );
        // Constructions spell the concrete instantiation.
        assert!(
            out.contains("BoxFull[int64]{Field0: 7}"),
            "tuple-variant construction must instantiate, got: {out}"
        );
        assert!(
            out.contains("BoxEmpty[int64]{}"),
            "unit-variant construction must instantiate, got: {out}"
        );
    }

    /// Q-go-enum-return-boxing: a value-position `if` whose branches yield enum
    /// variants, returned where the declared type is the sealed enum interface,
    /// must lower to a `func() Shape { ... }()` closure (not `func() interface{}`),
    /// so the boxed variant struct is assignable to the `Shape` return.
    #[test]
    fn enum_variant_if_branches_box_into_sealed_interface() {
        // enum Shape { Circle, Square }  (both unit for brevity)
        let e = node(
            1,
            NodeKind::EnumDecl {
                annotations: vec![],
                visibility: Visibility::Public,
                name: ident("Shape"),
                generic_params: vec![],
                variants: vec![
                    node(
                        2,
                        NodeKind::EnumVariant {
                            name: ident("Circle"),
                            payload: EnumVariantPayload::Unit,
                        },
                    ),
                    node(
                        3,
                        NodeKind::EnumVariant {
                            name: ident("Square"),
                            payload: EnumVariantPayload::Unit,
                        },
                    ),
                ],
            },
        );
        // fn pick(big: Bool) -> Shape { if (big) { Circle } else { Square } }
        let if_expr = node(
            10,
            NodeKind::If {
                let_pattern: None,
                condition: Box::new(id_node(11, "big")),
                then_block: Box::new(block(12, vec![], Some(id_node(13, "Circle")))),
                else_block: Some(Box::new(block(14, vec![], Some(id_node(15, "Square"))))),
            },
        );
        let f = node(
            20,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Public,
                is_async: false,
                name: ident("pick"),
                generic_params: vec![],
                params: vec![typed_param_node(21, "big", "Bool")],
                return_type: Some(Box::new(node(
                    22,
                    NodeKind::TypeNamed {
                        path: type_path(&["Shape"]),
                        args: vec![],
                    },
                ))),
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(block(23, vec![], Some(if_expr))),
            },
        );
        let out = gen(&module(vec![], vec![e, f]));
        assert!(
            out.contains("func() Shape {"),
            "the value-position if must be a `func() Shape` closure (boxed into \
             the sealed interface), not a bare-interface closure, got: {out}"
        );
        assert!(
            !out.contains("func() interface{} { if "),
            "the if-closure must not fall back to a bare interface, got: {out}"
        );
        assert!(
            out.contains("return ShapeCircle{}") && out.contains("return ShapeSquare{}"),
            "each branch returns its variant struct, got: {out}"
        );
    }

    /// Q-go-enum-return-boxing: an UNTYPED `let m = if (..) { Circle } else
    /// { Square }` infers the variant's owning enum, so the value closure is
    /// typed `func() Shape` rather than the enclosing fn's unrelated return type.
    #[test]
    fn untyped_let_if_over_variants_infers_enum_iife_type() {
        let e = node(
            1,
            NodeKind::EnumDecl {
                annotations: vec![],
                visibility: Visibility::Public,
                name: ident("Shape"),
                generic_params: vec![],
                variants: vec![
                    node(
                        2,
                        NodeKind::EnumVariant {
                            name: ident("Circle"),
                            payload: EnumVariantPayload::Unit,
                        },
                    ),
                    node(
                        3,
                        NodeKind::EnumVariant {
                            name: ident("Square"),
                            payload: EnumVariantPayload::Unit,
                        },
                    ),
                ],
            },
        );
        // fn run() -> Void { let m = if (true) { Circle } else { Square } }
        let let_binding = node(
            10,
            NodeKind::LetBinding {
                is_mut: false,
                pattern: Box::new(bind_pat(11, "m")),
                ty: None,
                value: Box::new(node(
                    12,
                    NodeKind::If {
                        let_pattern: None,
                        condition: Box::new(bool_lit(13, true)),
                        then_block: Box::new(block(14, vec![], Some(id_node(15, "Circle")))),
                        else_block: Some(Box::new(block(16, vec![], Some(id_node(17, "Square"))))),
                    },
                )),
            },
        );
        let f = node(
            20,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Public,
                is_async: false,
                name: ident("run"),
                generic_params: vec![],
                params: vec![],
                return_type: None,
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(block(23, vec![let_binding], None)),
            },
        );
        let out = gen(&module(vec![], vec![e, f]));
        assert!(
            out.contains("m := func() Shape {"),
            "an untyped let over variant branches infers the `Shape` closure \
             type, got: {out}"
        );
    }

    #[test]
    fn effects_as_interface_params() {
        let body = block(
            3,
            vec![node(
                4,
                NodeKind::LetBinding {
                    is_mut: false,
                    pattern: Box::new(bind_pat(5, "msg")),
                    ty: None,
                    value: Box::new(str_lit(6, "hello")),
                },
            )],
            Some(node(
                7,
                NodeKind::EffectOp {
                    effect: type_path(&["Log"]),
                    operation: ident("info"),
                    args: vec![AirArg {
                        label: None,
                        value: id_node(8, "msg"),
                    }],
                },
            )),
        );
        let f = node(
            1,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Public,
                is_async: false,
                name: ident("process"),
                generic_params: vec![],
                params: vec![param_node(2, "data")],
                return_type: None,
                effect_clause: vec![type_path(&["Log"]), type_path(&["Clock"])],
                where_clause: vec![],
                body: Box::new(body),
            },
        );
        let out = gen(&module(vec![], vec![f]));
        assert!(
            out.contains("func Process(data interface{}, log Log, clock Clock)"),
            "got: {out}"
        );
        assert!(out.contains("log.Info(msg)"), "got: {out}");
    }

    /// Q-clock-handler-routing: inside a `with Clock` function the §18.3.1 time
    /// builtins route through the in-scope `clock` handler — `Instant.now()` →
    /// `clock.NowMonotonic()`, `sleep(d)` → `clock.Sleep(d)`, and the derived
    /// `start.elapsed()` via `clock.NowMonotonic().Sub(start)` — NOT the inlined
    /// host primitives (`time.Now()` / `time.Sleep`).
    #[test]
    fn clock_time_ops_route_through_handler() {
        let out = gen(&module(vec![], vec![clock_timed_fn()]));
        assert!(out.contains("clock.NowMonotonic()"), "got: {out}");
        assert!(out.contains("clock.Sleep("), "got: {out}");
        assert!(
            !out.contains("time.Now()"),
            "host clock primitive leaked past the handler: {out}"
        );
        assert!(
            !out.contains("time.Sleep"),
            "host sleep primitive leaked past the handler: {out}"
        );
    }

    /// `Duration` / `Instant` used as type annotations must render their Go
    /// value representations (`int64` / `time.Time`), not the undefined
    /// identifiers, so a `Clock` handler impl compiles (Q-clock-handler-routing
    /// supporting fix).
    #[test]
    fn builtin_time_types_map_to_go() {
        let f = node(
            1,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Public,
                is_async: false,
                name: ident("span"),
                generic_params: vec![],
                params: vec![typed_param_node(2, "d", "Duration")],
                return_type: Some(Box::new(node(
                    3,
                    NodeKind::TypeNamed {
                        path: type_path(&["Instant"]),
                        args: vec![],
                    },
                ))),
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(block(10, vec![], None)),
            },
        );
        let out = gen(&module(vec![], vec![f]));
        assert!(out.contains("d int64"), "Duration annotation: {out}");
        assert!(out.contains("time.Time"), "Instant annotation: {out}");
    }

    /// Builds `fn timed() with Clock { let start = Instant.now(); sleep(
    /// Duration.millis(1)); let d = start.elapsed() }` — the `with Clock` clause
    /// puts the `clock` handler in scope so the time builtins route through it.
    fn clock_timed_fn() -> AIRNode {
        let instant_now = node(
            40,
            NodeKind::Call {
                callee: Box::new(node(
                    41,
                    NodeKind::FieldAccess {
                        object: Box::new(id_node(42, "Instant")),
                        field: ident("now"),
                    },
                )),
                args: vec![],
                type_args: vec![],
            },
        );
        let duration_millis = node(
            50,
            NodeKind::Call {
                callee: Box::new(node(
                    51,
                    NodeKind::FieldAccess {
                        object: Box::new(id_node(52, "Duration")),
                        field: ident("millis"),
                    },
                )),
                args: vec![AirArg {
                    label: None,
                    value: int_lit(53, "1"),
                }],
                type_args: vec![],
            },
        );
        let sleep_call = node(
            60,
            NodeKind::Call {
                callee: Box::new(id_node(61, "sleep")),
                args: vec![AirArg {
                    label: None,
                    value: duration_millis,
                }],
                type_args: vec![],
            },
        );
        let elapsed_call = node(
            70,
            NodeKind::MethodCall {
                receiver: Box::new(id_node(71, "start")),
                method: ident("elapsed"),
                type_args: vec![],
                args: vec![],
            },
        );
        let body = block(
            30,
            vec![
                node(
                    31,
                    NodeKind::LetBinding {
                        is_mut: false,
                        pattern: Box::new(bind_pat(32, "start")),
                        ty: None,
                        value: Box::new(instant_now),
                    },
                ),
                sleep_call,
                node(
                    33,
                    NodeKind::LetBinding {
                        is_mut: false,
                        pattern: Box::new(bind_pat(34, "d")),
                        ty: None,
                        value: Box::new(elapsed_call),
                    },
                ),
            ],
            None,
        );
        node(
            1,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Private,
                is_async: false,
                name: ident("timed"),
                generic_params: vec![],
                params: vec![],
                return_type: None,
                effect_clause: vec![type_path(&["Clock"])],
                where_clause: vec![],
                body: Box::new(body),
            },
        )
    }

    #[test]
    fn generics_with_type_params() {
        let body = block(2, vec![], Some(id_node(3, "value")));
        let f = node(
            1,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Public,
                is_async: false,
                name: ident("identity"),
                generic_params: vec![bock_ast::GenericParam {
                    id: 10,
                    span: span(),
                    name: ident("T"),
                    bounds: vec![],
                }],
                params: vec![typed_param_node(2, "value", "T")],
                return_type: Some(Box::new(node(
                    4,
                    NodeKind::TypeNamed {
                        path: type_path(&["T"]),
                        args: vec![],
                    },
                ))),
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(body),
            },
        );
        let out = gen(&module(vec![], vec![f]));
        assert!(
            out.contains("func Identity[T any](value T) T {"),
            "got: {out}"
        );
    }

    #[test]
    fn generics_with_bounds() {
        let body = block(2, vec![], Some(id_node(3, "value")));
        let f = node(
            1,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Public,
                is_async: false,
                name: ident("constrained"),
                generic_params: vec![bock_ast::GenericParam {
                    id: 10,
                    span: span(),
                    name: ident("T"),
                    bounds: vec![type_path(&["Comparable"])],
                }],
                params: vec![typed_param_node(2, "value", "T")],
                return_type: Some(Box::new(node(
                    4,
                    NodeKind::TypeNamed {
                        path: type_path(&["T"]),
                        args: vec![],
                    },
                ))),
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(body),
            },
        );
        let out = gen(&module(vec![], vec![f]));
        // GAP-C: `Comparable` is a sealed-core trait with no user `impl` in this
        // module, so the bound lowers to Go's self-contained ordered constraint
        // `__bockOrdered` (there is no `Comparable` type in Go). A user-declared
        // `Comparable` trait would keep its name (see `use_core_compare` exec).
        assert!(
            out.contains("func Constrained[T __bockOrdered](value T) T {"),
            "got: {out}"
        );
    }

    #[test]
    fn match_to_switch() {
        let m = node(
            1,
            NodeKind::Match {
                scrutinee: Box::new(id_node(2, "x")),
                arms: vec![
                    node(
                        3,
                        NodeKind::MatchArm {
                            pattern: Box::new(node(
                                4,
                                NodeKind::LiteralPat {
                                    lit: Literal::Int("1".into()),
                                },
                            )),
                            guard: None,
                            body: Box::new(block(5, vec![], Some(str_lit(6, "one")))),
                        },
                    ),
                    node(
                        7,
                        NodeKind::MatchArm {
                            pattern: Box::new(node(8, NodeKind::WildcardPat)),
                            guard: None,
                            body: Box::new(block(9, vec![], Some(str_lit(10, "other")))),
                        },
                    ),
                ],
            },
        );
        let out = gen(&module(vec![], vec![m]));
        assert!(out.contains("switch"), "got: {out}");
        assert!(out.contains("default:"), "got: {out}");
    }

    /// A guarded arm now lowers to the shared if/else-if chain: the arm's
    /// condition tests the *pattern* AND the *guard* (`x == 1 && (ok)`), so a
    /// failed guard falls through to the next arm — the fall-through the prior
    /// `case 1: if ok { … }` lowering could not express (its `break` exited the
    /// whole switch).
    #[test]
    fn match_arm_guard_emits_if() {
        let m = node(
            1,
            NodeKind::Match {
                scrutinee: Box::new(id_node(2, "x")),
                arms: vec![node(
                    3,
                    NodeKind::MatchArm {
                        pattern: Box::new(node(
                            4,
                            NodeKind::LiteralPat {
                                lit: Literal::Int("1".into()),
                            },
                        )),
                        guard: Some(Box::new(id_node(5, "ok"))),
                        body: Box::new(block(
                            6,
                            vec![node(7, NodeKind::Return { value: None })],
                            None,
                        )),
                    },
                )],
            },
        );
        let out = gen(&module(vec![], vec![m]));
        assert!(
            out.contains("if x == 1 && (ok) {"),
            "guard should test pattern AND guard in an if-chain, got: {out}"
        );
        assert!(
            !out.contains("switch"),
            "a guarded match must not use a switch, got: {out}"
        );
        assert!(
            !out.contains("// guard"),
            "guard should not be a comment, got: {out}"
        );
    }

    #[test]
    fn let_binding() {
        let l = node(
            1,
            NodeKind::LetBinding {
                is_mut: false,
                pattern: Box::new(bind_pat(2, "x")),
                ty: None,
                value: Box::new(int_lit(3, "42")),
            },
        );
        let out = gen(&module(vec![], vec![l]));
        // An untyped integer-literal binding is pinned to `int64` (Bock `Int`):
        // a bare `x := 42` would be Go's default `int`, which then fails to mix
        // with an `int64` value downstream. See the `pin_int64` path.
        assert!(out.contains("var x int64 = 42"), "got: {out}");
    }

    #[test]
    fn let_binding_with_type() {
        let l = node(
            1,
            NodeKind::LetBinding {
                is_mut: false,
                pattern: Box::new(bind_pat(2, "x")),
                ty: Some(Box::new(node(
                    4,
                    NodeKind::TypeNamed {
                        path: type_path(&["Int"]),
                        args: vec![],
                    },
                ))),
                value: Box::new(int_lit(3, "42")),
            },
        );
        let out = gen(&module(vec![], vec![l]));
        assert!(out.contains("var x int64 = 42"), "got: {out}");
    }

    /// Q-match-exprpos (P4): a value-position `let flag: Bool = match n { … }`
    /// inside a function returning `String`. The expression-position match IIFE
    /// must take its return type from the *binding's* declared type (`bool`), not
    /// the enclosing function's return type (`string`) — otherwise the IIFE
    /// (`func() string { … }()`) is not assignable to `var flag bool`. The fix
    /// records the declared `let` type as `current_expected_type` and prefers it
    /// for the IIFE return.
    #[test]
    fn expr_position_match_uses_binding_type_not_fn_ret() {
        let m = node(
            10,
            NodeKind::Match {
                scrutinee: Box::new(id_node(11, "n")),
                arms: vec![
                    node(
                        12,
                        NodeKind::MatchArm {
                            pattern: Box::new(node(
                                13,
                                NodeKind::LiteralPat {
                                    lit: Literal::Int("0".into()),
                                },
                            )),
                            guard: None,
                            body: Box::new(block(14, vec![], Some(bool_lit(15, true)))),
                        },
                    ),
                    node(
                        16,
                        NodeKind::MatchArm {
                            pattern: Box::new(node(17, NodeKind::WildcardPat)),
                            guard: None,
                            body: Box::new(block(18, vec![], Some(bool_lit(19, false)))),
                        },
                    ),
                ],
            },
        );
        let let_flag = node(
            20,
            NodeKind::LetBinding {
                is_mut: false,
                pattern: Box::new(bind_pat(21, "flag")),
                ty: Some(Box::new(node(
                    22,
                    NodeKind::TypeNamed {
                        path: type_path(&["Bool"]),
                        args: vec![],
                    },
                ))),
                value: Box::new(m),
            },
        );
        let f = node(
            1,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Public,
                is_async: false,
                name: ident("decide"),
                generic_params: vec![],
                params: vec![typed_param_node(2, "n", "Int")],
                return_type: Some(Box::new(node(
                    3,
                    NodeKind::TypeNamed {
                        path: type_path(&["String"]),
                        args: vec![],
                    },
                ))),
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(block(4, vec![let_flag], Some(str_lit(5, "x")))),
            },
        );
        let out = gen(&module(vec![], vec![f]));
        assert!(
            out.contains("var flag bool = func() bool {"),
            "IIFE must be typed with the binding type (bool), not the fn return (string), got: {out}"
        );
        assert!(
            !out.contains("func() string {"),
            "the match IIFE must not be typed with the function return type, got: {out}"
        );
    }

    #[test]
    fn if_else() {
        let stmt = node(
            1,
            NodeKind::If {
                let_pattern: None,
                condition: Box::new(bool_lit(2, true)),
                then_block: Box::new(block(3, vec![], Some(int_lit(4, "1")))),
                else_block: Some(Box::new(block(5, vec![], Some(int_lit(6, "0"))))),
            },
        );
        let out = gen(&module(vec![], vec![stmt]));
        assert!(out.contains("if true {"), "got: {out}");
        assert!(out.contains("} else {"), "got: {out}");
    }

    #[test]
    fn for_loop() {
        // The loop variable is *referenced* in the body, so it keeps its name.
        let stmt = node(
            1,
            NodeKind::For {
                pattern: Box::new(bind_pat(2, "item")),
                iterable: Box::new(id_node(3, "items")),
                body: Box::new(block(4, vec![id_node(5, "item")], None)),
            },
        );
        let out = gen(&module(vec![], vec![stmt]));
        assert!(out.contains("for _, item := range items {"), "got: {out}");
    }

    #[test]
    fn for_loop_unused_var_drops_to_for_range() {
        // An unused loop variable would make Go reject `for _, item := range`
        // ("declared and not used"); `for _, _ := range` is itself invalid ("no
        // new variables on left side of :="). The emitter drops both to the bare
        // `for range items` form.
        let stmt = node(
            1,
            NodeKind::For {
                pattern: Box::new(bind_pat(2, "item")),
                iterable: Box::new(id_node(3, "items")),
                body: Box::new(block(4, vec![], None)),
            },
        );
        let out = gen(&module(vec![], vec![stmt]));
        assert!(out.contains("for range items {"), "got: {out}");
        assert!(!out.contains("for _, item"), "got: {out}");
    }

    #[test]
    fn while_loop() {
        let stmt = node(
            1,
            NodeKind::While {
                condition: Box::new(bool_lit(2, true)),
                body: Box::new(block(3, vec![], None)),
            },
        );
        let out = gen(&module(vec![], vec![stmt]));
        assert!(out.contains("for true {"), "got: {out}");
    }

    #[test]
    fn infinite_loop() {
        let stmt = node(
            1,
            NodeKind::Loop {
                body: Box::new(block(2, vec![], None)),
            },
        );
        let out = gen(&module(vec![], vec![stmt]));
        assert!(out.contains("for {"), "got: {out}");
    }

    #[test]
    fn string_interpolation() {
        let interp = node(
            1,
            NodeKind::Interpolation {
                parts: vec![
                    AirInterpolationPart::Literal("Hello, ".into()),
                    AirInterpolationPart::Expr(Box::new(id_node(2, "name"))),
                    AirInterpolationPart::Literal("!".into()),
                ],
            },
        );
        let out = gen(&module(vec![], vec![interp]));
        assert!(out.contains("fmt.Sprintf"), "got: {out}");
        // Each `${expr}` part renders through `__bockStr` (a `%s` verb over a
        // `string`) so a user value with a `Displayable` impl dispatches through
        // its `ToString` (Q-displayable-interpolation-dispatch); other values
        // fall back to `%v` inside the helper.
        assert!(out.contains("Hello, %s!"), "got: {out}");
        assert!(out.contains("__bockStr(name)"), "got: {out}");
        assert!(out.contains("import \"fmt\""), "got: {out}");
    }

    /// Q-go-percent-interpolation: a literal `%` inside an interpolated string
    /// lands in a `fmt.Sprintf` FORMAT string and must double to `%%` — left
    /// single it pairs with the following bytes as a verb (`"${n}% pass"` →
    /// `95%!p(MISSING)ass`), a silent cross-target output divergence (the build
    /// stays green and only Go corrupts).
    #[test]
    fn interpolation_escapes_literal_percent() {
        let interp = node(
            1,
            NodeKind::Interpolation {
                parts: vec![
                    AirInterpolationPart::Expr(Box::new(id_node(2, "n"))),
                    AirInterpolationPart::Literal("% pass, 100%% raw".into()),
                ],
            },
        );
        let out = gen(&module(vec![], vec![interp]));
        // The `${n}` part renders through `__bockStr` (`%s`); the literal `%`
        // bytes still double in the format string.
        assert!(
            out.contains(r#"fmt.Sprintf("%s%% pass, 100%%%% raw", __bockStr(n))"#),
            "literal % must be doubled in the Sprintf format string, got: {out}"
        );
    }

    /// Q-go-runtime-helper-shadowing: a parameter named after a public module
    /// fn (`lines` — the `core.string` helper shape) must be spelled as the
    /// LOCAL (`lines`) at every reference, not the PascalCased helper
    /// (`Lines`) — here in for-in iterable position, the dogfood repro.
    #[test]
    fn local_param_shadows_public_fn_rename() {
        let pub_lines = node(
            10,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Public,
                is_async: false,
                name: ident("lines"),
                generic_params: vec![],
                params: vec![],
                return_type: None,
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(block(11, vec![], None)),
            },
        );
        let loop_stmt = node(
            20,
            NodeKind::For {
                pattern: Box::new(bind_pat(21, "line")),
                iterable: Box::new(id_node(22, "lines")),
                body: Box::new(block(23, vec![id_node(24, "line")], None)),
            },
        );
        let count_fn = node(
            30,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Private,
                is_async: false,
                name: ident("count"),
                generic_params: vec![],
                params: vec![param_node(31, "lines")],
                return_type: None,
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(block(32, vec![loop_stmt], None)),
            },
        );
        let out = gen(&module(vec![], vec![pub_lines, count_fn]));
        assert!(
            out.contains("range lines {"),
            "an in-scope param must shadow the public-fn rename, got: {out}"
        );
        assert!(
            !out.contains("range Lines"),
            "the PascalCased helper must not be referenced for the local, got: {out}"
        );
    }

    /// Q-go-split-combinator-typing: the builtin String-method return table
    /// mirrors `try_emit_string_method`'s lowerings (`split` → `[]string`,
    /// transforms → `string`, …), gated on the checker's receiver-kind
    /// annotation so a same-named user method is never mistaken for the
    /// builtin.
    #[test]
    fn string_builtin_return_type_mirrors_lowering() {
        let build = |method: &str, tag: Option<&str>| {
            let object = id_node(5, "s");
            let callee = node(
                6,
                NodeKind::FieldAccess {
                    object: Box::new(object),
                    field: ident(method),
                },
            );
            // The lowerer clones the receiver into the leading self arg: same
            // NodeId as the field-access object (see `desugared_self_call`).
            let args = vec![
                AirArg {
                    label: None,
                    value: id_node(5, "s"),
                },
                AirArg {
                    label: None,
                    value: str_lit(7, ","),
                },
            ];
            let mut call = node(
                8,
                NodeKind::Call {
                    callee: Box::new(callee.clone()),
                    args: args.clone(),
                    type_args: vec![],
                },
            );
            if let Some(tag) = tag {
                call.metadata.insert(
                    bock_types::checker::RECV_KIND_META_KEY.to_string(),
                    bock_air::Value::String(tag.to_string()),
                );
            }
            (call, callee, args)
        };
        for (method, expect) in [
            ("split", Some("[]string")),
            ("trim", Some("string")),
            ("to_upper", Some("string")),
            ("len", Some("int64")),
            ("contains", Some("bool")),
            ("char_at", Some("__bockOption")),
            ("frobnicate", None),
        ] {
            let (call, callee, args) = build(method, Some("Primitive:String"));
            assert_eq!(
                GoEmitCtx::string_builtin_return_go_type(&call, &callee, &args).as_deref(),
                expect,
                "method {method}"
            );
        }
        // No checker annotation / non-String receiver → not the builtin.
        let (call, callee, args) = build("split", None);
        assert_eq!(
            GoEmitCtx::string_builtin_return_go_type(&call, &callee, &args),
            None
        );
        let (call, callee, args) = build("split", Some("User:Tokenizer"));
        assert_eq!(
            GoEmitCtx::string_builtin_return_go_type(&call, &callee, &args),
            None
        );
    }

    #[test]
    fn record_construction() {
        let rc = node(
            1,
            NodeKind::RecordConstruct {
                path: type_path(&["Point"]),
                fields: vec![
                    AirRecordField {
                        name: ident("x"),
                        value: Some(Box::new(int_lit(2, "1"))),
                    },
                    AirRecordField {
                        name: ident("y"),
                        value: Some(Box::new(int_lit(3, "2"))),
                    },
                ],
                spread: None,
            },
        );
        let out = gen(&module(vec![], vec![rc]));
        assert!(out.contains("Point{X: 1, Y: 2}"), "got: {out}");
    }

    #[test]
    fn list_literal() {
        let l = node(
            1,
            NodeKind::ListLiteral {
                elems: vec![int_lit(2, "1"), int_lit(3, "2"), int_lit(4, "3")],
            },
        );
        let out = gen(&module(vec![], vec![l]));
        // A homogeneous integer list literal now infers a concrete element
        // type (`[]int64`), not the erased `[]interface{}` — so element
        // arithmetic / typed iteration / typed returns compile (P3-α item 1b).
        assert!(out.contains("[]int64{1, 2, 3}"), "got: {out}");
    }

    /// A list literal with no concretely-inferable common element type (here a
    /// mixed int/string literal) falls back to the erased `[]interface{}` —
    /// never a wrong concrete type (P3-α item 1b).
    #[test]
    fn list_literal_mixed_falls_back_to_interface() {
        let l = node(
            1,
            NodeKind::ListLiteral {
                elems: vec![int_lit(2, "1"), str_lit(3, "x")],
            },
        );
        let out = gen(&module(vec![], vec![l]));
        assert!(out.contains("[]interface{}{1, \"x\"}"), "got: {out}");
    }

    /// An empty list literal cannot infer an element type, so it falls back to
    /// `[]interface{}` when emitted with no declared-type context.
    #[test]
    fn empty_list_literal_falls_back_to_interface() {
        let l = node(1, NodeKind::ListLiteral { elems: vec![] });
        let out = gen(&module(vec![], vec![l]));
        assert!(out.contains("[]interface{}{}"), "got: {out}");
    }

    /// A homogeneous map literal infers its key and value element types
    /// separately (`map[string]int64`), not the erased
    /// `map[interface{}]interface{}` (P3-α item 1b).
    #[test]
    fn map_literal_infers_key_and_value() {
        let entry = AirMapEntry {
            key: str_lit(2, "a"),
            value: int_lit(3, "1"),
        };
        let m = node(
            1,
            NodeKind::MapLiteral {
                entries: vec![entry],
            },
        );
        let out = gen(&module(vec![], vec![m]));
        assert!(out.contains("map[string]int64{\"a\": 1}"), "got: {out}");
    }

    /// A homogeneous set literal infers a concrete element type
    /// (`map[int64]struct{}`).
    #[test]
    fn set_literal_infers_elem() {
        let s = node(
            1,
            NodeKind::SetLiteral {
                elems: vec![int_lit(2, "1"), int_lit(3, "2")],
            },
        );
        let out = gen(&module(vec![], vec![s]));
        assert!(
            out.contains("map[int64]struct{}{1: {}, 2: {}}"),
            "got: {out}"
        );
    }

    #[test]
    fn effect_decl_to_interface() {
        let ed = node(
            1,
            NodeKind::EffectDecl {
                annotations: vec![],
                visibility: Visibility::Public,
                name: ident("Logger"),
                generic_params: vec![],
                components: vec![],
                operations: vec![
                    node(
                        2,
                        NodeKind::FnDecl {
                            annotations: vec![],
                            visibility: Visibility::Public,
                            is_async: false,
                            name: ident("info"),
                            generic_params: vec![],
                            params: vec![typed_param_node(3, "msg", "String")],
                            return_type: None,
                            effect_clause: vec![],
                            where_clause: vec![],
                            body: Box::new(block(4, vec![], None)),
                        },
                    ),
                    node(
                        5,
                        NodeKind::FnDecl {
                            annotations: vec![],
                            visibility: Visibility::Public,
                            is_async: false,
                            name: ident("error"),
                            generic_params: vec![],
                            params: vec![typed_param_node(6, "msg", "String")],
                            return_type: None,
                            effect_clause: vec![],
                            where_clause: vec![],
                            body: Box::new(block(7, vec![], None)),
                        },
                    ),
                ],
            },
        );
        let out = gen(&module(vec![], vec![ed]));
        assert!(out.contains("type Logger interface {"), "got: {out}");
        assert!(out.contains("Info(string)"), "got: {out}");
        assert!(out.contains("Error(string)"), "got: {out}");
    }

    #[test]
    fn result_construct_ok() {
        // `ResultConstruct` lowers to the tagged Result-runtime constructor
        // `__bockOk(..)` — the same shape the surface `Ok(..)` construction emits
        // and the `Result` match reads — reconciling construction with match. A
        // numeric-*literal* payload is boxed at its concrete Go type
        // (`int64(42)`), so the `interface{}` box's dynamic type is `int64`, not
        // the untyped-constant default `int` — a later `.(int64)` / generic
        // `.(T)` payload assertion would otherwise panic (`box_payload_str`).
        let rc = node(
            1,
            NodeKind::ResultConstruct {
                variant: ResultVariant::Ok,
                value: Some(Box::new(int_lit(2, "42"))),
            },
        );
        let out = gen(&module(vec![], vec![rc]));
        assert!(out.contains("__bockOk(int64(42))"), "got: {out}");
    }

    #[test]
    fn result_construct_err() {
        let rc = node(
            1,
            NodeKind::ResultConstruct {
                variant: ResultVariant::Err,
                value: Some(Box::new(str_lit(2, "failed"))),
            },
        );
        let out = gen(&module(vec![], vec![rc]));
        // A string payload is *not* boxed — only numeric literals are cast.
        assert!(out.contains("__bockErr(\"failed\")"), "got: {out}");
    }

    #[test]
    fn numeric_literal_go_type_recognises_int_float_and_negation() {
        // Bare int / float literals carry their concrete Go boxing type; a unary
        // negation is transparent; a string / identifier is not numeric.
        assert_eq!(
            GoEmitCtx::numeric_literal_go_type(&int_lit(1, "7")),
            Some("int64")
        );
        let flt = node(
            2,
            NodeKind::Literal {
                lit: Literal::Float("1.5".into()),
            },
        );
        assert_eq!(GoEmitCtx::numeric_literal_go_type(&flt), Some("float64"));
        let neg = node(
            3,
            NodeKind::UnaryOp {
                op: UnaryOp::Neg,
                operand: Box::new(int_lit(4, "1")),
            },
        );
        assert_eq!(GoEmitCtx::numeric_literal_go_type(&neg), Some("int64"));
        assert_eq!(
            GoEmitCtx::numeric_literal_go_type(&str_lit(5, "x")),
            None,
            "a string literal is not a numeric payload"
        );
        assert_eq!(
            GoEmitCtx::numeric_literal_go_type(&id_node(6, "v")),
            None,
            "a variable reference is not a numeric literal"
        );
    }

    #[test]
    fn rendered_collection_elem_parses_slice_and_map() {
        // A collection-typed IIFE return propagates its element type(s) to the
        // arm-body literals; the rendering is parsed back from the Go type string.
        assert_eq!(
            GoEmitCtx::rendered_collection_elem("[]int64"),
            Some(("int64".to_string(), None))
        );
        assert_eq!(
            GoEmitCtx::rendered_collection_elem("map[string]int64"),
            Some(("string".to_string(), Some("int64".to_string())))
        );
        // A nested map value keeps its inner brackets balanced.
        assert_eq!(
            GoEmitCtx::rendered_collection_elem("map[string][]int64"),
            Some(("string".to_string(), Some("[]int64".to_string())))
        );
        // A non-collection type yields nothing (the IIFE stays scalar-typed).
        assert_eq!(GoEmitCtx::rendered_collection_elem("__bockOption"), None);
        assert_eq!(GoEmitCtx::rendered_collection_elem("int64"), None);
    }

    #[test]
    fn class_to_struct_with_methods() {
        let cls = node(
            1,
            NodeKind::ClassDecl {
                annotations: vec![],
                visibility: Visibility::Public,
                name: ident("Counter"),
                generic_params: vec![],
                base: None,
                traits: vec![],
                fields: vec![bock_ast::RecordDeclField {
                    id: 0,
                    span: span(),
                    name: ident("count"),
                    ty: TypeExpr::Named {
                        id: 0,
                        span: span(),
                        path: type_path(&["Int"]),
                        args: vec![],
                    },
                    default: None,
                }],
                methods: vec![node(
                    2,
                    NodeKind::FnDecl {
                        annotations: vec![],
                        visibility: Visibility::Public,
                        is_async: false,
                        name: ident("increment"),
                        generic_params: vec![],
                        // Instance method leads with `self` (real lowering).
                        params: vec![param_node(4, "self")],
                        return_type: None,
                        effect_clause: vec![],
                        where_clause: vec![],
                        body: Box::new(block(3, vec![], None)),
                    },
                )],
            },
        );
        let out = gen(&module(vec![], vec![cls]));
        assert!(out.contains("type Counter struct {"), "got: {out}");
        assert!(out.contains("Count\tint64"), "got: {out}");
        assert!(out.contains("func NewCounter("), "got: {out}");
        assert!(
            out.contains("func (self *Counter) Increment()"),
            "got: {out}"
        );
    }

    #[test]
    fn lambda_expression() {
        let lam = node(
            1,
            NodeKind::Lambda {
                params: vec![param_node(2, "x")],
                body: Box::new(node(
                    3,
                    NodeKind::BinaryOp {
                        op: BinOp::Mul,
                        left: Box::new(id_node(4, "x")),
                        right: Box::new(int_lit(5, "2")),
                    },
                )),
            },
        );
        let out = gen(&module(vec![], vec![lam]));
        assert!(
            out.contains("func(x interface{}) interface{} { return (x * 2) }"),
            "got: {out}"
        );
    }

    #[test]
    fn impl_block_methods() {
        let imp = node(
            1,
            NodeKind::ImplBlock {
                annotations: vec![],
                generic_params: vec![],
                trait_path: None,
                trait_args: vec![],
                target: Box::new(node(
                    2,
                    NodeKind::TypeNamed {
                        path: type_path(&["Point"]),
                        args: vec![],
                    },
                )),
                where_clause: vec![],
                methods: vec![node(
                    3,
                    NodeKind::FnDecl {
                        annotations: vec![],
                        visibility: Visibility::Public,
                        is_async: false,
                        name: ident("distance"),
                        generic_params: vec![],
                        // Instance method leads with `self`; a no-`self` method is
                        // an associated function (emitted as a free function).
                        params: vec![param_node(7, "self")],
                        return_type: Some(Box::new(node(
                            4,
                            NodeKind::TypeNamed {
                                path: type_path(&["Float"]),
                                args: vec![],
                            },
                        ))),
                        effect_clause: vec![],
                        where_clause: vec![],
                        body: Box::new(block(5, vec![], Some(int_lit(6, "0")))),
                    },
                )],
            },
        );
        let out = gen(&module(vec![], vec![imp]));
        assert!(
            out.contains("func (self *Point) Distance() float64 {"),
            "got: {out}"
        );
    }

    /// A plain inherent `impl` method that names `Self` in its return type must
    /// resolve `Self` to the receiver type (`Point`), not the `/* Self */`
    /// placeholder. Before P3-α item 6-go-self, `go_self_subst` was set only for
    /// trait impls (value receivers), so an inherent-impl `Self` lowered to the
    /// placeholder and produced an invalid Go signature.
    #[test]
    fn self_in_plain_impl_resolves_to_receiver_type() {
        let imp = node(
            1,
            NodeKind::ImplBlock {
                annotations: vec![],
                generic_params: vec![],
                trait_path: None,
                trait_args: vec![],
                target: Box::new(node(
                    2,
                    NodeKind::TypeNamed {
                        path: type_path(&["Point"]),
                        args: vec![],
                    },
                )),
                where_clause: vec![],
                methods: vec![node(
                    3,
                    NodeKind::FnDecl {
                        annotations: vec![],
                        visibility: Visibility::Public,
                        is_async: false,
                        name: ident("clone"),
                        generic_params: vec![],
                        // Instance method leads with `self` (real lowering).
                        params: vec![param_node(6, "self")],
                        return_type: Some(Box::new(node(4, NodeKind::TypeSelf))),
                        effect_clause: vec![],
                        where_clause: vec![],
                        body: Box::new(block(5, vec![], None)),
                    },
                )],
            },
        );
        let out = gen(&module(vec![], vec![imp]));
        assert!(
            out.contains("func (self *Point) Clone() Point {"),
            "Self should resolve to the receiver type Point, got: {out}"
        );
        assert!(
            !out.contains("/* Self */"),
            "Self placeholder must not leak, got: {out}"
        );
    }

    /// A record construction with a spread base (`Point { y: 9, ..p }`) lowers
    /// to a copy-then-override IIFE — Go has no struct-spread syntax — rather
    /// than dropping the `..p` base (P3-α item 5).
    #[test]
    fn record_spread_lowers_to_iife() {
        let spread_base = id_node(10, "p");
        let rc = node(
            1,
            NodeKind::RecordConstruct {
                path: type_path(&["Point"]),
                fields: vec![AirRecordField {
                    name: ident("y"),
                    value: Some(Box::new(int_lit(2, "9"))),
                }],
                spread: Some(Box::new(spread_base)),
            },
        );
        let out = gen(&module(vec![], vec![rc]));
        assert!(
            out.contains("func() Point { __s := p; __s.Y = 9; return __s }()"),
            "spread should copy base then override, got: {out}"
        );
        assert!(
            !out.contains("/* spread */"),
            "the dropped-spread TODO must be gone, got: {out}"
        );
    }

    #[test]
    fn concurrency_goroutine() {
        // Async function → goroutine pattern with channel.
        // The await expression maps to channel receive.
        let body = block(
            3,
            vec![],
            Some(node(
                4,
                NodeKind::Await {
                    expr: Box::new(id_node(5, "ch")),
                },
            )),
        );
        let f = node(
            1,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Public,
                is_async: true,
                name: ident("fetchData"),
                generic_params: vec![],
                params: vec![],
                return_type: None,
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(body),
            },
        );
        let out = gen(&module(vec![], vec![f]));
        assert!(out.contains("func FetchData()"), "got: {out}");
        assert!(out.contains("<-ch"), "got: {out}");
    }

    #[test]
    fn async_fn_emits_goroutine_wrapper() {
        // Async function with Int return → sync body + FnAsync wrapper
        // returning `<-chan int`.
        let body = block(3, vec![], Some(int_lit(4, "42")));
        let f = node(
            1,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Public,
                is_async: true,
                name: ident("task1"),
                generic_params: vec![],
                params: vec![],
                return_type: Some(Box::new(node(
                    5,
                    NodeKind::TypeNamed {
                        path: type_path(&["Int"]),
                        args: vec![],
                    },
                ))),
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(body),
            },
        );
        let out = gen(&module(vec![], vec![f]));
        assert!(
            out.contains("func Task1() int64 {"),
            "sync body missing: {out}"
        );
        assert!(
            out.contains("func Task1Async() <-chan int64 {"),
            "async wrapper missing: {out}"
        );
        assert!(out.contains("__ch := make(chan int64, 1)"), "got: {out}");
        assert!(out.contains("go func() {"), "got: {out}");
        assert!(out.contains("__ch <- Task1()"), "got: {out}");
        assert!(out.contains("return __ch"), "got: {out}");
    }

    /// A `public fn main` must still emit Go\'s entry `func main()`, not the
    /// PascalCased `func Main()` (codegen-correctness defect 6).
    #[test]
    fn public_main_emits_entry_point() {
        let f = node(
            1,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Public,
                is_async: false,
                name: ident("main"),
                generic_params: vec![],
                params: vec![],
                return_type: None,
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(block(2, vec![], None)),
            },
        );
        let out = gen(&module(vec![], vec![f]));
        assert!(out.contains("func main() {"), "got: {out}");
        assert!(!out.contains("func Main"), "got: {out}");
    }

    /// The Optional runtime prelude is emitted only when the module uses
    /// `Optional`/`Some`/`None` (codegen-correctness defect 4).
    #[test]
    fn optional_runtime_gated_on_use() {
        // A module that constructs `Some`/`None` pulls in the runtime.
        let some_call = node(
            10,
            NodeKind::Call {
                callee: Box::new(id_node(11, "Some")),
                args: vec![AirArg {
                    label: None,
                    value: int_lit(12, "1"),
                }],
                type_args: vec![],
            },
        );
        let f = node(
            1,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Private,
                is_async: false,
                name: ident("main"),
                generic_params: vec![],
                params: vec![],
                return_type: None,
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(block(2, vec![some_call], None)),
            },
        );
        let out = gen(&module(vec![], vec![f]));
        assert!(out.contains("type __bockOption struct"), "got: {out}");
        assert!(out.contains("__bockSome("), "got: {out}");

        // A module that does not mention Optional gets no prelude.
        let f2 = node(
            1,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Private,
                is_async: false,
                name: ident("main"),
                generic_params: vec![],
                params: vec![],
                return_type: None,
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(block(2, vec![], None)),
            },
        );
        let out2 = gen(&module(vec![], vec![f2]));
        assert!(!out2.contains("__bockOption"), "got: {out2}");
    }

    #[test]
    fn result_runtime_gated_and_constructed() {
        // A module that constructs `Ok`/`Err` pulls in the Result runtime + the
        // shared numeric helpers, and lowers the constructors to `__bockOk`/
        // `__bockErr` (not the old `v, nil` multi-return).
        let ok_call = node(
            10,
            NodeKind::Call {
                callee: Box::new(id_node(11, "Ok")),
                args: vec![AirArg {
                    label: None,
                    value: int_lit(12, "7"),
                }],
                type_args: vec![],
            },
        );
        let f = node(
            1,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Private,
                is_async: false,
                name: ident("main"),
                generic_params: vec![],
                params: vec![],
                return_type: None,
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(block(2, vec![ok_call], None)),
            },
        );
        let out = gen(&module(vec![], vec![f]));
        assert!(out.contains("type __bockResult struct"), "got: {out}");
        assert!(out.contains("__bockOk("), "got: {out}");
        // The shared numeric helpers are emitted exactly once.
        assert_eq!(
            out.matches("func __bockAsInt64").count(),
            1,
            "numeric helpers must be emitted once; got: {out}"
        );
    }

    /// The Go Optional runtime stores the `Some` payload as `interface{}`. A
    /// `match` arm binding it (`Some(x)`) must type-assert to the scrutinee's
    /// concrete element type so typed use (`x + 10`) compiles. The element type
    /// is resolved structurally from the `Optional[T]` parameter scrutinee.
    #[test]
    fn optional_match_some_payload_type_asserted() {
        // fn addTen(o: Int?) -> Int { match o { Some(x) => return x; None => return 0 } }
        let opt_int_ty = node(
            200,
            NodeKind::TypeOptional {
                inner: Box::new(node(
                    201,
                    NodeKind::TypeNamed {
                        path: type_path(&["Int"]),
                        args: vec![],
                    },
                )),
            },
        );
        let o_param = node(
            30,
            NodeKind::Param {
                pattern: Box::new(bind_pat(31, "o")),
                ty: Some(Box::new(opt_int_ty)),
                default: None,
            },
        );
        let some_arm = node(
            40,
            NodeKind::MatchArm {
                pattern: Box::new(node(
                    41,
                    NodeKind::ConstructorPat {
                        path: type_path(&["Some"]),
                        fields: vec![bind_pat(42, "x")],
                    },
                )),
                guard: None,
                body: Box::new(block(
                    43,
                    vec![node(
                        44,
                        NodeKind::Return {
                            value: Some(Box::new(id_node(45, "x"))),
                        },
                    )],
                    None,
                )),
            },
        );
        let none_arm = node(
            50,
            NodeKind::MatchArm {
                pattern: Box::new(node(
                    51,
                    NodeKind::ConstructorPat {
                        path: type_path(&["None"]),
                        fields: vec![],
                    },
                )),
                guard: None,
                body: Box::new(block(
                    52,
                    vec![node(
                        53,
                        NodeKind::Return {
                            value: Some(Box::new(int_lit(54, "0"))),
                        },
                    )],
                    None,
                )),
            },
        );
        let match_stmt = node(
            60,
            NodeKind::Match {
                scrutinee: Box::new(id_node(61, "o")),
                arms: vec![some_arm, none_arm],
            },
        );
        let f = node(
            1,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Private,
                is_async: false,
                name: ident("addTen"),
                generic_params: vec![],
                params: vec![o_param],
                return_type: Some(Box::new(node(
                    2,
                    NodeKind::TypeNamed {
                        path: type_path(&["Int"]),
                        args: vec![],
                    },
                ))),
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(block(3, vec![match_stmt], None)),
            },
        );
        let out = gen(&module(vec![], vec![f]));
        // The `Int` element type is recovered through the widening helper
        // `__bockAsInt64` rather than a hard `.(int64)` assertion: a payload
        // boxed from an untyped Go constant (`Some(10)`) is a Go `int`, on which
        // `.(int64)` panics at runtime.
        assert!(
            out.contains("x := __bockAsInt64(__opt.v);"),
            "Some payload should be recovered via the int64 widening helper, got: {out}"
        );
    }

    /// Build an `impl Counter { fn next(self) -> Int? { ... } }` whose method
    /// has an `Optional[Int]` return type, plus a `match it.next() { Some(x) =>
    /// return x; None => return 0 }` driver function. Used to exercise the
    /// method-call-scrutinee payload resolution (the `core.iter` desugar shape).
    fn iterator_module_with_method_match() -> AIRNode {
        let opt_int_ty = node(
            200,
            NodeKind::TypeOptional {
                inner: Box::new(node(
                    201,
                    NodeKind::TypeNamed {
                        path: type_path(&["Int"]),
                        args: vec![],
                    },
                )),
            },
        );
        let next_method = node(
            10,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Private,
                is_async: false,
                name: ident("next"),
                generic_params: vec![],
                params: vec![param_node(11, "self")],
                return_type: Some(Box::new(opt_int_ty)),
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(block(
                    12,
                    vec![node(
                        13,
                        NodeKind::Return {
                            value: Some(Box::new(node(
                                14,
                                NodeKind::Call {
                                    callee: Box::new(id_node(15, "None")),
                                    args: vec![],
                                    type_args: vec![],
                                },
                            ))),
                        },
                    )],
                    None,
                )),
            },
        );
        let imp = node(
            5,
            NodeKind::ImplBlock {
                annotations: vec![],
                generic_params: vec![],
                trait_path: None,
                trait_args: vec![],
                target: Box::new(node(
                    6,
                    NodeKind::TypeNamed {
                        path: type_path(&["Counter"]),
                        args: vec![],
                    },
                )),
                where_clause: vec![],
                methods: vec![next_method],
            },
        );
        // fn drive(it: Counter) -> Int {
        //   match it.next() { Some(x) => return x; None => return 0 }
        // }
        let scrutinee = node(
            60,
            NodeKind::MethodCall {
                receiver: Box::new(id_node(61, "it")),
                method: ident("next"),
                type_args: vec![],
                args: vec![],
            },
        );
        let some_arm = node(
            40,
            NodeKind::MatchArm {
                pattern: Box::new(node(
                    41,
                    NodeKind::ConstructorPat {
                        path: type_path(&["Some"]),
                        fields: vec![bind_pat(42, "x")],
                    },
                )),
                guard: None,
                body: Box::new(block(
                    43,
                    vec![node(
                        44,
                        NodeKind::Return {
                            value: Some(Box::new(id_node(45, "x"))),
                        },
                    )],
                    None,
                )),
            },
        );
        let none_arm = node(
            50,
            NodeKind::MatchArm {
                pattern: Box::new(node(
                    51,
                    NodeKind::ConstructorPat {
                        path: type_path(&["None"]),
                        fields: vec![],
                    },
                )),
                guard: None,
                body: Box::new(block(
                    52,
                    vec![node(
                        53,
                        NodeKind::Return {
                            value: Some(Box::new(int_lit(54, "0"))),
                        },
                    )],
                    None,
                )),
            },
        );
        let match_stmt = node(
            70,
            NodeKind::Match {
                scrutinee: Box::new(scrutinee),
                arms: vec![some_arm, none_arm],
            },
        );
        let drive = node(
            80,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Private,
                is_async: false,
                name: ident("drive"),
                generic_params: vec![],
                params: vec![typed_param_node(81, "it", "Counter")],
                return_type: Some(Box::new(node(
                    82,
                    NodeKind::TypeNamed {
                        path: type_path(&["Int"]),
                        args: vec![],
                    },
                ))),
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(block(83, vec![match_stmt], None)),
            },
        );
        module(vec![], vec![imp, drive])
    }

    #[test]
    fn optional_match_method_call_scrutinee_payload_resolved() {
        // The scrutinee `it.next()` is a method call whose method returns
        // `Int?`; the bound `Some` payload must be recovered as `int64` (via the
        // widening helper), not left as bare `interface{}`. This is the
        // `core.iter` `for x in <Iterable>` desugar shape — regression-locking
        // the Go method-call-scrutinee defect.
        let out = gen(&iterator_module_with_method_match());
        assert!(
            out.contains("x := __bockAsInt64(__opt.v);"),
            "method-call-scrutinee Some payload should be resolved to int64, got: {out}"
        );
    }

    /// Build a `loop { match it.next() { Some(x) => { ... } None => break } }`
    /// driver — the exact statement-position desugar shape, where the 2-arm
    /// Optional match lowers to `if/else` and a bare `break` already exits the
    /// `for`. No loop label may be allocated (Go rejects an unused label).
    fn loop_with_optional_match_break() -> AIRNode {
        let scrutinee = node(
            60,
            NodeKind::MethodCall {
                receiver: Box::new(id_node(61, "it")),
                method: ident("next"),
                type_args: vec![],
                args: vec![],
            },
        );
        let some_arm = node(
            40,
            NodeKind::MatchArm {
                pattern: Box::new(node(
                    41,
                    NodeKind::ConstructorPat {
                        path: type_path(&["Some"]),
                        fields: vec![bind_pat(42, "x")],
                    },
                )),
                guard: None,
                // Some(x) => { sum = sum + x } — a statement-style arm body.
                body: Box::new(block(
                    43,
                    vec![node(
                        44,
                        NodeKind::Assign {
                            op: AssignOp::Assign,
                            target: Box::new(id_node(45, "sum")),
                            value: Box::new(node(
                                46,
                                NodeKind::BinaryOp {
                                    op: BinOp::Add,
                                    left: Box::new(id_node(47, "sum")),
                                    right: Box::new(id_node(48, "x")),
                                },
                            )),
                        },
                    )],
                    None,
                )),
            },
        );
        let none_arm = node(
            50,
            NodeKind::MatchArm {
                pattern: Box::new(node(
                    51,
                    NodeKind::ConstructorPat {
                        path: type_path(&["None"]),
                        fields: vec![],
                    },
                )),
                guard: None,
                body: Box::new(block(
                    52,
                    vec![node(53, NodeKind::Break { value: None })],
                    None,
                )),
            },
        );
        let match_stmt = node(
            70,
            NodeKind::Match {
                scrutinee: Box::new(scrutinee),
                arms: vec![some_arm, none_arm],
            },
        );
        let loop_node = node(
            71,
            NodeKind::Loop {
                body: Box::new(block(72, vec![match_stmt], None)),
            },
        );
        let f = node(
            80,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Private,
                is_async: false,
                name: ident("run"),
                generic_params: vec![],
                params: vec![],
                return_type: None,
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(block(81, vec![loop_node], None)),
            },
        );
        module(vec![], vec![f])
    }

    #[test]
    fn optional_match_break_loop_has_no_unused_label() {
        // A 2-arm Some/None match lowers to `if __opt.tag == "Some" { ... } else
        // { break }`; the bare `break` already exits the `for`, so no
        // `__bockLoopN:` label must be emitted (Go errors on an unused label).
        let out = gen(&loop_with_optional_match_break());
        assert!(
            !out.contains("__bockLoop"),
            "Optional match-in-loop must not allocate an unused loop label, got: {out}"
        );
        // The bare `break` is still present and targets the enclosing `for`.
        assert!(out.contains("break"), "expected a break, got: {out}");
    }

    #[test]
    fn go_loop_label_skipped_for_optional_match_but_kept_for_switch_match() {
        // An Optional match (`Some`/`None`) lowers to if/else: bare break ⇒ no
        // label needed.
        let opt_break = node(
            1,
            NodeKind::Match {
                scrutinee: Box::new(id_node(2, "o")),
                arms: vec![
                    node(
                        3,
                        NodeKind::MatchArm {
                            pattern: Box::new(node(
                                4,
                                NodeKind::ConstructorPat {
                                    path: type_path(&["Some"]),
                                    fields: vec![bind_pat(5, "x")],
                                },
                            )),
                            guard: None,
                            body: Box::new(block(6, vec![], Some(id_node(7, "x")))),
                        },
                    ),
                    node(
                        8,
                        NodeKind::MatchArm {
                            pattern: Box::new(node(
                                9,
                                NodeKind::ConstructorPat {
                                    path: type_path(&["None"]),
                                    fields: vec![],
                                },
                            )),
                            guard: None,
                            body: Box::new(block(
                                10,
                                vec![node(11, NodeKind::Break { value: None })],
                                None,
                            )),
                        },
                    ),
                ],
            },
        );
        assert!(
            !go_loop_needs_label(&opt_break),
            "Optional match-in-loop should not need a label"
        );
        // A non-Optional value-switch match with a `break` arm DOES need a label
        // (bare break would exit the Go switch, not the loop).
        let switch_break = node(
            20,
            NodeKind::Match {
                scrutinee: Box::new(id_node(21, "i")),
                arms: vec![
                    node(
                        22,
                        NodeKind::MatchArm {
                            pattern: Box::new(node(
                                23,
                                NodeKind::LiteralPat {
                                    lit: Literal::Int("5".into()),
                                },
                            )),
                            guard: None,
                            body: Box::new(block(
                                24,
                                vec![node(25, NodeKind::Break { value: None })],
                                None,
                            )),
                        },
                    ),
                    node(
                        26,
                        NodeKind::MatchArm {
                            pattern: Box::new(node(27, NodeKind::WildcardPat)),
                            guard: None,
                            body: Box::new(block(28, vec![], None)),
                        },
                    ),
                ],
            },
        );
        assert!(
            go_loop_needs_label(&switch_break),
            "non-Optional switch match with break should need a label"
        );
    }

    #[test]
    fn async_main_no_wrapper() {
        // main is Go's entry — skip the wrapper to avoid dead code.
        let body = block(2, vec![], None);
        let f = node(
            1,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Private,
                is_async: true,
                name: ident("main"),
                generic_params: vec![],
                params: vec![],
                return_type: None,
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(body),
            },
        );
        let out = gen(&module(vec![], vec![f]));
        assert!(out.contains("func main() {"), "got: {out}");
        assert!(!out.contains("mainAsync"), "got: {out}");
    }

    #[test]
    fn async_call_rewritten_to_async_wrapper() {
        // Calling `task1()` from another async fn should route through
        // `Task1Async()` so callers can `await` (= `<-`) the channel.
        let task1 = node(
            10,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Public,
                is_async: true,
                name: ident("task1"),
                generic_params: vec![],
                params: vec![],
                return_type: Some(Box::new(node(
                    11,
                    NodeKind::TypeNamed {
                        path: type_path(&["Int"]),
                        args: vec![],
                    },
                ))),
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(block(12, vec![], Some(int_lit(13, "1")))),
            },
        );
        // caller body: let a = task1(); let b = task1(); await a; await b
        let call_task1 = |id: u32| {
            node(
                id,
                NodeKind::Call {
                    callee: Box::new(id_node(id + 1, "task1")),
                    args: vec![],
                    type_args: vec![],
                },
            )
        };
        let let_stmt = |id: u32, name: &str, val: AIRNode| {
            node(
                id,
                NodeKind::LetBinding {
                    is_mut: false,
                    pattern: Box::new(bind_pat(id + 1, name)),
                    ty: None,
                    value: Box::new(val),
                },
            )
        };
        let await_id = |id: u32, name: &str| {
            node(
                id,
                NodeKind::Await {
                    expr: Box::new(id_node(id + 1, name)),
                },
            )
        };
        let caller_body = block(
            20,
            vec![
                let_stmt(30, "a", call_task1(31)),
                let_stmt(40, "b", call_task1(41)),
                let_stmt(50, "ra", await_id(51, "a")),
                let_stmt(60, "rb", await_id(61, "b")),
            ],
            None,
        );
        let caller = node(
            100,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Private,
                is_async: true,
                name: ident("run"),
                generic_params: vec![],
                params: vec![],
                return_type: None,
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(caller_body),
            },
        );
        let out = gen(&module(vec![], vec![task1, caller]));
        // Concurrent goroutines: both bindings start channels.
        assert!(out.contains("a := Task1Async()"), "got: {out}");
        assert!(out.contains("b := Task1Async()"), "got: {out}");
        // Awaits receive from the channels.
        assert!(out.contains("ra := <-a"), "got: {out}");
        assert!(out.contains("rb := <-b"), "got: {out}");
    }

    #[test]
    fn break_continue() {
        let brk = node(1, NodeKind::Break { value: None });
        let cont = node(2, NodeKind::Continue);
        let out = gen(&module(vec![], vec![brk, cont]));
        assert!(out.contains("break"), "got: {out}");
        assert!(out.contains("continue"), "got: {out}");
    }

    #[test]
    fn guard_statement() {
        let g = node(
            1,
            NodeKind::Guard {
                let_pattern: None,
                condition: Box::new(bool_lit(2, true)),
                else_block: Box::new(block(
                    3,
                    vec![node(4, NodeKind::Return { value: None })],
                    None,
                )),
            },
        );
        let out = gen(&module(vec![], vec![g]));
        assert!(out.contains("if !(true)"), "got: {out}");
    }

    #[test]
    fn ownership_erased() {
        let borrow = node(
            1,
            NodeKind::Borrow {
                expr: Box::new(id_node(2, "x")),
            },
        );
        let mv = node(
            3,
            NodeKind::Move {
                expr: Box::new(id_node(4, "y")),
            },
        );
        let out = gen(&module(vec![], vec![borrow, mv]));
        assert!(out.contains("x"), "got: {out}");
        assert!(out.contains("y"), "got: {out}");
        // Should NOT contain borrow/move keywords.
        assert!(!out.contains("&x"), "got: {out}");
    }

    #[test]
    fn type_mapping() {
        let ctx = GoEmitCtx::new();
        assert_eq!(ctx.map_type_name("Int"), "int64");
        assert_eq!(ctx.map_type_name("Float"), "float64");
        assert_eq!(ctx.map_type_name("Bool"), "bool");
        assert_eq!(ctx.map_type_name("String"), "string");
        assert_eq!(ctx.map_type_name("Char"), "rune");
        assert_eq!(ctx.map_type_name("Void"), "struct{}");
        assert_eq!(ctx.map_type_name("Any"), "interface{}");
    }

    #[test]
    fn parse_tuple_struct_field_types_round_trips_type_to_go() {
        // Inverse of `type_to_go`'s `TypeTuple` arm: parse the per-field Go types
        // back out of the rendered `struct{ Field0 T0; Field1 T1 }`.
        assert_eq!(
            GoEmitCtx::parse_tuple_struct_field_types("struct{ Field0 int64; Field1 int64 }"),
            vec!["int64".to_string(), "int64".to_string()]
        );
        assert_eq!(
            GoEmitCtx::parse_tuple_struct_field_types("struct{ Field0 int64; Field1 string }"),
            vec!["int64".to_string(), "string".to_string()]
        );
        // A non-tuple-struct string yields no fields (callers fall back to
        // element inference).
        assert!(GoEmitCtx::parse_tuple_struct_field_types("[]int64").is_empty());
        assert!(GoEmitCtx::parse_tuple_struct_field_types("int64").is_empty());
    }

    /// Build a `TypeNamed { path: [name], args }` AIR node.
    fn type_named(name: &str, args: Vec<AIRNode>) -> AIRNode {
        node(
            900,
            NodeKind::TypeNamed {
                path: type_path(&[name]),
                args,
            },
        )
    }

    /// The three collection types emit a concrete Go container with their
    /// element/key/value types recovered recursively, NOT the erased
    /// `interface{}` element (P3-α item 1a).
    #[test]
    fn type_to_go_collections_carry_element_types() {
        let ctx = GoEmitCtx::new();
        let int_ty = || type_named("Int", vec![]);
        let str_ty = || type_named("String", vec![]);

        assert_eq!(
            ctx.type_to_go(&type_named("List", vec![int_ty()])),
            "[]int64"
        );
        assert_eq!(
            ctx.type_to_go(&type_named("Set", vec![int_ty()])),
            "map[int64]struct{}"
        );
        assert_eq!(
            ctx.type_to_go(&type_named("Map", vec![str_ty(), int_ty()])),
            "map[string]int64"
        );
        // Recursive: a list of maps.
        let inner_map = type_named("Map", vec![str_ty(), int_ty()]);
        assert_eq!(
            ctx.type_to_go(&type_named("List", vec![inner_map])),
            "[]map[string]int64"
        );
        // A bare collection with no type arg keeps the erased element.
        assert_eq!(ctx.type_to_go(&type_named("List", vec![])), "[]interface{}");
    }

    /// Lifting the collection element type must NOT disturb the genuine runtime
    /// structs `Optional`/`Result`, which still erase their payload to the
    /// tagged runtime struct (`__bockOption` / `__bockResult`) — the regression
    /// the P3-α item 1a change was warned against.
    #[test]
    fn type_to_go_runtime_structs_unchanged() {
        let ctx = GoEmitCtx::new();
        let int_ty = || type_named("Int", vec![]);
        let str_ty = || type_named("String", vec![]);
        assert_eq!(
            ctx.type_to_go(&type_named("Optional", vec![int_ty()])),
            "__bockOption"
        );
        assert_eq!(
            ctx.type_to_go(&type_named("Result", vec![int_ty(), str_ty()])),
            "__bockResult"
        );
    }

    /// `optional_inner_type_node` / `result_inner_type_nodes` peel one container
    /// layer for the nested-pattern declared-type threading: an
    /// `Optional[Result[(Int, Int), String]]` peels to its `Result[…]`, which
    /// peels to the `(Int, Int)` tuple and the `String` err. Rendering the peeled
    /// tuple node reproduces the concrete struct the nested tuple payload is
    /// asserted to.
    #[test]
    fn peel_optional_result_tuple_decl_type_nodes() {
        let ctx = GoEmitCtx::new();
        let int_ty = || type_named("Int", vec![]);
        let str_ty = || type_named("String", vec![]);
        let tuple_ty = node(
            910,
            NodeKind::TypeTuple {
                elems: vec![int_ty(), int_ty()],
            },
        );
        let result_ty = type_named("Result", vec![tuple_ty, str_ty()]);
        let opt_ty = type_named("Optional", vec![result_ty]);

        // Optional → Result.
        let inner = ctx
            .optional_inner_type_node(&opt_ty)
            .expect("peels Optional");
        assert!(matches!(inner.kind, NodeKind::TypeNamed { .. }));
        // Result → (tuple ok, string err).
        let (ok, err) = ctx.result_inner_type_nodes(inner).expect("peels Result");
        assert_eq!(
            ctx.type_to_go(ok),
            "struct{ Field0 int64; Field1 int64 }",
            "the Ok payload is the concrete tuple struct"
        );
        assert_eq!(ctx.type_to_go(err.expect("err arg present")), "string");
        // A non-container type peels to nothing.
        assert!(ctx.optional_inner_type_node(&int_ty()).is_none());
        assert!(ctx.result_inner_type_nodes(&int_ty()).is_none());
    }

    /// `peel_constructor_decl_ty` maps a tag to the inner declared type it carries
    /// (`Some`/`Ok` → ok/elem, `Err` → err), and `tuple_field_decl_tys` splits a
    /// declared tuple type into per-field nodes (or `None` on arity mismatch).
    #[test]
    fn constructor_and_tuple_decl_type_peeling() {
        let ctx = GoEmitCtx::new();
        let int_ty = || type_named("Int", vec![]);
        let str_ty = || type_named("String", vec![]);
        let result_ty = type_named("Result", vec![int_ty(), str_ty()]);
        let opt_ty = type_named("Optional", vec![result_ty.clone()]);

        // `Some` peels Optional → Result (a runtime container, renders __bockResult).
        let some_inner = ctx
            .peel_constructor_decl_ty("Some", Some(&opt_ty))
            .expect("Some peels Optional");
        assert_eq!(ctx.type_to_go(&some_inner), "__bockResult");
        // `Ok` peels Result → Int; `Err` → String.
        let ok_inner = ctx
            .peel_constructor_decl_ty("Ok", Some(&result_ty))
            .expect("Ok peels Result");
        assert_eq!(ctx.type_to_go(&ok_inner), "int64");
        let err_inner = ctx
            .peel_constructor_decl_ty("Err", Some(&result_ty))
            .expect("Err peels Result");
        assert_eq!(ctx.type_to_go(&err_inner), "string");
        // Unknown declared type ⇒ no peel.
        assert!(ctx.peel_constructor_decl_ty("Some", None).is_none());

        // A 2-tuple splits into two field nodes; an arity mismatch yields Nones.
        let tuple_ty = node(
            911,
            NodeKind::TypeTuple {
                elems: vec![int_ty(), str_ty()],
            },
        );
        let fields = ctx.tuple_field_decl_tys(Some(&tuple_ty), 2);
        assert_eq!(fields.len(), 2);
        assert_eq!(ctx.type_to_go(fields[0].expect("field 0")), "int64");
        assert_eq!(ctx.type_to_go(fields[1].expect("field 1")), "string");
        // Arity mismatch / unknown ⇒ all None.
        assert!(ctx
            .tuple_field_decl_tys(Some(&tuple_ty), 3)
            .iter()
            .all(Option::is_none));
        assert!(ctx
            .tuple_field_decl_tys(None, 2)
            .iter()
            .all(Option::is_none));
    }

    /// `fn_type_go_signature` renders a declared `Fn(Int) -> Int` to its Go
    /// param/return types, used to type a lambda returned in tail position;
    /// `fn_type_ret_node` only keeps a function-typed return.
    #[test]
    fn fn_type_signature_for_returned_lambda() {
        let ctx = GoEmitCtx::new();
        let int_ty = || type_named("Int", vec![]);
        let fn_ty = node(
            912,
            NodeKind::TypeFunction {
                params: vec![int_ty()],
                ret: Box::new(int_ty()),
                effects: Vec::new(),
            },
        );
        let (params, ret) = ctx.fn_type_go_signature(&fn_ty).expect("is a fn type");
        assert_eq!(params, vec!["int64".to_string()]);
        assert_eq!(ret, "int64");
        // A non-function return type yields no signature / no kept node.
        assert!(ctx.fn_type_go_signature(&int_ty()).is_none());
        assert!(GoEmitCtx::fn_type_ret_node(Some(&int_ty())).is_none());
        assert!(GoEmitCtx::fn_type_ret_node(Some(&fn_ty)).is_some());
    }

    /// `payload_access_go` asserts an Optional/Result *leaf* payload bind to its
    /// concrete element type — numeric via the widening helpers, others via a
    /// direct assertion — and reads the raw payload for unit/unknown types (a hard
    /// assertion on a boxed `nil` would panic).
    #[test]
    fn payload_access_typed_leaf_bind() {
        let ctx = GoEmitCtx::new();
        assert_eq!(
            ctx.payload_access_go("opt", Some("int64")),
            "__bockAsInt64(opt.v)"
        );
        assert_eq!(
            ctx.payload_access_go("opt", Some("float64")),
            "__bockAsFloat64(opt.v)"
        );
        assert_eq!(
            ctx.payload_access_go("res", Some("string")),
            "res.v.(string)"
        );
        assert_eq!(ctx.payload_access_go("opt", Some("struct{}")), "opt.v");
        assert_eq!(ctx.payload_access_go("opt", None), "opt.v");
    }

    /// `specialise_lambda_param_types` sees through a `type` alias to a function
    /// type so a lambda argument bound to a `Predicate = Fn(Int) -> Bool`
    /// parameter is typed `func(x int64) bool`, not the erased `interface{}`.
    #[test]
    fn lambda_param_types_see_through_fn_type_alias() {
        let mut ctx = GoEmitCtx::new();
        let int_ty = || type_named("Int", vec![]);
        let bool_ty = || type_named("Bool", vec![]);
        let fn_ty = node(
            913,
            NodeKind::TypeFunction {
                params: vec![int_ty()],
                ret: Box::new(bool_ty()),
                effects: Vec::new(),
            },
        );
        // Register `type Predicate = Fn(Int) -> Bool`.
        ctx.type_aliases.insert("Predicate".to_string(), fn_ty);
        let alias = type_named("Predicate", vec![]);
        let tys = ctx
            .specialise_lambda_param_types(&alias, &[], &HashMap::new())
            .expect("alias resolves to a fn type");
        assert_eq!(tys, vec!["int64".to_string()]);
    }

    #[test]
    fn naming_conventions() {
        assert_eq!(to_camel_case("hello_world"), "helloWorld");
        assert_eq!(to_camel_case("HelloWorld"), "helloWorld");
        assert_eq!(to_camel_case("already"), "already");
        assert_eq!(to_pascal_case("hello_world"), "HelloWorld");
        assert_eq!(to_pascal_case("helloWorld"), "HelloWorld");
        assert_eq!(to_pascal_case("Already"), "Already");
    }

    #[test]
    fn escape_go_string_special_chars() {
        assert_eq!(escape_go_string("hello\nworld"), "hello\\nworld");
        assert_eq!(escape_go_string("tab\there"), "tab\\there");
        assert_eq!(escape_go_string("quote\"here"), "quote\\\"here");
    }

    // ── End-to-end: syntax validation ───────────────────────────────────────

    #[test]
    #[ignore] // requires `go` to be installed
    fn generated_go_passes_vet() {
        let body = block(
            2,
            vec![],
            Some(node(
                3,
                NodeKind::Interpolation {
                    parts: vec![
                        AirInterpolationPart::Literal("Hello, ".into()),
                        AirInterpolationPart::Expr(Box::new(id_node(4, "name"))),
                    ],
                },
            )),
        );
        let f = node(
            1,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Public,
                is_async: false,
                name: ident("greet"),
                generic_params: vec![],
                params: vec![typed_param_node(5, "name", "String")],
                return_type: Some(Box::new(node(
                    6,
                    NodeKind::TypeNamed {
                        path: type_path(&["String"]),
                        args: vec![],
                    },
                ))),
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(body),
            },
        );
        let code = gen(&module(vec![], vec![f]));

        // Write to temp file and run go vet.
        let dir = std::env::temp_dir().join("bock_go_test");
        let _ = std::fs::create_dir_all(&dir);
        let file_path = dir.join("output.go");
        std::fs::write(&file_path, &code).unwrap();

        let output = std::process::Command::new("go")
            .args(["vet", file_path.to_str().unwrap()])
            .output();
        match output {
            Ok(o) => {
                if !o.status.success() {
                    let stderr = String::from_utf8_lossy(&o.stderr);
                    panic!("go vet failed:\n{stderr}\n\nGenerated code:\n{code}");
                }
            }
            Err(e) => {
                panic!("Failed to run go vet: {e}");
            }
        }
        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    #[ignore] // requires `go` to be installed
    fn generated_go_compiles_and_runs() {
        // Build a complete Go program that prints "42".
        let body = block(
            2,
            vec![node(
                3,
                NodeKind::LetBinding {
                    is_mut: false,
                    pattern: Box::new(bind_pat(4, "x")),
                    ty: None,
                    value: Box::new(int_lit(5, "42")),
                },
            )],
            None,
        );
        let main_fn = node(
            1,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Private,
                is_async: false,
                name: ident("main"),
                generic_params: vec![],
                params: vec![],
                return_type: None,
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(body),
            },
        );
        let code = gen(&module(vec![], vec![main_fn]));

        let dir = std::env::temp_dir().join("bock_go_run_test");
        let _ = std::fs::create_dir_all(&dir);
        let file_path = dir.join("main.go");
        std::fs::write(&file_path, &code).unwrap();

        let output = std::process::Command::new("go")
            .args(["build", file_path.to_str().unwrap()])
            .current_dir(&dir)
            .output();
        match output {
            Ok(o) => {
                if !o.status.success() {
                    let stderr = String::from_utf8_lossy(&o.stderr);
                    panic!("go build failed:\n{stderr}\n\nGenerated code:\n{code}");
                }
            }
            Err(e) => {
                panic!("Failed to run go build: {e}");
            }
        }
        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn expr_match_no_unused_var() {
        // Expression-position match should not emit unused `__v`.
        let match_expr = node(
            1,
            NodeKind::Match {
                scrutinee: Box::new(id_node(2, "x")),
                arms: vec![
                    node(
                        3,
                        NodeKind::MatchArm {
                            pattern: Box::new(node(
                                4,
                                NodeKind::LiteralPat {
                                    lit: Literal::Int("1".into()),
                                },
                            )),
                            guard: None,
                            body: Box::new(block(5, vec![], Some(str_lit(6, "one")))),
                        },
                    ),
                    node(
                        7,
                        NodeKind::MatchArm {
                            pattern: Box::new(node(8, NodeKind::WildcardPat)),
                            guard: None,
                            body: Box::new(block(9, vec![], Some(str_lit(10, "other")))),
                        },
                    ),
                ],
            },
        );
        // Emit in expression context via a let binding.
        let let_node = node(
            20,
            NodeKind::LetBinding {
                is_mut: false,
                pattern: Box::new(bind_pat(21, "result")),
                ty: None,
                value: Box::new(match_expr),
            },
        );
        let out = gen(&module(vec![], vec![let_node]));
        assert!(
            !out.contains("__v"),
            "expression-position match should not emit __v, got: {out}"
        );
        assert!(
            out.contains("switch x"),
            "should emit switch with scrutinee directly, got: {out}"
        );
    }

    // ── Prelude function mapping tests ──────────────────────────────────────

    /// Helper: generate Go for a module with a `main` function containing a single call.
    fn gen_prelude_call(func_name: &str, arg: AIRNode) -> String {
        let call = node(
            10,
            NodeKind::Call {
                callee: Box::new(id_node(11, func_name)),
                args: vec![AirArg {
                    label: None,
                    value: arg,
                }],
                type_args: vec![],
            },
        );
        let body = block(2, vec![call], None);
        let f = node(
            1,
            NodeKind::FnDecl {
                name: ident("main"),
                params: vec![],
                return_type: None,
                body: Box::new(body),
                generic_params: vec![],
                visibility: Visibility::Private,
                annotations: vec![],
                effect_clause: vec![],
                where_clause: vec![],
                is_async: false,
            },
        );
        gen(&module(vec![], vec![f]))
    }

    /// Helper: generate Go for a nullary prelude call (no args).
    fn gen_prelude_call_no_args(func_name: &str) -> String {
        let call = node(
            10,
            NodeKind::Call {
                callee: Box::new(id_node(11, func_name)),
                args: vec![],
                type_args: vec![],
            },
        );
        let body = block(2, vec![call], None);
        let f = node(
            1,
            NodeKind::FnDecl {
                name: ident("main"),
                params: vec![],
                return_type: None,
                body: Box::new(body),
                generic_params: vec![],
                visibility: Visibility::Private,
                annotations: vec![],
                effect_clause: vec![],
                where_clause: vec![],
                is_async: false,
            },
        );
        gen(&module(vec![], vec![f]))
    }

    #[test]
    fn prelude_println_maps_to_fmt_println() {
        let out = gen_prelude_call("println", str_lit(12, "hello"));
        assert!(
            out.contains("fmt.Println("),
            "println should map to fmt.Println, got: {out}"
        );
        assert!(
            !out.contains("println("),
            "should not emit bare println(, got: {out}"
        );
    }

    #[test]
    fn prelude_print_maps_to_fmt_print() {
        let out = gen_prelude_call("print", str_lit(12, "hello"));
        assert!(
            out.contains("fmt.Print("),
            "print should map to fmt.Print, got: {out}"
        );
    }

    #[test]
    fn prelude_debug_maps_to_fmt_printf() {
        let out = gen_prelude_call("debug", str_lit(12, "val"));
        assert!(
            out.contains("fmt.Printf(\"%+v\\n\", "),
            "debug should map to fmt.Printf, got: {out}"
        );
    }

    #[test]
    fn prelude_assert_maps_to_panic() {
        let out = gen_prelude_call("assert", bool_lit(12, true));
        assert!(
            out.contains("if !true { panic(\"assertion failed\") }"),
            "assert should map to if-panic, got: {out}"
        );
    }

    #[test]
    fn prelude_todo_maps_to_panic_not_implemented() {
        let out = gen_prelude_call_no_args("todo");
        assert!(
            out.contains("panic(\"not implemented\")"),
            "todo should map to panic, got: {out}"
        );
    }

    #[test]
    fn prelude_unreachable_maps_to_panic_unreachable() {
        let out = gen_prelude_call_no_args("unreachable");
        assert!(
            out.contains("panic(\"unreachable\")"),
            "unreachable should map to panic, got: {out}"
        );
    }

    #[test]
    fn non_prelude_call_passes_through() {
        let out = gen_prelude_call("my_custom_func", str_lit(12, "arg"));
        assert!(
            out.contains("myCustomFunc("),
            "non-prelude call should use camelCase, got: {out}"
        );
    }

    #[test]
    fn handling_block_passes_handlers_to_effectful_call() {
        use bock_air::AirHandlerPair;

        let effect_decl = node(
            1,
            NodeKind::EffectDecl {
                annotations: vec![],
                visibility: Visibility::Public,
                name: ident("Logger"),
                generic_params: vec![],
                components: vec![],
                operations: vec![node(
                    2,
                    NodeKind::FnDecl {
                        annotations: vec![],
                        visibility: Visibility::Public,
                        is_async: false,
                        name: ident("log"),
                        generic_params: vec![],
                        params: vec![typed_param_node(3, "msg", "String")],
                        return_type: None,
                        effect_clause: vec![],
                        where_clause: vec![],
                        body: Box::new(block(4, vec![], None)),
                    },
                )],
            },
        );

        let inner_fn = node(
            10,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Private,
                is_async: false,
                name: ident("inner"),
                generic_params: vec![],
                params: vec![],
                return_type: None,
                effect_clause: vec![type_path(&["Logger"])],
                where_clause: vec![],
                body: Box::new(block(12, vec![], Some(str_lit(13, "hello")))),
            },
        );

        let call_inner = node(
            20,
            NodeKind::Call {
                callee: Box::new(id_node(21, "inner")),
                args: vec![],
                type_args: vec![],
            },
        );
        let handling = node(
            30,
            NodeKind::HandlingBlock {
                handlers: vec![AirHandlerPair {
                    effect: type_path(&["Logger"]),
                    handler: Box::new(node(
                        31,
                        NodeKind::Call {
                            callee: Box::new(id_node(32, "StdoutLogger")),
                            args: vec![],
                            type_args: vec![],
                        },
                    )),
                }],
                body: Box::new(block(33, vec![], Some(call_inner))),
            },
        );
        let main_fn = node(
            40,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Private,
                is_async: false,
                name: ident("main"),
                generic_params: vec![],
                params: vec![],
                return_type: None,
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(block(41, vec![handling], None)),
            },
        );

        let out = gen(&module(vec![], vec![effect_decl, inner_fn, main_fn]));
        // Go: inner(__logger)
        assert!(
            out.contains("inner(__logger)"),
            "handling block should pass handler to effectful call, got: {out}"
        );
        assert!(
            out.contains("__logger := stdoutLogger()"),
            "handling block should instantiate handler, got: {out}"
        );
    }

    #[test]
    fn sibling_handling_blocks_do_not_share_go_block_scope() {
        use bock_air::AirHandlerPair;

        // Two *sibling* `handling` blocks, each `let part = …` under the SAME
        // name. Each block lowers to its own `{ … }` Go block scope, so both
        // must declare a fresh `part := …` — neither may be rewritten into a
        // bare `part = …` assignment against the other (a name that left scope
        // when the first block closed; Go would reject it as `undefined: part`).
        // Regression for Q-go-handling-let-redeclaration (mirrors the js/ts fix
        // Q-js-handling-let-redeclaration, #371).
        let make_handling = |id: u32, val: &str| {
            node(
                id,
                NodeKind::HandlingBlock {
                    handlers: vec![AirHandlerPair {
                        effect: type_path(&["Logger"]),
                        handler: Box::new(node(
                            id + 1,
                            NodeKind::Call {
                                callee: Box::new(id_node(id + 2, "StdoutLogger")),
                                args: vec![],
                                type_args: vec![],
                            },
                        )),
                    }],
                    body: Box::new(block(
                        id + 3,
                        vec![node(
                            id + 4,
                            NodeKind::LetBinding {
                                is_mut: false,
                                pattern: Box::new(bind_pat(id + 5, "part")),
                                ty: None,
                                value: Box::new(str_lit(id + 6, val)),
                            },
                        )],
                        Some(id_node(id + 7, "part")),
                    )),
                },
            )
        };
        let main_fn = node(
            40,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Private,
                is_async: false,
                name: ident("main"),
                generic_params: vec![],
                params: vec![],
                return_type: None,
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(block(
                    41,
                    vec![make_handling(50, "first"), make_handling(70, "second")],
                    None,
                )),
            },
        );

        let out = gen(&module(vec![], vec![main_fn]));
        assert_eq!(
            out.matches("part := ").count(),
            2,
            "each sibling handling block should declare its own `part := …`, got: {out}"
        );
        assert!(
            !out.contains("part = \""),
            "no sibling handling block may rewrite its `let part` into a bare \
             assignment, got: {out}"
        );
    }

    // ── C.8 Go effect codegen polish tests ──────────────────────────────────

    fn type_named_node(id: u32, name: &str) -> AIRNode {
        node(
            id,
            NodeKind::TypeNamed {
                path: type_path(&[name]),
                args: vec![],
            },
        )
    }

    /// Effect interface: Void-returning operations emit no return type.
    #[test]
    fn effect_interface_drops_void_return_type() {
        let void_op = node(
            2,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Public,
                is_async: false,
                name: ident("log"),
                generic_params: vec![],
                params: vec![typed_param_node(3, "msg", "String")],
                return_type: Some(Box::new(type_named_node(4, "Void"))),
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(block(5, vec![], None)),
            },
        );
        let effect_decl = node(
            1,
            NodeKind::EffectDecl {
                annotations: vec![],
                visibility: Visibility::Public,
                name: ident("Logger"),
                generic_params: vec![],
                components: vec![],
                operations: vec![void_op],
            },
        );
        let out = gen(&module(vec![], vec![effect_decl]));
        assert!(
            out.contains("type Logger interface {"),
            "should emit interface, got: {out}"
        );
        assert!(
            out.contains("Log(string)\n"),
            "Void op should have no return type, got: {out}"
        );
        assert!(
            !out.contains("Log(string) struct{}"),
            "Void op should NOT emit struct{{}} return, got: {out}"
        );
    }

    /// Public effectful function: Void return type is dropped in Go signature.
    #[test]
    fn fn_decl_drops_void_return_type() {
        let f = node(
            10,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Public,
                is_async: false,
                name: ident("do_thing"),
                generic_params: vec![],
                params: vec![],
                return_type: Some(Box::new(type_named_node(11, "Void"))),
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(block(12, vec![], None)),
            },
        );
        let out = gen(&module(vec![], vec![f]));
        assert!(
            out.contains("func DoThing() {"),
            "Void fn should have no return type, got: {out}"
        );
        assert!(
            !out.contains("DoThing() struct{}"),
            "should not emit struct{{}} return, got: {out}"
        );
    }

    /// Public function call sites emit PascalCase matching their definition.
    #[test]
    fn call_site_uses_pascal_case_for_public_fn() {
        let pub_fn = node(
            10,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Public,
                is_async: false,
                name: ident("do_thing"),
                generic_params: vec![],
                params: vec![],
                return_type: None,
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(block(12, vec![], None)),
            },
        );
        let call = node(
            20,
            NodeKind::Call {
                callee: Box::new(id_node(21, "do_thing")),
                args: vec![],
                type_args: vec![],
            },
        );
        let main_fn = node(
            30,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Private,
                is_async: false,
                name: ident("main"),
                generic_params: vec![],
                params: vec![],
                return_type: None,
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(block(31, vec![], Some(call))),
            },
        );
        let out = gen(&module(vec![], vec![pub_fn, main_fn]));
        assert!(
            out.contains("DoThing()"),
            "call to public fn should be PascalCase, got: {out}"
        );
        assert!(
            !out.contains("doThing()"),
            "call should NOT use camelCase for public fn, got: {out}"
        );
    }

    /// Go forbids a struct having a field and a method with the same name. A
    /// record whose field name collides with a method's PascalCased Go name (the
    /// `core.error` shape: `record SimpleError { message: String }` +
    /// `fn message(self) -> String`) must emit the method under a disambiguated
    /// name (`MessageMethod`) at the *trait interface*, the *receiver method*,
    /// and every *call site* so they agree — while the field stays `Message`.
    /// Q-go-error-message: pre-S6b this emitted both `Message` field and
    /// `Message()` method on `SimpleError`, which `go build` rejects.
    #[test]
    fn method_colliding_with_field_is_disambiguated() {
        // record SimpleError { message: String }
        let record_decl = node(
            1,
            NodeKind::RecordDecl {
                annotations: vec![],
                visibility: Visibility::Public,
                name: ident("SimpleError"),
                generic_params: vec![],
                fields: vec![bock_ast::RecordDeclField {
                    id: 0,
                    span: span(),
                    name: ident("message"),
                    ty: TypeExpr::Named {
                        id: 0,
                        span: span(),
                        path: type_path(&["String"]),
                        args: vec![],
                    },
                    default: None,
                }],
            },
        );
        // trait Error { fn message(self) -> String }
        let trait_decl = node(
            2,
            NodeKind::TraitDecl {
                annotations: vec![],
                visibility: Visibility::Public,
                is_platform: false,
                name: ident("Error"),
                generic_params: vec![],
                associated_types: vec![],
                methods: vec![node(
                    3,
                    NodeKind::FnDecl {
                        annotations: vec![],
                        visibility: Visibility::Public,
                        is_async: false,
                        name: ident("message"),
                        generic_params: vec![],
                        params: vec![param_node(4, "self")],
                        return_type: Some(Box::new(type_named_node(5, "String"))),
                        effect_clause: vec![],
                        where_clause: vec![],
                        body: Box::new(block(6, vec![], None)),
                    },
                )],
            },
        );
        // impl Error for SimpleError { public fn message(self) -> String { self.message } }
        let method = node(
            10,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Public,
                is_async: false,
                name: ident("message"),
                generic_params: vec![],
                params: vec![param_node(11, "self")],
                return_type: Some(Box::new(type_named_node(12, "String"))),
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(block(
                    13,
                    vec![],
                    Some(node(
                        14,
                        NodeKind::FieldAccess {
                            object: Box::new(id_node(15, "self")),
                            field: ident("message"),
                        },
                    )),
                )),
            },
        );
        let impl_block = node(
            20,
            NodeKind::ImplBlock {
                annotations: vec![],
                target: Box::new(type_named_node(21, "SimpleError")),
                trait_path: Some(type_path(&["Error"])),
                trait_args: vec![],
                generic_params: vec![],
                where_clause: vec![],
                methods: vec![method],
            },
        );
        // fn read(e: SimpleError) -> String { e.message() }
        let read_fn = node(
            30,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Public,
                is_async: false,
                name: ident("read"),
                generic_params: vec![],
                params: vec![typed_param_node(31, "e", "SimpleError")],
                return_type: Some(Box::new(type_named_node(32, "String"))),
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(block(
                    33,
                    vec![],
                    Some(node(
                        34,
                        NodeKind::MethodCall {
                            receiver: Box::new(id_node(35, "e")),
                            method: ident("message"),
                            type_args: vec![],
                            args: vec![],
                        },
                    )),
                )),
            },
        );
        let out = gen(&module(
            vec![],
            vec![record_decl, trait_decl, impl_block, read_fn],
        ));
        // The field stays `Message`.
        assert!(
            out.contains("Message\tstring"),
            "field should remain `Message`, got: {out}"
        );
        // The method (interface, receiver, call site) is disambiguated.
        assert!(
            out.contains("MessageMethod() string"),
            "trait interface should declare `MessageMethod()`, got: {out}"
        );
        assert!(
            out.contains("SimpleError) MessageMethod()"),
            "receiver method should be `MessageMethod()`, got: {out}"
        );
        assert!(
            out.contains(".MessageMethod()"),
            "call site should be `.MessageMethod()`, got: {out}"
        );
        // The body still reads the field (`self.Message`), and no plain
        // `Message()` method (the colliding form Go rejects) is emitted.
        assert!(
            out.contains("return self.Message"),
            "method body should read the field `self.Message`, got: {out}"
        );
        assert!(
            !out.contains(") Message() string"),
            "must NOT emit a `Message()` method colliding with the field, got: {out}"
        );
    }

    /// Trait/effect impl blocks use value receivers so `Handler{}` satisfies the interface.
    #[test]
    fn impl_block_methods_use_value_receivers() {
        let record_decl = node(
            1,
            NodeKind::RecordDecl {
                annotations: vec![],
                visibility: Visibility::Public,
                name: ident("StdoutLogger"),
                generic_params: vec![],
                fields: vec![],
            },
        );
        let method = node(
            10,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Public,
                is_async: false,
                name: ident("log"),
                generic_params: vec![],
                // Instance method leads with `self` (real lowering); a no-`self`
                // method is an associated function (free function, no receiver).
                params: vec![
                    param_node(14, "self"),
                    typed_param_node(11, "msg", "String"),
                ],
                return_type: Some(Box::new(type_named_node(12, "Void"))),
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(block(13, vec![], None)),
            },
        );
        let impl_block = node(
            20,
            NodeKind::ImplBlock {
                annotations: vec![],
                target: Box::new(type_named_node(21, "StdoutLogger")),
                trait_path: Some(type_path(&["Logger"])),
                trait_args: vec![],
                generic_params: vec![],
                where_clause: vec![],
                methods: vec![method],
            },
        );
        let out = gen(&module(vec![], vec![record_decl, impl_block]));
        assert!(
            out.contains("func (self StdoutLogger) Log("),
            "impl method should use value receiver, got: {out}"
        );
        assert!(
            !out.contains("func (self *StdoutLogger) Log("),
            "impl method should NOT use pointer receiver, got: {out}"
        );
    }

    /// Module-level `handle` declares a var AND registers it so module-level
    /// calls to effectful functions pick it up.
    #[test]
    fn module_handle_registers_handler_for_calls() {
        use bock_air::AirHandlerPair;
        let _ = AirHandlerPair {
            effect: type_path(&["Logger"]),
            handler: Box::new(str_lit(999, "placeholder")),
        };

        let effect_decl = node(
            1,
            NodeKind::EffectDecl {
                annotations: vec![],
                visibility: Visibility::Public,
                name: ident("Logger"),
                generic_params: vec![],
                components: vec![],
                operations: vec![node(
                    2,
                    NodeKind::FnDecl {
                        annotations: vec![],
                        visibility: Visibility::Public,
                        is_async: false,
                        name: ident("log"),
                        generic_params: vec![],
                        params: vec![typed_param_node(3, "msg", "String")],
                        return_type: Some(Box::new(type_named_node(4, "Void"))),
                        effect_clause: vec![],
                        where_clause: vec![],
                        body: Box::new(block(5, vec![], None)),
                    },
                )],
            },
        );

        let effectful_fn = node(
            10,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Public,
                is_async: false,
                name: ident("do_log"),
                generic_params: vec![],
                params: vec![],
                return_type: None,
                effect_clause: vec![type_path(&["Logger"])],
                where_clause: vec![],
                body: Box::new(block(11, vec![], None)),
            },
        );

        let module_handle = node(
            20,
            NodeKind::ModuleHandle {
                effect: type_path(&["Logger"]),
                handler: Box::new(node(
                    21,
                    NodeKind::Call {
                        callee: Box::new(id_node(22, "StdoutLogger")),
                        args: vec![],
                        type_args: vec![],
                    },
                )),
            },
        );

        let main_call = node(
            30,
            NodeKind::Call {
                callee: Box::new(id_node(31, "do_log")),
                args: vec![],
                type_args: vec![],
            },
        );
        let main_fn = node(
            40,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Private,
                is_async: false,
                name: ident("main"),
                generic_params: vec![],
                params: vec![],
                return_type: None,
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(block(41, vec![], Some(main_call))),
            },
        );

        let out = gen(&module(
            vec![],
            vec![effect_decl, effectful_fn, module_handle, main_fn],
        ));
        assert!(
            out.contains("var __logger Logger = stdoutLogger()"),
            "module handle should declare var, got: {out}"
        );
        assert!(
            out.contains("DoLog(__logger)"),
            "module-level call should receive __logger, got: {out}"
        );
    }

    /// Handling block suppresses Go "declared but not used" errors for handler vars.
    #[test]
    fn handling_block_emits_unused_suppression() {
        use bock_air::AirHandlerPair;
        let effect_decl = node(
            1,
            NodeKind::EffectDecl {
                annotations: vec![],
                visibility: Visibility::Public,
                name: ident("Logger"),
                generic_params: vec![],
                components: vec![],
                operations: vec![node(
                    2,
                    NodeKind::FnDecl {
                        annotations: vec![],
                        visibility: Visibility::Public,
                        is_async: false,
                        name: ident("log"),
                        generic_params: vec![],
                        params: vec![typed_param_node(3, "msg", "String")],
                        return_type: Some(Box::new(type_named_node(4, "Void"))),
                        effect_clause: vec![],
                        where_clause: vec![],
                        body: Box::new(block(5, vec![], None)),
                    },
                )],
            },
        );
        let handling = node(
            30,
            NodeKind::HandlingBlock {
                handlers: vec![AirHandlerPair {
                    effect: type_path(&["Logger"]),
                    handler: Box::new(node(
                        31,
                        NodeKind::Call {
                            callee: Box::new(id_node(32, "StdoutLogger")),
                            args: vec![],
                            type_args: vec![],
                        },
                    )),
                }],
                body: Box::new(block(33, vec![], Some(str_lit(34, "body")))),
            },
        );
        let main_fn = node(
            40,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Private,
                is_async: false,
                name: ident("main"),
                generic_params: vec![],
                params: vec![],
                return_type: None,
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(block(41, vec![handling], None)),
            },
        );
        let out = gen(&module(vec![], vec![effect_decl, main_fn]));
        assert!(
            out.contains("_ = __logger"),
            "should suppress unused-var error for handler, got: {out}"
        );
    }

    /// Void effect operations (e.g., log) are not wrapped in `return` when a
    /// tail expression in a Void-returning function.
    #[test]
    fn void_effect_op_tail_not_wrapped_in_return() {
        let effect_decl = node(
            1,
            NodeKind::EffectDecl {
                annotations: vec![],
                visibility: Visibility::Public,
                name: ident("Logger"),
                generic_params: vec![],
                components: vec![],
                operations: vec![node(
                    2,
                    NodeKind::FnDecl {
                        annotations: vec![],
                        visibility: Visibility::Public,
                        is_async: false,
                        name: ident("log"),
                        generic_params: vec![],
                        params: vec![typed_param_node(3, "msg", "String")],
                        return_type: Some(Box::new(type_named_node(4, "Void"))),
                        effect_clause: vec![],
                        where_clause: vec![],
                        body: Box::new(block(5, vec![], None)),
                    },
                )],
            },
        );
        let log_call = node(
            10,
            NodeKind::Call {
                callee: Box::new(id_node(11, "log")),
                args: vec![bock_air::AirArg {
                    label: None,
                    value: str_lit(12, "hello"),
                }],
                type_args: vec![],
            },
        );
        let caller = node(
            20,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Public,
                is_async: false,
                name: ident("do_log"),
                generic_params: vec![],
                params: vec![],
                return_type: Some(Box::new(type_named_node(21, "Void"))),
                effect_clause: vec![type_path(&["Logger"])],
                where_clause: vec![],
                body: Box::new(block(22, vec![], Some(log_call))),
            },
        );
        let out = gen(&module(vec![], vec![effect_decl, caller]));
        assert!(
            out.contains("logger.Log("),
            "effect op should be rewritten as handler.Method, got: {out}"
        );
        assert!(
            !out.contains("return logger.Log("),
            "Void effect op in Void fn should NOT be preceded by `return`, got: {out}"
        );
    }

    // ── Generics codegen (DV12 / P1-b2) ───────────────────────────────────────

    fn generic_param(id: u32, name: &str) -> bock_ast::GenericParam {
        bock_ast::GenericParam {
            id,
            span: span(),
            name: ident(name),
            bounds: vec![],
        }
    }

    fn named_type(id: u32, name: &str) -> AIRNode {
        node(
            id,
            NodeKind::TypeNamed {
                path: type_path(&[name]),
                args: vec![],
            },
        )
    }

    /// `record Box[T] { value: T }`.
    fn generic_box_record() -> AIRNode {
        node(
            10,
            NodeKind::RecordDecl {
                annotations: vec![],
                visibility: Visibility::Private,
                name: ident("Box"),
                generic_params: vec![generic_param(11, "T")],
                fields: vec![bock_ast::RecordDeclField {
                    id: 12,
                    span: span(),
                    name: ident("value"),
                    ty: TypeExpr::Named {
                        id: 13,
                        span: span(),
                        path: type_path(&["T"]),
                        args: vec![],
                    },
                    default: None,
                }],
            },
        )
    }

    /// `impl Box { fn get(self) -> T { return self.value } }`.
    fn generic_box_impl() -> AIRNode {
        let self_param = node(
            20,
            NodeKind::Param {
                pattern: Box::new(bind_pat(21, "self")),
                ty: None,
                default: None,
            },
        );
        let body = block(
            22,
            vec![],
            Some(node(
                23,
                NodeKind::Return {
                    value: Some(Box::new(node(
                        24,
                        NodeKind::FieldAccess {
                            object: Box::new(id_node(25, "self")),
                            field: ident("value"),
                        },
                    ))),
                },
            )),
        );
        let method = node(
            26,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Private,
                is_async: false,
                name: ident("get"),
                generic_params: vec![],
                params: vec![self_param],
                return_type: Some(Box::new(named_type(27, "T"))),
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(body),
            },
        );
        node(
            30,
            NodeKind::ImplBlock {
                annotations: vec![],
                generic_params: vec![],
                trait_path: None,
                trait_args: vec![],
                target: Box::new(named_type(31, "Box")),
                where_clause: vec![],
                methods: vec![method],
            },
        )
    }

    #[test]
    fn generic_method_receiver_carries_type_params() {
        // `impl Box { ... }` for `record Box[T]` must emit
        // `func (self *Box[T]) get() T` — Go requires the type-param list on the
        // receiver, recovered from the record decl since the impl has none.
        let out = gen(&module(
            vec![],
            vec![generic_box_record(), generic_box_impl()],
        ));
        assert!(
            out.contains("func (self *Box[T]) get() T {"),
            "generic method receiver should carry `[T]`, got: {out}"
        );
    }

    /// `impl Box { fn map[U](self, f: Fn(T) -> U) -> Box[U] { Box { value:
    /// f(self.value) } } }`.
    fn generic_box_map_impl() -> AIRNode {
        let self_param = node(
            120,
            NodeKind::Param {
                pattern: Box::new(bind_pat(121, "self")),
                ty: None,
                default: None,
            },
        );
        // `f: Fn(T) -> U`
        let f_ty = node(
            122,
            NodeKind::TypeFunction {
                params: vec![named_type(123, "T")],
                ret: Box::new(named_type(124, "U")),
                effects: vec![],
            },
        );
        let f_param = node(
            125,
            NodeKind::Param {
                pattern: Box::new(bind_pat(126, "f")),
                ty: Some(Box::new(f_ty)),
                default: None,
            },
        );
        // Body: `Box { value: f(self.value) }`
        let call_f = node(
            127,
            NodeKind::Call {
                callee: Box::new(id_node(128, "f")),
                type_args: vec![],
                args: vec![AirArg {
                    label: None,
                    value: node(
                        129,
                        NodeKind::FieldAccess {
                            object: Box::new(id_node(130, "self")),
                            field: ident("value"),
                        },
                    ),
                }],
            },
        );
        let construct = node(
            131,
            NodeKind::RecordConstruct {
                path: type_path(&["Box"]),
                fields: vec![bock_air::AirRecordField {
                    name: ident("value"),
                    value: Some(Box::new(call_f)),
                }],
                spread: None,
            },
        );
        let body = block(132, vec![], Some(construct));
        let ret_ty = node(
            133,
            NodeKind::TypeNamed {
                path: type_path(&["Box"]),
                args: vec![named_type(134, "U")],
            },
        );
        let method = node(
            135,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Public,
                is_async: false,
                name: ident("map"),
                generic_params: vec![generic_param(136, "U")],
                params: vec![self_param, f_param],
                return_type: Some(Box::new(ret_ty)),
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(body),
            },
        );
        node(
            137,
            NodeKind::ImplBlock {
                annotations: vec![],
                generic_params: vec![],
                trait_path: None,
                trait_args: vec![],
                target: Box::new(named_type(138, "Box")),
                where_clause: vec![],
                methods: vec![method],
            },
        )
    }

    #[test]
    fn method_level_type_params_lower_to_free_function() {
        // DQ28: Go forbids method type params, so `Box[T].map[U]` lowers to a
        // free function `func Box_Map[T any, U any](self Box[T], f func(T) U)
        // Box[U]` (the receiver becomes a leading `self` parameter; the
        // receiver's `T` and the method's `U` combine on the free function).
        let out = gen(&module(
            vec![],
            vec![generic_box_record(), generic_box_map_impl()],
        ));
        assert!(
            out.contains("func Box_Map[T any, U any](self Box[T], f func(T) U) Box[U] {"),
            "method-generic should free-function-lower with combined type params, got: {out}"
        );
        // The invalid `func (self *Box[T]) Map[U](..)` (Go syntax error) must NOT
        // be emitted.
        assert!(
            !out.contains(") Map["),
            "must not emit a Go method with type params, got: {out}"
        );
    }

    #[test]
    fn method_level_type_param_call_site_rewrites_to_free_function() {
        // A call `b.map(f)` to the free-function-lowered `Box.map[U]` rewrites to
        // `Box_Map(b, f)` (receiver-first), for both the `MethodCall` and the
        // desugared `Call(FieldAccess(b, map), [b, f])` shapes.
        let recv = id_node(200, "b");
        let cb = node(
            201,
            NodeKind::Lambda {
                params: vec![param_node(202, "x")],
                body: Box::new(block(
                    203,
                    vec![],
                    Some(node(
                        204,
                        NodeKind::BinaryOp {
                            op: BinOp::Mul,
                            left: Box::new(id_node(205, "x")),
                            right: Box::new(int_lit(206, "2")),
                        },
                    )),
                )),
            },
        );
        let call = node(
            207,
            NodeKind::MethodCall {
                receiver: Box::new(recv),
                method: ident("map"),
                type_args: vec![],
                args: vec![AirArg {
                    label: None,
                    value: cb,
                }],
            },
        );
        let let_stmt = node(
            208,
            NodeKind::LetBinding {
                is_mut: false,
                pattern: Box::new(bind_pat(209, "r")),
                ty: None,
                value: Box::new(call),
            },
        );
        let out = gen(&module(
            vec![],
            vec![generic_box_record(), generic_box_map_impl(), let_stmt],
        ));
        assert!(
            out.contains("Box_Map(b, "),
            "call site should rewrite to the free-function call `Box_Map(b, ..)`, got: {out}"
        );
        assert!(
            !out.contains("b.Map("),
            "call site must not keep the Go method-call form, got: {out}"
        );
    }

    #[test]
    fn generic_construct_emits_explicit_type_args() {
        // `Box { value: 42 }` → `Box[int64]{Value: 42}` (Go does not infer
        // struct type args from composite-literal fields).
        let construct = node(
            40,
            NodeKind::RecordConstruct {
                path: type_path(&["Box"]),
                fields: vec![bock_air::AirRecordField {
                    name: ident("value"),
                    value: Some(Box::new(int_lit(41, "42"))),
                }],
                spread: None,
            },
        );
        let let_stmt = node(
            42,
            NodeKind::LetBinding {
                is_mut: false,
                pattern: Box::new(bind_pat(43, "b")),
                ty: None,
                value: Box::new(construct),
            },
        );
        let out = gen(&module(vec![], vec![generic_box_record(), let_stmt]));
        assert!(
            out.contains("Box[int64]{Value: 42}"),
            "generic construction should carry explicit `[int64]`, got: {out}"
        );
    }

    #[test]
    fn generic_fn_return_list_literal_uses_param_type() {
        // GAP-C: `fn single[T](x: T) -> List[T] { return [x] }` must emit
        // `return []T{x}`, not `[]interface{}{x}` (which a `[]T` return rejects).
        let list_t = node(
            61,
            NodeKind::TypeNamed {
                path: type_path(&["List"]),
                args: vec![named_type(62, "T")],
            },
        );
        let body = block(
            63,
            vec![],
            Some(node(
                64,
                NodeKind::Return {
                    value: Some(Box::new(node(
                        65,
                        NodeKind::ListLiteral {
                            elems: vec![id_node(66, "x")],
                        },
                    ))),
                },
            )),
        );
        let f = node(
            67,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Private,
                is_async: false,
                name: ident("single"),
                generic_params: vec![generic_param(68, "T")],
                params: vec![node(
                    69,
                    NodeKind::Param {
                        pattern: Box::new(bind_pat(70, "x")),
                        ty: Some(Box::new(named_type(71, "T"))),
                        default: None,
                    },
                )],
                return_type: Some(Box::new(list_t)),
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(body),
            },
        );
        let out = gen(&module(vec![], vec![f]));
        assert!(
            out.contains("return []T{x}"),
            "generic fn returning a list literal should use `[]T`, got: {out}"
        );
    }

    #[test]
    fn generic_construct_uses_declared_type_args_for_nested_param() {
        // GAP-C/D plumbing: `let c: ListIter[Int] = ListIter { xs: [...] }` for
        // `record ListIter[T] { xs: List[T] }` must emit `ListIter[int64]{...}`.
        // Field inference yields `any` here (no field is typed exactly `T`; `xs`
        // is `List[T]`), so the construction must adopt the declared binding
        // type's concrete args.
        let record = node(
            10,
            NodeKind::RecordDecl {
                annotations: vec![],
                visibility: Visibility::Private,
                name: ident("ListIter"),
                generic_params: vec![generic_param(11, "T")],
                fields: vec![bock_ast::RecordDeclField {
                    id: 12,
                    span: span(),
                    name: ident("xs"),
                    ty: TypeExpr::Named {
                        id: 13,
                        span: span(),
                        path: type_path(&["List"]),
                        args: vec![TypeExpr::Named {
                            id: 14,
                            span: span(),
                            path: type_path(&["T"]),
                            args: vec![],
                        }],
                    },
                    default: None,
                }],
            },
        );
        let construct = node(
            20,
            NodeKind::RecordConstruct {
                path: type_path(&["ListIter"]),
                fields: vec![bock_air::AirRecordField {
                    name: ident("xs"),
                    value: Some(Box::new(node(
                        21,
                        NodeKind::ListLiteral {
                            elems: vec![int_lit(22, "1")],
                        },
                    ))),
                }],
                spread: None,
            },
        );
        let let_stmt = node(
            23,
            NodeKind::LetBinding {
                is_mut: false,
                pattern: Box::new(bind_pat(24, "c")),
                ty: Some(Box::new(node(
                    25,
                    NodeKind::TypeNamed {
                        path: type_path(&["ListIter"]),
                        args: vec![named_type(26, "Int")],
                    },
                ))),
                value: Box::new(construct),
            },
        );
        let out = gen(&module(vec![], vec![record, let_stmt]));
        assert!(
            out.contains("ListIter[int64]{"),
            "construction should adopt the declared binding type's `[int64]`, got: {out}"
        );
        assert!(
            !out.contains("ListIter[any]{"),
            "construction must NOT fall back to `[any]` when a declared type is present, got: {out}"
        );
    }

    #[test]
    fn lambda_return_type_inferred_from_body() {
        // `(n: Int) => n + 1` → `func(n int64) int64 { return (n + 1) }`, not
        // `interface{}` (which fails to satisfy a typed `func(int64) int64`).
        let lambda = node(
            50,
            NodeKind::Lambda {
                params: vec![typed_param_node(51, "n", "Int")],
                body: Box::new(node(
                    52,
                    NodeKind::BinaryOp {
                        op: BinOp::Add,
                        left: Box::new(id_node(53, "n")),
                        right: Box::new(int_lit(54, "1")),
                    },
                )),
            },
        );
        let let_stmt = node(
            55,
            NodeKind::LetBinding {
                is_mut: false,
                pattern: Box::new(bind_pat(56, "inc")),
                ty: None,
                value: Box::new(lambda),
            },
        );
        let out = gen(&module(vec![], vec![let_stmt]));
        assert!(
            out.contains("func(n int64) int64 {"),
            "lambda should infer `int64` return type, got: {out}"
        );
        assert!(
            !out.contains("func(n int64) interface{}"),
            "lambda should NOT fall back to interface{{}} return, got: {out}"
        );
    }

    #[test]
    fn unify_go_pattern_binds_iterator_and_slice_params() {
        let gp = vec!["T".to_string(), "U".to_string()];
        // `ListIterator[T]` against `ListIterator[int64]` binds T → int64.
        let mut b = HashMap::new();
        GoEmitCtx::unify_go_pattern("ListIterator[T]", "ListIterator[int64]", &gp, &mut b);
        assert_eq!(b.get("T"), Some(&"int64".to_string()));
        // `[]T` against `[]string` binds T → string.
        let mut b2 = HashMap::new();
        GoEmitCtx::unify_go_pattern("[]T", "[]string", &gp, &mut b2);
        assert_eq!(b2.get("T"), Some(&"string".to_string()));
        // A bare param binds to the whole concrete type.
        let mut b3 = HashMap::new();
        GoEmitCtx::unify_go_pattern("U", "map[string]int64", &gp, &mut b3);
        assert_eq!(b3.get("U"), Some(&"map[string]int64".to_string()));
        // A structural mismatch records nothing (conservative).
        let mut b4 = HashMap::new();
        GoEmitCtx::unify_go_pattern("ListIterator[T]", "int64", &gp, &mut b4);
        assert!(b4.is_empty());
    }

    #[test]
    fn split_top_level_commas_respects_nesting() {
        assert_eq!(
            GoEmitCtx::split_top_level_commas("int64, []string"),
            vec!["int64".to_string(), "[]string".to_string()]
        );
        // A comma nested inside `[...]` is not a top-level separator.
        assert_eq!(
            GoEmitCtx::split_top_level_commas("map[string]int64, T"),
            vec!["map[string]int64".to_string(), "T".to_string()]
        );
        assert_eq!(
            GoEmitCtx::split_top_level_commas("int64"),
            vec!["int64".to_string()]
        );
    }

    #[test]
    fn replace_type_token_only_swaps_whole_identifiers() {
        // `T` in `[]T` is replaced; `T` inside `Tree` is not.
        assert_eq!(
            GoEmitCtx::replace_type_token("[]T", "T", "int64"),
            "[]int64"
        );
        assert_eq!(GoEmitCtx::replace_type_token("Tree", "T", "int64"), "Tree");
        assert_eq!(
            GoEmitCtx::replace_type_token("func(T) T", "T", "int64"),
            "func(int64) int64"
        );
        assert!(GoEmitCtx::contains_type_token("ListIterator[T]", "T"));
        assert!(!GoEmitCtx::contains_type_token("ListIterator[int64]", "T"));
    }

    #[test]
    fn generic_trait_with_type_param_is_a_generic_interface() {
        // A trait that declares its own generic param (`Iterable[T]`) becomes a
        // generic Go interface so a method signature naming `T` is in scope.
        let method = node(
            2,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Public,
                is_async: false,
                name: ident("iter"),
                generic_params: vec![],
                params: vec![node(
                    10,
                    NodeKind::Param {
                        pattern: Box::new(bind_pat(11, "self")),
                        ty: None,
                        default: None,
                    },
                )],
                return_type: Some(Box::new(node(
                    20,
                    NodeKind::TypeNamed {
                        path: type_path(&["ListIterator"]),
                        args: vec![named_type(21, "T")],
                    },
                ))),
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(block(3, vec![], None)),
            },
        );
        let t = node(
            1,
            NodeKind::TraitDecl {
                annotations: vec![],
                visibility: Visibility::Public,
                is_platform: false,
                name: ident("Iterable"),
                generic_params: vec![generic_param(30, "T")],
                associated_types: vec![],
                methods: vec![method],
            },
        );
        let out = gen(&module(vec![], vec![t]));
        assert!(
            out.contains("type Iterable[T any] interface {"),
            "generic trait should carry its type param on the interface, got: {out}"
        );
        assert!(
            out.contains("Iter() ListIterator[T]"),
            "the method return must keep `[T]`, got: {out}"
        );
    }

    // ── Per-module native-package tree (S3) ─────────────────────────────────

    fn mod_path(segs: &[&str]) -> bock_ast::ModulePath {
        bock_ast::ModulePath {
            segments: segs.iter().map(|s| ident(s)).collect(),
            span: span(),
        }
    }

    /// A module node with a declared dotted `path` (e.g. `core.option`).
    fn module_with_path(path: &[&str], imports: Vec<AIRNode>, items: Vec<AIRNode>) -> AIRNode {
        node(
            0,
            NodeKind::Module {
                path: Some(mod_path(path)),
                annotations: vec![],
                imports,
                items,
            },
        )
    }

    /// An `import <path>.{ name }` AIR node (a single-item `Named` import).
    fn import_named(id: u32, path: &[&str], name: &str) -> AIRNode {
        node(
            id,
            NodeKind::ImportDecl {
                path: mod_path(path),
                items: bock_ast::ImportItems::Named(vec![bock_ast::ImportedName {
                    span: span(),
                    name: ident(name),
                    alias: None,
                }]),
            },
        )
    }

    /// A bare `fn <name>() -> <tail>` declaration with the given visibility.
    fn fn_decl_tail(id: u32, vis: Visibility, name: &str, tail: AIRNode) -> AIRNode {
        node(
            id,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: vis,
                is_async: false,
                name: ident(name),
                generic_params: vec![],
                params: vec![],
                return_type: None,
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(block(id + 1, vec![], Some(tail))),
            },
        )
    }

    #[test]
    fn per_module_emits_native_go_package_tree() {
        // entry `module main` uses `mathutil.add_one`; `module mathutil` exports
        // a `public fn add_one`. Per-module emission must produce the native Go
        // *source* package: `main.go` (one `package main`) and the flat
        // `mathutil.go` (same package — the call site needs no import) —
        // separate files, not a single collapsed file. The `go.mod` run
        // affordance is emitted by the scaffolder (project mode), NOT codegen
        // (S6a / DV18).
        let call = node(
            10,
            NodeKind::Call {
                callee: Box::new(id_node(11, "add_one")),
                args: vec![AirArg {
                    label: None,
                    value: int_lit(12, "6"),
                }],
                type_args: vec![],
            },
        );
        let main_mod = module_with_path(
            &["main"],
            vec![import_named(5, &["mathutil"], "add_one")],
            vec![fn_decl_tail(1, Visibility::Private, "main", call)],
        );
        let util_mod = module_with_path(
            &["mathutil"],
            vec![],
            vec![fn_decl_tail(
                20,
                Visibility::Public,
                "add_one",
                int_lit(22, "7"),
            )],
        );

        let gen = GoGenerator::new();
        let out = gen
            .generate_project(&[
                (&main_mod, std::path::Path::new("src/main.bock")),
                (&util_mod, std::path::Path::new("src/mathutil.bock")),
            ])
            .unwrap();
        let by_name = |p: &str| out.files.iter().find(|f| f.path == std::path::Path::new(p));
        let main_file = by_name("main.go").expect("main.go emitted");
        let util_file = by_name("mathutil.go").expect("flat mathutil.go emitted");
        // Codegen no longer emits the manifest (S6a / DV18) — the scaffolder
        // owns the `go.mod` in project mode.
        assert!(
            by_name("go.mod").is_none(),
            "codegen must NOT emit go.mod — the scaffolder owns it (S6a)"
        );

        assert!(
            main_file.content.starts_with("package main"),
            "main.go must be `package main`; got:\n{}",
            main_file.content
        );
        // Same package → the cross-module call needs no import statement.
        assert!(
            !main_file.content.contains("import \"mathutil\""),
            "main.go must NOT import the sibling (same package); got:\n{}",
            main_file.content
        );
        // The exported fn is PascalCased on emit; the call site matches.
        assert!(
            util_file.content.contains("func AddOne("),
            "mathutil.go must carry the exported fn; got:\n{}",
            util_file.content
        );
        assert!(
            main_file.content.contains("AddOne("),
            "main.go must call the cross-module fn; got:\n{}",
            main_file.content
        );
    }

    #[test]
    fn per_module_nested_module_flattens_filename() {
        // A nested `core.option` module flattens to a single flat file
        // `core.option.go` (one package per dir — no subdirectory; dots kept so
        // a `core.test` module never collides with Go's `_test.go` suffix).
        let opt_mod = module_with_path(
            &["core", "option"],
            vec![],
            vec![fn_decl_tail(
                20,
                Visibility::Public,
                "get_or",
                int_lit(22, "0"),
            )],
        );
        let main_mod = module_with_path(
            &["main"],
            vec![import_named(5, &["core", "option"], "get_or")],
            vec![fn_decl_tail(
                1,
                Visibility::Private,
                "main",
                node(
                    10,
                    NodeKind::Call {
                        callee: Box::new(id_node(11, "get_or")),
                        args: vec![],
                        type_args: vec![],
                    },
                ),
            )],
        );
        let gen = GoGenerator::new();
        let out = gen
            .generate_project(&[
                (&main_mod, std::path::Path::new("src/main.bock")),
                (&opt_mod, std::path::Path::new("src/core/option.bock")),
            ])
            .unwrap();
        let by_name = |p: &str| out.files.iter().find(|f| f.path == std::path::Path::new(p));
        by_name("core.option.go").expect("nested module flattens to core.option.go");
        // No subdirectory: there must be no `core/option.go`.
        assert!(
            by_name("core/option.go").is_none(),
            "go must NOT emit a subdirectory package file"
        );
    }

    /// `fn f() { let x = if (c) { 1 } else { return 0 }  x }` — value-position
    /// `if` with a diverging else. The shared value-CF hoist lowers it to a
    /// `var __bockCf0 T` (type inferred from the assigned arm values) plus
    /// statement-form assignment, never `/* unsupported */` or an IIFE that
    /// captures the `return`.
    fn diverging_value_if_fn() -> AIRNode {
        let then_b = block(2, vec![], Some(int_lit(3, "1")));
        let ret = node(
            5,
            NodeKind::Return {
                value: Some(Box::new(int_lit(6, "0"))),
            },
        );
        let else_b = block(4, vec![], Some(ret));
        let if_node = node(
            1,
            NodeKind::If {
                let_pattern: None,
                condition: Box::new(id_node(7, "c")),
                then_block: Box::new(then_b),
                else_block: Some(Box::new(else_b)),
            },
        );
        let let_x = node(
            10,
            NodeKind::LetBinding {
                is_mut: false,
                pattern: Box::new(bind_pat(11, "x")),
                ty: None,
                value: Box::new(if_node),
            },
        );
        let body = block(20, vec![let_x], Some(id_node(21, "x")));
        let f = node(
            30,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Private,
                is_async: false,
                name: ident("f"),
                generic_params: vec![],
                params: vec![],
                return_type: None,
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(body),
            },
        );
        module(vec![], vec![f])
    }

    #[test]
    fn diverging_value_if_hoists_to_stmt_form_no_unsupported() {
        let out = gen(&diverging_value_if_fn());
        assert!(
            !out.contains("/* unsupported */"),
            "diverging value-if must not emit `/* unsupported */`, got: {out}"
        );
        // The temp is declared with an inferred Go type (`int64` from the arms).
        // Go's `go_value_ident` strips the leading `__`, so the name is `bockCf0`.
        assert!(
            out.contains("var bockCf0 int64"),
            "must declare a typed temp `var bockCf0 int64`, got: {out}"
        );
        assert!(
            out.contains("bockCf0 = 1"),
            "value arm must assign the temp, got: {out}"
        );
        assert!(
            out.contains("return 0"),
            "diverging arm must keep its return, got: {out}"
        );
    }

    // ── Q-guard-let-shared (go) ───────────────────────────────────────────────

    /// `guard (let Ok(v) = cond) else { return … }` must test the discriminant
    /// tag and bind the payload into the *enclosing* scope (live after the
    /// guard), not negate the non-bool `__bockResult` and drop the binding.
    #[test]
    fn go_guard_let_tests_tag_and_binds_payload() {
        // guard (let Ok(v) = res) else { return }
        let guard = node(
            10,
            NodeKind::Guard {
                let_pattern: Some(Box::new(node(
                    11,
                    NodeKind::ConstructorPat {
                        path: type_path(&["Ok"]),
                        fields: vec![bind_pat(12, "v")],
                    },
                ))),
                condition: Box::new(id_node(13, "res")),
                else_block: Box::new(block(
                    14,
                    vec![node(15, NodeKind::Return { value: None })],
                    None,
                )),
            },
        );
        // fn check() -> Void { guard …; }
        let f = node(
            20,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Private,
                is_async: false,
                name: ident("check"),
                generic_params: vec![],
                params: vec![],
                return_type: None,
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(block(21, vec![guard], None)),
            },
        );
        let out = gen(&module(vec![], vec![f]));
        // The discriminant is hoisted into a `__guard` temp and tested by tag,
        // never negated as a bool.
        assert!(
            out.contains("__guard0 := res"),
            "guard-let must hoist the discriminant, got: {out}"
        );
        assert!(
            out.contains("if !(__guard0.tag == \"Ok\")"),
            "guard-let must test the tag, got: {out}"
        );
        assert!(
            !out.contains("if !(res)"),
            "guard-let must not negate the non-bool discriminant, got: {out}"
        );
        // The else arm diverges, then the payload binding lands after the `if`.
        assert!(
            out.contains("v := __guard0.v"),
            "guard-let must bind the payload after the guard, got: {out}"
        );
    }

    // ── Q-let-shadow-const (go) ───────────────────────────────────────────────

    /// A shadowing `let` re-binding a name already declared in the block lowers
    /// to a reassignment (`acc = …`), not a colliding re-declaration
    /// (`acc := …` / `var acc … = …` — Go's "no new variables on left side").
    #[test]
    fn go_let_shadow_rebinds_as_assignment() {
        let stmts = vec![
            // let acc = 1
            node(
                10,
                NodeKind::LetBinding {
                    is_mut: false,
                    pattern: Box::new(bind_pat(11, "acc")),
                    ty: None,
                    value: Box::new(int_lit(12, "1")),
                },
            ),
            // let acc = acc + 2  (shadow → reassignment)
            node(
                13,
                NodeKind::LetBinding {
                    is_mut: false,
                    pattern: Box::new(bind_pat(14, "acc")),
                    ty: None,
                    value: Box::new(node(
                        15,
                        NodeKind::BinaryOp {
                            op: BinOp::Add,
                            left: Box::new(id_node(16, "acc")),
                            right: Box::new(int_lit(17, "2")),
                        },
                    )),
                },
            ),
        ];
        let f = node(
            20,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Private,
                is_async: false,
                name: ident("run"),
                generic_params: vec![],
                params: vec![],
                return_type: None,
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(block(21, stmts, Some(id_node(22, "acc")))),
            },
        );
        let out = gen(&module(vec![], vec![f]));
        // First binding declares; the second reassigns.
        assert!(
            out.contains("acc = (acc + 2)"),
            "the shadowing re-bind must be an assignment, got: {out}"
        );
        // No second declaration of `acc`.
        assert!(
            out.matches("acc :=").count() + out.matches("var acc").count() <= 1,
            "a shadowing re-bind must not re-declare `acc`, got: {out}"
        );
    }

    /// A `let` shadowing a *parameter* (the same Go scope as the body) must also
    /// reassign, not re-declare.
    #[test]
    fn go_let_shadow_of_param_rebinds_as_assignment() {
        // fn bump(n) { let n = n + 1; n }
        let let_n = node(
            10,
            NodeKind::LetBinding {
                is_mut: false,
                pattern: Box::new(bind_pat(11, "n")),
                ty: None,
                value: Box::new(node(
                    12,
                    NodeKind::BinaryOp {
                        op: BinOp::Add,
                        left: Box::new(id_node(13, "n")),
                        right: Box::new(int_lit(14, "1")),
                    },
                )),
            },
        );
        let f = node(
            20,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Private,
                is_async: false,
                name: ident("bump"),
                generic_params: vec![],
                params: vec![typed_param_node(21, "n", "Int")],
                return_type: Some(Box::new(node(
                    22,
                    NodeKind::TypeNamed {
                        path: type_path(&["Int"]),
                        args: vec![],
                    },
                ))),
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(block(23, vec![let_n], Some(id_node(24, "n")))),
            },
        );
        let out = gen(&module(vec![], vec![f]));
        assert!(
            out.contains("n = (n + 1)"),
            "a `let` shadowing a param must reassign, got: {out}"
        );
        assert!(
            !out.contains("n := (n + 1)") && !out.contains("var n int64 = (n + 1)"),
            "a `let` shadowing a param must not re-declare, got: {out}"
        );
    }

    // ── Q-propagate-operator-noop (go) ────────────────────────────────────────

    /// `let v = expr?` must unwrap the `Ok`/`Some` payload and early-return the
    /// propagated error/None on failure — not pass `expr` through unchanged
    /// (which bound the whole `__bockResult` and never short-circuited).
    #[test]
    fn go_propagate_let_unwraps_and_early_returns() {
        // let v = f()?
        let let_v = node(
            10,
            NodeKind::LetBinding {
                is_mut: false,
                pattern: Box::new(bind_pat(11, "v")),
                ty: None,
                value: Box::new(node(
                    12,
                    NodeKind::Propagate {
                        expr: Box::new(node(
                            13,
                            NodeKind::Call {
                                callee: Box::new(id_node(14, "f")),
                                args: vec![],
                                type_args: vec![],
                            },
                        )),
                    },
                )),
            },
        );
        // fn g() -> Result[Int, String] { let v = f()?; Ok(v) }
        let body_tail = node(
            15,
            NodeKind::Call {
                callee: Box::new(id_node(16, "Ok")),
                args: vec![AirArg {
                    label: None,
                    value: id_node(17, "v"),
                }],
                type_args: vec![],
            },
        );
        let f = node(
            20,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Private,
                is_async: false,
                name: ident("g"),
                generic_params: vec![],
                params: vec![],
                return_type: Some(Box::new(node(
                    22,
                    NodeKind::TypeNamed {
                        path: type_path(&["Result"]),
                        args: vec![
                            node(
                                23,
                                NodeKind::TypeNamed {
                                    path: type_path(&["Int"]),
                                    args: vec![],
                                },
                            ),
                            node(
                                24,
                                NodeKind::TypeNamed {
                                    path: type_path(&["String"]),
                                    args: vec![],
                                },
                            ),
                        ],
                    },
                ))),
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(block(21, vec![let_v], Some(body_tail))),
            },
        );
        let out = gen(&module(vec![], vec![f]));
        // The operand is hoisted; failure early-returns the propagated Err.
        assert!(
            out.contains("__try0 := f()"),
            "propagate must hoist the operand, got: {out}"
        );
        assert!(
            out.contains("if __try0.tag == \"Err\""),
            "propagate must check the Err tag, got: {out}"
        );
        assert!(
            out.contains("return __try0"),
            "propagate must early-return the propagated Err, got: {out}"
        );
        // The success payload is bound to `v` (not the whole `__bockResult`).
        assert!(
            out.contains("v := __try0.v") || out.contains("v := __bockAsInt64(__try0.v)"),
            "propagate must bind the unwrapped payload, got: {out}"
        );
        // It is no longer a no-op passthrough.
        assert!(
            !out.contains("v := f()\n"),
            "propagate must not pass the operand through unchanged, got: {out}"
        );
    }

    /// Build a single-param fn whose body is `return match scrutinee { arms }`,
    /// for exercising the expression-position match lowering.
    fn return_match_fn(name: &str, param: &str, ty: &str, arms: Vec<AIRNode>) -> AIRNode {
        let match_node = node(
            500,
            NodeKind::Match {
                scrutinee: Box::new(id_node(501, param)),
                arms,
            },
        );
        let ret = node(
            502,
            NodeKind::Return {
                value: Some(Box::new(match_node)),
            },
        );
        node(
            1,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Public,
                is_async: false,
                name: ident(name),
                generic_params: vec![],
                params: vec![typed_param_node(2, param, ty)],
                return_type: Some(Box::new(node(
                    3,
                    NodeKind::TypeNamed {
                        path: type_path(&["String"]),
                        args: vec![],
                    },
                ))),
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(block(4, vec![], Some(ret))),
            },
        )
    }

    /// Q-list-range-pattern-shared: a list-pattern `match` in expression position
    /// (`return match items { [] => …; [only] => …; [first, ..rest] => … }`) must
    /// route to the if-chain (the shared recogniser now flags `ListPat`), emitting
    /// a `len(...)` test per arm and positional element / `..rest` slice binds —
    /// not the broken `switch` whose every arm collapsed to `case interface{}:`.
    #[test]
    fn go_list_pattern_expr_match_lowers_to_ifchain_with_binds() {
        let empty_arm = node(
            20,
            NodeKind::MatchArm {
                pattern: Box::new(node(
                    21,
                    NodeKind::ListPat {
                        elems: vec![],
                        rest: None,
                    },
                )),
                guard: None,
                body: Box::new(block(22, vec![], Some(str_lit(23, "empty")))),
            },
        );
        let single_arm = node(
            30,
            NodeKind::MatchArm {
                pattern: Box::new(node(
                    31,
                    NodeKind::ListPat {
                        elems: vec![bind_pat(32, "only")],
                        rest: None,
                    },
                )),
                guard: None,
                body: Box::new(block(33, vec![], Some(id_node(34, "only")))),
            },
        );
        let head_rest_arm = node(
            40,
            NodeKind::MatchArm {
                pattern: Box::new(node(
                    41,
                    NodeKind::ListPat {
                        elems: vec![bind_pat(42, "first")],
                        rest: Some(Box::new(bind_pat(43, "rest"))),
                    },
                )),
                guard: None,
                body: Box::new(block(44, vec![], Some(id_node(45, "first")))),
            },
        );
        let else_arm = node(
            46,
            NodeKind::MatchArm {
                pattern: Box::new(node(47, NodeKind::WildcardPat)),
                guard: None,
                body: Box::new(block(48, vec![], Some(str_lit(49, "other")))),
            },
        );
        let f = return_match_fn(
            "DescribeList",
            "items",
            "List",
            vec![empty_arm, single_arm, head_rest_arm, else_arm],
        );
        let out = gen(&module(vec![], vec![f]));
        // No broken `case interface{}:` placeholder.
        assert!(
            !out.contains("case interface{}"),
            "list-pattern match must not emit a broken `case interface{{}}`, got: {out}"
        );
        // `[]` → exact length 0.
        assert!(
            out.contains("len(items) == 0"),
            "`[]` arm should test len == 0, got: {out}"
        );
        // `[only]` → length 1 and binds `only := items[0]`.
        assert!(
            out.contains("len(items) == 1") && out.contains("only := items[0]"),
            "`[only]` should test len == 1 and bind `only`, got: {out}"
        );
        // `[first, ..rest]` → length >= 1, binds first and a rest slice.
        assert!(
            out.contains("len(items) >= 1"),
            "`[first, ..rest]` should test len >= 1, got: {out}"
        );
        assert!(
            out.contains("first := items[0]") && out.contains("rest := items[1:]"),
            "`[first, ..rest]` should bind `first` and `rest := items[1:]`, got: {out}"
        );
        // The arm bodies return their values (expression-position IIFE).
        assert!(
            out.contains("return \"empty\""),
            "arm bodies must return their value, got: {out}"
        );
    }

    /// Q-list-range-pattern-shared: a range-pattern `match` in expression position
    /// must route to the if-chain with a relational bounds test (`>= lo && < hi`
    /// exclusive, `<=` inclusive) — not a broken `switch`.
    #[test]
    fn go_range_pattern_expr_match_lowers_to_ifchain_with_bounds() {
        let lo_arm = node(
            20,
            NodeKind::MatchArm {
                pattern: Box::new(node(
                    21,
                    NodeKind::RangePat {
                        lo: Box::new(int_lit(22, "1")),
                        hi: Box::new(int_lit(23, "10")),
                        inclusive: false,
                    },
                )),
                guard: None,
                body: Box::new(block(24, vec![], Some(str_lit(25, "a")))),
            },
        );
        let hi_arm = node(
            30,
            NodeKind::MatchArm {
                pattern: Box::new(node(
                    31,
                    NodeKind::RangePat {
                        lo: Box::new(int_lit(32, "10")),
                        hi: Box::new(int_lit(33, "20")),
                        inclusive: true,
                    },
                )),
                guard: None,
                body: Box::new(block(34, vec![], Some(str_lit(35, "b")))),
            },
        );
        let else_arm = node(
            40,
            NodeKind::MatchArm {
                pattern: Box::new(node(41, NodeKind::WildcardPat)),
                guard: None,
                body: Box::new(block(42, vec![], Some(str_lit(43, "c")))),
            },
        );
        let f = return_match_fn("ClassifyRange", "n", "Int", vec![lo_arm, hi_arm, else_arm]);
        let out = gen(&module(vec![], vec![f]));
        assert!(
            !out.contains("case interface{}"),
            "range-pattern match must not emit a broken `case interface{{}}`, got: {out}"
        );
        // Exclusive `1..10` → `n >= 1 && n < 10`.
        assert!(
            out.contains("n >= 1") && out.contains("n < 10"),
            "`1..10` should test `n >= 1 && n < 10`, got: {out}"
        );
        // Inclusive `10..=20` → `n >= 10 && n <= 20`.
        assert!(
            out.contains("n >= 10") && out.contains("n <= 20"),
            "`10..=20` should test `n >= 10 && n <= 20`, got: {out}"
        );
    }

    fn float_lit(id: u32, val: &str) -> AIRNode {
        node(
            id,
            NodeKind::Literal {
                lit: Literal::Float(val.into()),
            },
        )
    }

    fn pow_fn(name: &str, ret_ty: &str, left: AIRNode, right: AIRNode) -> AIRNode {
        let pow = node(
            10,
            NodeKind::BinaryOp {
                op: BinOp::Pow,
                left: Box::new(left),
                right: Box::new(right),
            },
        );
        node(
            1,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Public,
                is_async: false,
                name: ident(name),
                generic_params: vec![],
                params: vec![],
                return_type: Some(Box::new(node(
                    2,
                    NodeKind::TypeNamed {
                        path: type_path(&[ret_ty]),
                        args: vec![],
                    },
                ))),
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(block(3, vec![], Some(pow))),
            },
        )
    }

    #[test]
    fn pow_int_lowers_to_int_pow_helper() {
        // `2 ** 10` (Int ** Int) must NOT emit the broken `(2 /* pow */ 10)`
        // (a Go syntax error) — it lowers to the `__bockIntPow` runtime helper
        // with both operands coerced to `int64`, and the helper is emitted.
        let f = pow_fn("p", "Int", int_lit(11, "2"), int_lit(12, "10"));
        let out = gen(&module(vec![], vec![f]));
        assert!(
            out.contains("__bockIntPow(int64(2), int64(10))"),
            "Int pow should lower to __bockIntPow, got: {out}"
        );
        assert!(
            !out.contains("/* pow */"),
            "Int pow must not emit the broken `/* pow */` form, got: {out}"
        );
        assert!(
            out.contains("func __bockIntPow("),
            "the integer-power runtime helper must be emitted, got: {out}"
        );
    }

    #[test]
    fn pow_float_lowers_to_math_pow() {
        // `2.0 ** 3.0` (Float ** Float) lowers to `math.Pow` (float64 in/out)
        // and pulls in the `math` import.
        let f = pow_fn("p", "Float", float_lit(11, "2.0"), float_lit(12, "3.0"));
        let out = gen(&module(vec![], vec![f]));
        assert!(
            out.contains("math.Pow(float64(2.0), float64(3.0))"),
            "Float pow should lower to math.Pow, got: {out}"
        );
        assert!(
            out.contains("\"math\""),
            "Float pow must import the math package, got: {out}"
        );
        assert!(
            !out.contains("/* pow */"),
            "Float pow must not emit the broken `/* pow */` form, got: {out}"
        );
    }

    fn match_arm(id: u32, pattern: AIRNode, body_str: &str) -> AIRNode {
        node(
            id,
            NodeKind::MatchArm {
                pattern: Box::new(pattern),
                guard: None,
                body: Box::new(block(id + 1, vec![], Some(str_lit(id + 2, body_str)))),
            },
        )
    }

    fn constructor_pat(id: u32, name: &str, fields: Vec<AIRNode>) -> AIRNode {
        node(
            id,
            NodeKind::ConstructorPat {
                path: type_path(&[name]),
                fields,
            },
        )
    }

    #[test]
    fn go_valpos_bind_match_routes_to_ifchain_and_binds() {
        // Q-go-valpos-bind-match: `return match n { x => "got ${x}" }`. A bare
        // bind has no value to switch on, so the value-switch IIFE emitted the
        // broken `case interface{}:` and dropped `x`. It must route to the
        // if-chain, binding `x := n` in an unconditional `else`.
        let arm = match_arm(20, bind_pat(21, "x"), "got it");
        let f = return_match_fn("EchoBinding", "n", "Int", vec![arm]);
        let out = gen(&module(vec![], vec![f]));
        assert!(
            !out.contains("case interface{}"),
            "bind-pattern match must not emit `case interface{{}}`, got: {out}"
        );
        assert!(
            out.contains("x := n"),
            "the bind arm must introduce `x := n`, got: {out}"
        );
    }

    #[test]
    fn go_valpos_nested_optional_match_binds_nested_payload() {
        // Q-go-nested-optional-match: `match val { Some(Ok(n)) => …; Some(Err(e))
        // => …; None => … }`. The nested arm must route to the if-chain (the flat
        // tag-switch dropped the nested `n`), testing both `.tag`s and binding `n`
        // off the typed nested `.v` payload — never `undefined: n`.
        let some_ok = constructor_pat(
            20,
            "Some",
            vec![constructor_pat(21, "Ok", vec![bind_pat(22, "n")])],
        );
        let some_err = constructor_pat(
            30,
            "Some",
            vec![constructor_pat(31, "Err", vec![bind_pat(32, "e")])],
        );
        let none = constructor_pat(40, "None", vec![]);
        let arms = vec![
            match_arm(50, some_ok, "got n"),
            match_arm(53, some_err, "err e"),
            match_arm(56, none, "nothing"),
        ];
        let f = return_match_fn("NestedUnwrap", "val", "Optional", arms);
        let out = gen(&module(vec![], vec![f]));
        assert!(
            out.contains("val.tag == \"Some\"") && out.contains(".tag == \"Ok\""),
            "nested Optional/Result match must test both tags, got: {out}"
        );
        assert!(
            out.contains("n := "),
            "the nested `Ok(n)` payload must be bound, got: {out}"
        );
        assert!(
            !out.contains("case interface{}"),
            "nested match must not emit a broken `case interface{{}}`, got: {out}"
        );
    }

    #[test]
    fn go_valpos_plain_record_match_routes_to_ifchain_and_binds() {
        // Q-plainrecord-valpos-match: `match p { Point { x, .. } => "x=${x}" }`. A
        // plain record is a concrete struct (no sealed-interface type/value to
        // switch on), so the value/type-switch emitted the broken `case Point:`
        // and dropped `x`. It must route to the if-chain, reading `x := p.X`.
        let rec_pat = node(
            20,
            NodeKind::RecordPat {
                path: type_path(&["Point"]),
                fields: vec![bock_air::AirRecordPatternField {
                    name: ident("x"),
                    pattern: None,
                }],
                rest: true,
            },
        );
        let arm = match_arm(30, rec_pat, "x val");
        let f = return_match_fn("GetX", "p", "Point", vec![arm]);
        let out = gen(&module(vec![], vec![f]));
        assert!(
            !out.contains("case Point") && !out.contains("case interface{}"),
            "plain-record match must not emit `case Point` / `case interface{{}}`, got: {out}"
        );
        assert!(
            out.contains("x := p.X"),
            "the plain-record field must be bound as `x := p.X`, got: {out}"
        );
    }

    /// A `Fn(...) -> Void` *parameter* type lowers to a Go `func(...)` with NO
    /// result type — `func() struct{}` (the Void *value* type as a result) is a
    /// function that must `return struct{}{}`, which a void closure body never
    /// does, so the closure would not satisfy the parameter. Guards Item 1's
    /// type-lowering fix (`type_to_go`'s `TypeFunction` arm).
    #[test]
    fn fn_void_param_type_lowers_to_bare_func() {
        // fn on_click(handler: Fn() -> Void) -> Void { handler() }
        let fn_void_ty = node(
            2,
            NodeKind::TypeFunction {
                params: vec![],
                ret: Box::new(type_named_node(3, "Void")),
                effects: vec![],
            },
        );
        let handler_param = node(
            4,
            NodeKind::Param {
                pattern: Box::new(bind_pat(5, "handler")),
                ty: Some(Box::new(fn_void_ty)),
                default: None,
            },
        );
        let call = node(
            6,
            NodeKind::Call {
                callee: Box::new(id_node(7, "handler")),
                args: vec![],
                type_args: vec![],
            },
        );
        let f = node(
            1,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Public,
                is_async: false,
                name: ident("on_click"),
                generic_params: vec![],
                params: vec![handler_param],
                return_type: Some(Box::new(type_named_node(8, "Void"))),
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(block(9, vec![call], None)),
            },
        );
        let out = gen(&module(vec![], vec![f]));
        assert!(
            out.contains("handler func()"),
            "`Fn() -> Void` param should lower to `func()`, got: {out}"
        );
        assert!(
            !out.contains("func() struct{}"),
            "`Fn() -> Void` must NOT emit `func() struct{{}}`, got: {out}"
        );
    }

    /// An inherent (`impl Type`) method whose name a `trait` the type implements
    /// also declares is exported (`Render`, not `render`) so it satisfies the Go
    /// interface directly, AND the redundant same-named `impl Trait for Type`
    /// forwarder (`fn render(self) { self.render() }`) is skipped — emitting it
    /// would produce `func (T) Render() { return self.Render() }`, infinite
    /// recursion. Guards Item 2 (method-name casing + no self-recursive
    /// forwarder).
    #[test]
    fn inherent_method_exported_for_trait_and_no_recursive_forwarder() {
        // trait Component { fn render(self) -> String }
        let trait_method = node(
            10,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Public,
                is_async: false,
                name: ident("render"),
                generic_params: vec![],
                params: vec![param_node(11, "self")],
                return_type: Some(Box::new(type_named_node(12, "String"))),
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(block(13, vec![], None)),
            },
        );
        let trait_decl = node(
            14,
            NodeKind::TraitDecl {
                annotations: vec![],
                visibility: Visibility::Public,
                is_platform: false,
                name: ident("Component"),
                generic_params: vec![],
                associated_types: vec![],
                methods: vec![trait_method],
            },
        );
        // impl Button { fn render(self) -> String { "x" } }  (private, inherent)
        let inherent_method = node(
            20,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Private,
                is_async: false,
                name: ident("render"),
                generic_params: vec![],
                params: vec![param_node(21, "self")],
                return_type: Some(Box::new(type_named_node(22, "String"))),
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(block(23, vec![], Some(str_lit(24, "x")))),
            },
        );
        let inherent_impl = node(
            25,
            NodeKind::ImplBlock {
                annotations: vec![],
                generic_params: vec![],
                trait_path: None,
                trait_args: vec![],
                target: Box::new(type_named_node(26, "Button")),
                where_clause: vec![],
                methods: vec![inherent_method],
            },
        );
        // impl Component for Button { fn render(self) -> String { self.render() } }
        let forwarder_method = node(
            30,
            NodeKind::FnDecl {
                annotations: vec![],
                visibility: Visibility::Public,
                is_async: false,
                name: ident("render"),
                generic_params: vec![],
                params: vec![param_node(31, "self")],
                return_type: Some(Box::new(type_named_node(32, "String"))),
                effect_clause: vec![],
                where_clause: vec![],
                body: Box::new(block(
                    33,
                    vec![],
                    Some(node(
                        34,
                        NodeKind::MethodCall {
                            receiver: Box::new(id_node(35, "self")),
                            method: ident("render"),
                            type_args: vec![],
                            args: vec![],
                        },
                    )),
                )),
            },
        );
        let trait_impl = node(
            36,
            NodeKind::ImplBlock {
                annotations: vec![],
                generic_params: vec![],
                trait_path: Some(type_path(&["Component"])),
                trait_args: vec![],
                target: Box::new(type_named_node(37, "Button")),
                where_clause: vec![],
                methods: vec![forwarder_method],
            },
        );
        let out = gen(&module(vec![], vec![trait_decl, inherent_impl, trait_impl]));
        // The inherent method is exported to `Render`.
        assert!(
            out.contains("Button) Render() string"),
            "inherent method should be exported `Render`, got: {out}"
        );
        // The lowercase inherent name must not survive.
        assert!(
            !out.contains("Button) render() string"),
            "inherent method must not stay lowercase `render`, got: {out}"
        );
        // The self-recursive forwarder must be skipped (only ONE `Render` body).
        assert_eq!(
            out.matches("Button) Render()").count(),
            1,
            "exactly one `Render` method should be emitted on Button, got: {out}"
        );
        assert!(
            !out.contains("return self.Render()"),
            "the self-recursive forwarder must be eliminated, got: {out}"
        );
    }
}