brink-ir 0.0.17

Intermediate representations for inkle's ink narrative scripting language
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
//! `HirFile` → `.brink` source: a faithful pretty-printer over lowered HIR
//! (issue #1178, `docs/b0-sequencing.md` §B0.8b).
//!
//! This is deliberately **shared machinery**, not a one-off converter
//! script: the future `.brink` formatter and printer-based IDE rewrites
//! (folding, refactors) are the other planned consumers of "take an
//! `HirFile` and produce source text for it" — the 2026-07-19 evening
//! ruling (NF-5) that spawned this issue explicitly named that as the
//! reason the old charter §8.5 converter deferral no longer holds.
//!
//! # Scope — only currently-supported constructs
//!
//! The native surface (`brink_syntax_native` + [`super::lower_native`]) does
//! not yet lower every `Stmt`/`Expr` shape the shared HIR types can carry
//! (see `lower_native`'s own module docs for the current judgment calls and
//! gaps). This emitter is **conservative by construction**: every
//! `emit_*` function returns [`EmitError::Unsupported`] the moment it meets
//! a shape it cannot faithfully respell, rather than guessing or emitting
//! partial/lossy text. [`emit_file`] is all-or-nothing — a single
//! unsupported node anywhere in the tree fails the whole call. This is the
//! "never emit invalid syntax" rule from the issue: an `Err` is always
//! preferable to a best-effort guess that might not round-trip.
//!
//! Supported today: `var`/`const`/`flags`/`struct`/`extern` top-level
//! declarations; `flow`/`fn` knots with one level of nested `flow`
//! stitches (native's own Q4(b) two-level fence — `Stitch` has no further
//! nesting field, so this is not a self-imposed limit); params (bare,
//! `ref`, type-annotated); content lines (text, glue, `{expr}`
//! interpolation, trailing tags); diverts (`-> target`, `-> END`,
//! `-> DONE`, with call args); tunnel calls (`-> target ->`); `return` /
//! `return -> target` / `return <expr>` (issue #1973); `{?}` choice points
//! (sticky/once, guards, labels,
//! the `text[bracket]inner` display split, `else {}` fallback, the
//! dissolved-gather continuation — including a **labeled** continuation,
//! and a mid-flow `Stmt::LabeledBlock` wherever it occurs, both via G-1's
//! `(name)` content-line-label spelling, see the "Labeled lines" section
//! above); `{if cond {} else {}}` conditionals — `CondKind::InitialCondition`
//! with at most one else arm carrying no condition of its own; `{match subj
//! {}}` (`CondKind::Switch`). **Correction (issue #1951, 2026-08-01
//! triage):** native's `if`/`else` grammar has supported a flat `else if`
//! chain since #1258/#1261 (2026-07-22) — `lower_native::cond`'s own doc
//! comment and this emitter both used to claim otherwise, which is exactly
//! how that stale claim ended up mis-transcribed into #1335's "no `else if`
//! chain" native-grammar hole. The grammar was never the gap; this emitter
//! is: a native-authored chain always lowers to *nested* `InitialCondition`s
//! (an `else` arm's body containing another `Conditional`), which this
//! emitter's ordinary recursive `emit_conditional` call already reproduces
//! faithfully. **Correction (issue #1975):** `CondKind::IfElse` itself (ink's
//! own independently-chained, no-shared-subject 3+-branch form —
//! `full_corpus_sweep`'s `"IfElse conditional"` bucket) is now **supported**
//! too: `emit_if_else_chain` walks the flat `IfElse` branch list and
//! re-shapes it into the same nested `{if …} else { {if …} else { … } }`
//! structure a native-authored `else if` chain lowers to, rather than
//! refusing outright.
//!
//! Explicitly unsupported (each a real gap, not an oversight — see
//! `docs/b0-sequencing.md` §3 and `tests/tier1-brink-respell/README.md`'s
//! own gap findings for the native-grammar context). **Correction (issue
//! #1991, PR #2002; issue #1972):** `Stmt::TempDecl`/`Assignment`/
//! `ExprStmt` at prose-body position no longer belong in the
//! native-grammar-gap bucket this section used to open with — `~ let name =
//! expr`/`~ x = expr`/`~ expr` (the content-ground logic-line escape,
//! charter §8.2, extended to `TempDecl` by #1972) is exactly that bare
//! prose-body statement, and `lower_native::body::lower_logic_line` has
//! produced all three since #1991/#1972 landed. This printer emits all
//! three (`emit_temp_decl`/`emit_assignment`/`emit_expr_stmt`, below).
//! **Correction (issue #1972, second slice):** `Stmt::LogicBlock`/`Await`
//! at prose-body position — the `~{ … }` multi-statement escape and the
//! `~ until cond` condition-park escape (native's sole `await` spelling) —
//! are likewise no longer a native-**grammar** gap:
//! `lower_native::body::lower_logic_line` produces both, and this printer
//! now emits both too (`emit_logic_block`/`emit_await`, below), **with one
//! carved-out residual**: a `Stmt::LogicBlock` whose `scope` is `Opens`/
//! `Continues` (a *whole* code-ground body split around a nested `> text`
//! line, issue #1992/#2028 — never produced by this content-ground escape,
//! only by a `fn`'s own default body or a `flow`'s whole-body `~{ }`
//! override) still refuses: re-spelling that shape needs `emit_knot`-level
//! restructuring (the original single code-ground body, not a nested
//! `~{ }` per run) that is out of this slice's scope. A `LogicBlock`
//! containing nested `if`/`while`/`for` control flow also still refuses —
//! this printer's new code-ground statement printer
//! (`emit_block_stmt_stream`) only spells the leaf `BlockStmt` shapes
//! (`TempDecl`/`Assignment`/`ExprStmt`/`Return`/`Break`/`Continue`/
//! `Await`), not nested control flow, which would need the full code-ground
//! `if`/`while`/`for` printer this slice doesn't build. `Stmt::Sequence`/
//! `ContentPart::InlineSequence`/`InlineConditional`
//! (alternations `~`/`&`/`!`/`|` — **unlike the code-dialect-ground gaps
//! above, this one is emitter-only, not native-grammar**: native's
//! `ALTERNATION_BLOCK` parses and `lower_native::body`/`expr` already
//! lower it to real `Sequence`/`InlineSequence`/`InlineConditional` HIR
//! today, per a #1335 B0.8b re-check — this emitter has simply never
//! grown the `emit_*` arm for it, a real, closeable, still-open follow-up
//! this slice didn't take on); `Stmt::ThreadStart`
//! (a splice `<- flow(args)` has a ruled spelling *inside* a `{?}` choice
//! point, but only as a sibling of the choice lines around it — the HIR
//! flattens it into an ordinary preceding/trailing statement with no
//! marker of that original nesting, so re-nesting it correctly would need
//! more than this slice's scope; still refused, not guessed);
//! `ContentPart::Spring` (the deferred word-break marker
//! `lower::choice::replace_trailing_ws_with_spring` produces — no native
//! token forces that same runtime-deferred-whitespace behavior, and
//! respelling it as a literal space would silently change what renders,
//! so it stays refused). **Correction (issue #1975):** `CondKind::IfElse`
//! used to be listed here as an emitter-only gap (see the corrected note on
//! `CondKind::InitialCondition` above, issue #1951) — it is now supported,
//! see `emit_if_else_chain` below.
//! **Correction (issue #1974):** `Stmt::ThreadStart` is likewise no longer
//! refused in the two positions the HIR actually flattens it into. A run of
//! splices immediately *preceding* a `Stmt::ChoiceSet` in the same stream
//! re-nests as leading `<- flow(args)` line(s) inside the `{?}` ahead of the
//! first choice (`emit_choice_set`'s `leading`), and a *trailing* run on a
//! choice's own `body.stmts` re-nests as sibling line(s) printed right after
//! that choice, never inside its braces (`split_trailing_thread_starts` /
//! `emit_trailing_thread_starts`). Both adjacencies are unambiguous, so
//! nothing is guessed; a `ThreadStart` in any *other* position (no
//! `ChoiceSet` after the run, or interleaved mid-choice-body) has no legal
//! native spelling at all and is still refused.
//! **Correction (issue #1973):** a prose-body `return` with a value
//! expression used to be listed here as a native-grammar gap —
//! `parser/divert.rs::return_stmt` now parses a trailing value expression
//! at content-ground position (mirroring the code-ground `return expr?;`
//! form it always supported), `lower_native::body`'s `N::RETURN_STMT` arm
//! lowers it, and this emitter now spells it back (`emit_return`, below).
//! Whether a non-function `flow`'s prose body may *semantically* carry a
//! return value stays an open design question (unchanged by this fix) —
//! `brink-analyzer`'s existing E032 ("explicit return outside function")
//! still rejects it there exactly as before; only a `fn`'s value-carrying
//! return round-trips through this emitter today. Most `Expr` variants
//! beyond literals/paths/operators/calls
//! (collections, structs, refs, a divert target used as a value, and
//! `#fn(f, a)` — the *binding* form, which by the 2026-08-01 ruling has no
//! native spelling at all; a **zero-bound** `#fn(f)` does respell, as the
//! bare name `f`, issue #1862);
//! any knot/stitch/decl directive channel (`is_local`, `#@effects`,
//! `#@was`, visibility, doc comments); `IncludeSite`, `ModuleDecl`,
//! file-level `VisibilityDirective`/`was_directives`; `@[allow(…)]`
//! suppression scopes (`HirFile::allow_scopes`, issue #1614/#1161 — the
//! scope carries only a `(range, codes)` fact with no pointer back to the
//! declaration it decorates, so there is no way to re-place the
//! annotation line from here; refused loudly rather than silently
//! dropping the suppression).
//!
//! Type annotations are **supported** in every position the native grammar
//! spells them (NG-A/B/C, issues #1487/#1488/#1489): parameters
//! (`fn f(g: Guest)`), `var`/`const` bindings, and a `flow`/`fn` header's
//! `: type` return clause. They used to be listed above as an unsupported
//! channel — correct while native had no annotation grammar at all, but a
//! `.brink` file can now *contain* them, so refusing to spell them back
//! would make legal native source un-round-trippable.
//!
//! `Import` (`use`/`import` declarations, M-2) is likewise **supported**
//! now: issues #1581/#1590 fixed `Import.module` to be the real
//! `::`-joined module name (not the ink-era `.`-joined, leaf-inclusive
//! string) and made an item alias actually take effect, so the shape this
//! emitter produces is exactly what `hir::lower_native::import` needs to
//! reconstruct it. It used to be listed above as an unsupported channel —
//! correct while `Import.module` couldn't match a real module name even
//! on a clean round-trip, but that is fixed upstream of this emitter now.

use std::fmt::Write as _;

use crate::{
    AssignOp, Assignment, AwaitStmt, Block, BlockStmt, Choice, ChoiceSet, CondBranch, CondKind,
    Conditional, ConstDecl, Content, ContentPart, DivertPath, DivertTarget, Expr, ExternalDecl,
    HirFile, Import, InfixOp, Knot, LambdaBody, ListDecl, LogicBlock, LogicBlockScope, Name, Param,
    Path, PostfixOp, PrefixOp, Return, Stitch, Stmt, StringPart, StructDecl, Tag, TempDecl,
    ThreadStart, TypeExpr, VarDecl,
};

// ─── Labeled lines (G-1) ─────────────────────────────────────────────
//
// `tests/tier1-brink-respell/README.md`'s G-1 finding ("no ruled spelling
// for a labeled mid-flow re-entry point") was ruled 2026-07-20 ("label
// any content line", landed in `brink-syntax-native`'s
// `content::at_content_label`/`label`): every content line now takes an
// optional leading `(name)` — the native spelling for both a mid-flow
// labeled re-entry point
// (`Stmt::LabeledBlock`, `lower_native::body::lower_items`'s
// label-absorption) and a labeled dissolved-gather continuation
// (`ChoiceSet.continuation.label`, `lower_native::body::lower_continuation`).
// Neither construct is a distinct `Stmt` shape on the way back out — the
// label decorates whichever line the absorbed stream's *first* item
// happens to render as, exactly mirroring how `lower_content_line_body`
// built it going in. [`emit_labeled_stmt_stream`] is the shared reverse:
// it recognizes the same handful of leading shapes
// `lower_content_line_body`/`lower_content_run` can produce for a single
// content line (content alone, content sharing its line with a divert or
// single-hop tunnel call) and refuses — never guesses — anything else.

/// Why [`emit_file`] refused to produce source text. Always fatal to the
/// whole call — see the module doc's "never emit invalid syntax" rule.
#[derive(Debug, Clone, thiserror::Error)]
pub enum EmitError {
    /// A construct with no known (or no faithful) native spelling.
    /// `what` names the construct; `context` is a short breadcrumb (e.g.
    /// the enclosing knot's name) since HIR nodes carry no stable text
    /// location an emit-time error can point at cheaply.
    #[error("unsupported for native emission: {what} ({context})")]
    Unsupported { what: &'static str, context: String },
    /// `root_content` is non-empty and needs wrapping in a synthesized
    /// `flow main() { … }` (the native story-entry convention,
    /// `lower_native::entry_root_content`'s doc), but a top-level `flow`/
    /// `fn` named `main` already exists — emitting both would either
    /// collide or silently drop one. Not fabricated a rename; surfaced.
    #[error(
        "root content needs a synthesized `flow main()`, but a top-level `main` already exists"
    )]
    RootMainCollision,
}

fn unsupported(what: &'static str, context: impl Into<String>) -> EmitError {
    EmitError::Unsupported {
        what,
        context: context.into(),
    }
}

/// Recognize `lower_native::entry_root_content`'s synthesized shape: a
/// `root_content` consisting of exactly one bare `-> main` divert, matching
/// a real top-level, zero-param, non-`fn` `main` knot. See this function's
/// call site for why that shape must not be re-wrapped.
fn is_synthetic_main_entry(hir: &HirFile) -> bool {
    let [Stmt::Divert(d)] = hir.root_content.stmts.as_slice() else {
        return false;
    };
    let DivertPath::Path(p) = &d.target.path else {
        return false;
    };
    if !d.target.args.is_empty() {
        return false;
    }
    let [seg] = p.segments.as_slice() else {
        return false;
    };
    if seg.text != "main" {
        return false;
    }
    hir.knots
        .iter()
        .any(|k| k.name.text == "main" && k.params.is_empty() && !k.is_function)
}

/// Refuse the file-level channels [`emit_file`] has no spelling for at all
/// (as opposed to the per-declaration channels each `emit_*` function
/// checks itself). Split out of [`emit_file`] purely to keep that function
/// under clippy's line-count lint — these checks have no data to
/// contribute to the emitted text either way.
fn refuse_unsupported_file_channels(hir: &HirFile) -> Result<(), EmitError> {
    if !hir.includes.is_empty() {
        return Err(unsupported("INCLUDE sites", "file"));
    }
    if hir.module.is_some() {
        return Err(unsupported("#@module directive", "file"));
    }
    if !hir.visibility.is_empty() || !hir.was_directives.is_empty() {
        return Err(unsupported(
            "file-level visibility/#@was directives",
            "file",
        ));
    }
    // `@[allow(Exxx, …)]` scopes (issue #1614/#1161) carry only a
    // `(range, codes)` fact — no pointer back to the declaration they
    // decorate — so there is no way to re-place the annotation line above
    // the right node from here. Refusing loudly beats the alternative of
    // silently dropping the suppression (a `.brink` round-trip that
    // quietly stops silencing a diagnostic the author explicitly allowed
    // is a correctness regression, not a formatting nit).
    if !hir.allow_scopes.is_empty() {
        return Err(unsupported("@[allow(…)] suppression scopes", "file"));
    }
    // `HirFile::element_matches` (issue #1838) needs no guard of its own:
    // it is *derived* from a claiming `@[element(claims = "…")]`, and
    // `emit_knot`/`emit_stitch` already refuse any container carrying an
    // `element_annotation`, so a file with matches can never reach a
    // successful emit in the first place.
    Ok(())
}

/// Emit a complete `.brink` source file from `hir`.
///
/// All-or-nothing: the first unsupported construct anywhere in the tree
/// fails the whole call (see the module doc). Ordering is canonical, not
/// source-position-preserving — `imports`, `variables`, `constants`,
/// `lists` (`flags`), `structs`, `externals`, then `knots` (each knot's
/// own body content precedes its nested stitches, since `Knot.body`/
/// `Knot.stitches` are separate fields with no shared interleaving order
/// to reconstruct). A non-empty `root_content` is wrapped in a
/// synthesized top-level `flow main() { … }` (see
/// [`EmitError::RootMainCollision`]).
pub fn emit_file(hir: &HirFile) -> Result<String, EmitError> {
    refuse_unsupported_file_channels(hir)?;

    let mut out = String::new();
    let mut wrote_any = false;

    let before = out.len();
    for import in &hir.imports {
        emit_import(&mut out, import);
    }
    if out.len() != before {
        out.push('\n');
        wrote_any = true;
    }

    for v in &hir.variables {
        emit_var_decl(&mut out, v)?;
        wrote_any = true;
    }
    if wrote_any {
        out.push('\n');
    }

    let before = out.len();
    for c in &hir.constants {
        emit_const_decl(&mut out, c)?;
    }
    if out.len() != before {
        out.push('\n');
        wrote_any = true;
    }

    let before = out.len();
    for l in &hir.lists {
        emit_flags_decl(&mut out, l)?;
    }
    if out.len() != before {
        out.push('\n');
        wrote_any = true;
    }

    let before = out.len();
    for s in &hir.structs {
        emit_struct_decl(&mut out, s)?;
        out.push('\n');
    }
    if out.len() != before {
        wrote_any = true;
    }

    let before = out.len();
    for e in &hir.externals {
        emit_external_decl(&mut out, e)?;
    }
    if out.len() != before {
        out.push('\n');
        wrote_any = true;
    }

    let mut knots: Vec<&Knot> = hir.knots.iter().collect();
    let synthetic_main;
    // `lower_native::entry_root_content` synthesizes `root_content` as a
    // single `-> main` divert whenever a top-level, zero-param, non-`fn`
    // `main` knot exists (the native story-entry convention, charter §15)
    // — that shape is *already* representable by the real `main` knot
    // alone once re-lowered, so it must not be re-wrapped (a native
    // fixture round-tripping through this emitter would otherwise always
    // hit `RootMainCollision`, since `main` legitimately exists both as a
    // knot and as this synthesized entry). Only a *genuine* non-empty
    // root content (ink's literal pre-first-knot weave) needs wrapping.
    if !hir.root_content.stmts.is_empty() && !is_synthetic_main_entry(hir) {
        if knots
            .iter()
            .any(|k| k.name.text == "main" && k.params.is_empty())
        {
            return Err(EmitError::RootMainCollision);
        }
        let empty_range = rowan::TextRange::empty(rowan::TextSize::from(0));
        synthetic_main = Knot {
            ptr: crate::provenance::Provenance::synthetic(
                crate::provenance::NodeClass::Knot,
                empty_range,
            ),
            name: Name {
                text: "main".to_string(),
                range: empty_range,
            },
            is_function: false,
            params: Vec::new(),
            body: hir.root_content.clone(),
            stitches: Vec::new(),
            is_local: false,
            effects_assertion: None,
            element_annotation: None,
            convention_annotation: None,
            style_annotation: None,
            return_type: None,
            doc: None,
            visibility: None,
            was: None,
        };
        knots.insert(0, &synthetic_main);
    }

    for (i, knot) in knots.iter().enumerate() {
        if i > 0 {
            out.push('\n');
        }
        emit_knot(&mut out, knot)?;
    }
    let _ = wrote_any;

    Ok(out)
}

// ─── Top-level declarations ─────────────────────────────────────────

fn emit_var_decl(out: &mut String, v: &VarDecl) -> Result<(), EmitError> {
    if v.is_local || v.doc.is_some() || v.was.is_some() {
        return Err(unsupported("var directive channel", &v.name.text));
    }
    let pub_kw = pub_prefix(v.visibility, "var directive channel", &v.name.text)?;
    let ty = emit_annotation_suffix(v.annotation.as_ref());
    let value = emit_expr(&v.value, &v.name.text)?;
    let _ = writeln!(out, "{pub_kw}var {}{ty} = {value}", v.name.text);
    Ok(())
}

fn emit_const_decl(out: &mut String, c: &ConstDecl) -> Result<(), EmitError> {
    if c.doc.is_some() || c.was.is_some() {
        return Err(unsupported("const directive channel", &c.name.text));
    }
    let pub_kw = pub_prefix(c.visibility, "const directive channel", &c.name.text)?;
    let ty = emit_annotation_suffix(c.annotation.as_ref());
    let value = emit_expr(&c.value, &c.name.text)?;
    let _ = writeln!(out, "{pub_kw}const {}{ty} = {value}", c.name.text);
    Ok(())
}

/// `"pub "` for `Some(VisibilityMark::Public)`, `""` for `None` — the native
/// spelling for the seven `pub`-eligible declaration kinds (issue #1582,
/// RULED 2026-08-03, `docs/decision-log.md` "Native visibility marker: a
/// `pub` keyword"; `crates/internal/brink-syntax-native/src/parser/decl.rs`'s
/// `eat_pub`/seven `at_pub_*_decl` guards are the parser side this mirrors).
///
/// `Some(VisibilityMark::Private)` has no native spelling to fall back to —
/// unlike `pub`'s single keyword, native has no `priv`/`private` keyword and
/// no `#@private` tag-directive channel (that's ink-dialect-only, via
/// `hir::lower::directive`); native lowering
/// (`lower_native::decl::visibility_mark`, plus the knot/stitch sites in
/// `lower_native::container`) can only ever produce `None` or
/// `Some(Public)`. Nor can ink-sourced HIR deliver a `Some(Private)` here in
/// practice: ink's `#@private`/`#@public` directives also populate
/// `HirFile::visibility`, and `refuse_unsupported_file_channels` (above)
/// refuses any non-empty file-level visibility list before per-decl emission
/// runs. This arm is therefore defense-in-depth against hand-constructed
/// HIR — kept refused because the variant genuinely has nothing to
/// round-trip to, not because any current producer reaches it. (If the
/// file-level refusal is ever lifted, note an ink `#@public` file without
/// `#@module` would respell into a single-file `.brink` whose top-level
/// `pub` immediately warns E092 — the redundant-override diagnostic.)
fn pub_prefix(
    visibility: Option<crate::VisibilityMark>,
    channel: &'static str,
    name: &str,
) -> Result<&'static str, EmitError> {
    match visibility {
        None => Ok(""),
        Some(crate::VisibilityMark::Public) => Ok("pub "),
        Some(crate::VisibilityMark::Private) => Err(unsupported(channel, name)),
    }
}

/// `": T"` for an annotated binding or return clause, `""` when absent.
/// Used both for the text between a `var`/`const` name and its `=` (NG-B,
/// issue #1488) and, in [`emit_knot`], for a `fn`/`flow` header's `: type`
/// return clause after the parameter list (NG-C, issue #1489) — the
/// rendering is identical, only what follows the annotation differs.
fn emit_annotation_suffix(annotation: Option<&TypeExpr>) -> String {
    annotation.map_or_else(String::new, |ty| format!(": {}", emit_type(ty)))
}

fn emit_flags_decl(out: &mut String, l: &ListDecl) -> Result<(), EmitError> {
    if l.doc.is_some() || l.was.is_some() {
        return Err(unsupported("flags directive channel", &l.name.text));
    }
    let pub_kw = pub_prefix(l.visibility, "flags directive channel", &l.name.text)?;
    let members: Vec<String> = l
        .members
        .iter()
        .map(|m| {
            let mut s = m.name.text.clone();
            if let Some(v) = m.value {
                let _ = write!(s, " = {v}");
            }
            if m.is_active { format!("({s})") } else { s }
        })
        .collect();
    let _ = writeln!(
        out,
        "{pub_kw}flags {} = {}",
        l.name.text,
        members.join(", ")
    );
    Ok(())
}

fn emit_struct_decl(out: &mut String, s: &StructDecl) -> Result<(), EmitError> {
    if s.doc.is_some() {
        return Err(unsupported("struct directive channel", &s.name.text));
    }
    let pub_kw = pub_prefix(s.visibility, "struct directive channel", &s.name.text)?;
    let _ = writeln!(out, "{pub_kw}struct {} {{", s.name.text);
    for f in &s.fields {
        let ty = emit_type(&f.ty);
        let _ = writeln!(out, "  {}: {ty}", f.name.text);
    }
    let _ = writeln!(out, "}}");
    Ok(())
}

fn emit_external_decl(out: &mut String, e: &ExternalDecl) -> Result<(), EmitError> {
    if e.doc.is_some() || e.was.is_some() {
        return Err(unsupported("extern directive channel", &e.name.text));
    }
    let pub_kw = pub_prefix(e.visibility, "extern directive channel", &e.name.text)?;
    let params: Vec<&str> = e.params.iter().map(|p| p.name.as_str()).collect();
    let _ = writeln!(out, "{pub_kw}extern {}({})", e.name.text, params.join(", "));
    Ok(())
}

/// `Import.module`/`items`/`bare` now round-trip faithfully on their own
/// (issues #1581/#1590 fixed the `::`-joining and honored aliases, both
/// upstream of this emitter — the whole `Import` shape it produces is
/// exactly what `hir::lower_native::import` needs to reconstruct it), so
/// the emitter's own blanket refusal predates that fix, not a still-live
/// gap. `bare: false` is the qualified form (`import module`, no leaf
/// item — the same shape a single-segment `use module;` also produces, so
/// `import` is the canonical spelling for either origin, not a guess).
/// `bare: true` always uses the `use module::{items};` brace form, even
/// for a single item — `use module::item;`'s shorthand is a *parse*
/// convenience the grammar also accepts, but re-lowering the brace form
/// yields an identical `Import`, so there is no faithfulness reason to
/// prefer the shorthand here. Like [`emit_type`], every `Import` shape has
/// a faithful spelling, so this is infallible too.
///
/// `import`'s own grammar (`parser::decl::import_decl`) never consumes a
/// trailing `;` — unlike every other native declaration, and unlike `use`
/// (whose `;` is optional but recognized, `parser::decl::use_decl`'s own
/// doc) — so a `;` after `import module` is not part of the statement at
/// all; it would parse as a *second*, out-of-position top-level construct
/// and diagnose `E129`. Only `use`'s brace form gets one here.
fn emit_import(out: &mut String, import: &Import) {
    if import.bare {
        let items: Vec<String> = import
            .items
            .iter()
            .map(|item| match &item.alias {
                Some(alias) => format!("{} as {alias}", item.name),
                None => item.name.clone(),
            })
            .collect();
        let _ = writeln!(out, "use {}::{{{}}};", import.module, items.join(", "));
    } else {
        let _ = writeln!(out, "import {}", import.module);
    }
}

/// Every `TypeExpr` shape has a faithful native spelling (NG-A/B/C,
/// issues #1487/#1488/#1489), so unlike the rest of this module's
/// `emit_*` functions, this one is infallible — there is no
/// `EmitError::Unsupported` arm to grow here.
fn emit_type(ty: &TypeExpr) -> String {
    match ty {
        TypeExpr::Named { name, .. } => name.clone(),
        TypeExpr::Generic { name, args, .. } => {
            let rendered: Vec<String> = args.iter().map(emit_type).collect();
            format!("{name}<{}>", rendered.join(", "))
        }
        TypeExpr::Fn { params, ret, .. } => {
            let rendered: Vec<String> = params.iter().map(emit_type).collect();
            format!("fn({}): {}", rendered.join(", "), emit_type(ret))
        }
    }
}

fn emit_param(p: &Param, context: &str) -> Result<String, EmitError> {
    if p.is_divert {
        return Err(unsupported("divert-typed parameter", context));
    }
    let mut s = String::new();
    if p.is_ref {
        s.push_str("ref ");
    }
    s.push_str(&p.name.text);
    if let Some(ty) = &p.annotation {
        let _ = write!(s, ": {}", emit_type(ty));
    }
    Ok(s)
}

fn emit_params(params: &[Param], context: &str) -> Result<String, EmitError> {
    let rendered: Result<Vec<String>, EmitError> =
        params.iter().map(|p| emit_param(p, context)).collect();
    Ok(rendered?.join(", "))
}

// ─── Containers ──────────────────────────────────────────────────────

fn emit_knot(out: &mut String, k: &Knot) -> Result<(), EmitError> {
    if k.is_local
        || k.effects_assertion.is_some()
        || k.element_annotation.is_some()
        || k.convention_annotation.is_some()
        || k.style_annotation.is_some()
        || k.doc.is_some()
        || k.was.is_some()
    {
        return Err(unsupported("knot directive/doc channel", &k.name.text));
    }
    let pub_kw = pub_prefix(k.visibility, "knot directive/doc channel", &k.name.text)?;
    let keyword = if k.is_function { "fn" } else { "flow" };
    let params = emit_params(&k.params, &k.name.text)?;
    // The ruled `: type` return clause goes after the parameter list
    // (NG-C, issue #1489) — never before the `(`, and never as an arrow.
    let ret = emit_annotation_suffix(k.return_type.as_ref());
    // Body-dialect selector (charter §4, RULED 2026-07-23): plain `{ }` is
    // the per-keyword *default* — `fn` → code-ground `STMT_BLOCK`, `flow` →
    // prose-ground `BLOCK`. This printer's statement stream
    // (`emit_stmt_stream`/`emit_return`/`emit_temp_decl`/etc.) only ever
    // spells **prose-ground** syntax (bare `return`, no `;`; the `~ …`
    // content-ground logic-line escape) — it never emits the `;`-terminated
    // code-ground form. A `flow`'s bare `{` already matches its prose
    // default, so it needs no override. A `fn`'s bare `{` would instead
    // select the *code*-ground default, and reparsing prose-ground
    // statements under that dialect fails (issue #2029) — so a `fn`'s body
    // always needs the explicit `>{ }` prose override to reparse into the
    // same shape this printer just wrote.
    let selector = if k.is_function { ">" } else { "" };
    let _ = writeln!(
        out,
        "{pub_kw}{keyword} {}({params}){ret} {selector}{{",
        k.name.text
    );
    emit_block_stmts(out, &k.body, 1, &k.name.text)?;
    for s in &k.stitches {
        emit_stitch(out, s, 1)?;
    }
    let _ = writeln!(out, "}}");
    Ok(())
}

fn emit_stitch(out: &mut String, s: &Stitch, depth: usize) -> Result<(), EmitError> {
    if s.is_local
        || s.effects_assertion.is_some()
        || s.element_annotation.is_some()
        || s.convention_annotation.is_some()
        || s.style_annotation.is_some()
        || s.doc.is_some()
        || s.was.is_some()
    {
        return Err(unsupported("stitch directive/doc channel", &s.name.text));
    }
    let pub_kw = pub_prefix(s.visibility, "stitch directive/doc channel", &s.name.text)?;
    let indent = "  ".repeat(depth);
    let params = emit_params(&s.params, &s.name.text)?;
    // The ruled `: type` return clause (NG-C, issue #1489, widened to
    // stitches by #1509) — same position as a top-level `flow`/`fn`'s, see
    // `emit_knot`.
    let ret = emit_annotation_suffix(s.return_type.as_ref());
    let _ = writeln!(
        out,
        "{indent}{pub_kw}flow {}({params}){ret} {{",
        s.name.text
    );
    emit_block_stmts(out, &s.body, depth + 1, &s.name.text)?;
    let _ = writeln!(out, "{indent}}}");
    Ok(())
}

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

/// Emit a block's statement stream at `depth` levels of two-space indent.
///
/// `Stmt::ChoiceSet` is the one shape that isn't a simple one-line-per-stmt
/// mapping: per the dissolved-gather model (`lower_native::body`'s module
/// doc), a `{?}` choice point always absorbs everything lexically after it
/// into its own `continuation` — so on the way back out, the continuation's
/// statements are flattened back into the *same* line stream immediately
/// after the closing `}`, not nested inside another block.
fn emit_block_stmts(
    out: &mut String,
    block: &Block,
    depth: usize,
    context: &str,
) -> Result<(), EmitError> {
    if block.label.is_some() {
        return Err(unsupported("labeled block", context));
    }
    emit_stmt_stream(out, &block.stmts, depth, context)
}

fn emit_stmt_stream(
    out: &mut String,
    stmts: &[Stmt],
    depth: usize,
    context: &str,
) -> Result<(), EmitError> {
    let indent = "  ".repeat(depth);
    let mut i = 0;
    while i < stmts.len() {
        let stmt = &stmts[i];
        match stmt {
            Stmt::EndOfLine => {}
            // A `Content` with no `Stmt::EndOfLine` immediately after it
            // (checked by peeking ahead, not by anything on `Content`
            // itself — the marker is structural, sitting in the stream)
            // means the source had a divert/tunnel-call on the *same*
            // physical line right after the text
            // (`lower_content_run`'s interior `flush_content(..., false)`
            // before an embedded `DIVERT_STMT`/`TUNNEL_CALL`, e.g. `Bye.
            // -> b`). Emitting them as two separate output lines would
            // silently insert a real line break the source never had —
            // re-lowering would then see a genuine trailing
            // `Stmt::EndOfLine` after the content that the original
            // never carried, an observable behavior change (an extra
            // newline in play output), not just a formatting choice.
            Stmt::Content(c) => {
                let same_line_divert = match stmts.get(i + 1) {
                    Some(Stmt::Divert(d)) => Some(emit_divert_target(&d.target, context)?),
                    Some(Stmt::TunnelCall(t)) if t.targets.len() == 1 => Some(format!(
                        "{} ->",
                        emit_divert_target(&t.targets[0], context)?
                    )),
                    _ => None,
                };
                if let Some(divert_text) = same_line_divert {
                    let text =
                        escape_leading_line_start_sigil(&emit_content_parts(&c.parts, context)?);
                    if !c.tags.is_empty() {
                        return Err(unsupported(
                            "tags on a content line sharing its line with a divert",
                            context,
                        ));
                    }
                    let _ = writeln!(out, "{indent}{text}-> {divert_text}");
                    i += 2;
                    continue;
                }
                emit_content_line(out, &indent, c, context)?;
            }
            Stmt::Divert(d) => {
                let target = emit_divert_target(&d.target, context)?;
                let _ = writeln!(out, "{indent}-> {target}");
            }
            Stmt::TunnelCall(t) => {
                if t.targets.len() != 1 {
                    return Err(unsupported("multi-hop tunnel chain", context));
                }
                let target = emit_divert_target(&t.targets[0], context)?;
                let _ = writeln!(out, "{indent}-> {target} ->");
            }
            Stmt::Return(r) => {
                let line = emit_return(r, context)?;
                let _ = writeln!(out, "{indent}{line}");
            }
            Stmt::ChoiceSet(cs) => {
                emit_choice_set_and_continuation(out, &indent, depth, cs, &[], context)?;
                return Ok(());
            }
            Stmt::Conditional(cond) => emit_conditional(out, &indent, depth, cond, context)?,
            Stmt::LabeledBlock(b) => {
                // G-1's other absorption shape: a `(name)` label on an
                // ordinary mid-flow content line (`lower_items`'s own
                // label-absorption arm) wraps everything after it in a
                // `LabeledBlock` that is *always* the last statement of
                // whatever stream produced it (`lower_items` returns
                // immediately after pushing it) — never nested, always
                // flattened back into the surrounding stream. Same
                // `(name)`-prefix-on-the-first-line spelling as a labeled
                // continuation, via the same shared helper.
                let Some(label) = &b.label else {
                    // Not reachable from native lowering (a `LabeledBlock`
                    // is only ever constructed with a label) — refuse
                    // rather than silently emitting an unlabeled block as
                    // if it were an ordinary flattened stream.
                    return Err(unsupported("labeled block without a label", context));
                };
                emit_labeled_stmt_stream(out, label, &b.stmts, depth, context)?;
                return Ok(());
            }
            Stmt::Sequence(_) => return Err(unsupported("alternation sequence", context)),
            Stmt::TempDecl(t) => {
                let line = emit_temp_decl(t, context)?;
                let _ = writeln!(out, "{indent}{line}");
            }
            Stmt::Assignment(a) => {
                let line = emit_assignment(a, context)?;
                let _ = writeln!(out, "{indent}{line}");
            }
            Stmt::ExprStmt(e) => {
                let line = emit_expr_stmt(e, context)?;
                let _ = writeln!(out, "{indent}{line}");
            }
            Stmt::ThreadStart(_) => {
                // A splice reached *before* any choice line in a `{?}`
                // point lowers as a plain sibling statement immediately
                // preceding the resulting `Stmt::ChoiceSet` — never
                // flattened away, just not marked (`lower/block/weave.rs`'s
                // `addContentToPreviousWeavePoint`-mirroring fold and
                // `lower_native::choice::lower_choice_point`'s own
                // `preamble` both produce exactly this adjacency; see
                // their module docs). So a run of one or more consecutive
                // `ThreadStart`s immediately followed by a `ChoiceSet` in
                // *this* stream is re-nestable: pull the run out and hand
                // it to `emit_choice_set` to print as leading splice
                // line(s) inside the `{?}` block, ahead of the first
                // choice/`else`. A `ThreadStart` with no `ChoiceSet`
                // immediately after the run has no legal native spelling
                // at all (a splice outside a choice point is deliberately
                // refused by the grammar, charter §11, ruling
                // #1260/#1263) — refuse rather than guess.
                let mut j = i;
                while matches!(stmts.get(j), Some(Stmt::ThreadStart(_))) {
                    j += 1;
                }
                let Some(Stmt::ChoiceSet(cs)) = stmts.get(j) else {
                    return Err(unsupported("thread-start splice", context));
                };
                let leading: Vec<&ThreadStart> = stmts[i..j]
                    .iter()
                    .map(|s| match s {
                        Stmt::ThreadStart(t) => t,
                        _ => unreachable!("loop above only advances over ThreadStart"),
                    })
                    .collect();
                emit_choice_set_and_continuation(out, &indent, depth, cs, &leading, context)?;
                return Ok(());
            }
            Stmt::LogicBlock(lb) => emit_logic_block(out, &indent, depth, lb, context)?,
            Stmt::Await(a) => {
                let line = emit_await(a, context)?;
                let _ = writeln!(out, "{indent}{line}");
            }
            // Issue #2108: `AttachElement`/`EndElementRun` are synthesized
            // by `hir::lower_native::element::try_claim`'s attach-mode
            // rewrite — an attach convention's claimed line has already
            // been consumed as source text (the whole point of the ruled
            // model, "an attaching convention CONSUMES its own line") and
            // has no native spelling of its own left to respell. Refuse
            // rather than guess, matching this emitter's own conservative-
            // by-construction contract.
            Stmt::AttachElement(_) | Stmt::EndElementRun => {
                return Err(unsupported("attach-mode element rewrite", context));
            }
        }
        i += 1;
    }
    Ok(())
}

/// Emit a statement stream whose *first* line carries an implicit
/// `(label)` prefix — the G-1 shape shared by [`Stmt::LabeledBlock`] and a
/// labeled `ChoiceSet` continuation (see this module's "Labeled lines"
/// section doc). `label` is not itself a statement; it decorates whatever
/// `lower_content_line_body`/`lower_content_run` produced for the labeled
/// line, so this recognizes exactly the leading shapes those functions can
/// produce for a single content line: content alone, or content sharing
/// its line with a divert/single-hop tunnel call (mirroring
/// `emit_stmt_stream`'s own `same_line_divert` peek), plus the empty-`stmts`
/// case (a bare label with no content on its own line and nothing after
/// it — `flush_content`'s empty-flush short-circuit means the labeled
/// line contributes no `Stmt` of its own when it has no text). A shape
/// this function doesn't recognize is refused, not guessed, per the
/// module's "never emit invalid/lossy syntax" rule.
fn emit_labeled_stmt_stream(
    out: &mut String,
    label: &Name,
    stmts: &[Stmt],
    depth: usize,
    context: &str,
) -> Result<(), EmitError> {
    let indent = "  ".repeat(depth);
    let mut head = format!("{indent}({})", label.text);

    match stmts {
        [Stmt::Content(c), Stmt::Divert(d), rest @ ..] => {
            if !c.tags.is_empty() {
                return Err(unsupported(
                    "tags on a labeled content line sharing its line with a divert",
                    context,
                ));
            }
            let text = emit_content_parts(&c.parts, context)?;
            if !text.is_empty() {
                let _ = write!(head, " {text}");
            }
            let target = emit_divert_target(&d.target, context)?;
            let _ = writeln!(out, "{head}-> {target}");
            emit_stmt_stream(out, rest, depth, context)
        }
        [Stmt::Content(c), Stmt::TunnelCall(t), rest @ ..] if t.targets.len() == 1 => {
            if !c.tags.is_empty() {
                return Err(unsupported(
                    "tags on a labeled content line sharing its line with a tunnel call",
                    context,
                ));
            }
            let text = emit_content_parts(&c.parts, context)?;
            if !text.is_empty() {
                let _ = write!(head, " {text}");
            }
            let target = emit_divert_target(&t.targets[0], context)?;
            let _ = writeln!(out, "{head}-> {target} ->");
            emit_stmt_stream(out, rest, depth, context)
        }
        [Stmt::Content(c), Stmt::EndOfLine, rest @ ..] => {
            let text = emit_content_parts(&c.parts, context)?;
            if !text.is_empty() {
                let _ = write!(head, " {text}");
            }
            for tag in &c.tags {
                let _ = write!(head, " #{}", emit_tag(tag, context)?);
            }
            let _ = writeln!(out, "{head}");
            emit_stmt_stream(out, rest, depth, context)
        }
        // A `Content`-leading shape none of the three arms above matched
        // (e.g. tags or trailing statements this function doesn't yet
        // recognize) — a real gap, refused rather than guessed.
        [Stmt::Content(_), ..] => Err(unsupported(
            "labeled line with an unsupported leading shape",
            context,
        )),
        // The labeled line's own content was empty (`flush_content`'s
        // empty-flush short-circuit, `lower_native::body`: a bare `(name)`
        // with nothing else on its line produces no `Content` of its own)
        // and whatever follows isn't a `Content` line either — most often a
        // `{?}` choice point sitting directly under the label with nothing
        // between them (`tests/tier1/choices/I093-default-choices`'s
        // `- (start)` immediately followed by choice lines), but this
        // covers any non-`Content` leading shape uniformly: a bare label
        // line, then the rest of the stream emitted normally at the same
        // depth. Subsumes the old all-consumed `[]` case (`emit_stmt_stream`
        // on an empty slice is a no-op), so it is folded in here rather
        // than kept as a separate arm.
        rest => {
            let _ = writeln!(out, "{head}");
            emit_stmt_stream(out, rest, depth, context)
        }
    }
}

/// `~ let name: type = expr` — the content-ground `Stmt::TempDecl` printer
/// (issue #1972). Reuses [`emit_annotation_suffix`] for the same `": T"`
/// rendering `var`/`const` bindings already use — the ascription is the
/// identical `TypeExpr` shape in every binding position.
fn emit_temp_decl(t: &TempDecl, context: &str) -> Result<String, EmitError> {
    let ty = emit_annotation_suffix(t.annotation.as_ref());
    match &t.value {
        Some(v) => {
            let value = emit_expr(v, context)?;
            Ok(format!("~ let {}{ty} = {value}", t.name.text))
        }
        None => Ok(format!("~ let {}{ty}", t.name.text)),
    }
}

/// `~ x = expr` / `~ x += expr` / `~ x -= expr` — the content-ground
/// `Stmt::Assignment` printer (issue #1991/#1972). `target` is always an
/// `Expr::Path` in practice (`lower_native::control_flow::lower_assignment`
/// never constructs any other shape there), but `emit_expr` is reused
/// rather than special-cased so a target this emitter can't yet spell
/// still refuses loudly instead of being assumed away.
fn emit_assignment(a: &Assignment, context: &str) -> Result<String, EmitError> {
    let target = emit_expr(&a.target, context)?;
    let op = match a.op {
        AssignOp::Set => "=",
        AssignOp::Add => "+=",
        AssignOp::Sub => "-=",
    };
    let value = emit_expr(&a.value, context)?;
    Ok(format!("~ {target} {op} {value}"))
}

/// `~ expr` — the content-ground `Stmt::ExprStmt` printer (issue
/// #1991/#1972), an expression evaluated for its side effect (a function
/// call being the overwhelmingly common case).
fn emit_expr_stmt(e: &Expr, context: &str) -> Result<String, EmitError> {
    Ok(format!("~ {}", emit_expr(e, context)?))
}

/// `~ until cond` — the content-ground `Stmt::Await` printer (issue #1972).
/// `until` is native's sole flow-suspension spelling (decision-log
/// 2026-07-23 item 4, retiring `await`) — reused verbatim whether the
/// `AwaitStmt` came from this content-ground escape or the ink-dialect's own
/// `~ await cond` (this emitter is shared across both dialects' lowered
/// HIR). `AwaitStmt.condition` is `None` only for a malformed source whose
/// condition already failed to parse (`lower_until_stmt`'s doc) — never the
/// shape of a real source this emitter is asked to respell — so refuse
/// rather than guess at a spelling for it.
fn emit_await(a: &AwaitStmt, context: &str) -> Result<String, EmitError> {
    let Some(cond) = &a.condition else {
        return Err(unsupported("`until`/`await` with no condition", context));
    };
    Ok(format!("~ until {}", emit_expr(cond, context)?))
}

/// `~{ … }` — the content-ground `Stmt::LogicBlock` printer (issue #1972):
/// a multi-statement escape into code, using the same `~{ }` sigil the
/// whole-body override (`flow name() ~{ … }`, issue #1309) and the
/// code-ground `> text` split (issues #1992/#2028) both use.
///
/// Only spells a `Standalone`-scoped block — the scope this content-ground
/// escape always produces (`lower_native::body::lower_logic_line`'s doc,
/// never split). An `Opens`/`Continues` block only ever comes from a
/// *whole* code-ground body split around a nested `> text` line
/// (`lower_stmt_block_as_body`, issue #1992/#2028); re-spelling that shape
/// correctly needs `emit_knot`-level restructuring back to one shared
/// code-ground body rather than a nested `~{ }` per run, out of this
/// slice's scope — refused, not guessed (see this module's doc).
fn emit_logic_block(
    out: &mut String,
    indent: &str,
    depth: usize,
    lb: &LogicBlock,
    context: &str,
) -> Result<(), EmitError> {
    if lb.scope != LogicBlockScope::Standalone {
        return Err(unsupported(
            "a code-ground body split by a nested `> text` line",
            context,
        ));
    }
    let _ = writeln!(out, "{indent}~{{");
    emit_block_stmt_stream(out, &lb.stmts, depth + 1, context)?;
    let _ = writeln!(out, "{indent}}}");
    Ok(())
}

/// A `~{ … }`/whole-code-ground-body statement stream (issue #1972): the
/// code-ground counterpart of [`emit_stmt_stream`] — `;`-terminated per
/// statement, no content lines, choices, or diverts (`BlockStmt`'s closed
/// T1b set, `docs/t1b-surface-spec.md` §2's seam rule). Only the **leaf**
/// shapes are spelled (`TempDecl`/`Assignment`/`ExprStmt`/`Return`/`Break`/
/// `Continue`/`Await`) — nested `If`/`While`/`For` control flow refuses:
/// printing those faithfully needs the full code-ground control-flow
/// printer (`if cond { … } else { … }`/`while cond { … }`/`for x in expr {
/// … }`), a separate, larger lift this slice does not take on (see this
/// module's doc "Explicitly unsupported" section).
fn emit_block_stmt_stream(
    out: &mut String,
    stmts: &[BlockStmt],
    depth: usize,
    context: &str,
) -> Result<(), EmitError> {
    let indent = "  ".repeat(depth);
    for stmt in stmts {
        let line = match stmt {
            BlockStmt::TempDecl(t) => {
                let ty = emit_annotation_suffix(t.annotation.as_ref());
                match &t.value {
                    Some(v) => format!("let {}{ty} = {}", t.name.text, emit_expr(v, context)?),
                    None => format!("let {}{ty}", t.name.text),
                }
            }
            BlockStmt::Assignment(a) => {
                let target = emit_expr(&a.target, context)?;
                let op = match a.op {
                    AssignOp::Set => "=",
                    AssignOp::Add => "+=",
                    AssignOp::Sub => "-=",
                };
                format!("{target} {op} {}", emit_expr(&a.value, context)?)
            }
            BlockStmt::ExprStmt(e) => emit_expr(e, context)?,
            BlockStmt::Return(r) => {
                // Unlike `Stmt::Return` at weave/content-ground position
                // (this file's `emit_stmt_stream`, which reuses the same
                // `emit_return`), a code-ground `return` has no `-> target`
                // respelling: `parser/stmt.rs::return_stmt`'s doc states it
                // "has no tunnel-redirect (`return -> x`) counterpart —
                // that respelling is a content-ground/tunnel concept with
                // no code-ground meaning", and `parser/expr.rs::atom` has no
                // divert-target atom for `return`'s general-expression arm
                // to reach. `lower_block_return`/`lower_return_stmt` lower
                // whatever `ReturnStmt::value()` yields, and the ink dialect
                // does have divert-target-as-value, so refuse rather than
                // spell an output that would fail to reparse (review
                // finding, w111) — the same leaf-only refuse-don't-guess
                // posture the rest of this function already takes.
                if matches!(r.value, Some(Expr::DivertTarget(_))) {
                    return Err(unsupported(
                        "code-ground `return -> target` (no tunnel-redirect counterpart at code-ground position)",
                        context,
                    ));
                }
                emit_return(r, context)?
            }
            BlockStmt::Break(_) => "break".to_string(),
            BlockStmt::Continue(_) => "continue".to_string(),
            BlockStmt::Await(a) => {
                let Some(cond) = &a.condition else {
                    return Err(unsupported("`until` with no condition", context));
                };
                format!("until {}", emit_expr(cond, context)?)
            }
            BlockStmt::If(_) | BlockStmt::While(_) | BlockStmt::For(_) => {
                return Err(unsupported(
                    "nested control flow inside a `~{ }` logic block",
                    context,
                ));
            }
        };
        let _ = writeln!(out, "{indent}{line};");
    }
    Ok(())
}

/// `return` / `return -> target` / `return <expr>` (issue #1973's last
/// case — `lower_native::body`'s `N::RETURN_STMT` arm now lowers a
/// content-ground value expression, mirroring the code-ground `return
/// expr?;` form it always supported; see that arm's doc for the E032 note
/// on why this stays a pure grammar/emitter fix, not a semantics change).
/// `Expr::DivertTarget` keeps its own dedicated `return -> target` spelling
/// (the tunnel-return redirect, checked first) — every other `Expr` shape
/// falls to the general `return <expr>` case, which simply reuses
/// [`emit_expr`] and propagates whatever it can't spell.
fn emit_return(r: &Return, context: &str) -> Result<String, EmitError> {
    if !r.onwards_args.is_empty() {
        return Err(unsupported("tunnel-return onwards args", context));
    }
    match &r.value {
        None => Ok("return".to_string()),
        Some(Expr::DivertTarget(p)) => Ok(format!("return -> {}", emit_path(p))),
        Some(v) => Ok(format!("return {}", emit_expr(v, context)?)),
    }
}

fn emit_divert_target(t: &DivertTarget, context: &str) -> Result<String, EmitError> {
    let head = match &t.path {
        DivertPath::Path(p) => emit_path(p),
        DivertPath::Done => "DONE".to_string(),
        DivertPath::End => "END".to_string(),
    };
    if t.args.is_empty() {
        Ok(head)
    } else {
        let rendered: Result<Vec<String>, EmitError> =
            t.args.iter().map(|a| emit_expr(a, context)).collect();
        Ok(format!("{head}({})", rendered?.join(", ")))
    }
}

fn emit_path(p: &Path) -> String {
    p.segments
        .iter()
        .map(|s| s.text.as_str())
        .collect::<Vec<_>>()
        .join(".")
}

fn emit_content_line(
    out: &mut String,
    indent: &str,
    c: &Content,
    context: &str,
) -> Result<(), EmitError> {
    let text = escape_leading_line_start_sigil(&emit_content_parts(&c.parts, context)?);
    let mut line = format!("{indent}{text}");
    for tag in &c.tags {
        let _ = write!(line, " #{}", emit_tag(tag, context)?);
    }
    let _ = writeln!(out, "{line}");
    Ok(())
}

fn emit_tag(tag: &Tag, context: &str) -> Result<String, EmitError> {
    emit_content_parts(&tag.parts, context)
}

fn emit_content_parts(parts: &[ContentPart], context: &str) -> Result<String, EmitError> {
    let mut s = String::new();
    for part in parts {
        match part {
            ContentPart::Text(t) => s.push_str(&escape_content_text(t)),
            ContentPart::Glue => s.push_str("<>"),
            ContentPart::Interpolation(e) => {
                let _ = write!(s, "{{{}}}", emit_expr(e, context)?);
            }
            ContentPart::Spring => return Err(unsupported("word-break spring", context)),
            ContentPart::InlineConditional(_) => {
                return Err(unsupported("inline conditional in content", context));
            }
            ContentPart::InlineSequence(_) => {
                return Err(unsupported("inline sequence in content", context));
            }
            ContentPart::Span(span) => s.push_str(&emit_span(span, context)?),
        }
    }
    Ok(s)
}

/// `<name attr="v">…</name>`, or the self-closing `<name attr="v"/>` shape
/// when `children` is empty (the point-marker case, §8b.11) — the exact
/// inverse of `hir::lower_native::body::lower_span`.
fn emit_span(span: &crate::hir::SpanPart, context: &str) -> Result<String, EmitError> {
    let mut s = format!("<{}", span.name);
    for attr in &span.attrs {
        let _ = write!(s, " {}=\"{}\"", attr.name, escape_attr_value(&attr.value));
    }
    if span.children.is_empty() {
        s.push_str("/>");
        return Ok(s);
    }
    s.push('>');
    s.push_str(&emit_content_parts(&span.children, context)?);
    let _ = write!(s, "</{}>", span.name);
    Ok(s)
}

/// Escape a content-line literal for round-trip through native `.brink`
/// source. `\` `<` `{` `#` are exactly the escape set §8d.6 rules final
/// (`parser::markup::escape`'s `is_escapable`) — each would otherwise
/// reopen a real construct on re-parse (`<b>` a span, `{name}` an
/// interpolation, `#tag` a tag, a bare `\` an invalid escape sequence).
/// This is the emit-side inverse: a HIR `Text` containing e.g. `<b>` used
/// to round-trip as literal text before the markup grammar existed (#1716)
/// — now it must be escaped, or it silently re-parses as a `SPAN`.
fn escape_content_text(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    for c in s.chars() {
        if matches!(c, '\\' | '<' | '{' | '#') {
            out.push('\\');
        }
        out.push(c);
    }
    out
}

/// Guard a real body-line-position `Content` emission against a hazard
/// the four-char inline escape set never had to consider: `\@` (§8d.6's
/// line-start escape, issue #1744) now legitimately produces a `Text`
/// part whose literal content can begin with `@` immediately followed by
/// an identifier — exactly the shape `parser::element::at_cue` triggers a
/// `CUE` on when it is the very first thing on a physical body line.
/// Emitted verbatim, that would silently re-parse as a cue instead of the
/// literal text it started as. Only the one leading character needs
/// escaping (mid-line `@` needs none — `SyntaxKind::AT`'s own doc: "reached
/// mid-line — folds into plain TEXT"), and only when the adjacent char
/// actually qualifies as an identifier start (`[A-Za-z_]`,
/// `lexer::ident::is_ident_start_byte`) — `@ 5pm` or a bare trailing `@`
/// never opened a cue in the first place and needs no escaping either.
///
/// `!` now carries the exact same hazard (issue #2004): a leading
/// `!` immediately followed by an identifier is `parser::element::
/// at_bang_dispatch`'s own adjacency test for a `BANG_DISPATCH` (`!name`
/// dispatch, §3.5b) at body-line position, so a literal leading `!name` —
/// authored directly or produced by `\!` — needs the same one-character
/// re-escape or it would silently re-parse as a dispatch instead of the
/// literal text it started as. `! Wait, listen.` (a gap after the `!`)
/// never opened a dispatch and needs no escaping, matching `at_bang_
/// dispatch`'s own adjacency requirement.
///
/// Only called at genuine body-line-position emission
/// (`emit_stmt_stream`'s `Stmt::Content` arm, `emit_content_line`) — a
/// labeled line (`emit_labeled_stmt_stream`) already carries its own
/// `(name) ` prefix ahead of the content, so on re-parse the line's first
/// token is never `AT`/`BANG` there regardless of what the content says,
/// and needs no equivalent guard.
fn escape_leading_line_start_sigil(text: &str) -> String {
    let mut chars = text.chars();
    match chars.next() {
        Some(sigil @ ('@' | '!')) => {
            let rest = chars.as_str();
            if rest.starts_with(|c: char| c.is_ascii_alphabetic() || c == '_') {
                format!("\\{sigil}{rest}")
            } else {
                text.to_string()
            }
        }
        _ => text.to_string(),
    }
}

/// Escape a span attribute value for round-trip. `SPAN_ATTR_VALUE` shares
/// the lexer's string-mode token pair (`STRING_TEXT`/`STRING_ESCAPE`) with
/// ordinary string literals, whose only recognized escapes are `\n` `\t`
/// `\\` `\"` (`lexer::lex_string_token`) — and a raw, unescaped newline or
/// carriage return inside a quoted string terminates it early (the
/// lexer's unterminated-string recovery emits `NEWLINE` and closes the
/// string), so this escapes those too, not just the structurally-required
/// `"`/`\`.
fn escape_attr_value(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    for c in s.chars() {
        match c {
            '\\' => out.push_str("\\\\"),
            '"' => out.push_str("\\\""),
            '\n' => out.push_str("\\n"),
            '\t' => out.push_str("\\t"),
            _ => out.push(c),
        }
    }
    out
}

// ─── Choices ─────────────────────────────────────────────────────────

/// Emit a `ChoiceSet` plus whatever follows it in the source stream (the
/// dissolved-gather continuation, flattened back in place — see
/// `emit_stmt_stream`'s own doc). Shared by the two `emit_stmt_stream`
/// arms that can reach a `ChoiceSet`: the plain case (no leading splices)
/// and the thread-start-splice re-nesting case (one or more leading
/// splices pulled out of the surrounding stream, issue #1974).
fn emit_choice_set_and_continuation(
    out: &mut String,
    indent: &str,
    depth: usize,
    cs: &ChoiceSet,
    leading: &[&ThreadStart],
    context: &str,
) -> Result<(), EmitError> {
    emit_choice_set(out, indent, depth, cs, leading, context)?;
    // The continuation's statements are the rest of *this* stream,
    // flattened in place — see `emit_stmt_stream`'s doc. A labeled
    // continuation (a gather `(name)` immediately after the `{?}`, per
    // `lower_native::body::lower_continuation`) spells with G-1's
    // `(name)` content-line-label prefix on the continuation's own first
    // line — see `emit_labeled_stmt_stream`.
    match &cs.continuation.label {
        Some(label) => {
            emit_labeled_stmt_stream(out, label, &cs.continuation.stmts, depth, context)?;
        }
        None => {
            emit_stmt_stream(out, &cs.continuation.stmts, depth, context)?;
        }
    }
    Ok(())
}

fn emit_choice_set(
    out: &mut String,
    indent: &str,
    depth: usize,
    cs: &ChoiceSet,
    leading: &[&ThreadStart],
    context: &str,
) -> Result<(), EmitError> {
    let _ = writeln!(out, "{indent}{{?");
    // Leading splices (a `<- flow(args)` reached before any choice line)
    // print as sibling lines at the same indent as the choices
    // themselves, ahead of the first one — see the `emit_stmt_stream`
    // `Stmt::ThreadStart` arm that collects `leading`.
    let child_indent = "  ".repeat(depth + 1);
    for t in leading {
        let target = emit_divert_target(&t.target, context)?;
        let _ = writeln!(out, "{child_indent}<- {target}");
    }
    for choice in &cs.choices {
        emit_choice(out, depth + 1, choice, context)?;
    }
    let _ = writeln!(out, "{indent}}}");
    Ok(())
}

/// Split a choice body's `stmts` into (own content, trailing splices).
///
/// A splice reached *after* a choice line, still before the next
/// choice/`else`, is appended onto that choice's own `body.stmts` as a
/// trailing run (`lower_native::choice`'s doc: "interspersed content
/// 'belongs to the previous choice'"; ink's own weave-fold mirrors this
/// identically, `lower/block/weave.rs`'s `addContentToPreviousWeavePoint`
/// citation) — always at the *end*, since nothing else is a legal sibling
/// of a choice line once a splice starts (native's `choice_point` loop
/// only recognizes another `CHOICE`/`SPLICE`/`ELSE_BRANCH` next, never a
/// bare content line). This is the maximal trailing run, so a
/// `ThreadStart` anywhere *before* it (interleaved with other content) is
/// left in the returned "own content" half and refused by the ordinary
/// `emit_stmt_stream` walk — a shape with no native spelling, not one
/// this splits away.
fn split_trailing_thread_starts(stmts: &[Stmt]) -> (&[Stmt], &[Stmt]) {
    let mut split = stmts.len();
    while split > 0 && matches!(stmts[split - 1], Stmt::ThreadStart(_)) {
        split -= 1;
    }
    stmts.split_at(split)
}

fn emit_trailing_thread_starts(
    out: &mut String,
    indent: &str,
    trailing: &[Stmt],
    context: &str,
) -> Result<(), EmitError> {
    for stmt in trailing {
        let Stmt::ThreadStart(t) = stmt else {
            unreachable!("split_trailing_thread_starts guarantees only ThreadStart here")
        };
        let target = emit_divert_target(&t.target, context)?;
        let _ = writeln!(out, "{indent}<- {target}");
    }
    Ok(())
}

fn emit_choice(out: &mut String, depth: usize, c: &Choice, context: &str) -> Result<(), EmitError> {
    let indent = "  ".repeat(depth);
    if !c.tags.is_empty() {
        return Err(unsupported("choice-line trailing tags", context));
    }

    // Native has no grammar for a splice *inside* a choice's own nested
    // `{}` body (that block parses through the generic
    // `braced_item_list`, which never recognizes `THREAD` — only
    // `choice_point`'s own loop does), so a trailing splice must print as
    // a sibling line *after* this choice, at the same depth as its
    // bullet/`else`, never nested inside its braces (issue #1974).
    let (own_stmts, trailing_threads) = split_trailing_thread_starts(c.body.stmts.as_slice());

    if c.is_fallback {
        let _ = write!(out, "{indent}else ");
        emit_choice_body(out, depth, c.body.label.as_ref(), own_stmts, context)?;
        emit_trailing_thread_starts(out, &indent, trailing_threads, context)?;
        return Ok(());
    }

    let marker = if c.is_sticky { "+" } else { "*" };
    let mut head = format!("{indent}{marker}");
    if let Some(cond) = &c.condition {
        // `c.binding` mirrors `IfStmt`/`ConditionalBlock`'s own
        // `binding_suffix` handling above — a guard-`as` binding respells
        // as a trailing `as NAME` inside the same `{if …}` braces
        // (`docs/decision-log.md` 2026-07-26 "Choice-guard `as`
        // un-deferred"; grammar: `parser::choice::choice_guard`).
        let binding_suffix = match &c.binding {
            Some(name) => format!(" as {}", name.text),
            None => String::new(),
        };
        let _ = write!(
            head,
            " {{if {}{binding_suffix}}}",
            emit_expr(cond, context)?
        );
    }
    if let Some(label) = &c.label {
        let _ = write!(head, " ({})", label.text);
    }
    if let Some(start) = &c.start_content {
        let text = emit_content_parts(&start.parts, context)?;
        if !text.is_empty() {
            let _ = write!(head, " {text}");
        }
    }
    let needs_brackets = c.bracket_content.is_some() || c.inner_content.is_some();
    if needs_brackets {
        let bracket = match &c.bracket_content {
            Some(b) => emit_content_parts(&b.parts, context)?,
            None => String::new(),
        };
        let _ = write!(head, "[{bracket}]");
        if let Some(inner) = &c.inner_content {
            let text = emit_content_parts(&inner.parts, context)?;
            head.push_str(&text);
        }
    }
    out.push_str(&head);

    // A labeled choice body has no faithful native spelling — same guard as
    // `emit_choice_body`'s fallback (`else {}`) path, applied here for
    // consistency on the non-fallback path.
    if c.body.label.is_some() {
        return Err(unsupported("labeled choice body", context));
    }

    // The choice body's own stmts always start with a structural
    // `Stmt::EndOfLine` marker (`lower_native::choice`'s own doc: "the
    // list-display/echoed-text boundary marker"), optionally preceded by a
    // `Divert`/`TunnelCall` pulled out of the bracket/inner text regions
    // (N-1: a divert immediately following `]` with no further text). See
    // this function's three-way dispatch below. `own_stmts` already has
    // any trailing splice(s) stripped off (see this function's top).
    let stmts = own_stmts;
    match stmts {
        [] => {
            return Err(unsupported(
                "malformed choice body (no EndOfLine marker)",
                context,
            ));
        }
        [Stmt::EndOfLine] => {
            out.push('\n');
        }
        [Stmt::Divert(d), Stmt::EndOfLine] => {
            let target = emit_divert_target(&d.target, context)?;
            let _ = writeln!(out, " -> {target}");
        }
        [Stmt::TunnelCall(t), Stmt::EndOfLine] if t.targets.len() == 1 => {
            let target = emit_divert_target(&t.targets[0], context)?;
            let _ = writeln!(out, " -> {target} ->");
        }
        [Stmt::EndOfLine, rest @ ..] => {
            out.push(' ');
            emit_choice_body_stmts(out, depth, rest, context)?;
        }
        // The same-line `Divert`/`TunnelCall` shape above (N-1) only covers
        // a choice body that is *exactly* that one statement — a gather-
        // style fold can still absorb further statements onto this same
        // choice after it (e.g. a trailing un-indented `->DONE` line with
        // no gather of its own, folded onto the last choice in a weave
        // that has no separate continuation to hold it — see
        // `tests/tier1/choices/varying-choice`), which the two-element
        // patterns above don't match. Braces are always a faithful
        // respelling of a choice body's full statement stream, divert
        // included, so fall back to the general block form rather than
        // refusing a shape the compact one-liner just can't reach.
        [Stmt::Divert(_) | Stmt::TunnelCall(_), Stmt::EndOfLine, ..] => {
            out.push(' ');
            emit_choice_body_stmts(out, depth, stmts, context)?;
        }
        _ => {
            return Err(unsupported(
                "malformed choice body (no leading EndOfLine)",
                context,
            ));
        }
    }
    emit_trailing_thread_starts(out, &indent, trailing_threads, context)
}

fn emit_choice_body(
    out: &mut String,
    depth: usize,
    label: Option<&Name>,
    stmts: &[Stmt],
    context: &str,
) -> Result<(), EmitError> {
    if label.is_some() {
        return Err(unsupported("labeled choice/else body", context));
    }
    match stmts {
        [] => Err(unsupported(
            "malformed else body (no EndOfLine marker)",
            context,
        )),
        [Stmt::EndOfLine] => {
            out.push('\n');
            Ok(())
        }
        [Stmt::EndOfLine, rest @ ..] => emit_choice_body_stmts(out, depth, rest, context),
        // Unlike a regular choice line, `else` has no inline-content region
        // of its own to embed a same-line divert in — the native grammar's
        // `choice_point` loop only recognizes `else` when immediately
        // followed by `{` (`parser::choice::choice_point`), so an `else`
        // whose body is a bare `-> target` (ink's classic "no visible
        // option" fallback choice, lowered with `is_fallback: true` and no
        // display text — see `tests/tier1/choices/I079-…`) must always
        // spell with braces, never the regular path's compact one-liner.
        [Stmt::Divert(_) | Stmt::TunnelCall(_), ..] => {
            emit_choice_body_stmts(out, depth, stmts, context)
        }
        _ => Err(unsupported(
            "malformed else body (no leading EndOfLine)",
            context,
        )),
    }
}

fn emit_choice_body_stmts(
    out: &mut String,
    depth: usize,
    rest: &[Stmt],
    context: &str,
) -> Result<(), EmitError> {
    let indent = "  ".repeat(depth);
    let _ = writeln!(out, "{{");
    emit_stmt_stream(out, rest, depth + 1, context)?;
    let _ = writeln!(out, "{indent}}}");
    Ok(())
}

// ─── Conditionals ────────────────────────────────────────────────────

fn emit_conditional(
    out: &mut String,
    indent: &str,
    depth: usize,
    cond: &Conditional,
    context: &str,
) -> Result<(), EmitError> {
    match &cond.kind {
        CondKind::InitialCondition => {
            if cond.branches.is_empty() || cond.branches.len() > 2 {
                return Err(unsupported("multi-branch conditional shape", context));
            }
            let first = &cond.branches[0];
            let Some(if_cond) = &first.condition else {
                return Err(unsupported(
                    "conditional with no leading condition",
                    context,
                ));
            };
            // B1b (issue #1475): a template `{if}` can carry an `as`
            // binding — `{if EXPR as n: … else: …}` — and `AS_BINDING` is
            // proven valid *inside* the braced `{if EXPR as n { … }}` form
            // too (`brink-syntax-native::parser::tests::brace_family::
            // conditional_block_carries_an_as_binding_on_the_braced_form`),
            // so respelling it as a suffix on the condition head is a
            // faithful round-trip, not a guess at new grammar. Omitting it
            // would silently change what the respelled source means: the
            // arm's body could reference a binding that no longer exists.
            let binding_suffix = match &first.binding {
                Some(name) => format!(" as {}", name.text),
                None => String::new(),
            };
            let _ = writeln!(
                out,
                "{indent}{{if {}{binding_suffix} {{",
                emit_expr(if_cond, context)?
            );
            emit_block_stmts(out, &first.body, depth + 1, context)?;
            if let Some(second) = cond.branches.get(1) {
                if second.condition.is_some() {
                    return Err(unsupported("`else if` chain", context));
                }
                let _ = writeln!(out, "{indent}}} else {{");
                emit_block_stmts(out, &second.body, depth + 1, context)?;
            }
            let _ = writeln!(out, "{indent}}}}}");
            Ok(())
        }
        CondKind::Switch(subject) => {
            let _ = writeln!(out, "{indent}{{match {} {{", emit_expr(subject, context)?);
            for branch in &cond.branches {
                emit_match_arm(out, depth + 1, branch, context)?;
            }
            let _ = writeln!(out, "{indent}}}}}");
            Ok(())
        }
        CondKind::IfElse => emit_if_else_chain(out, indent, depth, &cond.branches, context),
    }
}

/// `CondKind::IfElse` — ink's own independently-chained, no-shared-subject
/// 3+-branch conditional (`{ - cond1: … - cond2: … - else: … }`, no shared
/// switch expression) — has no flat native counterpart to walk into
/// (`lower_native::cond::lower_conditional`'s doc: a native-authored chain
/// always lowers through *nesting* instead, one `InitialCondition` per
/// `else if` link). This function re-shapes the flat `IfElse` branch list
/// back into that same nesting on the way out: the first branch becomes the
/// `{if …}` head, and if more branches remain, they recurse into this same
/// function as the *body* of an `else { … }` arm — reproducing exactly the
/// `{if …} else { {if …} else { … } }` shape a native `else if` chain would
/// have lowered to in the first place (issue #1975).
fn emit_if_else_chain(
    out: &mut String,
    indent: &str,
    depth: usize,
    branches: &[CondBranch],
    context: &str,
) -> Result<(), EmitError> {
    let Some((first, rest)) = branches.split_first() else {
        return Err(unsupported("empty `else if` chain", context));
    };
    let Some(if_cond) = &first.condition else {
        return Err(unsupported(
            "`else if` chain branch with no leading condition",
            context,
        ));
    };
    // Same B1b `as`-binding respelling `CondKind::InitialCondition` does
    // above — ink's own lowering never sets `CondBranch::binding` (it's
    // native-only, `lower/conditional/multiline.rs::lower_if_else_branches`
    // always passes `None`), but a future native-authored `IfElse` shape (or
    // hand-built HIR) could carry one, and dropping it silently would change
    // what the respelled source means.
    let binding_suffix = match &first.binding {
        Some(name) => format!(" as {}", name.text),
        None => String::new(),
    };
    let _ = writeln!(
        out,
        "{indent}{{if {}{binding_suffix} {{",
        emit_expr(if_cond, context)?
    );
    emit_block_stmts(out, &first.body, depth + 1, context)?;
    match rest {
        [] => {}
        // Exactly one branch left and it's a plain `else` (no condition of
        // its own): the terminal link in the chain, spelled flat rather than
        // as a pointless one-branch nested `{if}`.
        [only] if only.condition.is_none() => {
            let _ = writeln!(out, "{indent}}} else {{");
            emit_block_stmts(out, &only.body, depth + 1, context)?;
        }
        // Two or more branches remain, or the sole remaining branch still
        // carries its own condition (an `IfElse` chain with no trailing
        // `else` at all): nest the rest one level deeper inside this arm's
        // `else { … }`, recursing back into this same function — this is
        // the flattened-to-nested reshape the issue asks for.
        _ => {
            let inner_indent = "  ".repeat(depth + 1);
            let _ = writeln!(out, "{indent}}} else {{");
            emit_if_else_chain(out, &inner_indent, depth + 1, rest, context)?;
        }
    }
    let _ = writeln!(out, "{indent}}}}}");
    Ok(())
}

fn emit_match_arm(
    out: &mut String,
    depth: usize,
    branch: &CondBranch,
    context: &str,
) -> Result<(), EmitError> {
    let Some(pattern) = &branch.condition else {
        return Err(unsupported(
            "match arm with no pattern (default arm)",
            context,
        ));
    };
    let indent = "  ".repeat(depth);
    let _ = writeln!(out, "{indent}{} => {{", emit_expr(pattern, context)?);
    emit_block_stmts(out, &branch.body, depth + 1, context)?;
    let _ = writeln!(out, "{indent}}}");
    Ok(())
}

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

fn emit_expr(e: &Expr, context: &str) -> Result<String, EmitError> {
    match e {
        Expr::Int(n) => Ok(n.to_string()),
        Expr::Float(f) => Ok(f.to_f64().to_string()),
        Expr::Bool(b) => Ok(b.to_string()),
        Expr::Path(p) => Ok(emit_path(p)),
        Expr::String(s) => {
            let mut out = String::from("\"");
            for part in &s.parts {
                match part {
                    StringPart::Literal(t) => out.push_str(t),
                    StringPart::Interpolation(inner) => {
                        let _ = write!(out, "{{{}}}", emit_expr(inner, context)?);
                    }
                }
            }
            out.push('"');
            Ok(out)
        }
        Expr::Prefix(op, inner) => {
            let op_str = match op {
                PrefixOp::Negate => "-",
                PrefixOp::Not => "not ",
            };
            Ok(format!("{op_str}{}", emit_expr(inner, context)?))
        }
        Expr::Infix(ie) => {
            let op_str = infix_op_str(ie.op);
            Ok(format!(
                "{} {op_str} {}",
                emit_expr(&ie.lhs, context)?,
                emit_expr(&ie.rhs, context)?
            ))
        }
        Expr::Postfix(inner, op) => {
            let op_str = match op {
                PostfixOp::Increment => "++",
                PostfixOp::Decrement => "--",
            };
            Ok(format!("{}{op_str}", emit_expr(inner, context)?))
        }
        Expr::Call(path, args) => {
            let rendered: Result<Vec<String>, EmitError> =
                args.iter().map(|a| emit_expr(a, context)).collect();
            Ok(format!("{}({})", emit_path(path), rendered?.join(", ")))
        }
        Expr::Null => Err(unsupported("`null` literal", context)),
        Expr::DivertTarget(_) => Err(unsupported("divert-target-as-value expression", context)),
        Expr::ListLiteral(_) => Err(unsupported("list literal expression", context)),
        Expr::ArrayLiteral(_) => Err(unsupported("array sigil literal", context)),
        Expr::MapLiteral(_) => Err(unsupported("map sigil literal", context)),
        Expr::Index(_) => Err(unsupported("index expression", context)),
        Expr::Range(_) => Err(unsupported("range literal", context)),
        Expr::StructLiteral(_) => Err(unsupported("struct construction literal", context)),
        Expr::FieldAccess(_) => Err(unsupported("field access expression", context)),
        // `#fn(target)` — a **zero-bound** ink fn literal respells as the
        // bare target name, the native surface's fn-value spelling (RULED
        // 2026-08-01, `docs/t1c-spec.md` §2a, issue #1862): `#fn(scene)`
        // becomes `scene`, and a native call stays `scene()`, so the
        // respelling is unambiguous.
        //
        // `#fn(f, a)` — the *binding* (partial-application) form — is a
        // different story and still refuses: the ruling deliberately gave
        // it no native spelling, because for a `ref` param it binds a
        // durable **cell** and a lambda (`|x| f(a, x)`) captures by value
        // only. Emitting a lambda here would silently change that
        // semantics, so this errors rather than inventing one.
        Expr::FnLiteral(fl) if fl.args.is_empty() => Ok(emit_path(&fl.target)),
        Expr::FnLiteral(_) => Err(unsupported("`#fn` literal with bound arguments", context)),
        Expr::RefArg(_) => Err(unsupported("`ref` argument expression", context)),
        // Lambdas (issue #1685): the expression-body form respells exactly
        // — pipes, optional param annotations, the ruled `:` return clause.
        // A *braced* body carries code-ground statements, which this
        // emitter has no printer for at all (`LogicBlock` is unsupported
        // for the same reason), so it refuses loudly rather than emitting a
        // body it cannot round-trip.
        Expr::Lambda(l) => {
            let params = emit_params(&l.params, context)?;
            let ret = emit_annotation_suffix(l.return_type.as_ref());
            match &l.body {
                LambdaBody::Expr(body) => {
                    Ok(format!("|{params}|{ret} {}", emit_expr(body, context)?))
                }
                LambdaBody::Block { .. } => Err(unsupported("lambda with a braced body", context)),
            }
        }
        // Internal-only (issue #1839, `docs/decision-log.md` 2026-08-01
        // "Content-as-value") — never constructed from surface syntax, so
        // there is nothing to round-trip. Mirrors this module's existing
        // refusal for every other claiming-file construct (module doc:
        // "`emit_native` cannot round-trip a claiming file").
        Expr::Fragment(_) => Err(unsupported("internal fragment-capture expression", context)),
    }
}

fn infix_op_str(op: InfixOp) -> &'static str {
    match op {
        InfixOp::Add => "+",
        InfixOp::Sub => "-",
        InfixOp::Mul => "*",
        InfixOp::Div => "/",
        InfixOp::Mod => "%",
        InfixOp::Intersect => "^",
        InfixOp::Eq => "==",
        InfixOp::NotEq => "!=",
        InfixOp::Lt => "<",
        InfixOp::Gt => ">",
        InfixOp::LtEq => "<=",
        InfixOp::GtEq => ">=",
        InfixOp::And => "&&",
        InfixOp::Or => "||",
        InfixOp::Has => "?",
        InfixOp::HasNot => "!?",
        InfixOp::Coalesce => "or",
    }
}

#[cfg(test)]
#[expect(
    clippy::panic,
    reason = "test-only `let-else { panic!(...) }` assertions for concise failure messages"
)]
mod tests {
    use super::*;

    /// Parse + lower native `src`, then run it through [`emit_file`].
    fn lower_and_emit(src: &str) -> Result<String, EmitError> {
        let parse = brink_syntax_native::parse(src);
        let tree = parse.tree();
        let (hir, _manifest, diags) = crate::hir::lower_native::lower(crate::FileId(0), &tree);
        assert!(diags.is_empty(), "unexpected diagnostics: {diags:?}");
        emit_file(&hir)
    }

    /// Reparse + relower `src` and return the resulting `HirFile` (asserting
    /// a clean parse and lowering, the same discipline `lower_and_emit`
    /// uses). Used to check a round-trip lands on an equivalent HIR shape,
    /// not just "parses without error".
    fn reparse_and_lower(src: &str) -> crate::HirFile {
        let parse = brink_syntax_native::parse(src);
        assert!(
            parse.errors().is_empty(),
            "emitted source has parse errors: {:?}\n--- source ---\n{src}",
            parse.errors()
        );
        let tree = parse.tree();
        let (hir, _manifest, diags) = crate::hir::lower_native::lower(crate::FileId(0), &tree);
        assert!(
            diags.is_empty(),
            "emitted source has lowering diagnostics: {diags:?}\n--- source ---\n{src}"
        );
        hir
    }

    /// A `{?}` choice point followed by a labeled gather `(again)` attaches
    /// the label directly to `ChoiceSet::continuation.label`
    /// (`lower_native::body::lower_continuation`'s dissolved-gather
    /// convention — see `labeled_gather_after_choices_attaches_label_to_continuation`
    /// in `lower_native::tests`). G-1's `(name)` content-line-label spelling
    /// (RULED 2026-07-20) gives this a faithful native respelling: the
    /// emitter must round-trip it, not refuse it.
    #[test]
    fn labeled_gather_continuation_round_trips() {
        let src = "flow a() {\n  {?\n    * A.\n  }\n  (again)\n  Loop point.\n}\n";
        let emitted = lower_and_emit(src).expect("labeled gather continuation must now emit");
        assert!(
            emitted.contains("(again)"),
            "emitted source dropped the continuation label:\n{emitted}"
        );

        let hir = reparse_and_lower(&emitted);
        let body = &hir.knots[0].body;
        let Stmt::ChoiceSet(cs) = &body.stmts[0] else {
            panic!("expected ChoiceSet as the re-lowered body's first statement: {body:?}");
        };
        assert_eq!(
            cs.continuation.label.as_ref().map(|n| n.text.as_str()),
            Some("again"),
            "re-lowered continuation lost its label"
        );
    }

    /// A standalone labeled content line mid-flow (`(mid) Middle.`) lowers
    /// to `Stmt::LabeledBlock` (`lower_native::tests::
    /// standalone_labeled_content_line_becomes_labeled_block`). Same G-1
    /// spelling, same round-trip obligation.
    #[test]
    fn labeled_mid_flow_block_round_trips() {
        let src = "flow a() {\n  Intro.\n  (mid) Middle.\n  End.\n}\n";
        let emitted = lower_and_emit(src).expect("labeled mid-flow block must now emit");
        assert!(
            emitted.contains("(mid)"),
            "emitted source dropped the mid-flow label:\n{emitted}"
        );

        let hir = reparse_and_lower(&emitted);
        let body = &hir.knots[0].body;
        let labeled = body
            .stmts
            .iter()
            .find_map(|s| match s {
                Stmt::LabeledBlock(b) => Some(b),
                _ => None,
            })
            .expect("expected a LabeledBlock among the re-lowered body's statements");
        assert_eq!(labeled.label.as_ref().map(|n| n.text.as_str()), Some("mid"));
    }

    /// A gather label with no content at all on its own line, and nothing
    /// following it in its enclosing flow, has no `Stmt` to attach to
    /// (`flush_content`'s empty-flush short-circuit): the emitter must
    /// print a bare `(again)` line, with no dangling trailing space and no
    /// spurious refusal.
    #[test]
    fn bare_label_line_at_end_of_flow_has_no_trailing_space() {
        let src = "flow a() {\n  {?\n    * A.\n  }\n  (again)\n}\n";
        let emitted = lower_and_emit(src).expect("bare trailing label must now emit");
        assert!(
            emitted.lines().any(|l| l.trim() == "(again)"),
            "expected a bare `(again)` line with no trailing text:\n{emitted}"
        );
        reparse_and_lower(&emitted);
    }

    /// When the labeled line's own content is empty but real content
    /// follows on later lines (`(again)` alone, then `Loop point.` on the
    /// next line), `flush_content`'s empty-flush short-circuit means the
    /// label attaches directly onto that *following* content statement —
    /// there is no separate empty `Content`/`EndOfLine` pair to hold it.
    /// This collapses the label and the following text onto one output
    /// line, which is a faithful respelling (the original label line
    /// contributed no text of its own either way).
    #[test]
    fn label_with_empty_own_line_attaches_to_following_content() {
        let src = "flow a() {\n  {?\n    * A.\n  }\n  (again)\n  Loop point.\n}\n";
        let emitted = lower_and_emit(src).expect("labeled gather continuation must now emit");
        assert!(
            emitted.lines().any(|l| l.trim() == "(again) Loop point."),
            "expected the label to attach to the following content line:\n{emitted}"
        );
    }

    /// A `fn(params…): ret` type annotation (e.g. `f: fn(int): bool`) is
    /// legal native source per
    /// `fn_type_annotation_parses_with_its_colon_return`
    /// (`brink-syntax-native::parser::tests::declaration`), so the emitter
    /// must spell it back rather than refuse — the module doc's "supported"
    /// claim would otherwise be false for this one `TypeExpr` shape.
    #[test]
    fn fn_type_annotation_round_trips() {
        // A `flow` (not `fn`) — orthogonal to which body dialect a `fn`'s
        // default code-ground body now round-trips through (see
        // `fn_default_code_ground_body_round_trips_via_logic_block`, issue
        // #1972's second slice); this test is only about the `fn(...)`
        // *type* annotation on the parameter, so it keeps the simplest body
        // shape.
        let src = "flow apply(f: fn(int): bool) {\n  Hello.\n}\n";
        let emitted = lower_and_emit(src).expect("fn(...) type annotation must now emit");
        assert!(
            emitted.contains("fn(int): bool"),
            "expected the emitted source to spell the fn(...) type back out:\n{emitted}"
        );

        let hir = reparse_and_lower(&emitted);
        let Some(annotation) = &hir.knots[0].params[0].annotation else {
            panic!("re-lowered param lost its type annotation");
        };
        assert!(
            matches!(annotation, TypeExpr::Fn { .. }),
            "expected a re-lowered TypeExpr::Fn, got {annotation:?}"
        );
    }

    /// #1509: a *nested* flow's `: type` return clause (`hir::Stitch::
    /// return_type`, widening NG-C's `Knot.return_type`) must round-trip
    /// through the emitter exactly like a top-level flow/fn's does
    /// (`emit_knot`'s own `ret` suffix) — `emit_stitch` used to omit it
    /// silently.
    #[test]
    fn stitch_return_type_round_trips() {
        let src = "flow garden() {\n  flow gate(): int {\n    Onward.\n  }\n}\n";
        let emitted = lower_and_emit(src).expect("stitch return type must now emit");
        assert!(
            emitted.contains("gate(): int"),
            "expected the emitted source to spell the stitch's return type back out:\n{emitted}"
        );

        let hir = reparse_and_lower(&emitted);
        match &hir.knots[0].stitches[0].return_type {
            Some(TypeExpr::Named { name, .. }) => assert_eq!(name, "int"),
            other => panic!("re-lowered stitch lost its return type: {other:?}"),
        }
    }

    /// B1b (issue #1475): a template `{if}` conditional's `as` binding must
    /// round-trip, not silently disappear. Before this fix
    /// `CondKind::InitialCondition`'s emission never read
    /// `CondBranch::binding`, so the respelled source dropped the `as l`
    /// suffix — a different-behavior HIR behind a green round-trip check,
    /// since the success arm's body still referenced `l`.
    #[test]
    fn conditional_as_binding_round_trips() {
        let src = "flow a() {\n  {if some(9) as l: number {l} else: nobody}\n}\n";
        let emitted = lower_and_emit(src).expect("`as` binding conditional must now emit");
        assert!(
            emitted.contains("as l"),
            "emitted source dropped the `as` binding:\n{emitted}"
        );

        let hir = reparse_and_lower(&emitted);
        let body = &hir.knots[0].body;
        let Stmt::Conditional(cond) = &body.stmts[0] else {
            panic!("expected Conditional as the re-lowered body's first statement: {body:?}");
        };
        assert_eq!(
            cond.branches[0].binding.as_ref().map(|n| n.text.as_str()),
            Some("l"),
            "re-lowered conditional lost its `as` binding"
        );
    }

    /// The choice-guard form (issue #1508) round-trips the same way the
    /// template form above does: `hir::Choice::binding` respells as a
    /// trailing `as NAME` inside the guard's `{if …}` braces
    /// (`emit_choice`'s `binding_suffix`), and re-lowering the emitted
    /// source recovers it on the re-lowered `Choice`.
    #[test]
    fn choice_guard_as_binding_round_trips() {
        let src = "flow a() {\n  {?\n    * {if some(9) as n} [pick] {n}\n  }\n}\n";
        let emitted = lower_and_emit(src).expect("choice guard `as` binding must now emit");
        assert!(
            emitted.contains("as n"),
            "emitted source dropped the guard's `as` binding:\n{emitted}"
        );

        let hir = reparse_and_lower(&emitted);
        let body = &hir.knots[0].body;
        let Stmt::ChoiceSet(cs) = &body.stmts[0] else {
            panic!("expected ChoiceSet as the re-lowered body's first statement: {body:?}");
        };
        assert_eq!(
            cs.choices[0].binding.as_ref().map(|n| n.text.as_str()),
            Some("n"),
            "re-lowered choice lost its guard `as` binding"
        );
    }

    /// Issues #1581/#1590 fixed `Import.module` upstream of this emitter
    /// (real `::`-joined module names, honored aliases) — `emit_import`
    /// must now spell `use`/`import` back rather than refuse the whole
    /// file, covering both the qualified form and the bare form with an
    /// aliased and an unaliased item.
    #[test]
    fn use_and_import_round_trip() {
        let src = "import story::market\n\
                    use story::market::barter::haggle;\n\
                    use story::shop::{gold as g, silver};\n\n\
                    flow a() {\n  Hi.\n}\n";
        let emitted = lower_and_emit(src).expect("use/import must now emit");

        let hir = reparse_and_lower(&emitted);
        assert_eq!(hir.imports.len(), 3, "emitted source:\n{emitted}");

        assert!(!hir.imports[0].bare);
        assert_eq!(hir.imports[0].module, "story::market");
        assert!(hir.imports[0].items.is_empty());

        assert!(hir.imports[1].bare);
        assert_eq!(hir.imports[1].module, "story::market::barter");
        assert_eq!(hir.imports[1].items.len(), 1);
        assert_eq!(hir.imports[1].items[0].name, "haggle");
        assert_eq!(hir.imports[1].items[0].alias, None);

        assert!(hir.imports[2].bare);
        assert_eq!(hir.imports[2].module, "story::shop");
        assert_eq!(hir.imports[2].items.len(), 2);
        assert_eq!(hir.imports[2].items[0].name, "gold");
        assert_eq!(hir.imports[2].items[0].alias.as_deref(), Some("g"));
        assert_eq!(hir.imports[2].items[1].name, "silver");
        assert_eq!(hir.imports[2].items[1].alias, None);
    }

    /// Issue #1685: the `Expr::Lambda` arm added to `emit_expr` is
    /// reachable from a top-level `var` initializer
    /// (`a_lambda_lowers_in_a_top_level_var_initializer_too`,
    /// `crates/internal/brink-ir/tests/native_lambdas.rs`) — every other
    /// emit shape added to this file got a round-trip test right here
    /// (`fn_type_annotation_round_trips`, `stitch_return_type_round_trips`,
    /// `conditional_as_binding_round_trips`, `use_and_import_round_trip`),
    /// so the lambda arm needs one too, pinned on the trickiest spelling:
    /// an *annotated* expression body, where the ruled `:` return clause
    /// sits immediately before the unbraced body with no other separator
    /// (`|x: int|: int x + 1`) — the shape most likely to re-parse wrong.
    #[test]
    fn lambda_with_annotated_expr_body_round_trips() {
        let src = "var add = |x: int|: int x + 1\n";
        let emitted = lower_and_emit(src).expect("annotated lambda expr body must emit");
        assert!(
            emitted.contains("|x: int|: int"),
            "expected the emitted source to spell the param + return annotations back out:\n{emitted}"
        );

        let hir = reparse_and_lower(&emitted);
        let Expr::Lambda(lambda) = &hir.variables.first().expect("one var").value else {
            panic!(
                "re-lowered var initializer lost its lambda: {:?}",
                hir.variables
            );
        };
        assert_eq!(lambda.params.len(), 1);
        assert!(
            matches!(&lambda.params[0].annotation, Some(TypeExpr::Named { name, .. }) if name == "int"),
            "re-lowered lambda lost its param annotation: {:?}",
            lambda.params[0].annotation
        );
        assert!(
            matches!(&lambda.return_type, Some(TypeExpr::Named { name, .. }) if name == "int"),
            "re-lowered lambda lost its return annotation: {:?}",
            lambda.return_type
        );
        assert!(
            matches!(&lambda.body, LambdaBody::Expr(_)),
            "re-lowered lambda lost its expression body: {:?}",
            lambda.body
        );
    }

    /// Issue #1973: a value-carrying `return <expr>` at content-ground
    /// position now emits (and round-trips) instead of refusing with
    /// `"return with a value expression"`.
    ///
    /// Deliberately a `flow`, not an `fn` (unlike the corpus's own motivating
    /// shape, e.g. I003-tunnel-to-death's `is_alive` function): at the time
    /// this test was written, `emit_knot` always printed a plain `{` for
    /// both keywords, never the `>{ }` prose-ground override an `fn`'s
    /// body-dialect default needs to re-parse this emitter's prose-ground
    /// statement text (`emit_return`'s own `return <expr>` spelling
    /// included) — a real, separate, pre-existing gap this fix's own
    /// round-trip check surfaced (every `fn` this emitter produces already
    /// needed it, for a bare `return` too; nothing here changed that
    /// channel), flagged rather than folded into this PR. This test instead
    /// pinned exactly what #1973 changed: the grammar/lowering/emitter path
    /// for a value-carrying return, isolated from that unrelated
    /// container-keyword gap. `brink-analyzer`'s E032 (return outside
    /// function) is a different crate/pass this emitter test harness never
    /// runs — see `value_carrying_return_in_non_function_still_emits_e032`
    /// in `brink-analyzer` for that semantics pin. **Correction (issue
    /// #2029, fixed):** `emit_knot` now spells `>{ }` for every `fn` (see
    /// its own doc comment) — `fn_prose_body_value_return_round_trips`,
    /// below, is the `fn` counterpart this note used to say was missing.
    #[test]
    fn value_carrying_return_round_trips() {
        let src = "flow f() {\n  Hello.\n  return hp > 0\n}\n";
        let emitted = lower_and_emit(src).expect("value-carrying return must now emit");
        assert!(
            emitted.contains("return hp > 0"),
            "expected the emitted source to spell the return's value back out:\n{emitted}"
        );

        let hir = reparse_and_lower(&emitted);
        let Stmt::Return(r) = hir.knots[0].body.stmts.last().expect("a Return statement") else {
            panic!(
                "expected Return as the re-lowered body's last statement: {:?}",
                hir.knots[0].body.stmts
            );
        };
        assert!(
            matches!(&r.value, Some(Expr::Infix(_))),
            "re-lowered return lost its value expression: {:?}",
            r.value
        );
    }

    /// The tunnel-return redirect keeps its own dedicated spelling and must
    /// not be swallowed by the general value-expression case #1973 added —
    /// `Expr::DivertTarget` is checked first in `emit_return`.
    #[test]
    fn return_redirect_still_wins_over_general_value_emission() {
        let src = "flow b() {\n  Bye.\n}\nflow a() {\n  return -> b\n}\n";
        let emitted = lower_and_emit(src).expect("return redirect must emit");
        assert!(
            emitted.contains("return -> b"),
            "expected the tunnel-return redirect spelling, got:\n{emitted}"
        );
        assert!(
            !emitted.contains("return b"),
            "must not spell the redirect as a bare value expression:\n{emitted}"
        );
        reparse_and_lower(&emitted);
    }

    /// Review finding (w111): a code-ground `return` (inside a `~{ }` block)
    /// has no `return -> target` respelling — `parser/stmt.rs::return_stmt`'s
    /// doc states it "has no tunnel-redirect (`return -> x`) counterpart",
    /// and `parser/expr.rs::atom` has no divert-target atom for the
    /// general-expression arm of a code-ground `return` to reach — so no
    /// real `.brink` source can lower a `BlockStmt::Return` whose value is
    /// `Expr::DivertTarget` (this is a guard against `emit_return`'s shared
    /// use by the content-ground `Stmt::Return` printer, which *does*
    /// support it, not a reachable-from-source case — built by hand rather
    /// than parsed, same posture the finding itself took). The
    /// `BlockStmt::Return` arm must refuse rather than spell `return ->
    /// target`, which would fail to reparse at code-ground position.
    #[test]
    fn code_ground_return_with_divert_target_value_refuses_to_emit() {
        let lb = crate::LogicBlock {
            ptr: crate::Provenance::synthetic(
                crate::provenance::NodeClass::LogicBlock,
                rowan::TextRange::new(rowan::TextSize::new(0), rowan::TextSize::new(1)),
            ),
            stmts: vec![BlockStmt::Return(Return {
                ptr: None,
                kind: crate::ReturnKind::TunnelRedirect,
                value: Some(Expr::DivertTarget(crate::Path {
                    segments: vec![Name {
                        text: "b".to_string(),
                        range: rowan::TextRange::new(
                            rowan::TextSize::new(0),
                            rowan::TextSize::new(1),
                        ),
                    }],
                    range: rowan::TextRange::new(rowan::TextSize::new(0), rowan::TextSize::new(1)),
                    crosses_module_wall: false,
                })),
                onwards_args: Vec::new(),
            })],
            scope: LogicBlockScope::Standalone,
        };
        let mut out = String::new();
        let err = emit_logic_block(&mut out, "", 0, &lb, "test").expect_err(
            "a code-ground return of a divert target must refuse, not guess a spelling",
        );
        assert!(matches!(err, EmitError::Unsupported { .. }), "{err:?}");
    }

    /// Issue #1614/#1161: `HirFile::allow_scopes` carries a `(range,
    /// codes)` fact with no pointer back to the declaration it decorates —
    /// this emitter has no way to re-place the `@[allow(…)]` line, so a
    /// file that has one must refuse loudly rather than silently produce
    /// `.brink` text that has quietly stopped suppressing the diagnostic
    /// the original author explicitly allowed.
    #[test]
    fn allow_scope_is_refused_not_silently_dropped() {
        let src = "@[allow(E014)]\nvar gold = 0\n";
        let err = lower_and_emit(src).expect_err("an @[allow(…)] scope must not silently vanish");
        assert!(
            matches!(
                err,
                EmitError::Unsupported {
                    what: "@[allow(…)] suppression scopes",
                    ..
                }
            ),
            "expected the allow-scopes refusal, got {err:?}"
        );
    }

    /// A choice's body's own `Stmt` stream always starts `[divert?,
    /// EndOfLine, …body]` (`lower_native::choice::lower_choice`'s own
    /// preamble, unconditionally). A same-line divert (N-1) followed by a
    /// braced body reproduces the same three-element `[Divert, EndOfLine,
    /// Divert]` leading shape the ink-fed "gather-fold" case does (the one
    /// the old two-element `[Divert, EndOfLine]` pattern alone never
    /// covered) — this only checks the emit arm doesn't refuse it and
    /// produces re-parseable text carrying both diverts; exact re-lowered
    /// `Stmt` positions aren't asserted (the compact same-line divert
    /// becomes an ordinary braced-body line on the way back, which is a
    /// different-but-equally-faithful respelling, not a round-trip
    /// requirement this shape needs to meet byte-for-byte). See
    /// `brink-respell`'s `varying_choice` test for the real
    /// oracle-corpus fixture, proven episode-identical end to end, that
    /// motivated this fix.
    #[test]
    fn choice_body_with_trailing_statement_after_same_line_divert_round_trips() {
        let src = "flow a() {\n  {?\n    * Hop. -> b {\n      -> c\n    }\n  }\n}\n\
             flow b() {\n  Hi.\n}\n\
             flow c() {\n  Hey.\n}\n";
        let emitted =
            lower_and_emit(src).expect("a trailing statement after a same-line divert must emit");
        assert!(emitted.contains("-> b"), "{emitted}");
        assert!(emitted.contains("-> c"), "{emitted}");

        let hir = reparse_and_lower(&emitted);
        let Stmt::ChoiceSet(cs) = &hir.knots[0].body.stmts[0] else {
            panic!(
                "expected ChoiceSet as the re-lowered body's first statement: {:?}",
                hir.knots[0].body
            );
        };
        let target_names: Vec<String> = cs.choices[0]
            .body
            .stmts
            .iter()
            .filter_map(|s| match s {
                Stmt::Divert(d) => match &d.target.path {
                    DivertPath::Path(p) => Some(emit_path(p)),
                    _ => None,
                },
                _ => None,
            })
            .collect();
        assert_eq!(target_names, vec!["b".to_string(), "c".to_string()]);
    }

    /// A bare `(name)` label with nothing else on its own line, immediately
    /// followed by a `{?}` choice point — a `Stmt::LabeledBlock` whose
    /// *first* statement is a `ChoiceSet`, not `Content`
    /// (`tests/tier1/choices/default-choices`'s ` - (start)` is the real
    /// oracle-corpus shape this generalizes from — mechanically respelling
    /// it does emit and reparse cleanly now, but that fixture's ` - (start)`
    /// sits in *root* content, which needs `emit_file`'s synthesized
    /// `flow main() { … }` wrapper; that gives the gather a different
    /// qualified address path than the ink original and fails the
    /// differential on an unrelated addressing mismatch, not this fix —
    /// see the PR's own findings, not a `brink-respell` fixture, since it
    /// can't be made green without touching root-content addressing. This
    /// test isolates the same leading-shape fix inside an ordinary `flow`,
    /// with no root-content synthesis in the way).
    #[test]
    fn labeled_block_immediately_followed_by_choice_point_round_trips() {
        let src = "flow a() {\n  (start)\n  {?\n    *[Choice 1]\n    *[Choice 2]\n  }\n}\n";
        let emitted =
            lower_and_emit(src).expect("a label directly above a choice point must now emit");
        assert!(emitted.contains("(start)"), "{emitted}");

        let hir = reparse_and_lower(&emitted);
        let Stmt::LabeledBlock(b) = &hir.knots[0].body.stmts[0] else {
            panic!(
                "expected LabeledBlock as the re-lowered body's first statement: {:?}",
                hir.knots[0].body
            );
        };
        assert_eq!(b.label.as_ref().map(|n| n.text.as_str()), Some("start"));
        assert!(
            matches!(b.stmts.as_slice(), [Stmt::ChoiceSet(_)]),
            "expected the label's body to be exactly a ChoiceSet: {:?}",
            b.stmts
        );
    }

    // ── Issue #1972: the content-ground logic-line escape's printer ─────
    // ── (`Stmt::TempDecl`/`Assignment`/`ExprStmt` at prose-body position) ─
    //
    // Before this landed, all three refused with `EmitError::Unsupported`
    // even though `~ let name = expr`/`~ x = expr`/`~ expr` had a real
    // grammar and lowering (the `Assignment`/`ExprStmt` half since #1991,
    // `TempDecl` newly added by this same issue) — a real gap for
    // `brink-respell`'s ink→native corpus conversion (the "temp
    // declaration"/"assignment"/"expression statement" `full_corpus_sweep`
    // buckets), not a parser one.

    #[test]
    fn logic_line_assignment_round_trips() {
        let src = "flow a() {\n  ~ n = 5\n}\n";
        let emitted = lower_and_emit(src).expect("a content-ground assignment must now emit");
        assert!(emitted.contains("~ n = 5"), "{emitted}");

        let hir = reparse_and_lower(&emitted);
        let Stmt::Assignment(a) = &hir.knots[0].body.stmts[0] else {
            panic!(
                "expected Stmt::Assignment as the re-lowered body's first statement: {:?}",
                hir.knots[0].body
            );
        };
        assert_eq!(a.op, crate::AssignOp::Set);
        assert!(matches!(a.value, Expr::Int(5)));
    }

    #[test]
    fn logic_line_compound_assignment_round_trips() {
        for (src_op, expected_op) in [("+=", crate::AssignOp::Add), ("-=", crate::AssignOp::Sub)] {
            let src = format!("flow a() {{\n  ~ n {src_op} 3\n}}\n");
            let emitted =
                lower_and_emit(&src).expect("a content-ground compound assignment must now emit");
            assert!(emitted.contains(&format!("~ n {src_op} 3")), "{emitted}");

            let hir = reparse_and_lower(&emitted);
            let Stmt::Assignment(a) = &hir.knots[0].body.stmts[0] else {
                panic!("expected Stmt::Assignment: {:?}", hir.knots[0].body);
            };
            assert_eq!(a.op, expected_op);
        }
    }

    #[test]
    fn logic_line_bare_call_round_trips() {
        // `bump` is a `flow` (not `fn`) purely for simplicity — `emit_knot`
        // now spells a `fn`'s body correctly too (the `>{ }` override,
        // issue #2029), and its own default code-ground body round-trips
        // as a nested `~{ }` logic block since this issue's second slice
        // (`fn_default_code_ground_body_round_trips_via_logic_block`,
        // below); nothing about the fix under test here depends on which
        // keyword `bump` uses.
        let src = "flow bump() {\n  return\n}\nflow a() {\n  ~ bump()\n}\n";
        let emitted = lower_and_emit(src).expect("a content-ground bare call must now emit");
        assert!(emitted.contains("~ bump()"), "{emitted}");

        let hir = reparse_and_lower(&emitted);
        let a_body = &hir.knots[1].body;
        assert!(matches!(a_body.stmts[0], Stmt::ExprStmt(Expr::Call(..))));
    }

    #[test]
    fn logic_line_temp_decl_round_trips() {
        let src = "flow a() {\n  ~ let n = 5\n}\n";
        let emitted = lower_and_emit(src).expect("a content-ground temp decl must now emit");
        assert!(emitted.contains("~ let n = 5"), "{emitted}");

        let hir = reparse_and_lower(&emitted);
        let Stmt::TempDecl(t) = &hir.knots[0].body.stmts[0] else {
            panic!(
                "expected Stmt::TempDecl as the re-lowered body's first statement: {:?}",
                hir.knots[0].body
            );
        };
        assert_eq!(t.name.text, "n");
        assert!(matches!(t.value, Some(Expr::Int(5))));
    }

    #[test]
    fn logic_line_temp_decl_with_annotation_and_no_initializer_round_trips() {
        let src = "flow a() {\n  ~ let n: int\n}\n";
        let emitted =
            lower_and_emit(src).expect("an annotated, uninitialized temp decl must now emit");
        assert!(emitted.contains("~ let n: int"), "{emitted}");

        let hir = reparse_and_lower(&emitted);
        let Stmt::TempDecl(t) = &hir.knots[0].body.stmts[0] else {
            panic!("expected Stmt::TempDecl: {:?}", hir.knots[0].body);
        };
        assert!(t.value.is_none());
        assert!(t.annotation.is_some());
    }

    /// Issue #1974: a splice (`<- flow(args)`) reached *before* any choice
    /// line in a `{?}` point lowers to a plain `Stmt::ThreadStart` sitting
    /// immediately before the resulting `Stmt::ChoiceSet` in the same
    /// stream — `lower_native::choice::lower_choice_point`'s `preamble`.
    /// The emitter must re-nest it as a sibling line inside the `{?}`,
    /// ahead of the first choice, not refuse the whole file.
    #[test]
    fn leading_thread_start_splice_round_trips() {
        let src = "flow hub() {\n  {?\n    <- options(2)\n  }\n}\n\
                    flow options(count) {\n  -> DONE\n}\n";
        let emitted = lower_and_emit(src).expect("a leading thread-start splice must now emit");
        assert!(
            emitted.contains("<- options(2)"),
            "expected the emitted source to spell the splice (with its args) back out:\n{emitted}"
        );

        let hir = reparse_and_lower(&emitted);
        let Stmt::ThreadStart(ts) = &hir.knots[0].body.stmts[0] else {
            panic!(
                "expected a leading Stmt::ThreadStart in the re-lowered body: {:?}",
                hir.knots[0].body
            );
        };
        assert!(
            matches!(&ts.target.path, DivertPath::Path(p) if p.segments.last().is_some_and(|s| s.text == "options"))
        );
        assert!(matches!(ts.target.args.as_slice(), [Expr::Int(2)]));
        assert!(
            matches!(hir.knots[0].body.stmts.get(1), Some(Stmt::ChoiceSet(_))),
            "expected the ChoiceSet immediately after the re-lowered leading splice: {:?}",
            hir.knots[0].body
        );
    }

    /// Issue #1974: a splice reached *after* a choice line (still before
    /// the next choice/`else`) lowers by appending its `Stmt::ThreadStart`
    /// onto that choice's own `body.stmts`, as the last statement
    /// (`lower_native::choice`'s doc: "interspersed content 'belongs to
    /// the previous choice'"). The emitter must re-nest it as a sibling
    /// line printed right after that choice — never inside that choice's
    /// own nested `{}` body, which has no splice grammar at all.
    #[test]
    fn trailing_thread_start_splice_round_trips() {
        let src = "flow main() {\n  {?\n    * Look. You look around.\n    <- helper(3)\n    * Other choice.\n  }\n}\n\
                    flow helper(n) {\n  -> DONE\n}\n";
        let emitted = lower_and_emit(src).expect("a trailing thread-start splice must now emit");
        assert!(
            emitted.contains("<- helper(3)"),
            "expected the emitted source to spell the splice (with its args) back out:\n{emitted}"
        );

        let hir = reparse_and_lower(&emitted);
        let Stmt::ChoiceSet(cs) = &hir.knots[0].body.stmts[0] else {
            panic!(
                "expected ChoiceSet as the re-lowered body's first statement: {:?}",
                hir.knots[0].body
            );
        };
        assert_eq!(
            cs.choices.len(),
            2,
            "expected both choices to survive: {cs:?}"
        );
        let Some(Stmt::ThreadStart(ts)) = cs.choices[0].body.stmts.last() else {
            panic!(
                "expected the first choice's body to end in a re-lowered Stmt::ThreadStart: {:?}",
                cs.choices[0].body
            );
        };
        assert!(
            matches!(&ts.target.path, DivertPath::Path(p) if p.segments.last().is_some_and(|s| s.text == "helper"))
        );
        assert!(matches!(ts.target.args.as_slice(), [Expr::Int(3)]));
    }

    // ── Issue #1972 (second slice): `~ until cond` / `~{ … }` printers ───
    //
    // Before this landed, both refused with `EmitError::Unsupported`
    // unconditionally — `Stmt::LogicBlock`/`Stmt::Await` at prose-body
    // position were a native-**grammar** gap, not just an emission one (see
    // this module's doc), so `brink-respell` could never even reach these
    // printers from a real ink source; these tests build the HIR the
    // ordinary way (parse a `.brink` fixture) since the grammar now exists.

    #[test]
    fn logic_line_until_round_trips() {
        let src = "flow a() {\n  ~ until n > 0\n}\n";
        let emitted = lower_and_emit(src).expect("a content-ground `until` must now emit");
        assert!(emitted.contains("~ until n > 0"), "{emitted}");

        let hir = reparse_and_lower(&emitted);
        let Stmt::Await(a) = &hir.knots[0].body.stmts[0] else {
            panic!(
                "expected Stmt::Await as the re-lowered body's first statement: {:?}",
                hir.knots[0].body
            );
        };
        assert!(matches!(a.condition, Some(Expr::Infix(_))));
    }

    #[test]
    fn logic_line_block_round_trips() {
        let src = "flow a() {\n  ~{\n    let m = 1;\n    n = m;\n    bump();\n  }\n}\n";
        let emitted = lower_and_emit(src).expect("a content-ground logic block must now emit");
        assert!(emitted.contains("~{"), "{emitted}");
        assert!(emitted.contains("let m = 1;"), "{emitted}");
        assert!(emitted.contains("n = m;"), "{emitted}");
        assert!(emitted.contains("bump();"), "{emitted}");

        let hir = reparse_and_lower(&emitted);
        let Stmt::LogicBlock(lb) = &hir.knots[0].body.stmts[0] else {
            panic!(
                "expected Stmt::LogicBlock as the re-lowered body's first statement: {:?}",
                hir.knots[0].body
            );
        };
        assert_eq!(lb.scope, crate::LogicBlockScope::Standalone);
        assert_eq!(lb.stmts.len(), 3);
        assert!(matches!(lb.stmts[0], crate::BlockStmt::TempDecl(_)));
        assert!(matches!(lb.stmts[1], crate::BlockStmt::Assignment(_)));
        assert!(matches!(lb.stmts[2], crate::BlockStmt::ExprStmt(_)));
        // Review finding (w111): the block contains a call (`bump()`), so
        // the re-lowered stream must still carry the trailing
        // `Stmt::EndOfLine` `lower_logic_line`'s `LogicBlock` arm now
        // appends — round-tripping through the emitter and back must not
        // lose it, the same guard `logic_line_block_containing_a_call_
        // lowers_to_end_of_line` pins at the lowering layer directly.
        assert!(
            matches!(hir.knots[0].body.stmts.get(1), Some(Stmt::EndOfLine)),
            "expected a trailing Stmt::EndOfLine after the re-lowered LogicBlock: {:?}",
            hir.knots[0].body
        );
    }

    #[test]
    fn logic_line_block_with_nested_control_flow_refuses_to_emit() {
        // A deliberately narrower first slice (this module's doc): nested
        // `if`/`while`/`for` inside a `~{ }` block would need the full
        // code-ground control-flow printer, which this slice doesn't
        // build — refused loudly, never guessed.
        let src = "flow a() {\n  ~{\n    if n > 0 {\n      n = 1;\n    }\n  }\n}\n";
        let err = lower_and_emit(src)
            .expect_err("nested control flow inside a `~{ }` block must still refuse, not guess");
        assert!(matches!(err, EmitError::Unsupported { .. }));
    }

    #[test]
    fn fn_default_code_ground_body_round_trips_via_logic_block() {
        // A byproduct of this issue's second slice, not its main target: a
        // `fn`'s default code-ground body lowers to exactly one whole-body
        // `Stmt::LogicBlock(Standalone)` (`lower_stmt_block_as_body`'s
        // doc — a body with no `> text` split is byte-for-byte the
        // original, unsplit shape). `emit_knot` already spells a `fn`'s
        // body with the `>{ }` prose override (issue #2029), so that single
        // `LogicBlock` now round-trips as one nested `~{ }` escape inside
        // it — closing a slice of the "LogicBlock bodies aren't emittable"
        // gap this module's doc used to describe as wholly open, though the
        // nested-control-flow / split-scope residuals above still stand.
        let src = "fn shout() {\n  n = n + 1;\n}\n";
        let emitted = lower_and_emit(src).expect("a fn's default code-ground body must now emit");
        assert!(emitted.contains(">{"), "{emitted}");
        assert!(emitted.contains("~{"), "{emitted}");

        let hir = reparse_and_lower(&emitted);
        let Stmt::LogicBlock(lb) = &hir.knots[0].body.stmts[0] else {
            panic!(
                "expected the re-lowered fn body to still be one whole-body LogicBlock: {:?}",
                hir.knots[0].body
            );
        };
        assert_eq!(lb.scope, crate::LogicBlockScope::Standalone);
        assert!(matches!(
            lb.stmts.as_slice(),
            [crate::BlockStmt::Assignment(_)]
        ));
    }

    // ── Issue #1975: `CondKind::IfElse` re-nesting ───────────────────────
    //
    // `CondKind::IfElse` — ink's own independently-chained,
    // no-shared-subject conditional (`{ - cond1: … - cond2: … - else: … }`)
    // — has no native lowering path (`lower_native::cond::lower_conditional`
    // only ever constructs `InitialCondition`/`Switch`, plus the empty-branch
    // parser-recovery case), so it can't be reached through `lower_and_emit`
    // like the tests above. These build the HIR by hand instead, calling
    // `emit_conditional` directly — the same shape `brink-respell`'s
    // `ink_corpus_convert.rs::ifelse_ext_three_way_chain` proves end to end
    // from a real ink source (that test is the one that fails without this
    // fix; these are the isolated, fast unit-level complement).

    fn synthetic_branch(condition: Option<Expr>, stmt: Stmt) -> CondBranch {
        let empty_range = rowan::TextRange::empty(rowan::TextSize::from(0));
        CondBranch {
            ptr: crate::provenance::Provenance::synthetic(
                crate::provenance::NodeClass::ConditionalBranch,
                empty_range,
            ),
            condition,
            binding: None,
            body: Block {
                label: None,
                stmts: vec![stmt.clone()],
                container_id: None,
                tail: crate::tail_from_stmts(&[stmt]),
            },
            container_id: None,
        }
    }

    /// A 2-way `IfElse` (one real condition, one trailing plain `else`) —
    /// the minimal shape — re-nests to exactly the same text
    /// `CondKind::InitialCondition` would have produced for the same
    /// branches.
    #[test]
    fn if_else_two_way_chain_emits_nested_native_syntax() {
        let empty_range = rowan::TextRange::empty(rowan::TextSize::from(0));
        let cond = Conditional {
            ptr: crate::provenance::Provenance::synthetic(
                crate::provenance::NodeClass::Conditional,
                empty_range,
            ),
            kind: CondKind::IfElse,
            branches: vec![
                synthetic_branch(Some(Expr::Bool(true)), Stmt::ExprStmt(Expr::Int(1))),
                synthetic_branch(None, Stmt::ExprStmt(Expr::Int(2))),
            ],
        };
        let mut out = String::new();
        emit_conditional(&mut out, "", 0, &cond, "test")
            .expect("a 2-way IfElse chain must now emit");
        assert_eq!(out, "{if true {\n  ~ 1\n} else {\n  ~ 2\n}}\n");
    }

    /// A 3-way `IfElse` (two real conditions plus a trailing `else`) —
    /// `emit_conditional`'s old `CondKind::IfElse` arm refused this
    /// unconditionally — re-nests into `{if …} else { {if …} else { … } }`,
    /// which reparses back to two nested `InitialCondition`s (exactly what
    /// a native-authored `else if` chain would have produced).
    #[test]
    fn if_else_three_way_chain_emits_nested_native_syntax_and_reparses() {
        let empty_range = rowan::TextRange::empty(rowan::TextSize::from(0));
        let cond = Conditional {
            ptr: crate::provenance::Provenance::synthetic(
                crate::provenance::NodeClass::Conditional,
                empty_range,
            ),
            kind: CondKind::IfElse,
            branches: vec![
                synthetic_branch(Some(Expr::Bool(true)), Stmt::ExprStmt(Expr::Int(1))),
                synthetic_branch(Some(Expr::Bool(false)), Stmt::ExprStmt(Expr::Int(2))),
                synthetic_branch(None, Stmt::ExprStmt(Expr::Int(3))),
            ],
        };
        let mut out = String::new();
        emit_conditional(&mut out, "", 0, &cond, "test")
            .expect("a 3-way IfElse chain must now emit");
        assert_eq!(
            out,
            "{if true {\n  ~ 1\n} else {\n  {if false {\n    ~ 2\n  } else {\n    ~ 3\n  }}\n}}\n"
        );

        // The emitted text must itself be legal `.brink` source whose body
        // opens with a conditional statement carrying a *nested*
        // `Conditional` inside its `else` arm — proving this isn't just
        // syntactically-plausible text but a real, reparseable nesting
        // (mirroring what a native-authored `else if` chain lowers to,
        // per `lower_native::cond::lower_conditional`'s doc).
        let src = format!("flow a() {{\n{out}}}\n");
        let hir = reparse_and_lower(&src);
        let Some(Stmt::Conditional(outer)) = hir.knots[0].body.stmts.first() else {
            panic!(
                "expected the re-lowered body's first statement to be a Conditional: {:?}",
                hir.knots[0].body
            );
        };
        assert_eq!(outer.kind, CondKind::InitialCondition);
        assert_eq!(outer.branches.len(), 2);
        let else_arm = &outer.branches[1];
        assert!(else_arm.condition.is_none());
        let Some(Stmt::Conditional(inner)) = else_arm.body.stmts.first() else {
            panic!(
                "expected the outer else arm's body to open with a nested Conditional: {:?}",
                else_arm.body
            );
        };
        assert_eq!(inner.kind, CondKind::InitialCondition);
        assert_eq!(inner.branches.len(), 2);
    }

    /// An `IfElse` chain with **no** trailing `else` (every branch carries
    /// its own condition) still needs an `else { }` wrapper to hold the
    /// recursively-nested next condition — but the innermost nested
    /// `Conditional` must not spuriously synthesize a terminal bare `else`
    /// arm of its own, since none was present in the source branches.
    #[test]
    fn if_else_chain_with_no_trailing_else_has_no_innermost_else_arm() {
        let empty_range = rowan::TextRange::empty(rowan::TextSize::from(0));
        let cond = Conditional {
            ptr: crate::provenance::Provenance::synthetic(
                crate::provenance::NodeClass::Conditional,
                empty_range,
            ),
            kind: CondKind::IfElse,
            branches: vec![
                synthetic_branch(Some(Expr::Bool(true)), Stmt::ExprStmt(Expr::Int(1))),
                synthetic_branch(Some(Expr::Bool(false)), Stmt::ExprStmt(Expr::Int(2))),
            ],
        };
        let mut out = String::new();
        emit_conditional(&mut out, "", 0, &cond, "test")
            .expect("an IfElse chain with no trailing else must now emit");
        assert_eq!(
            out,
            "{if true {\n  ~ 1\n} else {\n  {if false {\n    ~ 2\n  }}\n}}\n"
        );
    }

    /// A `fn` whose body is the prose-ground override (`>{ … }`, charter
    /// §4) must round-trip back into the **same** `>{ … }` spelling, not
    /// the bare `{` (which would select the *code*-ground default). This
    /// printer's statement stream (`emit_return`, here) only ever spells
    /// prose-ground syntax (no `;`) — under the wrong default, reparsing
    /// fails outright (issue #2029: `expected SEMICOLON, found R_BRACE`
    /// with the bug present). A value-carrying `return` (issue #1973) is
    /// the shape that surfaced this — see `emit_return`'s own doc.
    #[test]
    fn fn_prose_body_value_return_round_trips() {
        let src = "fn heal(hp) >{\n  return hp\n}\n";
        let emitted = lower_and_emit(src).expect("fn with a value return must emit");
        assert!(
            emitted.contains(">{"),
            "emitted fn body must carry the `>{{` prose-ground override:\n{emitted}"
        );

        let hir = reparse_and_lower(&emitted);
        assert!(hir.knots[0].is_function);
        let Stmt::Return(r) = &hir.knots[0].body.stmts[0] else {
            panic!(
                "expected Stmt::Return as the re-lowered fn body's first statement: {:?}",
                hir.knots[0].body
            );
        };
        assert!(matches!(r.value, Some(Expr::Path(_))));
    }

    /// The same dialect-selector bug reaches *any* statement the emitter
    /// regenerates for a `fn`'s prose body, not just a value-carrying
    /// `return` — a bare `return` and ordinary content lines hit it too
    /// (issue #2029's stated scope, broader than #1973's).
    #[test]
    fn fn_prose_body_bare_return_and_content_round_trip() {
        let src = "fn greet() >{\n  Hi.\n  return\n}\n";
        let emitted = lower_and_emit(src).expect("fn with prose content + bare return must emit");
        assert!(
            emitted.contains(">{"),
            "emitted fn body must carry the `>{{` prose-ground override:\n{emitted}"
        );

        let hir = reparse_and_lower(&emitted);
        assert!(hir.knots[0].is_function);
        let stmts = &hir.knots[0].body.stmts;
        assert!(
            stmts.iter().any(|s| matches!(s, Stmt::Content(_))),
            "expected a Content statement among the re-lowered fn body: {stmts:?}"
        );
        assert!(
            stmts
                .iter()
                .any(|s| matches!(s, Stmt::Return(r) if r.value.is_none())),
            "expected a bare Return statement among the re-lowered fn body: {stmts:?}"
        );
    }

    /// A `flow`'s bare `{` is already its prose default — `emit_knot` must
    /// not start writing a spurious selector for it (only `fn` needs the
    /// override; a `flow` never does).
    #[test]
    fn flow_body_has_no_selector_prefix() {
        let src = "flow a() {\n  Hi.\n}\n";
        let emitted = lower_and_emit(src).expect("flow must emit");
        assert!(
            emitted.contains("flow a() {\n"),
            "flow header must stay a bare `{{`, no selector:\n{emitted}"
        );
    }

    // ── `pub` visibility round-trip (issue #2200) ──────────────────────
    //
    // Before this fix, every one of these seven declaration kinds hit the
    // old blanket `visibility.is_some()` refusal the moment `pub` (issue
    // #1582/#2194) made `Some(VisibilityMark::Public)` reachable from a
    // real native parse. These tests don't go through
    // `crates/internal/brink-respell/tests/round_trip.rs`'s
    // `round_trip_case` (full `brink-analyzer` pass via
    // `explore_from_brink_native`) — deliberately: a single-file harness
    // with no `module { }` wrapper is always an *undeclared* module
    // (`brink-analyzer::manifest::effective_visibility`), whose default
    // visibility is already `Public`, so any top-level `pub` there is a
    // *redundant* override and trips `E092` on the hand-written original
    // itself, before any emit/reparse — orthogonal to this fix. This
    // module's own `lower_and_emit`/`reparse_and_lower` pair (the same
    // narrower discipline `labeled_gather_continuation_round_trips` above
    // already established) proves the emitter/parser agree without that
    // detour.

    #[test]
    fn pub_var_const_flags_struct_extern_round_trip() {
        let src = "\
pub var hp: int = 10
pub const cap = 100
pub flags mood = calm, wary
pub struct npc {
  hp: int
}
pub extern log_msg(msg)

flow a() {
  Hi.
}
";
        let emitted = lower_and_emit(src).expect("pub var/const/flags/struct/extern must now emit");
        assert!(
            emitted.contains("pub var hp"),
            "var must keep its pub prefix:\n{emitted}"
        );
        assert!(
            emitted.contains("pub const cap"),
            "const must keep its pub prefix:\n{emitted}"
        );
        assert!(
            emitted.contains("pub flags mood"),
            "flags must keep its pub prefix:\n{emitted}"
        );
        assert!(
            emitted.contains("pub struct npc"),
            "struct must keep its pub prefix:\n{emitted}"
        );
        assert!(
            emitted.contains("pub extern log_msg"),
            "extern must keep its pub prefix:\n{emitted}"
        );

        let hir = reparse_and_lower(&emitted);
        assert_eq!(
            hir.variables[0].visibility,
            Some(crate::VisibilityMark::Public)
        );
        assert_eq!(
            hir.constants[0].visibility,
            Some(crate::VisibilityMark::Public)
        );
        assert_eq!(hir.lists[0].visibility, Some(crate::VisibilityMark::Public));
        assert_eq!(
            hir.structs[0].visibility,
            Some(crate::VisibilityMark::Public)
        );
        assert_eq!(
            hir.externals[0].visibility,
            Some(crate::VisibilityMark::Public)
        );
    }

    #[test]
    fn pub_flow_knot_and_nested_stitch_round_trip() {
        let src = "pub flow a() {\n  Hi.\n\n  pub flow b() {\n    Inner.\n  }\n}\n";
        let emitted = lower_and_emit(src)
            .expect("pub flow (knot) and nested pub flow (stitch) must now emit");
        assert!(
            emitted.starts_with("pub flow a("),
            "top-level flow must keep its pub prefix:\n{emitted}"
        );
        assert!(
            emitted.contains("pub flow b("),
            "nested flow (stitch) must keep its pub prefix:\n{emitted}"
        );

        let hir = reparse_and_lower(&emitted);
        assert_eq!(hir.knots[0].visibility, Some(crate::VisibilityMark::Public));
        assert_eq!(
            hir.knots[0].stitches[0].visibility,
            Some(crate::VisibilityMark::Public)
        );
    }

    #[test]
    fn pub_fn_round_trips() {
        let src = "pub fn heal() {\n  return 1;\n}\n";
        let emitted = lower_and_emit(src).expect("pub fn must now emit");
        assert!(
            emitted.starts_with("pub fn heal("),
            "fn must keep its pub prefix:\n{emitted}"
        );

        let hir = reparse_and_lower(&emitted);
        assert_eq!(hir.knots[0].visibility, Some(crate::VisibilityMark::Public));
    }

    #[test]
    fn absent_visibility_still_emits_with_no_pub_prefix() {
        // Regression guard: `None` (no `pub`, the pre-#2194 default) must
        // stay exactly as before — no prefix, no behavior change.
        let src = "var hp = 1\nflow a() {\n  Hi.\n}\n";
        let emitted = lower_and_emit(src).expect("undecorated declarations must still emit");
        assert!(
            emitted.starts_with("var hp"),
            "var with no visibility mark must not gain a pub prefix:\n{emitted}"
        );
        assert!(
            emitted.contains("\nflow a("),
            "flow with no visibility mark must not gain a pub prefix:\n{emitted}"
        );
    }

    #[test]
    fn private_visibility_mark_is_still_refused_with_a_named_reason() {
        // `lower_native::decl::visibility_mark` can only ever produce
        // `None` or `Some(VisibilityMark::Public)` — native has no
        // `priv`/`private` keyword and no `#@private` tag-directive
        // channel (that's ink-dialect-only). `Some(Private)` is
        // unreachable from a real `.brink` parse, and ink-sourced HIR
        // with any visibility directive is refused earlier at the file
        // level (`refuse_unsupported_file_channels` on
        // `HirFile::visibility`) — so this arm is defense-in-depth
        // against hand-built HIR (exactly what this test constructs):
        // keep refusing with a reason naming the real cause, never
        // silently drop or mis-emit as `pub`.
        let src = "var hp = 10\n";
        let parse = brink_syntax_native::parse(src);
        let tree = parse.tree();
        let (mut hir, _manifest, diags) = crate::hir::lower_native::lower(crate::FileId(0), &tree);
        assert!(diags.is_empty(), "unexpected diagnostics: {diags:?}");
        hir.variables[0].visibility = Some(crate::VisibilityMark::Private);

        let err = emit_file(&hir)
            .expect_err("Some(Private) has no native spelling and must stay refused");
        let msg = err.to_string();
        assert!(
            msg.contains("var directive channel"),
            "refusal message should name the var-directive channel, got: {msg}"
        );
    }
}