buildlang 1.15.0

BuildLang (buildc): an effects-oriented compiler with a verified C path, shader output, experimental backends, opt-in linear types, and re-checkable accountability + scientific-runtime receipts
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
//! Increment 2b/2c: the MIR affine/borrow checker.
//!
//! A forward move/init dataflow over `#[linear]` locals, run to a fixpoint over
//! the CFG. It flags the SOUNDNESS-critical linear-resource violations that the
//! name-keyed AST tracker cannot follow through MIR places:
//!
//! - **use-after-move** (double-consume): a linear local moved while already
//!   `Moved` on the incoming path.
//! - **borrow-after-move**: a linear local READ (`Ref`/`AddressOf`/deref/field)
//!   while `Moved` (closes class 5).
//! - **move-out-of-shared-borrow**: a linear referent moved out through a
//!   `Deref` of a shared (`&`, `is_mut == false`) reference (closes class 1
//!   direct form), OR by passing a shared borrow of a linear into a callee
//!   parameter that moves the referent out and returns it (closes class 3,
//!   interprocedurally).
//!
//! # 2c additions
//!
//! - **Match-idiom / class 1 match form.** buildlang does not lower `match` to a
//!   `Switch`; it emits a `tag`-read + `Eq` + `If`-chain, and binds a variant
//!   payload with `MirRValue::VariantField { base, .. }`. A `VariantField` bind
//!   is a MOVE of its `base` (a partial move of the scrutinee, tracked
//!   conservatively as the WHOLE base moving). Matching a linear enum through a
//!   shared `&` first materializes `let s = *r` (`Deref` of the shared borrow),
//!   which the 2b move-out-of-shared-borrow rule already flags.
//! - **Class 3 (generic deref through a borrow parameter).** buildlang
//!   monomorphizes generics, but the monomorphization can erase the referent's
//!   linearity (a `deref_any::<Coin>` specializes to `deref_any_i32` with
//!   `r: &i32`), so the concrete linear never appears as a linear local inside
//!   the callee. The move-out is invisible per-function. We close it
//!   interprocedurally: a module pre-pass finds every function whose
//!   by-reference parameter's referent leaves the borrow by value — either
//!   DIRECTLY (the body derefs it and returns the dereferenced value) or
//!   TRANSITIVELY (the body passes it into an already-borrow-escaping parameter
//!   of a callee). This "borrow-escaping" set is computed as a FIXPOINT over the
//!   call graph, so a one-level wrapper `wrap(r) { deref_any(r) }` is caught
//!   even though `wrap` never derefs `r` itself (closing the transitive class-3
//!   hole). A call that passes a shared borrow of a linear (`&coin`) into such a
//!   parameter moves the referent out of the shared borrow ->
//!   `LinearMoveOutOfBorrow`. A callee that only READS through the borrow (never
//!   derefs-and-returns it and never hands it to an escaping callee position,
//!   e.g. `peek(c: &Coin) -> i64 { 0 }`) is not borrow-escaping, so the legal
//!   borrow-read case is not flagged.
//!
//! Pure function of MIR (`super::cfg` / `super::liveness` substrate + the 2a
//! linearity annotations and span side-table). No `TypeContext`.
//!
//! # Soundness disposition
//!
//! This is a CHECKER, so a missed error is unsound. The dataflow is
//! conservative in the sound direction: at a CFG merge a local is `Moved` if it
//! is `Moved` on ANY predecessor path (maybe-moved), so a value consumed on one
//! branch is treated as consumed after the join. When it is genuinely ambiguous
//! whether a linear appears in a MOVING position, we treat it as a move (missing
//! a move is unsound; over-reporting is a precision concern deferred to 2c). The
//! direct single-move / loop-rebind / borrow-only cases are pinned precise by
//! the unit tests so this conservatism does not regress safe code.

use std::collections::{HashMap, HashSet};
use std::sync::Arc;

use crate::codegen::ir::{
    LocalId, MirBlock, MirFunction, MirPlace, MirRValue, MirStmtKind, MirTerminator, MirValue,
    PlaceProjection,
};
use crate::lexer::Span;
use crate::types::{TypeError, TypeErrorWithSpan};

use super::cfg::{block_id_index, reachable_blocks, terminator_successors};

/// Move/init state of a single linear local at a program point.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum MoveState {
    /// The local owns a live value (freshly defined or never consumed).
    Init,
    /// The local's value has been consumed (moved out).
    Moved,
}

/// Per-block move/init lattice: `state[block][local]`. Missing = `Init`.
type BlockState = HashMap<LocalId, MoveState>;

/// Interprocedural context threaded into the per-function check. Computed once
/// per module by `check_module`; empty for the standalone `check(func)` entry
/// point (which is then exactly the 2b per-function behavior plus the local 2c
/// idiom handling).
#[derive(Debug, Default, Clone)]
struct LinearContext {
    /// For each function NAME, the set of parameter INDICES that are
    /// "borrow-escaping": the parameter arrives by reference (`Ptr`) and the
    /// referent leaves the borrow by value out of the function — either DIRECTLY
    /// (the body derefs it and returns the dereferenced value) or TRANSITIVELY
    /// (the body passes it into an already-borrow-escaping parameter of a
    /// callee). Computed as a FIXPOINT over the call graph
    /// (`escaping_params_fixpoint`), so a one-level wrapper `wrap(r) {
    /// deref_any(r) }` is included even though `wrap` never derefs `r` itself.
    /// Passing a shared borrow of a linear into such a parameter moves the
    /// referent OUT of the borrow (class 3). A parameter that is only READ
    /// through (never deref-and-returned, never handed to an escaping callee
    /// position) is absent, so the legal borrow-read case is not flagged.
    borrow_escaping_params: HashMap<Arc<str>, HashSet<usize>>,
}

/// True iff local `id` is `#[linear]` (2a annotation: the `"linear"` marker in
/// `MirLocal.annotations`).
fn is_linear_local(func: &MirFunction, id: LocalId) -> bool {
    func.locals
        .iter()
        .find(|l| l.id == id)
        .is_some_and(|l| l.annotations.iter().any(|a| a.as_ref() == "linear"))
}

/// The `MirLocal.name` of `id`, best-effort, for diagnostics (`_N` fallback).
fn local_name(func: &MirFunction, id: LocalId) -> String {
    func.locals
        .iter()
        .find(|l| l.id == id)
        .and_then(|l| l.name.as_ref())
        .map(|n| n.to_string())
        .unwrap_or_else(|| format!("_{}", id.0))
}

/// Build the set of pointer locals that are SHARED (`&`, `is_mut == false`)
/// borrows OF A LINEAR local, by scanning statement provenance. We do NOT rely
/// on any annotation on the pointer local: real 2a lowering tags only linear
/// ADT `Struct` locals, never a `&Coin` binding (whose type is
/// `Ptr(Struct("Coin"))`). Instead we recover the borrow from its defining
/// statement:
///
///   `dest = &referent`      (`Ref { is_mut: false, place }`)
///   `dest = addr_of referent` (`AddressOf { is_mut: false, place }`)
///
/// where `place.local` is a linear local. Each such `dest` is a shared ref to a
/// linear value; a `Deref` of it is the illegal move-out-of-shared-borrow.
///
/// SCOPE: only a DIRECT borrow of a linear BASE local (`place.local` linear).
/// Borrowing a linear FIELD (`&obj.coin`) or a reborrow leaves `dest` out of
/// the set (its base local is not linear), which is correct for 2b: those are
/// 2c and are simply not flagged here.
fn shared_linear_ref_set(func: &MirFunction) -> HashSet<LocalId> {
    let mut set = HashSet::new();
    let Some(blocks) = &func.blocks else {
        return set;
    };
    // Seed: a direct shared borrow `dest = &linear`.
    for block in blocks {
        for stmt in &block.stmts {
            if let MirStmtKind::Assign { dest, value } = &stmt.kind {
                let place = match value {
                    MirRValue::Ref {
                        is_mut: false,
                        place,
                    }
                    | MirRValue::AddressOf {
                        is_mut: false,
                        place,
                    } => place,
                    _ => continue,
                };
                // Only a borrow of the linear local itself (base, no
                // projections into a linear field) is the direct 2b case.
                if place.projections.is_empty() && is_linear_local(func, place.local) {
                    set.insert(*dest);
                }
            }
        }
    }
    // Propagate the shared-ref taint through by-value copies of the pointer:
    // `dest = Use(src)` / `Cast(src)` where `src` is a shared linear ref makes
    // `dest` one too. buildlang copies the borrow into a scrutinee temp before a
    // `match &w` (`_5 = Use(_4)`); without this the field-extract at the copy
    // would not be seen as going through the borrow. A small fixpoint handles a
    // copy chain regardless of block order.
    let mut changed = true;
    while changed {
        changed = false;
        for block in blocks {
            for stmt in &block.stmts {
                if let MirStmtKind::Assign { dest, value } = &stmt.kind {
                    let src = match value {
                        MirRValue::Use(MirValue::Local(s))
                        | MirRValue::Cast {
                            value: MirValue::Local(s),
                            ..
                        } => *s,
                        _ => continue,
                    };
                    if set.contains(&src) && set.insert(*dest) {
                        changed = true;
                    }
                }
            }
        }
    }
    set
}

/// The name a `Call` terminator dispatches to, if it is a direct named call
/// (`Global` for a regular function, `Function` for a monomorphized generic).
fn call_target_name(func_val: &MirValue) -> Option<&Arc<str>> {
    match func_val {
        MirValue::Global(n) | MirValue::Function(n) => Some(n),
        _ => None,
    }
}

/// Map each by-reference (`Ptr`) parameter LOCAL of `func` to its parameter
/// INDEX. Param index == LocalId enumeration order (builder invariant: params
/// are the leading locals `0..N` in declaration order). Only pointer params are
/// candidates for borrow-escaping (a by-value param cannot leak a borrow it does
/// not hold). Shared by the direct (`borrow_escaping_params_of`) and the
/// inductive (call-graph fixpoint) escape rules.
fn ref_param_index_of(func: &MirFunction) -> HashMap<LocalId, usize> {
    func.locals
        .iter()
        .enumerate()
        .filter(|(_, l)| l.is_param && matches!(l.ty, crate::codegen::ir::MirType::Ptr(_)))
        .map(|(idx, l)| (l.id, idx))
        .collect()
}

/// The set of parameter indices of `func` that are DIRECTLY "borrow-escaping": a
/// by-reference (`Ptr`) parameter whose referent is DEREFERENCED by value and
/// RETURNED. This is the structural signature of a `fn deref_any<T>(r: &T) -> T
/// { *r }` after monomorphization (`r: &i32`, body `t = *r; return t`) — the
/// callee takes ownership of the referent out of a borrow it does not own.
///
/// The analysis is intraprocedural and purely structural (no types needed):
///   1. A parameter local `p` (`is_param`, `Ptr` type) is a candidate.
///   2. A statement `t = Deref(p)` (a by-value move-out of `*p`) taints `t`.
///   3. Tainting propagates by-value through:
///        - `t2 = Use(t)` / `Cast(t)` (a copy of a tainted local),
///        - `t2 = Aggregate { .., t, .. }` (laundering the referent into a
///          struct/tuple/enum-variant/array; field-INSENSITIVE — ANY tainted
///          operand taints the whole aggregate),
///        - `t2 = t.field` / `t.Variant._i` / `t[i]`
///          (`FieldAccess`/`VariantField`/`IndexAccess` projecting the referent
///          back out of a tainted aggregate; keyed off the BASE's taint).
///      So laundering `*r` through a struct/tuple/enum and unboxing it again
///      (`fn deref_boxed<T>(r:&T)->T { let b = Box{item:*r}; b.item }`, or the
///      tuple/enum/multi-level-wrapper variants) stays tainted end to end.
///   4. `Return(Some(t))` where `t` is tainted marks the ORIGINATING parameter
///      as borrow-escaping.
///
/// PRECISION: the taint only reaches the ORIGINATING param through `Return`. A
/// function that derefs `*r` into an aggregate but stores it LOCALLY and returns
/// a scalar / nothing (a `peek`-like fn) never returns a tainted local, so its
/// param is NOT flagged. The aggregate over-approximation (a returned aggregate
/// whose tainted field is not the returned one) still only fires on `Return`, so
/// it cannot flag a non-returning function.
///
/// This is the BASE case of the module-level fixpoint (`escaping_params_fixpoint`):
/// a param that escapes WITHOUT delegating through a callee. The INDUCTIVE case
/// (a param passed into an already-escaping callee param) is added by the
/// fixpoint, which closes the transitive one-level-wrapper hole (`wrap` -> `deref_any`).
///
/// SOUNDNESS DISPOSITION: this over-approximates "the callee moves the referent
/// out of the borrow and hands it back". It is deliberately NARROW (deref +
/// return), so a callee that only READS through the borrow (`peek(c: &Coin) ->
/// i64 { 0 }`, never derefs it) is NOT flagged — the legal borrow-read case is
/// preserved. A callee that derefs-and-returns but is called with a NON-linear,
/// NON-borrowed argument is harmless (the call-site rule only fires when the arg
/// is a shared borrow of a linear).
fn borrow_escaping_params_of(func: &MirFunction) -> HashSet<usize> {
    let mut escaping = HashSet::new();
    let Some(blocks) = &func.blocks else {
        return escaping;
    };

    // Candidate reference parameters: leading locals with `is_param` and a
    // pointer type. Param index == LocalId index (builder invariant: params are
    // locals 0..N in declaration order).
    let ref_param_index = ref_param_index_of(func);
    if ref_param_index.is_empty() {
        return escaping;
    }

    // `tainted[local] = originating param index`: `local` currently holds the
    // dereferenced referent of that reference parameter (moved out of the
    // borrow). A fixpoint over the CFG is unnecessary for the shapes buildlang
    // emits (a monomorphized `*r; return` is straight-line), but we do a simple
    // multi-pass to be robust to block ordering / a copy chain across blocks.
    let mut tainted: HashMap<LocalId, usize> = HashMap::new();
    // Taint of a single operand `MirValue`: the origin param index if it is a
    // tainted local, else `None`. Used by the aggregate-operand scan.
    let taint_of = |tainted: &HashMap<LocalId, usize>, v: &MirValue| -> Option<usize> {
        match v {
            MirValue::Local(l) => tainted.get(l).copied(),
            _ => None,
        }
    };
    let mut changed = true;
    while changed {
        changed = false;
        for block in blocks {
            for stmt in &block.stmts {
                if let MirStmtKind::Assign { dest, value } = &stmt.kind {
                    let origin = match value {
                        // t = *p : move the referent of ref-param `p` out.
                        MirRValue::Deref {
                            ptr: MirValue::Local(p),
                            ..
                        } => ref_param_index.get(p).copied(),
                        // t2 = Use(t) / Cast(t) : a by-value copy of a tainted
                        // local carries the taint forward.
                        MirRValue::Use(MirValue::Local(s))
                        | MirRValue::Cast {
                            value: MirValue::Local(s),
                            ..
                        } => tainted.get(s).copied(),
                        // t2 = Aggregate { .., t, .. } : laundering `*r` through
                        // a struct/tuple/enum-variant/array carries the taint to
                        // the aggregate. Field-INSENSITIVE and conservative: ANY
                        // tainted operand taints the WHOLE aggregate (we do not
                        // track which field holds it). If the aggregate is later
                        // returned, the param escapes — even if the returned
                        // field is not the tainted one. Over-approximates in the
                        // sound direction (see the module doc).
                        MirRValue::Aggregate { operands, .. } => {
                            operands.iter().find_map(|op| taint_of(&tainted, op))
                        }
                        // t2 = base.field / base.Variant._i / base[i] : projecting
                        // a field/variant-payload/element back OUT of a tainted
                        // aggregate re-exposes the laundered referent. Keyed off
                        // the BASE's taint (field-insensitive), so unboxing the
                        // struct in `deref_boxed` (`b.item`) stays tainted.
                        MirRValue::FieldAccess { base, .. }
                        | MirRValue::VariantField { base, .. }
                        | MirRValue::IndexAccess { base, .. } => taint_of(&tainted, base),
                        _ => None,
                    };
                    if let Some(idx) = origin {
                        if tainted.insert(*dest, idx) != Some(idx) {
                            changed = true;
                        }
                    }
                }
            }
        }
    }

    // A `Return(Some(local))` of a tainted local escapes its origin parameter.
    for block in blocks {
        if let Some(MirTerminator::Return(Some(MirValue::Local(l)))) = &block.terminator {
            if let Some(&idx) = tainted.get(l) {
                escaping.insert(idx);
            }
        }
    }
    escaping
}

/// Compute, for EVERY function in the module, the set of borrow-escaping
/// parameter indices, as a FIXPOINT over the call graph. This is what closes the
/// transitive class-3 hole: a one-level wrapper `fn wrap<T>(r: &T) -> T {
/// deref_any(r) }` does not deref-and-return `r` itself (its body is a single
/// `Call`), so the DIRECT rule (`borrow_escaping_params_of`) misses it — yet
/// passing `&coin` into `wrap` still moves the referent out of the borrow,
/// because `wrap` hands `r` straight to `deref_any`'s already-escaping param.
///
/// - BASE case: `borrow_escaping_params_of(F)` (deref-and-return, per function).
/// - INDUCTIVE case: param `pi` of `F` is escaping if `F` contains a call
///   `G(.., arg_i, ..)` where `arg_i == Local(p)`, `p` is the reference-param
///   local at index `pi` of `F`, and position `i` of the callee `G` is ALREADY
///   in `G`'s escaping set. Callees are matched by MIR function name
///   (monomorphized/mangled: `wrap_i32` calls `deref_any_i32`); a callee's param
///   set is looked up in the growing map, defaulting to empty for unknown/extern
///   callees.
///
/// TERMINATION: the escaping sets only GROW (monotone) and are bounded by the
/// parameter count of each function; a `changed` flag stops the loop once no set
/// grows. Direct recursion (`F` calls `F`) and mutual recursion (`F` <-> `G`)
/// are handled by construction — a recursive call just reads the current set and
/// contributes monotonically, so the fixpoint still converges.
///
/// PRECISION: the inductive step only fires when the arg is EXACTLY the
/// reference parameter local passed into an escaping position. A wrapper that
/// passes `r` into a NON-escaping param (`fn w2(r:&T){ peek(r) }`, `peek` not
/// escaping) contributes nothing, so `w2` stays clean. Read-only field access
/// through the borrow never reaches an escaping callee position either.
fn escaping_params_fixpoint(functions: &[MirFunction]) -> HashMap<Arc<str>, HashSet<usize>> {
    // Seed with the direct (base-case) escaping set for every named function.
    let mut escaping: HashMap<Arc<str>, HashSet<usize>> = functions
        .iter()
        .map(|f| (f.name.clone(), borrow_escaping_params_of(f)))
        .collect();

    // Per-function: the reference-parameter local -> param index map, precomputed
    // once (the call-graph iteration reuses it every round).
    let ref_params: HashMap<Arc<str>, HashMap<LocalId, usize>> = functions
        .iter()
        .map(|f| (f.name.clone(), ref_param_index_of(f)))
        .collect();

    let mut changed = true;
    while changed {
        changed = false;
        for f in functions {
            // A function with no reference parameters can never become escaping
            // via the inductive rule (nothing to leak through a callee).
            let Some(my_ref_params) = ref_params.get(&f.name) else {
                continue;
            };
            if my_ref_params.is_empty() {
                continue;
            }
            let Some(blocks) = &f.blocks else {
                continue;
            };

            // Collect the new escaping indices this round without holding a
            // mutable borrow of `escaping` while reading callee sets.
            let mut newly_escaping: Vec<usize> = Vec::new();
            for block in blocks {
                let Some(MirTerminator::Call {
                    func: callee, args, ..
                }) = &block.terminator
                else {
                    continue;
                };
                match call_target_name(callee) {
                    // DIRECT named call: MY `&`-param escapes iff it is passed
                    // into an ALREADY-escaping position of the resolvable callee.
                    Some(callee_name) => {
                        let Some(callee_escaping) = escaping.get(callee_name) else {
                            continue;
                        };
                        for (i, a) in args.iter().enumerate() {
                            if let MirValue::Local(p) = a {
                                if let Some(&my_idx) = my_ref_params.get(p) {
                                    if callee_escaping.contains(&i) {
                                        newly_escaping.push(my_idx);
                                    }
                                }
                            }
                        }
                    }
                    // INDIRECT call (`call_target_name` is `None`): the callee is
                    // a fn-pointer local, a struct-field fn-pointer, or otherwise
                    // not a resolvable named function, so its body is OPAQUE — we
                    // cannot see whether it derefs-and-escapes the borrow.
                    // Conservative-sound: ANY of MY `&`-params forwarded as an
                    // argument to it is marked borrow-escaping. This over-
                    // approximates (a fn-pointer that does NOT escape is still
                    // flagged), which is acceptable: passing a `&linear` into a
                    // fn-pointer-consuming function is rare, and missing the
                    // escape would be UNSOUND (a double-spend clone). Closes the
                    // higher-order fn-pointer hole (`apply(deref_any, &coin)`).
                    None => {
                        for a in args {
                            if let MirValue::Local(p) = a {
                                if let Some(&my_idx) = my_ref_params.get(p) {
                                    newly_escaping.push(my_idx);
                                }
                            }
                        }
                    }
                }
            }

            if !newly_escaping.is_empty() {
                let my_set = escaping.entry(f.name.clone()).or_default();
                for idx in newly_escaping {
                    if my_set.insert(idx) {
                        changed = true;
                    }
                }
            }
        }
    }

    // Drop empty sets: the call-site rule only consults present entries, and an
    // empty set is indistinguishable from absent. Keeps the context minimal.
    escaping.retain(|_, set| !set.is_empty());
    escaping
}

/// Whether an rvalue is a MOVE-OUT-OF-SHARED-BORROW of a linear referent:
/// a `Deref { ptr }` (or a place with a leading `Deref` projection) whose
/// pointer local is a known shared borrow of a linear value (`shared_refs`,
/// built from borrow provenance). Returns the offending pointer local.
fn move_out_of_shared_borrow(value: &MirRValue, shared_refs: &HashSet<LocalId>) -> Option<LocalId> {
    match value {
        MirRValue::Deref {
            ptr: MirValue::Local(p),
            ..
        } if shared_refs.contains(p) => Some(*p),
        // A `Discriminant`/`Len` of a place with a leading `Deref` projection
        // is a place read through a borrow; a leading `Deref` of a shared
        // linear ref is the same move-out shape.
        MirRValue::Discriminant(place) | MirRValue::Len(place) => {
            if matches!(place.projections.first(), Some(PlaceProjection::Deref))
                && shared_refs.contains(&place.local)
            {
                Some(place.local)
            } else {
                None
            }
        }
        _ => None,
    }
}

/// Whether `dest = value` EXTRACTS a LINEAR value out of a shared borrow via a
/// field / variant-payload / element projection: `dest = base.field` (or
/// `.Variant._i` / `base[i]`) where `base` is a known shared borrow of a linear
/// (`shared_refs`) and `dest` is itself a linear local. Returns the offending
/// borrow local.
///
/// This is the record-/tuple-/slice-pattern-through-`&` move-out. buildlang
/// lowers `match &w { Wrap { coin: c } => .. }` to `c = base.coin` where `base`
/// is the shared-borrowed scrutinee and `c` is linear; extracting the linear
/// field BY VALUE out of a `&` duplicates it (the borrow does not own it).
///
/// PRECISION: guarded on `dest` being a LINEAR local. Reading a NON-linear field
/// through the borrow (`w.value: i64`, or the read-only `peek` case) has a
/// non-linear `dest` and is not flagged, preserving the legal borrow-read. The
/// `VariantField` case here is the through-`&` enum destructure (the base is a
/// shared borrow); the owned-scrutinee `VariantField` move is handled separately
/// by `moved_by_rvalue`.
fn field_extract_of_linear_out_of_borrow(
    func: &MirFunction,
    dest: &LocalId,
    value: &MirRValue,
    shared_refs: &HashSet<LocalId>,
) -> Option<LocalId> {
    if !is_linear_local(func, *dest) {
        return None;
    }
    let base = match value {
        MirRValue::FieldAccess { base, .. }
        | MirRValue::VariantField { base, .. }
        | MirRValue::IndexAccess { base, .. } => base,
        _ => return None,
    };
    match base {
        MirValue::Local(p) if shared_refs.contains(p) => Some(*p),
        _ => None,
    }
}

/// Collect every linear local MOVED by an rvalue in an `Assign`. A move of
/// linear `L` = the rvalue transfers `L`'s value out: `Use(Local(L))`, or `L`
/// as an aggregate operand / cast / repeat operand (by-value composition), or a
/// `VariantField` bind that destructures a payload OUT of `L` (a partial move,
/// tracked conservatively as the whole scrutinee moving — 2c match idiom). A
/// `Ref`/`AddressOf`/`FieldAccess`/`Deref` READS but does not move.
fn moved_by_rvalue(func: &MirFunction, value: &MirRValue, out: &mut Vec<LocalId>) {
    let mut push_if_linear = |v: &MirValue| {
        if let MirValue::Local(l) = v {
            if is_linear_local(func, *l) {
                out.push(*l);
            }
        }
    };
    match value {
        MirRValue::Use(v) => push_if_linear(v),
        // By-value composition consumes its operands. Conservative-sound: a
        // linear placed into an aggregate / repeated / cast by value is moved.
        MirRValue::Aggregate { operands, .. } => operands.iter().for_each(&mut push_if_linear),
        MirRValue::Repeat { value: v, .. } => push_if_linear(v),
        MirRValue::Cast { value: v, .. } => push_if_linear(v),
        // 2c match idiom: `dest = VariantField { base }` binds a variant payload
        // out of `base`. buildlang lowers a `match` arm's payload bind to this,
        // so extracting the payload CONSUMES the scrutinee. Moving one variant
        // field out of an owned scrutinee is a partial move; we conservatively
        // treat the WHOLE `base` as moved (sound: a later use of a DIFFERENT
        // field is over-rejected, but no unsound program passes). This makes the
        // owned-match-payload case a move of the scrutinee; the match-through-`&`
        // case additionally trips the `Deref`-of-shared-borrow rule at the
        // `let s = *r` statement lowering emits before the bind.
        MirRValue::VariantField { base, .. } => push_if_linear(base),
        // Borrows and reads do NOT move.
        MirRValue::Ref { .. }
        | MirRValue::AddressOf { .. }
        | MirRValue::AddressOfGlobal { .. }
        | MirRValue::FieldAccess { .. }
        | MirRValue::IndexAccess { .. }
        | MirRValue::Deref { .. }
        | MirRValue::Discriminant(_)
        | MirRValue::Len(_)
        | MirRValue::BinaryOp { .. }
        | MirRValue::UnaryOp { .. }
        | MirRValue::NullaryOp(..)
        | MirRValue::TextureSample { .. } => {}
    }
}

/// Collect every linear local READ-BUT-NOT-MOVED by an rvalue: a borrow
/// (`Ref`/`AddressOf` whose place base is `L`), or a field / deref / index read
/// of `L`. These do not change move state, but if `L` is already `Moved` they
/// are a borrow-after-move.
fn read_by_rvalue(func: &MirFunction, value: &MirRValue, out: &mut Vec<LocalId>) {
    fn push_place(func: &MirFunction, place: &MirPlace, out: &mut Vec<LocalId>) {
        if is_linear_local(func, place.local) {
            out.push(place.local);
        }
    }
    fn push_val(func: &MirFunction, v: &MirValue, out: &mut Vec<LocalId>) {
        if let MirValue::Local(l) = v {
            if is_linear_local(func, *l) {
                out.push(*l);
            }
        }
    }
    match value {
        MirRValue::Ref { place, .. } | MirRValue::AddressOf { place, .. } => {
            push_place(func, place, out)
        }
        MirRValue::Discriminant(place) | MirRValue::Len(place) => push_place(func, place, out),
        MirRValue::FieldAccess { base, .. } => push_val(func, base, out),
        // `VariantField` is a MOVE of its base (`moved_by_rvalue`), not a
        // non-consuming read, so it is intentionally absent here.
        MirRValue::IndexAccess { base, index, .. } => {
            push_val(func, base, out);
            push_val(func, index, out);
        }
        MirRValue::Deref { ptr, .. } => push_val(func, ptr, out),
        MirRValue::BinaryOp { left, right, .. } => {
            push_val(func, left, out);
            push_val(func, right, out);
        }
        MirRValue::UnaryOp { operand, .. } => push_val(func, operand, out),
        // A plain `Use`/aggregate/cast/repeat/variant-field is a MOVE (handled
        // by `moved_by_rvalue`), not a non-consuming read.
        MirRValue::Use(_)
        | MirRValue::Aggregate { .. }
        | MirRValue::Repeat { .. }
        | MirRValue::Cast { .. }
        | MirRValue::VariantField { .. }
        | MirRValue::NullaryOp(..)
        | MirRValue::AddressOfGlobal { .. }
        | MirRValue::TextureSample { .. } => {}
    }
}

/// Span for the statement at `(block_id, stmt_index)`, or `Span::dummy()`.
fn stmt_span(func: &MirFunction, block_id: u32, stmt_idx: usize) -> Span {
    func.spans
        .stmt
        .get(&(block_id, stmt_idx))
        .copied()
        .unwrap_or_else(Span::dummy)
}

/// Span for the terminator of block `block_id`, or `Span::dummy()`.
fn term_span(func: &MirFunction, block_id: u32) -> Span {
    func.spans
        .terminator
        .get(&block_id)
        .copied()
        .unwrap_or_else(Span::dummy)
}

/// Run the whole-module affine/borrow check. Computes the interprocedural
/// context (borrow-escaping parameters, for class 3) once, then runs the
/// per-function check with it. This is the entry point the compile pipeline
/// uses; `check(func)` remains for direct per-function checks (unit tests) and
/// is exactly `check_module` with an empty context.
pub(crate) fn check_module(functions: &[MirFunction]) -> Vec<TypeErrorWithSpan> {
    // Borrow-escaping parameters, computed as a FIXPOINT over the call graph so
    // a transitive wrapper (`wrap(&coin)` delegating to `deref_any`) is caught,
    // not just the direct deref-and-return shape.
    let ctx = LinearContext {
        borrow_escaping_params: escaping_params_fixpoint(functions),
    };
    functions
        .iter()
        .flat_map(|f| check_with_context(f, &ctx))
        .collect()
}

/// Run the affine/borrow check over one MIR function with no interprocedural
/// context. Pure per-function entry point (unit tests, direct callers). Class 3
/// (borrow-escaping-parameter) detection needs `check_module`; without context
/// this is the 2b behavior plus the local 2c match idiom.
pub(crate) fn check(func: &MirFunction) -> Vec<TypeErrorWithSpan> {
    check_with_context(func, &LinearContext::default())
}

/// Run the affine/borrow check over one MIR function, given the module context.
/// Pure: no mutation, no side effects. Returns every violation with its
/// statement-level span.
///
/// The transfer function walks each block forward from a per-block entry state
/// (the join of predecessor exit states). For every statement/terminator, in
/// order: process READS (borrow-after-move), MOVE-OUT-OF-BORROW, then MOVES
/// (use-after-move + `Init -> Moved`), then the DEFINITION reset (`-> Init`).
/// Iterating to a fixpoint over the CFG lets the maybe-moved join propagate
/// across back-edges and merges. Diagnostics are collected on a final,
/// deterministic forward pass over the converged entry states so each site is
/// reported at most once in stable order.
fn check_with_context(func: &MirFunction, ctx: &LinearContext) -> Vec<TypeErrorWithSpan> {
    let blocks = match &func.blocks {
        Some(b) if !b.is_empty() => b.as_slice(),
        _ => return Vec::new(),
    };

    // If the function has no linear locals at all, nothing to check.
    if !func.locals.iter().any(|l| is_linear_local(func, l.id)) {
        return Vec::new();
    }

    let n = blocks.len();
    let id_to_index = block_id_index(blocks);
    let reachable = reachable_blocks(blocks);

    // Borrow-provenance pre-pass: which pointer locals are shared borrows of a
    // linear value. Computed once; used by move-out-of-shared-borrow detection.
    let shared_refs = shared_linear_ref_set(func);

    // Predecessor lists over reachable blocks only (an unreachable predecessor
    // contributes no real dataflow and would spuriously mark values moved).
    let mut preds: Vec<Vec<usize>> = vec![Vec::new(); n];
    for (i, b) in blocks.iter().enumerate() {
        if !reachable[i] {
            continue;
        }
        for s in terminator_successors(&b.terminator, &id_to_index) {
            if s < n {
                preds[s].push(i);
            }
        }
    }

    // Dataflow fixpoint: `entry[b]` / `exit[b]` are the move/init states at
    // each block boundary. Entry = maybe-moved join of predecessor exits.
    let mut entry: Vec<BlockState> = vec![BlockState::new(); n];
    let mut exit: Vec<BlockState> = vec![BlockState::new(); n];

    let mut changed = true;
    while changed {
        changed = false;
        for i in 0..n {
            if !reachable[i] {
                continue;
            }
            // Join predecessor exits: a local is Moved on entry if Moved on ANY
            // predecessor path (maybe-moved). Absent = Init, so the union takes
            // Moved whenever any predecessor recorded Moved.
            let mut new_entry = BlockState::new();
            for &p in &preds[i] {
                for (&local, &st) in &exit[p] {
                    let slot = new_entry.entry(local).or_insert(MoveState::Init);
                    if st == MoveState::Moved {
                        *slot = MoveState::Moved;
                    }
                }
            }
            if new_entry != entry[i] {
                entry[i] = new_entry;
                changed = true;
            }
            // Transfer through the block (no diagnostics on the fixpoint pass).
            let mut state = entry[i].clone();
            transfer_block(func, ctx, &blocks[i], &shared_refs, &mut state, None);
            if state != exit[i] {
                exit[i] = state;
                changed = true;
            }
        }
    }

    // Final diagnostic pass over the converged entry states, deterministic
    // block order. Only reachable blocks (unreachable code is never executed
    // and its "moves" are not real).
    let mut errors = Vec::new();
    for i in 0..n {
        if !reachable[i] {
            continue;
        }
        let mut state = entry[i].clone();
        transfer_block(
            func,
            ctx,
            &blocks[i],
            &shared_refs,
            &mut state,
            Some(&mut errors),
        );
    }
    errors
}

/// Apply the block's statements and terminator to `state` in order. When
/// `errors` is `Some`, emit diagnostics (use-after-move / borrow-after-move /
/// move-out-of-shared-borrow); when `None`, run silently (fixpoint pass).
fn transfer_block(
    func: &MirFunction,
    ctx: &LinearContext,
    block: &MirBlock,
    shared_refs: &HashSet<LocalId>,
    state: &mut BlockState,
    mut errors: Option<&mut Vec<TypeErrorWithSpan>>,
) {
    let block_id = block.id.0;
    for (idx, stmt) in block.stmts.iter().enumerate() {
        let span = stmt_span(func, block_id, idx);
        apply_stmt(
            func,
            &stmt.kind,
            shared_refs,
            state,
            span,
            errors.as_deref_mut(),
        );
    }
    let span = term_span(func, block_id);
    apply_terminator(
        func,
        ctx,
        &block.terminator,
        shared_refs,
        state,
        span,
        errors.as_deref_mut(),
    );
}

/// Transfer for one statement. Order within a statement:
/// 1. move-out-of-shared-borrow (a distinct illegal SOURCE shape),
/// 2. reads (borrow-after-move) on the still-current state,
/// 3. moves (use-after-move, then `Init -> Moved`),
/// 4. the definition reset (`dest -> Init`), which happens AFTER the rvalue is
///    evaluated (so `L = Use(L)` reads/moves the old L, then rebinds fresh).
fn apply_stmt(
    func: &MirFunction,
    kind: &MirStmtKind,
    shared_refs: &HashSet<LocalId>,
    state: &mut BlockState,
    span: Span,
    mut errors: Option<&mut Vec<TypeErrorWithSpan>>,
) {
    if let MirStmtKind::Assign { dest, value } = kind {
        // (1) move-out-of-shared-borrow: `dest = *shared_ref_to_linear`.
        if let Some(ptr) = move_out_of_shared_borrow(value, shared_refs) {
            if let Some(errs) = errors.as_deref_mut() {
                errs.push(TypeErrorWithSpan::new(
                    TypeError::LinearMoveOutOfBorrow {
                        name: local_name(func, ptr),
                    },
                    span,
                ));
            }
        }

        // (1b) field/variant/index EXTRACT of a LINEAR value out of a shared
        // borrow: `dest = base.field` (or `.Variant._i` / `[i]`) where `base` is
        // a shared borrow of a linear and `dest` is itself a linear local. This
        // is the record-/tuple-/slice-pattern-through-`&` move-out: buildlang
        // lowers `match &w { Wrap { coin: c } => .. }` to `c = (*w_ref).coin`, a
        // by-value extract of the linear `coin` field out of the shared-borrowed
        // `w`. Guarded on `dest` being linear so reading a NON-linear field
        // through the borrow (`w.value: i64`, the legal `peek` case) stays clean.
        if let Some(ptr) = field_extract_of_linear_out_of_borrow(func, dest, value, shared_refs) {
            if let Some(errs) = errors.as_deref_mut() {
                errs.push(TypeErrorWithSpan::new(
                    TypeError::LinearMoveOutOfBorrow {
                        name: local_name(func, ptr),
                    },
                    span,
                ));
            }
        }

        // (2) reads: borrow-after-move on the current state.
        let mut reads = Vec::new();
        read_by_rvalue(func, value, &mut reads);
        for l in reads {
            if state.get(&l) == Some(&MoveState::Moved) {
                if let Some(errs) = errors.as_deref_mut() {
                    errs.push(TypeErrorWithSpan::new(
                        TypeError::LinearBorrowAfterMove {
                            name: local_name(func, l),
                        },
                        span,
                    ));
                }
            }
        }

        // (3) moves: use-after-move, then transition to Moved.
        let mut moves = Vec::new();
        moved_by_rvalue(func, value, &mut moves);
        for l in moves {
            apply_move(func, l, state, span, errors.as_deref_mut());
        }

        // (4) definition reset: the dest is (re)bound to a fresh value.
        if is_linear_local(func, *dest) {
            state.insert(*dest, MoveState::Init);
        }
        return;
    }

    // Non-Assign statements that touch a linear. A store's `value` can either
    // BORROW/READ a linear (borrow-after-move if already `Moved`) or CONSUME a
    // linear BY VALUE (a real move: `Use`/aggregate/cast/repeat operand). The
    // store target (`ptr`/`base`) is itself a borrow-position read of the
    // pointer/base local. We split the two:
    //   - `reads`: borrow-only touches -> borrow-after-move check, NO state
    //     change.
    //   - `moves`: by-value operands consumed into the store -> `apply_move`
    //     (use-after-move + `Init -> Moved`), so a linear moved through a store
    //     is a real consume (a subsequent use is then use-after-move, not a
    //     borrow-after-move).
    // A store target is not a linear rebind we track, so it never resets state.
    let mut reads = Vec::new();
    let mut moves = Vec::new();
    match kind {
        MirStmtKind::DerefAssign { ptr, value } => {
            if is_linear_local(func, *ptr) {
                reads.push(*ptr);
            }
            read_by_rvalue(func, value, &mut reads);
            moved_by_rvalue(func, value, &mut moves);
        }
        MirStmtKind::FieldDerefAssign { ptr, value, .. } => {
            if is_linear_local(func, *ptr) {
                reads.push(*ptr);
            }
            read_by_rvalue(func, value, &mut reads);
            moved_by_rvalue(func, value, &mut moves);
        }
        MirStmtKind::FieldAssign { base, value, .. } => {
            if is_linear_local(func, *base) {
                reads.push(*base);
            }
            read_by_rvalue(func, value, &mut reads);
            moved_by_rvalue(func, value, &mut moves);
        }
        MirStmtKind::GlobalStore { value, .. } => {
            read_by_rvalue(func, value, &mut reads);
            moved_by_rvalue(func, value, &mut moves);
        }
        MirStmtKind::IndexStore {
            base, index, value, ..
        } => {
            // `base[index]` is a borrow of both operands (not a consume), then
            // the value is stored (its rvalue read/moved as usual).
            for v in [base, index] {
                if let MirValue::Local(l) = v {
                    if is_linear_local(func, *l) {
                        reads.push(*l);
                    }
                }
            }
            read_by_rvalue(func, value, &mut reads);
            moved_by_rvalue(func, value, &mut moves);
        }
        // A workgroup barrier reads and moves no locals, so like the other
        // non-store statements it contributes nothing to the linear state.
        MirStmtKind::Assign { .. }
        | MirStmtKind::StorageLive(_)
        | MirStmtKind::StorageDead(_)
        | MirStmtKind::Nop
        | MirStmtKind::WorkgroupBarrier => {}
    }
    // Borrow-after-move on the current (pre-move) state for borrow-only touches.
    for l in reads {
        if state.get(&l) == Some(&MoveState::Moved) {
            if let Some(errs) = errors.as_deref_mut() {
                errs.push(TypeErrorWithSpan::new(
                    TypeError::LinearBorrowAfterMove {
                        name: local_name(func, l),
                    },
                    span,
                ));
            }
        }
    }
    // Then apply the by-value consumes (use-after-move + `Init -> Moved`).
    for l in moves {
        apply_move(func, l, state, span, errors.as_deref_mut());
    }
}

/// Transfer for a terminator. `Call` args passed BY VALUE move their linear
/// operands; `Return(Some(Local(L)))` moves `L`; the `Call` dest (re)binds.
/// `If`/`Switch`/`Assert` conditions READ their linear operand.
///
/// Class 3 (interprocedural, needs `ctx`): if an arg is a shared borrow of a
/// linear (`p in shared_refs`) and the callee's corresponding parameter is
/// BORROW-ESCAPING (it derefs-and-returns the referent), the call moves the
/// linear OUT of the shared borrow -> `LinearMoveOutOfBorrow`.
fn apply_terminator(
    func: &MirFunction,
    ctx: &LinearContext,
    term: &Option<MirTerminator>,
    shared_refs: &HashSet<LocalId>,
    state: &mut BlockState,
    span: Span,
    mut errors: Option<&mut Vec<TypeErrorWithSpan>>,
) {
    match term {
        Some(MirTerminator::Call {
            args,
            dest,
            func: callee,
            ..
        }) => {
            // Class 3: a shared borrow of a linear passed into a borrow-escaping
            // parameter is moved out of the borrow by the callee.
            if let Some(name) = call_target_name(callee) {
                if let Some(escaping) = ctx.borrow_escaping_params.get(name) {
                    for (i, a) in args.iter().enumerate() {
                        if let MirValue::Local(p) = a {
                            if escaping.contains(&i) && shared_refs.contains(p) {
                                if let Some(errs) = errors.as_deref_mut() {
                                    errs.push(TypeErrorWithSpan::new(
                                        TypeError::LinearMoveOutOfBorrow {
                                            name: local_name(func, *p),
                                        },
                                        span,
                                    ));
                                }
                            }
                        }
                    }
                }
            }
            // The callee value itself is never a linear by-value move (it is a
            // function/global reference), but guard defensively for a Local.
            if let MirValue::Local(l) = callee {
                if is_linear_local(func, *l) {
                    apply_move(func, *l, state, span, errors.as_deref_mut());
                }
            }
            for a in args {
                if let MirValue::Local(l) = a {
                    if is_linear_local(func, *l) {
                        apply_move(func, *l, state, span, errors.as_deref_mut());
                    }
                }
            }
            // The call's dest (re)binds a fresh value.
            if let Some(d) = dest {
                if is_linear_local(func, *d) {
                    state.insert(*d, MoveState::Init);
                }
            }
        }
        Some(MirTerminator::Return(Some(MirValue::Local(l)))) => {
            if is_linear_local(func, *l) {
                apply_move(func, *l, state, span, errors.as_deref_mut());
            }
        }
        Some(MirTerminator::If {
            cond: MirValue::Local(l),
            ..
        })
        | Some(MirTerminator::Switch {
            value: MirValue::Local(l),
            ..
        })
        | Some(MirTerminator::Assert {
            cond: MirValue::Local(l),
            ..
        }) => {
            // A condition READS its operand: borrow-after-move if moved.
            if is_linear_local(func, *l) && state.get(l) == Some(&MoveState::Moved) {
                if let Some(errs) = errors.as_deref_mut() {
                    errs.push(TypeErrorWithSpan::new(
                        TypeError::LinearBorrowAfterMove {
                            name: local_name(func, *l),
                        },
                        span,
                    ));
                }
            }
        }
        _ => {}
    }
}

/// Apply a single MOVE of linear `l`: if `l` is already `Moved`, emit
/// use-after-move; then set `l -> Moved`.
fn apply_move(
    func: &MirFunction,
    l: LocalId,
    state: &mut BlockState,
    span: Span,
    errors: Option<&mut Vec<TypeErrorWithSpan>>,
) {
    if state.get(&l) == Some(&MoveState::Moved) {
        if let Some(errs) = errors {
            errs.push(TypeErrorWithSpan::new(
                TypeError::LinearUseAfterMove {
                    name: local_name(func, l),
                },
                span,
            ));
        }
    }
    state.insert(l, MoveState::Moved);
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::codegen::ir::{
        BlockId, LocalId, MirBlock, MirConst, MirFnSig, MirFunction, MirLocal, MirRValue, MirStmt,
        MirTerminator, MirType, MirValue,
    };
    use std::sync::Arc;

    /// A `#[linear]` local of struct type `Qubit`.
    fn linear_local(id: u32, name: &str) -> MirLocal {
        MirLocal {
            id: LocalId(id),
            name: Some(Arc::from(name)),
            ty: MirType::Struct(Arc::from("Qubit")),
            is_mut: false,
            is_param: false,
            annotations: vec![Arc::from("linear")],
        }
    }

    /// A plain (non-linear) i64 local.
    fn i64_local(id: u32, name: &str) -> MirLocal {
        MirLocal {
            id: LocalId(id),
            name: Some(Arc::from(name)),
            ty: MirType::i64(),
            is_mut: false,
            is_param: false,
            annotations: Vec::new(),
        }
    }

    /// An UNTAGGED `&Qubit` pointer local: type `Ptr(Struct("Qubit"))`, EMPTY
    /// annotations, exactly as real 2a lowering produces for a `&coin` binding
    /// (2a tags only linear-ADT `Struct` locals, never a reference-to-linear).
    /// The move-out-of-shared-borrow detector must recover the borrow from
    /// provenance (`r = &coin`), not from any marker on `r`.
    fn untagged_ptr_local(id: u32, name: &str) -> MirLocal {
        MirLocal {
            id: LocalId(id),
            name: Some(Arc::from(name)),
            ty: MirType::Ptr(Box::new(MirType::Struct(Arc::from("Qubit")))),
            is_mut: false,
            is_param: false,
            annotations: Vec::new(),
        }
    }

    fn count(errors: &[TypeErrorWithSpan], pred: impl Fn(&TypeError) -> bool) -> usize {
        errors.iter().filter(|e| pred(&e.error)).count()
    }

    /// A `#[linear]` local named `_N` (anonymous), struct type `Qubit`. Used for
    /// the compiler-generated scrutinee copies a `match` produces.
    fn linear_anon(id: u32) -> MirLocal {
        MirLocal {
            id: LocalId(id),
            name: None,
            ty: MirType::Struct(Arc::from("Qubit")),
            is_mut: false,
            is_param: false,
            annotations: vec![Arc::from("linear")],
        }
    }

    /// A by-REFERENCE parameter local (`is_param`, `Ptr(Struct("Qubit"))`, empty
    /// annotations) — the `r: &T` a monomorphized generic like `deref_any`
    /// receives. Param index == its LocalId (builder invariant).
    fn ref_param_local(id: u32, name: &str) -> MirLocal {
        MirLocal {
            id: LocalId(id),
            name: Some(Arc::from(name)),
            ty: MirType::Ptr(Box::new(MirType::Struct(Arc::from("Qubit")))),
            is_mut: false,
            is_param: true,
            annotations: Vec::new(),
        }
    }

    /// A `VariantField` bind of variant `Full` field 0 out of `base`.
    fn variant_field(base: LocalId) -> MirRValue {
        MirRValue::VariantField {
            base: MirValue::Local(base),
            variant_name: Arc::from("Full"),
            field_index: 0,
            field_ty: MirType::Struct(Arc::from("Coin")),
        }
    }

    // 1. Linear moved twice in a single straight-line block -> exactly one
    //    LinearUseAfterMove (the second move; the first is legal).
    //    bb0: _1 = Use(_0) ; _2 = Use(_0) ; return
    #[test]
    fn double_move_reports_use_after_move() {
        let mut func = MirFunction::new("f", MirFnSig::new(vec![], MirType::Void));
        func.locals.push(linear_local(0, "q"));
        func.locals.push(i64_local(1, "_1"));
        func.locals.push(i64_local(2, "_2"));
        let mut b0 = MirBlock::new(BlockId(0));
        b0.stmts.push(MirStmt::assign(
            LocalId(1),
            MirRValue::Use(MirValue::Local(LocalId(0))),
        ));
        b0.stmts.push(MirStmt::assign(
            LocalId(2),
            MirRValue::Use(MirValue::Local(LocalId(0))),
        ));
        b0.terminator = Some(MirTerminator::Return(None));
        func.blocks = Some(vec![b0]);

        let errors = check(&func);
        assert_eq!(
            count(&errors, |e| matches!(
                e,
                TypeError::LinearUseAfterMove { .. }
            )),
            1,
            "exactly one use-after-move (the second consume of `q`): {errors:?}"
        );
        assert_eq!(errors.len(), 1, "no other diagnostics: {errors:?}");
    }

    // 2. Linear moved, then borrowed (`Ref`) -> LinearBorrowAfterMove.
    //    bb0: _1 = Use(_0) ; _2 = Ref(_0) ; return
    #[test]
    fn move_then_borrow_reports_borrow_after_move() {
        let mut func = MirFunction::new("f", MirFnSig::new(vec![], MirType::Void));
        func.locals.push(linear_local(0, "q"));
        func.locals.push(i64_local(1, "_1"));
        func.locals.push(i64_local(2, "_2"));
        let mut b0 = MirBlock::new(BlockId(0));
        b0.stmts.push(MirStmt::assign(
            LocalId(1),
            MirRValue::Use(MirValue::Local(LocalId(0))),
        ));
        b0.stmts.push(MirStmt::assign(
            LocalId(2),
            MirRValue::Ref {
                is_mut: false,
                place: MirPlace::local(LocalId(0)),
            },
        ));
        b0.terminator = Some(MirTerminator::Return(None));
        func.blocks = Some(vec![b0]);

        let errors = check(&func);
        assert_eq!(
            count(&errors, |e| matches!(
                e,
                TypeError::LinearBorrowAfterMove { .. }
            )),
            1,
            "the post-move borrow must be flagged: {errors:?}"
        );
        assert_eq!(
            count(&errors, |e| matches!(
                e,
                TypeError::LinearUseAfterMove { .. }
            )),
            0,
            "the borrow is not a second move: {errors:?}"
        );
    }

    // 3. Linear moved out of `*r` where `r` is a REAL shared `&Qubit` (empty
    //    annotations, as 2a lowering produces) -> LinearMoveOutOfBorrow. The
    //    detector keys off the borrow provenance `r = &coin`, not a marker on
    //    `r` (which real lowering never stamps).
    //    bb0: _1 = &_0 (shared) ; _2 = Deref(_1) ; return   (_0 linear)
    #[test]
    fn move_out_of_shared_borrow_reports() {
        let mut func = MirFunction::new("f", MirFnSig::new(vec![], MirType::Void));
        func.locals.push(linear_local(0, "coin"));
        // `r`: a real `&Qubit` binding -> Ptr(Struct), NO "linear" annotation.
        func.locals.push(untagged_ptr_local(1, "r"));
        func.locals.push(i64_local(2, "_2"));
        let mut b0 = MirBlock::new(BlockId(0));
        // r = &coin  (shared borrow of a linear local -> provenance set)
        b0.stmts.push(MirStmt::assign(
            LocalId(1),
            MirRValue::Ref {
                is_mut: false,
                place: MirPlace::local(LocalId(0)),
            },
        ));
        // _2 = *r  (move out of the shared borrow)
        b0.stmts.push(MirStmt::assign(
            LocalId(2),
            MirRValue::Deref {
                ptr: MirValue::Local(LocalId(1)),
                pointee_ty: MirType::Struct(Arc::from("Qubit")),
            },
        ));
        b0.terminator = Some(MirTerminator::Return(None));
        func.blocks = Some(vec![b0]);

        let errors = check(&func);
        assert_eq!(
            count(&errors, |e| matches!(
                e,
                TypeError::LinearMoveOutOfBorrow { .. }
            )),
            1,
            "moving `*r` out of a real shared borrow must be flagged: {errors:?}"
        );
    }

    // 4. Linear moved ONCE -> no errors.
    //    bb0: _1 = Use(_0) ; return
    #[test]
    fn single_move_is_clean() {
        let mut func = MirFunction::new("f", MirFnSig::new(vec![], MirType::Void));
        func.locals.push(linear_local(0, "q"));
        func.locals.push(i64_local(1, "_1"));
        let mut b0 = MirBlock::new(BlockId(0));
        b0.stmts.push(MirStmt::assign(
            LocalId(1),
            MirRValue::Use(MirValue::Local(LocalId(0))),
        ));
        b0.terminator = Some(MirTerminator::Return(None));
        func.blocks = Some(vec![b0]);

        let errors = check(&func);
        assert!(errors.is_empty(), "a single move is legal: {errors:?}");
    }

    // 5. Linear moved, then re-bound, then used again -> no errors (the redef
    //    resets to Init, so the second use consumes a fresh value).
    //    bb0: _1 = Use(_0) ; _0 = Call(make) ; _2 = Use(_0) ; return
    #[test]
    fn move_then_rebind_then_use_is_clean() {
        let mut func = MirFunction::new("f", MirFnSig::new(vec![], MirType::Void));
        func.locals.push(linear_local(0, "q"));
        func.locals.push(i64_local(1, "_1"));
        func.locals.push(i64_local(2, "_2"));
        let mut b0 = MirBlock::new(BlockId(0));
        // move _0 out
        b0.stmts.push(MirStmt::assign(
            LocalId(1),
            MirRValue::Use(MirValue::Local(LocalId(0))),
        ));
        // re-bind _0 via a call dest (fresh value)
        b0.terminator = Some(MirTerminator::Call {
            func: MirValue::Function(Arc::from("make_qubit")),
            args: Vec::new(),
            dest: Some(LocalId(0)),
            target: Some(BlockId(1)),
            unwind: None,
        });
        // use the fresh _0 once
        let mut b1 = MirBlock::new(BlockId(1));
        b1.stmts.push(MirStmt::assign(
            LocalId(2),
            MirRValue::Use(MirValue::Local(LocalId(0))),
        ));
        b1.terminator = Some(MirTerminator::Return(None));
        func.blocks = Some(vec![b0, b1]);

        let errors = check(&func);
        assert!(
            errors.is_empty(),
            "re-binding resets move state; the later use is legal: {errors:?}"
        );
    }

    // 6. Branch: one arm moves _0, both arms join, then _0 moved after the join
    //    -> LinearUseAfterMove (maybe-moved join treats it as moved).
    //    bb0: if cond -> bb1 else bb2
    //    bb1: _1 = Use(_0) -> bb3       (moves on this arm)
    //    bb2: -> bb3                    (does not move)
    //    bb3: _2 = Use(_0) ; return     (move after join)
    #[test]
    fn maybe_moved_join_reports_use_after_move() {
        let mut func = MirFunction::new("f", MirFnSig::new(vec![], MirType::Void));
        func.locals.push(linear_local(0, "q"));
        func.locals.push(i64_local(1, "_1"));
        func.locals.push(i64_local(2, "_2"));
        func.locals.push(i64_local(9, "cond"));

        let mut b0 = MirBlock::new(BlockId(0));
        b0.terminator = Some(MirTerminator::If {
            cond: MirValue::Local(LocalId(9)),
            then_block: BlockId(1),
            else_block: BlockId(2),
        });
        let mut b1 = MirBlock::new(BlockId(1));
        b1.stmts.push(MirStmt::assign(
            LocalId(1),
            MirRValue::Use(MirValue::Local(LocalId(0))),
        ));
        b1.terminator = Some(MirTerminator::Goto(BlockId(3)));
        let mut b2 = MirBlock::new(BlockId(2));
        b2.terminator = Some(MirTerminator::Goto(BlockId(3)));
        let mut b3 = MirBlock::new(BlockId(3));
        b3.stmts.push(MirStmt::assign(
            LocalId(2),
            MirRValue::Use(MirValue::Local(LocalId(0))),
        ));
        b3.terminator = Some(MirTerminator::Return(None));
        func.blocks = Some(vec![b0, b1, b2, b3]);

        let errors = check(&func);
        assert_eq!(
            count(&errors, |e| matches!(
                e,
                TypeError::LinearUseAfterMove { .. }
            )),
            1,
            "a value maybe-moved on one arm is moved after the join: {errors:?}"
        );
    }

    // 7. Loop (back-edge) that re-binds _0 each iteration and moves it once per
    //    iteration -> no errors. The redef at the loop head resets state before
    //    the per-iteration move.
    //    bb0: _0 = Call(make) -> bb1
    //    bb1(header): if cond -> bb2 else bb3
    //    bb2(body): _1 = Use(_0) ; _0 = Call(make) -> bb1    (move then rebind)
    //    bb3: return
    #[test]
    fn loop_rebind_and_move_each_iteration_is_clean() {
        let mut func = MirFunction::new("f", MirFnSig::new(vec![], MirType::Void));
        func.locals.push(linear_local(0, "q"));
        func.locals.push(i64_local(1, "_1"));
        func.locals.push(i64_local(9, "cond"));

        let mut b0 = MirBlock::new(BlockId(0));
        b0.terminator = Some(MirTerminator::Call {
            func: MirValue::Function(Arc::from("make_qubit")),
            args: Vec::new(),
            dest: Some(LocalId(0)),
            target: Some(BlockId(1)),
            unwind: None,
        });
        let mut b1 = MirBlock::new(BlockId(1));
        b1.terminator = Some(MirTerminator::If {
            cond: MirValue::Local(LocalId(9)),
            then_block: BlockId(2),
            else_block: BlockId(3),
        });
        let mut b2 = MirBlock::new(BlockId(2));
        // move _0 out
        b2.stmts.push(MirStmt::assign(
            LocalId(1),
            MirRValue::Use(MirValue::Local(LocalId(0))),
        ));
        // rebind _0 (fresh) and loop back
        b2.terminator = Some(MirTerminator::Call {
            func: MirValue::Function(Arc::from("make_qubit")),
            args: Vec::new(),
            dest: Some(LocalId(0)),
            target: Some(BlockId(1)),
            unwind: None,
        });
        let mut b3 = MirBlock::new(BlockId(3));
        b3.terminator = Some(MirTerminator::Return(None));
        func.blocks = Some(vec![b0, b1, b2, b3]);

        let errors = check(&func);
        assert!(
            errors.is_empty(),
            "each iteration rebinds then moves once; no double-consume: {errors:?}"
        );
    }

    // 8. A NON-linear local moved twice -> no errors (only linear locals are
    //    checked).
    //    bb0: _1 = Use(_0) ; _2 = Use(_0) ; return   (_0 is NOT linear)
    #[test]
    fn non_linear_double_move_is_ignored() {
        let mut func = MirFunction::new("f", MirFnSig::new(vec![], MirType::Void));
        func.locals.push(i64_local(0, "x")); // NOT linear
        func.locals.push(i64_local(1, "_1"));
        func.locals.push(i64_local(2, "_2"));
        let mut b0 = MirBlock::new(BlockId(0));
        b0.stmts.push(MirStmt::assign(
            LocalId(1),
            MirRValue::Use(MirValue::Local(LocalId(0))),
        ));
        b0.stmts.push(MirStmt::assign(
            LocalId(2),
            MirRValue::Use(MirValue::Local(LocalId(0))),
        ));
        b0.terminator = Some(MirTerminator::Return(None));
        func.blocks = Some(vec![b0]);

        let errors = check(&func);
        assert!(
            errors.is_empty(),
            "non-linear locals are not move-checked: {errors:?}"
        );
    }

    // 9. Linear moved BY VALUE into a store statement (`FieldAssign`), then used
    //    again -> exactly one LinearUseAfterMove. The store consumes the linear,
    //    so the later `Use` is a double-consume, NOT a borrow-after-move.
    //    bb0: _1.field = Use(_0) ; _2 = Use(_0) ; return   (_0 linear)
    #[test]
    fn store_by_value_move_is_consumed() {
        let mut func = MirFunction::new("f", MirFnSig::new(vec![], MirType::Void));
        func.locals.push(linear_local(0, "coin"));
        func.locals.push(i64_local(1, "obj"));
        func.locals.push(i64_local(2, "_2"));
        let mut b0 = MirBlock::new(BlockId(0));
        // obj.field = coin  (FieldAssign; value = Use(coin): consumes `coin`)
        b0.stmts.push(MirStmt::new(MirStmtKind::FieldAssign {
            base: LocalId(1),
            field_name: Arc::from("field"),
            value: MirRValue::Use(MirValue::Local(LocalId(0))),
        }));
        // _2 = Use(coin)  (coin already Moved -> use-after-move)
        b0.stmts.push(MirStmt::assign(
            LocalId(2),
            MirRValue::Use(MirValue::Local(LocalId(0))),
        ));
        b0.terminator = Some(MirTerminator::Return(None));
        func.blocks = Some(vec![b0]);

        let errors = check(&func);
        assert_eq!(
            count(&errors, |e| matches!(
                e,
                TypeError::LinearUseAfterMove { .. }
            )),
            1,
            "the store consumes `coin`; the later use is a use-after-move: {errors:?}"
        );
        assert_eq!(
            count(&errors, |e| matches!(
                e,
                TypeError::LinearBorrowAfterMove { .. }
            )),
            0,
            "the store is a by-value move, not a borrow-after-move: {errors:?}"
        );
        assert_eq!(errors.len(), 1, "no other diagnostics: {errors:?}");
    }

    // 10. Negative: a `&mut` borrow of a linear, then a deref-move, must NOT emit
    //     LinearMoveOutOfBorrow. Mutable-borrow move-out is a different (allowed
    //     for now) case; only SHARED borrows are the categorical violation.
    //     bb0: r = &mut coin ; a = Deref(r) ; return
    #[test]
    fn move_out_of_mut_borrow_is_not_flagged() {
        let mut func = MirFunction::new("f", MirFnSig::new(vec![], MirType::Void));
        func.locals.push(linear_local(0, "coin"));
        func.locals.push(untagged_ptr_local(1, "r"));
        func.locals.push(i64_local(2, "a"));
        let mut b0 = MirBlock::new(BlockId(0));
        // r = &mut coin  (mutable borrow: not the shared move-out shape)
        b0.stmts.push(MirStmt::assign(
            LocalId(1),
            MirRValue::Ref {
                is_mut: true,
                place: MirPlace::local(LocalId(0)),
            },
        ));
        // a = *r
        b0.stmts.push(MirStmt::assign(
            LocalId(2),
            MirRValue::Deref {
                ptr: MirValue::Local(LocalId(1)),
                pointee_ty: MirType::Struct(Arc::from("Coin")),
            },
        ));
        b0.terminator = Some(MirTerminator::Return(None));
        func.blocks = Some(vec![b0]);

        let errors = check(&func);
        assert_eq!(
            count(&errors, |e| matches!(
                e,
                TypeError::LinearMoveOutOfBorrow { .. }
            )),
            0,
            "a `&mut` deref-move is not a shared move-out-of-borrow: {errors:?}"
        );
    }

    // ===================================================================
    // 2c: match idiom, class 1/3, and precision.
    // ===================================================================

    // 11. MATCH IDIOM (owned): a `VariantField` bind out of an OWNED linear
    //     scrutinee copy MOVES the scrutinee; a later use of the scrutinee is a
    //     use-after-move. This is the `match w { Full(c) => ... }` shape: the
    //     scrutinee `_4 = Use(w)` is copied, then `c = VariantField(_4)` extracts
    //     the payload (consuming `_4`), and a second `VariantField(_4)` is a
    //     double-consume.
    //     bb0: _1 = Use(_0) ; _2 = VariantField(_1) ; _3 = VariantField(_1) ; ret
    #[test]
    fn owned_variant_field_bind_moves_scrutinee() {
        let mut func = MirFunction::new("f", MirFnSig::new(vec![], MirType::Void));
        func.locals.push(linear_local(0, "w")); // owned linear scrutinee
        func.locals.push(linear_anon(1)); // scrutinee copy `_1 = Use(w)`
        func.locals.push(i64_local(2, "c1"));
        func.locals.push(i64_local(3, "c2"));
        let mut b0 = MirBlock::new(BlockId(0));
        b0.stmts.push(MirStmt::assign(
            LocalId(1),
            MirRValue::Use(MirValue::Local(LocalId(0))),
        ));
        // first payload bind out of the scrutinee copy (legal: moves _1 once)
        b0.stmts
            .push(MirStmt::assign(LocalId(2), variant_field(LocalId(1))));
        // second payload bind out of the SAME scrutinee copy -> use-after-move
        b0.stmts
            .push(MirStmt::assign(LocalId(3), variant_field(LocalId(1))));
        b0.terminator = Some(MirTerminator::Return(None));
        func.blocks = Some(vec![b0]);

        let errors = check(&func);
        assert_eq!(
            count(&errors, |e| matches!(
                e,
                TypeError::LinearUseAfterMove { .. }
            )),
            1,
            "the second VariantField bind double-consumes the scrutinee: {errors:?}"
        );
    }

    // 12. MATCH IDIOM (owned) is CLEAN when the payload is bound exactly once.
    //     A single `VariantField` bind is a legal (first) move of the scrutinee.
    //     bb0: _1 = Use(_0) ; _2 = VariantField(_1) ; return
    #[test]
    fn owned_variant_field_bind_once_is_clean() {
        let mut func = MirFunction::new("f", MirFnSig::new(vec![], MirType::Void));
        func.locals.push(linear_local(0, "w"));
        func.locals.push(linear_anon(1));
        func.locals.push(i64_local(2, "c"));
        let mut b0 = MirBlock::new(BlockId(0));
        b0.stmts.push(MirStmt::assign(
            LocalId(1),
            MirRValue::Use(MirValue::Local(LocalId(0))),
        ));
        b0.stmts
            .push(MirStmt::assign(LocalId(2), variant_field(LocalId(1))));
        b0.terminator = Some(MirTerminator::Return(None));
        func.blocks = Some(vec![b0]);

        let errors = check(&func);
        assert!(
            errors.is_empty(),
            "a single payload bind is a legal move of the scrutinee: {errors:?}"
        );
    }

    // 13. MATCH-THROUGH-`&` (class 1 match form): buildlang lowers `match &w`
    //     (linear enum) to `r = &w ; s = *r ; ... VariantField(s)`. The `s = *r`
    //     Deref of a shared borrow of a linear is the move-out-of-shared-borrow,
    //     flagged at that statement — even before the VariantField bind. This is
    //     the exact MIR the compiler emits (see the class1 repro).
    //     bb0: r = &coin ; s = Deref(r) ; c = VariantField(s) ; return
    #[test]
    fn match_through_shared_borrow_reports_move_out_of_borrow() {
        let mut func = MirFunction::new("f", MirFnSig::new(vec![], MirType::Void));
        func.locals.push(linear_local(0, "w")); // linear enum scrutinee
        func.locals.push(untagged_ptr_local(1, "r")); // r = &w
        func.locals.push(linear_anon(2)); // s = *r (derefed copy)
        func.locals.push(i64_local(3, "c"));
        let mut b0 = MirBlock::new(BlockId(0));
        // r = &w (shared borrow of a linear -> provenance set)
        b0.stmts.push(MirStmt::assign(
            LocalId(1),
            MirRValue::Ref {
                is_mut: false,
                place: MirPlace::local(LocalId(0)),
            },
        ));
        // s = *r (move out of the shared borrow)
        b0.stmts.push(MirStmt::assign(
            LocalId(2),
            MirRValue::Deref {
                ptr: MirValue::Local(LocalId(1)),
                pointee_ty: MirType::Struct(Arc::from("Qubit")),
            },
        ));
        // c = VariantField(s) (payload bind out of the derefed copy)
        b0.stmts
            .push(MirStmt::assign(LocalId(3), variant_field(LocalId(2))));
        b0.terminator = Some(MirTerminator::Return(None));
        func.blocks = Some(vec![b0]);

        let errors = check(&func);
        assert_eq!(
            count(&errors, |e| matches!(
                e,
                TypeError::LinearMoveOutOfBorrow { .. }
            )),
            1,
            "matching a linear through a shared borrow moves it out of the borrow: {errors:?}"
        );
    }

    // 14. CLASS 3 (interprocedural): passing a shared borrow of a linear into a
    //     BORROW-ESCAPING parameter (a callee that derefs-and-returns its `&`
    //     param) moves the referent out of the borrow.
    //     callee `deref_any`: r:&Qubit (param0) ; t = *r ; return t
    //     caller `main`: r = &coin ; deref_any(r) -> LinearMoveOutOfBorrow
    #[test]
    fn class3_pass_shared_borrow_into_borrow_escaping_param_reports() {
        // Callee: fn deref_any(r: &Qubit) -> Qubit { *r }
        let mut callee = MirFunction::new("deref_any", MirFnSig::new(vec![], MirType::Void));
        callee.locals.push(ref_param_local(0, "r")); // param 0: &Qubit
        callee.locals.push(linear_anon(1)); // t = *r
        let mut cb0 = MirBlock::new(BlockId(0));
        cb0.stmts.push(MirStmt::assign(
            LocalId(1),
            MirRValue::Deref {
                ptr: MirValue::Local(LocalId(0)),
                pointee_ty: MirType::Struct(Arc::from("Qubit")),
            },
        ));
        cb0.terminator = Some(MirTerminator::Return(Some(MirValue::Local(LocalId(1)))));
        callee.blocks = Some(vec![cb0]);

        // Caller: r = &coin ; deref_any(r)
        let mut caller = MirFunction::new("main", MirFnSig::new(vec![], MirType::Void));
        caller.locals.push(linear_local(0, "coin"));
        caller.locals.push(untagged_ptr_local(1, "r")); // r = &coin
        caller.locals.push(linear_anon(2)); // call dest
        let mut mb0 = MirBlock::new(BlockId(0));
        mb0.stmts.push(MirStmt::assign(
            LocalId(1),
            MirRValue::Ref {
                is_mut: false,
                place: MirPlace::local(LocalId(0)),
            },
        ));
        mb0.terminator = Some(MirTerminator::Call {
            func: MirValue::Function(Arc::from("deref_any")),
            args: vec![MirValue::Local(LocalId(1))],
            dest: Some(LocalId(2)),
            target: Some(BlockId(1)),
            unwind: None,
        });
        let mut mb1 = MirBlock::new(BlockId(1));
        mb1.terminator = Some(MirTerminator::Return(None));
        caller.blocks = Some(vec![mb0, mb1]);

        let errors = check_module(&[callee, caller]);
        assert_eq!(
            count(&errors, |e| matches!(
                e,
                TypeError::LinearMoveOutOfBorrow { .. }
            )),
            1,
            "passing `&coin` into a deref-and-return param moves it out of the borrow: {errors:?}"
        );
    }

    // 15. CLASS 3 negative: a callee that only READS through its `&` parameter
    //     (never derefs-and-returns it) is NOT borrow-escaping, so passing
    //     `&coin` to it is the LEGAL borrow-read case and is not flagged.
    //     callee `peek`: r:&Qubit (param0) ; return 0
    //     caller: r = &coin ; peek(r) -> clean
    #[test]
    fn class3_read_only_borrow_param_is_clean() {
        // Callee: fn peek(r: &Qubit) -> i64 { 0 }  (never derefs r)
        let mut callee = MirFunction::new("peek", MirFnSig::new(vec![], MirType::Void));
        callee.locals.push(ref_param_local(0, "r"));
        let mut cb0 = MirBlock::new(BlockId(0));
        cb0.terminator = Some(MirTerminator::Return(Some(MirValue::Const(MirConst::Int(
            0,
            MirType::i64(),
        )))));
        callee.blocks = Some(vec![cb0]);

        // Caller: r = &coin ; peek(r)
        let mut caller = MirFunction::new("main", MirFnSig::new(vec![], MirType::Void));
        caller.locals.push(linear_local(0, "coin"));
        caller.locals.push(untagged_ptr_local(1, "r"));
        caller.locals.push(i64_local(2, "res"));
        let mut mb0 = MirBlock::new(BlockId(0));
        mb0.stmts.push(MirStmt::assign(
            LocalId(1),
            MirRValue::Ref {
                is_mut: false,
                place: MirPlace::local(LocalId(0)),
            },
        ));
        mb0.terminator = Some(MirTerminator::Call {
            func: MirValue::Global(Arc::from("peek")),
            args: vec![MirValue::Local(LocalId(1))],
            dest: Some(LocalId(2)),
            target: Some(BlockId(1)),
            unwind: None,
        });
        let mut mb1 = MirBlock::new(BlockId(1));
        mb1.terminator = Some(MirTerminator::Return(None));
        caller.blocks = Some(vec![mb0, mb1]);

        let errors = check_module(&[callee, caller]);
        assert!(
            errors.is_empty(),
            "a read-only borrow parameter is the legal borrow case; not flagged: {errors:?}"
        );
    }

    // ===================================================================
    // 2c PRECISION (the payoff): safe compositional shapes the name-keyed AST
    // tracker over-rejects must CHECK CLEAN on the MIR checker (zero errors).
    // The AST tracker is still active at the CLI (2d removes it), so these are
    // proven here directly against `check`.
    // ===================================================================

    // 16. SAFE: a linear in a TUPLE, used once.
    //     `let t = (coin, 7) ; spend(t.0)` lowers to a tuple Aggregate then a
    //     field read + move of the linear operand. Consumed exactly once.
    //     bb0: _1 = coin ; _2 = Aggregate(_1, 7) ; call spend(_1) ; return
    #[test]
    fn precision_linear_in_tuple_used_once_is_clean() {
        let mut func = MirFunction::new("f", MirFnSig::new(vec![], MirType::Void));
        func.locals.push(linear_local(0, "coin"));
        func.locals.push(i64_local(1, "seven"));
        func.locals.push(i64_local(2, "tuple")); // the tuple aggregate
        let mut b0 = MirBlock::new(BlockId(0));
        // t = (coin, 7): the linear operand is moved into the tuple once.
        b0.stmts.push(MirStmt::assign(
            LocalId(2),
            MirRValue::Aggregate {
                kind: crate::codegen::ir::AggregateKind::Tuple,
                operands: vec![MirValue::Local(LocalId(0)), MirValue::Local(LocalId(1))],
            },
        ));
        b0.terminator = Some(MirTerminator::Return(None));
        func.blocks = Some(vec![b0]);

        let errors = check(&func);
        assert!(
            errors.is_empty(),
            "a linear moved into a tuple exactly once is safe: {errors:?}"
        );
    }

    // 17. SAFE: `Option<Linear>` / `Ok(q)` constructed and consumed once.
    //     `let o = Some(coin) ; consume(o)` lowers to an enum-variant Aggregate
    //     (moving the linear in once) then a by-value consume of the option.
    //     bb0: _1 = coin ; _2 = Aggregate::Variant(Some, _1) ; call consume(_2)
    #[test]
    fn precision_option_of_linear_once_is_clean() {
        let mut func = MirFunction::new("f", MirFnSig::new(vec![], MirType::Void));
        func.locals.push(linear_local(0, "coin"));
        func.locals.push(linear_anon(1)); // the Option<Coin> value (still linear)
        func.locals.push(i64_local(2, "res"));
        let mut b0 = MirBlock::new(BlockId(0));
        // o = Some(coin): the linear is moved into the variant payload once.
        b0.stmts.push(MirStmt::assign(
            LocalId(1),
            MirRValue::Aggregate {
                kind: crate::codegen::ir::AggregateKind::Variant(
                    Arc::from("Option"),
                    0,
                    Arc::from("Some"),
                ),
                operands: vec![MirValue::Local(LocalId(0))],
            },
        ));
        // consume(o): by-value consume of the option once.
        b0.terminator = Some(MirTerminator::Call {
            func: MirValue::Global(Arc::from("consume")),
            args: vec![MirValue::Local(LocalId(1))],
            dest: Some(LocalId(2)),
            target: Some(BlockId(1)),
            unwind: None,
        });
        let mut b1 = MirBlock::new(BlockId(1));
        b1.terminator = Some(MirTerminator::Return(None));
        func.blocks = Some(vec![b0, b1]);

        let errors = check(&func);
        assert!(
            errors.is_empty(),
            "constructing Some(coin) and consuming it once is safe: {errors:?}"
        );
    }

    // 18. SAFE: `fn id<T>(x: T) -> T { x }` applied to a linear, then consumed.
    //     After monomorphization `id_Coin(coin)` returns a fresh owned linear
    //     bound to `c2`; the call MOVES `coin` once (arg by value) and REBINDS
    //     `c2` (call dest); `spend(c2)` consumes `c2` once. `id` is NOT
    //     borrow-escaping (it takes `x: T` by value, not `&T`), so no class-3
    //     flag.
    //     bb0: _0 = coin ; call id(_0) -> dest _1 ; bb1: call spend(_1) ; return
    #[test]
    fn precision_generic_identity_over_linear_is_clean() {
        // Callee: fn id(x: Qubit) -> Qubit { x }  (by-VALUE param, not a ref)
        let mut callee = MirFunction::new("id", MirFnSig::new(vec![], MirType::Void));
        callee.locals.push(linear_local(0, "x")); // by-value linear param
        let mut cb0 = MirBlock::new(BlockId(0));
        cb0.terminator = Some(MirTerminator::Return(Some(MirValue::Local(LocalId(0)))));
        callee.blocks = Some(vec![cb0]);

        // Caller: c2 = id(coin) ; spend(c2)
        let mut caller = MirFunction::new("main", MirFnSig::new(vec![], MirType::Void));
        caller.locals.push(linear_local(0, "coin"));
        caller.locals.push(linear_anon(1)); // c2 (call dest, fresh)
        caller.locals.push(i64_local(2, "r"));
        let mut mb0 = MirBlock::new(BlockId(0));
        mb0.terminator = Some(MirTerminator::Call {
            func: MirValue::Function(Arc::from("id")),
            args: vec![MirValue::Local(LocalId(0))],
            dest: Some(LocalId(1)),
            target: Some(BlockId(1)),
            unwind: None,
        });
        let mut mb1 = MirBlock::new(BlockId(1));
        mb1.terminator = Some(MirTerminator::Call {
            func: MirValue::Global(Arc::from("spend")),
            args: vec![MirValue::Local(LocalId(1))],
            dest: Some(LocalId(2)),
            target: Some(BlockId(2)),
            unwind: None,
        });
        let mut mb2 = MirBlock::new(BlockId(2));
        mb2.terminator = Some(MirTerminator::Return(None));
        caller.blocks = Some(vec![mb0, mb1, mb2]);

        let errors = check_module(&[callee, caller]);
        assert!(
            errors.is_empty(),
            "generic identity over a linear, consumed once, is safe: {errors:?}"
        );
    }

    // 19. SAFE: a closure that moves a linear once. The linear is captured by
    //     value into a closure Aggregate (moved once), and the closure is called
    //     once. No double-consume.
    //     bb0: _0 = coin ; _1 = Aggregate::Closure(_0) ; call _1 ; return
    #[test]
    fn precision_closure_moving_linear_once_is_clean() {
        let mut func = MirFunction::new("f", MirFnSig::new(vec![], MirType::Void));
        func.locals.push(linear_local(0, "coin"));
        func.locals.push(i64_local(1, "clos")); // the closure environment value
        func.locals.push(i64_local(2, "res"));
        let mut b0 = MirBlock::new(BlockId(0));
        // clos = move |...| { ... coin ... }: capture the linear by value once.
        b0.stmts.push(MirStmt::assign(
            LocalId(1),
            MirRValue::Aggregate {
                kind: crate::codegen::ir::AggregateKind::Closure(Arc::from("closure_0")),
                operands: vec![MirValue::Local(LocalId(0))],
            },
        ));
        // call the closure once.
        b0.terminator = Some(MirTerminator::Call {
            func: MirValue::Local(LocalId(1)),
            args: vec![],
            dest: Some(LocalId(2)),
            target: Some(BlockId(1)),
            unwind: None,
        });
        let mut b1 = MirBlock::new(BlockId(1));
        b1.terminator = Some(MirTerminator::Return(None));
        func.blocks = Some(vec![b0, b1]);

        let errors = check(&func);
        assert!(
            errors.is_empty(),
            "a closure capturing a linear by value once is safe: {errors:?}"
        );
    }

    // ===================================================================
    // 2c FIXPOINT (transitive class-3 hole): the borrow-escaping-parameter
    // analysis must close over the CALL GRAPH. A one-level wrapper that hands
    // its `&` param to an escaping callee is ALSO borrow-escaping, so passing
    // `&linear` into the wrapper is a move-out-of-borrow. Precision is
    // preserved: a wrapper delegating to a NON-escaping callee is not flagged.
    // ===================================================================

    /// Build a `deref_any`-shaped callee: `fn NAME(r: &Qubit) -> Qubit { *r }`
    /// (param 0 is directly borrow-escaping — derefs and returns the referent).
    fn deref_and_return_callee(name: &str) -> MirFunction {
        let mut f = MirFunction::new(name, MirFnSig::new(vec![], MirType::Void));
        f.locals.push(ref_param_local(0, "r")); // param 0: &Qubit
        f.locals.push(linear_anon(1)); // t = *r
        let mut b0 = MirBlock::new(BlockId(0));
        b0.stmts.push(MirStmt::assign(
            LocalId(1),
            MirRValue::Deref {
                ptr: MirValue::Local(LocalId(0)),
                pointee_ty: MirType::Struct(Arc::from("Qubit")),
            },
        ));
        b0.terminator = Some(MirTerminator::Return(Some(MirValue::Local(LocalId(1)))));
        f.blocks = Some(vec![b0]);
        f
    }

    /// Build a `peek`-shaped read-only callee: `fn NAME(r: &Qubit) -> i64 { 0 }`
    /// (never derefs r; NOT borrow-escaping).
    fn read_only_callee(name: &str) -> MirFunction {
        let mut f = MirFunction::new(name, MirFnSig::new(vec![], MirType::Void));
        f.locals.push(ref_param_local(0, "r"));
        let mut b0 = MirBlock::new(BlockId(0));
        b0.terminator = Some(MirTerminator::Return(Some(MirValue::Const(MirConst::Int(
            0,
            MirType::i64(),
        )))));
        f.blocks = Some(vec![b0]);
        f
    }

    /// Build a wrapper `fn NAME(r: &Qubit) -> Qubit { CALLEE(r) }`: its param 0
    /// (a `&`) is forwarded straight into arg-position 0 of `callee`. It NEVER
    /// derefs `r` itself, so the direct escape rule misses it; the fixpoint must
    /// mark it escaping iff `callee`'s param 0 is escaping.
    ///   bb0: call CALLEE(r) -> dest _1 ; bb1: return _1
    fn forwarding_wrapper(name: &str, callee: &str) -> MirFunction {
        let mut f = MirFunction::new(name, MirFnSig::new(vec![], MirType::Void));
        f.locals.push(ref_param_local(0, "r")); // param 0: &Qubit
        f.locals.push(linear_anon(1)); // call dest (the returned value)
        let mut b0 = MirBlock::new(BlockId(0));
        b0.terminator = Some(MirTerminator::Call {
            func: MirValue::Function(Arc::from(callee)),
            args: vec![MirValue::Local(LocalId(0))],
            dest: Some(LocalId(1)),
            target: Some(BlockId(1)),
            unwind: None,
        });
        let mut b1 = MirBlock::new(BlockId(1));
        b1.terminator = Some(MirTerminator::Return(Some(MirValue::Local(LocalId(1)))));
        f.blocks = Some(vec![b0, b1]);
        f
    }

    /// Build a `main` that does `r = &coin ; NAME(r)` (a single call passing a
    /// shared borrow of a linear into `callee`'s param 0). Distinct dest local.
    fn caller_passing_shared_borrow(callee: &str) -> MirFunction {
        let mut caller = MirFunction::new("main", MirFnSig::new(vec![], MirType::Void));
        caller.locals.push(linear_local(0, "coin"));
        caller.locals.push(untagged_ptr_local(1, "r")); // r = &coin
        caller.locals.push(linear_anon(2)); // call dest
        let mut mb0 = MirBlock::new(BlockId(0));
        mb0.stmts.push(MirStmt::assign(
            LocalId(1),
            MirRValue::Ref {
                is_mut: false,
                place: MirPlace::local(LocalId(0)),
            },
        ));
        mb0.terminator = Some(MirTerminator::Call {
            func: MirValue::Function(Arc::from(callee)),
            args: vec![MirValue::Local(LocalId(1))],
            dest: Some(LocalId(2)),
            target: Some(BlockId(1)),
            unwind: None,
        });
        let mut mb1 = MirBlock::new(BlockId(1));
        mb1.terminator = Some(MirTerminator::Return(None));
        caller.blocks = Some(vec![mb0, mb1]);
        caller
    }

    // 20. FIXPOINT (the fix): a wrapper `wrap` that FORWARDS its `&` param to
    //     `deref_any` (an escaping callee) must be computed borrow-escaping by
    //     the module fixpoint, even though `wrap` never derefs `r` itself.
    #[test]
    fn fixpoint_forwarding_wrapper_param_is_borrow_escaping() {
        let deref_any = deref_and_return_callee("deref_any");
        let wrap = forwarding_wrapper("wrap", "deref_any");
        let escaping = escaping_params_fixpoint(&[deref_any, wrap]);

        assert_eq!(
            escaping.get("deref_any"),
            Some(&HashSet::from([0])),
            "the direct deref-and-return param is the base case: {escaping:?}"
        );
        assert_eq!(
            escaping.get("wrap"),
            Some(&HashSet::from([0])),
            "the wrapper forwarding into an escaping callee must be escaping too: {escaping:?}"
        );
    }

    // 21. FIXPOINT end-to-end: passing `&coin` into the wrapper `wrap` (which
    //     delegates to `deref_any`) moves the referent out of the shared borrow.
    //     This is the transitive class-3 double-spend hole, now closed.
    #[test]
    fn fixpoint_pass_shared_borrow_into_wrapper_reports() {
        let deref_any = deref_and_return_callee("deref_any");
        let wrap = forwarding_wrapper("wrap", "deref_any");
        let main = caller_passing_shared_borrow("wrap");

        let errors = check_module(&[deref_any, wrap, main]);
        assert_eq!(
            count(&errors, |e| matches!(
                e,
                TypeError::LinearMoveOutOfBorrow { .. }
            )),
            1,
            "passing `&coin` into a transitive wrapper moves it out of the borrow: {errors:?}"
        );
    }

    // 22. PRECISION: a wrapper `w2` that forwards its `&` param to a NON-escaping
    //     callee (`peek`, read-only) must NOT become borrow-escaping, and passing
    //     `&coin` into `w2` must stay CLEAN.
    #[test]
    fn fixpoint_wrapper_over_non_escaping_callee_stays_clean() {
        let peek = read_only_callee("peek");
        let w2 = forwarding_wrapper("w2", "peek");
        let main = caller_passing_shared_borrow("w2");

        let escaping = escaping_params_fixpoint(&[peek.clone(), w2.clone()]);
        assert!(
            escaping.get("peek").is_none(),
            "a read-only borrow param is not escaping: {escaping:?}"
        );
        assert!(
            escaping.get("w2").is_none(),
            "a wrapper over a non-escaping callee is not escaping: {escaping:?}"
        );

        let errors = check_module(&[peek, w2, main]);
        assert!(
            errors.is_empty(),
            "passing `&coin` into a non-escaping wrapper is the legal borrow case: {errors:?}"
        );
    }

    // 23. FIXPOINT depth: a two-level wrapper chain `wrap2 -> wrap -> deref_any`
    //     must propagate escaping all the way up (proves the fixpoint iterates,
    //     not just one hop), so passing `&coin` into `wrap2` is rejected.
    #[test]
    fn fixpoint_two_level_wrapper_reports() {
        let deref_any = deref_and_return_callee("deref_any");
        let wrap = forwarding_wrapper("wrap", "deref_any");
        let wrap2 = forwarding_wrapper("wrap2", "wrap");

        let escaping = escaping_params_fixpoint(&[deref_any.clone(), wrap.clone(), wrap2.clone()]);
        assert_eq!(
            escaping.get("wrap2"),
            Some(&HashSet::from([0])),
            "escaping must propagate two hops up the wrapper chain: {escaping:?}"
        );

        let main = caller_passing_shared_borrow("wrap2");
        let errors = check_module(&[deref_any, wrap, wrap2, main]);
        assert_eq!(
            count(&errors, |e| matches!(
                e,
                TypeError::LinearMoveOutOfBorrow { .. }
            )),
            1,
            "passing `&coin` into a two-level wrapper moves it out of the borrow: {errors:?}"
        );
    }

    // 24. FIXPOINT termination: a self-recursive wrapper (`rec` calls `rec` with
    //     its own `&` param, and also forwards to `deref_any`) must converge (no
    //     infinite loop) and be marked escaping. Guards against a call-graph
    //     cycle diverging the fixpoint.
    //     fn rec(r: &Qubit) -> Qubit { if _ { rec(r) } else { deref_any(r) } }
    #[test]
    fn fixpoint_recursive_wrapper_terminates_and_reports() {
        let deref_any = deref_and_return_callee("deref_any");

        let mut rec = MirFunction::new("rec", MirFnSig::new(vec![], MirType::Void));
        rec.locals.push(ref_param_local(0, "r")); // param 0: &Qubit
        rec.locals.push(i64_local(9, "cond"));
        rec.locals.push(linear_anon(1)); // dest of the recursive call
        rec.locals.push(linear_anon(2)); // dest of the deref_any call
        let mut rb0 = MirBlock::new(BlockId(0));
        rb0.terminator = Some(MirTerminator::If {
            cond: MirValue::Local(LocalId(9)),
            then_block: BlockId(1),
            else_block: BlockId(2),
        });
        // then: t = rec(r) ; return t   (direct recursion on the &-param)
        let mut rb1 = MirBlock::new(BlockId(1));
        rb1.terminator = Some(MirTerminator::Call {
            func: MirValue::Function(Arc::from("rec")),
            args: vec![MirValue::Local(LocalId(0))],
            dest: Some(LocalId(1)),
            target: Some(BlockId(3)),
            unwind: None,
        });
        // else: t = deref_any(r) ; return t
        let mut rb2 = MirBlock::new(BlockId(2));
        rb2.terminator = Some(MirTerminator::Call {
            func: MirValue::Function(Arc::from("deref_any")),
            args: vec![MirValue::Local(LocalId(0))],
            dest: Some(LocalId(2)),
            target: Some(BlockId(3)),
            unwind: None,
        });
        let mut rb3 = MirBlock::new(BlockId(3));
        rb3.terminator = Some(MirTerminator::Return(Some(MirValue::Local(LocalId(1)))));
        rec.blocks = Some(vec![rb0, rb1, rb2, rb3]);

        // Terminates (monotone growth + `changed` flag), and `rec` param 0 is
        // escaping via the `deref_any(r)` arm.
        let escaping = escaping_params_fixpoint(&[deref_any.clone(), rec.clone()]);
        assert_eq!(
            escaping.get("rec"),
            Some(&HashSet::from([0])),
            "a recursive wrapper forwarding to an escaping callee is escaping: {escaping:?}"
        );

        let main = caller_passing_shared_borrow("rec");
        let errors = check_module(&[deref_any, rec, main]);
        assert_eq!(
            count(&errors, |e| matches!(
                e,
                TypeError::LinearMoveOutOfBorrow { .. }
            )),
            1,
            "passing `&coin` into the recursive wrapper is rejected: {errors:?}"
        );
    }

    // ===================================================================
    // FIX A (aggregate-launder class 3): the borrow-escape taint must follow
    // `*r` through an Aggregate (struct/tuple/enum-variant) and back out through
    // a FieldAccess / VariantField / IndexAccess projection. Otherwise a generic
    // `deref_boxed<T>(r:&T)->T { let b = Box{item:*r}; b.item }` launders the
    // referent past the deref-and-return signature and clones a linear. Precision
    // preserved: a fn that boxes `*r` locally but returns a scalar stays clean.
    // ===================================================================

    /// A struct-aggregate operand of `Box_i32` over one operand local.
    fn box_aggregate(operand: LocalId) -> MirRValue {
        MirRValue::Aggregate {
            kind: crate::codegen::ir::AggregateKind::Struct(Arc::from("Box_i32")),
            operands: vec![MirValue::Local(operand)],
        }
    }

    /// `fn deref_boxed(r:&Qubit) -> Qubit { let b = Box{item:*r}; b.item }`,
    /// exactly the monomorphized MIR the compiler emits (verified via `mir emit`):
    ///   _2 = *r ; _3 = Aggregate{Box}(_2) ; _4 = Use(_3) ; _5 = _4.item ; ret _5
    /// The deref taints `_2`, the aggregate carries taint to `_3`, `Use` to `_4`,
    /// the field-access re-exposes it in `_5`, and the return escapes param 0.
    fn deref_boxed_struct_callee(name: &str) -> MirFunction {
        let mut f = MirFunction::new(name, MirFnSig::new(vec![], MirType::Void));
        f.locals.push(ref_param_local(0, "r")); // param 0: &Qubit
        f.locals.push(linear_anon(2)); // _2 = *r
        f.locals.push(i64_local(3, "box")); // _3 = Box { item: _2 }
        f.locals.push(i64_local(4, "b")); // _4 = Use(_3)
        f.locals.push(linear_anon(5)); // _5 = _4.item
        let mut b0 = MirBlock::new(BlockId(0));
        b0.stmts.push(MirStmt::assign(
            LocalId(2),
            MirRValue::Deref {
                ptr: MirValue::Local(LocalId(0)),
                pointee_ty: MirType::Struct(Arc::from("Qubit")),
            },
        ));
        b0.stmts
            .push(MirStmt::assign(LocalId(3), box_aggregate(LocalId(2))));
        b0.stmts.push(MirStmt::assign(
            LocalId(4),
            MirRValue::Use(MirValue::Local(LocalId(3))),
        ));
        b0.stmts.push(MirStmt::assign(
            LocalId(5),
            MirRValue::FieldAccess {
                base: MirValue::Local(LocalId(4)),
                field_name: Arc::from("item"),
                field_ty: MirType::Struct(Arc::from("Qubit")),
            },
        ));
        b0.terminator = Some(MirTerminator::Return(Some(MirValue::Local(LocalId(5)))));
        f.blocks = Some(vec![b0]);
        f
    }

    // A1. FIX A: the struct-launder callee `deref_boxed` must be detected
    //     borrow-escaping (taint follows `*r` into the aggregate and back out
    //     through the field access).
    #[test]
    fn fixa_deref_boxed_struct_is_borrow_escaping() {
        let f = deref_boxed_struct_callee("deref_boxed");
        let escaping = escaping_params_fixpoint(&[f]);
        assert_eq!(
            escaping.get("deref_boxed"),
            Some(&HashSet::from([0])),
            "taint must follow *r through the struct aggregate and field-access: {escaping:?}"
        );
    }

    // A2. FIX A end-to-end: `deref_boxed(&coin)` (struct launder) rejects.
    #[test]
    fn fixa_pass_shared_borrow_into_struct_launder_reports() {
        let f = deref_boxed_struct_callee("deref_boxed");
        let main = caller_passing_shared_borrow("deref_boxed");
        let errors = check_module(&[f, main]);
        assert_eq!(
            count(&errors, |e| matches!(
                e,
                TypeError::LinearMoveOutOfBorrow { .. }
            )),
            1,
            "passing `&coin` into a struct-laundering deref-return clones the linear: {errors:?}"
        );
    }

    // A3. FIX A (tuple): `fn deref_tup(r:&T)->T { let t=(*r,); t.0 }` launders
    //     through a tuple Aggregate + IndexAccess-shaped/field projection. Model
    //     the payload extract as a FieldAccess of tuple field 0 (same base-taint
    //     path); the essential edge is Aggregate + projection.
    #[test]
    fn fixa_deref_tuple_is_borrow_escaping_and_reports() {
        let mut f = MirFunction::new("deref_tup", MirFnSig::new(vec![], MirType::Void));
        f.locals.push(ref_param_local(0, "r"));
        f.locals.push(linear_anon(1)); // _1 = *r
        f.locals.push(i64_local(2, "tup")); // _2 = (*r,) tuple aggregate
        f.locals.push(linear_anon(3)); // _3 = tup.0
        let mut b0 = MirBlock::new(BlockId(0));
        b0.stmts.push(MirStmt::assign(
            LocalId(1),
            MirRValue::Deref {
                ptr: MirValue::Local(LocalId(0)),
                pointee_ty: MirType::Struct(Arc::from("Qubit")),
            },
        ));
        b0.stmts.push(MirStmt::assign(
            LocalId(2),
            MirRValue::Aggregate {
                kind: crate::codegen::ir::AggregateKind::Tuple,
                operands: vec![MirValue::Local(LocalId(1))],
            },
        ));
        b0.stmts.push(MirStmt::assign(
            LocalId(3),
            MirRValue::FieldAccess {
                base: MirValue::Local(LocalId(2)),
                field_name: Arc::from("0"),
                field_ty: MirType::Struct(Arc::from("Qubit")),
            },
        ));
        b0.terminator = Some(MirTerminator::Return(Some(MirValue::Local(LocalId(3)))));
        f.blocks = Some(vec![b0]);

        let escaping = escaping_params_fixpoint(std::slice::from_ref(&f));
        assert_eq!(
            escaping.get("deref_tup"),
            Some(&HashSet::from([0])),
            "taint must follow *r through a tuple aggregate and projection: {escaping:?}"
        );
        let main = caller_passing_shared_borrow("deref_tup");
        let errors = check_module(&[f, main]);
        assert_eq!(
            count(&errors, |e| matches!(
                e,
                TypeError::LinearMoveOutOfBorrow { .. }
            )),
            1,
            "the tuple launder clones the linear: {errors:?}"
        );
    }

    // A4. FIX A (enum): `fn deref_enum(r:&T)->T { let e=Some(*r); match e {..} }`
    //     launders through an enum-variant Aggregate and extracts it back with a
    //     VariantField bind. The variant-field projection must carry the taint.
    #[test]
    fn fixa_deref_enum_is_borrow_escaping_and_reports() {
        let mut f = MirFunction::new("deref_enum", MirFnSig::new(vec![], MirType::Void));
        f.locals.push(ref_param_local(0, "r"));
        f.locals.push(linear_anon(1)); // _1 = *r
        f.locals.push(linear_anon(2)); // _2 = Some(*r) enum-variant aggregate
        f.locals.push(linear_anon(3)); // _3 = VariantField(_2)
        let mut b0 = MirBlock::new(BlockId(0));
        b0.stmts.push(MirStmt::assign(
            LocalId(1),
            MirRValue::Deref {
                ptr: MirValue::Local(LocalId(0)),
                pointee_ty: MirType::Struct(Arc::from("Qubit")),
            },
        ));
        b0.stmts.push(MirStmt::assign(
            LocalId(2),
            MirRValue::Aggregate {
                kind: crate::codegen::ir::AggregateKind::Variant(
                    Arc::from("Option"),
                    0,
                    Arc::from("Some"),
                ),
                operands: vec![MirValue::Local(LocalId(1))],
            },
        ));
        b0.stmts
            .push(MirStmt::assign(LocalId(3), variant_field(LocalId(2))));
        b0.terminator = Some(MirTerminator::Return(Some(MirValue::Local(LocalId(3)))));
        f.blocks = Some(vec![b0]);

        let escaping = escaping_params_fixpoint(std::slice::from_ref(&f));
        assert_eq!(
            escaping.get("deref_enum"),
            Some(&HashSet::from([0])),
            "taint must follow *r through an enum-variant aggregate and VariantField: {escaping:?}"
        );
        let main = caller_passing_shared_borrow("deref_enum");
        let errors = check_module(&[f, main]);
        assert_eq!(
            count(&errors, |e| matches!(
                e,
                TypeError::LinearMoveOutOfBorrow { .. }
            )),
            1,
            "the enum launder clones the linear: {errors:?}"
        );
    }

    // A5. FIX A (3-level wrapper chain): `w2 -> w1 -> launder`, where `launder`
    //     is the struct-launder callee. The fixpoint over the call graph must
    //     propagate escaping from the aggregate-launder base all the way up two
    //     forwarding hops, so passing `&coin` into `w2` is rejected. This proves
    //     FIX A (aggregate base case) composes with the existing call-graph
    //     fixpoint (transitive case).
    #[test]
    fn fixa_three_level_wrapper_over_launder_reports() {
        let launder = deref_boxed_struct_callee("launder");
        let w1 = forwarding_wrapper("w1", "launder");
        let w2 = forwarding_wrapper("w2", "w1");

        let escaping = escaping_params_fixpoint(&[launder.clone(), w1.clone(), w2.clone()]);
        assert_eq!(
            escaping.get("launder"),
            Some(&HashSet::from([0])),
            "the aggregate-launder base case is escaping: {escaping:?}"
        );
        assert_eq!(
            escaping.get("w2"),
            Some(&HashSet::from([0])),
            "escaping must propagate two hops up over an aggregate-launder base: {escaping:?}"
        );
        let main = caller_passing_shared_borrow("w2");
        let errors = check_module(&[launder, w1, w2, main]);
        assert_eq!(
            count(&errors, |e| matches!(
                e,
                TypeError::LinearMoveOutOfBorrow { .. }
            )),
            1,
            "passing `&coin` into a 3-level wrapper over a launder clones the linear: {errors:?}"
        );
    }

    // A6. FIX A PRECISION: a `peek`-like fn that derefs `*r` into a LOCAL
    //     aggregate but RETURNS A SCALAR (never returns the tainted aggregate)
    //     must NOT be flagged borrow-escaping. The taint reaches the param only
    //     through `Return`; a scalar return leaves it clean.
    //     fn inspect(r:&T) -> i64 { let b = Box{item:*r}; 0 }
    #[test]
    fn fixa_deref_into_local_aggregate_returning_scalar_stays_clean() {
        let mut f = MirFunction::new("inspect", MirFnSig::new(vec![], MirType::Void));
        f.locals.push(ref_param_local(0, "r"));
        f.locals.push(linear_anon(1)); // _1 = *r
        f.locals.push(i64_local(2, "box")); // _2 = Box { item: _1 } (local, unreturned)
        let mut b0 = MirBlock::new(BlockId(0));
        b0.stmts.push(MirStmt::assign(
            LocalId(1),
            MirRValue::Deref {
                ptr: MirValue::Local(LocalId(0)),
                pointee_ty: MirType::Struct(Arc::from("Qubit")),
            },
        ));
        b0.stmts
            .push(MirStmt::assign(LocalId(2), box_aggregate(LocalId(1))));
        // returns a SCALAR const, not the tainted aggregate.
        b0.terminator = Some(MirTerminator::Return(Some(MirValue::Const(MirConst::Int(
            0,
            MirType::i64(),
        )))));
        f.blocks = Some(vec![b0]);

        let escaping = escaping_params_fixpoint(std::slice::from_ref(&f));
        assert!(
            escaping.get("inspect").is_none(),
            "boxing *r locally but returning a scalar is not borrow-escaping: {escaping:?}"
        );
        // And passing `&coin` into it is the legal borrow-read case.
        let main = caller_passing_shared_borrow("inspect");
        let errors = check_module(&[f, main]);
        assert!(
            errors.is_empty(),
            "a fn that boxes *r locally and returns a scalar is clean: {errors:?}"
        );
    }

    // ===================================================================
    // FIX B (record-pattern-through-`&` move-out): after the lowering binds a
    // record-pattern field to a real projection of the shared-borrowed
    // scrutinee, the checker must reject extracting a LINEAR field by value out
    // of the borrow. `shared_linear_ref_set` propagates through the scrutinee
    // COPY (`_5 = Use(_4)`); the field-extract rule fires on a linear `dest`.
    // Precision: extracting a NON-linear field through the borrow stays clean.
    // ===================================================================

    // B1. FIX B: `match &w { Wrap { coin: c } => spend(c) }` — the exact MIR the
    //     lowering now emits: `_4 = &w ; _5 = Use(_4) ; c = _5.coin ; spend(c)`.
    //     Extracting the linear `coin` field out of the shared-borrowed `w`
    //     (through the scrutinee copy `_5`) is a move-out-of-borrow.
    #[test]
    fn fixb_record_pattern_field_extract_through_borrow_reports() {
        let mut func = MirFunction::new("main", MirFnSig::new(vec![], MirType::Void));
        func.locals.push(linear_local(0, "w")); // linear struct scrutinee
        func.locals.push(untagged_ptr_local(1, "_4")); // _4 = &w
        func.locals.push(untagged_ptr_local(2, "_5")); // _5 = Use(_4) (scrutinee copy)
        func.locals.push(linear_anon(3)); // c = _5.coin (linear field extract)
        let mut b0 = MirBlock::new(BlockId(0));
        // _4 = &w  (shared borrow of the linear -> provenance seed)
        b0.stmts.push(MirStmt::assign(
            LocalId(1),
            MirRValue::Ref {
                is_mut: false,
                place: MirPlace::local(LocalId(0)),
            },
        ));
        // _5 = Use(_4)  (buildlang copies the borrow into the scrutinee temp)
        b0.stmts.push(MirStmt::assign(
            LocalId(2),
            MirRValue::Use(MirValue::Local(LocalId(1))),
        ));
        // c = _5.coin  (extract the LINEAR field out of the borrowed scrutinee)
        b0.stmts.push(MirStmt::assign(
            LocalId(3),
            MirRValue::FieldAccess {
                base: MirValue::Local(LocalId(2)),
                field_name: Arc::from("coin"),
                field_ty: MirType::Struct(Arc::from("Qubit")),
            },
        ));
        b0.terminator = Some(MirTerminator::Return(None));
        func.blocks = Some(vec![b0]);

        let errors = check(&func);
        assert_eq!(
            count(&errors, |e| matches!(
                e,
                TypeError::LinearMoveOutOfBorrow { .. }
            )),
            1,
            "extracting a linear field out of a shared borrow (record pattern) is a move-out: {errors:?}"
        );
    }

    // B2. FIX B PRECISION: extracting a NON-linear field through the shared
    //     borrow (`Point { x: v }` reading `v: i64`) must NOT be flagged — the
    //     rule is guarded on the `dest` being linear.
    //     _1 = &p ; _2 = Use(_1) ; v = _2.x (v NON-linear) ; return
    #[test]
    fn fixb_nonlinear_field_extract_through_borrow_is_clean() {
        let mut func = MirFunction::new("main", MirFnSig::new(vec![], MirType::Void));
        func.locals.push(linear_local(0, "p")); // linear scrutinee (has a non-linear field)
        func.locals.push(untagged_ptr_local(1, "r")); // r = &p
        func.locals.push(untagged_ptr_local(2, "s")); // s = Use(r)
        func.locals.push(i64_local(3, "v")); // v = s.x  (NON-linear dest)
        let mut b0 = MirBlock::new(BlockId(0));
        b0.stmts.push(MirStmt::assign(
            LocalId(1),
            MirRValue::Ref {
                is_mut: false,
                place: MirPlace::local(LocalId(0)),
            },
        ));
        b0.stmts.push(MirStmt::assign(
            LocalId(2),
            MirRValue::Use(MirValue::Local(LocalId(1))),
        ));
        // v = s.x  (NON-linear field extract: not flagged)
        b0.stmts.push(MirStmt::assign(
            LocalId(3),
            MirRValue::FieldAccess {
                base: MirValue::Local(LocalId(2)),
                field_name: Arc::from("x"),
                field_ty: MirType::i64(),
            },
        ));
        b0.terminator = Some(MirTerminator::Return(None));
        func.blocks = Some(vec![b0]);

        let errors = check(&func);
        assert_eq!(
            count(&errors, |e| matches!(
                e,
                TypeError::LinearMoveOutOfBorrow { .. }
            )),
            0,
            "reading a non-linear field through a shared borrow is the legal case: {errors:?}"
        );
    }

    // ===================================================================
    // FIX 2 (higher-order fn-pointer escape): a `&`-param FORWARDED into an
    // INDIRECT call (a `Call` whose `func` is not a resolvable named function —
    // a fn-pointer local or a struct-field fn-pointer) must be marked
    // borrow-escaping. The callee body is opaque, so we must assume it may
    // deref-escape the borrow. Otherwise `apply(f: fn(&Coin)->Coin, r: &Coin) {
    // f(r) }` never marks `r` escaping and `apply(deref_any, &coin)` twice
    // clones the linear. Conservative-sound over-approximation.
    // ===================================================================

    /// An `apply`-shaped function: `fn NAME(f: fn(&Qubit)->Qubit, r: &Qubit) ->
    /// Qubit { f(r) }`. Param 0 is the fn-pointer `f`; param 1 is the `&`-param
    /// `r`, forwarded into an INDIRECT call `Call { func: Local(f), args: [r] }`.
    /// `call_target_name(Local(f))` is `None`, so the direct rule never sees it;
    /// the new indirect rule must mark param 1 escaping.
    ///   bb0: call (Local f)(r) -> dest _2 ; bb1: return _2
    fn apply_indirect_call(name: &str, fn_ptr_param: u32, ref_param: u32) -> MirFunction {
        let mut f = MirFunction::new(name, MirFnSig::new(vec![], MirType::Void));
        // Param 0: the fn-pointer `f` (modeled as a pointer-typed param so it is
        // a leading param local; it is used as the callee `func`, never an arg).
        f.locals.push(ref_param_local(fn_ptr_param, "f"));
        // Param 1: the `&Qubit` `r` (the borrow that is forwarded into `f`).
        f.locals.push(ref_param_local(ref_param, "r"));
        f.locals.push(linear_anon(2)); // call dest (the returned value)
        let mut b0 = MirBlock::new(BlockId(0));
        b0.terminator = Some(MirTerminator::Call {
            func: MirValue::Local(LocalId(fn_ptr_param)), // INDIRECT: fn-ptr local
            args: vec![MirValue::Local(LocalId(ref_param))],
            dest: Some(LocalId(2)),
            target: Some(BlockId(1)),
            unwind: None,
        });
        let mut b1 = MirBlock::new(BlockId(1));
        b1.terminator = Some(MirTerminator::Return(Some(MirValue::Local(LocalId(2)))));
        f.blocks = Some(vec![b0, b1]);
        f
    }

    // F2-1. FIX 2 (direct): `apply`'s `&`-param (index 1) is forwarded into an
    //       indirect fn-pointer call and must be marked borrow-escaping.
    #[test]
    fn fix2_apply_ref_param_forwarded_into_fn_pointer_is_escaping() {
        let apply = apply_indirect_call("apply", 0, 1);
        let escaping = escaping_params_fixpoint(std::slice::from_ref(&apply));
        assert_eq!(
            escaping.get("apply"),
            Some(&HashSet::from([1])),
            "a &-param forwarded into an indirect fn-pointer call is escaping: {escaping:?}"
        );
    }

    // F2-2. FIX 2 end-to-end (direct): passing `&coin` into `apply`'s escaping
    //       `&`-param (index 1) moves the linear out of the shared borrow.
    //       caller: r = &coin ; apply(_, r) — arg 0 is a fn-ptr, arg 1 is `r`.
    #[test]
    fn fix2_pass_shared_borrow_into_apply_reports() {
        let apply = apply_indirect_call("apply", 0, 1);

        // Caller: r = &coin ; apply(fptr, r). Distinct dest local.
        let mut caller = MirFunction::new("main", MirFnSig::new(vec![], MirType::Void));
        caller.locals.push(linear_local(0, "coin"));
        caller.locals.push(untagged_ptr_local(1, "r")); // r = &coin
        caller.locals.push(untagged_ptr_local(2, "fptr")); // the fn-pointer value
        caller.locals.push(linear_anon(3)); // call dest
        let mut mb0 = MirBlock::new(BlockId(0));
        mb0.stmts.push(MirStmt::assign(
            LocalId(1),
            MirRValue::Ref {
                is_mut: false,
                place: MirPlace::local(LocalId(0)),
            },
        ));
        mb0.terminator = Some(MirTerminator::Call {
            func: MirValue::Function(Arc::from("apply")),
            // arg 0: the fn-pointer (a non-linear value); arg 1: the borrow `r`.
            args: vec![MirValue::Local(LocalId(2)), MirValue::Local(LocalId(1))],
            dest: Some(LocalId(3)),
            target: Some(BlockId(1)),
            unwind: None,
        });
        let mut mb1 = MirBlock::new(BlockId(1));
        mb1.terminator = Some(MirTerminator::Return(None));
        caller.blocks = Some(vec![mb0, mb1]);

        let errors = check_module(&[apply, caller]);
        assert_eq!(
            count(&errors, |e| matches!(
                e,
                TypeError::LinearMoveOutOfBorrow { .. }
            )),
            1,
            "passing `&coin` into apply's fn-pointer-forwarded &-param is a move-out: {errors:?}"
        );
    }

    // F2-3. FIX 2 (2-level): `apply2` forwards its `&`-param into `apply` (a
    //       NAMED callee whose param 1 is escaping via the indirect rule). The
    //       existing call-graph fixpoint must then propagate escaping up to
    //       `apply2` param 1. Proves the indirect base composes with the
    //       transitive (named-forwarding) case.
    //       fn apply2(f, r) { apply(f, r) }  (r forwarded into apply's param 1)
    #[test]
    fn fix2_two_level_apply_over_fn_pointer_reports() {
        let apply = apply_indirect_call("apply", 0, 1);

        // apply2(f: param0, r: param1) { apply(f, r) } — a NAMED call to apply,
        // forwarding r into apply's escaping param-1 position.
        let mut apply2 = MirFunction::new("apply2", MirFnSig::new(vec![], MirType::Void));
        apply2.locals.push(ref_param_local(0, "f"));
        apply2.locals.push(ref_param_local(1, "r"));
        apply2.locals.push(linear_anon(2)); // call dest
        let mut b0 = MirBlock::new(BlockId(0));
        b0.terminator = Some(MirTerminator::Call {
            func: MirValue::Function(Arc::from("apply")),
            args: vec![MirValue::Local(LocalId(0)), MirValue::Local(LocalId(1))],
            dest: Some(LocalId(2)),
            target: Some(BlockId(1)),
            unwind: None,
        });
        let mut b1 = MirBlock::new(BlockId(1));
        b1.terminator = Some(MirTerminator::Return(Some(MirValue::Local(LocalId(2)))));
        apply2.blocks = Some(vec![b0, b1]);

        let escaping = escaping_params_fixpoint(&[apply.clone(), apply2.clone()]);
        assert_eq!(
            escaping.get("apply2"),
            Some(&HashSet::from([1])),
            "escaping must propagate up from the fn-pointer base to apply2: {escaping:?}"
        );

        // Caller: r = &coin ; apply2(fptr, r) — must reject.
        let mut caller = MirFunction::new("main", MirFnSig::new(vec![], MirType::Void));
        caller.locals.push(linear_local(0, "coin"));
        caller.locals.push(untagged_ptr_local(1, "r"));
        caller.locals.push(untagged_ptr_local(2, "fptr"));
        caller.locals.push(linear_anon(3));
        let mut mb0 = MirBlock::new(BlockId(0));
        mb0.stmts.push(MirStmt::assign(
            LocalId(1),
            MirRValue::Ref {
                is_mut: false,
                place: MirPlace::local(LocalId(0)),
            },
        ));
        mb0.terminator = Some(MirTerminator::Call {
            func: MirValue::Function(Arc::from("apply2")),
            args: vec![MirValue::Local(LocalId(2)), MirValue::Local(LocalId(1))],
            dest: Some(LocalId(3)),
            target: Some(BlockId(1)),
            unwind: None,
        });
        let mut mb1 = MirBlock::new(BlockId(1));
        mb1.terminator = Some(MirTerminator::Return(None));
        caller.blocks = Some(vec![mb0, mb1]);

        let errors = check_module(&[apply, apply2, caller]);
        assert_eq!(
            count(&errors, |e| matches!(
                e,
                TypeError::LinearMoveOutOfBorrow { .. }
            )),
            1,
            "passing `&coin` into a 2-level fn-pointer wrapper is a move-out: {errors:?}"
        );
    }

    // F2-4. FIX 2 (struct-field fn-pointer): a call whose `func` is a `Local`
    //       loaded from a struct field (`ops.extract`) is ALSO an indirect call
    //       (`call_target_name` is `None`), so `run`'s `&`-param forwarded into
    //       it is escaping. Modeled the same as the fn-pointer local: the callee
    //       is a `Local` the checker cannot resolve to a name.
    //       fn run(ops_extract: fn, r: &Qubit) { ops_extract(r) }
    #[test]
    fn fix2_struct_field_fn_pointer_is_escaping_and_reports() {
        // Same MIR shape as apply: an indirect `Call { func: Local(..) }`. The
        // struct-field load that produced the fn-pointer local is upstream; what
        // matters to the checker is that the callee is an unresolvable `Local`.
        let run = apply_indirect_call("run", 0, 1);
        let escaping = escaping_params_fixpoint(std::slice::from_ref(&run));
        assert_eq!(
            escaping.get("run"),
            Some(&HashSet::from([1])),
            "a &-param forwarded into a struct-field fn-pointer call is escaping: {escaping:?}"
        );

        let main = {
            let mut caller = MirFunction::new("main", MirFnSig::new(vec![], MirType::Void));
            caller.locals.push(linear_local(0, "coin"));
            caller.locals.push(untagged_ptr_local(1, "r"));
            caller.locals.push(untagged_ptr_local(2, "fptr"));
            caller.locals.push(linear_anon(3));
            let mut mb0 = MirBlock::new(BlockId(0));
            mb0.stmts.push(MirStmt::assign(
                LocalId(1),
                MirRValue::Ref {
                    is_mut: false,
                    place: MirPlace::local(LocalId(0)),
                },
            ));
            mb0.terminator = Some(MirTerminator::Call {
                func: MirValue::Function(Arc::from("run")),
                args: vec![MirValue::Local(LocalId(2)), MirValue::Local(LocalId(1))],
                dest: Some(LocalId(3)),
                target: Some(BlockId(1)),
                unwind: None,
            });
            let mut mb1 = MirBlock::new(BlockId(1));
            mb1.terminator = Some(MirTerminator::Return(None));
            caller.blocks = Some(vec![mb0, mb1]);
            caller
        };
        let errors = check_module(&[run, main]);
        assert_eq!(
            count(&errors, |e| matches!(
                e,
                TypeError::LinearMoveOutOfBorrow { .. }
            )),
            1,
            "passing `&coin` into a struct-field fn-pointer consumer is a move-out: {errors:?}"
        );
    }

    // F2-5. FIX 2 PRECISION: a function that makes an INDIRECT call but does NOT
    //       forward any `&`-param into it must NOT become escaping. `g` has a
    //       `&`-param `r` it never passes to the fn-pointer (it calls `f()` with
    //       no args), so `g` stays clean and passing `&coin` into `g` is legal.
    //       fn g(f: fn()->(), r: &Qubit) { f() }   (r not forwarded)
    #[test]
    fn fix2_indirect_call_not_forwarding_ref_param_stays_clean() {
        let mut g = MirFunction::new("g", MirFnSig::new(vec![], MirType::Void));
        g.locals.push(ref_param_local(0, "f")); // fn-pointer param
        g.locals.push(ref_param_local(1, "r")); // &-param, NOT forwarded
        g.locals.push(i64_local(2, "res"));
        let mut b0 = MirBlock::new(BlockId(0));
        b0.terminator = Some(MirTerminator::Call {
            func: MirValue::Local(LocalId(0)), // indirect call
            args: vec![],                      // no args: r is not forwarded
            dest: Some(LocalId(2)),
            target: Some(BlockId(1)),
            unwind: None,
        });
        let mut b1 = MirBlock::new(BlockId(1));
        b1.terminator = Some(MirTerminator::Return(None));
        g.blocks = Some(vec![b0, b1]);

        let escaping = escaping_params_fixpoint(std::slice::from_ref(&g));
        assert!(
            escaping.get("g").is_none(),
            "an indirect call not forwarding a &-param is not escaping: {escaping:?}"
        );

        let main = caller_passing_shared_borrow("g");
        let errors = check_module(&[g, main]);
        assert!(
            errors.is_empty(),
            "passing `&coin` into a fn that does not forward it is the legal case: {errors:?}"
        );
    }
}