verif 0.2.0

In-code formal verification for Rust: an embedded bit-blasting SMT decision procedure, a symbolic DSL, contracts and a machine-checked proof kernel. SPARK-inspired, no_std, no external dependencies.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
12009
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049
12050
12051
12052
12053
12054
12055
12056
12057
12058
12059
12060
12061
12062
12063
12064
12065
12066
12067
12068
12069
12070
12071
12072
12073
12074
12075
12076
12077
12078
12079
12080
12081
12082
12083
12084
12085
12086
12087
12088
12089
12090
12091
12092
12093
12094
12095
12096
12097
12098
12099
12100
12101
12102
12103
12104
12105
12106
12107
12108
12109
12110
12111
12112
12113
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124
12125
12126
12127
12128
12129
12130
12131
12132
12133
12134
12135
12136
12137
12138
12139
12140
12141
12142
12143
12144
12145
12146
12147
12148
12149
12150
12151
12152
12153
12154
12155
12156
12157
12158
12159
12160
12161
12162
12163
12164
12165
12166
12167
12168
12169
12170
12171
12172
12173
12174
12175
12176
12177
12178
12179
12180
12181
12182
12183
12184
12185
12186
12187
12188
12189
12190
12191
12192
12193
12194
12195
12196
12197
12198
12199
12200
12201
12202
12203
12204
12205
12206
12207
12208
12209
12210
12211
12212
12213
12214
12215
12216
12217
12218
12219
12220
12221
12222
12223
12224
12225
12226
12227
12228
12229
12230
12231
12232
12233
12234
12235
12236
12237
12238
12239
12240
12241
12242
12243
12244
12245
12246
12247
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260
12261
12262
12263
12264
12265
12266
12267
12268
12269
12270
12271
12272
12273
12274
12275
12276
12277
12278
12279
12280
12281
12282
12283
12284
12285
12286
12287
12288
12289
12290
12291
12292
12293
12294
12295
12296
12297
12298
12299
12300
12301
12302
12303
12304
12305
12306
12307
12308
12309
12310
12311
12312
12313
12314
12315
12316
12317
12318
12319
12320
12321
12322
12323
12324
12325
12326
12327
12328
12329
12330
12331
12332
12333
12334
12335
12336
12337
12338
12339
12340
12341
12342
12343
12344
12345
12346
12347
12348
12349
12350
12351
12352
12353
12354
12355
12356
12357
12358
12359
12360
12361
12362
12363
12364
12365
12366
12367
12368
12369
12370
12371
12372
12373
12374
12375
12376
12377
12378
12379
12380
12381
12382
12383
12384
12385
12386
12387
12388
12389
12390
12391
12392
12393
12394
12395
12396
12397
12398
12399
12400
12401
12402
12403
12404
12405
12406
12407
12408
12409
12410
12411
12412
12413
12414
12415
12416
12417
12418
12419
12420
12421
12422
12423
12424
12425
12426
12427
12428
12429
12430
12431
12432
12433
12434
12435
12436
12437
12438
12439
12440
12441
12442
12443
12444
12445
12446
12447
12448
12449
12450
12451
12452
12453
12454
12455
12456
12457
12458
12459
12460
12461
12462
12463
12464
12465
12466
12467
12468
12469
12470
12471
12472
12473
12474
12475
12476
12477
12478
12479
12480
12481
12482
12483
12484
12485
12486
12487
12488
12489
12490
12491
12492
12493
12494
12495
12496
12497
12498
12499
12500
12501
12502
12503
12504
12505
12506
12507
12508
12509
12510
12511
12512
12513
12514
12515
12516
12517
12518
12519
12520
12521
12522
12523
12524
12525
12526
12527
12528
12529
12530
12531
12532
12533
12534
12535
12536
12537
12538
12539
12540
12541
12542
12543
12544
12545
12546
12547
12548
12549
12550
12551
12552
12553
12554
12555
12556
12557
12558
12559
12560
12561
12562
12563
12564
12565
12566
12567
12568
12569
12570
12571
12572
12573
12574
12575
12576
12577
12578
12579
12580
12581
12582
12583
12584
12585
12586
12587
12588
12589
12590
12591
12592
12593
12594
12595
12596
12597
12598
12599
12600
12601
12602
12603
12604
12605
12606
12607
12608
12609
12610
12611
12612
12613
12614
12615
12616
12617
12618
12619
12620
12621
12622
12623
12624
12625
12626
12627
12628
12629
12630
12631
12632
12633
12634
12635
12636
12637
12638
12639
12640
12641
12642
12643
12644
12645
12646
12647
12648
12649
12650
12651
12652
12653
12654
12655
12656
12657
12658
12659
12660
12661
12662
12663
12664
12665
12666
12667
12668
12669
12670
12671
12672
12673
12674
12675
12676
12677
12678
12679
12680
12681
12682
12683
12684
12685
12686
12687
12688
12689
12690
12691
12692
12693
12694
12695
12696
12697
12698
12699
12700
12701
12702
12703
12704
12705
12706
12707
12708
12709
12710
12711
12712
12713
12714
12715
12716
12717
12718
12719
12720
12721
12722
12723
12724
12725
12726
12727
12728
12729
12730
12731
12732
12733
12734
12735
12736
12737
12738
12739
12740
12741
12742
12743
12744
12745
12746
12747
12748
12749
12750
12751
12752
12753
12754
12755
12756
12757
12758
12759
12760
12761
12762
12763
12764
12765
12766
12767
12768
12769
12770
12771
12772
12773
12774
12775
12776
12777
12778
12779
12780
12781
12782
12783
12784
12785
12786
12787
12788
12789
12790
12791
12792
12793
12794
12795
12796
12797
12798
12799
12800
12801
12802
12803
12804
12805
12806
12807
12808
12809
12810
12811
12812
12813
12814
12815
12816
12817
12818
12819
12820
12821
12822
12823
12824
12825
12826
12827
12828
12829
12830
12831
12832
12833
12834
12835
12836
12837
12838
12839
12840
12841
12842
12843
12844
12845
12846
12847
12848
12849
12850
12851
12852
12853
12854
12855
12856
12857
12858
12859
12860
12861
12862
12863
12864
12865
12866
12867
12868
12869
12870
12871
12872
12873
12874
12875
12876
12877
12878
12879
12880
12881
12882
12883
12884
12885
12886
12887
12888
12889
12890
12891
12892
12893
12894
12895
12896
12897
12898
12899
12900
12901
12902
12903
12904
12905
12906
12907
12908
12909
12910
12911
12912
12913
12914
12915
12916
12917
12918
12919
12920
12921
12922
12923
12924
12925
12926
12927
12928
12929
12930
12931
12932
12933
12934
12935
12936
12937
12938
12939
12940
12941
12942
12943
12944
12945
12946
12947
12948
12949
12950
12951
12952
12953
12954
12955
12956
12957
12958
12959
12960
12961
12962
12963
12964
12965
12966
12967
12968
12969
12970
12971
12972
12973
12974
12975
12976
12977
12978
12979
12980
12981
12982
12983
12984
12985
12986
12987
12988
12989
12990
12991
12992
12993
12994
12995
12996
12997
12998
12999
13000
13001
13002
13003
13004
13005
13006
13007
13008
13009
13010
13011
13012
13013
13014
13015
13016
13017
13018
13019
13020
13021
13022
13023
13024
13025
13026
13027
13028
13029
13030
13031
13032
13033
13034
13035
13036
13037
13038
13039
13040
13041
13042
13043
13044
13045
13046
13047
13048
13049
13050
13051
13052
13053
13054
13055
13056
13057
13058
13059
13060
13061
13062
13063
13064
13065
13066
13067
13068
13069
13070
13071
13072
13073
13074
13075
13076
13077
13078
13079
13080
13081
13082
13083
13084
13085
13086
13087
13088
13089
13090
13091
13092
13093
13094
13095
13096
13097
13098
13099
13100
13101
13102
13103
13104
13105
13106
13107
13108
13109
13110
13111
13112
13113
13114
13115
13116
13117
13118
13119
13120
13121
13122
13123
13124
13125
13126
13127
13128
13129
13130
13131
13132
13133
13134
13135
13136
13137
13138
13139
13140
13141
13142
13143
13144
13145
13146
13147
13148
13149
13150
13151
13152
13153
13154
13155
13156
13157
13158
13159
13160
13161
13162
13163
13164
13165
13166
13167
13168
13169
13170
13171
13172
13173
13174
13175
13176
13177
13178
13179
13180
13181
13182
13183
13184
13185
13186
13187
13188
13189
13190
13191
13192
13193
13194
13195
13196
13197
13198
13199
13200
13201
13202
13203
13204
13205
13206
13207
13208
13209
13210
13211
13212
13213
13214
13215
13216
13217
13218
13219
13220
13221
13222
13223
13224
13225
13226
13227
13228
13229
13230
13231
13232
13233
13234
13235
13236
13237
13238
13239
13240
13241
13242
13243
13244
13245
13246
13247
13248
13249
13250
13251
13252
13253
13254
13255
13256
13257
13258
13259
13260
13261
13262
13263
13264
13265
13266
13267
13268
13269
13270
13271
13272
13273
13274
13275
13276
13277
13278
13279
13280
13281
13282
13283
13284
13285
13286
13287
13288
13289
13290
13291
13292
13293
13294
13295
13296
13297
13298
13299
13300
13301
13302
13303
13304
13305
13306
13307
13308
13309
13310
13311
13312
13313
13314
13315
13316
13317
13318
13319
13320
13321
13322
13323
13324
13325
13326
13327
13328
13329
13330
13331
13332
13333
13334
13335
13336
13337
13338
13339
13340
13341
13342
13343
13344
13345
13346
13347
13348
13349
13350
13351
13352
13353
13354
13355
13356
13357
13358
13359
13360
13361
13362
13363
13364
13365
13366
13367
13368
13369
13370
13371
13372
13373
13374
13375
13376
13377
13378
13379
13380
13381
13382
13383
13384
13385
13386
13387
13388
13389
13390
13391
13392
13393
13394
13395
13396
13397
13398
13399
13400
13401
13402
13403
13404
13405
13406
13407
13408
13409
13410
13411
13412
13413
13414
13415
13416
13417
13418
13419
13420
13421
13422
13423
13424
13425
13426
13427
13428
13429
13430
13431
13432
13433
13434
13435
13436
13437
13438
13439
13440
13441
13442
13443
13444
13445
13446
13447
13448
13449
13450
13451
13452
13453
13454
13455
13456
13457
13458
13459
13460
13461
13462
13463
13464
13465
13466
13467
13468
13469
13470
13471
13472
13473
13474
13475
13476
13477
13478
13479
13480
13481
13482
13483
13484
13485
13486
13487
13488
13489
13490
13491
13492
13493
13494
13495
13496
13497
13498
13499
13500
13501
13502
13503
13504
13505
13506
13507
13508
13509
13510
13511
13512
13513
13514
13515
13516
13517
13518
13519
13520
13521
13522
13523
13524
13525
//! Tests for the MIR frontend.
//!
//! Run with a nightly compiler available:
//!
//! ```text
//! VERIF_RUSTC=$(rustup which --toolchain nightly rustc) \
//!   cargo test --features std --test mir
//! ```
//!
//! The tests that need `rustc` resolve it from `VERIF_RUSTC`, or fall back to the
//! `rustc` on `PATH`, and skip themselves only when there is no compiler at all.
//!
//! The fallback matters because a test that returns early reports `ok` while
//! checking nothing, so the suite's verdict would otherwise depend on the
//! environment: `let Some(report) = verify_all() else { return }` passes whether
//! the MIR frontend ran or not. `--emit=mir` is a *stable* flag (only
//! `-Zunpretty=mir` needs a nightly), so the `rustc` cargo already uses is enough,
//! and the fallback makes the ~21 `else { return }` tests run on any machine that
//! can build the crate. See the `U5` item in `ROADMAP.md`.
//!
//! # The oracle
//!
//! The strongest test here is [`counterexamples_really_panic`]. It takes each
//! counterexample the verifier produced, feeds those exact inputs to the *real*
//! function, and requires it to panic. That is a differential check against Rust
//! itself rather than against another model: if the engine reported a
//! counterexample that does not actually panic, the test fails. It is the same
//! discipline as the Z3 and Kani oracles elsewhere in this crate, with `rustc`'s
//! own code generator as the reference.
//!
//! # What is covered where
//!
//! The IR-level checks live in `src/mir/parse.rs` and `src/mir/exec.rs` as unit
//! tests, because the IR is crate-internal. This file covers the public surface:
//! the contract scanner, contract evaluation, and the end-to-end verdicts.

use verif::mir::spec;
use verif::mir::verify::{self, FnOutcome};

// The functions under verification, compiled from the exact file the frontend
// reads. `#[path]` rather than a copy means the test and the verifier can never
// drift: the same bytes are both verified and executed.
#[path = "../examples/mir_targets/actuator.rs"]
mod actuator;
use actuator::*;

const TARGET: &str = "examples/mir_targets/actuator.rs";

/// The compiler to read MIR from, or `None` so the test can skip.
///
/// `VERIF_RUSTC` wins when it is set (CI points it at a nightly, so the dump
/// format is exercised on the bleeding edge). Otherwise the `rustc` on `PATH` is
/// used, because `--emit=mir` is a stable flag and a test that silently checks
/// nothing is worse than one that runs on a stable compiler. The skip is now
/// reserved for a machine with no `rustc` at all, which cannot build this crate.
fn rustc() -> Option<String> {
    if let Some(p) = std::env::var("VERIF_RUSTC").ok().filter(|s| !s.is_empty()) {
        return Some(p);
    }
    let out = std::process::Command::new("rustc").arg("--version").output().ok()?;
    if !out.status.success() {
        return None;
    }
    Some("rustc".to_string())
}

fn verify_all() -> Option<verify::FileReport> {
    let rustc = rustc()?;
    let only: Vec<String> = [
        "clamp",
        "rate_limit",
        "sat_add",
        "sat_sub",
        "shift_const",
        "shift_left",
        "sat_shift",
        "sat_mul",
        "unchecked_add",
        "unchecked_sub",
        "unchecked_div",
        "unchecked_mul",
        "off_by_one_guard",
        "clamp_signed",
        "checked_div_signed",
        "unchecked_div_signed",
        "signed_add_misguarded",
        "signed_add_guarded",
        "signed_mul_guarded",
        "signed_mul_negative",
        "checked_neg",
        "inline_saves_arg",
        "inline_clobber_would_pass",
        "bool_ret_false",
        "bool_ret_true",
        "nested_parens",
        "vacuous_precondition",
        "add_always_overflows",
        "div_by_always_zero",
        "clamp_bounds",
        "first_of",
        "slice_alias_tripwire",
        "assert_guarded",
        "assert_misguarded",
        "panic_guarded",
    ]
    .iter()
    .map(|s| s.to_string())
    .collect();
    match verify::verify_file_with(&rustc, std::path::Path::new(TARGET), &only) {
        Ok(r) => Some(r),
        Err(e) => panic!("frontend failed: {e}"),
    }
}

fn outcome<'a>(report: &'a verify::FileReport, name: &str) -> &'a FnOutcome {
    report
        .outcomes
        .iter()
        .find(|(n, _)| n == name)
        .map(|(_, o)| o)
        .unwrap_or_else(|| panic!("{name} missing from the report"))
}

/// Assert a function is *refuted* -- `Failed`, with a counterexample.
///
/// `!o.is_verified()` accepts `Skipped` and `Incomplete` too, so it does not
/// establish that a false postcondition produced a counterexample, which is the
/// only thing a tripwire is for: a tripwire that "passes" because the function
/// was skipped has never been pulled. This requires the refutation itself. A
/// shape that is *honestly* undecided must be asserted `!is_verified()` on
/// purpose, with a comment saying so.
fn assert_refuted(report: &verify::FileReport, name: &str) {
    let o = outcome(report, name);
    assert!(
        matches!(o, FnOutcome::Failed { .. }),
        "`{name}` must be refuted with a counterexample, got: {o}"
    );
}

// ---------------------------------------------------------------------------
// The contract scanner
// ---------------------------------------------------------------------------

#[test]
fn scanner_reads_contracts_and_ignores_inner_docs() {
    let src = r#"
//! requires: this is documentation, not a contract
//! ensures: and neither is this

/// requires: b != 0
/// ensures: ret <= a
pub fn div(a: u32, b: u32) -> u32 {
    a / b
}
"#;
    let specs = spec::extract(src);
    let c = specs.get("div").expect("div should have a contract");
    assert_eq!(c.requires, vec!["b != 0".to_string()]);
    assert_eq!(c.ensures, vec!["ret <= a".to_string()]);
}

#[test]
fn scanner_does_not_leak_a_contract_onto_a_later_function() {
    let src = r#"
/// requires: a > 0
const LIMIT: u32 = 10;

pub fn unrelated(a: u32) -> u32 { a }
"#;
    let specs = spec::extract(src);
    assert!(
        specs.get("unrelated").is_none(),
        "a contract before a non-fn item must not attach to the next fn"
    );
}

#[test]
fn scanner_finds_a_pub_crate_fn() {
    let src = "/// requires: a > 0\npub(crate) fn f(a: u32) -> u32 { a }\n";
    let specs = spec::extract(src);
    assert!(specs.get("f").is_some(), "`pub(crate) fn` must be recognized");
}

// ---------------------------------------------------------------------------
// Contract evaluation
// ---------------------------------------------------------------------------

#[test]
fn contract_expressions_evaluate() {
    use verif::smt;
    smt::with_fresh(|| {
        let mut args = std::collections::BTreeMap::new();
        args.insert("b".to_string(), smt::var_named(2, 32));
        let p = spec::eval_requires("b != 0", &args, &std::collections::BTreeSet::new(), &spec::FieldEnv::default()).expect("should parse");
        // `b != 0` is satisfiable but not valid: some b violates it.
        assert!(!smt::prove(p).is_proved());
    });
}

#[test]
fn a_contradictory_contract_is_refused_or_vacuous() {
    use verif::smt;
    smt::with_fresh(|| {
        let mut args = std::collections::BTreeMap::new();
        args.insert("b".to_string(), smt::var_named(2, 32));
        // `b < 3 && b > 10` describes no input. The parser should read it, and
        // the engine should say the precondition is unsatisfiable.
        let p = spec::eval_requires("b < 3 && b > 10", &args, &std::collections::BTreeSet::new(), &spec::FieldEnv::default()).expect("should parse");
        assert_eq!(smt::find_model(p), smt::Verdict::Unsat);
    });
}

#[test]
fn refuses_a_contract_it_cannot_read() {
    use verif::smt;
    smt::with_fresh(|| {
        let args = std::collections::BTreeMap::new();
        // A function call is not in the grammar, so it must be refused rather
        // than approximated.
        assert!(spec::eval_requires("foo(b) < 3", &args, &std::collections::BTreeSet::new(), &spec::FieldEnv::default()).is_err());
        // A cast is not in the grammar either.
        assert!(spec::eval_requires("b as u64 < 3", &args, &std::collections::BTreeSet::new(), &spec::FieldEnv::default()).is_err());
        // An unknown name is not an argument.
        assert!(spec::eval_requires("zzz < 3", &args, &std::collections::BTreeSet::new(), &spec::FieldEnv::default()).is_err());
    });
}

/// A literal takes its width from the operand it meets, not from the widest
/// name in scope.
///
/// This is the bug that array support exposed. A slice or array length is a
/// `usize`, so once such an argument is in scope it becomes the widest name, and
/// a contract literal was sized from that maximum. `ret <= 255` for a `u8` return
/// then became an 8-bit value compared against a 64-bit constant, which the
/// engine reports as ill-formed -- and an ill-formed term makes *every*
/// obligation in the run `Unknown`, so the whole function was `incomplete` for a
/// reason that had nothing to do with its body.
///
/// The check is on the evaluator, so it does not depend on a corpus function
/// happening to be skipped for another reason. `n` stands in for the wide
/// length, `ret` for the narrow return: the literal must end up 8 bits wide.
#[test]
fn a_literal_is_sized_from_the_operand_it_meets() {
    use verif::smt;
    smt::with_fresh(|| {
        let mut args = std::collections::BTreeMap::new();
        // A 64-bit "length", in scope but not mentioned by the contract.
        args.insert("xs__len".to_string(), smt::var(64));
        let ret = smt::var(8);
        let p = spec::eval_ensures(
            "ret <= 255",
            &args,
            Some(ret),
            spec::RET_IS_UNKNOWN,
            &std::collections::BTreeSet::new(),
            false,
            &spec::FieldEnv::default(),
            &std::collections::BTreeMap::new(),
        )
        .expect("a narrow literal against a wide name must still parse");
        assert!(
            !smt::is_ill_formed(),
            "the literal must be resized to `ret`'s 8 bits, not left at 64"
        );
        assert!(smt::prove(p).is_proved(), "`ret <= 255` must hold for every u8");
    });
}

/// A literal too large for the operand's width is refused, not truncated.
///
/// `a <= 300` for a `u8` cannot hold, so the honest answers are "refused" or
/// "unsatisfiable". What it must *not* be is silently narrowed to `a <= 44`,
/// which is what a truncating resize would do -- and that is a weaker claim that
/// could pass. The refusal is the error, and it names the width so the message is
/// actionable.
#[test]
fn a_literal_that_does_not_fit_is_refused_not_truncated() {
    use verif::smt;
    smt::with_fresh(|| {
        let mut args = std::collections::BTreeMap::new();
        args.insert("a".to_string(), smt::var(8));
        let err = spec::eval_requires(
            "a <= 300",
            &args,
            &std::collections::BTreeSet::new(),
            &spec::FieldEnv::default(),
        )
        .expect_err("300 does not fit a u8, so the contract must be refused");
        assert!(
            err.to_string().contains("300") && err.to_string().contains("8-bit"),
            "the refusal must name the value and the width, got: {err}"
        );
    });
}

/// An aggregate return refuses a bare `ret` with the aggregate message.
///
/// `ret <= 5` on a `(u8, u8)` return is refused because there is no single word.
/// The message must say *that*, so the reader knows to name a component.
#[test]
fn a_bare_ret_on_an_aggregate_names_the_aggregate_reason() {
    use verif::smt;
    smt::with_fresh(|| {
        let args = std::collections::BTreeMap::new();
        let err = spec::eval_ensures(
            "ret <= 5",
            &args,
            None,
            spec::RET_IS_AGGREGATE,
            &std::collections::BTreeSet::new(),
            false,
            &spec::FieldEnv::default(),
            &std::collections::BTreeMap::new(),
        )
        .expect_err("a bare `ret` on an aggregate must be refused");
        assert!(
            err.to_string().contains(spec::RET_IS_AGGREGATE),
            "the message must name the aggregate cause and the fix, got: {err}"
        );
    });
}

/// A scalar return whose value is unknown is refused with *that* reason.
///
/// The value can be unbound on a path where a read was unmodelled, even though
/// the return type is a plain `u8`. Reporting "the return type is an aggregate"
/// here would be false, and would send the reader looking for a tuple that does
/// not exist. The two causes have to be two messages.
#[test]
fn a_bare_ret_with_an_unknown_value_names_the_unknown_reason() {
    use verif::smt;
    smt::with_fresh(|| {
        let args = std::collections::BTreeMap::new();
        let err = spec::eval_ensures(
            "ret <= 5",
            &args,
            None,
            spec::RET_IS_UNKNOWN,
            &std::collections::BTreeSet::new(),
            false,
            &spec::FieldEnv::default(),
            &std::collections::BTreeMap::new(),
        )
        .expect_err("a bare `ret` with no value must be refused");
        assert!(
            err.to_string().contains(spec::RET_IS_UNKNOWN),
            "a scalar return must not be reported as an aggregate, got: {err}"
        );
    });
}

/// A literal in *arithmetic* is sized to the operand it meets, like one in a
/// comparison.
///
/// `add_literal_wide_scope` has a slice length in scope, so a literal built at
/// the scope's 64-bit width would make `a + 5` an 8-bit/64-bit `Add` -- ill-formed,
/// and so `Unknown` for the whole run. The fix is the same `align_widths` the
/// comparison path uses, reached through the additive and multiplicative operators.
#[test]
fn a_literal_in_arithmetic_is_sized_to_its_operand() {
    let Some(report) = verify_realistic(&["add_literal_wide_scope"]) else { return };
    let o = outcome(&report, "add_literal_wide_scope");
    assert!(o.is_verified(), "`add_literal_wide_scope` must verify, got: {o}");
}

/// A named type bound carries the width of the type it names.
///
/// `i8::MIN` is the bit pattern `0x80`, exactly 8 bits. Building it at the
/// widest name in scope and narrowing it back would compare a 64-bit `128`
/// against the signed 8-bit range and refuse it -- a refusal of a bound that is
/// in range by construction. The width has to come from `i8`, not from the
/// contract's other operands.
#[test]
fn a_named_type_bound_keeps_its_own_width() {
    use verif::smt;
    smt::with_fresh(|| {
        let mut args = std::collections::BTreeMap::new();
        args.insert("a".to_string(), smt::var(8));
        // A 64-bit name in scope, which is what used to widen the bound.
        args.insert("xs__len".to_string(), smt::var(64));
        let mut signed = std::collections::BTreeSet::new();
        signed.insert("a".to_string());
        let p = spec::eval_requires("a >= i8::MIN", &args, &signed, &spec::FieldEnv::default())
            .expect("`a >= i8::MIN` must parse even with a wide name in scope");
        assert!(!smt::is_ill_formed(), "the bound must be built at 8 bits");
        // Every `i8` is `>= i8::MIN`, so the precondition holds on all inputs.
        assert!(smt::prove(p).is_proved(), "`a >= i8::MIN` must hold for every i8");
    });
}

/// A negative literal keeps its value when it is widened to a signed operand.
///
/// `a >= -10` for an `i8` is the shape that makes sign extension matter: the
/// literal is parsed as `0 - 10` at some width and then has to reach `a`'s width
/// with the *value* preserved. Zero-extending it would turn `-10` into a large
/// positive number and the comparison would mean something else entirely.
#[test]
fn a_negative_literal_is_sign_extended_to_the_operand() {
    use verif::smt;
    smt::with_fresh(|| {
        let mut args = std::collections::BTreeMap::new();
        args.insert("a".to_string(), smt::var(8));
        let mut signed = std::collections::BTreeSet::new();
        signed.insert("a".to_string());
        let p = spec::eval_requires("a >= -10", &args, &signed, &spec::FieldEnv::default())
            .expect("`a >= -10` must parse");
        assert!(!smt::is_ill_formed(), "the literal must be resized to 8 bits");
        // `a >= -10` is the negation of `a < -10`, and for an `i8` the only such
        // `a` are -128..=-11. So the contract must *fail* on those inputs.
        // `-10` as an `i8` is the bit pattern `0xF6`; building it from a `u64`
        // would itself be ill-formed, since `2^64 - 10` does not fit 8 bits.
        let minus_ten = smt::word(0xF6, 8);
        let bad = smt::and(p, smt::slt(args["a"], minus_ten));
        assert_eq!(
            smt::find_model(bad),
            smt::Verdict::Unsat,
            "`a >= -10` must exclude exactly `a < -10`"
        );
    });
    smt::with_fresh(|| {
        // The same contract with a 64-bit name also in scope. `-10` is built at
        // 64 bits as `2^64 - 10`, and narrowing that to 8 bits must take the low
        // byte (`0xF6`) rather than rebuild a word from the 64-bit value, which
        // would not fit and would set the ill-formed flag.
        let mut args = std::collections::BTreeMap::new();
        args.insert("a".to_string(), smt::var(8));
        args.insert("xs__len".to_string(), smt::var(64));
        let mut signed = std::collections::BTreeSet::new();
        signed.insert("a".to_string());
        let p = spec::eval_requires("a >= -10", &args, &signed, &spec::FieldEnv::default())
            .expect("`a >= -10` must parse with a wide name in scope");
        assert!(
            !smt::is_ill_formed(),
            "narrowing a negative literal must take the low bits, not rebuild a wide word"
        );
        // And it must still mean what it says: satisfiable (an `i8` of `-10`
        // satisfies it), but not valid (every `i8` below `-10` violates it).
        assert!(
            !matches!(smt::find_model(p), smt::Verdict::Unsat),
            "`a >= -10` must be satisfiable"
        );
        assert!(!smt::prove(p).is_proved(), "`a >= -10` must not hold for every i8");
    });
}

// ---------------------------------------------------------------------------
// End-to-end verification, with rustc
// ---------------------------------------------------------------------------

#[test]
fn guarded_functions_verify() {
    let Some(report) = verify_all() else { return };
    for name in [
        "clamp",
        "rate_limit",
        "sat_add",
        "sat_sub",
        // The three shift shapes: a constant amount, a guarded variable amount,
        // and an unguarded variable amount (which rustc's own `assert(n < 16)`
        // rejects, so it belongs with the refuted functions rather than here).
        "shift_const",
        "sat_shift",
        "clamp_bounds",
    ] {
        let o = outcome(&report, name);
        assert!(
            o.is_verified(),
            "{name} should verify, got: {o}\n{}",
            report_source_hint()
        );
    }
}

#[test]
fn unchecked_functions_are_refuted() {
    let Some(report) = verify_all() else { return };
    for name in [
        "unchecked_add",
        "unchecked_sub",
        "unchecked_div",
        "unchecked_mul",
        "shift_left",
    ] {
        let o = outcome(&report, name);
        assert!(
            matches!(o, FnOutcome::Failed { .. }),
            "{name} must be refuted, got: {o}"
        );
    }
}

/// The load-bearing test: every counterexample must be real.
///
/// A counterexample is the verifier's evidence. If it does not reproduce, the
/// engine is wrong in the worst direction — it reported a defect that is not
/// there — so this replays each one against the real function and requires the
/// panic.
#[test]
fn counterexamples_really_panic() {
    let Some(report) = verify_all() else { return };
    let prev = std::panic::take_hook();
    std::panic::set_hook(Box::new(|_| {}));
    let mut checked = 0;
    let mut failures: Vec<String> = Vec::new();
    for name in [
        "unchecked_add",
        "unchecked_sub",
        "unchecked_div",
        "unchecked_mul",
        // The variable shift is modelled now, so its counterexample is a real
        // one: `n == 16` is exactly what rustc's own `assert(n < 16)` rejects.
        "shift_left",
        // The signed cases. These are the ones that would have slipped through
        // while comparisons and overflow tests were unsigned-only, so replaying
        // their witnesses against the real functions is what keeps the fix
        // honest.
        "unchecked_div_signed",
        "signed_add_misguarded",
        "signed_mul_negative",
    ] {
        let o = outcome(&report, name);
        let FnOutcome::Failed { detail, .. } = o else {
            continue;
        };
        // The arguments are locals `_1` and `_2`, and the report names them by
        // their source `debug` names, so the helper is told what to look for. A
        // model names only the variables it had to constrain -- `unchecked_div`
        // reports `d = 0` and says nothing about the dividend, because the divisor
        // alone decides -- so a missing variable is *unconstrained* and any value
        // will do. Zero is used, and the panic is still required: that is what
        // makes the witness a witness rather than a coincidence of one chosen
        // value.
        let names = match name {
            "unchecked_div" => ("n", "d"),
            "shift_left" => ("x", "n"),
            _ => ("a", "b"),
        };
        let (a, b) = parse_cex_args(detail, names);
        let a = a.unwrap_or(0);
        let b = b.unwrap_or(0);
        let panicked = match name {
            "unchecked_add" => std::panic::catch_unwind(|| unchecked_add(a as u16, b as u16)).is_err(),
            "unchecked_sub" => std::panic::catch_unwind(|| unchecked_sub(a as u16, b as u16)).is_err(),
            "unchecked_div" => std::panic::catch_unwind(|| unchecked_div(a as u32, b as u32)).is_err(),
            "unchecked_mul" => std::panic::catch_unwind(|| unchecked_mul(a as u8, b as u8)).is_err(),
            "shift_left" => std::panic::catch_unwind(|| shift_left(a as u16, b as u16)).is_err(),
            "unchecked_div_signed" => {
                std::panic::catch_unwind(|| unchecked_div_signed(a as i16, b as i16)).is_err()
            }
            "signed_add_misguarded" => {
                std::panic::catch_unwind(|| signed_add_misguarded(a as i8, b as i8)).is_err()
            }
            "signed_mul_negative" => {
                std::panic::catch_unwind(|| signed_mul_negative(a as i8, b as i8)).is_err()
            }
            _ => unreachable!(),
        };
        if !panicked {
            failures.push(format!(
                "{name}: counterexample a={a} b={b} did not actually panic, \
                 so the engine reported a defect that is not there"
            ));
        }
        checked += 1;
    }
    // Restore the real hook *before* asserting, or the failure message is
    // swallowed by the no-op hook and the test just says "FAILED".
    std::panic::set_hook(prev);
    assert!(failures.is_empty(), "{}", failures.join("\n"));
    assert!(checked >= 3, "expected several counterexamples, checked {checked}");
}

/// A wrong `ensures` is caught even when the function cannot panic.
///
/// `off_by_one_guard` guards its `a + b` correctly, so it is panic-free; the bug
/// is that it claims `ret >= a` and the `else` branch returns `0`. This is the
/// case that separates "no overflow" from "meets its contract", and it is why the
/// postcondition check exists rather than being folded into the panic check.
#[test]
fn a_wrong_postcondition_is_caught() {
    let Some(report) = verify_all() else { return };
    let o = outcome(&report, "off_by_one_guard");
    match o {
        FnOutcome::Failed { kind, detail } => {
            // `kind` says which obligation failed; `detail` is only the witness.
            assert!(
                kind.contains("postcondition"),
                "the failing obligation should be the postcondition, got kind `{kind}`"
            );
            // The witness must be a real input: `32768 + 32768` has the guard
            // take the `else` branch and return 0, which is not `>= a`. It must
            // also be *readable*: the report names the arguments, not `v1`/`v2`.
            assert!(
                detail.contains("a =") && detail.contains("b ="),
                "the witness should name both arguments by their source names, got: {detail}"
            );
            assert!(
                !detail.contains("v1:"),
                "the witness must not fall back to `v1:` for a named argument, got: {detail}"
            );
        }
        other => panic!("a wrong postcondition must be refuted, got: {other}"),
    }
}

/// U4: a counterexample is rendered with the source names, not internal indices.
///
/// A report that says `v1:u8 = 224` makes the reader decode the MIR's local
/// numbering; `x = 224` is what the source wrote. The names are the `debug`
/// bindings `rustc` records, which the frontend already keeps in `local_names`.
///
/// The teeth are the *fallback*: a variable with no source name must keep its
/// `v{index}:u{width}` spelling, so naming is a readability improvement and never
/// a loss of information. `unchecked_div` is the shape that exercises both at
/// once -- its dividend is a named argument, and the model constrains only the
/// divisor, so the witness names one variable and drops the other.
#[test]
fn a_counterexample_is_rendered_with_source_names() {
    let Some(report) = verify_all() else { return };

    // Every refutation in the corpus names its arguments by source name, and none
    // leaks a `v1:`/`v2:` spelling for a variable that *has* a source name.
    // `unchecked_div` constrains only the divisor, so its witness names `d` and
    // drops the dividend -- which is the point: a variable the model did not need
    // is *unconstrained*, and naming is not the same as requiring.
    for (name, expected) in [
        ("unchecked_add", &["a = ", "b = "][..]),
        ("unchecked_div", &["d = "][..]),
        ("shift_left", &["n = "][..]),
        ("signed_mul_negative", &["a = ", "b = "][..]),
    ] {
        let o = outcome(&report, name);
        let FnOutcome::Failed { detail, .. } = o else {
            panic!("{name} must be refuted, got: {o}");
        };
        for want in expected {
            assert!(
                detail.contains(want),
                "{name}: the witness should name its argument `{want}`, got: {detail}"
            );
        }
        assert!(
            !detail.contains("v1:") && !detail.contains("v2:"),
            "{name}: a named argument must not fall back to `vN:`, got: {detail}"
        );
    }

    // The positive control for the fallback: a variable the source did not name
    // still renders as `v{index}:u{width}`. This is a bare arithmetic function
    // with no `debug`-named arguments beyond the parameters, so the check is that
    // the *parameters* are named while nothing regresses to the old form for the
    // variables the model actually touched.
    let o = outcome(&report, "off_by_one_guard");
    let FnOutcome::Failed { detail, .. } = o else {
        panic!("off_by_one_guard must be refuted, got: {o}");
    };
    assert!(
        detail.contains("a = 32768") && detail.contains("b = 32768"),
        "the guard's witness must name both arguments, got: {detail}"
    );

    // The unnamed *assignment* itself, not only its absence. A witness whose
    // constrained variable has no `debug` name (a temporary, a promoted constant,
    // an aggregate part) must still be rendered, as `v{index}:u{width}`. Dropping
    // unnamed assignments would silently shrink a witness to the named subset --
    // and every check above would still pass, because they assert named names are
    // *present* and unnamed names are *absent from named functions*, never that an
    // unnamed one appears at all. `a_field_array_write_is_not_the_seed` (in the
    // realistic corpus) is the shape that produces one: its only constrained
    // variable is a temporary, so its witness is entirely unnamed.
    let Some(report) = verify_realistic(&["a_field_array_write_is_not_the_seed"]) else {
        return;
    };
    let o = outcome(&report, "a_field_array_write_is_not_the_seed");
    let FnOutcome::Failed { detail, .. } = o else {
        panic!("a_field_array_write_is_not_the_seed must be refuted, got: {o}");
    };
    assert!(
        detail.contains("v") && detail.contains(":u8 = "),
        "a variable with no source name must still render as `vN:uW = value`, got: {detail}"
    );
}

/// The other direction of the oracle: a function reported panic-free must not
/// panic on concrete inputs.
#[test]
fn verified_functions_do_not_panic_concretely() {
    let Some(report) = verify_all() else { return };
    if !outcome(&report, "sat_add").is_verified() {
        return;
    }
    let prev = std::panic::take_hook();
    std::panic::set_hook(Box::new(|_| {}));
    let mut violations = Vec::new();
    // `sat_add` is verified for all u16, so every u16 pair must be fine. The full
    // cross product is 2^32, so this sweeps the overflow boundary plus a spread
    // of interior values.
    let mut vals: Vec<u16> = vec![0, 1, 2, 100, 32767, 32768, 65534, 65535];
    for i in 0..40u32 {
        vals.push((i.wrapping_mul(1657)) as u16);
    }
    for &a in &vals {
        for &b in &vals {
            if std::panic::catch_unwind(|| sat_add(a, b)).is_err() {
                violations.push((a, b));
            }
        }
    }
    std::panic::set_hook(prev);
    assert!(
        violations.is_empty(),
        "sat_add was verified but panics on {violations:?}"
    );
}

#[test]
fn sat_mul_is_verified_by_the_exact_division_overflow_test() {
    let Some(report) = verify_all() else { return };
    let o = outcome(&report, "sat_mul");
    // `sat_mul` guards `a * b` with `b != 0 && a > u16::MAX / b`. That guard *is* the
    // exact unsigned multiply-overflow predicate, so the path into the multiply
    // refutes the overflow obligation -- no 32-bit product is built, and the widened
    // product that used to be the documented BDD wall is never posed.
    //
    // This was `Incomplete` before, for a reason worth keeping visible: the predicate
    // used to be `x > MAX / c` only for a *constant* operand, and the general path
    // widened to `2w` bits and refused past 16. The two obligations were then
    // `b == 0 && a <= MAX / b` (path) against `b != 0 && a > MAX / b` (overflow), which
    // cancel pairwise but only *across* two `&&` spines -- so the boolean fold has to
    // see through the spines for the query to close. Both halves are what this test
    // pins: the exact division predicate, and the spine-aware cancellation that lets it
    // fold against the guard.
    assert!(
        o.is_verified(),
        "sat_mul must be verified now that the multiply-overflow test is exact: {o}"
    );
}

/// The signed/unsigned split, pinned so it cannot regress.
///
/// While comparisons and overflow tests were unsigned-only, every case here
/// produced the wrong answer and every *unsigned* case above still passed — the
/// whole suite was green while the engine called `127 + 1` panic-free. These
/// assertions are what would have caught it.
#[test]
fn signed_arithmetic_is_not_verified_as_unsigned() {
    let Some(report) = verify_all() else { return };

    // `signed_add_misguarded` has `requires: a <= 127 && b <= 1` and adds. The
    // unsigned overflow test calls it safe because the wrapped sum `128u8` is not
    // below `127u8`; the correct signed rule fires. It must be a *failure*, and
    // it must not be reported verified.
    let o = outcome(&report, "signed_add_misguarded");
    assert!(
        matches!(o, FnOutcome::Failed { .. }),
        "a signed add that overflows on `127 + 1` must be refuted, got: {o}"
    );

    // The guard that actually holds, so the fix is not "reject everything signed".
    for name in ["signed_add_guarded", "signed_mul_guarded", "clamp_signed", "checked_neg"] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "{name} should verify, got: {o}");
    }

    // A signed comparison must take the negative branch. `clamp_signed` cannot
    // verify at all if `x < lo` is read unsigned, because `-1` would look larger
    // than any positive bound — so its `Verified` above is the comparison check.
    // This asserts the same thing from the other side: the misguarded add fails,
    // which requires the *overflow* test to be signed-aware rather than the
    // comparison alone.
    let o = outcome(&report, "signed_mul_negative");
    assert!(
        matches!(o, FnOutcome::Failed { .. }),
        "a signed multiply of two negatives must be refuted, got: {o}"
    );
}

/// `i8::MIN` must parse as the top bit, not as zero.
///
/// `checked_neg`'s guard is `a != i8::MIN`. When the constant parser returned 0
/// for every `MIN`, that guard meant `a != 0`, so the `-128` case looked excluded
/// and the negation was reported panic-free. The function must verify *now* only
/// because the guard is real, and a version without it must fail.
#[test]
fn the_min_constant_is_the_top_bit() {
    let Some(report) = verify_all() else { return };
    let o = outcome(&report, "checked_neg");
    assert!(
        o.is_verified(),
        "checked_neg excludes i8::MIN and should verify, got: {o}"
    );
    // Replay the guard's own boundary against the real function: `-128` really
    // does panic under debug overflow checks, which is why excluding it matters.
    let prev = std::panic::take_hook();
    std::panic::set_hook(Box::new(|_| {}));
    let panics = std::panic::catch_unwind(|| {
        // `black_box` keeps the constant from being folded, so the negation is
        // computed at runtime and its overflow check is the one being tested.
        let a: i8 = std::hint::black_box(i8::MIN);
        -a
    })
    .is_err();
    std::panic::set_hook(prev);
    assert!(panics, "`-i8::MIN` must panic, so the guard is load-bearing");
}

/// The other direction for signed code: a function reported panic-free must not
/// panic on any input it admits.
#[test]
fn verified_signed_functions_do_not_panic_concretely() {
    let Some(report) = verify_all() else { return };
    if !outcome(&report, "signed_add_guarded").is_verified() {
        return;
    }
    let prev = std::panic::take_hook();
    std::panic::set_hook(Box::new(|_| {}));
    let mut violations = Vec::new();
    // The guard admits only `|a|, |b| <= 60`, so sweep that box exhaustively.
    for a in -60i16..=60 {
        for b in -60i16..=60 {
            if std::panic::catch_unwind(|| signed_add_guarded(a as i8, b as i8)).is_err() {
                violations.push((a, b));
            }
        }
    }
    std::panic::set_hook(prev);
    assert!(
        violations.is_empty(),
        "signed_add_guarded was verified but panics on {violations:?}"
    );
}

/// A contract naming a function that is not in the MIR must be reported, not
/// silently ignored — otherwise a typo turns a verified function into an
/// unverified one that still looks green.
#[test]
fn a_missing_contracted_function_is_reported() {
    let Some(rustc) = rustc() else { return };
    let only = vec!["no_such_function".to_string()];
    let report = verify::verify_file_with(&rustc, std::path::Path::new(TARGET), &only)
        .expect("the file itself should still parse");
    assert!(
        report.outcomes.is_empty(),
        "nothing should have been verified for an absent function"
    );
}

// ---------------------------------------------------------------------------
// The safety gate itself, with no compiler needed
// ---------------------------------------------------------------------------

#[test]
fn a_report_with_an_unknown_path_is_never_clean() {
    // The API-level invariant this whole frontend rests on: an incomplete run is
    // not a pass.
    let mut report = verify::FileReport::default();
    report.outcomes.push((
        "f".to_string(),
        FnOutcome::Incomplete {
            report: Box::new(verify::MirReport::default()),
            reasons: vec!["budget".to_string()],
        },
    ));
    assert!(!report.is_clean());
    assert_eq!(report.incomplete(), 1);
    assert_eq!(report.verified(), 0);
}

#[test]
fn a_missing_contract_is_not_clean() {
    let mut report = verify::FileReport::default();
    report.missing.push("ghost".to_string());
    assert!(!report.is_clean());
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

fn report_source_hint() -> &'static str {
    "hint: the frontend reads the MIR of examples/mir_targets/actuator.rs; \
     run examples/mir_demo.rs to see the per-function report"
}

/// Pull the values of a function's two arguments out of a rendered counterexample.
///
/// A witness is rendered with the source names the MIR `debug` bindings recorded
/// (`a = 128, b = 128`), so the argument names are passed in. A variable with no
/// source name keeps its `v{index}:u{width}` spelling, and the arguments here are
/// always locals `_1` and `_2`, so the fallback keys `v1:` / `v2:` are accepted
/// too -- the helper reads a named or an unnamed witness alike.
fn parse_cex_args(detail: &str, names: (&str, &str)) -> (Option<u64>, Option<u64>) {
    let mut first = None;
    let mut second = None;
    for part in detail.split(',') {
        let part = part.trim();
        let Some((lhs, rhs)) = part.split_once('=') else {
            continue;
        };
        let lhs = lhs.trim();
        let val: u64 = rhs.trim().parse().unwrap_or(0);
        if lhs == names.0 || lhs.starts_with("v1:") {
            first = Some(val);
        } else if lhs == names.1 || lhs.starts_with("v2:") {
            second = Some(val);
        }
    }
    (first, second)
}

// ---------------------------------------------------------------------------
// Regressions for the frontend bugs a review found: caller state clobbered by
// an inlined call, a boolean return bypassing the postcondition, a redundant
// parenthesis pair hiding a contract, and a scoped `debug` alias renaming a
// parameter.
// ---------------------------------------------------------------------------

/// An inlined call must not hand the caller the callee's locals.
///
/// Both functions have the same shape — save an argument, call something, return
/// the saved value — and differ only in the postcondition. The correct one must
/// verify and the false one must be refuted. If the caller's environment is not
/// restored, the continuation reads the callee's `_1` (holding `b`), so both
/// would verify.
#[test]
fn an_inlined_call_does_not_clobber_the_caller() {
    let Some(report) = verify_all() else { return };

    let good = outcome(&report, "inline_saves_arg");
    assert!(
        good.is_verified(),
        "returning the caller's saved argument must verify, got: {good}"
    );

    let bad = outcome(&report, "inline_clobber_would_pass");
    assert!(
        matches!(bad, FnOutcome::Failed { .. }),
        "`ret == b` is false for `inline_clobber_would_pass` and must be refuted, got: {bad}"
    );
}

/// A `bool` return must be checked against the declared postcondition.
#[test]
fn a_boolean_return_is_checked_against_its_postcondition() {
    let Some(report) = verify_all() else { return };

    let bad = outcome(&report, "bool_ret_false");
    assert!(
        matches!(bad, FnOutcome::Failed { .. }),
        "`ret == false` for a function returning `true` must be refuted, got: {bad}"
    );

    let good = outcome(&report, "bool_ret_true");
    assert!(
        good.is_verified(),
        "`ret == true` for the same function must verify, got: {good}"
    );
}

/// A redundant parenthesis pair must not make a contract unreadable.
#[test]
fn nested_predicate_parentheses_parse() {
    let Some(report) = verify_all() else { return };
    let o = outcome(&report, "nested_parens");
    assert!(
        o.is_verified(),
        "`requires: ((a == 0))` must parse and verify, got: {o}"
    );
}

/// A scoped `debug` alias must not rename a parameter.
///
/// `inline_saves_arg` prints `debug a => _1;` at function level and then
/// `debug saved => _1;` inside `scope 1`, because `let saved = a;` reuses `_1`.
/// When the later line overwrote the earlier one, the parameter `a` disappeared
/// and `ensures: ret == a` was skipped as unreadable. The function verifying at
/// all is the check; this pins the names directly so the failure is legible.
#[test]
fn a_scoped_debug_alias_does_not_rename_a_parameter() {
    let Some(rustc) = rustc() else { return };
    let mir = verify::emit_mir_with(&rustc, std::path::Path::new(TARGET))
        .expect("emit MIR for the target file");
    // The parameter is named `a`, and the `ensures` names it, so the contract
    // must be readable. A skip here is the bug.
    let report = verify::verify_file_with(&rustc, std::path::Path::new(TARGET), &[
        "inline_saves_arg".to_string(),
    ])
    .expect("verify");
    let (_, o) = report.outcomes.first().expect("one outcome");
    assert!(
        !matches!(o, FnOutcome::Skipped { .. }),
        "the contract names the parameter `a` and must be readable, got: {o}"
    );
    assert!(mir.contains("debug a => _1;"), "the parameter binding is expected in the dump");
}

/// An unreadable `ensures` must not be folded into `true`.
///
/// The postcondition closure used to swallow a parse error and return `true`, so
/// a function whose `ensures` could not be read would verify with *nothing*
/// checked. A skipped outcome is the honest report, and `is_clean` must not call
/// it clean.
#[test]
fn an_unreadable_ensures_is_reported_not_ignored() {
    let Some(rustc) = rustc() else { return };
    let dir = std::env::temp_dir().join("verif_unreadable_ensures");
    let _ = std::fs::create_dir_all(&dir);
    let src = dir.join("u.rs");
    std::fs::write(
        &src,
        "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nspec! {\n    requires: true,\n    ensures: ret == nonsense_name,\n}\npub fn f(a: u8) -> u8 { a }\n",
    )
    .expect("write source");
    let report =
        verify::verify_file_with_opts(&rustc, &src, &["f".to_string()], &probe_opts()).expect("verify");
    let (_, o) = report.outcomes.first().expect("one outcome");
    assert!(
        matches!(o, FnOutcome::Skipped { .. }),
        "an unreadable `ensures` must be reported as skipped, got: {o}"
    );
    assert!(
        !report.is_clean(),
        "a report with a skipped function must not be clean"
    );
}

/// An unsatisfiable precondition is a finding, not a pass.
///
/// The implication `pre => post` is vacuously true when `pre` is unsatisfiable,
/// so a naive discharge reports `Verified`. The frontend adds an explicit
/// satisfiability obligation for exactly this reason, and it must be a failure.
#[test]
fn an_unsatisfiable_precondition_is_a_failure() {
    let Some(report) = verify_all() else { return };
    let o = outcome(&report, "vacuous_precondition");
    assert!(
        matches!(o, FnOutcome::Failed { .. }),
        "`a < 3 && a > 10` admits no input and must be a failure, got: {o}"
    );
    let text = o.to_string();
    assert!(
        text.contains("vacuous precondition"),
        "the failure must name the vacuous precondition, got: {text}"
    );
}

/// A panic obligation whose verdict is `Valid` is a failure, not a proof.
///
/// This is the case that exposes the inverted polarity directly: the panic query
/// is `Valid`, and a naive "proved means good" reading counts it as discharged.
/// The obligation asks "can this panic happen", so `Valid` means it happens on
/// every input — the worst answer. `div_by_always_zero` divides by `d & 0`, which
/// is zero for every `d`, so its query is `Valid`.
#[test]
fn a_panic_obligation_that_is_valid_is_a_failure() {
    let Some(report) = verify_all() else { return };
    let o = outcome(&report, "div_by_always_zero");
    assert!(
        matches!(o, FnOutcome::Failed { .. }),
        "a divide by an always-zero divisor must be a failure, got: {o}"
    );
    assert!(
        o.to_string().contains("panic"),
        "the failure must name the panic, got: {o}"
    );
}

/// A panic that happens on *every* input must be a failure, not a proof.
///
/// The obligation for a panic asks "can this panic happen", so `Unsat` is the
/// good answer and `Valid` is the worst one — the panic fires for every input.
/// Reading the verdict as "proved means good" inverts the answer, and
/// `add_always_overflows` is the case that makes it visible: the overflow query
/// is `Valid`, and the function must be reported as failing.
#[test]
fn a_panic_on_every_input_is_a_failure() {
    let Some(report) = verify_all() else { return };
    let o = outcome(&report, "add_always_overflows");
    assert!(
        matches!(o, FnOutcome::Failed { .. }),
        "an add that always overflows must be a failure, got: {o}"
    );
}

/// An `Unsat` postcondition must not be described as a universal truth.
///
/// A postcondition query is `path => post`. `Unsat` means it is false on *every*
/// input -- the postcondition holds for none. A `Valid` panic query means the
/// opposite, that the failure fires on *every* input. Both leave no witness, and
/// the first version collapsed both to "holds for every input", which describes
/// an impossible postcondition as a universal truth. The two diagnoses must
/// differ, and the `Unsat` one must say the postcondition holds for no input.
#[test]
fn an_impossible_postcondition_says_so_rather_than_the_opposite() {
    let Some(report) = verify_all() else { return };
    let o = outcome(&report, "bool_ret_false");
    let FnOutcome::Failed { kind, detail } = o else {
        panic!("`bool_ret_false` must fail, got: {o}");
    };
    assert_eq!(kind, "postcondition");
    assert!(
        detail.contains("no input"),
        "an `Unsat` postcondition must be reported as holding for no input, got: {detail}"
    );
    assert!(
        !detail.contains("holds for every input"),
        "the diagnosis is reversed: an `Unsat` postcondition holds for none, got: {detail}"
    );
}

/// The counterpart: a panic query that is `Valid` fires on *every* input, and
/// its diagnosis must say that rather than borrowing the postcondition wording.
#[test]
fn an_always_firing_panic_says_it_fires_on_every_input() {
    let Some(report) = verify_all() else { return };
    let o = outcome(&report, "div_by_always_zero");
    let FnOutcome::Failed { kind, detail } = o else {
        panic!("`div_by_always_zero` must fail, got: {o}");
    };
    assert!(kind.contains("panic"), "the kind must name the panic, got: {kind}");
    assert!(
        detail.contains("every input"),
        "a `Valid` panic query fires on every input and must say so, got: {detail}"
    );
}

// ---------------------------------------------------------------------------
// Contract projections: struct fields and tuple components
// ---------------------------------------------------------------------------

const REALISTIC: &str = "examples/mir_targets/realistic.rs";

/// Deliberately runs on the *default* test-thread stack (2 MiB), not a worker with
/// a larger one.
///
/// The corpus contains a nested shift amount (`x >> ((2 << 7) + 1)`), and the
/// parser recurses once per nesting level, so this call is sensitive to the size of
/// a parser frame. It used to abort here: `smt::evaluate` owns a 12 KiB memo as a
/// stack local, the optimiser inlined it into the recursive parser, and every level
/// of nesting then cost 12 KiB of stack. The fix is `#[inline(never)]` on
/// `evaluate` (see its doc comment), which brings the parse depth to ~15 KiB. This
/// test is the *gate* on that: reverting the attribute re-inflates the frames and
/// this aborts, so the fix cannot be quietly undone. A 64 MiB worker thread here
/// would have hidden the bug instead of catching it.
fn verify_realistic(only: &[&str]) -> Option<verify::FileReport> {
    let rustc = rustc()?;
    let only: Vec<String> = only.iter().map(|s| s.to_string()).collect();
    match verify::verify_file_with(&rustc, std::path::Path::new(REALISTIC), &only) {
        Ok(r) => Some(r),
        Err(e) => panic!("frontend failed: {e}"),
    }
}

/// The independent corpus, scored, so the number in `ROADMAP.md` cannot drift.
///
/// `everyday.rs` is ordinary Rust written by an external reviewer, not shaped for
/// the tool, so it is the honest measure of coverage (unlike `realistic.rs`, about
/// a third of which is adversarial fixtures). Phase 1's exit criterion in
/// `ROADMAP.md` is defined on it.
///
/// The assertion is a *floor*, not the exact score: adding a function that decides
/// must not fail the test, but a change that makes the tool decide *fewer* of them
/// -- a regression -- must. The exact score is printed by `mir_coverage`, which is
/// what `ROADMAP.md` quotes.
///
/// The count is `Verified` only, which is what `mir_coverage` counts. Counting
/// `Failed` too would let the floor and the scoreboard disagree: a corpus function
/// that turned from `verified` into `refuted` keeps the test's number and lowers
/// the printed one. `everyday.rs` is ordinary Rust that is supposed to *hold*, so a
/// refutation here is a bug, not coverage -- it is asserted against separately.
#[test]
fn the_independent_corpus_does_not_regress() {
    const EVERYDAY: &str = "examples/mir_targets/everyday.rs";
    let Some(rustc) = rustc() else { return };
    let report = match verify::verify_file_with(&rustc, std::path::Path::new(EVERYDAY), &[]) {
        Ok(r) => r,
        Err(e) => panic!("frontend failed: {e}"),
    };
    // A `Failed` is a *contract* failure only when the obligation that failed is a
    // claim the source made: a postcondition, a vacuous precondition, a loop
    // invariant. A `Failed` on a **panic** (or `unreachable`) obligation is not a
    // bug in the frontend -- it is the tool correctly reporting that the body can
    // panic. `sum_iter` (`v.iter().sum()`) is ordinary Rust whose unguarded
    // addition *does* overflow for `[u32::MAX, 1]`, and `everyday.rs` documents it
    // as "may overflow": rejecting every `Failed` would fail a correct panic
    // finding the moment iterator support lands. So the assertion is per kind, not
    // per outcome -- the panic family is allowed, the contract family is not.
    let refuted: Vec<&str> = report
        .outcomes
        .iter()
        .filter(|(_, o)| match o {
            FnOutcome::Failed { kind, .. } => {
                !(kind.starts_with("panic:") || kind.starts_with("reachable"))
            }
            _ => false,
        })
        .map(|(n, _)| n.as_str())
        .collect();
    assert!(
        refuted.is_empty(),
        "everyday.rs is ordinary Rust whose *contracts* must hold, so a contract \
         refutation is a bug: {refuted:?}. A corpus function that is genuinely false \
         belongs in a tripwire fixture, not here. (A panic or unreachable failure is a \
         legitimate finding and is allowed.)"
    );
    let verified = report
        .outcomes
        .iter()
        .filter(|(_, o)| matches!(o, FnOutcome::Verified { .. }))
        .count();
    assert!(
        verified >= 17,
        "everyday.rs verified {verified} of {} functions, down from 17 (81%); a regression \
         dropped coverage of ordinary Rust below Phase 1's 80% exit criterion. Run: cargo run \
         --features std --example mir_coverage -- {EVERYDAY}",
        report.outcomes.len()
    );
}

/// Every edition compiles the corpus to the same verdicts (review E4).
///
/// The frontend resolves the edition from the crate's own `Cargo.toml` rather
/// than pinning one, because the edition is part of what the program *means*: a
/// 2021 crate with `extern "C" { … }` is a compile error under 2024, and a `for`
/// loop over an array iterates `&T` under 2018 and `T` under 2021. So the same
/// source under two editions can lower to two different programs, and a verdict
/// is only meaningful for the edition it was produced under.
///
/// This runs `actuator.rs` under all four editions and requires the *whole*
/// outcome map to match, not just the decided count: a fixture that flipped from
/// `verified` to `failed` while another flipped the other way would keep the count
/// and change the answer. It is the check the `VERIF_EDITION` override exists for.
#[test]
fn every_edition_compiles_the_corpus_to_the_same_verdicts() {
    use verify::Edition;
    let Some(rustc) = rustc() else { return };
    let editions =
        [Edition::Rust2015, Edition::Rust2018, Edition::Rust2021, Edition::Rust2024];
    let mut baseline: Option<Vec<(String, String)>> = None;
    for ed in editions {
        let opts = verify::MirOptions { edition: Some(ed), ..Default::default() };
        let report = verify::verify_file_with_opts(&rustc, std::path::Path::new(TARGET), &[], &opts)
            .unwrap_or_else(|e| panic!("frontend failed under {}: {e}", ed.as_str()));
        // The verdict *shape*, so a `Failed { kind }` change is caught too, not
        // only a `Verified`/`Failed` flip.
        let shape: Vec<(String, String)> = report
            .outcomes
            .iter()
            .map(|(n, o)| (n.clone(), format!("{o:?}")))
            .collect();
        match &baseline {
            None => baseline = Some(shape),
            Some(base) => assert_eq!(
                base,
                &shape,
                "edition {} disagrees with 2015 on {}",
                ed.as_str(),
                TARGET
            ),
        }
    }
}

/// A 2021-only file is verified under *its own* edition, and refused under 2024
/// (review E1).
///
/// `extern "C" { … }` must be `unsafe extern` from edition 2024, so the same bytes
/// are a compile error there and valid under 2021. Pinning one edition would
/// therefore refuse a valid 2021 crate -- which is exactly what the frontend did
/// before `resolve_edition`. This asserts both directions: 2021 produces MIR and a
/// verdict, and 2024 is a frontend error (rustc's message, not a verdict). A test
/// that only checked the 2021 direction would pass on a frontend that ignored the
/// edition and always used rustc's default.
#[test]
fn a_2021_only_file_is_verified_under_its_own_edition() {
    use verify::Edition;
    let Some(rustc) = rustc() else { return };
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nextern \"C\" {\n    fn abs(x: i32) -> i32;\n}\nspec! {\n    ensures: ret == x,\n}\npub fn f(x: u8) -> u8 { x }\n";
    static SEQ: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
    let seq = SEQ.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
    let dir = std::env::temp_dir().join(format!("verif-edition-{}-{seq}", std::process::id()));
    let _ = std::fs::create_dir_all(&dir);
    let file = dir.join("probe.rs");
    std::fs::write(&file, src).expect("write probe");

    let opts21 = verify::MirOptions { edition: Some(Edition::Rust2021), ..Default::default() };
    let report = verify::verify_file_with_opts(&rustc, &file, &[], &opts21)
        .expect("a 2021 file with `extern \"C\"` must compile under 2021");
    let o = report.outcomes.iter().find(|(n, _)| n == "f").map(|(_, o)| o.clone());
    assert!(
        matches!(o, Some(FnOutcome::Verified { .. })),
        "the 2021 fixture must verify under 2021, got: {o:?}"
    );

    // The same bytes under 2024 are a rustc error, so the frontend reports it
    // rather than a verdict. That is the evidence the edition is *used*.
    let opts24 = verify::MirOptions { edition: Some(Edition::Rust2024), ..Default::default() };
    assert!(
        verify::verify_file_with_opts(&rustc, &file, &[], &opts24).is_err(),
        "the 2021-only file must be refused under 2024, not silently compiled"
    );
    let _ = std::fs::remove_dir_all(&dir);
}

/// A real package that omits `package.edition` resolves to Cargo's 2015 default.
///
/// Cargo assigns edition 2015 to a `[package]` that does not state one, so the
/// frontend must verify such a package rather than refuse it. This is the
/// distinction between "what edition is this crate?" (2015, by Cargo's default)
/// and "what edition is this loose file?" (no answer). The unit tests pin the
/// resolution directly; this pins it end to end, because the refusal the review
/// found happened before `rustc` was ever invoked.
#[test]
fn a_package_that_omits_an_edition_is_verified_as_2015() {
    let Some(rustc) = rustc() else { return };
    // `VERIF_EDITION` is a deliberate *override* -- it is what the edition matrix
    // uses to force a file that has a manifest under another edition -- so it wins
    // over the manifest and this test cannot assert the manifest default while it
    // is set. The environment is process-global, so the sibling unit test guards
    // the same way.
    if std::env::var("VERIF_EDITION").map(|v| !v.is_empty()).unwrap_or(false) {
        return;
    }
    static SEQ: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
    let seq = SEQ.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
    let dir = std::env::temp_dir().join(format!("verif-pkg2015-{}-{seq}", std::process::id()));
    let _ = std::fs::create_dir_all(&dir);
    // A manifest with no `edition`, exactly as the review describes.
    std::fs::write(
        dir.join("Cargo.toml"),
        "[package]\nname = \"demo\"\nversion = \"0.1.0\"\n",
    )
    .expect("write manifest");
    let file = dir.join("probe.rs");
    std::fs::write(&file, "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nspec! {\n    ensures: ret == x,\n}\npub fn f(x: u8) -> u8 { x }\n")
        .expect("write probe");

    // No explicit edition: the manifest supplies it. A refusal here is the bug.
    let opts = verify::MirOptions { edition: None, ..Default::default() };
    let (text, edition) = verify::emit_mir_with_opts(&rustc, &file, &opts)
        .expect("a package without an `edition` must resolve to Cargo's 2015 default");
    assert_eq!(edition, verify::Edition::Rust2015, "the default edition is 2015");
    assert!(!text.is_empty(), "MIR must be emitted for the package");
    let _ = std::fs::remove_dir_all(&dir);
}

/// A user function returning `!` is reported as a diverging call, not a panic.
///
/// `die() -> ! { loop {} }` never returns, so a reachable call to it is a
/// reachable failure -- but the failure is an infinite loop, not a panic. The
/// report used to say `panic: die`, naming an event that never occurs. The kind is
/// read from the callee's name, and an unrecognised `!`-returning function is
/// `diverges`, which is the honest label.
#[test]
fn a_non_panicking_diverging_call_is_not_labelled_a_panic() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\npub fn die() -> ! { loop {} }\nspec! {\n    requires: true,\n    ensures: ret == 1,\n}\npub fn f(x: u8) -> u8 { if x == 0 { die(); } 1 }\n";
    let Some(o) = verify_src("f", src) else { return };
    match o {
        FnOutcome::Failed { kind, .. } => {
            assert!(
                kind.contains("diverges"),
                "a user `!`-returning function must be labelled `diverges`, not `panic`: got `{kind}`"
            );
            assert!(
                !kind.contains("panic"),
                "a non-panicking diverging call must not be labelled a panic: got `{kind}`"
            );
        }
        other => panic!("a reachable call to a `!` function must fail, got: {other}"),
    }
}

/// A real panic *is* labelled a panic, so the label still distinguishes the two.
///
/// The companion to the test above: the fix must not flatten every diverging call
/// to `diverges`. An `assert!` is a panic, and the report says so.
#[test]
fn a_panicking_diverging_call_is_labelled_a_panic() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nspec! {\n    ensures: ret == 1,\n}\npub fn f(x: u8) -> u8 { if x == 0 { panic!(\"boom\"); } 1 }\n";
    let Some(o) = verify_src("f", src) else { return };
    match o {
        FnOutcome::Failed { kind, .. } => assert!(
            kind.contains("panic"),
            "a real panic must be labelled `panic`: got `{kind}`"
        ),
        other => panic!("an unguarded panic must fail, got: {other}"),
    }
}

/// A contract can name a struct field, and the field's *value* reaches the body.
///
/// `pick_from`'s precondition is `c.lo <= c.hi` and its postcondition is
/// `ret >= c.lo`. Both name fields of a `&Config`, which the MIR dump prints only
/// as `_1: &Config` with no layout — the frontend has to read the layout from the
/// source. Before that, this function was `skipped` with "unknown name".
#[test]
fn a_contract_can_name_a_struct_field() {
    let Some(report) = verify_realistic(&["pick_from", "clamp_by_value"]) else { return };
    for name in ["pick_from", "clamp_by_value"] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify, got: {o}");
    }
}

/// The tripwire for field *independence*.
///
/// `field_alias_tripwire` has the same shape as `clamp_by_value` but claims
/// `ret >= c.hi`, which is false whenever `v < c.lo <= c.hi`. It must FAIL. If
/// `c.lo` and `c.hi` were bound to the same symbolic variable the claim would be
/// `ret >= x` with `ret` in `{x, v}` and `v` unconstrained-but-`<= x`... which is
/// exactly the vacuous pass this test exists to catch. A counterexample is the
/// positive evidence that the two fields are distinct inputs.
#[test]
fn struct_fields_are_independent_inputs_not_one_aliased_variable() {
    let Some(report) = verify_realistic(&["field_alias_tripwire"]) else { return };
    let o = outcome(&report, "field_alias_tripwire");
    let FnOutcome::Failed { kind, detail } = o else {
        panic!(
            "`field_alias_tripwire` must fail: a pass means `c.lo` and `c.hi` \
             collapsed to one variable and the contract was vacuous. got: {o}"
        );
    };
    assert_eq!(kind, "postcondition", "got: {o}");
    // The witness must name two different variables for the two fields. If the
    // binding aliased, the same variable index would appear for both.
    let names: Vec<&str> = detail.split_whitespace().filter(|t| t.starts_with('v')).collect();
    assert!(
        names.len() >= 2,
        "the counterexample must assign both fields to show they differ, got: {detail}"
    );
    assert!(
        names.iter().collect::<std::collections::BTreeSet<_>>().len() >= 2,
        "the two fields were assigned the same variable, so they are aliased: {detail}"
    );
}

/// A slice contract the frontend must *not* prove: `first_of` is sound, but
/// `slice_alias_tripwire` claims `ret == xs[1]` where the body returns `xs[0]`.
///
/// The tripwire exists because the tempting wrong model is to give a slice one
/// "element" variable. Then `xs[0] == xs[1]` and the claim would hold. A failure
/// is the evidence that two indices are two inputs; a pass means the slice was
/// collapsed and `first_of`'s proof is not worth anything.
#[test]
fn slice_elements_are_independent_inputs_not_one_aliased_variable() {
    let Some(report) = verify_realistic(&["slice_alias_tripwire"]) else { return };
    let o = outcome(&report, "slice_alias_tripwire");
    let FnOutcome::Failed { kind, detail } = o else {
        panic!(
            "`slice_alias_tripwire` must fail: a pass means `xs[0]` and `xs[1]` \
             collapsed to one variable and the slice contract was vacuous. got: {o}"
        );
    };
    assert_eq!(kind, "postcondition", "got: {o}");
    let names: Vec<&str> = detail.split_whitespace().filter(|t| t.starts_with('v')).collect();
    assert!(
        names.iter().collect::<std::collections::BTreeSet<_>>().len() >= 2,
        "the two elements were assigned the same variable, so they are aliased: {detail}"
    );
}

/// The slice length is a symbolic input, not a constant, and `xs.len()` in a
/// contract names the same variable the body's `PtrMetadata` reads.
///
/// `first_of` requires `xs.len() > 0` and claims `ret == xs[0]`. If the length
/// were a fixed constant (say `0`), the precondition would be unsatisfiable and
/// the function would be reported vacuous rather than verified; if the contract's
/// `xs.len()` and the body's `PtrMetadata` were *different* variables, the
/// bounds `assert` could not be discharged from the precondition and the function
/// would be incomplete. Verified is the only outcome that shows both hold.
#[test]
fn a_slice_length_is_a_symbolic_input_shared_with_the_body() {
    let Some(report) = verify_realistic(&["first_of"]) else { return };
    let o = outcome(&report, "first_of");
    assert!(o.is_verified(), "`first_of` must verify, got: {o}");
}

/// A fixed-size array argument: elements are inputs, but the length is not.
///
/// `max_of_pair` requires `a[0] <= a[1]` and claims `ret == a[1]`. This is the
/// array counterpart of `first_of`, and the difference is the point: a slice's
/// length is a symbolic input read through `PtrMetadata`, while an array's length
/// is a *type* constant, so the bounds check in the body is against the literal
/// `4` and `a.len()` in a contract is `4` rather than a fresh variable.
#[test]
fn a_contract_can_name_a_fixed_size_array_element() {
    let Some(report) = verify_realistic(&["max_of_pair", "array_third"]) else { return };
    for name in ["max_of_pair", "array_third"] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify, got: {o}");
    }
}

/// An array built in the body has *concrete* elements, not inputs.
///
/// `array_literal_elem` returns `a[1]` of `[10, 20, 30, 40]` and claims
/// `ret == 20`. Verified is the evidence that the element map holds the literal
/// values: if the index were modelled as an unconstrained input the claim would
/// fail with a counterexample instead.
#[test]
fn an_array_literal_element_is_concrete() {
    let Some(report) = verify_realistic(&["array_literal_elem"]) else { return };
    let o = outcome(&report, "array_literal_elem");
    assert!(o.is_verified(), "`array_literal_elem` must verify, got: {o}");
}

/// The tripwire for array *element independence*.
///
/// `array_alias_tripwire` has the same body as `max_of_pair` but claims
/// `ret <= a[0]`, which is false when `a[1] > a[0]`. It must FAIL, and the
/// counterexample must assign `a[0]` and `a[1]` to *different* variables -- a
/// pass would mean the elements collapsed to one input and `max_of_pair`'s proof
/// was vacuous.
#[test]
fn array_elements_are_independent_inputs_not_one_aliased_variable() {
    let Some(report) = verify_realistic(&["array_alias_tripwire"]) else { return };
    let o = outcome(&report, "array_alias_tripwire");
    let FnOutcome::Failed { kind, detail } = o else {
        panic!(
            "`array_alias_tripwire` must fail: a pass means `a[0]` and `a[1]` \
             collapsed to one variable and the array contract was vacuous. got: {o}"
        );
    };
    assert_eq!(kind, "postcondition", "got: {o}");
    let names: Vec<&str> = detail.split_whitespace().filter(|t| t.starts_with('v')).collect();
    assert!(
        names.iter().collect::<std::collections::BTreeSet<_>>().len() >= 2,
        "the two elements were assigned the same variable, so they are aliased: {detail}"
    );
}

/// The corpus tripwires for the write/read-store desync, asserted end to end.
///
/// Each of these contracts is *false* and the frontend used to prove one of them.
/// The corpus (`examples/mir_targets/realistic.rs`) carries them so
/// `examples/mir_coverage` measures the shape; this pins the verdict so a
/// regression is a test failure rather than a quiet false proof.
#[test]
fn a_write_cannot_prove_a_stale_contract() {
    let names = [
        "array_write_then_symbolic_read",
        "mut_ref_array_write_then_symbolic_read",
        "generated_name_shadows_argument",
    ];
    let Some(report) = verify_realistic(&names) else { return };
    for name in names {
        let o = outcome(&report, name);
        assert!(
            matches!(o, FnOutcome::Failed { .. }),
            "`{name}` proves a false contract; got: {o}"
        );
    }
    // The struct-reassignment shape is refused rather than refuted: the write
    // poisons `c.lo`, and the postcondition then has no single value to check.
    // The requirement is the same -- never `Verified`.
    let Some(report) = verify_realistic(&["struct_reassign_then_field_read"]) else { return };
    let o = outcome(&report, "struct_reassign_then_field_read");
    assert!(
        !o.is_verified(),
        "`struct_reassign_then_field_read` proves a stale field; got: {o}"
    );
}

/// Stage 2: the newly modelled rvalues, each with its tripwire.
///
/// These four shapes -- a struct literal, a reference to a place, `.len()` on an
/// array, and a whole-struct reassignment -- were all `Unknown` before, so their
/// contracts were unreadable rather than checked. Each pair below is a true
/// contract that must now verify and a false one that a wrong model would prove.
/// The tripwires are the point: verifying the true half alone would not show the
/// model is right, only that it is present.
#[test]
fn stage_two_rvalues_are_modelled_and_their_tripwires_refuted() {
    let verified = [
        "struct_literal_reads_its_field",
        "struct_literal_order",
        "reference_reads_through",
        "reference_write_reaches_target",
        "array_len_is_the_type_constant",
        "struct_reassign_reads_new_field",
        "enum_match_is_bounded",
        "enum_match_selects_by_tag",
        "enum_payload_reads_through",
        "enum_constructed_locally",
        "enum_constructed_payload_reads_through",
        "enum_explicit_discriminant_selects_by_tag",
        "enum_user_variant_not_option",
        "enum_user_variant_not_option_constructed",
    ];
    let refuted = [
        // Written in reverse order; a positional-by-write-order model proves it.
        "struct_literal_order_tripwire",
        // The pre-write value; a snapshot model of `&mut` proves it.
        "reference_write_tripwire",
        // Claims every arm returns `2`; only a constant tag proves it.
        "enum_match_tripwire",
        // Claims the `Off` arm ran for a value built as `Mode::Idle`; a model
        // that resolved the variant to the wrong index (or to a symbolic tag)
        // would prove it.
        "enum_constructed_locally_tripwire",
        // Claims the `B` arm ran for `Gap::C`, whose real tag is `4`; a reader
        // numbering variants by position would call it `1` and prove this.
        "enum_explicit_discriminant_tripwire",
        // Claims the `None` arm ran for `Shadow::Some`, whose real tag is `0`;
        // taking `Option`'s numbering for the qualified name proves this.
        "enum_user_variant_not_option_tripwire",
        "enum_user_variant_not_option_constructed_tripwire",
    ];
    let Some(report) = verify_realistic(&verified) else { return };
    for name in verified {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify, got: {o}");
    }
    let Some(report) = verify_realistic(&refuted) else { return };
    for name in refuted {
        let o = outcome(&report, name);
        let FnOutcome::Failed { .. } = o else {
            panic!("`{name}` is a false contract that must be refuted, got: {o}");
        };
    }
}

/// A `u16` product is decided end to end, which the product bound now admits.
///
/// The `u16` `saturating_mul` in the corpus used to report "past what the model
/// covers": the widened product test needs a `2w`-bit multiplier, and the bound
/// stopped the widening at `2w == 16`. The bound is 16 now, so the guarded
/// fixture must verify and the deliberately unguarded one must still be refuted.
/// The pair matters more than the verify: a bound raised without the refutation
/// would be a model that proves the wrong thing, which is worse than one that
/// declines.
#[test]
fn a_u16_product_is_decided_end_to_end() {
    let Some(report) = verify_realistic(&["u16_product_guarded"]) else { return };
    let o = outcome(&report, "u16_product_guarded");
    assert!(o.is_verified(), "`u16_product_guarded` must verify, got: {o}");

    let Some(report) = verify_realistic(&["u16_product_unguarded_tripwire"]) else { return };
    let o = outcome(&report, "u16_product_unguarded_tripwire");
    let FnOutcome::Failed { .. } = o else {
        panic!("`u16_product_unguarded_tripwire` is false and must be refuted, got: {o}");
    };
}


/// The complement bit counts `count_zeros`, `leading_ones` and `trailing_ones`,
/// each pinned by a true half and a tripwire.
///
/// Each is `core`'s own definition -- the corresponding *positive* count of `!x`
/// -- so a model that summed the wrong polarity would be wrong by the complement,
/// which is a wrong value and not a weaker proof. `count_zeros` is pinned at a
/// single operand where the two polarities disagree, and the two one-sided counts
/// are pinned at the boundary of their precondition. The tripwires are the
/// evidence the count is read at all: a constant answer would prove neither the
/// true half nor the false one.
#[test]
fn the_complement_bit_counts_agree_with_rust() {
    let verified = [
        "count_zeros_of_zero_is_the_width",
        "leading_ones_of_a_top_bit_value",
        "trailing_ones_of_a_small_value",
    ];
    let refuted = [
        "count_zeros_of_zero_is_the_width_tripwire",
        "leading_ones_of_a_top_bit_value_tripwire",
        "trailing_ones_of_a_small_value_tripwire",
    ];
    let Some(report) = verify_realistic(&verified) else { return };
    for name in verified {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify, got: {o}");
    }
    let Some(report) = verify_realistic(&refuted) else { return };
    for name in refuted {
        let o = outcome(&report, name);
        let FnOutcome::Failed { .. } = o else {
            panic!("`{name}` is a false contract that must be refuted, got: {o}");
        };
    }
}

/// `is_power_of_two` is modelled as a `bool` the `if` branches on.
///
/// This is the `bool`-channel pin, and it is a different kind of test from the
/// bit counts: the model produces a `Pred` rather than a `Var`, and the executor
/// writes it as a `Val::Bool`. A model that wrote a one-bit *word* into the
/// destination would leave the local readable only as a word, and the postcondition
/// `ret == true` would not read it -- so `is_power_of_two_of_the_top_bit` fails
/// rather than passing by accident. The zero tripwire is the sharp one: `0` is the
/// single value the bare `x & (x - 1) == 0` mask test would accept, so a model
/// that dropped the `x != 0` conjunct would *prove* the false contract.
#[test]
fn is_power_of_two_is_a_modelled_bool() {
    let verified = ["is_power_of_two_of_the_top_bit", "is_power_of_two_of_three"];
    let refuted = ["is_power_of_two_of_zero_tripwire"];
    let Some(report) = verify_realistic(&verified) else { return };
    for name in verified {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify, got: {o}");
    }
    let Some(report) = verify_realistic(&refuted) else { return };
    for name in refuted {
        let o = outcome(&report, name);
        let FnOutcome::Failed { .. } = o else {
            panic!("`{name}` is a false contract that must be refuted, got: {o}");
        };
    }
}

/// The `bool` is consumed by a `switchInt`, not only read in a postcondition.
///
/// `is_power_of_two_is_a_modelled_bool` returns the predicate, so the `bool`
/// reaches the return place and the obligation reads it there; a regression in
/// *branching* on it would leave those tests green. These fixtures put the
/// predicate in an `if`, where the executor has to turn the `Pred` into a path
/// condition and take one of two arms returning different constants. The two
/// arms are pinned separately, so a model that took a constant arm fails one of
/// them, and the zero tripwire pins the `x != 0` conjunct *through the branch*:
/// a model that dropped it takes the `then` arm and proves the false `ret == 7`.
#[test]
fn is_power_of_two_branches_on_the_predicate() {
    let verified = [
        "is_power_of_two_branching_takes_the_true_arm",
        "is_power_of_two_branching_takes_the_false_arm",
    ];
    let refuted = ["is_power_of_two_branching_of_zero_tripwire"];
    let Some(report) = verify_realistic(&verified) else { return };
    for name in verified {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify, got: {o}");
    }
    let Some(report) = verify_realistic(&refuted) else { return };
    for name in refuted {
        let o = outcome(&report, name);
        let FnOutcome::Failed { .. } = o else {
            panic!("`{name}` is a false contract that must be refuted, got: {o}");
        };
    }
}

/// `midpoint`, `next_multiple_of` (and its `checked_` counterpart), the two
/// logarithms and the `next_power_of_two` pair, pinned in both directions.
///
/// `core::num` methods added as a group. The true halves pin the *value* --
/// `midpoint` of an overflowing sum (`255.midpoint(255) == 255`, not the wrapped
/// `254`), the truncating rounding (`(-5).midpoint(-2) == -3`, not the floor
/// `-4`), `next_multiple_of` of an exact multiple (the identity, which a model
/// that always added `b` gets wrong), the logs at their boundary values
/// (`ilog2(1) == 0`, `ilog10(9) == 0`), and `next_power_of_two` at the three
/// boundaries that separate a right model from a plausible wrong one (`0 -> 1`,
/// a power of two rounding to itself, and `100 -> 128`). Each true half has a
/// `_cannot_prove_this` twin that the same wrong model would prove, so a green
/// verdict on the twin fails the test.
///
/// The panic-shaped methods carry the opposite evidence: `next_multiple_of` that
/// overflows, `ilog2`/`ilog10` of zero, and `next_power_of_two` of `129` must
/// *fail* with the panic obligation, and a control
/// (`next_multiple_of_is_panic_free_here`, `next_power_of_two_is_panic_free_here`)
/// must verify, so a model that reported a panic unconditionally cannot pass. The
/// `checked_` counterparts are the value-shaped ones: `Some(128)` where the plain
/// method would not panic, `None` where it would -- for both the power-of-two and
/// the multiple-of pairs, including the zero divisor for the latter.
#[test]
fn the_new_num_methods_verify_and_their_tripwires_refute() {
    let verified = [
        "midpoint_self_is_the_identity",
        "midpoint_of_an_overflowing_sum",
        "midpoint_rounds_toward_zero",
        "midpoint_signed_truncates",
        "midpoint_is_between",
        "next_multiple_of_rounds_up",
        "next_multiple_of_an_exact_multiple_is_the_identity",
        "next_multiple_of_is_panic_free_here",
        "checked_next_multiple_of_rounds_up",
        "checked_next_multiple_of_that_overflows_is_none",
        "checked_next_multiple_of_by_zero_is_none",
        "ilog2_of_100",
        "ilog2_of_one_is_zero",
        "ilog2_of_a_signed_100",
        "ilog2_is_bounded",
        "ilog10_of_100",
        "ilog10_below_ten_is_zero",
        "ilog10_of_a_u32_thousand",
        "ilog10_is_bounded",
        "checked_ilog2_in_range",
        "checked_ilog2_of_zero_is_none",
        "checked_ilog10_in_range",
        "checked_ilog10_of_zero_is_none",
        "checked_ilog2_of_a_negative_is_none",
        "next_power_of_two_rounds_up",
        "next_power_of_two_of_a_power_is_itself",
        "next_power_of_two_of_zero_is_one",
        "next_power_of_two_of_a_u32_thousand",
        "next_power_of_two_is_panic_free_here",
        "checked_next_power_of_two_in_range",
        "checked_next_power_of_two_out_of_range_is_none",
    ];
    let refuted = [
        "midpoint_of_an_overflowing_sum_cannot_prove_this",
        "midpoint_rounds_toward_zero_cannot_prove_this",
        "midpoint_signed_truncates_cannot_prove_this",
        "midpoint_is_between_cannot_prove_this",
        "next_multiple_of_rounds_up_cannot_prove_this",
        "checked_next_multiple_of_overflow_is_some_cannot_prove_this",
        "ilog2_of_100_cannot_prove_this",
        "ilog10_of_100_cannot_prove_this",
        "checked_ilog2_overflow_is_some_cannot_prove_this",
        "checked_ilog2_of_a_negative_cannot_prove_this",
        "next_power_of_two_rounds_up_cannot_prove_this",
        "checked_next_power_of_two_in_range_cannot_prove_this",
        "checked_next_power_of_two_payload_cannot_prove_this",
    ];
    // The panic-shaped fixtures fail with a *panic* obligation, so the kind is
    // asserted too: a postcondition failure on one of them would be a different
    // finding and must not satisfy the test.
    let panics = [
        "next_multiple_of_that_overflows_panics",
        "ilog2_of_zero_panics",
        "ilog10_of_zero_panics",
        "next_power_of_two_that_overflows_panics",
    ];
    let Some(report) = verify_realistic(&verified) else { return };
    for name in verified {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify, got: {o}");
    }
    let Some(report) = verify_realistic(&refuted) else { return };
    for name in refuted {
        let o = outcome(&report, name);
        let FnOutcome::Failed { .. } = o else {
            panic!("`{name}` is a false contract that must be refuted, got: {o}");
        };
    }
    let Some(report) = verify_realistic(&panics) else { return };
    for name in panics {
        let o = outcome(&report, name);
        let FnOutcome::Failed { kind, .. } = o else {
            panic!("`{name}` must fail on its panic obligation, got: {o}");
        };
        assert!(
            kind.starts_with("panic"),
            "`{name}` must fail on a panic obligation, got kind `{kind}`"
        );
    }
}

/// `is_negative`/`is_positive` are modelled as signed comparisons, not bit tests.
///
/// The signedness is the whole point: `i8::MIN` is `0x80`, the *largest* unsigned
/// byte, so a model that read the top bit as a plain bit (or compared unsigned)
/// would call it positive and refute `is_negative_of_min`. The zero tripwire is
/// the sharp one in the other direction: `0` is neither sign, so a model that
/// answered `x != 0` for `is_positive`, or the bare top bit for `is_negative`,
/// would *prove* a false contract. The `MAX`/`MIN`/`-1` pins name the boundary
/// values, so a failure names the value that broke rather than only the function.
#[test]
fn the_sign_predicates_are_signed_comparisons() {
    let verified = [
        "is_negative_of_min",
        "is_negative_of_max",
        "is_positive_of_max",
        "is_negative_branching_takes_the_true_arm",
    ];
    let refuted = ["is_negative_of_zero_tripwire", "is_positive_of_minus_one_tripwire"];
    let Some(report) = verify_realistic(&verified) else { return };
    for name in verified {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify, got: {o}");
    }
    let Some(report) = verify_realistic(&refuted) else { return };
    for name in refuted {
        let o = outcome(&report, name);
        let FnOutcome::Failed { .. } = o else {
            panic!("`{name}` is a false contract that must be refuted, got: {o}");
        };
    }
}

/// The mixed-signedness additions, each pinned in both directions.
///
/// Every shape gets a true contract *and* a false one with a concrete witness, so
/// the suite carries the negative direction the standing tripwire rule requires:
/// a model that proves a false claim about `add_signed`/`add_unsigned` fails here
/// rather than passing because every fixture happened to be true. The false pins
/// are chosen to separate the readings that the true pins cannot:
///
/// * `overflowing_add_signed_wrong_flag_tripwire` (`250u8 + (-1i8)`, in range) is
///   the one a *bare-carry* flag model would prove, since the carry fires there.
/// * `overflowing_add_unsigned_wrong_flag_tripwire` (`1i8 + 1u8`, in range) is
///   the one a *signed-addition* flag model would prove.
/// * `saturating_add_unsigned_wrong_value_tripwire` names the *wrapping* answer,
///   which pins that the two families are not the same shape.
///
/// The checked family is read through a `match` rather than `ret.is_some()`,
/// because the contract language cannot name an `Option` payload; the arm the
/// match takes is what the `Some`/`None` verdicts pin.
#[test]
fn the_mixed_signedness_additions_verify_and_their_tripwires_refute() {
    let verified = [
        "wrapping_add_signed_of_a_negative",
        "saturating_add_signed_clamps_to_zero",
        "overflowing_add_signed_below_zero_is_overflow",
        "overflowing_add_signed_within_range_is_not_overflow",
        "overflowing_add_signed_past_the_top_tripwire",
        "wrapping_add_unsigned_of_the_top_bit",
        "saturating_add_unsigned_clamps_to_signed_max",
        "saturating_add_unsigned_top_bit_tripwire",
        "checked_add_signed_in_range_is_the_sum",
        "checked_add_unsigned_in_range_is_the_sum",
        "overflowing_add_unsigned_within_range_is_not_overflow",
        "overflowing_add_unsigned_past_the_top_is_overflow",
    ];
    let refuted = [
        "wrapping_add_signed_wrong_value_tripwire",
        "saturating_add_signed_wrong_value_tripwire",
        "overflowing_add_signed_wrong_flag_tripwire",
        "checked_add_signed_none_tripwire",
        "wrapping_add_unsigned_wrong_value_tripwire",
        "saturating_add_unsigned_wrong_value_tripwire",
        "overflowing_add_unsigned_wrong_flag_tripwire",
        "checked_add_unsigned_wrong_value_tripwire",
    ];
    let Some(report) = verify_realistic(&verified) else { return };
    for name in verified {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify, got: {o}");
    }
    let Some(report) = verify_realistic(&refuted) else { return };
    for name in refuted {
        let o = outcome(&report, name);
        let FnOutcome::Failed { kind, .. } = o else {
            panic!("`{name}` is a false mixed-add contract that must be refuted, got: {o}");
        };
        assert_eq!(kind, "postcondition", "`{name}` got: {o}");
    }
}

/// A local method sharing an intrinsic's name is answered by its body, not `core`.
///
/// rustc prints a trait impl's call as `<u8 as Trait>::is_power_of_two` and its
/// definition as `<impl at …>::is_power_of_two`, so the exact-name lookup missed
/// and the intrinsic model -- classifying on the suffix -- answered first. A trait
/// that disagrees with `core` then got a *false proof*: the body returns `false`
/// at `x == 1`, but the model returned `core`'s `true` and proved `ret == true`.
/// The true contract must verify (the body really is `false` there) and the false
/// one must be refuted, which pins both that the body was read and that the model
/// no longer shadows it.
#[test]
fn a_trait_method_sharing_an_intrinsic_name_is_answered_by_its_body() {
    let verified = ["trait_is_power_of_two_disagrees_with_core"];
    let refuted = ["trait_is_power_of_two_disagrees_with_core_tripwire"];
    let Some(report) = verify_realistic(&verified) else { return };
    for name in verified {
        let o = outcome(&report, name);
        assert!(
            o.is_verified(),
            "`{name}` must verify: the trait body returns `false` at `x == 1`, so a \
             model that answered from `core`'s predicate would refute it; got: {o}"
        );
    }
    let Some(report) = verify_realistic(&refuted) else { return };
    for name in refuted {
        let o = outcome(&report, name);
        let FnOutcome::Failed { kind, .. } = o else {
            panic!(
                "`{name}` must be refuted: it claims `core`'s answer, which the trait \
                 body does not produce. A pass is the false proof this pins. got: {o}"
            );
        };
        assert_eq!(kind, "postcondition", "`{name}` got: {o}");
    }
}

/// `Ord::min`/`Ord::max` are modelled, and a local trait sharing their name is
/// answered by its body rather than by the model.
///
/// The two halves pin both directions. The verifying half covers the unsigned and
/// signed orderings and the explicit `Ord::min` spelling; `ord_min_of_min_and_one_is_min`
/// is the one that separates them, since `i8::MIN`'s bits sort the other way
/// unsigned. The refuting half is the tripwire on the method form, plus the
/// local-trait pair: `local_ord_min_is_answered_by_its_body` verifies only from the
/// body's constant, and `local_ord_min_is_not_the_model_tripwire` is refuted only
/// if the body -- not the model -- answered, so the model cannot silently take the
/// call over.
#[test]
fn ord_min_and_max_are_modelled_and_a_local_trait_still_wins() {
    let verified = [
        "ord_min_is_at_most_both",
        "ord_max_is_at_least_x",
        "ord_min_of_min_and_one_is_min",
        "explicit_ord_min_is_modelled",
        "local_ord_min_is_answered_by_its_body",
    ];
    let refuted = ["ord_min_is_x_tripwire", "local_ord_min_is_not_the_model_tripwire"];
    let Some(report) = verify_realistic(&verified) else { return };
    for name in verified {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify, got: {o}");
    }
    let Some(report) = verify_realistic(&refuted) else { return };
    for name in refuted {
        let o = outcome(&report, name);
        let FnOutcome::Failed { kind, .. } = o else {
            panic!("`{name}` is a false contract that must be refuted, got: {o}");
        };
        assert_eq!(kind, "postcondition", "`{name}` got: {o}");
    }
}

/// `PartialEq::eq`/`ne` and `PartialOrd::lt`/`le`/`gt`/`ge` are modelled as the
/// comparison operators they denote, and a local trait sharing their name is
/// answered by its body rather than by the model.
///
/// The verifying half pins all six operators, with `partial_ge_method_is_the_operator`
/// choosing `x < y` so `>=` is separated from `<=`, and
/// `partial_lt_signed_is_not_unsigned` pinning the ordering choice on `i8::MIN` (the
/// one value whose bits sort the other way unsigned). The refuting half is the
/// tripwire on the method form plus the local-trait pair:
/// `local_partial_ord_lt_is_answered_by_its_body` verifies only from the body's
/// constant, and `local_partial_ord_lt_is_not_the_model_tripwire` is refuted only
/// if the body -- not the model -- answered.
#[test]
fn partial_cmp_methods_are_modelled_and_a_local_trait_still_wins() {
    let verified = [
        "partial_eq_method_is_the_operator",
        "partial_ne_method_is_the_operator",
        "partial_lt_method_is_the_operator",
        "partial_ge_method_is_the_operator",
        "partial_lt_signed_is_not_unsigned",
        "local_partial_ord_lt_is_answered_by_its_body",
        // A comparison against a *constant* operand: the literal is a promoted
        // constant referenced by place in unoptimised MIR, so this is the shape
        // that pins the promoted-scalar collection.
        "partial_eq_const_is_the_operator",
        "partial_lt_const_signed_is_not_unsigned",
    ];
    let refuted = [
        "partial_lt_is_not_le_tripwire",
        "local_partial_ord_lt_is_not_the_model_tripwire",
        "partial_lt_const_is_not_le_tripwire",
    ];
    let Some(report) = verify_realistic(&verified) else { return };
    for name in verified {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify, got: {o}");
    }
    let Some(report) = verify_realistic(&refuted) else { return };
    for name in refuted {
        let o = outcome(&report, name);
        let FnOutcome::Failed { kind, .. } = o else {
            panic!("`{name}` is a false contract that must be refuted, got: {o}");
        };
        assert_eq!(kind, "postcondition", "`{name}` got: {o}");
    }
}

/// `Ord::cmp` is modelled, returning the `Ordering` enum whose three variants a
/// `switchInt` on `discriminant(_)` selects.
///
/// The verifying entries pin all three arms -- `ord_cmp_less_arm`,
/// `ord_cmp_equal_arm` and `ord_cmp_greater_arm` return distinct constants so each
/// arm proves which tag the model produced -- plus `ord_cmp_matches_less` (the
/// `matches!` spelling) and `ord_cmp_signed_is_not_unsigned`, which pins the signed
/// ordering on `i8::MIN` (its bits sort the other way unsigned, so an unsigned
/// model would take the `Greater` arm). The refuting entry is the tripwire:
/// `x.cmp(&y) == Less` is not `x <= y`, so it fails at `x == y` -- a model that
/// read the tag as unsigned (comparing the `Less` discriminant `0xff` against
/// `255`) or that confused `Less` with `Equal` would *prove* it.
#[test]
fn ord_cmp_returns_the_ordering_enum() {
    let verified = [
        "ord_cmp_less_arm",
        "ord_cmp_equal_arm",
        "ord_cmp_greater_arm",
        "ord_cmp_matches_less",
        "ord_cmp_signed_is_not_unsigned",
    ];
    let refuted = ["ord_cmp_is_not_le_tripwire"];
    let Some(report) = verify_realistic(&verified) else { return };
    for name in verified {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify, got: {o}");
    }
    let Some(report) = verify_realistic(&refuted) else { return };
    for name in refuted {
        let o = outcome(&report, name);
        let FnOutcome::Failed { kind, .. } = o else {
            panic!("`{name}` is a false contract that must be refuted, got: {o}");
        };
        assert_eq!(kind, "postcondition", "`{name}` got: {o}");
    }
}

/// A widening conversion (`From::from`/`Into::into`) is modelled, by the source's
/// signedness.
///
/// The three verifying entries pin the three cases that separate a correct model
/// from a wrong one: `from_u8_is_modelled` and `into_u8_is_modelled` are the two
/// trait spellings reaching one value, and
/// `unsigned_source_widens_to_signed_destination` is the *signedness change* that
/// must zero-fill (the source is unsigned) even though the destination is signed.
/// `signed_widening_keeps_negative_values` is the sign-extension half: `i8::MIN`
/// stays `i8::MIN` in an `i16`.
///
/// The tripwire is the evidence the source's signedness is read at all:
/// `-1i8` widens to `-1i16` (`0xffff`), so a model that zero-filled a signed source
/// would return `255` and *prove* `ret == 255`. Refuting it is what shows the
/// extension is chosen by the source's sign and not by `truncate`.
#[test]
fn widening_conversions_are_modelled_by_the_sources_signedness() {
    let verified = [
        "from_u8_is_modelled",
        "into_u8_is_modelled",
        "signed_widening_keeps_negative_values",
        "unsigned_source_widens_to_signed_destination",
    ];
    let Some(report) = verify_realistic(&verified) else { return };
    for name in verified {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify, got: {o}");
    }
    let refuted = ["signed_widening_is_not_zero_fill_tripwire"];
    let Some(report) = verify_realistic(&refuted) else { return };
    for name in refuted {
        let o = outcome(&report, name);
        let FnOutcome::Failed { kind, .. } = o else {
            panic!("`{name}` is a false contract that must be refuted, got: {o}");
        };
        assert_eq!(kind, "postcondition", "`{name}` got: {o}");
    }
}

/// A local trait whose name and owner type collide with a modelled conversion's
/// must not answer a call the compiler resolved to the *library* trait.
///
/// The `conv_shim` module implements its own `From<u8> for u16` returning `7`,
/// while `core`'s `From<u8> for u16` widens `x`. Both share the method tail
/// (`from`) and the owner (`u16`), so before the trait check
/// `resolve_callee` answered `<u16 as std::convert::From<u8>>::from` from the
/// local body and *proved* `ret == 7` -- a false proof of a library call. This
/// test is the tripwire: `local_from_does_not_answer_the_std_trait` must be
/// refuted, and the local trait's own call
/// (`local_from_is_answered_by_its_own_body`) must still verify, so the guard
/// separates the two traits rather than refusing the local one outright.
#[test]
fn a_local_trait_named_from_does_not_answer_the_std_trait() {
    let verified = ["local_from_is_answered_by_its_own_body"];
    let Some(report) = verify_realistic(&verified) else { return };
    for name in verified {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify from the local body, got: {o}");
    }
    let refuted = ["local_from_does_not_answer_the_std_trait"];
    let Some(report) = verify_realistic(&refuted) else { return };
    for name in refuted {
        let o = outcome(&report, name);
        let FnOutcome::Failed { kind, .. } = o else {
            panic!("`{name}` claims the local body's answer for a library call and must be refuted, got: {o}");
        };
        assert_eq!(kind, "postcondition", "`{name}` got: {o}");
    }
}

/// A user type named `Range` must not be answered by the for-loop range model.
///
/// rustc 1.85 (the MSRV) prints the standard library's `for`-loop projection with
/// the type *unqualified* -- `<RangeInclusive<u32> as IntoIterator>::into_iter` --
/// where current stable qualifies it (`<std::ops::RangeInclusive<u32> as …>`). An
/// unqualified `Range` is a name a *user* type can carry, so the two `Iterator`
/// methods are matched on the trait as well as the type. This test pins both
/// directions with its own source (so it does not depend on a corpus edit):
///
/// * a user `struct Range<T>` with a custom `into_iter` (whose default body
///   *panics*) must never be answered with the model's identity semantics -- the
///   model would return the range unchanged and prove `ret == 1` for a function
///   that always panics; the honest verdict is a refutation (`Failed`);
/// * the standard library's own range projection must still be modelled, so the
///   fix cannot pass by refusing every range call.
#[test]
fn a_user_type_named_range_is_not_answered_by_the_range_model() {
    let spoof = r#"macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }

pub mod a {
    pub trait Custom {
        fn into_iter(self) -> Self
        where
            Self: Sized,
        {
            panic!("into_iter is not the identity here")
        }
    }
}
pub mod b {
    pub trait Custom {
        fn into_iter(self) -> Self
        where
            Self: Sized,
        {
            panic!("the other trait")
        }
    }
}
pub struct Range<T> {
    pub start: T,
    pub end: T,
}
impl a::Custom for Range<u8> {}

spec! {
    requires: true,
    ensures: ret == 1,
}
pub fn caller() -> u8 {
    let r = Range { start: 1u8, end: 2u8 };
    let x = <Range<u8> as a::Custom>::into_iter(r);
    x.start
}
"#;
    let Some(o) = verify_src("caller", spoof) else { return };
    // The soundness property: the model must never answer this call. If it did, it
    // would return the range unchanged and *prove* `ret == 1`. On current stable the
    // default body's panic is modelled, so the verdict is a refutation (`Failed`); on
    // 1.85 the panic path is not modelled, so the verdict is a refusal (`Incomplete`).
    // Both are honest; only `verified` would be a false proof.
    assert!(
        !o.is_verified(),
        "a user `Range`'s custom `into_iter` must not be answered by the range \
         model (the model would prove `ret == 1` for a function that panics), got: {o}"
    );

    // The positive control: the standard library's own `for` loop is still modelled.
    let real = r#"macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }

spec! {
    requires: true,
    ensures: ret == 3,
}
pub fn sums() -> u32 {
    let mut c = 0u32;
    for i in 1u32..=2u32 {
        c += i;
    }
    c
}
"#;
    let Some(o) = verify_src("sums", real) else { return };
    assert!(
        o.is_verified(),
        "the standard library's `for`-loop range must still be modelled, got: {o}"
    );

    // The sharper shape: a trait *default* body that mutates `self` through a helper
    // the impl *overrides*. The call inside the default body is `<Self as T>::set`,
    // whose receiver is literally `Self`, so the concrete type is not known there.
    // Resolving it to the *default* `set` (a no-op) inlined the wrong body and
    // *proved* `ret == 1` for a caller whose real result is `42` -- a false proof,
    // pre-existing and independent of the range model. The call is refused now, so
    // the soundness property is "never `verified`".
    let self_receiver = r#"macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }

#[derive(Clone, Copy)]
pub struct P {
    pub v: u8,
}
pub trait T: Copy {
    fn go(mut self) -> Self
    where
        Self: Sized,
    {
        self.set(42);
        self
    }
    fn set(&mut self, x: u8) {
        let _ = x;
    }
}
impl T for P {
    fn set(&mut self, x: u8) {
        self.v = x;
    }
}

spec! {
    requires: true,
    ensures: ret == 1,
}
pub fn caller() -> u8 {
    let p = P { v: 1 };
    let q = <P as T>::go(p);
    q.v
}
"#;
    let Some(o) = verify_src("caller", self_receiver) else { return };
    assert!(
        !o.is_verified(),
        "a `<Self as T>::m` call inside a trait default body must not be answered \
         by the default method when an impl overrides it (that proved `ret == 1` for \
         a body returning `42`), got: {o}"
    );
}

/// A bare trait call must not be answered by the model when the crate declares a
/// crate-root trait of that name with a *default* body.
///
/// rustc prints a crate-root trait's call bare (`<u8 as Ord>::min`), and a default
/// method has no `impl` block in the dump -- its definition is printed as
/// `Ord::min`. So `resolve_callee` cannot match it by location, and before the
/// `declared_traits` guard the model answered `<u8 as Ord>::min` from `core`'s
/// ordering. `crate_root_ord_default_*` pair pins both directions: the body returns
/// a constant, so the verifying half proves the body was read and the refuted half
/// proves the model was not consulted.
#[test]
fn a_bare_crate_root_trait_call_is_not_answered_by_the_model() {
    let verified = ["crate_root_ord_default_verifies_from_its_body"];
    let Some(report) = verify_realistic(&verified) else { return };
    for name in verified {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify from the default body, got: {o}");
    }
    let refuted = ["crate_root_ord_default_is_not_the_model_tripwire"];
    let Some(report) = verify_realistic(&refuted) else { return };
    for name in refuted {
        let o = outcome(&report, name);
        let FnOutcome::Failed { kind, .. } = o else {
            panic!(
                "`{name}` claims `core`'s ordering for a call the compiler resolved to \
                 the local trait and must be refuted, got: {o}"
            );
        };
        assert_eq!(kind, "postcondition", "`{name}` got: {o}");
    }
}

/// An unrelated same-named trait must not answer a bare trait call.
///
/// `m::Trait` is a different trait from the crate-root `Trait`, but rustc prints a
/// *bare* `Trait` call, so a suffix match let the module trait's body answer the
/// crate-root trait's call -- a false proof. `suffix_collision_*` pin both
/// directions: the call names the crate-root trait (whose default body returns
/// `9`), and the module trait's body returns `7`, so the refuted half fails only if
/// the module trait was *not* consulted.
#[test]
fn an_unrelated_same_named_trait_does_not_answer_a_bare_call() {
    let verified = ["suffix_collision_verifies_from_the_named_trait"];
    let Some(report) = verify_realistic(&verified) else { return };
    for name in verified {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify from the crate-root trait, got: {o}");
    }
    let refuted = ["suffix_collision_is_not_the_module_trait_tripwire"];
    let Some(report) = verify_realistic(&refuted) else { return };
    for name in refuted {
        let o = outcome(&report, name);
        let FnOutcome::Failed { kind, .. } = o else {
            panic!(
                "`{name}` claims the module trait's body for a call naming the crate-root \
                 trait and must be refuted, got: {o}"
            );
        };
        assert_eq!(kind, "postcondition", "`{name}` got: {o}");
    }
}

/// A local trait method reached through an imported library trait path must not be
/// answered by the model.
///
/// `use std::convert::From;` plus a local `impl From<Local> for u16` prints the call
/// `<u16 as From<Local>>::from` -- the *bare* library name, because the local `From`
/// is the only one in scope. The crate declares no trait named `From`, so the
/// `declared_traits` guard alone would not refuse it, and the model would answer a
/// call the compiler resolved to the local body. The owner (`u16`), the method
/// (`from`) and the *self type* (`Local`, the conversion's source) together name
/// the same method. This lives in its own source file rather than the shared corpus
/// because the corpus's own `conv_shim::From` forces rustc to qualify the library
/// trait, which hides the bare spelling.
#[test]
fn a_local_impl_of_an_imported_trait_does_not_answer_the_model() {
    let src = r#"macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }

pub struct Local;
impl From<Local> for u16 {
    fn from(_x: Local) -> Self {
        107
    }
}
spec! {
    requires: true,
    ensures: ret == 107,
}
pub fn imported_from_verifies_from_the_local_body() -> u16 {
    <u16 as From<Local>>::from(Local)
}
spec! {
    requires: true,
    ensures: ret == 7,
}
pub fn imported_from_is_not_the_model_tripwire() -> u16 {
    <u16 as From<Local>>::from(Local)
}
"#;
    let o = verify_src("imported_from_verifies_from_the_local_body", src);
    let Some(o) = o else { return };
    assert!(
        o.is_verified(),
        "`imported_from_verifies_from_the_local_body` must verify from the local body, got: {o}"
    );
    let o = verify_src("imported_from_is_not_the_model_tripwire", src);
    let Some(o) = o else { return };
    match &o {
        FnOutcome::Failed { kind, .. } => {
            assert_eq!(kind, "postcondition", "the tripwire got: {o}");
        }
        _ => panic!(
            "`imported_from_is_not_the_model_tripwire` claims the modelled conversion for a \
             call the compiler resolved to a local impl and must be refuted, got: {o}"
        ),
    }
}

/// A trait's *default* body must not answer a call that an `impl` overrides.
///
/// rustc keeps a trait's default method in the dump even when an implementation
/// overrides it, so `<Foo as T>::value(&foo)` names the override while the trait's
/// own `T::value` body (returning `1`) is still present. `resolve_callee` used to
/// prefer the default body, inlining `1` for code Rust returns `2` from -- a false
/// proof. The impl candidate must win; the default is used only when no impl
/// overrides the method.
#[test]
fn an_override_wins_over_the_traits_default_body() {
    let src = r#"macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }

pub trait T { fn value(&self) -> u8 { 1 } }
pub struct Foo(pub u8);
impl T for Foo { fn value(&self) -> u8 { 2 } }
pub struct Bar(pub u8);
impl T for Bar {}
spec! {
    requires: true,
    ensures: ret == 2,
}
pub fn override_verifies(f: &Foo) -> u8 { <Foo as T>::value(f) }
spec! {
    requires: true,
    ensures: ret == 1,
}
pub fn override_is_not_the_default_tripwire(f: &Foo) -> u8 { <Foo as T>::value(f) }
spec! {
    requires: true,
    ensures: ret == 1,
}
pub fn default_still_used_when_not_overridden(b: &Bar) -> u8 { <Bar as T>::value(b) }
"#;
    let Some(o) = verify_src("override_verifies", src) else { return };
    assert!(o.is_verified(), "the override's body must answer, got: {o}");
    let Some(o) = verify_src("default_still_used_when_not_overridden", src) else { return };
    assert!(o.is_verified(), "the default must still answer when nothing overrides it, got: {o}");
    let Some(o) = verify_src("override_is_not_the_default_tripwire", src) else { return };
    match &o {
        FnOutcome::Failed { kind, .. } => assert_eq!(kind, "postcondition", "got: {o}"),
        _ => panic!("the default body must not answer a call an impl overrides, got: {o}"),
    }
}

/// A local `impl` of a generic trait must not answer a call to a *different*
/// instantiation of the same trait.
///
/// `impl From<Local> for u16` and the library's `impl From<u8> for u16` share the
/// trait path (`From`), the owner (`u16`) and the method (`from`); only the trait's
/// *argument* separates them. A call `<u16 as From<u8>>::from(7u8)` must reach the
/// modelled widening (`7`), and the local impl's own call
/// `<u16 as From<Local>>::from(...)` must reach its body (`107`).
#[test]
fn a_generic_traits_arguments_separate_its_impls() {
    let src = r#"macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }

pub struct Local;
impl From<Local> for u16 { fn from(_: Local) -> Self { 107 } }
spec! {
    requires: true,
    ensures: ret == 7,
}
pub fn library_call_is_seven() -> u16 { <u16 as From<u8>>::from(7u8) }
spec! {
    requires: true,
    ensures: ret == 107,
}
pub fn library_call_is_not_the_local_body_tripwire() -> u16 { <u16 as From<u8>>::from(7u8) }
spec! {
    requires: true,
    ensures: ret == 107,
}
pub fn local_call_is_onehundredseven() -> u16 { <u16 as From<Local>>::from(Local) }
"#;
    let Some(o) = verify_src("library_call_is_seven", src) else { return };
    assert!(o.is_verified(), "the modelled widening must answer a library call, got: {o}");
    let Some(o) = verify_src("local_call_is_onehundredseven", src) else { return };
    assert!(o.is_verified(), "the local impl's own instantiation must answer, got: {o}");
    let Some(o) = verify_src("library_call_is_not_the_local_body_tripwire", src) else { return };
    match &o {
        FnOutcome::Failed { kind, .. } => assert_eq!(kind, "postcondition", "got: {o}"),
        _ => panic!("a different instantiation's body must not answer, got: {o}"),
    }
}

/// A trait imported into a module must be recorded as the imported path, not
/// prefixed with the module.
///
/// `use std::convert::From;` makes a bare `From` inside `m` the standard trait, and
/// rustc prints the call with the library path. The scanner used to prefix `m`
/// unconditionally (`m::From`), which matched no call -- a lost proof. The same
/// must hold after a nested module, whose scope used to reset the parent's imports.
#[test]
fn an_imported_trait_keeps_its_imported_path() {
    let src = r#"macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }

pub struct Local;
pub mod m {
    use std::convert::From;
    pub mod nested {}
    impl From<crate::Local> for u16 { fn from(_: crate::Local) -> Self { 107 } }
}
spec! {
    requires: true,
    ensures: ret == 107,
}
pub fn imported_trait_after_nested_module() -> u16 {
    <u16 as std::convert::From<crate::Local>>::from(crate::Local)
}
"#;
    let Some(o) = verify_src("imported_trait_after_nested_module", src) else { return };
    assert!(o.is_verified(), "the impl must be reached through its imported trait, got: {o}");
}

/// A multi-line, restricted-visibility import must still be read.
///
/// `pub(crate) use a::b::{\n    C,\n};` is one item whose path spans lines and
/// whose visibility is not a bare `pub `. A line-at-a-time scan with a `pub `
/// prefix missed it, so the impl was qualified with its own module and no call
/// matched -- a lost proof.
#[test]
fn a_multiline_restricted_import_is_read() {
    let src = r#"macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }

pub struct Local;
pub mod m {
    pub(crate) use std::convert::{
        From,
    };
    impl From<crate::Local> for u16 { fn from(_: crate::Local) -> Self { 107 } }
}
spec! {
    requires: true,
    ensures: ret == 107,
}
pub fn multiline_imported_trait() -> u16 {
    <u16 as std::convert::From<crate::Local>>::from(crate::Local)
}
"#;
    let Some(o) = verify_src("multiline_imported_trait", src) else { return };
    assert!(o.is_verified(), "a folded restricted import must still qualify the trait, got: {o}");
}

/// A nested trait argument keeps its balanced spelling on both sides of the match.
///
/// `call_trait_args` reads the argument off the *call* (`<W as Wrap<Option<u8>>>::get`)
/// and `impl_trait_args` reads it off the impl header (`impl Wrap<Option<u8>> for W`).
/// A "strip trailing `>`" reading on the call side cuts *every* closing bracket, so
/// the call would say `Option<u8` while the impl says `Option<u8>`, the two would not
/// match, and the local body would be lost -- the call reported as unmodelled. The
/// verifying entry is the evidence the body answers; the tripwire is the evidence it
/// is the *body* (returning `9`) and not a guess, since a model that constant-folded
/// or refused would not refute it.
#[test]
fn a_nested_trait_argument_keeps_its_balanced_spelling() {
    let src = r#"macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }

pub trait Wrap<T> { fn get(&self) -> u8; }
pub struct W;
impl Wrap<Option<u8>> for W { fn get(&self) -> u8 { 9 } }
spec! {
    requires: true,
    ensures: ret == 9,
}
pub fn nested_arg(w: &W) -> u8 { <W as Wrap<Option<u8>>>::get(w) }
spec! {
    requires: true,
    ensures: ret == 4,
}
pub fn nested_arg_tripwire(w: &W) -> u8 { <W as Wrap<Option<u8>>>::get(w) }
"#;
    let Some(o) = verify_src("nested_arg", src) else { return };
    assert!(o.is_verified(), "a nested trait argument must still find the local body, got: {o}");
    let Some(o) = verify_src("nested_arg_tripwire", src) else { return };
    match &o {
        FnOutcome::Failed { kind, .. } => assert_eq!(kind, "postcondition", "got: {o}"),
        _ => panic!("the local body returns 9, so `ret == 4` must be refuted, got: {o}"),
    }
}

/// A `#[cfg]`-disabled later `use` must not displace a live alias.
///
/// The import scan reads raw source, but rustc emits MIR only for enabled items. A
/// live `use std::convert::From as Conv;` binds `Conv`; a later
/// `#[cfg(any())] use crate::Other as Conv;` binds nothing, yet a naive map lets it
/// overwrite the live binding, so the impl written with `Conv` is recorded with the
/// wrong trait and its call is lost. The verifying entry is the evidence the live
/// binding still resolves; the tripwire is the evidence the *local body* (returning
/// `107`) answers, not the model. The same-line attribute form is covered too, since
/// the attribute hides the `use` keyword from the scanner there.
#[test]
fn a_cfg_disabled_import_does_not_displace_a_live_alias() {
    let src = r#"macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }

pub trait From { fn make() -> u8 { 0 } }
pub mod m {
    use std::convert::From as Conv;
    pub struct Local;
    impl Conv<Local> for u16 { fn from(_: Local) -> Self { 107 } }
    #[cfg(any())]
    use crate::Other as Conv;
}
pub struct Other;
spec! {
    requires: true,
    ensures: ret == 107,
}
pub fn cfg_live() -> u16 { <u16 as std::convert::From<m::Local>>::from(m::Local) }
spec! {
    requires: true,
    ensures: ret == 5,
}
pub fn cfg_tripwire() -> u16 { <u16 as std::convert::From<m::Local>>::from(m::Local) }
"#;
    let Some(o) = verify_src("cfg_live", src) else { return };
    assert!(o.is_verified(), "a live alias must survive a later cfg-disabled import, got: {o}");
    let Some(o) = verify_src("cfg_tripwire", src) else { return };
    match &o {
        FnOutcome::Failed { kind, .. } => assert_eq!(kind, "postcondition", "got: {o}"),
        _ => panic!("the live local body returns 107, so `ret == 5` must be refuted, got: {o}"),
    }
}

/// A same-line `#[cfg] use …` is read, not skipped.
///
/// An attribute on the item's own line (`#[cfg(all())] use std::convert::From as
/// Conv;`) hides the `use` keyword from [`is_use_start`] unless the attribute is
/// split off first. When it is skipped, the binding is lost entirely, so an impl
/// written with `Conv` is qualified with its own module (`m::Conv`) and the call the
/// compiler resolved to the imported trait matches nothing -- a lost proof. The
/// condition here is *enabled*, so the binding is the live one and the call must
/// still reach the impl's body.
#[test]
fn a_same_line_cfg_import_is_read() {
    let src = r#"macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }

pub mod m {
    #[cfg(all())] use std::convert::From as Conv;
    pub struct Local;
    impl Conv<Local> for u16 { fn from(_: Local) -> Self { 107 } }
}
spec! {
    requires: true,
    ensures: ret == 107,
}
pub fn same_line_live_import() -> u16 {
    <u16 as std::convert::From<m::Local>>::from(m::Local)
}
"#;
    let Some(o) = verify_src("same_line_live_import", src) else { return };
    assert!(o.is_verified(), "a same-line cfg import must still bind, got: {o}");
}

/// A `#[cfg]` attribute that spans lines is read as a whole, not by its first line.
///
/// The same hazard as the one-line `#[cfg(any())]` case, but the attribute wraps, so
/// the line directly above the `use` is its closing `)]` rather than its `#[cfg…`.
/// A reader that inspects one line above the `use` sees `))]`, concludes there is no
/// condition, treats the disabled import as live, and lets it displace the live
/// alias -- recording the impl with the wrong trait and losing the call. The
/// verifying entry is the evidence the live alias survives; the tripwire is the
/// evidence the local body (returning `107`) answers, not the model.
#[test]
fn a_multiline_cfg_import_does_not_displace_a_live_alias() {
    let src = r#"macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }

pub trait From { fn make() -> u8 { 0 } }
pub mod m {
    use std::convert::From as Conv;
    pub struct Local;
    impl Conv<Local> for u16 { fn from(_: Local) -> Self { 107 } }
    #[cfg(all(
        any(),
    ))]
    use crate::Other as Conv;
}
pub struct Other;
spec! {
    requires: true,
    ensures: ret == 107,
}
pub fn multi_cfg_live() -> u16 { <u16 as std::convert::From<m::Local>>::from(m::Local) }
spec! {
    requires: true,
    ensures: ret == 5,
}
pub fn multi_cfg_tripwire() -> u16 { <u16 as std::convert::From<m::Local>>::from(m::Local) }
"#;
    let Some(o) = verify_src("multi_cfg_live", src) else { return };
    assert!(o.is_verified(), "a multi-line cfg import must not displace a live alias, got: {o}");
    let Some(o) = verify_src("multi_cfg_tripwire", src) else { return };
    match &o {
        FnOutcome::Failed { kind, .. } => assert_eq!(kind, "postcondition", "got: {o}"),
        _ => panic!("the live local body returns 107, so `ret == 5` must be refuted, got: {o}"),
    }
}

/// A quoted delimiter in an attribute must not hide the `#[cfg]` above it.
///
/// `#[doc = "("]` holds an unmatched `(` inside a string. A reader that counts every
/// character as a delimiter never finds the attribute's closing `]`, runs past it, and
/// then reports the whole attribute run as unreadable -- so the disabled `#[cfg]`
/// import above it is treated as live and displaces the live alias. The impl written
/// with `Conv` is then recorded against the wrong trait and the call is lost
/// (`converts a type the model does not hold`). The verifying entry is the evidence
/// the live alias survives; the tripwire is the evidence the local body (returning
/// `107`) answers, not the model.
#[test]
fn a_quoted_delimiter_does_not_hide_a_cfg_import() {
    let src = r#"macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }

pub trait From { fn make() -> u8 { 0 } }
pub mod m {
    use std::convert::From as Conv;
    pub struct Local;
    impl Conv<Local> for u16 { fn from(_: Local) -> Self { 107 } }
    #[cfg(any())]
    #[doc = "("]
    use crate::Other as Conv;
}
pub struct Other;
spec! {
    requires: true,
    ensures: ret == 107,
}
pub fn quoted_cfg_live() -> u16 { <u16 as std::convert::From<m::Local>>::from(m::Local) }
spec! {
    requires: true,
    ensures: ret == 5,
}
pub fn quoted_cfg_tripwire() -> u16 { <u16 as std::convert::From<m::Local>>::from(m::Local) }
"#;
    let Some(o) = verify_src("quoted_cfg_live", src) else { return };
    assert!(o.is_verified(), "a quoted delimiter must not hide the cfg import, got: {o}");
    let Some(o) = verify_src("quoted_cfg_tripwire", src) else { return };
    match &o {
        FnOutcome::Failed { kind, .. } => assert_eq!(kind, "postcondition", "got: {o}"),
        _ => panic!("the live local body returns 107, so `ret == 5` must be refuted, got: {o}"),
    }
}

/// The corpus pins both directions for the nested argument and the cfg alias.
///
/// The inline tests above use a small self-contained source; these entries sit in
/// `examples/mir_targets/realistic.rs` beside the other trait-identity fixtures, so
/// the two new spellings are exercised against the full corpus (where `conv_shim`
/// already forces rustc to qualify the standard path) and not only in isolation.
#[test]
fn the_nested_argument_and_cfg_alias_fixtures_are_decided() {
    let verified = [
        "nested_trait_arg_finds_the_body",
        "cfg_disabled_import_keeps_the_live_alias",
        "cfg_multiline_import_keeps_the_live_alias",
        "cfg_quoted_delimiter_keeps_the_live_alias",
    ];
    let Some(report) = verify_realistic(&verified) else { return };
    for name in verified {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify from its local body, got: {o}");
    }
    let refuted = [
        "nested_trait_arg_is_not_a_guess_tripwire",
        "cfg_disabled_import_is_not_the_wrong_alias_tripwire",
        "cfg_multiline_import_is_not_the_wrong_alias_tripwire",
        "cfg_quoted_delimiter_is_not_the_wrong_alias_tripwire",
    ];
    let Some(report) = verify_realistic(&refuted) else { return };
    for name in refuted {
        let o = outcome(&report, name);
        let FnOutcome::Failed { kind, .. } = o else {
            panic!("`{name}` is a false contract that must be refuted, got: {o}");
        };
        assert_eq!(kind, "postcondition", "`{name}` got: {o}");
    }
}

/// The bitwise complement `!x`, pinned by a true half and a tripwire.
///
/// `!x` is `all_ones ^ x` at the *operand's* width. The model reads that width
/// off the value and refuses a destination local that disagrees with it, rather
/// than masking at `dest_w`. The three verifying entries name the operand's width
/// at three different widths (`u8`, `i8`, `u32`), so a model that masked at the
/// wrong width would fail the `u32` one; `not_of_zero_is_minus_one` pins the sign
/// the value keeps; and the tripwire is the evidence the operand is read at all,
/// since a constant `255` (or `all_ones ^ 0`) would prove it.
#[test]
fn the_bitwise_complement_reads_the_operands_width() {
    let verified = [
        "not_of_zero_is_all_ones",
        "not_is_255_minus_x",
        "not_of_zero_is_minus_one",
        "not_of_zero_u32_is_all_ones",
    ];
    let Some(report) = verify_realistic(&verified) else { return };
    for name in verified {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify, got: {o}");
    }
    let Some(report) = verify_realistic(&["not_is_always_255_tripwire"]) else { return };
    let o = outcome(&report, "not_is_always_255_tripwire");
    let FnOutcome::Failed { .. } = o else {
        panic!("`not_is_always_255_tripwire` is false and must be refuted, got: {o}");
    };
}

/// The struct-literal tripwire's counterexample is the `hi` operand, not `lo`.
///
/// [`struct_literal_order_tripwire`] writes `lo: 7` and `hi: a` and claims
/// `ret == a`. Refuting it is not enough on its own: a wrong model could refute
/// it for the wrong reason. What makes the refutation *evidence* is the witness,
/// which must be `a == 0` -- the value that differs from `lo`'s `7`. A model that
/// read the fields in write order would have *proved* this contract instead, so
/// reaching a witness at all is the proof the order is declaration order.
#[test]
fn a_struct_literal_reads_fields_in_declaration_order() {
    let Some(report) = verify_realistic(&["struct_literal_order_tripwire"]) else { return };
    let o = outcome(&report, "struct_literal_order_tripwire");
    let FnOutcome::Failed { detail, .. } = o else {
        panic!("`struct_literal_order_tripwire` must be refuted, got: {o}");
    };
    assert!(
        detail.contains("= 0"),
        "the witness must be `a == 0`, the value that is not `lo` (7); got: {detail}"
    );
}

/// A contract can name a *tuple struct's* field, numbered by position.
///
/// `struct_head` recognised only a braced body, so `pub struct Pair(pub u8, pub u8)`
/// was never read as a struct at all: the argument's type stayed an opaque
/// `Other("Pair")` with no layout, and a contract naming `p.0` was refused as
/// "unknown name `p`". The positional index *is* the MIR field index, so `p.0` and
/// the body's `(*_1).0` must name one variable -- and each field must be its own
/// input, not one aliased word.
///
/// The four verdicts pin the four things that could go wrong: the read itself
/// (`verified`), the read through a reference as well as by value (`verified`),
/// the declared *signedness* of the field (`verified`, and only under the signed
/// reading), and the independence of the two fields (`FAILED`, which is the
/// evidence they are distinct -- one shared variable would make the contract hold).
#[test]
fn a_contract_can_name_a_tuple_struct_field() {
    let Some(report) = verify_realistic(&[
        "tuple_struct_reads_its_field",
        "tuple_struct_by_value_reads_its_field",
        "tuple_struct_signed_field_reads",
        "tuple_struct_fields_are_independent",
    ]) else {
        return;
    };
    for name in [
        "tuple_struct_reads_its_field",
        "tuple_struct_by_value_reads_its_field",
        "tuple_struct_signed_field_reads",
    ] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must be read and verified; got: {o}");
    }
    let o = outcome(&report, "tuple_struct_fields_are_independent");
    assert!(
        !o.is_verified(),
        "`tuple_struct_fields_are_independent` must be refuted -- a reader that aliased \
         `p.0` and `p.1` to one variable would prove it; got: {o}"
    );
}

/// A tuple field the layout does not have is refused, and so is a *nested* path
/// that walks off the layout.
///
/// `Pair` has two fields, so `p.2` names nothing. Binding it to a fabricated word
/// would let a contract about a field that does not exist be proved, which is a
/// false proof rather than a missing one. `n.p.q` is the nested form: `n.p` *is*
/// bound (it is a `Pair` with a layout), but `Pair` has no field `q`, so the walk
/// stops and the path is refused -- naming the segment that failed, not the whole
/// path. Both refusals are the safe direction, and this pins them.
#[test]
fn a_tuple_struct_field_that_does_not_exist_is_refused() {
    let Some(report) = verify_realistic(&[
        "tuple_struct_out_of_range_field_is_refused",
        "nested_path_off_the_layout_is_refused",
    ]) else {
        return;
    };
    for name in [
        "tuple_struct_out_of_range_field_is_refused",
        "nested_path_off_the_layout_is_refused",
    ] {
        let o = outcome(&report, name);
        assert!(
            !o.is_verified(),
            "`{name}` names a field the layout does not have and must be refused; got: {o}"
        );
    }
}

/// A *nested* field projection is read: the contract's `n.p.0` and the body's
/// `(((*_1).0).0)` are one input.
///
/// This is the coverage the nested support adds. `n.p.0` is a walk of two field
/// projections, and the single-field store would answer it by looking up the
/// *outer* field `(local, 0)` twice -- returning `n.p` for `n.p.0`, a wrong value
/// rather than a missing one. The positive entry pins that the contract and the
/// body agree; the false entry pins that a disagreeing postcondition is refuted,
/// so the agreement is not vacuous.
#[test]
fn a_nested_field_projection_is_read_and_agrees_with_the_body() {
    let Some(report) = verify_realistic(&[
        "nested_tuple_projection_reads",
        "nested_tuple_projection_is_the_body_value",
        "nested_named_field_reads_its_leaf",
        "nested_named_field_is_the_body_value",
    ]) else {
        return;
    };
    for name in [
        "nested_tuple_projection_reads",
        "nested_tuple_projection_is_the_body_value",
        "nested_named_field_reads_its_leaf",
        "nested_named_field_is_the_body_value",
    ] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify, got: {o}");
    }
}

/// Two nested leaves are *independent* inputs, which is what makes the binding a
/// walk rather than a single field.
///
/// The entries are toothed: each claims a value the body does not return, so a
/// reader that aliased the leaves (`n.p.0` with `n.p.1`, or `o.i.v` with `o.j.v`)
/// would *verify* them -- a false proof. Refuted is the evidence the leaves are
/// distinct, and each counterexample names two different variables, which is the
/// same evidence at the term level.
#[test]
fn nested_leaves_under_different_paths_are_independent() {
    let Some(report) = verify_realistic(&[
        "nested_tuple_projection_leaves_are_independent",
        "nested_named_leaves_under_different_fields",
    ]) else {
        return;
    };
    for name in [
        "nested_tuple_projection_leaves_are_independent",
        "nested_named_leaves_under_different_fields",
    ] {
        let o = outcome(&report, name);
        assert!(
            matches!(o, FnOutcome::Failed { .. }),
            "`{name}` must be refuted: aliasing its leaves would make it a false proof; got: {o}"
        );
    }
}

/// A field write through a `&mut` is applied, not dropped -- and the *false*
/// direction is refuted rather than proved.
///
/// This is the regression for the two-sided soundness bug: `((*_1).0) = v` (a
/// `Deref` then a `Field`) had no arm in the store and fell to the drop arm. The
/// true entry pins that the write lands (the read after it is `5`); the false entry
/// pins that the postcondition is checked against the written value, so a contract
/// the body cannot satisfy is refuted instead of proved. Dropping the write makes
/// the false entry *verify* and the true entry fail, so both directions are toothed.
#[test]
fn a_field_write_through_a_mutable_reference_is_applied() {
    let Some(report) = verify_realistic(&[
        "field_write_through_ref_mut_takes_effect",
        "field_write_through_ref_mut_is_not_dropped",
    ]) else {
        return;
    };
    let true_dir = outcome(&report, "field_write_through_ref_mut_takes_effect");
    assert!(
        true_dir.is_verified(),
        "the write must land, so the read after it is 5; got: {true_dir}"
    );
    let false_dir = outcome(&report, "field_write_through_ref_mut_is_not_dropped");
    assert!(
        matches!(false_dir, FnOutcome::Failed { .. }),
        "the postcondition must be checked against the written value, so `ret == 0` \
         is refuted rather than proved; got: {false_dir}"
    );
}

/// A *nested* write through a `&mut` lands where the nested read looks, and the
/// false direction is refuted rather than proved.
///
/// The nested store is keyed by the whole chain, so `o.i.v = 5` and the later
/// `o.i.v` meet in one entry. A write keyed by a single field index would land on
/// the *outer* field and the read would see the caller's value: the true entry
/// would fail and the false entry would *verify*. Both are toothed here.
#[test]
fn a_nested_field_write_through_a_mutable_reference_is_applied() {
    let Some(report) = verify_realistic(&[
        "nested_field_write_through_ref_mut_takes_effect",
        "nested_field_write_through_ref_mut_is_not_dropped",
    ]) else {
        return;
    };
    let true_dir = outcome(&report, "nested_field_write_through_ref_mut_takes_effect");
    assert!(true_dir.is_verified(), "the nested write must land; got: {true_dir}");
    let false_dir = outcome(&report, "nested_field_write_through_ref_mut_is_not_dropped");
    assert!(
        matches!(false_dir, FnOutcome::Failed { .. }),
        "the nested write must be seen by the read, so `ret == 0` is refuted; got: {false_dir}"
    );
}

/// A seeded nested path is invalidated by a write that replaces the value it was
/// part of, and carried across by a whole-struct copy.
///
/// Both are the "parts" rules one level down. `o.i = Inner { v: 9 }` makes the seed
/// for `o.i.v` stale, so the false entry must be *refuted*: the body returns 9, and
/// `ret == 3` holds for no input. Refutation and not merely "not proved" is the
/// assertion, because a refusal also satisfies "not proved" and says nothing about
/// whether the write landed. The body lowers to a struct literal into a temp
/// followed by `(_1.0) = move _2`, so reaching the refutation is the evidence the
/// temp's parts were re-rooted under the destination's nested place.
///
/// And `c = d` must move `c.i.v` with the copy, so the true entry verifies -- it
/// pins `c.i.v` and `d.i.v` to different values, so `verified` is the evidence the
/// copy moved the right one rather than leaving `c`'s own seed.
#[test]
fn a_nested_seed_is_invalidated_by_a_write_and_carried_by_a_copy() {
    let Some(report) = verify_realistic(&[
        "whole_inner_write_makes_nested_seed_stale",
        "whole_inner_write_is_seen_by_the_leaf",
        "whole_inner_write_through_a_mutable_reference_is_seen",
        "whole_inner_move_carries_the_source_leaf",
        "whole_inner_move_is_not_the_stale_destination",
        "nested_swap_carries_the_other_side",
        "nested_swap_is_not_the_stale_side",
        "nested_aggregate_argument_reaches_the_helper",
        "nested_aggregate_argument_is_not_the_stale_destination",
        "nested_return_carries_the_field",
        "nested_return_is_not_the_stale_field",
        "whole_struct_copy_carries_nested_paths",
    ]) else {
        return;
    };
    let stale = outcome(&report, "whole_inner_write_makes_nested_seed_stale");
    assert!(
        matches!(stale, FnOutcome::Failed { .. }),
        "a stale nested seed must let the false contract be refuted, not refused; got: {stale}"
    );
    // The positive direction: the same body's true contract (`ret == 9`) verifies, so
    // the refutation above is the write landing and not a blanket refusal. Without this
    // pair, an engine that answered `Unknown` for the whole shape would pass.
    let landed = outcome(&report, "whole_inner_write_is_seen_by_the_leaf");
    assert!(
        landed.is_verified(),
        "a nested aggregate write must land where the leaf reads it; got: {landed}"
    );
    // The `&mut` companion: the same write's destination carries a leading deref, so the
    // re-rooting must resolve it or the field path never matches the read.
    let via_ref = outcome(&report, "whole_inner_write_through_a_mutable_reference_is_seen");
    assert!(
        via_ref.is_verified(),
        "a nested aggregate write through a &mut must reach the target's field path; got: {via_ref}"
    );
    let copy = outcome(&report, "whole_struct_copy_carries_nested_paths");
    assert!(copy.is_verified(), "a whole-struct copy must carry nested paths; got: {copy}");
    // A nested-to-nested move: the *source* is a projection, which `copy_parts` refuses, so
    // the re-rooting has to run on a nested source too. The pair pins both directions -- the
    // source's leaf arrives (true `ret == 3`), and the destination's stale leaf is gone (the
    // false `ret == 7` is refuted rather than proved against the seed the move replaced).
    let carried = outcome(&report, "whole_inner_move_carries_the_source_leaf");
    assert!(
        carried.is_verified(),
        "a nested-to-nested move must carry the source's leaf; got: {carried}"
    );
    let stale_move = outcome(&report, "whole_inner_move_is_not_the_stale_destination");
    assert!(
        matches!(stale_move, FnOutcome::Failed { .. }),
        "the destination's pre-move leaf must not survive the move; got: {stale_move}"
    );
    // A nested swap: both sides are nested, so this is the shape that needs the guard to be
    // "either side nested". The two seeds are distinct, so the positive verdict is the
    // *other* side's leaf, and the twin claims the pre-swap one survived.
    let swap = outcome(&report, "nested_swap_carries_the_other_side");
    assert!(swap.is_verified(), "a nested swap must exchange the leaves; got: {swap}");
    let swap_stale = outcome(&report, "nested_swap_is_not_the_stale_side");
    assert!(
        matches!(swap_stale, FnOutcome::Failed { .. }),
        "a nested swap must not keep each side's own leaf; got: {swap_stale}"
    );
    // A nested aggregate handed to a helper by value and read back: the callee's return is a
    // struct it built field-by-field, so the merge has to carry the *fields* (not only the
    // tuple components). The positive verdict is the value that crossed; the twin claims a
    // different one survived and must be refuted.
    let through = outcome(&report, "nested_aggregate_argument_reaches_the_helper");
    assert!(
        through.is_verified(),
        "a returned nested aggregate must keep its fields; got: {through}"
    );
    let through_stale =
        outcome(&report, "nested_aggregate_argument_is_not_the_stale_destination");
    assert!(
        matches!(through_stale, FnOutcome::Failed { .. }),
        "a returned nested aggregate must not keep a stale field; got: {through_stale}"
    );
    // A returned struct's *field* read back through a two-level path: the `Struct` literal
    // stores the field value but not the operand aggregate's own parts, so the field's parts
    // have to be re-rooted. The positive verdict is the value that crossed; the twin claims a
    // different leaf and must be refuted.
    let field = outcome(&report, "nested_return_carries_the_field");
    assert!(
        field.is_verified(),
        "a returned struct's field must keep its parts; got: {field}"
    );
    let field_stale = outcome(&report, "nested_return_is_not_the_stale_field");
    assert!(
        matches!(field_stale, FnOutcome::Failed { .. }),
        "a returned struct's field must not keep a stale leaf; got: {field_stale}"
    );
}

/// A helper's write through a `&mut` argument reaches the caller, and the false
/// direction is refuted rather than proved.
///
/// The write happens one frame down. Only an *indexed* reference argument's store
/// used to cross back, so a field or scalar write through a `&mut` died with the
/// callee's frame: the caller read its own seed, and the false entries here were
/// *proved* against bodies that return 5. Both directions are toothed.
#[test]
fn a_helpers_write_through_a_mutable_reference_reaches_the_caller() {
    let Some(report) = verify_realistic(&[
        "helper_field_write_reaches_the_caller",
        "a_stale_caller_field_cannot_prove_this",
        "helper_scalar_write_reaches_the_caller",
        "a_stale_caller_scalar_cannot_prove_this",
    ]) else {
        return;
    };
    for name in
        ["helper_field_write_reaches_the_caller", "helper_scalar_write_reaches_the_caller"]
    {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "the helper's write must reach the caller; got: {o}");
    }
    for name in ["a_stale_caller_field_cannot_prove_this", "a_stale_caller_scalar_cannot_prove_this"]
    {
        let o = outcome(&report, name);
        assert!(
            matches!(o, FnOutcome::Failed { .. }),
            "a stale caller value must not prove this false contract; got: {o}"
        );
    }
}

/// An array element reached through a struct field is one word for the contract and
/// the body, and the write does not spill onto a sibling array's element.
///
/// `x.a[0] = 9; x.a[0]` lowers to `(_1.0)[_2] = 9` and `_0 = copy (_1.0)[_4]`. The
/// element store used to be keyed by the local and index alone, so the write landed
/// under a key the read never consulted and the read returned the *seed* -- the false
/// direction, where `ret == 5` was proved against a body returning 9. The pair below
/// toothes both directions, and the sibling entry catches a store that collides
/// `x.a[0]` with `x.b[0]`.
#[test]
fn a_field_array_element_is_one_word_for_contract_and_body() {
    let Some(report) = verify_realistic(&[
        "a_field_array_write_is_read_back",
        "a_field_array_write_is_not_the_seed",
        "a_sibling_field_array_is_untouched",
    ]) else {
        return;
    };
    let read_back = outcome(&report, "a_field_array_write_is_read_back");
    assert!(
        read_back.is_verified(),
        "a write to `x.a[0]` must be seen by the read of `x.a[0]`; got: {read_back}"
    );
    let seed = outcome(&report, "a_field_array_write_is_not_the_seed");
    assert!(
        matches!(seed, FnOutcome::Failed { .. }),
        "the pre-write seed must not prove a contract the body refutes; got: {seed}"
    );
    let sibling = outcome(&report, "a_sibling_field_array_is_untouched");
    assert!(
        sibling.is_verified(),
        "writing `x.a[0]` must leave `x.b[0]` alone; got: {sibling}"
    );
}

/// A *nested* array field is the same word on both sides, and the false direction is
/// refuted rather than proved.
///
/// `o.i.a[0] = 9; o.i.a[0]` lowers to `(_1.0.0)[_2] = 9` and `_0 = copy (_1.0.0)[_4]`. The
/// element's key is the whole field chain, so a store that stopped at the first field
/// (`o.i`) would key the write and the read differently and leave the read at the seed --
/// the false direction, where `ret == 5` was proved against a body returning 9.
#[test]
fn a_nested_field_array_element_is_one_word_for_contract_and_body() {
    let Some(report) = verify_realistic(&[
        "a_nested_field_array_write_is_read_back",
        "a_nested_field_array_write_is_not_the_seed",
    ]) else {
        return;
    };
    let read_back = outcome(&report, "a_nested_field_array_write_is_read_back");
    assert!(
        read_back.is_verified(),
        "a nested `o.i.a[0]` write must be seen by the read; got: {read_back}"
    );
    let seed = outcome(&report, "a_nested_field_array_write_is_not_the_seed");
    assert!(
        matches!(seed, FnOutcome::Failed { .. }),
        "the nested pre-write seed must not prove a refuted contract; got: {seed}"
    );
}

/// A helper's write to a field-array element crosses back through a `&mut`, and the read
/// crosses *in* through a `&`; both directions are pinned, with their false twins.
///
/// The callee's write lands in its own element store, so the caller's store has to be
/// re-based on return -- for a whole-local target (`set_first_element(&mut x)`) unchanged,
/// and for a sub-place target (`set_holder_first_element(&mut o.i)`) prefixed with the
/// target's chain. Without either the write died with the frame, the caller read its seed,
/// and the false `ret == 5` was **proved** against a body returning 9. The read direction is
/// the same re-basing run the other way: `read_holder_first_element(&o.i)` reads the
/// caller's `o.i.a[0]`, so its contract decides rather than being unreadable.
#[test]
fn a_field_array_element_crosses_a_reference_argument() {
    let Some(report) = verify_realistic(&[
        "a_helper_field_array_write_is_read_back",
        "a_helper_field_array_write_is_not_the_seed",
        "a_helper_nested_aggregate_element_write_is_read_back",
        "a_helper_nested_aggregate_element_write_is_not_the_seed",
        "a_helper_nested_aggregate_element_read",
    ]) else {
        return;
    };
    for name in [
        "a_helper_field_array_write_is_read_back",
        "a_helper_nested_aggregate_element_write_is_read_back",
        "a_helper_nested_aggregate_element_read",
    ] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must decide and verify; got: {o}");
    }
    for name in [
        "a_helper_field_array_write_is_not_the_seed",
        "a_helper_nested_aggregate_element_write_is_not_the_seed",
    ] {
        let o = outcome(&report, name);
        assert!(
            matches!(o, FnOutcome::Failed { .. }),
            "`{name}` claims the caller's seed after the helper wrote; must be refuted, got: {o}"
        );
    }
}

/// An array handed over *as* a reference target (`&mut x.a`) used to be `Incomplete` in
/// both directions. It now *decides*: the true contract is `Verified` and the false twin is
/// refuted -- and the pair is what makes the decision trustworthy.
///
/// The callee's `(*_1)[0] = 9` lands in its own element store, and the return path re-bases
/// it onto the caller's field-path store (`elems_path`, keyed by the target's *field* chain
/// `x.a`) instead of dropping it. So the caller's `x.a[0]` read is the callee's write, and
/// `ensures: ret == 9` is provable while `ensures: ret == 5` is refuted.
///
/// This test is the tripwire the *hole* was pinned with, kept in its stronger form. While
/// the shape was unmodelled it asserted `Incomplete`; the fix that made it decide is the
/// change the pin existed to police, because a re-basing that connected the two stores the
/// *wrong* way would turn the false twin `Verified` first. It did not: the twin is `Failed`.
/// If a later change makes this shape incomplete again, this test fails and the two
/// assertions say exactly which direction broke.
#[test]
fn a_whole_array_target_decides_without_a_false_proof() {
    let Some(report) = verify_realistic(&[
        "a_whole_array_target_crosses_a_reference_argument",
        "a_whole_array_target_cannot_prove_this",
        "a_whole_array_target_sibling_cannot_prove_this",
        "a_whole_array_target_read_crosses_a_reference_argument",
        "a_whole_array_target_read_cannot_prove_this",
        "a_whole_array_target_read_modify_write",
        "a_whole_array_target_read_modify_write_cannot_prove_this",
        "a_nested_whole_array_target_crosses_a_reference_argument",
        "a_nested_whole_array_target_cannot_prove_this",
        "a_whole_array_target_crosses_two_frames",
        "a_whole_array_target_crosses_two_frames_cannot_prove_this",
    ]) else {
        return;
    };
    let o = outcome(&report, "a_whole_array_target_crosses_a_reference_argument");
    assert!(
        o.is_verified(),
        "a `&mut x.a` array target re-bases the callee's element onto the caller's \
         field-path store, so the true contract `ret == 9` must verify; got: {o}"
    );
    let twin = outcome(&report, "a_whole_array_target_cannot_prove_this");
    assert!(
        matches!(twin, FnOutcome::Failed { .. }),
        "the false twin of the whole-array target must be refuted -- a false proof is the \
         only unacceptable outcome, and `Verified` here would be one; got: {twin}"
    );
    let sibling = outcome(&report, "a_whole_array_target_sibling_cannot_prove_this");
    assert!(
        !sibling.is_verified(),
        "the write to `x.a` must not leak into the sibling `x.b`, so claiming `x.b[0]` \
         changed must not verify; got: {sibling}"
    );
    let read = outcome(&report, "a_whole_array_target_read_crosses_a_reference_argument");
    assert!(
        read.is_verified(),
        "the read-only direction (`peek_of(&x.a)`) must also decide -- the carry-in \
         re-bases the caller's field-path element onto the callee's store, so `ret == 5` \
         verifies; got: {read}"
    );
    let read_twin = outcome(&report, "a_whole_array_target_read_cannot_prove_this");
    assert!(
        !read_twin.is_verified(),
        "the false read-only twin (`ret == 7` against a seed of 5) must not verify; \
         got: {read_twin}"
    );
    // The three shapes below are the re-basing rule applied where it is easy to get it
    // *partly* right: a read-modify-write (both directions in one call), a target two
    // fields deep, and a write that crosses two frames. Each true contract must decide
    // and each false twin must be refuted -- a fix that handled only the write, or only
    // a single-field chain, or only one frame, would show up as a true direction stuck
    // `Incomplete` (or, worse, a twin `Verified`).
    for (name, want) in [
        ("a_whole_array_target_read_modify_write", true),
        ("a_whole_array_target_read_modify_write_cannot_prove_this", false),
        ("a_nested_whole_array_target_crosses_a_reference_argument", true),
        ("a_nested_whole_array_target_cannot_prove_this", false),
        ("a_whole_array_target_crosses_two_frames", true),
        ("a_whole_array_target_crosses_two_frames_cannot_prove_this", false),
    ] {
        let got = outcome(&report, name);
        if want {
            assert!(got.is_verified(), "`{name}` must verify; got: {got}");
        } else {
            assert!(
                !got.is_verified(),
                "`{name}` is a false direction and must not verify; got: {got}"
            );
        }
    }
}

/// A method's returned reference to an *element* re-bases onto the caller's element.
///
/// `x.first_mut()` is `&mut x.a[0]`, and the dump names the element index with a
/// local (`_2`), not the literal. Translating the returned reference to the caller
/// therefore has to resolve that local *before* the callee's frame is gone; carrying
/// it through left a place no element store is keyed by, the write `*x.first_mut() = 9`
/// was dropped, and the contract was unreadable -- `Incomplete` in both directions.
/// The true contract must now decide and the false twin must be refuted; the sibling
/// element is the second tooth, because a fix that pinned every returned element to
/// index 0 would decide the first case and read the wrong element here.
#[test]
fn a_returned_reference_to_an_element_reaches_the_caller() {
    let Some(report) = verify_realistic(&[
        "returned_ref_to_an_element_crosses_the_frame",
        "returned_ref_to_an_element_cannot_prove_this",
        "returned_ref_to_a_sibling_element",
        "returned_ref_to_a_sibling_element_cannot_prove_this",
        "returned_ref_write_is_not_the_wrong_element_cannot_prove_this",
        "returned_ref_to_a_whole_array_element",
        "returned_ref_to_a_whole_array_element_cannot_prove_this",
        "returned_ref_to_a_nested_whole_array_element",
        "returned_ref_to_a_nested_whole_array_element_cannot_prove_this",
        "returned_ref_to_a_guarded_symbolic_element",
        "returned_ref_to_a_guarded_symbolic_element_cannot_prove_this",
        "returned_ref_to_an_unpinned_symbolic_element_cannot_prove_this",
        "a_shifted_symbolic_element_cannot_prove_this",
        "a_shifted_symbolic_element_is_not_proved",
    ]) else {
        return;
    };
    for name in [
        "returned_ref_to_an_element_crosses_the_frame",
        "returned_ref_to_a_sibling_element",
        "returned_ref_to_a_whole_array_element",
        "returned_ref_to_a_nested_whole_array_element",
        "returned_ref_to_a_guarded_symbolic_element",
    ] {
        let o = outcome(&report, name);
        assert!(
            o.is_verified(),
            "the write through the returned element reference must reach the caller's \
             element, so `{name}` must verify; got: {o}"
        );
    }
    for name in [
        "returned_ref_to_an_element_cannot_prove_this",
        "returned_ref_to_a_sibling_element_cannot_prove_this",
        "returned_ref_write_is_not_the_wrong_element_cannot_prove_this",
        "returned_ref_to_a_whole_array_element_cannot_prove_this",
        "returned_ref_to_a_nested_whole_array_element_cannot_prove_this",
        "returned_ref_to_a_guarded_symbolic_element_cannot_prove_this",
        "returned_ref_to_an_unpinned_symbolic_element_cannot_prove_this",
    ] {
        let o = outcome(&report, name);
        assert!(
            !o.is_verified(),
            "`{name}` is a false direction -- a write aimed at the wrong element would \
             prove it; got: {o}"
        );
    }
    // The shifted-numbering pair is a *refusal* in both directions: the write may
    // land on any element, so neither `ret == 9` nor `ret == 5` decides. The false
    // direction must not be proved (the write must not be aimed at a caller local
    // that shares the callee index's number), and the true direction must not be
    // proved either (dropping the write must not leave the seed readable).
    for name in [
        "a_shifted_symbolic_element_cannot_prove_this",
        "a_shifted_symbolic_element_is_not_proved",
    ] {
        let o = outcome(&report, name);
        assert!(
            !o.is_verified(),
            "`{name}` must be a refusal -- a symbolic returned index names no single \
             element, so neither direction decides; got: {o}"
        );
    }
}

/// A helper's write-back covers a *nested* field and a read-modify-write, and the
/// caller's field seed crosses *into* the callee so the callee's read is modelled.
///
/// Two teeth here. The nested false entry must be refuted rather than proved: the
/// helper sets `o.i.v = 9` and the caller returns it, so `ret == 0` is false, and
/// a write-back that missed the nested chain would leave the caller's seed and
/// prove it. The read-modify-write entry must *verify* rather than go incomplete:
/// the callee reads `c.lo` before writing it, so carrying only the write-back
/// leaves the callee's read unmodelled and the contract unproved.
#[test]
fn a_helpers_nested_and_read_modify_write_through_a_reference_are_modelled() {
    let Some(report) = verify_realistic(&[
        "helper_nested_write_reaches_the_caller",
        "a_stale_caller_nested_cannot_prove_this",
        "read_modify_write_through_helper",
    ]) else {
        return;
    };
    let nested = outcome(&report, "helper_nested_write_reaches_the_caller");
    assert!(nested.is_verified(), "a nested write through a `&mut` must reach the caller; got: {nested}");
    let stale = outcome(&report, "a_stale_caller_nested_cannot_prove_this");
    assert!(
        matches!(stale, FnOutcome::Failed { .. }),
        "a stale caller nested value must not prove this false contract; got: {stale}"
    );
    let rmw = outcome(&report, "read_modify_write_through_helper");
    assert!(rmw.is_verified(), "a read-modify-write through a `&mut` must be modelled; got: {rmw}");
}

/// The write-back keys the *target's projection*, so `&mut c.lo` lands on the
/// field and a conditional helper write follows the callee's branch.
///
/// A target with a projection (`&mut c.lo`) must not be written to the caller's
/// local: that puts a bit-vector where a struct lives and leaves `c.lo` at its
/// seed, which is how the false twin here was *proved*. A conditional helper write
/// has to reach the caller from the branch it takes, so the `c == true` case reads
/// 5 -- and the false twin, whose precondition pins both the seed and the flag,
/// must be refuted rather than proved.
#[test]
fn a_helper_write_lands_on_the_targets_projection() {
    let Some(report) = verify_realistic(&[
        "helper_write_reaches_a_field_target",
        "a_stale_field_target_cannot_prove_this",
        "helper_write_reaches_a_nested_target",
        "a_stale_nested_target_cannot_prove_this",
        "conditional_helper_write_reaches_the_caller",
        "a_conditional_helper_write_cannot_prove_this",
    ]) else {
        return;
    };
    for name in [
        "helper_write_reaches_a_field_target",
        "helper_write_reaches_a_nested_target",
        "conditional_helper_write_reaches_the_caller",
    ] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "a helper write must reach the caller; got: {o}");
    }
    for name in [
        "a_stale_field_target_cannot_prove_this",
        "a_stale_nested_target_cannot_prove_this",
        "a_conditional_helper_write_cannot_prove_this",
    ] {
        let o = outcome(&report, name);
        assert!(
            matches!(o, FnOutcome::Failed { .. }),
            "a stale caller value must not prove this false contract; got: {o}"
        );
    }
}

/// A reference to an array *element* (`set5(&mut a[0])`) is a sub-place target too,
/// and a by-value argument's callee write must stay in the callee.
///
/// Two opposite directions in one test. The element target has to land the write at
/// `a[0]` and leave `a[1]` readable: the callee holds the caller's array under its
/// own local, its whole-place write poisons that store, and copying the store back
/// unconditionally would make the *untouched* sibling unknown -- which is why the
/// copy-back is gated on a whole-local target. The by-value pair is the other
/// direction: `set_five_by_value(x)` writes its own copy, so the caller must still
/// read its seed, and a leak back would prove the false `ret == 5`.
#[test]
fn an_element_target_is_written_and_a_by_value_copy_is_not() {
    let Some(report) = verify_realistic(&[
        "helper_write_reaches_an_element_target",
        "a_stale_element_target_cannot_prove_this",
        "an_element_write_leaves_its_sibling",
        "a_stale_element_sibling_cannot_prove_this",
        "a_by_value_callee_write_stays_in_the_callee",
        "a_by_value_callee_write_cannot_prove_this",
    ]) else {
        return;
    };
    for name in [
        "helper_write_reaches_an_element_target",
        "an_element_write_leaves_its_sibling",
        "a_by_value_callee_write_stays_in_the_callee",
    ] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "this contract must verify; got: {o}");
    }
    for name in [
        "a_stale_element_target_cannot_prove_this",
        "a_stale_element_sibling_cannot_prove_this",
        "a_by_value_callee_write_cannot_prove_this",
    ] {
        let o = outcome(&report, name);
        assert!(
            matches!(o, FnOutcome::Failed { .. }),
            "a stale caller value must not prove this false contract; got: {o}"
        );
    }
}

/// A method is inlined by its *last* path component plus its receiver's type,
/// because rustc defines an inherent method as `<impl at file:line:col>::set_lo`
/// but calls it as `Config::set_lo`; a shared tail is disambiguated by the type
/// rather than guessed at.
///
/// Before the tail match, a call to any method was "unmodelled function", so a
/// method's write through `&mut self` was invisible to its caller. The tripwire
/// pins the other direction: `twist` is both a method and a free function here, so
/// the tail alone names two bodies. The call `w.twist(5)` names the receiver's
/// type, so it resolves to `Widget::twist`; inlining the free function instead
/// would write through the wrong shape and *prove* a false contract, so the
/// resolution must pick the method and refute the claim.
#[test]
fn a_method_is_inlined_by_its_tail_and_a_shared_tail_resolves_by_type() {
    let Some(report) = verify_realistic(&[
        "a_method_write_reaches_the_caller",
        "a_method_write_cannot_prove_this",
        "a_method_read_modify_write",
        "a_method_leaves_its_sibling",
        "a_stale_sibling_cannot_prove_this",
        "an_ambiguous_method_tail_cannot_prove_this",
        "a_generic_method",
        "an_associated_fn_and_builder_chain",
        "a_trait_method",
        "a_tuple_struct_read_method",
        "a_method_with_an_argument",
        "a_reborrow_target_reaches_the_caller",
        "a_stale_reborrow_cannot_prove_this",
        "a_tuple_component_target_reaches_the_caller",
        "a_stale_tuple_component_cannot_prove_this",
    ]) else {
        return;
    };
    for name in [
        "a_method_write_reaches_the_caller",
        "a_method_read_modify_write",
        "a_method_leaves_its_sibling",
        // The shapes a method can take: a generic receiver, an associated function
        // feeding a `&mut self` builder that returns `&mut Self`, a trait method,
        // and a `&self` read (on a tuple struct, and with an argument).
        "a_generic_method",
        "an_associated_fn_and_builder_chain",
        "a_trait_method",
        "a_tuple_struct_read_method",
        "a_method_with_an_argument",
    ] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "a method write must reach its caller; got: {o}");
    }
    for name in ["a_method_write_cannot_prove_this", "a_stale_sibling_cannot_prove_this"] {
        let o = outcome(&report, name);
        assert!(
            matches!(o, FnOutcome::Failed { .. }),
            "a stale caller value must not prove this false contract; got: {o}"
        );
    }
    // A reborrow (`&mut *r`) and a tuple component (`&mut t.0`) are sub-place targets
    // too, and both land on the caller's storage.
    for name in [
        "a_reborrow_target_reaches_the_caller",
        "a_tuple_component_target_reaches_the_caller",
    ] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "a sub-place target must reach the caller; got: {o}");
    }
    for name in [
        "a_stale_reborrow_cannot_prove_this",
        "a_stale_tuple_component_cannot_prove_this",
    ] {
        let o = outcome(&report, name);
        assert!(
            matches!(o, FnOutcome::Failed { .. }),
            "a stale caller value must not prove this false contract; got: {o}"
        );
    }
    // The tail `twist` is shared by `Widget::twist` and a free `twist`, but the
    // call `w.twist(5)` names its receiver's type, so the resolver picks the
    // method by that type rather than by the tail alone. Answering it from the
    // free function -- which writes through a `&mut u8` -- would *prove* this
    // false contract; the correct body sets `w.v = 5`, so the postcondition is
    // refuted. Refuted is the evidence the right body was chosen.
    let o = outcome(&report, "an_ambiguous_method_tail_cannot_prove_this");
    assert!(
        matches!(o, FnOutcome::Failed { .. }),
        "a shared method tail must resolve to the receiver's own body and refute \
         the false contract, not prove it; got: {o}"
    );
}

/// A helper that *reads* its referent, a read-modify-write helper, a two-reference
/// call, and a nested *aggregate* target.
///
/// Each of these needs something the write-only case did not. A read needs the
/// referent's value carried *into* the callee (`_1` otherwise holds the reference
/// temp's `Unknown`, so `*v` is an unmodelled operand). A sub-place aggregate target
/// (`&mut o.i`) needs both the carry-in and the write-back re-based on the target's
/// projection -- dropping the write there refuted the true `ret == 5` and *proved*
/// the false `ret == 0`, which is why the false twin is pinned.
#[test]
fn a_reference_argument_is_read_and_written_through_its_target() {
    let Some(report) = verify_realistic(&[
        "a_read_through_a_helper",
        "a_stale_read_through_a_helper_cannot_prove_this",
        "a_bump_through_a_helper",
        "a_stale_bump_cannot_prove_this",
        "two_reference_arguments",
        "two_reference_arguments_other",
        "two_reference_arguments_swapped_cannot_prove_this",
        "a_swap_through_two_references",
        "a_swap_through_two_references_other",
        "a_stale_swap_cannot_prove_this",
        "a_nested_aggregate_target_reaches",
        "a_stale_nested_aggregate_cannot_prove_this",
        "a_nested_aggregate_read",
        "a_stale_nested_aggregate_read_cannot_prove_this",
    ]) else {
        return;
    };
    for name in [
        "a_read_through_a_helper",
        "a_bump_through_a_helper",
        "two_reference_arguments",
        "two_reference_arguments_other",
        "a_swap_through_two_references",
        "a_swap_through_two_references_other",
        "a_nested_aggregate_target_reaches",
        "a_nested_aggregate_read",
    ] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "a reference argument must reach its target; got: {o}");
    }
    for name in [
        "a_stale_read_through_a_helper_cannot_prove_this",
        "a_stale_bump_cannot_prove_this",
        "two_reference_arguments_swapped_cannot_prove_this",
        "a_stale_swap_cannot_prove_this",
        "a_stale_nested_aggregate_cannot_prove_this",
        "a_stale_nested_aggregate_read_cannot_prove_this",
    ] {
        let o = outcome(&report, name);
        assert!(
            matches!(o, FnOutcome::Failed { .. }),
            "a stale or unread referent must not prove this false contract; got: {o}"
        );
    }
}

/// The receiver *shapes* a method arrives in: a nested-field receiver, two calls in
/// sequence, a reborrowed receiver, a `&self` read feeding a `&mut` write, and a leaf
/// write that must leave its sibling at the caller's seed.
///
/// These decide through the same inlining and write-back as the plain receiver, which
/// is the point: the receiver is just another argument, and its target is a sub-place
/// (`o.i`) rather than a whole local.
#[test]
fn a_method_receiver_is_a_sub_place_like_any_other_argument() {
    let Some(report) = verify_realistic(&[
        "a_method_on_a_nested_receiver",
        "a_stale_nested_receiver_cannot_prove_this",
        "two_methods_in_sequence",
        "a_stale_first_method_cannot_prove_this",
        "a_method_through_a_reborrow",
        "a_stale_reborrow_receiver_cannot_prove_this",
        "a_read_method_feeds_a_write",
        "a_stale_read_method_cannot_prove_this",
        "a_leaf_write_leaves_its_sibling",
        "a_stale_leaf_sibling_cannot_prove_this",
    ]) else {
        return;
    };
    for name in [
        "a_method_on_a_nested_receiver",
        "two_methods_in_sequence",
        "a_method_through_a_reborrow",
        "a_read_method_feeds_a_write",
        "a_leaf_write_leaves_its_sibling",
    ] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "a method receiver must reach its target; got: {o}");
    }
    for name in [
        "a_stale_nested_receiver_cannot_prove_this",
        "a_stale_first_method_cannot_prove_this",
        "a_stale_reborrow_receiver_cannot_prove_this",
        "a_stale_read_method_cannot_prove_this",
        "a_stale_leaf_sibling_cannot_prove_this",
    ] {
        let o = outcome(&report, name);
        assert!(
            matches!(o, FnOutcome::Failed { .. }),
            "a stale receiver value must not prove this false contract; got: {o}"
        );
    }
}

/// A helper that forwards its reference to another helper, and a helper that
/// *returns* the reference it was given.
///
/// The returned reference is the sharp case: `let r = identity(&mut x); *r = 5`
/// lowers to `_3 = &mut _1; _2 = identity(_3); (*_2) = 5`, so the write is keyed on
/// a local the *call* defined. Without the reference's origin crossing the call,
/// `_2` had no address, the write went to a local no caller reads, and the false
/// `ret == 0` was proved while the true `ret == 5` was refuted.
#[test]
fn a_reference_crosses_a_call_in_both_directions() {
    let Some(report) = verify_realistic(&[
        "a_two_frame_chain",
        "a_stale_two_frame_chain_cannot_prove_this",
        "a_two_frame_constant",
        "a_stale_two_frame_constant_cannot_prove_this",
        "a_write_through_a_returned_reference",
        "a_stale_returned_reference_cannot_prove_this",
    ]) else {
        return;
    };
    for name in [
        "a_two_frame_chain",
        "a_two_frame_constant",
        "a_write_through_a_returned_reference",
    ] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "a forwarded reference must reach its target; got: {o}");
    }
    for name in [
        "a_stale_two_frame_chain_cannot_prove_this",
        "a_stale_two_frame_constant_cannot_prove_this",
        "a_stale_returned_reference_cannot_prove_this",
    ] {
        let o = outcome(&report, name);
        assert!(
            matches!(o, FnOutcome::Failed { .. }),
            "a dropped write must not prove this false contract; got: {o}"
        );
    }
}

/// A method that *returns* a reference to one of its own fields.
///
/// `*c.lo_mut() = 5` lowers to `_2 = Config::lo_mut(move _3); (*_2) = 5; _0 = c.lo`,
/// and inside the callee the returned reference is `&mut (*_1).0` -- a place in the
/// *callee's* terms. Translating its head through the argument's caller place gives
/// `c.lo`. Without that the returned reference named nothing, the write was dropped,
/// the true `ret == 5` was refuted, and the false `ret == 0` was **proved**.
#[test]
fn a_method_may_return_a_reference_to_its_own_field() {
    let Some(report) = verify_realistic(&[
        "a_write_through_a_returned_field_reference",
        "a_stale_returned_field_reference_cannot_prove_this",
        "a_write_through_the_other_returned_reference",
        "a_stale_other_returned_reference_cannot_prove_this",
        "a_write_through_a_returned_builder",
        "a_stale_returned_builder_cannot_prove_this",
    ]) else {
        return;
    };
    for name in [
        "a_write_through_a_returned_field_reference",
        "a_write_through_the_other_returned_reference",
        "a_write_through_a_returned_builder",
    ] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "a returned field reference must reach the field; got: {o}");
    }
    for name in [
        "a_stale_returned_field_reference_cannot_prove_this",
        "a_stale_other_returned_reference_cannot_prove_this",
        "a_stale_returned_builder_cannot_prove_this",
    ] {
        let o = outcome(&report, name);
        assert!(
            matches!(o, FnOutcome::Failed { .. }),
            "a dropped write through a returned reference must not prove this; got: {o}"
        );
    }
}

/// A returned reference may name a *nested* place, or a place in a *different*
/// argument.
///
/// The nested case (`&mut o.i.v`) needs the whole projection carried, not just the
/// head local, and the two-argument case needs the translation to pick the argument
/// the reference was actually built from rather than the first one.
#[test]
fn a_returned_reference_carries_its_whole_projection() {
    let Some(report) = verify_realistic(&[
        "a_write_through_a_returned_nested_reference",
        "a_stale_returned_nested_reference_cannot_prove_this",
        "a_returned_nested_reference_leaves_its_sibling",
        "a_write_through_a_method_nested_reference",
        "a_stale_method_nested_reference_cannot_prove_this",
        "a_write_through_a_second_argument_reference",
        "a_stale_second_argument_reference_cannot_prove_this",
    ]) else {
        return;
    };
    for name in [
        "a_write_through_a_returned_nested_reference",
        "a_returned_nested_reference_leaves_its_sibling",
        "a_write_through_a_method_nested_reference",
        "a_write_through_a_second_argument_reference",
    ] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "a returned reference must reach its place; got: {o}");
    }
    for name in [
        "a_stale_returned_nested_reference_cannot_prove_this",
        "a_stale_method_nested_reference_cannot_prove_this",
        "a_stale_second_argument_reference_cannot_prove_this",
    ] {
        let o = outcome(&report, name);
        assert!(
            matches!(o, FnOutcome::Failed { .. }),
            "a dropped write through a returned reference must not prove this; got: {o}"
        );
    }
}

/// A returned reference fed straight into another call, reborrowed, held alongside a
/// second returned reference to a different field, or naming a tuple component.
///
/// The reborrow (`let s = &mut *r` where `r` came from a call) and the pass-through
/// (`write_one(identity(&mut c.lo))`) both go through the same `addressed` mapping
/// the returned reference installs. Two returned references to different fields must
/// not collapse onto one, and a tuple component is a returned place the write-back key
/// *can* spell (`Field(0)`), unlike an array element.
#[test]
fn a_returned_reference_composes_with_the_next_use() {
    let Some(report) = verify_realistic(&[
        "a_reference_returned_into_another_call",
        "a_stale_returned_into_another_call_cannot_prove_this",
        "a_reborrow_of_a_returned_reference",
        "a_stale_reborrow_of_a_returned_reference_cannot_prove_this",
        "two_returned_field_references",
        "a_stale_two_returned_fields_cannot_prove_this",
        "a_returned_tuple_component_reference",
        "a_stale_tuple_component_reference_cannot_prove_this",
    ]) else {
        return;
    };
    for name in [
        "a_reference_returned_into_another_call",
        "a_reborrow_of_a_returned_reference",
        "two_returned_field_references",
        "a_returned_tuple_component_reference",
    ] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "a returned reference must compose with its next use; got: {o}");
    }
    for name in [
        "a_stale_returned_into_another_call_cannot_prove_this",
        "a_stale_reborrow_of_a_returned_reference_cannot_prove_this",
        "a_stale_two_returned_fields_cannot_prove_this",
        "a_stale_tuple_component_reference_cannot_prove_this",
    ] {
        let o = outcome(&report, name);
        assert!(
            matches!(o, FnOutcome::Failed { .. }),
            "a dropped or misplaced write must not prove this false contract; got: {o}"
        );
    }
}

/// Two `&mut` targets in one call: two *aggregate* fields, or two *elements* of one
/// array.
///
/// The two writes must stay independent -- the false twins read the *other* target,
/// and would verify if the two targets collapsed onto one place. The swap shapes read
/// both referents, so they also exercise the carry-in.
#[test]
fn two_reference_targets_in_one_call_stay_independent() {
    let Some(report) = verify_realistic(&[
        "two_aggregate_reference_targets",
        "two_aggregate_reference_targets_other",
        "a_stale_two_aggregate_targets_cannot_prove_this",
        "a_swap_of_two_aggregate_targets",
        "a_stale_swap_of_aggregate_targets_cannot_prove_this",
        "two_elements_through_one_reference",
        "two_elements_through_one_reference_other",
        "a_stale_two_elements_cannot_prove_this",
        "a_swap_of_two_elements",
        "a_stale_swap_of_elements_cannot_prove_this",
    ]) else {
        return;
    };
    for name in [
        "two_aggregate_reference_targets",
        "two_aggregate_reference_targets_other",
        "a_swap_of_two_aggregate_targets",
        "two_elements_through_one_reference",
        "two_elements_through_one_reference_other",
        "a_swap_of_two_elements",
    ] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "two reference targets must stay independent; got: {o}");
    }
    for name in [
        "a_stale_two_aggregate_targets_cannot_prove_this",
        "a_stale_swap_of_aggregate_targets_cannot_prove_this",
        "a_stale_two_elements_cannot_prove_this",
        "a_stale_swap_of_elements_cannot_prove_this",
    ] {
        let o = outcome(&report, name);
        assert!(
            matches!(o, FnOutcome::Failed { .. }),
            "collapsed or stale targets must not prove this false contract; got: {o}"
        );
    }
}

/// A *scalar* field reached through a `&T`/`&mut T` argument, both read and written.
///
/// The referent's parts are carried into the callee, so the callee's `d.v` resolves to
/// the caller's `d.v`; the write-back then re-bases on the target's projection, which
/// is empty here (the target is the whole local). An enum *payload* through a reference
/// stays sound-incomplete -- the discriminant switch is unmodelled -- which is the
/// honest refusal rather than a dropped write.
#[test]
fn a_scalar_field_crosses_a_reference_argument() {
    let Some(report) = verify_realistic(&[
        "a_scalar_field_read_through_a_reference",
        "a_stale_scalar_field_read_cannot_prove_this",
        "a_scalar_field_write_through_a_reference",
        "a_stale_scalar_field_write_cannot_prove_this",
    ]) else {
        return;
    };
    for name in [
        "a_scalar_field_read_through_a_reference",
        "a_scalar_field_write_through_a_reference",
    ] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "a scalar field must cross a reference argument; got: {o}");
    }
    for name in [
        "a_stale_scalar_field_read_cannot_prove_this",
        "a_stale_scalar_field_write_cannot_prove_this",
    ] {
        let o = outcome(&report, name);
        assert!(
            matches!(o, FnOutcome::Failed { .. }),
            "a stale scalar field must not prove this false contract; got: {o}"
        );
    }
}

/// A scalar `&mut` argument named in a contract (C9).
///
/// `Ty::Ref` is modelled as its referent, so a `&mut u16` argument's name denotes
/// the `u16` it addresses. The name is the *final* value -- what the body left
/// there -- and `old(c)` reaches the entry value. Binding a bare `c` to the entry
/// value (the first cut of this feature) was a **false proof**: `ensures: c == 0`
/// on `*c = 5` verified, because `c` still named the input.
#[test]
fn a_scalar_reference_argument_is_named_in_a_contract() {
    let Some(report) = verify_realistic(&[
        "bump_scalar_ref",
        "a_stale_scalar_ref_cannot_prove_this",
        "set_scalar_ref",
        "a_scalar_ref_write_cannot_prove_this",
        "signed_scalar_ref",
        "a_scalar_ref_caller",
        "bump_scalar_ref_star",
    ]) else {
        return;
    };
    for name in [
        "bump_scalar_ref",
        "set_scalar_ref",
        "signed_scalar_ref",
        "a_scalar_ref_caller",
        "bump_scalar_ref_star",
    ] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "a scalar reference argument must be nameable; got: {o}");
    }
    // The false directions: each claims the *entry* value survived a write, which
    // only a model that bound the postcondition's name to the input would prove.
    for name in ["a_stale_scalar_ref_cannot_prove_this", "a_scalar_ref_write_cannot_prove_this"] {
        let o = outcome(&report, name);
        assert!(
            matches!(o, FnOutcome::Failed { .. }),
            "a scalar reference write must not prove the stale contract; got: {o}"
        );
    }
}

/// A scalar `&mut` argument's name and `old(name)`, checked directly so the guard
/// runs even without a nightly `rustc`.
#[test]
fn a_scalar_reference_argument_reads_as_its_referent() {
    use verif::smt;
    smt::with_fresh(|| {
        let mut args = std::collections::BTreeMap::new();
        args.insert("c".to_string(), smt::var(16));
        let mut old = std::collections::BTreeMap::new();
        old.insert("c".to_string(), smt::var(16));
        // `c` (final) and `old(c)` (entry) are distinct inputs, so their equality
        // is not a tautology.
        let p = spec::eval_ensures(
            "c == old(c) + 1",
            &args,
            None,
            spec::RET_IS_AGGREGATE,
            &std::collections::BTreeSet::new(),
            false,
            &spec::FieldEnv::default(),
            &old,
        )
        .expect("a scalar reference argument and `old` must parse");
        assert!(!smt::is_ill_formed(), "the contract must be well-formed");
        // The claim is a real predicate over two distinct variables, not a
        // tautology: it is refutable at `c == old(c)`.
        assert!(
            matches!(smt::prove(p), smt::Verdict::Counterexample(_)),
            "`c == old(c) + 1` must be refutable, not a tautology"
        );
        // `*c` and `old(*c)` are the explicit spellings of the same two bindings,
        // so the roadmap's exit shape parses and means the same predicate.
        let star = spec::eval_ensures(
            "*c == old(*c) + 1",
            &args,
            None,
            spec::RET_IS_AGGREGATE,
            &std::collections::BTreeSet::new(),
            false,
            &spec::FieldEnv::default(),
            &old,
        )
        .expect("`*c` and `old(*c)` must parse");
        assert!(
            matches!(smt::prove(smt::iff(star, p)), smt::Verdict::Valid),
            "`*c == old(*c) + 1` must mean the same as `c == old(c) + 1`"
        );
        // `*NAME` on an unbound name is refused, not read as a fresh variable.
        assert!(
            spec::eval_ensures(
                "*d == 0",
                &args,
                None,
                spec::RET_IS_AGGREGATE,
                &std::collections::BTreeSet::new(),
                false,
                &spec::FieldEnv::default(),
                &old,
            )
            .is_err(),
            "`*d` on an unbound name must be refused"
        );
        // `old` on a name with no entry value is refused, not silently treated as
        // the name itself.
        let err = spec::eval_ensures(
            "c == old(d)",
            &args,
            None,
            spec::RET_IS_AGGREGATE,
            &std::collections::BTreeSet::new(),
            false,
            &spec::FieldEnv::default(),
            &old,
        )
        .expect_err("`old(d)` has no entry value and must be refused");
        assert!(err.to_string().contains("old"), "the refusal must name `old`, got: {err}");
    });
}

/// A reference reached *through* another reference, and a reference to a local copy.
///
/// `**rr = 5` lowers to `_4 = no_retag copy (*_3); (*_4) = 5` with `_4: &mut u8`. The
/// `no_retag` prefix left the rvalue unparsed and the deref-of-a-reference copy had no
/// address, so the write was dropped: the *false* `ret == 0` proved and the true
/// `ret == 5` refuted. The read shape `**rr` had the same hole, and a write through a
/// reference to a local *copy* must not reach the original.
#[test]
fn a_reference_through_a_reference_is_not_dropped() {
    let Some(report) = verify_realistic(&[
        "a_write_through_two_levels_of_reference",
        "a_stale_two_level_write_cannot_prove_this",
        "a_read_through_two_levels_of_reference",
        "a_stale_two_level_read_cannot_prove_this",
        "a_write_to_a_copy_stays_in_the_copy",
        "a_stale_copy_write_cannot_prove_this",
    ]) else {
        return;
    };
    for name in [
        "a_write_through_two_levels_of_reference",
        "a_read_through_two_levels_of_reference",
        "a_write_to_a_copy_stays_in_the_copy",
    ] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "a two-level reference must reach its place; got: {o}");
    }
    for name in [
        "a_stale_two_level_write_cannot_prove_this",
        "a_stale_two_level_read_cannot_prove_this",
        "a_stale_copy_write_cannot_prove_this",
    ] {
        let o = outcome(&report, name);
        assert!(
            matches!(o, FnOutcome::Failed { .. }),
            "a dropped two-level write, or an aliasing copy, must not prove this; got: {o}"
        );
    }
}

/// A copy between two reference targets, a field through a *double* reference, and a
/// call through a reference to a reference.
///
/// `*r = *s` on two `&mut` to different fields must copy the right way, and
/// `(**rr).lo = 5` resolves both levels inside the frame. The *argument* case
/// (`relay(&mut r)` with `v: &mut &mut u8`) crosses two frames and is refused rather
/// than modelled -- the callee's write had been dropped, proving a false `ret == 0`.
#[test]
fn a_reference_to_a_reference_crosses_a_frame_only_when_refused() {
    let Some(report) = verify_realistic(&[
        "a_copy_between_two_reference_targets",
        "a_stale_copy_between_reference_targets_cannot_prove_this",
        "a_write_through_a_double_reference_to_a_struct",
        "a_stale_double_reference_struct_cannot_prove_this",
        "a_call_through_a_reference_to_a_reference_is_refused",
    ]) else {
        return;
    };
    for name in [
        "a_copy_between_two_reference_targets",
        "a_write_through_a_double_reference_to_a_struct",
    ] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "a local reference chain must be modelled; got: {o}");
    }
    for name in [
        "a_stale_copy_between_reference_targets_cannot_prove_this",
        "a_stale_double_reference_struct_cannot_prove_this",
    ] {
        let o = outcome(&report, name);
        assert!(
            matches!(o, FnOutcome::Failed { .. }),
            "a stale or misdirected reference write must not prove this; got: {o}"
        );
    }
    let refused = outcome(&report, "a_call_through_a_reference_to_a_reference_is_refused");
    assert!(
        matches!(refused, FnOutcome::Incomplete { .. }),
        "a `&mut &mut T` call must be refused, not decided; got: {refused}"
    );
}

/// An array that is a struct *field* carries its elements when the aggregate is moved.
///
/// `ArrayHolder { a: [9, 1, 2, 3] }` builds the array in a temp whose elements live in the
/// whole-local `elems` store, then moves it into the field. `rebase_parts` re-based
/// `elems_path` (field-path arrays) but missed `elems`, so `x.a[0]` read `Unknown` and the
/// contract stayed `Incomplete` in *both* directions -- a coverage hole, not a false proof,
/// since a missing value proves nothing. A bare array literal always worked, so the hole
/// was specific to the field case. The whole-field write through a reference (`*h = n`)
/// exercises the same carry across a frame, and the sibling check keeps the re-base from
/// spilling the elements onto a neighbouring scalar.
#[test]
fn an_array_field_of_a_moved_aggregate_carries_its_elements() {
    let Some(report) = verify_realistic(&[
        "a_struct_literal_array_element_is_read_back",
        "a_struct_literal_array_element_is_not_the_seed_cannot_prove_this",
        "a_struct_literal_array_second_element_is_read_back",
        "a_struct_literal_array_second_element_is_not_the_first_cannot_prove_this",
        "a_nested_struct_literal_array_element_is_read_back",
        "a_nested_struct_literal_array_element_is_not_the_seed_cannot_prove_this",
        "a_whole_field_assignment_carries_its_array_elements",
        "a_stale_whole_field_array_element_cannot_prove_this",
        "a_whole_field_assignment_leaves_its_sibling",
        "a_whole_field_assignment_leaves_its_sibling_cannot_prove_this",
        "an_array_field_argument_is_read_by_element",
        "an_array_field_argument_is_read_by_element_cannot_prove_this",
    ]) else {
        return;
    };
    for name in [
        "a_struct_literal_array_element_is_read_back",
        "a_struct_literal_array_second_element_is_read_back",
        "a_nested_struct_literal_array_element_is_read_back",
        "a_whole_field_assignment_carries_its_array_elements",
        "a_whole_field_assignment_leaves_its_sibling",
        "an_array_field_argument_is_read_by_element",
    ] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "an array field's elements must reach the read; got: {o}");
    }
    for name in [
        "a_struct_literal_array_element_is_not_the_seed_cannot_prove_this",
        "a_struct_literal_array_second_element_is_not_the_first_cannot_prove_this",
        "a_nested_struct_literal_array_element_is_not_the_seed_cannot_prove_this",
        "a_stale_whole_field_array_element_cannot_prove_this",
        "a_whole_field_assignment_leaves_its_sibling_cannot_prove_this",
        "an_array_field_argument_is_read_by_element_cannot_prove_this",
    ] {
        let o = outcome(&report, name);
        assert!(
            matches!(o, FnOutcome::Failed { .. }),
            "a stale or misdirected array-field element must not prove this; got: {o}"
        );
    }
}

/// An array or tuple *literal* whose elements are aggregates carries the elements' leaves.
///
/// `[o.i, o.j]` stores each element's own value but not the leaves under it, so
/// `a[0].v` read `Unknown` and the contract stayed `Incomplete` in *both* directions
/// -- a coverage hole, not a false proof, since a missing value proves nothing. The
/// read is `_2[_5].0`: an index *then* a field, the opposite order from `o.a[0]`,
/// which is why it needed its own key rather than `rebase_parts`. The pairs pin both
/// directions: a read that leaked the *other* element's leaf, or kept the pre-write
/// leaf, would decide the false entry and the pair would look green while proving
/// something untrue.
#[test]
fn an_aggregate_literal_element_carries_its_own_leaves() {
    let Some(report) = verify_realistic(&[
        "an_array_literal_of_aggregates_is_read_back",
        "an_array_literal_of_aggregates_is_not_the_other_element_cannot_prove_this",
        "an_array_literal_of_aggregates_second_element_is_read_back",
        "an_array_literal_of_aggregates_second_is_not_the_first_cannot_prove_this",
        "an_array_literal_of_aggregate_fields_is_read_back",
        "an_array_literal_of_aggregate_fields_is_not_the_other_cannot_prove_this",
        "a_tuple_literal_of_aggregates_is_read_back",
        "a_tuple_literal_of_aggregates_is_not_the_other_cannot_prove_this",
        "an_element_write_of_an_aggregate_is_read_back",
        "an_element_write_of_an_aggregate_is_not_the_old_leaf_cannot_prove_this",
    ]) else {
        return;
    };
    for name in [
        "an_array_literal_of_aggregates_is_read_back",
        "an_array_literal_of_aggregates_second_element_is_read_back",
        "an_array_literal_of_aggregate_fields_is_read_back",
        "a_tuple_literal_of_aggregates_is_read_back",
        "an_element_write_of_an_aggregate_is_read_back",
    ] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "an aggregate element's leaves must reach the read; got: {o}");
    }
    for name in [
        "an_array_literal_of_aggregates_is_not_the_other_element_cannot_prove_this",
        "an_array_literal_of_aggregates_second_is_not_the_first_cannot_prove_this",
        "an_array_literal_of_aggregate_fields_is_not_the_other_cannot_prove_this",
        "a_tuple_literal_of_aggregates_is_not_the_other_cannot_prove_this",
        "an_element_write_of_an_aggregate_is_not_the_old_leaf_cannot_prove_this",
    ] {
        let o = outcome(&report, name);
        assert!(
            matches!(o, FnOutcome::Failed { .. }),
            "a leaked or stale element leaf must not prove this; got: {o}"
        );
    }
}

/// A write to a leaf *inside* an element of an array or tuple of aggregates is
/// read back, and a stale leaf cannot prove the false twin.
///
/// `elem_parts` is keyed index-first -- the key the read `a[0].v` consults -- so a
/// write that reached only `elems` (the element's own value) or no store at all left
/// the literal's old leaf in place. A stale leaf is a *wrong* value rather than a
/// missing one, so each `..._cannot_prove_this` twin below is a false contract the
/// old model **proved**; without them a fix that dropped the write would look green.
/// The tuple twin exercises the mirror key, which is the only path a `t.0.v` read
/// takes, and the second-element twin catches a fix that pinned index 0 for every
/// leaf write.
#[test]
fn a_leaf_write_inside_an_element_is_read_back() {
    let verified = [
        "an_element_leaf_write_is_read_back",
        "an_element_leaf_write_leaves_its_sibling",
        "a_second_element_leaf_write_is_read_back",
        "a_tuple_component_leaf_write_is_read_back",
    ];
    let refuted = [
        "an_element_leaf_write_is_not_the_old_leaf_cannot_prove_this",
        "an_element_leaf_write_is_not_the_sibling_cannot_prove_this",
        "a_second_element_leaf_write_is_not_the_old_leaf_cannot_prove_this",
        "a_tuple_component_leaf_write_is_not_the_old_leaf_cannot_prove_this",
    ];
    let mut names = verified.to_vec();
    names.extend_from_slice(&refuted);
    let Some(report) = verify_realistic(&names) else { return };
    for name in verified {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "a leaf write inside an element must reach the read; got: {o}");
    }
    for name in refuted {
        let o = outcome(&report, name);
        assert!(
            matches!(o, FnOutcome::Failed { .. }),
            "a stale element leaf must not prove this; got: {o}"
        );
    }
}

/// A `variant:` measure turns partial correctness into total correctness.
///
/// Without a measure a loop is only proved for *partial* correctness: a divergent
/// loop verifies, because nothing says the header is ever left. With one, the back
/// edge also proves `I && guard => V' < V`, and on a fixed bit width a strictly
/// decreasing sequence cannot be infinite -- so the loop must stop.
///
/// The negative twins are the point. `a_loop_that_cannot_terminate_fails` is the
/// soundness check: before the measure existed the divergent loop *verified*, and
/// the whole reason to add the feature is that this must not happen.
#[test]
fn a_variant_measure_proves_loop_termination() {
    // A real countdown loop: the measure decreases every iteration, so the
    // termination obligation is discharged. `u8`/`u16` are used because the
    // bit-blasted query for a 32-bit measure is larger than the engine's diagram
    // budget -- that is a refusal, not a failure, and it is the same budget that
    // bounds every other wide query.
    let ok = verify_src(
        "f",
        "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nspec! {\n    invariant: i <= n,\n    variant: n - i,\n    ensures: ret == n,\n}\npub fn f(n: u8) -> u8 { let mut i = 0u8; while i < n { i += 1; } i }\n",
    );
    let Some(o) = ok else { return };
    assert!(o.is_verified(), "a decreasing measure must prove termination; got: {o}");
}

/// A loop whose body never advances the measure is *not* proved, and the failure
/// names the termination obligation rather than a postcondition.
///
/// This is the case the feature exists for. `while i < 5 { i += 0 }` never leaves
/// the loop, so a tool that reports it `Verified` is certifying a function that
/// does not return. Before `variant:` the loop had no termination obligation at
/// all and came back `Verified`; now the measure `i` is proved not to decrease and
/// the function fails.
#[test]
fn a_loop_that_cannot_terminate_fails() {
    let div = verify_src(
        "f",
        "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nspec! {\n    invariant: i >= 0,\n    variant: i,\n    ensures: ret == 0,\n}\npub fn f(n: u32) -> u32 { let _ = n; let mut i = 0u32; while i < 5 { i += 0; } i }\n",
    );
    let Some(o) = div else { return };
    assert!(!o.is_verified(), "a divergent loop must not verify; got: {o}");
    match &o {
        FnOutcome::Failed { kind, .. } => {
            assert!(kind.contains("variant"), "the failure names termination; got: {kind}")
        }
        other => panic!("expected a termination failure, got: {other}"),
    }
}

/// A measure that moves the wrong way fails, so the obligation is a real proof
/// and not a restatement of the invariant.
#[test]
fn a_measure_that_increases_fails() {
    let wrong = verify_src(
        "f",
        "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nspec! {\n    invariant: i <= n,\n    variant: i,\n    ensures: ret == n,\n}\npub fn f(n: u8) -> u8 { let mut i = 0u8; while i < n { i += 1; } i }\n",
    );
    let Some(o) = wrong else { return };
    assert!(!o.is_verified(), "an increasing measure must fail; got: {o}");
}

/// Without a measure the loop is still checked, for partial correctness: the
/// invariant and the postcondition are proved exactly as before, and no
/// termination claim is made. Adding the feature must not make the measure
/// mandatory.
#[test]
fn a_loop_without_a_measure_is_still_verified_for_partial_correctness() {
    let partial = verify_src(
        "f",
        "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nspec! {\n    invariant: i <= n,\n    ensures: ret == n,\n}\npub fn f(n: u8) -> u8 { let mut i = 0u8; while i < n { i += 1; } i }\n",
    );
    let Some(o) = partial else { return };
    assert!(o.is_verified(), "partial correctness must still be proved; got: {o}");
}

/// A measure with no invariant has no loop to attach to, so it is refused rather
/// than ignored -- an ignored measure would report partial correctness as if it
/// were total.
#[test]
fn a_measure_without_an_invariant_is_refused() {
    let orphan = verify_src(
        "f",
        "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nspec! {\n    variant: n,\n    ensures: ret == 5,\n}\npub fn f(n: u32) -> u32 { let _ = n; 5 }\n",
    );
    let Some(o) = orphan else { return };
    assert!(!o.is_verified(), "an orphan measure must not verify; got: {o}");
    assert!(matches!(o, FnOutcome::Skipped { .. }), "expected a refusal; got: {o}");
}

/// A loop with no invariant is unwound up to `unwind:`, and the clause raises the
/// bound so a loop whose trip count exceeds the default still decides.
///
/// `count_twelve` runs twelve iterations, more than `DEFAULT_UNWIND` (9), so the
/// default refuses it. With `unwind: 20` the walk reaches the exit edge and the
/// postcondition is proved. This is the C4 exit criterion's bounded-unrolling
/// half: the bound is *declared* and governs the loop.
#[test]
fn an_unwind_clause_raises_the_unwinding_bound() {
    let default = verify_src(
        "count_twelve_default",
        "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nspec! {\n    requires: true,\n    ensures: ret == 12,\n}\npub fn count_twelve_default() -> u8 { let mut i = 0u8; while i < 12 { i += 1; } i }\n",
    );
    let Some(o) = default else { return };
    assert!(
        !o.is_verified(),
        "twelve iterations exceed the default bound, so it must not verify; got: {o}"
    );
    match &o {
        FnOutcome::Incomplete { reasons, .. } => assert!(
            reasons.iter().any(|r| r.contains("unwound past")),
            "the cutoff must name the unwinding bound; got: {reasons:?}"
        ),
        other => panic!("expected an unwinding cutoff, got: {other}"),
    }

    let raised = verify_src(
        "count_twelve_unwind",
        "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nspec! {\n    requires: true,\n    unwind: 20,\n    ensures: ret == 12,\n}\npub fn count_twelve_unwind() -> u8 { let mut i = 0u8; while i < 12 { i += 1; } i }\n",
    );
    let Some(o) = raised else { return };
    assert!(o.is_verified(), "an explicit larger bound must decide the loop; got: {o}");
}

/// The bound is an *unwinding assertion*: too small a bound must not decide the
/// function by accident. `unwind: 3` on a loop that runs five iterations cannot
/// reach the exit edge, so the bound is *refuted* -- a failure, not an
/// incompleteness.
///
/// This is the Kani-style discipline the review asked for (C4(b)): a bound can
/// never cut off a path *silently*, and a too-small bound fails the unwinding
/// check rather than being reported as "could not decide". A bound that were only
/// an incompleteness could be read as "no counterexample found", which is exactly
/// the weaker proof the clause exists to prevent.
#[test]
fn an_unwind_bound_too_small_for_the_loop_is_refuted() {
    let small = verify_src(
        "count_five_unwind3",
        "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nspec! {\n    requires: true,\n    unwind: 3,\n    ensures: ret == 5,\n}\npub fn count_five_unwind3() -> u8 { let mut i = 0u8; while i < 5 { i += 1; } i }\n",
    );
    let Some(o) = small else { return };
    assert!(!o.is_verified(), "a bound below the trip count must not verify; got: {o}");
    match &o {
        FnOutcome::Failed { kind, .. } => assert!(
            kind.contains("unwind: 3"),
            "the failure must name the declared bound; got: {kind}"
        ),
        other => panic!("expected an unwinding-assertion failure, got: {other}"),
    }
}

/// A postcondition that is false is still refuted under a raised bound, so the
/// clause widens *coverage* and not the proof.
#[test]
fn an_unwind_clause_does_not_turn_a_false_postcondition_true() {
    let bad = verify_src(
        "count_twelve_bad",
        "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nspec! {\n    requires: true,\n    unwind: 20,\n    ensures: ret == 11,\n}\npub fn count_twelve_bad() -> u8 { let mut i = 0u8; while i < 12 { i += 1; } i }\n",
    );
    let Some(o) = bad else { return };
    assert!(!o.is_verified(), "a false postcondition must not verify; got: {o}");
    match &o {
        FnOutcome::Failed { kind, .. } => {
            assert!(kind.contains("postcondition"), "the failure is the postcondition; got: {kind}")
        }
        other => panic!("expected a postcondition failure, got: {other}"),
    }
}

/// C4's exit criterion, exactly: `max_of` verified with `unwind: 5`, and refuted
/// with `unwind: 3` by the unwinding check.
///
/// `max_of` is a four-iteration loop over a `[u8; 4]` with a branch inside, so it
/// explores 199 paths -- above the default 64-path budget. `unwind: 5` both raises
/// the loop bound and grows the path budget to match (see [`exec::path_budget`]),
/// so the loop decides. `unwind: 3` cannot reach the exit edge, and because the
/// bound is *declared* that is the unwinding assertion failing, not an
/// incompleteness.
#[test]
fn max_of_verifies_with_a_sufficient_unwind_bound_and_fails_with_a_small_one() {
    const BODY: &str = "pub fn max_of(a: [u8; 4]) -> u8 { let mut m = 0; let mut i = 0; \
                        while i < 4 { if a[i] > m { m = a[i]; } i += 1; } m }\n";
    const ENSURES: &str = "/// ensures: ret >= a[0] && ret >= a[1] && ret >= a[2] && ret >= a[3]\n";

    let ok = verify_src("max_of", &format!("/// requires: true\n/// unwind: 5\n{ENSURES}{BODY}"));
    let Some(o) = ok else { return };
    assert!(o.is_verified(), "`max_of` under `unwind: 5` must verify; got: {o}");

    let small = verify_src(
        "max_of_small",
        &format!("/// requires: true\n/// unwind: 3\n{ENSURES}{BODY}")
            .replace("max_of", "max_of_small"),
    );
    let Some(o) = small else { return };
    assert!(!o.is_verified(), "`max_of` under `unwind: 3` must not verify; got: {o}");
    match &o {
        FnOutcome::Failed { kind, .. } => assert!(
            kind.contains("unwind: 3"),
            "the small-bound failure is the unwinding check; got: {kind}"
        ),
        other => panic!("expected an unwinding-assertion failure, got: {other}"),
    }
}

/// A clause-shaped doc comment that is not a clause is refused, not dropped.
///
/// `/// ensuers: ret == 99` used to be discarded: the function then had no
/// postcondition, was checked against `true`, and came back `Verified` with zero
/// obligations -- a green report for a claim nobody read. The same held for
/// `variant:`, where the reader wrote a termination measure and the tool ignored
/// it. Both are now `Skipped` with the line named, which is the discipline an
/// unreadable `requires` already follows.
///
/// The negative twins pin the other direction: ordinary prose that happens to
/// contain a colon (`/// direction: ...`) is more than two edits from every
/// keyword and must *not* be refused, or documentation would start failing
/// builds.
#[test]
fn a_clause_shaped_doc_comment_that_is_not_a_clause_is_refused() {
    let typo = verify_src(
        "f",
        "/// requires: x == 0\n/// ensuers: ret == 99\npub fn f(x: u32) -> u32 { 5 }\n",
    );
    let Some(o) = typo else { return };
    assert!(!o.is_verified(), "a typo must not verify the function without it; got: {o}");
    assert!(
        matches!(o, FnOutcome::Skipped { .. }),
        "a dropped clause is a refusal, not a silent pass; got: {o}"
    );
    assert!(o.to_string().contains("ensuers"), "the refusal names the line; got: {o}");
    assert!(o.to_string().contains("ensures"), "and suggests the keyword; got: {o}");

    // `variant:` is a *supported* clause, so it is no longer a near miss. A
    // measure with no invariant is still refused, because the invariant is what
    // locates the loop the measure is about.
    let orphan = verify_src("g", "/// variant: n\n/// ensures: ret == 5\npub fn g(n: u32) -> u32 { let _ = n; 5 }\n");
    let Some(o) = orphan else { return };
    assert!(!o.is_verified(), "an orphan measure must not pass silently; got: {o}");
    assert!(o.to_string().contains("variant"), "the refusal names it; got: {o}");

    // Prose is left alone. `direction:` is three edits from `decreases`, so it is
    // documentation rather than a typo, and the function verifies normally.
    let prose = verify_src(
        "h",
        "/// direction: a contract claiming the pre-write value would be proved.\n\
         /// requires: x == 0\n/// ensures: ret == 5\npub fn h(x: u32) -> u32 { 5 }\n",
    );
    let Some(o) = prose else { return };
    assert!(o.is_verified(), "prose with a colon is not a clause; got: {o}");
}

/// A pinned write after a symbolic one re-establishes that element, and the value
/// must be readable -- including through an element copy.
///
/// The poison is array-wide, but the leaf maps are the record of which indices have
/// been re-established: `poison_elems` clears them and a later pinned write
/// re-populates exactly its index. An array-wide veto on top of that hid a leaf the
/// body genuinely knows, so `a[i].v = 7; a[0].v = 9; let x = a[0]; x.v` was
/// `incomplete` although the copy is always `9`. The recovery also has to cross the
/// call boundary, along with the poison it follows.
///
/// The negative twins pin the other half: a rewrite of one element recovers neither
/// its sibling nor its own pre-write leaf, so the fix is per-element and not a
/// blanket un-poisoning.
#[test]
fn a_pinned_rewrite_after_a_symbolic_write_is_read_back() {
    let recovered = [
        "a_pinned_leaf_rewrite_after_a_symbolic_write_is_read_back",
        "a_pinned_element_rewrite_after_a_symbolic_write_is_read_back",
        "a_pinned_leaf_rewrite_after_a_symbolic_element_write_is_read_back",
        "a_helper_pinned_rewrite_after_a_symbolic_write_is_read_back",
    ];
    let Some(report) = verify_realistic(&recovered) else { return };
    for name in recovered {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "a pinned rewrite is a known leaf; got: {o}");
    }
    let unknown = [
        "a_pinned_rewrite_does_not_recover_its_sibling",
        "a_pinned_rewrite_does_not_revive_the_old_leaf",
    ];
    let Some(report) = verify_realistic(&unknown) else { return };
    for name in unknown {
        let o = outcome(&report, name);
        assert!(!o.is_verified(), "a sibling stays unknown after a pinned rewrite; got: {o}");
    }
}

/// An unpinned element write poisons the whole array, and a copy of an element
/// must not revive a pre-write leaf.
///
/// `a[i] = E { v: 7 }` with an unpinned `i` marks every element unknown. The poison
/// used to reach only one of the two leaf representations: `poison_elems` cleared
/// `elem_parts` and left its `nested` mirror, so `carry_elem_leaf_onto_local`
/// collected the mirror on the next element *copy* (`let x = a[0]`) and the copy
/// read the value from before the write -- a **false proof** of `ret == 3`. The
/// poison also had to cross the call boundary, which is where a helper's symbolic
/// write lost it: the callee held no leaves, the copy-back inserted none, and the
/// caller's own leaves survived.
///
/// These are asserted `!is_verified()` rather than `Failed`, because `incomplete` is
/// the honest answer for a write the run cannot pin: the array is genuinely unknown,
/// so neither the old leaf nor the new one may be proved. What must never happen is
/// `verified`, which is what the false contracts below were before.
#[test]
fn an_unpinned_write_poisons_the_whole_array() {
    let names = [
        "an_element_copy_after_an_unpinned_write_is_not_the_old_leaf",
        "an_element_copy_after_an_unpinned_write_is_not_the_sibling",
        "an_element_copy_after_an_unpinned_write_is_not_the_new_leaf",
        "an_element_copy_after_an_unpinned_leaf_write_is_not_the_old_leaf",
        "a_helper_unpinned_write_is_not_the_callers_old_leaf",
        "a_helper_unpinned_write_is_not_the_callers_sibling",
    ];
    let Some(report) = verify_realistic(&names) else { return };
    for name in names {
        let o = outcome(&report, name);
        assert!(
            !o.is_verified(),
            "an unpinned write leaves no element known, so this must not be proved; got: {o}"
        );
    }
}

/// A whole-*element* write of an aggregate crosses a call boundary too.
///
/// The leaf-write family above covers `a[0].v = 7`. This is the other half: the
/// whole element is replaced, which lowers to `(*_1)[_3] = move _2` through a
/// `&mut` argument. The element's own *value* crossed the boundary already, but its
/// leaves did not, so the caller read the leaf its literal had before the call and a
/// false `ret == 3` was **proved**. The gate that re-roots the leaves read
/// `dest.proj` directly and so missed the leading `Deref` this shape carries; it
/// goes through `elem_index_of` now, which reads past it.
///
/// The element-copy case (`a[0] = b[0]`) needed one more piece: reading an element
/// hands over its leaf *scalar*, not its parts, so the leaves died at the temp the
/// copy goes through. `carry_elem_leaf_onto_local` moves them onto that temp. That
/// pair was `incomplete` in both directions before, not a false proof.
#[test]
fn a_whole_element_write_of_an_aggregate_crosses_a_call_boundary() {
    let verified = [
        "an_element_copy_from_another_element_is_read_back",
        "a_helper_whole_element_write_is_read_back",
        "a_helper_whole_element_write_leaves_its_sibling",
        "a_helper_element_copy_is_read_back",
    ];
    let refuted = [
        "an_element_copy_is_not_the_old_leaf_cannot_prove_this",
        "a_helper_whole_element_write_is_not_the_callers_leaf_cannot_prove_this",
        "a_helper_whole_element_write_is_not_the_sibling_cannot_prove_this",
        "a_helper_element_copy_is_not_the_old_leaf_cannot_prove_this",
    ];
    let mut names = verified.to_vec();
    names.extend_from_slice(&refuted);
    let Some(report) = verify_realistic(&names) else { return };
    for name in verified {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "a whole-element write must reach the read; got: {o}");
    }
    for name in refuted {
        let o = outcome(&report, name);
        assert!(
            matches!(o, FnOutcome::Failed { .. }),
            "a stale element leaf must not prove this; got: {o}"
        );
    }
}

/// The leaf store crosses both call boundaries: an argument and a return.
///
/// A helper that writes a leaf through `&mut [T; N]` had its write die with the
/// frame, so the caller read the leaf its literal had before the call and a false
/// `ret == 3` was **proved**. A helper that only *reads* a leaf through its argument
/// saw `Unknown`, and a helper that *returns* an array of aggregates lost every leaf
/// so the caller could not read one at all. Each pair pins one direction: the
/// `..._cannot_prove_this` twins are the false contracts a dropped carry would decide.
#[test]
fn an_element_leaf_store_crosses_a_call_boundary() {
    let verified = [
        "a_helper_leaf_write_through_a_mutable_array_is_read_back",
        "a_helper_leaf_read_through_an_array_argument_decides",
        "a_returned_array_of_aggregates_keeps_its_leaves",
    ];
    let refuted = [
        "a_helper_leaf_write_is_not_the_callers_leaf_cannot_prove_this",
        "a_helper_leaf_read_is_not_a_made_up_value_cannot_prove_this",
        "a_returned_array_of_aggregates_is_not_a_made_up_value_cannot_prove_this",
    ];
    let mut names = verified.to_vec();
    names.extend_from_slice(&refuted);
    let Some(report) = verify_realistic(&names) else { return };
    for name in verified {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "a leaf must cross the call boundary; got: {o}");
    }
    for name in refuted {
        let o = outcome(&report, name);
        assert!(
            matches!(o, FnOutcome::Failed { .. }),
            "a dropped or stale leaf carry must not prove this; got: {o}"
        );
    }
}

/// A whole-aggregate replacement through a sub-place target decides.
///
/// `set_whole(&mut o.i, InnerBox { v: 9 })` replaces the whole referent. This was once
/// believed *sound-incomplete* -- a leaf write "cannot spell a whole value" -- but the
/// re-based write-back re-roots the callee's `fields` entry for the moved value under the
/// target's chain, and in this store a whole value and a leaf are both a `fields` entry, so
/// the shape was never actually the blocker. The sibling check keeps the re-root from
/// landing on the whole local, and the local-built value makes the carry come from a temp.
#[test]
fn a_whole_aggregate_replacement_through_a_sub_place_decides() {
    let Some(report) = verify_realistic(&[
        "whole_replacement_through_sub_place",
        "whole_replacement_through_sub_place_cannot_prove_this",
        "whole_replacement_leaves_the_sibling",
        "whole_replacement_leaves_the_sibling_cannot_prove_this",
        "whole_replacement_from_a_local",
        "whole_replacement_from_a_local_cannot_prove_this",
    ]) else {
        return;
    };
    for name in [
        "whole_replacement_through_sub_place",
        "whole_replacement_leaves_the_sibling",
        "whole_replacement_from_a_local",
    ] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "a whole replacement must reach the caller's leaf; got: {o}");
    }
    for name in [
        "whole_replacement_through_sub_place_cannot_prove_this",
        "whole_replacement_leaves_the_sibling_cannot_prove_this",
        "whole_replacement_from_a_local_cannot_prove_this",
    ] {
        let o = outcome(&report, name);
        assert!(
            matches!(o, FnOutcome::Failed { .. }),
            "a stale or misdirected whole replacement must not prove this; got: {o}"
        );
    }
}

/// A named `const` item's value is inlined at the use site.
///
/// rustc const-evaluates a `const` item and prints its *body* separately
/// (`const LIMIT: u8 = const 10_u8;`) while referring to it by name at the use
/// site (`_0 = const LIMIT`). Reading only the name left the use site an
/// unreadable path constant, so a contract that mentioned the constant was lost.
/// The true directions must decide, and each false twin -- whose postcondition is
/// false on *every* input -- must be refuted, which is what a regression that
/// guessed the value would fail.
#[test]
fn a_named_const_item_is_inlined_at_its_use_site() {
    let Some(report) = verify_realistic(&[
        "named_const_is_read",
        "named_const_is_read_cannot_prove_this",
        "named_const_keeps_its_width",
        "named_const_keeps_its_width_cannot_prove_this",
        "named_bool_const_selects",
        "named_bool_const_selects_cannot_prove_this",
        "named_const_bounds_a_comparison",
        "named_const_bounds_a_comparison_cannot_prove_this",
        "named_const_matches_an_equality",
        "named_const_matches_an_equality_cannot_prove_this",
        "named_negative_const_is_signed",
        "named_negative_const_is_signed_cannot_prove_this",
        "nested_module_const_is_read",
        "nested_module_const_is_read_cannot_prove_this",
    ]) else {
        return;
    };
    for name in [
        "named_const_is_read",
        "named_const_keeps_its_width",
        "named_bool_const_selects",
        "named_const_bounds_a_comparison",
        "named_const_matches_an_equality",
        "named_negative_const_is_signed",
        "nested_module_const_is_read",
    ] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "a named const must fold at its use site; got: {o}");
    }
    for name in [
        "named_const_is_read_cannot_prove_this",
        "named_const_keeps_its_width_cannot_prove_this",
        "named_bool_const_selects_cannot_prove_this",
        "named_const_bounds_a_comparison_cannot_prove_this",
        "named_const_matches_an_equality_cannot_prove_this",
        "named_negative_const_is_signed_cannot_prove_this",
        "nested_module_const_is_read_cannot_prove_this",
    ] {
        let o = outcome(&report, name);
        assert!(
            matches!(o, FnOutcome::Failed { .. }),
            "a wrong inlined const value must not prove this; got: {o}"
        );
    }
}

/// A contract may name a top-level `const` the way the body does.
///
/// The reader resolves a bare name against the arguments first, then against the
/// program's own `const` table -- the *same* table the executor folds a body's
/// `const LIMIT` from, so the two cannot disagree. The true directions must decide,
/// and each false twin must be refuted; a name that resolved to nothing (or to a
/// wrong value) would leave the twin unproved or, worse, prove it.
///
/// The case that is a *refusal* rather than a proof is checked separately: a
/// misspelled name is not an argument and must be `Skipped`, never `verified`.
/// (An expression-bodied constant used to be the other refusal; it is folded now,
/// and its resolution is asserted in the body of this test.)
#[test]
fn a_contract_may_name_a_const_item() {
    let Some(report) = verify_realistic(&[
        "a_contract_names_a_const",
        "a_contract_names_a_const_cannot_prove_this",
        "a_contract_names_a_negative_const",
        "a_contract_names_a_bool_const",
        "a_contract_names_a_qualified_const",
        "a_contract_names_a_qualified_const_cannot_prove_this",
        "a_contract_names_an_expression_const",
        "a_misspelled_const_is_refused",
    ]) else {
        return;
    };
    for name in [
        "a_contract_names_a_const",
        "a_contract_names_a_negative_const",
        "a_contract_names_a_bool_const",
        "a_contract_names_a_qualified_const",
    ] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "a contract must resolve a named const; got: {o}");
    }
    for name in [
        "a_contract_names_a_const_cannot_prove_this",
        "a_contract_names_a_qualified_const_cannot_prove_this",
    ] {
        let o = outcome(&report, name);
        assert!(
            matches!(o, FnOutcome::Failed { .. }),
            "a wrong const value must not prove this; got: {o}"
        );
    }
    // An *expression* const now resolves: the item is folded once, into the
    // program's const table, so a contract naming it reads the same value the
    // body's use site does.
    let expr = outcome(&report, "a_contract_names_an_expression_const");
    assert!(
        expr.is_verified(),
        "a contract must resolve an expression const now that it is folded; got: {expr}"
    );
    // A *misspelled* name is still refused, not guessed: folding must not turn an
    // unknown name into a value.
    let misspelled = outcome(&report, "a_misspelled_const_is_refused");
    assert!(
        matches!(misspelled, FnOutcome::Skipped { .. }),
        "an unresolvable const name must be refused, not guessed; got: {misspelled}"
    );
}

/// A `const` whose body is an *expression* is inlined, in both directions.
///
/// rustc prints such an item with its own locals and blocks (`const NEXT: u8 = {
/// let mut _0: u8; ... _1 = AddWithOverflow(const LIMIT, const 1_u8); ... }`)
/// rather than as a single literal, so the value lives in the body. The folder
/// interprets that body concretely -- every operand is a literal, so the checked
/// add folds and the body's own `assert(!overflow)` is checked -- and records the
/// result in the program's const table. Both the body's use site and a contract
/// that names the item then read the same value.
///
/// The false twin is the pin that matters: a *guessed* or mis-folded value would
/// prove `ret == 10`, which is wrong. It must be refuted.
#[test]
fn an_expression_const_is_inlined_in_both_directions() {
    let Some(report) = verify_realistic(&[
        "named_const_of_an_expression_is_inlined",
        "named_const_of_an_expression_is_inlined_cannot_prove_this",
        "an_expression_const_keeps_its_width",
    ]) else {
        return;
    };
    let true_direction = outcome(&report, "named_const_of_an_expression_is_inlined");
    assert!(
        true_direction.is_verified(),
        "an expression const must fold to its value; got: {true_direction}"
    );
    let false_direction =
        outcome(&report, "named_const_of_an_expression_is_inlined_cannot_prove_this");
    assert!(
        matches!(false_direction, FnOutcome::Failed { .. }),
        "`NEXT` is 11, so `ret == 10` must be refuted, not proved; got: {false_direction}"
    );
    // The folded value must keep the item's own width: a folder that dropped to
    // `u8` would truncate `BIG == 300` and the contract would not decide.
    let width = outcome(&report, "an_expression_const_keeps_its_width");
    assert!(width.is_verified(), "an expression const keeps its width; got: {width}");
}

/// A comparison folded in a *constant* is read at its operands' type, not at the
/// `bool` destination's.
///
/// rustc prints `const LESS: bool = -1i8 < 0i8` as `_0 = Lt(const -1_i8, const
/// 0_i8)`: the return place is `bool`, but the operands are `i8`. Taking the width
/// and signedness from the destination made the folder compare the `-1i8` pattern,
/// 255, as *unsigned* -- so `-1i8 < 0i8` folded to `false`, and a contract claiming
/// `ret == false` was *proved* from a wrong value. That is a false proof, not a lost
/// one, which is why the false twin here is the load-bearing assertion.
#[test]
fn a_signed_comparison_const_uses_the_operand_type() {
    let Some(report) = verify_realistic(&[
        "a_signed_comparison_const_is_read",
        "a_signed_comparison_const_is_read_cannot_prove_this",
        "an_unsigned_comparison_const_is_read",
    ]) else {
        return;
    };
    let signed = outcome(&report, "a_signed_comparison_const_is_read");
    assert!(
        signed.is_verified(),
        "`-1i8 < 0i8` is true and the contract must prove it; got: {signed}"
    );
    let false_direction = outcome(&report, "a_signed_comparison_const_is_read_cannot_prove_this");
    assert!(
        matches!(false_direction, FnOutcome::Failed { .. }),
        "`LESS` is true, so `ret == false` must be refuted -- reading the operands \
         as unsigned proves it from a wrong value; got: {false_direction}"
    );
    // The unsigned control folds the same way under either reading, so it pins
    // that the fix did not simply invert every comparison.
    let unsigned = outcome(&report, "an_unsigned_comparison_const_is_read");
    assert!(unsigned.is_verified(), "`200u8 > 100u8` is true; got: {unsigned}");
}

/// A constant may name one the dump prints *after* it.
///
/// rustc keeps source declaration order, so `FWD_NEXT`'s expression body is printed
/// before `FWD_LIMIT`'s literal. A single in-order pass folds `FWD_NEXT` while
/// `FWD_LIMIT` is still unresolved and leaves the item unmodelled -- its contracts
/// undecided. The fold runs to a fixpoint, so a later constant is seen. The false
/// twin pins that the value is real: a guess would prove `ret == 12`.
#[test]
fn a_forward_referenced_const_is_resolved() {
    let Some(report) = verify_realistic(&[
        "a_forward_referenced_const_is_read",
        "a_forward_referenced_const_is_read_cannot_prove_this",
    ]) else {
        return;
    };
    let resolved = outcome(&report, "a_forward_referenced_const_is_read");
    assert!(
        resolved.is_verified(),
        "a constant may name a later one; the fold must reach a fixpoint; got: {resolved}"
    );
    let false_direction = outcome(&report, "a_forward_referenced_const_is_read_cannot_prove_this");
    assert!(
        matches!(false_direction, FnOutcome::Failed { .. }),
        "`FWD_NEXT` is 11, so `ret == 12` must be refuted; got: {false_direction}"
    );
}

/// A file that declares an *unused* 128-bit expression constant still verifies its
/// smaller functions.
///
/// The parser reads every `const` definition when it loads a dump, including ones no
/// target names. rustc prints an expression-bodied `i128` as an `AddWithOverflow` at
/// width 128, and the folder's exact overflow test negates `1i128 << 127` --
/// `i128::MIN` -- which panics in a debug build, taking the whole run down for an
/// item nobody asked about. The width is refused, so the item is simply unmodelled
/// and the verdicts still arrive.
#[test]
fn an_unused_i128_expression_const_does_not_abort_the_run() {
    let Some(report) =
        verify_realistic(&["a_file_with_an_unused_i128_const_still_verifies"])
    else {
        return;
    };
    let o = outcome(&report, "a_file_with_an_unused_i128_const_still_verifies");
    assert!(
        o.is_verified(),
        "an unused i128 const must not abort the parse; got: {o}"
    );
}

/// A function-local `const` is not in scope in another function.
///
/// `a_local_const_owner` declares `const L: u8 = 3;` in its body; the dump prints
/// that definition bare (`const L: u8 = const 3_u8;`) -- the same spelling a
/// crate-level `const` gets -- while its *use* is qualified (`const
/// a_local_const_owner::L`). A bare program-wide key for the definition let the
/// other function's `requires: L == 3` resolve to a name its own source could not
/// write, so the function was *proved* from a foreign body: a wrong proof, not a
/// lost one. The name must resolve to nothing here, leaving the function `Skipped`
/// (unreadable `requires`), never `Verified`.
///
/// The crate-level constant path stays covered by
/// [`a_contract_may_name_a_const_item`], so this pins that the scope rule did not
/// also drop the in-scope names.
#[test]
fn a_local_const_is_not_in_scope_in_another_function() {
    let Some(report) = verify_realistic(&[
        "a_local_const_owner",
        "a_local_const_is_not_in_scope_in_another_function",
    ]) else {
        return;
    };
    let owner = outcome(&report, "a_local_const_owner");
    assert!(owner.is_verified(), "the owner reads its own local const; got: {owner}");
    let other = outcome(&report, "a_local_const_is_not_in_scope_in_another_function");
    assert!(
        !other.is_verified(),
        "a contract must not resolve another function's local const -- that is a \
         wrong proof; got: {other}"
    );
}

/// The whole-corpus direction invariant: a function whose *name* says it must not be
/// provable must come back `Failed` -- a real refutation with a counterexample.
///
/// The corpus pairs each contract with its false twin, spelled `..._cannot_prove_this`:
/// the same body, or one differing only in the direction of the claim, so a twin that
/// verifies is a false proof by construction. Checking the naming convention over the
/// whole file means a new false proof of any *known* shape fails this test the moment
/// it is introduced, instead of waiting for someone to remember to assert the twin in
/// the same commit that adds the shape.
///
/// The assertion is on `Failed`, not on `!is_verified()`. `!is_verified()` also accepts
/// `Skipped` and `Incomplete`, so a tripwire that "passed" that way would never have
/// been pulled -- a false contract that was never checked is not evidence the checker
/// works. Two entries are `Incomplete` on purpose, and they are named here with the
/// reason: a tripwire may be undecided only when it is *listed* as such, so a
/// regression that turns a refutation into a skip fails this test rather than slipping
/// through.
#[test]
fn no_cannot_prove_entry_is_ever_verified() {
    let Some(report) = verify_realistic(&[]) else { return };
    // Tripwires that are honestly undecided, with the reason they cannot be refuted
    // today. Each is a *lost* proof (a skip), never a false one, and each is a
    // documented coverage gap rather than a regression. Anything else that is not
    // `Failed` fails this test.
    let undecided_on_purpose: &[(&str, &str)] = &[
        (
            "a_shifted_symbolic_element_cannot_prove_this",
            "a symbolic shift of an array element is not modelled, so the contract is \
             unreadable -- the read never reaches the refutation",
        ),
        (
            "default_body_self_call_is_not_the_default_cannot_prove_this",
            "the self-call to a trait default body is unmodelled, so the path is cut \
             before the false claim is checked",
        ),
        (
            "generic_slice_element_cannot_prove_this",
            "a generic `&[T]` has no element width, so `v[0]` is an unmodelled element \
             read and `ret == d` is unreadable -- the length is modelled, the element \
             is not, so the claim is a skip rather than a refutation",
        ),
    ];
    let mut checked = 0usize;
    let mut liars = Vec::new();
    let mut wrongly_undecided = Vec::new();
    for (name, o) in &report.outcomes {
        let false_direction = name.ends_with("_cannot_prove_this");
        if !false_direction {
            continue;
        }
        checked += 1;
        if o.is_verified() {
            liars.push(name.clone());
        } else if !matches!(o, FnOutcome::Failed { .. })
            && !undecided_on_purpose.iter().any(|(n, _)| n == name)
        {
            wrongly_undecided.push(format!("{name}: {o}"));
        }
    }
    assert!(
        liars.is_empty(),
        "these entries claim a false contract and were `verified` -- a false proof: {liars:?}"
    );
    assert!(
        wrongly_undecided.is_empty(),
        "these tripwires were neither refuted nor on the documented undecided list -- a \
         tripwire that was not pulled is not evidence: {wrongly_undecided:#?}"
    );
    // A silent zero would make this test vacuous if the naming convention ever drifts,
    // so the count is asserted rather than merely reported.
    assert!(
        checked >= 40,
        "expected the corpus to carry dozens of false-direction probes, found {checked}; \
         has the naming convention changed?"
    );
}

/// The loop cut havocs what the loop writes *through*, not what it names.
///
/// A `for` loop advances its `RangeInclusive` through `&mut` and the range is a
/// widthless aggregate, so the loop header must havoc both the referent of the
/// reference and the aggregate's parts. When it havoced neither, the header state
/// kept the entry-time iterator value, the exit guard folded to `false`, the exit
/// edge was pruned as unsatisfiable, and the postcondition was never checked -- a
/// `for` loop whose postcondition was false came back `Verified`.
///
/// The two corpus pairs pin both directions: the true contract verifies and the
/// false twin is refuted. The false twin is also covered by the corpus-wide
/// `no_cannot_prove_entry_is_ever_verified` sweep; this test adds the *positive*
/// half, so a fix that refused every loop invariant would fail here.
#[test]
fn a_loop_havocs_what_it_writes_through() {
    let names = [
        "for_loop_invariant_holds",
        "for_loop_invariant_cannot_prove_this",
        "while_writes_through_ref_holds",
        "while_writes_through_ref_cannot_prove_this",
        "ref_arg_written_in_a_loop_holds",
        "ref_arg_written_in_a_loop_cannot_prove_this",
        "call_writes_through_in_a_loop_holds",
        "call_writes_through_in_a_loop_cannot_prove_this",
    ];
    let Some(report) = verify_realistic(&names) else { return };
    let outcome = |name: &str| {
        report
            .outcomes
            .iter()
            .find(|(n, _)| n == name)
            .unwrap_or_else(|| panic!("`{name}` must be in the report"))
            .1
            .clone()
    };
    for holds in [
        "for_loop_invariant_holds",
        "while_writes_through_ref_holds",
        "ref_arg_written_in_a_loop_holds",
        "call_writes_through_in_a_loop_holds",
    ] {
        assert!(
            outcome(holds).is_verified(),
            "`{holds}` establishes its postcondition and must verify, got: {}",
            outcome(holds)
        );
    }
    for twin in [
        "for_loop_invariant_cannot_prove_this",
        "while_writes_through_ref_cannot_prove_this",
        "ref_arg_written_in_a_loop_cannot_prove_this",
        "call_writes_through_in_a_loop_cannot_prove_this",
    ] {
        match outcome(twin) {
            FnOutcome::Failed { kind, .. } => assert!(
                kind.contains("postcondition") || kind.contains("invariant"),
                "`{twin}` must fail on its own claim, got `{kind}`"
            ),
            other => panic!(
                "`{twin}` claims a false postcondition and must be refuted; a header \
                 that is not havoced proves it, got: {other}"
            ),
        }
    }
}

/// A deep term is refused, not allowed to overflow the stack.
///
/// The engine's contract is that a query it cannot decide is *reported*: `Unknown`,
/// `Incomplete`, a budget trip -- never a crash. Every budget before this one counted
/// *steps*, and steps are not what a stack overflow is made of: `udiv(x, 3)` at 64 bits
/// builds a chain of adders and muxes ~262 terms deep, which is a few hundred steps, so it
/// passed every step budget and then aborted the process. On a 2 MiB stack -- the default
/// for a Rust test thread -- the walk wanted ~600 KiB in release and more in debug.
///
/// The guard is `MAX_WALK_DEPTH`, and this test is what holds it: it runs on the test
/// thread's own stack, so a regression that removes the guard aborts the test binary
/// (SIGABRT) instead of failing an assertion. The division-by-a-non-power-of-two shape is
/// the deepest legitimate walk in the corpus; the assertion that it reports rather than
/// panics is the whole point.
///
/// The deep walk needs a *symbolic* dividend: a precondition that pins the dividend folds
/// the whole divider to a literal (see [`a_signed_division_by_a_non_power_of_two_at_i64`]),
/// so the pinned shape no longer builds the deep term at all. The companion
/// [`a_symbolic_division_by_a_non_power_of_two_at_i64`] keeps the dividend free and is the
/// vehicle here.
#[test]
fn a_deep_term_walk_is_refused_not_allowed_to_overflow_the_stack() {
    let Some(report) = verify_realistic(&[
        "a_symbolic_division_by_a_non_power_of_two_at_i64",
        "a_signed_division_by_a_non_power_of_two_at_i64",
        "a_signed_constant_division_at_i64",
    ]) else {
        return;
    };
    // The symbolic case is the one that used to crash. Either answer but `Verified` is
    // acceptable -- the general divider is a documented scaling wall -- and the
    // unacceptable one is a false proof. Reaching this assertion at all is the fix.
    let deep = outcome(&report, "a_symbolic_division_by_a_non_power_of_two_at_i64");
    assert!(
        !deep.is_verified(),
        "the general divider must not be reported verified: {deep}"
    );
    // The *pinned* shape is now decided: the precondition leaves one dividend, the fold
    // collapses the divider to a literal, and the false contract is refuted. This is the
    // positive evidence that the fold, not a tighter budget, is what closed it -- a depth
    // bound that refused both shapes would pass the assertion above and be useless.
    let pinned = outcome(&report, "a_signed_division_by_a_non_power_of_two_at_i64");
    assert!(
        pinned.is_verified(),
        "the pinned divider must be decided by the fold, got: {pinned}"
    );
    // The power-of-two fast path is shallow and must still be *decided*, so the depth
    // bound cannot be so tight that it refuses the queries the engine already answers.
    let shallow = outcome(&report, "a_signed_constant_division_at_i64");
    assert!(
        shallow.is_verified(),
        "the power-of-two fast path must still verify: {shallow}"
    );
}

/// A pin applies to an aggregate *part*, not only to a whole argument.
///
/// `requires: w.v == -7` names a struct field, `requires: o.i.v == -7` names a nested
/// path, and `requires: xs[0] == -7` names an array element. All three are the same shape
/// as the argument-level pin -- a closed equality in the precondition, under any nesting of
/// `&&` -- but the variable the contract binds is a part's, not a local's. Keying the pin by
/// the argument's *local* reached none of them, so the body's `w.v / 3` stayed symbolic and
/// built the 64-bit restoring divider: `incomplete` in ~1.2s, decided in ~0s once the pin is
/// keyed by the variable's own index.
///
/// The `cannot_prove_this` companions are the point of the test. Without them, an engine
/// that answered `Verified` for everything would pass the positive assertions, so the
/// negatives are what make "decided" mean "decided correctly": the fold refutes a wrong
/// claim about the same pinned part, which is only possible if the value really is `-7`.
#[test]
fn a_pin_reaches_an_aggregate_part_not_only_a_whole_argument() {
    let Some(report) = verify_realistic(&[
        "a_field_pinned_division_at_i64",
        "a_field_pinned_division_cannot_prove_this",
        "a_nested_pinned_division_at_i64",
        "a_nested_pinned_division_cannot_prove_this",
        "an_element_pinned_division_at_i64",
        "an_element_pinned_division_cannot_prove_this",
    ]) else {
        return;
    };
    for name in [
        "a_field_pinned_division_at_i64",
        "a_nested_pinned_division_at_i64",
        "an_element_pinned_division_at_i64",
    ] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "a pinned aggregate part must be decided; `{name}` got: {o}");
    }
    for name in [
        "a_field_pinned_division_cannot_prove_this",
        "a_nested_pinned_division_cannot_prove_this",
        "an_element_pinned_division_cannot_prove_this",
    ] {
        let o = outcome(&report, name);
        assert!(
            !o.is_verified(),
            "a false claim about a pinned part must be refuted, not proved; `{name}` got: {o}"
        );
    }
}

/// A callee's contract is never taken on faith: its body is inlined, so a lying
/// `ensures` cannot be inherited by a caller, and a vacuous `requires` cannot be
/// used to assume a false postcondition.
///
/// This is the soundness property with the largest blast radius -- assuming a
/// callee's postcondition is the canonical way a modular verifier proves a false
/// contract -- so it is pinned directly rather than left implied by the absence of
/// a bug report. The recursion cases are the same property under the inductive
/// hypothesis, where an unguarded assumption is easiest to make.
#[test]
fn a_callee_contract_is_never_assumed() {
    let Some(report) = verify_realistic(&[
        "a_helper_whose_contract_is_false",
        "a_caller_of_a_lying_helper_cannot_prove_this",
        "a_caller_of_a_lying_helper_knows_the_truth",
        "a_call_that_violates_a_precondition_is_not_checked",
        "a_call_that_divides_by_zero_cannot_prove_this",
        "a_vacuous_helper_whose_postcondition_is_false",
        "a_caller_inheriting_a_vacuous_lie_cannot_prove_this",
        "a_caller_of_a_vacuous_helper_knows_the_truth",
        "a_recursive_contract_that_is_false_cannot_prove_this",
        "a_mutual_lie_a_cannot_prove_this",
        "a_mutual_lie_b_cannot_prove_this",
        "an_invariant_false_on_entry_cannot_prove_this",
        "an_invariant_not_preserved_cannot_prove_this",
    ]) else {
        return;
    };

    // The two entries that must verify are the *honest* directions: they are what
    // proves the shapes above are decidable at all, rather than undecided for a
    // reason that happens to look safe.
    for name in [
        "a_caller_of_a_lying_helper_knows_the_truth",
        "a_caller_of_a_vacuous_helper_knows_the_truth",
    ] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "the honest direction must verify; `{name}` got: {o}");
    }

    // A callee's `requires` is not an obligation at the call site -- the body is
    // inlined instead -- so this one verifies, and that is the honest answer rather
    // than a false proof: the helper returns its argument whatever it is. Pinning it
    // makes the *choice* visible instead of accidental.
    let unchecked = outcome(&report, "a_call_that_violates_a_precondition_is_not_checked");
    assert!(
        unchecked.is_verified(),
        "a callee precondition is not a call-site obligation; expected verified, got: {unchecked}"
    );

    for name in [
        "a_helper_whose_contract_is_false",
        "a_caller_of_a_lying_helper_cannot_prove_this",
        "a_call_that_divides_by_zero_cannot_prove_this",
        "a_vacuous_helper_whose_postcondition_is_false",
        "a_caller_inheriting_a_vacuous_lie_cannot_prove_this",
        "a_recursive_contract_that_is_false_cannot_prove_this",
        "a_mutual_lie_a_cannot_prove_this",
        "a_mutual_lie_b_cannot_prove_this",
        "an_invariant_false_on_entry_cannot_prove_this",
        "an_invariant_not_preserved_cannot_prove_this",
    ] {
        let o = outcome(&report, name);
        assert!(
            matches!(o, FnOutcome::Failed { .. }),
            "an assumed callee contract, an unchecked induction, or a vacuous \
             precondition must not prove this; `{name}` got: {o}"
        );
    }
}

/// A loop counter's arithmetic in an invariant is a *variable*'s arithmetic, not
/// a written constant expression.
///
/// The invariant is evaluated in the loop-head state, where the counter is bound
/// to the value it holds on entry (`0`). `i - 1` then has two constant operands,
/// and the fold read it as the *written* `0 - 1` and refused it as an unsigned
/// underflow -- even though Rust never range-checks a variable's arithmetic as a
/// constant expression. The refusal was a lost proof, not a false one, but it made
/// the commonest accumulator invariant unreadable. The written form is still
/// refused, and that negative control is what keeps the fix from dropping the
/// rustc-mirroring check.
#[test]
fn a_counter_arithmetic_in_an_invariant_is_not_a_written_constant() {
    let Some(report) = verify_realistic(&[
        "an_invariant_subtracts_from_a_counter",
        "an_invariant_subtracts_from_a_counter_cannot_prove_this",
        "an_invariant_written_underflow_is_refused",
    ]) else {
        return;
    };

    let o = outcome(&report, "an_invariant_subtracts_from_a_counter");
    assert!(
        o.is_verified(),
        "a counter's `i - 1` is a variable's arithmetic and must be read; got: {o}"
    );

    // The false twin proves the shape is *decided*, not discharged vacuously.
    let o = outcome(&report, "an_invariant_subtracts_from_a_counter_cannot_prove_this");
    assert!(
        matches!(o, FnOutcome::Failed { .. }),
        "the false twin must be refuted, not proved; got: {o}"
    );

    // The *written* `0 - 1` is still a rustc const-eval error, so it is refused.
    let o = outcome(&report, "an_invariant_written_underflow_is_refused");
    assert!(
        matches!(o, FnOutcome::Skipped { .. } | FnOutcome::Incomplete { .. }),
        "the written `0 - 1` must still be refused, not decided; got: {o}"
    );
}

/// A slice's length is read through the *one* rvalue the compiler prints, on any
/// toolchain.
///
/// `PtrMetadata(copy _1)` is what 1.99 and later print for a `&[T]`'s length;
/// before 1.99 (the MSRV, 1.85) the same query prints as `Len((*_1))`. Two things
/// had to hold for the entry to decide on both: `Len` had to be modelled at all,
/// and its argument had to survive parsing. `trim_end_matches(')')` stripped *both*
/// trailing parens, leaving `(*_1` -- unparseable, so the length was `Unknown` and
/// the bounds check above `a[i]` was unmodelled, turning this decidable entry into
/// an incomplete one on 1.85 only. The entry decides identically on both now.
#[test]
fn a_slices_length_is_read_on_any_toolchain() {
    let Some(report) = verify_realistic(&["slice_symbolic_index"]) else { return };
    let o = outcome(&report, "slice_symbolic_index");
    assert!(o.is_verified(), "the slice entry must decide on any toolchain; got: {o}");
}

/// A postcondition can name a component of a tuple return.
///
/// `split` returns `(u8, u8)` and claims `ret.0 <= a`. The return place is an
/// aggregate, so there is no single word for `ret`; the frontend binds `ret.0`
/// and `ret.1` to the components `_0 = (a, b)` actually stored.
#[test]
fn a_postcondition_can_name_a_tuple_component() {
    let Some(report) = verify_realistic(&["split"]) else { return };
    let o = outcome(&report, "split");
    assert!(o.is_verified(), "`split` must verify, got: {o}");
}

/// A same-dump helper that indexes its parameter must reach the caller's element.
///
/// `head` returns `a[0]` and `caller_of_head` claims `ret == a[0]`. The call is
/// inlined into a fresh callee `Env`, and before that `Env` was handed a copy of
/// the *indexed* store the callee's `_1[_2]` reads, the element was unknown and
/// the caller was incomplete. The two functions are checked together because the
/// property is about what crosses the call boundary.
#[test]
fn a_helper_that_indexes_its_parameter_reaches_the_caller_element() {
    let names = ["head", "caller_of_head"];
    let Some(report) = verify_realistic(&names) else { return };
    for name in names {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify, got: {o}");
    }
}

/// A helper that writes through a `&mut` array changes the *caller's* element.
///
/// `clear_head` writes `a[0] = 0` through the reference and returns it;
/// `caller_of_clear_head` claims `ret == 0`, which is only true if the helper's
/// write is visible to the caller. The reference is the shared store, so the
/// callee's post-state must be copied back.
#[test]
fn a_write_through_a_reference_reaches_the_caller() {
    let names = ["clear_head", "caller_of_clear_head"];
    let Some(report) = verify_realistic(&names) else { return };
    for name in names {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify, got: {o}");
    }
}

/// The soundness tripwire for the call boundary: a by-value helper's write to its
/// *own copy* must not reach the caller.
///
/// `clear_copy` clears its by-value array and returns `0`, but the caller's `a` is
/// the array it passed and is untouched, so `caller_of_written_copy` returning
/// `a[0]` is not `0`. If the callee's copy were shared with the caller's store --
/// the tempting way to make the element reads agree -- this false contract would
/// verify. It must fail.
#[test]
fn a_by_value_helper_cannot_write_the_callers_array() {
    let Some(report) = verify_realistic(&["caller_of_written_copy"]) else { return };
    let o = outcome(&report, "caller_of_written_copy");
    assert!(
        matches!(o, FnOutcome::Failed { .. }),
        "a by-value helper's write leaked to the caller; got: {o}"
    );
}

/// A signed comparison whose *left* operand is a constant must be signed.
///
/// `0 < c.lo` lowers to `Lt(const 0_i8, ...)`, and the constant carries the
/// signedness. Reading a constant operand as unsigned made this an unsigned test,
/// under which `i8::MIN` (`0x80`) is the largest value, so `ret == 1` was
/// *proved* for `c.lo == i8::MIN` -- a false proof, since `i8::MIN` is negative
/// and the branch is never taken. The pair pins both directions: the false claim
/// must fail, and the true one must verify.
#[test]
fn a_constant_left_signed_compare_is_signed_not_unsigned() {
    let names = ["signed_const_left_false", "signed_const_left_true"];
    let Some(report) = verify_realistic(&names) else { return };
    let bad = outcome(&report, "signed_const_left_false");
    assert!(
        matches!(bad, FnOutcome::Failed { .. }),
        "`signed_const_left_false` proves a false contract; got: {bad}"
    );
    let good = outcome(&report, "signed_const_left_true");
    assert!(good.is_verified(), "`signed_const_left_true` must verify, got: {good}");
}

/// A `(bool, u8)` return can be checked on its bool component.
///
/// `ret.0` is a `bool` in the type but compared against a one-bit word in the
/// postcondition, so the component has to be widened when it is read out of the
/// return place. Before that, `ret.0` had no binding and the contract was refused
/// as unreadable.
#[test]
fn a_postcondition_can_name_a_bool_tuple_component() {
    let Some(report) = verify_realistic(&["bool_component"]) else { return };
    let o = outcome(&report, "bool_component");
    assert!(o.is_verified(), "`bool_component` must verify, got: {o}");
}

/// A tuple local's component can be read back as a *field* projection.
///
/// `let t = (a, b); t.0` stores the components under an index projection and
/// reads them back as `_3.0`, a field projection. Before the two spellings met in
/// one store, `t.0` was `Unknown` and a contract about the return was unreadable.
#[test]
fn a_tuple_locals_component_can_be_read_back() {
    let Some(report) = verify_realistic(&["tuple_local_component"]) else { return };
    let o = outcome(&report, "tuple_local_component");
    assert!(o.is_verified(), "`tuple_local_component` must verify, got: {o}");
}

/// A component of a tuple-*returning call* reaches the caller.
///
/// `min_call` returns `min2(a, b).0`. The callee's components live in its own
/// return place, so they must cross the call boundary with the aggregate; before
/// that, only the aggregate crossed and the component was lost, leaving the
/// caller's contract unreadable rather than checked.
#[test]
fn a_tuple_returning_calls_component_reaches_the_caller() {
    let names = ["min2", "min_call"];
    let Some(report) = verify_realistic(&names) else { return };
    for name in names {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify, got: {o}");
    }
}

/// The soundness tripwires for the tuple-component reads.
///
/// Each is a *false* contract that the tempting wrong model would prove: reading
/// the wrong component of a tuple local, or carrying the components across a call
/// boundary in the wrong order (or as one shared value). A `verified` verdict
/// here is a proof of something untrue, so both must fail.
#[test]
fn a_tuple_component_read_cannot_prove_the_wrong_component() {
    let names = ["tuple_component_tripwire", "min_call_tripwire"];
    let Some(report) = verify_realistic(&names) else { return };
    for name in names {
        let o = outcome(&report, name);
        assert!(
            matches!(o, FnOutcome::Failed { .. }),
            "`{name}` proves the wrong tuple component; got: {o}"
        );
    }
}

/// A signed widening cast sign-extends; an unsigned one zero-fills.
///
/// Both directions are pinned because the two casts need *opposite* rules, and a
/// single-rule implementation is wrong for one of them. `truncate` zero-fills, so
/// using it for every cast modelled `-1i8 as i16` as `255` and *proved*
/// `requires: a == -1; ensures: ret == 255` -- a reachable false proof. A
/// sign-extend-everything rule would instead prove `255u8 as u16 == 65535`.
#[test]
fn a_widening_cast_respects_the_sources_signedness() {
    let names = [
        "widen_signed_true",
        "widen_signed_false",
        "widen_unsigned_true",
        "widen_unsigned_false",
    ];
    let Some(report) = verify_realistic(&names) else { return };
    for name in ["widen_signed_true", "widen_unsigned_true"] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify, got: {o}");
    }
    for name in ["widen_signed_false", "widen_unsigned_false"] {
        let o = outcome(&report, name);
        assert!(
            matches!(o, FnOutcome::Failed { .. }),
            "`{name}` proves a false widening; got: {o}"
        );
    }
}

/// A signed array element stays signed through the subscript.
///
/// The element's signedness lives in the array's element *type* and has to reach
/// the value the subscript produces. The two entries pin both directions: the
/// true one verifies only if `xs[0] < 0` is a signed test, and the tripwire
/// (`xs[0] == i8::MIN`, where the most negative element *is* negative) is
/// *proved* under the unsigned reading, so it must FAIL.
#[test]
fn a_signed_element_comparison_keeps_the_elements_sign() {
    let names = ["signed_slice_element_is_signed", "signed_slice_element_tripwire"];
    let Some(report) = verify_realistic(&names) else { return };
    let o = outcome(&report, "signed_slice_element_is_signed");
    assert!(o.is_verified(), "`signed_slice_element_is_signed` must verify, got: {o}");
    let o = outcome(&report, "signed_slice_element_tripwire");
    assert!(
        matches!(o, FnOutcome::Failed { .. }),
        "`signed_slice_element_tripwire` proves `!(i8::MIN < 0)`; got: {o}"
    );
}

/// A postcondition naming a component a *modelled call* wrote is not refused.
///
/// `overflowing_add` returns `(T, bool)` and the modelled call writes `_0.0` and
/// `_0.1` directly -- it never assigns the tuple local as a whole. So the value
/// component lives in the *field* store while a literal's `_0 = (a, b)` lives in
/// the element store, and a `ret.N` reader that consulted only the latter found
/// the component unbound. The symptom was the whole contract refused as "the
/// return type is an aggregate, so `ret` has no single value" -- a message about
/// the wrong problem, since the reader already knew to look at components.
///
/// The two verifying entries read `ret.1` (the flag) and `ret.0` (the value), so
/// both stores are exercised, and the two tripwires must FAIL: a reader that
/// dropped a component would make those contracts vacuously true.
#[test]
fn a_postcondition_can_name_a_component_a_modelled_call_wrote() {
    let names = [
        "overflowing_add_flag_or_exact_sum",
        "overflowing_add_small_operands_never_flag",
        "overflowing_add_value_is_exact_when_no_overflow",
        "overflowing_add_flag_never_set_tripwire",
        "overflowing_add_one_bound_only_tripwire",
        "overflowing_add_wrapped_value_below_operand_tripwire",
    ];
    let Some(report) = verify_realistic(&names) else { return };
    for name in [
        "overflowing_add_flag_or_exact_sum",
        "overflowing_add_small_operands_never_flag",
        "overflowing_add_value_is_exact_when_no_overflow",
    ] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify, got: {o}");
    }
    for name in [
        "overflowing_add_flag_never_set_tripwire",
        "overflowing_add_one_bound_only_tripwire",
        "overflowing_add_wrapped_value_below_operand_tripwire",
    ] {
        let o = outcome(&report, name);
        assert!(
            matches!(o, FnOutcome::Failed { .. }),
            "`{name}` states a false contract a dropped component would prove; got: {o}"
        );
    }
}

/// A `(u8, &u8)` return with a postcondition on `ret.0` alone is not skipped.
///
/// Component 1 is a reference with no bit-vector width. Binding every declared
/// component up front made the up-front parse probe refuse, so the function was
/// `Skipped` for a component the contract never named. Only the components a
/// contract names are probed.
#[test]
fn a_reference_component_the_contract_does_not_name_does_not_skip_the_function() {
    let Some(report) = verify_realistic(&["first_component_only"]) else { return };
    let o = outcome(&report, "first_component_only");
    assert!(o.is_verified(), "`first_component_only` must verify, got: {o}");
}

/// A contract naming a field the struct does not have is refused, not dropped.
///
/// This is the failure mode that matters: if the projection were ignored, the
/// conjunct would vanish and the function would verify against a weaker contract
/// than the source states. The refusal is asserted on the *evaluator* rather than
/// on a corpus function, so it cannot pass by the function being skipped for some
/// unrelated reason.
#[test]
fn a_contract_naming_an_unknown_field_is_refused() {
    let mut args = std::collections::BTreeMap::new();
    args.insert("c".to_string(), verif::smt::var(8));
    // `c` is a known aggregate with no bindings, so the refusal names the field.
    let mut env = spec::FieldEnv::default();
    env.declare_arg("c");
    let err = spec::eval_requires("c.lo <= 3", &args, &std::collections::BTreeSet::new(), &env)
        .expect_err("an unbound field must be refused");
    assert!(
        err.to_string().contains("unknown field `lo`"),
        "the refusal must name the field, got: {err}"
    );
}

/// The field rewrite is token-aware: it must not fire inside a longer name.
///
/// `myc.lo` is a different path from `c.lo`. A naive `str::replace` would rewrite
/// the tail of `myc.lo` into `myc__f0` (or rewrite `c.lo` inside it), binding the
/// contract to a variable the caller never declared — a wrong proof. The rewrite
/// only fires on a whole `ident.ident` run that matches a *bound* path.
#[test]
fn the_field_rewrite_does_not_fire_inside_a_longer_name() {
    let mut fields = spec::FieldEnv::default();
    let c = verif::smt::var(8);
    fields.bind_bits(1, "c", "lo", 0, 8, false).unwrap();
    let mut args = std::collections::BTreeMap::new();
    args.insert("myc".to_string(), c);
    // `myc` is *not* a declared aggregate, so its `.lo` is left alone and the
    // parser refuses the unknown name `myc`. The point is that it was not
    // rewritten into the binding that belongs to `c`.
    let err = spec::eval_requires("myc.lo <= 3", &args, &std::collections::BTreeSet::new(), &fields)
        .expect_err("`myc.lo` must not be rewritten to the binding for `c.lo`");
    let msg = err.to_string();
    assert!(
        msg.contains("unknown name") || msg.contains("expected a comparison"),
        "`myc.lo` must be refused, got: {msg}"
    );
    assert!(
        !msg.contains("unknown field"),
        "`myc` is not a known aggregate, so the message must not claim a bad field: {msg}"
    );
    // And the bound path itself *is* rewritten and resolves.
    let ok = spec::eval_requires("c.lo <= 3", &args, &std::collections::BTreeSet::new(), &fields);
    assert!(ok.is_ok(), "the bound path must resolve, got: {ok:?}");
}

// ---------------------------------------------------------------------------
// Symbolic array subscripts
// ---------------------------------------------------------------------------

/// `a[i]` on a fixed-length array is modelled as the exact ite chain over `0..N`,
/// and the precondition has to establish `i < N`.
///
/// An array's length is in its type, so unlike a slice there *is* a finite domain
/// to enumerate: `a[i]` is `ite(i == 0, a0, ite(i == 1, a1, ...))`, a definition
/// rather than an approximation. The chain can only be `a[i]` where `i < N`, so
/// `requires: i < 4` is what makes the subscript admissible.
#[test]
fn a_symbolic_array_index_is_modelled_as_an_ite_chain() {
    let Some(report) = verify_realistic(&["array_symbolic_index"]) else { return };
    let o = outcome(&report, "array_symbolic_index");
    assert!(o.is_verified(), "`array_symbolic_index` must verify, got: {o}");
}

/// The soundness gate: a symbolic subscript whose index the precondition does not
/// bound is refused, not checked.
///
/// The ite chain has to return *something* for `i >= N` (it falls through to the
/// last element), so a contract checked without `i < N` would be checked against a
/// value that is not `a[i]`. Refusing is the only honest answer; the tempting bug
/// is to conjoin `i < N` into the precondition, which would silently strengthen
/// what the source asked to be verified.
#[test]
fn an_unbounded_symbolic_array_index_is_refused() {
    let Some(report) = verify_realistic(&["array_unbounded_index_refused"]) else { return };
    let o = outcome(&report, "array_unbounded_index_refused");
    match o {
        FnOutcome::Skipped { reason } => assert!(
            reason.contains("not known to be in range"),
            "the refusal must say the index is unbounded, got: {reason}"
        ),
        other => panic!("`array_unbounded_index_refused` must be skipped, got: {other}"),
    }
}

/// A contract naming a *different* index than the body reads is refused.
///
/// Only `a[i]` has a chain; `a[j]` does not, and rewriting it to the `a[i]` chain
/// would prove `a[j] == a[i]` -- false for every `i != j`. The refusal is what
/// keeps a name that merely looks like the bound one from being silently aliased.
#[test]
fn a_contract_naming_a_different_symbolic_index_is_refused() {
    let Some(report) = verify_realistic(&["array_wrong_index_refused"]) else { return };
    let o = outcome(&report, "array_wrong_index_refused");
    assert!(
        matches!(o, FnOutcome::Skipped { .. }),
        "`array_wrong_index_refused` must be skipped, got: {o}"
    );
}

/// A contract error quotes the *source* text, not the rewritten form.
///
/// The parser reads the contract after the field/subscript rewrite, so its errors
/// naturally quote mangled names (`a__chain`, `c__f0`) that appear nowhere in the
/// source. A name the user cannot search for is a diagnosability bug, so the eval
/// boundary re-points the error at the original text. This pins that, because it
/// is the kind of thing a later refactor silently drops.
#[test]
fn a_contract_error_quotes_the_source_text() {
    use verif::mir::spec;
    use verif::smt;
    let mut fields = spec::FieldEnv::default();
    let mut args = std::collections::BTreeMap::new();
    // Bind `a[i]` as a symbolic subscript, so the rewrite fires. `i` is an
    // argument, which is what makes it bindable as an index.
    let i = smt::var(64);
    args.insert("i".to_string(), i);
    fields.bind_array_len(0, "a", 8, false, 4);
    fields.bind_sym_index("a", 8, spec::IndexBound::Const(4), "i", i);
    let src = "ret == a[i] && ret == a[j]";
    let err = spec::eval_ensures(
        src,
        &args,
        Some(smt::var(8)),
        spec::RET_IS_UNKNOWN,
        &std::collections::BTreeSet::new(),
        false,
        &fields,
        &std::collections::BTreeMap::new(),
    )
    .expect_err("`a[j]` has no chain and must be refused");
    assert_eq!(
        err.text, src,
        "the error must quote the source contract, not the rewritten one"
    );
    assert!(
        !err.text.contains("__"),
        "no mangled name may leak into the quoted text: {}",
        err.text
    );
}

/// A subscript by a name that is a *direct alias* of an argument is resolved.
///
/// `let k = i;` on a `Copy` value lowers to no local: the body's `a[k]` is
/// `a[i]`, and `k` survives only as a scoped `debug k => _2;`. The contract's `k`
/// therefore names the very index the body reads, so it must verify — and the
/// tripwire must FAIL, which is what shows the name resolved to the real index
/// rather than to a constant the postcondition happened to accept.
#[test]
fn a_symbolic_index_through_a_local_alias_is_resolved() {
    let Some(report) =
        verify_realistic(&["array_local_index_through_alias", "array_local_index_tripwire"])
    else {
        return;
    };
    let o = outcome(&report, "array_local_index_through_alias");
    assert!(o.is_verified(), "`array_local_index_through_alias` must verify, got: {o}");
    let t = outcome(&report, "array_local_index_tripwire");
    assert!(
        !t.is_verified(),
        "the alias tripwire must be refuted, got: {t}"
    );
}

/// The alias rule is not a licence to follow a reassigned argument.
///
/// `let k = i; i = 2;` — rustc materialises a real local for the copy and prints
/// `debug k => _3;` pointing at *that* local, not at `_2`. So `k` is not an alias
/// of the argument and the contract's `k` is refused. If the frontend instead
/// aliased `k` to `i` unconditionally, the contract would be checked against a
/// reassigned index and could be proved or refuted about the wrong value.
#[test]
fn a_subscript_through_a_reassigned_argument_is_refused() {
    let Some(report) = verify_realistic(&["array_local_index_written_refused"]) else { return };
    let o = outcome(&report, "array_local_index_written_refused");
    match o {
        FnOutcome::Skipped { reason } => assert!(
            reason.contains("unknown name"),
            "a subscript through a reassigned argument must be refused as an unknown name, \
             got: {reason}"
        ),
        other => panic!("`array_local_index_written_refused` must be skipped, got: {other}"),
    }
}

/// A subscript through an alias is one element under *every* spelling of the
/// index, not just the first.
///
/// `a[i]` and `a[k]` with `let k = i` name the same element, so a contract using
/// both must be readable and verify — the second subscript has to rewrite to the
/// same chain as the first. The tripwire claims they differ and must be refuted.
/// Keeping only the first spelling left the second unrewritten and the parser
/// refused the whole contract, which skipped a function that should have
/// verified; this pins the fix.
#[test]
fn a_symbolic_index_resolves_under_every_spelling() {
    let Some(report) =
        verify_realistic(&["array_two_spellings_of_one_index", "array_two_spellings_tripwire"])
    else {
        return;
    };
    let o = outcome(&report, "array_two_spellings_of_one_index");
    assert!(o.is_verified(), "`array_two_spellings_of_one_index` must verify, got: {o}");
    let t = outcome(&report, "array_two_spellings_tripwire");
    assert!(
        !t.is_verified(),
        "the two-spellings tripwire must be refuted, got: {t}"
    );
}

/// A subscript by a name that is *shadowed* is refused.
///
/// `let k = i; let k = 0;` prints two bindings for `k`, and only the second is
/// what `a[k]` reads. Resolving `k` from the first would check the contract
/// against `i` while the body used the constant — a contract that is false for
/// `i == 1` could prove. The name is bound more than once, so it is refused.
#[test]
fn a_shadowed_alias_is_refused() {
    let Some(report) = verify_realistic(&["array_shadowed_alias_refused"]) else { return };
    let o = outcome(&report, "array_shadowed_alias_refused");
    match o {
        FnOutcome::Skipped { reason } => assert!(
            reason.contains("unknown name"),
            "a shadowed alias must be refused as an unknown name, got: {reason}"
        ),
        other => panic!("`array_shadowed_alias_refused` must be skipped, got: {other}"),
    }
}

/// A *qualified* standard construction (`Option::<u8>::Some`) resolves.
///
/// rustc prints a standard construction with its generic arguments, so the
/// qualifier is `Option`, not `u8`. A reader that only consults declared layouts
/// for a qualified name finds none for `Option` and refuses the construction,
/// losing a shape the unqualified spelling handles. The true contract must
/// verify and the false one must be refuted, which only happens if the tag and
/// the payload are both read.
#[test]
fn a_qualified_standard_construction_resolves() {
    let Some(report) = verify_realistic(&[
        "option_qualified_some_payload",
        "option_qualified_some_payload_tripwire",
    ]) else {
        return;
    };
    let o = outcome(&report, "option_qualified_some_payload");
    assert!(o.is_verified(), "`option_qualified_some_payload` must verify, got: {o}");
    let t = outcome(&report, "option_qualified_some_payload_tripwire");
    assert!(!t.is_verified(), "the qualified-construction tripwire must be refuted, got: {t}");
}

/// The `Option` *methods* read the receiver's tag and payload, not a bit-vector
/// operand.
///
/// `unwrap_or`, `unwrap`, `expect`, `is_some` and `is_none` are the first modelled
/// calls whose input is an enum's state rather than a word. Each true direction
/// must verify and each tripwire must be *refuted* (not merely undecided): the
/// tripwires are the evidence the tag and payload are read, because a model that
/// returned a constant -- a fixed tag, or the payload unconditionally -- would
/// prove the false contract instead.
///
/// The two panic tripwires (`option_unwrap_unguarded`, `option_expect_unguarded`)
/// are the sharpest: they must fail with a *panic* obligation, because an
/// unguarded `unwrap` on a value that can be `None` really does panic. A model
/// that reported the method total would report them verified.
#[test]
fn the_option_methods_read_the_receivers_tag_and_payload() {
    let names = [
        "option_unwrap_or_fallback",
        "option_unwrap_or_fallback_tripwire",
        "option_is_some_true_on_some",
        "option_is_some_true_on_some_tripwire",
        "option_is_some_false_on_none",
        "option_is_some_false_on_none_tripwire",
        "option_is_none_false_on_some",
        "option_is_none_false_on_some_tripwire",
        "option_is_none_true_on_none",
        "option_is_none_true_on_none_tripwire",
        "option_unwrap_guarded",
        "option_unwrap_unguarded",
        "option_expect_guarded",
        "option_expect_unguarded",
        "option_is_some_guards_unwrap",
        "option_is_none_guards_unwrap",
        "option_guard_tripwire",
        "option_arg_unwrap_or",
        "option_arg_unwrap_or_tripwire",
        "option_arg_unwrap_unguarded",
        "result_is_ok",
        "result_is_ok_tripwire",
        "result_is_err",
        "result_is_err_tripwire",
        "result_unwrap_or",
        "result_unwrap_or_tripwire",
        "result_unwrap_unguarded",
    ];
    let Some(report) = verify_realistic(&names) else {
        return;
    };
    // The true directions.
    for name in [
        "option_unwrap_or_fallback",
        "option_is_some_true_on_some",
        "option_is_some_false_on_none",
        "option_is_none_false_on_some",
        "option_is_none_true_on_none",
        "option_unwrap_guarded",
        "option_expect_guarded",
        "option_is_some_guards_unwrap",
        "option_is_none_guards_unwrap",
        "option_arg_unwrap_or",
        "result_is_ok",
        "result_is_err",
        "result_unwrap_or",
    ] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify, got: {o}");
    }
    // The postcondition tripwires: refuted, so the tag and payload are read.
    for name in [
        "option_unwrap_or_fallback_tripwire",
        "option_is_some_true_on_some_tripwire",
        "option_is_some_false_on_none_tripwire",
        "option_is_none_false_on_some_tripwire",
        "option_is_none_true_on_none_tripwire",
        "option_arg_unwrap_or_tripwire",
        "result_is_ok_tripwire",
        "result_is_err_tripwire",
        "result_unwrap_or_tripwire",
    ] {
        let o = outcome(&report, name);
        assert!(!o.is_verified(), "`{name}` must be refuted, got: {o}");
    }
    // The panic tripwires: an unguarded `unwrap`/`expect`/argument `unwrap` panics
    // on the failure value, and the *wrong-arm* guard unwraps on that path, so each
    // must fail with a *panic* obligation, not merely be undecided.
    for name in [
        "option_unwrap_unguarded",
        "option_expect_unguarded",
        "option_guard_tripwire",
        "option_arg_unwrap_unguarded",
        "result_unwrap_unguarded",
    ] {
        let o = outcome(&report, name);
        match o {
            FnOutcome::Failed { kind, .. } => {
                assert!(
                    kind.contains("panic"),
                    "`{name}` must fail on a panic obligation, got kind: {kind}"
                );
            }
            other => panic!("`{name}` must fail with a panic, got: {other}"),
        }
    }
    // The `Result` panic message names `Err`, not `None`: the evidence the model
    // read the `Result` tag (payload on `Ok`) rather than the `Option` one. A model
    // that assumed `Some` would report the panic at the wrong variant, or (worse)
    // read the `Err` payload as the value.
    let FnOutcome::Failed { kind, .. } = outcome(&report, "result_unwrap_unguarded") else {
        panic!("`result_unwrap_unguarded` must fail on the `Err` panic");
    };
    assert!(
        kind.contains("Err") && !kind.contains("None"),
        "the `Result::unwrap` panic must name `Err`, got kind: {kind}"
    );
}

/// An enum-returning function's contract can name the *discriminant* methods:
/// `ret.is_ok()`, `ret.is_some()`, `ret.is_err()`, `ret.is_none()`.
///
/// A function returning `Option`/`Result` has no single return word, so a bare
/// `ret <= n` is refused. The four methods read the return place's tag, which the
/// executor passes as the return word. Each is total over the tag -- unlike
/// `ret.Ok`, which names a payload that exists on only one variant -- so they are
/// admitted without a payload guard.
///
/// The pairs are the teeth: a body that always returns `Some`/`Ok` must *verify*
/// `ret.is_some()`/`ret.is_ok()` and *refute* `ret.is_none()`/`ret.is_err()`, and
/// the `None`/`Err` bodies the other way round. A model that read the tag as a
/// constant, or swapped the two discriminants, would prove the tripwires.
#[test]
fn an_enum_return_contract_can_name_the_discriminant_methods() {
    let names = [
        "enum_ret_is_some",
        "enum_ret_is_some_tripwire",
        "enum_ret_is_none",
        "enum_ret_is_none_tripwire",
        "enum_ret_is_ok",
        "enum_ret_is_ok_tripwire",
        "enum_ret_is_err",
        "enum_ret_is_err_tripwire",
        "enum_ret_is_ok_or_err",
        "enum_ret_is_ok_and_err_tripwire",
        "enum_ret_passthrough",
        "enum_ret_passthrough_tripwire",
        "enum_ret_from_match",
        "enum_ret_from_match_tripwire",
        "enum_ret_method_on_non_enum",
        "enum_arg_is_ok_guarded",
        "enum_arg_is_ok_guarded_tripwire",
        "enum_arg_is_ok_or_err",
        "enum_arg_is_ok_and_err_tripwire",
    ];
    let Some(report) = verify_realistic(&names) else {
        return;
    };
    // The true directions: the success/failure tag matches the method.
    for name in [
        "enum_ret_is_some",
        "enum_ret_is_none",
        "enum_ret_is_ok",
        "enum_ret_is_err",
        "enum_ret_is_ok_or_err",
        "enum_ret_passthrough",
        "enum_ret_from_match",
        "enum_arg_is_ok_guarded",
        "enum_arg_is_ok_or_err",
    ] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify, got: {o}");
    }
    // The tripwires: refuted, so the method read the *right* discriminant.
    for name in [
        "enum_ret_is_some_tripwire",
        "enum_ret_is_none_tripwire",
        "enum_ret_is_ok_tripwire",
        "enum_ret_is_err_tripwire",
        "enum_ret_is_ok_and_err_tripwire",
        "enum_ret_passthrough_tripwire",
        "enum_ret_from_match_tripwire",
        "enum_arg_is_ok_guarded_tripwire",
        "enum_arg_is_ok_and_err_tripwire",
    ] {
        let o = outcome(&report, name);
        assert!(!o.is_verified(), "`{name}` must be refuted, got: {o}");
    }
    // The refusal: a non-enum return names a tag that does not exist, so the
    // method is unreadable -- a skip, never a verdict against a fabricated tag.
    let o = outcome(&report, "enum_ret_method_on_non_enum");
    assert!(
        matches!(o, FnOutcome::Skipped { .. }),
        "`ret.is_ok()` on a non-enum return must be refused, got: {o}"
    );
}

/// An enum-returning function's contract can name a *payload*: `ret.Ok`,
/// `ret.Some`, `ret.Err`, `ret.Data` (a user enum's variant).
///
/// A payload exists only on the variant's paths, so the clause naming it is
/// *guarded on the discriminant* (`tag == disc => clause`) and is vacuous on the
/// other variant. The true entries verify because the contract's `ret.Ok` and the
/// body's `((_0 as Ok).0)` are one variable; the tripwires claim a value the
/// payload cannot be, so a model that bound the payload to a fresh unconstrained
/// variable (or dropped the guard) would prove them. The two refusals pin the
/// boundary: a non-enum return and a unit variant have no payload to read.
#[test]
fn an_enum_return_contract_can_name_a_payload() {
    let names = [
        "ret_ok_payload",
        "ret_ok_payload_tripwire",
        "ret_some_payload",
        "ret_some_payload_tripwire",
        "ret_err_payload",
        "ret_err_payload_tripwire",
        "ret_payload_guarded_vacuous",
        "ret_payload_guarded_vacuous_tripwire",
        "ret_user_enum_payload",
        "ret_user_enum_payload_tripwire",
        "ret_payload_on_non_enum",
        "ret_payload_on_unit_variant",
    ];
    let Some(report) = verify_realistic(&names) else {
        return;
    };
    for name in [
        "ret_ok_payload",
        "ret_some_payload",
        "ret_err_payload",
        "ret_payload_guarded_vacuous",
        "ret_user_enum_payload",
    ] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify, got: {o}");
    }
    for name in [
        "ret_ok_payload_tripwire",
        "ret_some_payload_tripwire",
        "ret_err_payload_tripwire",
        "ret_payload_guarded_vacuous_tripwire",
        "ret_user_enum_payload_tripwire",
    ] {
        assert_refuted(&report, name);
    }
    // The refusals: a non-enum return and a unit variant have no payload, so the
    // contract is unreadable -- a skip, never a verdict against a fabricated value.
    for name in ["ret_payload_on_non_enum", "ret_payload_on_unit_variant"] {
        let o = outcome(&report, name);
        assert!(
            matches!(o, FnOutcome::Skipped { .. }),
            "`{name}` must be refused (no such payload), got: {o}"
        );
    }
}

/// A parenthesized comparison used as an operand is a `bool` value, and a bare
/// one-bit word at the predicate position reads as the value itself.
///
/// `ret == (x == 1)` is the natural spelling of a `bool`-returning function's
/// postcondition, and the arithmetic parser refused it with "missing `)`" -- a
/// dropped obligation for an ordinary shape. The group's predicate is turned into a
/// one-bit value, so the surrounding `==` compares two `bool`s as Rust does. The
/// true/tripwire pairs pin that the group is read as the comparison's *value*: a
/// reader that dropped the group would prove the tripwire, and one that read it as
/// a constant could not verify the true direction at every input.
#[test]
fn a_parenthesized_comparison_is_a_bool_operand() {
    let names = [
        "bool_ret_is_comparison",
        "bool_ret_is_comparison_tripwire",
        "bool_ret_grouped_arithmetic",
        "bool_ret_bare",
        "bool_ret_bare_tripwire",
        "bool_ret_negated",
    ];
    let Some(report) = verify_realistic(&names) else {
        return;
    };
    for name in [
        "bool_ret_is_comparison",
        "bool_ret_grouped_arithmetic",
        "bool_ret_bare",
        "bool_ret_negated",
    ] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify, got: {o}");
    }
    for name in ["bool_ret_is_comparison_tripwire", "bool_ret_bare_tripwire"] {
        assert_refuted(&report, name);
    }
}

/// Three payload-summary defects, each of which let a *false* claim be verified.
///
/// * **A parenthesized conjunction of payload clauses** re-guarded both payloads
///   with a combined `tag == 0 && tag == 1` -- unsatisfiable -- so the group was
///   vacuously true and `(ret.Ok == 9 && ret.Err == 8)` was *verified* on a body
///   returning `Err(7)`. The grouped and ungrouped spellings must now agree: both
///   refuted. The true companion pins that the fix did not simply refuse every
///   group.
/// * **A multi-field payload variant** wrote every operand to the single-valued
///   payload store, so the last field won and `ret.Pair` (field 0) denoted field 1.
///   `ret.Pair == 2` was verified; it is refuted now, and `ret.Pair == 1` verifies.
/// * **An argument named like the generated payload binding** (`ret__v_Ok`) was
///   overwritten by it, so `ret.Ok == ret__v_Ok` compared the payload with itself
///   and verified. The binding is now dropped on a collision, so the contract is
///   refused -- a skip, never a proof against the wrong value.
/// * **A *negated* payload comparison** read its discriminant guard from *inside*
///   the `!`: `!(ret.Ok == 9)` became `!(tag == 0 => payload == 9)`, which is false
///   on an `Err` return, so a valid contract was refuted while the equivalent
///   `ret.Ok != 9` verified. The guard is now applied outside the negation, and
///   the two spellings agree -- both verify, and both are refuted on an `Ok(9)`
///   body. A negation that spans two variants has no single scope, so it is
///   refused rather than given a contradictory guard.
#[test]
fn payload_summaries_cannot_prove_a_false_claim() {
    let names = [
        "grouped_payload_conjunction_cannot_prove_this",
        "grouped_payload_conjunction_ok",
        "ungrouped_payload_conjunction_cannot_prove_this",
        "duo_pair_first_field",
        "duo_pair_second_field_cannot_prove_this",
        "payload_binding_shadow_tripwire",
        "negated_payload_comparison",
        "negated_payload_comparison_inequality",
        "negated_payload_comparison_cannot_prove_this",
        "negated_payload_mixed_variant_is_refused",
    ];
    let Some(report) = verify_realistic(&names) else {
        return;
    };
    let o = outcome(&report, "grouped_payload_conjunction_ok");
    assert!(o.is_verified(), "`grouped_payload_conjunction_ok` must verify, got: {o}");
    let o = outcome(&report, "duo_pair_first_field");
    assert!(o.is_verified(), "`duo_pair_first_field` must verify, got: {o}");
    for name in [
        "grouped_payload_conjunction_cannot_prove_this",
        "ungrouped_payload_conjunction_cannot_prove_this",
        "duo_pair_second_field_cannot_prove_this",
    ] {
        assert_refuted(&report, name);
    }
    // The shadowing argument's contract is *unreadable* (the binding is dropped),
    // so the function is skipped -- the refusal, not a verdict.
    let o = outcome(&report, "payload_binding_shadow_tripwire");
    assert!(
        matches!(o, FnOutcome::Skipped { .. }),
        "`payload_binding_shadow_tripwire` must be refused, got: {o}"
    );
    // The negation fix: the `!(…)` and `!=` spellings must agree, both verifying
    // here (the `Err` path leaves `ret.Ok` vacuous), and both refuted on an `Ok(9)`
    // body. The mixed-variant negation has no single scope, so it is refused.
    for name in ["negated_payload_comparison", "negated_payload_comparison_inequality"] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify, got: {o}");
    }
    assert_refuted(&report, "negated_payload_comparison_cannot_prove_this");
    let o = outcome(&report, "negated_payload_mixed_variant_is_refused");
    assert!(
        matches!(o, FnOutcome::Skipped { .. }),
        "`negated_payload_mixed_variant_is_refused` must be refused, got: {o}"
    );
}

/// The `?` operator decides, on both the `Ok`/`Some` and the `Err`/`None` paths,
/// and its false directions are refuted.
///
/// `expr?` lowers to three steps the model has to agree on: `<T as Try>::branch`
/// produces a `ControlFlow<Residual, T>`, a `switchInt` reads its discriminant,
/// and on `Break` a `FromResidual::from_residual` call re-wraps the error as the
/// function's return enum. The test pins each step's own observable:
///
/// * the *success* value: `result_try_value_used` uses the yielded value
///   (`r.Ok + 1`), so a model that bound the `Continue` payload to the wrong
///   variant, or to the wrong width, would refute it.
/// * the *tag* the `switchInt` reads: the `Err`-propagation pair, where the
///   failure path must produce `ret.is_err()` and the opposite claim is refuted.
/// * `from_residual`'s re-wrap: `ret.Err == r.Err`, so the error value has to
///   survive the conversion, not just the tag.
///
/// Both families (`Result` and `Option`) are covered, because they differ in
/// where the payload lives (`Ok` is 0, `Some` is 1) and in whether the residual
/// carries a value (`Result` does, `Option`'s `None` does not).
#[test]
fn the_try_operator_decides_on_both_paths() {
    let names = [
        "result_try_ok_value",
        "result_try_ok_value_cannot_prove_this",
        "result_try_err_propagates",
        "result_try_err_propagates_cannot_prove_this",
        "result_try_value_used",
        "result_try_value_used_cannot_prove_this",
        "option_try_some_value",
        "option_try_some_value_cannot_prove_this",
        "option_try_none_propagates",
        "option_try_none_propagates_cannot_prove_this",
        "result_try_of_a_known_ok",
        "result_try_of_a_known_err",
        "result_try_of_a_known_err_cannot_prove_this",
        "option_try_of_a_known_none",
        "option_try_of_a_known_none_cannot_prove_this",
        "result_try_chained",
        "result_try_chained_cannot_prove_this",
        "result_try_in_either_branch",
        "result_try_in_either_branch_cannot_prove_this",
        "result_try_widens_the_error",
        "result_try_widens_the_error_cannot_prove_this",
    ];
    let Some(report) = verify_realistic(&names) else {
        return;
    };
    for name in [
        "result_try_ok_value",
        "result_try_err_propagates",
        "result_try_value_used",
        "option_try_some_value",
        "option_try_none_propagates",
        "result_try_of_a_known_ok",
        "result_try_of_a_known_err",
        "option_try_of_a_known_none",
        "result_try_chained",
        "result_try_in_either_branch",
        "result_try_widens_the_error",
    ] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify, got: {o}");
    }
    for name in [
        "result_try_ok_value_cannot_prove_this",
        "result_try_err_propagates_cannot_prove_this",
        "result_try_value_used_cannot_prove_this",
        "option_try_some_value_cannot_prove_this",
        "option_try_none_propagates_cannot_prove_this",
        "result_try_of_a_known_err_cannot_prove_this",
        "option_try_of_a_known_none_cannot_prove_this",
        "result_try_chained_cannot_prove_this",
        "result_try_in_either_branch_cannot_prove_this",
        "result_try_widens_the_error_cannot_prove_this",
    ] {
        assert_refuted(&report, name);
    }
}

/// The `Option`↔`Result` conversions -- `Option::ok_or`, `Result::ok`,
/// `Result::err` -- decide, and both the *tag* and the *payload* cross.
///
/// Three things are pinned, and each is a way the model could be wrong:
///
/// * the tag: `ok_or`/`ok` reinterpret the receiver's tag (`Ok` and `Some` are
///   discriminants 0 and 1, so `1 - tag`), while `err` *keeps* it (`Err` is
///   already the payload-carrying variant). A model that inverted `err`'s tag, or
///   read the result tag as a constant, proves a tripwire.
/// * the payload: the moved payload is recorded under the discriminant the
///   consuming `switchInt` selects, at the *payload's* width. The `_moves_the_payload`
///   pair reads it back through `unwrap`; a model that recorded the wrong width (as
///   the first cut did -- it stored the error argument as the `Ok` payload) refutes
///   the true direction and cannot prove the false one.
/// * the classifier: `ok_or` is printed with a *method* turbofish
///   (`Option::<u8>::ok_or::<i32>`), so its name must be found past the `<…>`
///   segment, or the call is reported unmodelled.
#[test]
fn the_option_result_conversions_decide_and_move_the_payload() {
    let names = [
        "ok_or_some_is_ok",
        "ok_or_some_is_ok_tripwire",
        "ok_or_none_is_err",
        "ok_or_none_is_err_tripwire",
        "ok_or_moves_the_payload",
        "ok_or_moves_the_payload_tripwire",
        "ok_ok_is_some",
        "ok_ok_is_some_tripwire",
        "ok_err_is_none",
        "ok_err_is_none_tripwire",
        "ok_moves_the_payload",
        "ok_moves_the_payload_tripwire",
        "err_err_is_some",
        "err_err_is_some_tripwire",
        "err_ok_is_none",
        "err_ok_is_none_tripwire",
        "err_moves_the_payload",
        "err_moves_the_payload_tripwire",
    ];
    let Some(report) = verify_realistic(&names) else {
        return;
    };
    for name in [
        "ok_or_some_is_ok",
        "ok_or_none_is_err",
        "ok_or_moves_the_payload",
        "ok_ok_is_some",
        "ok_err_is_none",
        "ok_moves_the_payload",
        "err_err_is_some",
        "err_ok_is_none",
        "err_moves_the_payload",
    ] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify, got: {o}");
    }
    for name in [
        "ok_or_some_is_ok_tripwire",
        "ok_or_none_is_err_tripwire",
        "ok_or_moves_the_payload_tripwire",
        "ok_ok_is_some_tripwire",
        "ok_err_is_none_tripwire",
        "ok_moves_the_payload_tripwire",
        "err_err_is_some_tripwire",
        "err_ok_is_none_tripwire",
        "err_moves_the_payload_tripwire",
    ] {
        // A *refutation*: the conversion is only pinned if the false contract
        // produced a counterexample. `!is_verified()` would also accept a skip.
        assert_refuted(&report, name);
    }
}

/// The three findings of the PR #8 review round on the enum conversions.
///
/// * A *constructed* source variant (`None::<u8>.ok_or(e)`, `Err(e).ok()`,
///   `Ok(x).err()`) records no payload for the variant that is absent, so the
///   conversion must still produce a result. It is sound to leave the unreachable
///   payload unbound when the tag is a *known* other discriminant; a symbolic tag
///   still refuses. The tripwires refute the *tag*, which is what proves the
///   conversion ran rather than the whole call being dropped.
/// * The grouping and negation lookaheads recognise the `base.method()` enum
///   predicate, so `(ret.is_ok())` and `!x.is_none()` read instead of being routed
///   into arithmetic.
#[test]
fn the_option_result_conversions_handle_constructed_variants_and_grouping() {
    let names = [
        "constructed_none_ok_or_is_err",
        "constructed_none_ok_or_is_err_tripwire",
        "constructed_err_ok_is_none",
        "constructed_err_ok_is_none_tripwire",
        "constructed_ok_err_is_none",
        "constructed_ok_err_is_none_tripwire",
        "constructed_ok_ok_is_some",
        "constructed_ok_ok_is_some_tripwire",
        "grouped_enum_predicate",
        "grouped_enum_predicate_tripwire",
        "negated_enum_predicate",
        "negated_enum_predicate_tripwire",
        "grouped_enum_predicate_conjunction",
        "grouped_enum_predicate_conjunction_tripwire",
        "bare_negated_enum_predicate",
        "bare_negated_enum_predicate_tripwire",
    ];
    let Some(report) = verify_realistic(&names) else {
        return;
    };
    for name in [
        "constructed_none_ok_or_is_err",
        "constructed_err_ok_is_none",
        "constructed_ok_err_is_none",
        "constructed_ok_ok_is_some",
        "grouped_enum_predicate",
        "negated_enum_predicate",
        "grouped_enum_predicate_conjunction",
        "bare_negated_enum_predicate",
    ] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify, got: {o}");
    }
    for name in [
        "constructed_none_ok_or_is_err_tripwire",
        "constructed_err_ok_is_none_tripwire",
        "constructed_ok_err_is_none_tripwire",
        "constructed_ok_ok_is_some_tripwire",
        "grouped_enum_predicate_tripwire",
        "negated_enum_predicate_tripwire",
        "grouped_enum_predicate_conjunction_tripwire",
        "bare_negated_enum_predicate_tripwire",
    ] {
        // A *refutation*, not merely "not verified": these tripwires are only
        // evidence of the conversion/parser if the false contract produced a
        // counterexample. `!is_verified()` would also accept a skip.
        assert_refuted(&report, name);
    }
}

/// `Option::or` and `Option::and` are total, and the *right* operand's tag and
/// payload cross.
///
/// Each combinator returns one of its two `Option`s by the receiver's tag, so the
/// result's tag is an `ite` on that tag and both payloads are carried. `or` is the
/// receiver on `Some` and the argument on `None`; `and` is the mirror, dropping the
/// receiver's payload on `Some`.
///
/// Three things are pinned, each a way the model could be wrong:
///
/// * the tag: `or` on a `None` receiver must take the *argument's* tag, and `and`
///   on a `Some` receiver must take the argument's -- so the two tripwires that
///   claim the receiver's own value are refuted.
/// * the payload: the selected payload is read back through `unwrap`, so a model
///   that recorded the wrong one refutes the true direction and cannot prove the
///   false one.
///
/// The drop-glue rule is shared with `ok`/`err`: `or` drops the argument's payload
/// on the `Some` path and `and` drops the receiver's, so a `T` whose destructor is
/// not modelled is refused rather than read as glue-free.
///
/// The *operand shapes* are pinned separately: a `checked_*` receiver's tag is the
/// arithmetic width while a seeded `Option` argument's is one bit, so the tags must
/// be normalised; `Option<T>::and<U>` returns `Option<U>`, so the receiver's
/// payload must never be `ite`-d with the argument's; and a constructed `None`
/// records no payload, so its absence must not refuse the call.
#[test]
fn option_or_and_are_total_and_select_the_right_operand() {
    let names = [
        "or_some_is_the_receiver",
        "or_some_is_the_receiver_tripwire",
        "or_none_is_the_argument",
        "or_none_is_the_argument_tripwire",
        "and_some_is_the_argument",
        "and_some_is_the_argument_tripwire",
        "and_none_is_the_receiver",
        "and_none_is_the_receiver_tripwire",
        "or_mixed_tag_widths_is_none",
        "or_mixed_tag_widths_tripwire",
        "or_mixed_tag_widths_none_is_argument",
        "and_different_payload_widths",
        "and_different_payload_widths_tripwire",
        "and_none_receiver_decides",
        "constructed_none_or_some",
        "constructed_some_or_none",
        "constructed_none_or_some_tripwire",
        "or_slices_selects_the_receiver",
        "or_slices_selects_the_receiver_tripwire",
        "and_slices_selects_the_argument",
        "and_slices_selects_the_argument_tripwire",
        "and_different_width_deref",
        "and_different_width_deref_tripwire",
        "and_scalar_receiver_ref_argument",
        "and_scalar_receiver_ref_argument_tripwire",
        "chained_or_selects_the_first",
        "chained_or_selects_the_first_cannot_prove_this",
        "and_with_a_conditional_argument",
        "and_with_a_conditional_argument_cannot_prove_this",
        "signed_or_element_is_signed",
        "signed_or_element_is_signed_cannot_prove_this",
        "signed_or_element_divides_signed",
        "signed_or_element_divides_signed_cannot_prove_this",
    ];
    let Some(report) = verify_realistic(&names) else {
        return;
    };
    for name in [
        "or_some_is_the_receiver",
        "or_none_is_the_argument",
        "and_some_is_the_argument",
        "and_none_is_the_receiver",
        "or_mixed_tag_widths_is_none",
        "or_mixed_tag_widths_none_is_argument",
        "and_different_payload_widths",
        "and_none_receiver_decides",
        "constructed_none_or_some",
        "constructed_some_or_none",
        "or_slices_selects_the_receiver",
        "and_slices_selects_the_argument",
        "and_different_width_deref",
        "and_scalar_receiver_ref_argument",
        "chained_or_selects_the_first",
        "and_with_a_conditional_argument",
        "signed_or_element_is_signed",
        "signed_or_element_divides_signed",
    ] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify, got: {o}");
    }
    for name in [
        "or_some_is_the_receiver_tripwire",
        "or_none_is_the_argument_tripwire",
        "and_some_is_the_argument_tripwire",
        "and_none_is_the_receiver_tripwire",
        "or_mixed_tag_widths_tripwire",
        "and_different_payload_widths_tripwire",
        "constructed_none_or_some_tripwire",
        "or_slices_selects_the_receiver_tripwire",
        "and_slices_selects_the_argument_tripwire",
        "and_different_width_deref_tripwire",
        "and_scalar_receiver_ref_argument_tripwire",
        "chained_or_selects_the_first_cannot_prove_this",
        "and_with_a_conditional_argument_cannot_prove_this",
        "signed_or_element_is_signed_cannot_prove_this",
        "signed_or_element_divides_signed_cannot_prove_this",
    ] {
        // A *refutation*, not merely "not verified": a swap of the two operands
        // must produce a counterexample, and a skip would not be evidence.
        assert_refuted(&report, name);
    }
}

/// `Option<&T>::copied()`/`cloned()` is the everyday `v.first().copied()` idiom:
/// the value the payload reference addresses, with the receiver's tag unchanged.
///
/// The model stores a `&T` payload as the value it addresses, so the result's
/// payload is the receiver's -- `v.first().copied()` and `*v.first().unwrap()`
/// name one word. Two things are pinned, each a way the model could be wrong:
///
/// * the payload: the copied value must be the slice's own element, so a claim
///   about the element verifies and a claim about a different value is refuted.
/// * the tag: `copied` keeps the receiver's tag, so on an empty slice `unwrap_or`
///   takes the default rather than panicking -- a model that fixed the tag to
///   `Some` would refute `copied_empty_is_none`.
///
/// `cloned` is the same shape as `copied` for a `Copy` element, so one arm serves
/// both and the `cloned` fixture pins that the shared arm is reached.
#[test]
fn option_copied_and_cloned_copy_the_payload_with_the_tag() {
    let names = [
        "copied_first_is_the_element",
        "copied_first_tripwire",
        "copied_get_is_the_element",
        "copied_get_tripwire",
        "copied_empty_is_none",
        "cloned_first_is_the_element",
    ];
    let Some(report) = verify_realistic(&names) else {
        return;
    };
    for name in [
        "copied_first_is_the_element",
        "copied_get_is_the_element",
        "copied_empty_is_none",
        "cloned_first_is_the_element",
    ] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify, got: {o}");
    }
    for name in ["copied_first_tripwire", "copied_get_tripwire"] {
        assert_refuted(&report, name);
    }
}

/// `char` is a 32-bit unsigned value, its literals and the `is_ascii_*` predicates.
///
/// The predicates are exact at width 32 because every range they test lies inside
/// `0..=0x7f`, so a value at or above `0x100` is outside all of them -- which is
/// what `char::is_ascii_digit` means too. `char_high_digit_tripwire` is the
/// evidence: `'\u{130}'`'s low byte is `'0'`, so a model that masked to eight bits
/// would *prove* it, while the real predicate is `false`.
#[test]
fn char_literals_and_ascii_predicates_are_modelled() {
    let names = [
        "char_as_byte",
        "char_ord_of_a",
        "char_digit_is_true",
        "char_digit_tripwire",
        "char_high_digit_tripwire",
        "char_high_is_not_digit",
        "char_newline_is_whitespace",
        "char_emoji_not_alphabetic",
        "char_max_is_the_scalar_maximum",
        "char_max_is_the_scalar_maximum_cannot_prove_this",
        "char_min_is_nul",
    ];
    let Some(report) = verify_realistic(&names) else {
        return;
    };
    for name in [
        "char_as_byte",
        "char_ord_of_a",
        "char_digit_is_true",
        "char_high_is_not_digit",
        "char_newline_is_whitespace",
        "char_emoji_not_alphabetic",
        "char_max_is_the_scalar_maximum",
        "char_min_is_nul",
    ] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify, got: {o}");
    }
    // Real refutations, with the scalar value in the witness -- so the literals
    // were read (120 is `'x'`, 304 is `0x130`) and not skipped.
    assert_refuted(&report, "char_digit_tripwire");
    assert_refuted(&report, "char_high_digit_tripwire");
    // `char::MAX` is `0x10FFFF`, not `2^32 - 1`: the false twin is refuted at the
    // *real* maximum (`c = 1114111`), which a width-derived bound would prove.
    assert_refuted(&report, "char_max_is_the_scalar_maximum_cannot_prove_this");
}

/// `slice::last` -- a fresh element for a slice, the exact element for an array.
///
/// A slice's length is symbolic, so `xs.last()` names no index the store can pin
/// and the element is a *fresh* variable: a contract true of every element holds
/// of the last one (`slice_last_is_an_element` verifies), and a claim that the last
/// element is a particular value is refuted, because a slice with that element can
/// exist. An *array*'s length is its type's constant, so `a.last()` is the exact
/// element `a[N-1]`: `array_last_is_the_last_element` verifies against `a[3] == 7`
/// and its twin, claiming `a[0] == 9`, refutes. The two directions together show the
/// element is neither a constant (the array case decides by the actual last element)
/// nor the first element (the twin refutes).
#[test]
fn slice_last_is_an_element_and_array_last_is_exact() {
    let names = [
        "slice_last_is_an_element",
        "array_last_is_the_last_element",
        "slice_last_is_an_element_cannot_prove_this",
        "array_last_is_the_last_element_cannot_prove_this",
        "slice_last_after_write_is_seen",
        "slice_last_after_write_is_seen_cannot_prove_this",
        "slice_last_is_kept_when_another_index_is_written",
        "slice_last_read_before_a_write_is_not_kept_cannot_prove_this",
    ];
    let Some(report) = verify_realistic(&names) else {
        return;
    };
    for name in [
        "slice_last_is_an_element",
        "array_last_is_the_last_element",
        "slice_last_after_write_is_seen",
        "slice_last_is_kept_when_another_index_is_written",
    ] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify, got: {o}");
    }
    // Real refutations: the fresh element is unconstrained, and the array's last
    // element is not the first, so neither claim can be proved.
    assert_refuted(&report, "slice_last_is_an_element_cannot_prove_this");
    assert_refuted(&report, "array_last_is_the_last_element_cannot_prove_this");
    // The write/read interaction: a concrete-index write that *is* the last
    // element must reach the sentinel, so the pre-write value does not survive.
    // A model that left the sentinel alone would prove the twin -- the false proof
    // this pair exists to pin.
    assert_refuted(&report, "slice_last_after_write_is_seen_cannot_prove_this");
    // The read-before/write/read-after shape at the length where the write reaches
    // `last()`. This is the review's exact example: both reads must not be the same
    // variable. The `len == 2` fixture above keeps `last()` across the write, so it
    // stays green whether or not the sentinel is refreshed; only this one refutes a
    // model that left the sentinel alone.
    assert_refuted(&report, "slice_last_read_before_a_write_is_not_kept_cannot_prove_this");
}

/// `u8::from(bool)` -- the one conversion the integer model would otherwise refuse.
///
/// A `bool` has no integer width, so `conversion_types` reads the pair but
/// `int_width` refuses the source. The conversion is the zero-extension of the
/// one-bit value: `false` -> 0, `true` -> 1. The `true`/`false` pair pins that the
/// bit is the *value*: a model that fixed it to a constant would prove one and
/// refute the other, and here `bool_to_u8_of_true_is_one` verifies while its
/// `false` twin refutes with the witness `b = 0`. The `u32` destination pins the
/// widening.
#[test]
fn a_bool_conversion_is_the_zero_extended_bit() {
    let names = [
        "bool_to_u8_is_zero_or_one",
        "bool_to_u8_of_true_is_one",
        "bool_to_u8_of_true_is_one_cannot_prove_this",
        "bool_to_u32_is_zero_or_one",
    ];
    let Some(report) = verify_realistic(&names) else {
        return;
    };
    for name in [
        "bool_to_u8_is_zero_or_one",
        "bool_to_u8_of_true_is_one",
        "bool_to_u32_is_zero_or_one",
    ] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify, got: {o}");
    }
    // A real refutation, so the bit is the value and not a constant one.
    assert_refuted(&report, "bool_to_u8_of_true_is_one_cannot_prove_this");
}

/// A `&mut bool` argument's final value is a predicate, not a word.
///
/// The postcondition's bare `c` means the value the body left at the target. A
/// `bool` target is a `Val::Bool`, so the word-only collection dropped it and the
/// contract was refused as an unknown name. The predicate is widened to a one-bit
/// word, exactly as a `bool` `ret` is. The false direction pins that the binding is
/// the *final* value, not the entry value.
#[test]
fn a_mutable_bool_reference_postcondition_reads_its_final_value() {
    let names = ["set_bool_ref", "a_bool_ref_write_cannot_prove_this", "keep_bool_ref"];
    let Some(report) = verify_realistic(&names) else {
        return;
    };
    for name in ["set_bool_ref", "keep_bool_ref"] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify, got: {o}");
    }
    // A real refutation, so the false direction is a counterexample and not a skip.
    assert_refuted(&report, "a_bool_ref_write_cannot_prove_this");
}

/// A trailing line comment on a variant line does not erase the enum layout.
///
/// `B = 3, // special` keeps the comma *before* the comment, so the reader that
/// trims a trailing comma from the raw line no longer finds one, rejects the
/// discriminant, and drops the whole layout -- taking every contract that names
/// a variant with it. The true contract must verify and the tripwire refute.
#[test]
fn a_trailing_comment_on_a_variant_keeps_the_layout() {
    let Some(report) = verify_realistic(&[
        "enum_trailing_comment_keeps_the_layout",
        "enum_trailing_comment_tripwire",
    ]) else {
        return;
    };
    let o = outcome(&report, "enum_trailing_comment_keeps_the_layout");
    assert!(
        o.is_verified(),
        "`enum_trailing_comment_keeps_the_layout` must verify, got: {o}"
    );
    let t = outcome(&report, "enum_trailing_comment_tripwire");
    assert!(!t.is_verified(), "the trailing-comment tripwire must be refuted, got: {t}");
}

/// A *slice* subscripted symbolically is modelled, not refused — when the
/// precondition bounds both the index and the length.
///
/// The whole feature turns on the domain being finite. An array's length is in its
/// type, so `0..N` can be enumerated; a slice's is a symbolic input, so the chain
/// enumerates `0..MAX_INDEX_CHAIN` and is exact only where the length is within
/// that. The precondition has to establish both facts, and this pins that the
/// modelled case really verifies rather than being refused.
#[test]
fn a_symbolic_slice_index_is_modelled_when_both_bounds_hold() {
    let Some(report) = verify_realistic(&["slice_symbolic_index"]) else { return };
    let o = outcome(&report, "slice_symbolic_index");
    assert!(o.is_verified(), "`slice_symbolic_index` must verify, got: {o}");
}

/// The tripwire: a false claim about `xs[i]` must be *refuted*, not refused.
///
/// The body returns `xs[i]`, and the contract claims that is also `xs[0]` — false
/// for every `i != 0`. A pass would mean the chain ignored the index. The
/// assertion is on `Failed` rather than on `!is_verified()` on purpose: a
/// `Skipped` or `Incomplete` outcome is also "not verified", and accepting one
/// would let the tripwire pass without the chain ever having been contradicted.
#[test]
fn a_slice_chain_depends_on_the_index() {
    let Some(report) = verify_realistic(&["slice_symbolic_index_tripwire"]) else { return };
    let o = outcome(&report, "slice_symbolic_index_tripwire");
    assert!(
        matches!(o, FnOutcome::Failed { .. }),
        "`slice_symbolic_index_tripwire` must be refuted (not skipped), got: {o}"
    );
}

/// Each of a slice's two bounds is required on its own, and the refusal names the
/// one that is missing.
///
/// The length bound is the half an index-only rule would miss, and the index bound
/// is the half a length-only rule would miss. Each entry below supplies one fact
/// and withholds the other, so a rule that checked only one of them would model a
/// subscript it should refuse.
#[test]
fn a_slice_subscript_missing_either_bound_is_refused() {
    let cases = [
        ("slice_unbounded_length_refused", "`xs.len() <= 64` does not hold"),
        ("slice_unbounded_index_refused", "`i < xs.len()` does not hold"),
    ];
    for (name, want) in cases {
        let Some(report) = verify_realistic(&[name]) else { return };
        let o = outcome(&report, name);
        match o {
            FnOutcome::Skipped { reason } => assert!(
                reason.contains(want),
                "`{name}`'s refusal must name the missing bound `{want}`, got: {reason}"
            ),
            other => panic!("`{name}` must be skipped, got: {other}"),
        }
    }
}

/// The chain is really evaluated: a false postcondition about `a[i]` produces a
/// counterexample that pins the index to an in-range value.
///
/// Without this, "verified" above would be consistent with the chain being a
/// constant the postcondition happened to accept. `body_only` claims `ret <= 9`
/// for an arbitrary `u8` element, which is false, so it must FAIL -- and the
/// counterexample must show the element's value, which only exists if the chain
/// selected a real element variable.
/// Verify one function from an inline source and return its outcome.
///
/// Each test below needs its own file, because the property under test is about
/// what the *frontend* does with a specific shape and the corpus is shared.
/// Writing the source to a temp dir keeps that shape next to its assertion.
///
/// Returns `None` only when no nightly `rustc` is configured, which is the one
/// legitimate reason to skip. A named function that is *absent* from the report
/// is a panic, not a skip: it means the frontend dropped it, and a test written
/// as `let Some(o) = verify_src(..) else { return }` would then pass without
/// checking anything. That is how `a_whole_struct_assignment_invalidates_its_fields`
/// hid a one-line struct that swallowed every following function. A vacuous pass
/// is worse than a failure, so this refuses to be quiet about it.
///
/// The guard `verify_src` applies: a contracted function that is absent from the
/// report is a failure, whatever `report.missing` says. `missing` is the same
/// drop from the frontend's side (a contract with no MIR function), and accepting
/// it as a pass reopened the hole the guard exists to close: a test whose function
/// the frontend drops -- for *any* reason, including one a future bug adds --
/// would assert nothing. The only source without a contract is one that carries no
/// `///` at all, which is a caller mistake, not a frontend drop.
///
/// Split out so the branch that made the guard vacuous -- `missing` naming the
/// function -- can be tested *without* a nightly `rustc`. The guard is about what
/// counts as a pass, and a test that needed the frontend could not check the one
/// case that mattered.
#[track_caller]
fn assert_not_dropped(name: &str, src: &str, present: bool, missing: &[String]) {
    // "Has a contract" must cover both spellings. It used to test only for `///`,
    // which is the doc-comment form: a source whose only contract is a `spec!`
    // block looked contract-free, so a dropped function passed the guard. The
    // marker is the clause *keyword*, which both spellings carry.
    let has_contract = src.contains("///")
        || ["requires", "ensures", "invariant", "decreases", "variant", "unwind"]
            .iter()
            .any(|k| src.contains(&format!("{k}:")));
    assert!(
        present || !has_contract,
        "`{name}` was dropped from the report: it has a contract but no outcome, so this test \
         would have passed without checking anything (missing: {missing:?})"
    );
}

fn verify_src(name: &str, src: &str) -> Option<FnOutcome> {
    let rustc = rustc()?;
    // A unique directory per call: two tests that both ask about a function
    // named `reads` run in parallel and would otherwise write the same
    // `probe.rs`, so one test could read the other's source. The counter keeps
    // the path unique within the process; the pid keeps it unique across
    // processes.
    static SEQ: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
    let seq = SEQ.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
    let dir = std::env::temp_dir()
        .join(format!("verif-{name}-{}-{seq}", std::process::id()));
    let _ = std::fs::create_dir_all(&dir);
    let file = dir.join("probe.rs");
    std::fs::write(&file, src).expect("write probe");
    let report = verify::verify_file_with_opts(&rustc, &file, &[], &probe_opts())
        .expect("frontend");
    let o = report
        .outcomes
        .into_iter()
        .find(|(n, _)| n == name)
        .map(|(_, o)| o);
    let _ = std::fs::remove_dir_all(&dir);
    assert_not_dropped(name, src, o.is_some(), &report.missing);
    o
}

/// A source file may *name* `verif` -- `use verif::spec;` above an in-code
/// contract block -- but the frontend runs `rustc` on a single file with no Cargo
/// graph behind it, so without an `--extern` entry that import is `E0432`. This
/// pins both halves: the same source is refused with no extern and verified with
/// one, so a `--extern` that never reaches the command line cannot pass.
#[test]
fn an_in_code_contract_can_name_verif_through_an_extern() {
    // Lookup and emission must name ONE compiler: a rlib's metadata is
    // compiler-specific, so probing with one toolchain and emitting with another
    // fails `E0514` even when both are installed and each has its own artifact.
    //
    // The compiler here is the one the *emitter* will use, and it is cargo's: the
    // rlib this test binary links was built by it, and a test binary has no `RUSTC`
    // in its environment. `--emit=mir` is stable, so no nightly is needed. The test
    // deliberately does NOT read `VERIF_RUSTC`: CI points that at a nightly, and
    // probing the stable-built rlib with a nightly is exactly the mismatch this
    // test exists to catch.
    let rustc = std::env::var("RUSTC")
        .ok()
        .filter(|s| !s.is_empty())
        .unwrap_or_else(|| "rustc".to_string());
    if std::process::Command::new(&rustc)
        .arg("--version")
        .output()
        .map(|o| !o.status.success())
        .unwrap_or(true)
    {
        return;
    }

    // `build_script_roots()` needs `OUT_DIR`, which a test binary does not have, so
    // the profile directory is found from the test binary's own path instead.
    // Cargo's layout is its own business (`deps/` or `build/<pkg>/<hash>/out/`, and
    // a stale rlib from an earlier build can sit beside a fresh one), so the walk
    // covers the whole profile and the choice is made by *trying* candidates
    // newest-first until one compiles -- the same rule `extern_options_with` uses.
    let exe = std::env::current_exe().expect("current exe");
    let profile = exe
        .ancestors()
        .find(|a| a.file_name().is_some_and(|n| n == "debug" || n == "release"))
        .expect("a debug or release profile directory above the test binary");

    fn walk(dir: &std::path::Path, depth: u32, out: &mut Vec<std::path::PathBuf>) {
        if depth == 0 {
            return;
        }
        let Ok(entries) = std::fs::read_dir(dir) else { return };
        for e in entries.flatten() {
            let p = e.path();
            if p.is_dir() {
                walk(&p, depth - 1, out);
            } else if p
                .file_name()
                .and_then(|n| n.to_str())
                .is_some_and(|n| n.starts_with("libverif") && n.ends_with(".rlib"))
            {
                out.push(p);
            }
        }
    }

    let mut rlibs = Vec::new();
    walk(profile, 6, &mut rlibs);
    let mut search: Vec<std::path::PathBuf> = rlibs
        .iter()
        .filter_map(|p| p.parent().map(|d| d.to_path_buf()))
        .collect();
    search.sort();
    search.dedup();
    let mut verif_rlibs: Vec<std::path::PathBuf> = rlibs
        .into_iter()
        .filter(|p| {
            p.file_name()
                .and_then(|n| n.to_str())
                .is_some_and(|n| n.starts_with("libverif-"))
        })
        .collect();
    verif_rlibs.sort();
    verif_rlibs.dedup();
    verif_rlibs.sort_by_key(|p| {
        std::cmp::Reverse(std::fs::metadata(p).and_then(|m| m.modified()).ok())
    });
    assert!(
        !verif_rlibs.is_empty(),
        "an rlib of `verif` must exist under {}",
        profile.display()
    );

    let src = "use verif::spec;\n\nspec! {\n    requires: n <= 200,\n    ensures: ret == n + 1,\n}\npub fn inc(n: u8) -> u8 { n + 1 }\n";

    static SEQ: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
    let seq = SEQ.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
    let dir = std::env::temp_dir().join(format!("verif-extern-e2e-{}-{seq}", std::process::id()));
    let _ = std::fs::create_dir_all(&dir);
    let file = dir.join("probe.rs");
    std::fs::write(&file, src).expect("write probe");

    // Without the extern, `use verif::spec;` cannot resolve: the frontend reports it.
    let bare = verify::verify_file_with_opts(&rustc, &file, &[], &probe_opts());
    assert!(
        bare.is_err(),
        "an import of `verif` must fail without an extern, got: {:?}",
        bare.map(|r| r.outcomes.len())
    );

    // With the extern, the same source verifies. Candidates are tried newest-first,
    // because a stale rlib from another compiler fails the probe (`E0514`) and the
    // next one is the answer -- which is the *same* compiler that emits the MIR.
    let mut verified = false;
    let mut last_err = String::new();
    for rlib in &verif_rlibs {
        let mut opts = probe_opts();
        opts.externs = vec![("verif".to_string(), rlib.clone())];
        opts.search_paths = search.clone();
        match verify::verify_file_with_opts(&rustc, &file, &[], &opts) {
            Ok(report) => {
                verified = report
                    .outcomes
                    .iter()
                    .find(|(n, _)| n == "inc")
                    .is_some_and(|(_, o)| o.is_verified());
                if verified {
                    break;
                }
                last_err = format!("{:?}", report.outcomes);
            }
            Err(e) => last_err = e,
        }
    }
    let _ = std::fs::remove_dir_all(&dir);
    assert!(
        verified,
        "with a working extern the in-code contract must verify; last error: {last_err}"
    );
}

/// Options for a probe written to a loose temp file.
///
/// A probe has no `Cargo.toml` above it, so the edition cannot be resolved from a
/// manifest -- and it is never guessed. The tests name the crate's own edition
/// (2024) explicitly. The corpus files (`actuator.rs`, `realistic.rs`) do live
/// under this repository's `Cargo.toml`, so those tests use the plain entry point
/// and exercise the manifest resolution path instead.
fn probe_opts() -> verify::MirOptions {
    verify::MirOptions { edition: Some(verify::Edition::Rust2024), ..Default::default() }
}

/// Every outcome and the `missing` list for one source, without the
/// "must be present" guard of [`verify_src`] -- for the trait-contract tests,
/// whose whole point is that a declaration has *no* outcome of its own.
fn verify_all_outcomes(src: &str) -> (Vec<(String, FnOutcome)>, Vec<String>) {
    let rustc = rustc().expect("a toolchain to dump MIR with");
    static SEQ: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
    let seq = SEQ.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
    let dir = std::env::temp_dir().join(format!("verif-all-{}-{seq}", std::process::id()));
    let _ = std::fs::create_dir_all(&dir);
    let file = dir.join("probe.rs");
    std::fs::write(&file, src).expect("write probe");
    let report = verify::verify_file_with_opts(&rustc, &file, &[], &probe_opts()).expect("frontend");
    let _ = std::fs::remove_dir_all(&dir);
    (report.outcomes, report.missing)
}

/// A contracted function the frontend dropped is a failure, even when the report
/// names it in `missing`.
///
/// `missing` is the drop reported from the frontend's side, and accepting it as a
/// pass reopened the hole the strict helper exists to close: a test whose function
/// the frontend drops -- for any reason, including one a future bug adds -- would
/// assert nothing. The guard must reject the `missing` case, which is what this
/// pins without needing a nightly `rustc`.
#[test]
#[should_panic(expected = "was dropped")]
fn a_dropped_contracted_function_fails_even_when_missing_names_it() {
    assert_not_dropped("ghost", "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nspec! {\n    ensures: true,\n}\npub fn ghost() {}", false, &["ghost".to_string()]);
}

/// The guard is not a blanket refusal: a function present in the report passes,
/// and so does a source with no contract at all (which is a caller mistake, not a
/// frontend drop).
#[test]
fn a_present_function_passes_the_drop_guard() {
    assert_not_dropped("f", "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nspec! {\n    ensures: true,\n}\npub fn f() {}", true, &[]);
    assert_not_dropped("f", "pub fn f() {}", false, &[]);
}

/// An element write must reach the store a *symbolic* read consults.
///
/// `a[0] = 0; a[i]` returns `0` when `i == 0`, so `ret == a[i]` is false. The
/// chain is built from the seeded elements, so if the write landed anywhere else
/// the chain would select the pre-write `a[0]` and this false contract would
/// verify. The test requires it to be *refuted*, not merely undecided: a
/// counterexample is the evidence that the chain is evaluated against the written
/// value, and `Verified` here would be a false proof.
#[test]
fn an_element_write_reaches_the_symbolic_chain() {
    let src = r#"macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }

spec! {
    requires: i < 4 && i == 0,
    ensures: ret == a[i],
}
pub fn written_then_symbolic(mut a: [u8; 4], i: usize) -> u8 {
    a[0] = 0;
    a[i]
}
"#;
    let Some(o) = verify_src("written_then_symbolic", src) else { return };
    assert!(
        matches!(o, FnOutcome::Failed { .. }),
        "a write must not leave a stale element readable by the chain; got: {o}"
    );
}

/// The same property through a `&mut [u8; 4]` reborrow.
#[test]
fn an_element_write_through_a_mutable_reference_reaches_the_chain() {
    let src = r#"macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }

spec! {
    requires: i < 4 && i == 0,
    ensures: ret == a[i],
}
pub fn ref_written_then_symbolic(a: &mut [u8; 4], i: usize) -> u8 {
    a[0] = 0;
    a[i]
}
"#;
    let Some(o) = verify_src("ref_written_then_symbolic", src) else { return };
    assert!(
        matches!(o, FnOutcome::Failed { .. }),
        "a reborrowed element write must reach the chain too; got: {o}"
    );
}

/// The `Vec<T>` length model: `new`/`push`/`len` track a length the executor
/// holds, so `make`-shaped code is decided rather than refused.
///
/// The element is *not* modelled (a `Vec`'s fields are not printed by the dump),
/// so this pins the length and nothing else. Each true direction must verify and
/// each false twin must be refuted -- a model that returned a constant, or that
/// ignored the `push`, would prove the twin. The helper direction pins that the
/// write reaches the caller through `&mut Vec<u8>`, and the argument direction
/// pins that a length the run never set is *refused* rather than invented.
#[test]
fn a_vec_length_is_tracked_through_new_push_and_len() {
    let src = r#"macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }

spec! {
    requires: true,
    ensures: ret == 1,
}
pub fn make_ok(n: u8) -> usize { let mut v = Vec::new(); v.push(n); v.len() }

spec! {
    requires: true,
    ensures: ret == 2,
}
pub fn make_bad(n: u8) -> usize { let mut v = Vec::new(); v.push(n); v.len() }

spec! {
    requires: true,
    ensures: ret == 2,
}
pub fn two_ok(n: u8) -> usize { let mut v = Vec::new(); v.push(n); v.push(n); v.len() }

spec! {
    requires: true,
    ensures: ret == 1,
}
pub fn two_bad(n: u8) -> usize { let mut v = Vec::new(); v.push(n); v.push(n); v.len() }

spec! {
    requires: true,
    ensures: ret == 0,
}
pub fn empty_ok() -> usize { Vec::<u8>::new().len() }

spec! {
    requires: true,
    ensures: ret == 3,
}
pub fn empty_bad() -> usize { Vec::<u8>::new().len() }

spec! {
    requires: true,
    ensures: ret == 1,
}
pub fn helper_ok(n: u8) -> usize { let mut v = Vec::new(); add(&mut v, n); v.len() }

spec! {
    requires: true,
    ensures: ret == 0,
}
pub fn helper_bad(n: u8) -> usize { let mut v = Vec::new(); add(&mut v, n); v.len() }

fn add(v: &mut Vec<u8>, x: u8) { v.push(x); }

spec! {
    requires: true,
    ensures: ret == 0,
}
pub fn arg_len_refused(v: &Vec<u8>) -> usize { v.len() }

spec! {
    requires: true,
    ensures: ret == 1,
}
pub fn is_empty_new_ok() -> u8 { Vec::<u8>::new().is_empty() as u8 }

spec! {
    requires: true,
    ensures: ret == 0,
}
pub fn is_empty_new_bad() -> u8 { Vec::<u8>::new().is_empty() as u8 }

spec! {
    requires: true,
    ensures: ret == 0,
}
pub fn not_empty_after_push_ok() -> u8 { let mut v = Vec::<u8>::new(); v.push(1); v.is_empty() as u8 }

spec! {
    requires: true,
    ensures: ret == 1,
}
pub fn not_empty_after_push_bad() -> u8 { let mut v = Vec::<u8>::new(); v.push(1); v.is_empty() as u8 }

spec! {
    requires: true,
    ensures: ret == 0,
}
pub fn pop_then_len_ok() -> usize { let mut v = Vec::<u8>::new(); v.push(1); v.pop(); v.len() }

spec! {
    requires: true,
    ensures: ret == 1,
}
pub fn pop_then_len_bad() -> usize { let mut v = Vec::<u8>::new(); v.push(1); v.pop(); v.len() }

spec! {
    requires: true,
    ensures: ret == 1,
}
pub fn pop_empty_is_none_ok() -> u8 { let mut v = Vec::<u8>::new(); v.pop().is_none() as u8 }

spec! {
    requires: true,
    ensures: ret == 0,
}
pub fn pop_empty_is_none_bad() -> u8 { let mut v = Vec::<u8>::new(); v.pop().is_none() as u8 }

spec! {
    requires: true,
    ensures: ret == 1,
}
pub fn pop_nonempty_is_some_ok() -> u8 { let mut v = Vec::<u8>::new(); v.push(9); v.pop().is_some() as u8 }

spec! {
    requires: true,
    ensures: ret == 0,
}
pub fn pop_nonempty_is_some_bad() -> u8 { let mut v = Vec::<u8>::new(); v.push(9); v.pop().is_some() as u8 }
"#;
    for name in [
        "make_ok",
        "two_ok",
        "empty_ok",
        "helper_ok",
        "is_empty_new_ok",
        "not_empty_after_push_ok",
        "pop_then_len_ok",
        "pop_empty_is_none_ok",
        "pop_nonempty_is_some_ok",
    ] {
        let Some(o) = verify_src(name, src) else { return };
        assert!(o.is_verified(), "`{name}` must verify; got: {o}");
    }
    for name in [
        "make_bad",
        "two_bad",
        "empty_bad",
        "helper_bad",
        "is_empty_new_bad",
        "not_empty_after_push_bad",
        "pop_then_len_bad",
        "pop_empty_is_none_bad",
        "pop_nonempty_is_some_bad",
    ] {
        let Some(o) = verify_src(name, src) else { return };
        assert!(
            matches!(o, FnOutcome::Failed { .. }),
            "`{name}` claims a length the body never produces and must be refuted, \
             not proved; got: {o}"
        );
    }
    // A `Vec` argument's length is not a value the run holds, so a `len` on it is
    // a refusal -- never an invented number.
    let Some(o) = verify_src("arg_len_refused", src) else { return };
    assert!(
        !o.is_verified(),
        "a `Vec` argument's length is unmodelled and must not verify; got: {o}"
    );
}

/// Three soundness holes in the `Vec` length model, each a *false proof*.
///
/// The model tracks a length per local (`Env::vec_len`). Each of these had the
/// length survive a place it should not, so a false postcondition was proved:
///
/// * **A user `Vec` is not the container.** A local `struct Vec<T>` whose `Drop`
///   panics was treated as glue-free (the model matched the bare `Vec`), so the
///   panic never ran and `f` was reported `Verified`. The container identity is
///   the *full* path (`std::vec::Vec` / `alloc::vec::Vec`); a user `Vec` is an
///   ordinary type and its destructor is resolved like any other.
/// * **The loop header havocs the length.** An inductive loop that pushes has to
///   forget the entry length, or the exit edge reads it: a `while i < 3 { v.push
///   (1); i += 1 }` proved `ret == 0` for the real `ret == 3`. `havoc_parts`
///   refreshes the tracked length to a fresh `usize`.
/// * **A move carries the length.** `let w = v` (`copy_parts`) and a returned
///   vector (the call's return merge) both have to transfer the tracked length to
///   the destination, or `w.len()`/`mk().len()` read a receiver with no length.
#[test]
fn a_vec_length_model_does_not_leak_across_a_drop_havoc_or_move() {
    let src = r#"macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }

mod m {
    pub struct Vec<T>(pub T);
    impl<T> Drop for Vec<T> {
        fn drop(&mut self) { panic!() }
    }
}

spec! {
    requires: true,
    ensures: true,
}
/// A user `Vec` whose destructor always panics: `drop` runs it, so this must not
/// be `verified`. The bare-name model treated `m::Vec<u8>` as glue-free.
pub fn user_vec_drop_panics() { let _x = m::Vec(0u8); }

spec! {
    requires: true,
    invariant: i <= 3,
    ensures: ret == 0,
}
/// The loop's entry length must be havoced at the header. A model that kept it
/// reads `0` at the exit and proves this; the body really returns `3`.
pub fn loop_push_cannot_prove_this() -> usize {
    let mut v = Vec::<u8>::new();
    let mut i = 0u8;
    while i < 3 { v.push(1); i += 1; }
    v.len()
}

spec! {
    requires: true,
    invariant: i <= 3,
    ensures: ret == 3,
}
/// The control: the same loop, but the postcondition is about the *counter*,
/// which the invariant does relate (`i <= 3` and the exit guard `i >= 3` give
/// `i == 3`). Havocing the vector's length must not break this -- a model that
/// refused every `Vec`-carrying loop would pass the twin above while checking
/// nothing.
pub fn loop_counter_ok() -> u8 {
    let mut v = Vec::<u8>::new();
    let mut i = 0u8;
    while i < 3 { v.push(1); i += 1; }
    i
}

spec! {
    requires: true,
    ensures: ret == 1,
}
/// A move (`let w = v`) must carry the tracked length to the destination.
pub fn move_ok(n: u8) -> usize { let mut v = Vec::new(); v.push(n); let w = v; w.len() }

spec! {
    requires: true,
    ensures: ret == 0,
}
/// The false twin: a model that dropped the length on the move reads a receiver
/// with no length and refuses; one that kept the *source* length and read it
/// through the wrong local would prove this.
pub fn move_bad(n: u8) -> usize { let mut v = Vec::new(); v.push(n); let w = v; w.len() }

spec! {
    requires: true,
    ensures: ret == 0,
}
/// A returned vector must carry its length to the caller's destination.
pub fn mk() -> Vec<u8> { Vec::new() }

spec! {
    requires: true,
    ensures: ret == 0,
}
pub fn returned_ok() -> usize { let v = mk(); v.len() }
"#;
    // The user `Vec`'s destructor panics, so the function must be *refused*.
    // The assertion is `Incomplete`, not merely `!is_verified()`: a `!is_verified`
    // check also passes on `Failed`, so it cannot tell a refusal (the honest
    // answer, since the destructor's `panic!` has no value) from a spurious
    // refutation of the `ensures: true` claim -- the same weakness the invariant
    // tripwire was fixed for. Measured, the drop of a user `Vec` whose fields are
    // not modelled is `Incomplete`; pinning it makes a regression to `Failed`
    // visible rather than silently passing.
    let Some(o) = verify_src("user_vec_drop_panics", src) else { return };
    match o {
        FnOutcome::Incomplete { .. } => {}
        other => panic!(
            "a user `Vec` whose `Drop` panics must be refused as `Incomplete` \
             (the destructor's `panic!` is not modelled as a value), got: {other} -- \
             a `verified` here is a false proof and a `Failed` would be a spurious \
             refutation of `ensures: true`"
        ),
    }

    // The loop twin must be *refuted* on its postcondition, not merely refused:
    // a refusal also satisfies "not verified" and would check nothing.
    let Some(o) = verify_src("loop_push_cannot_prove_this", src) else { return };
    match o {
        FnOutcome::Failed { kind, .. } => assert!(
            kind.contains("postcondition"),
            "the loop twin must fail on its postcondition; got `{kind}`"
        ),
        other => panic!(
            "the loop entry length must be havoced, so `ret == 0` for a body \
             returning 3 must be refuted; got: {other}"
        ),
    }
    // The control: havocing the length must not break a loop whose postcondition
    // is about a variable the invariant *does* relate.
    let Some(o) = verify_src("loop_counter_ok", src) else { return };
    assert!(
        o.is_verified(),
        "a `Vec`-carrying loop with a counter postcondition must still verify; got: {o}"
    );

    // The move: the true direction verifies, the false twin is refuted.
    let Some(o) = verify_src("move_ok", src) else { return };
    assert!(o.is_verified(), "a moved vector keeps its length; got: {o}");
    let Some(o) = verify_src("move_bad", src) else { return };
    assert!(
        matches!(o, FnOutcome::Failed { .. }),
        "the move twin claims `0` for a vector of length 1 and must be refuted; got: {o}"
    );

    // The returned vector: the caller's destination inherits the length.
    let Some(o) = verify_src("returned_ok", src) else { return };
    assert!(o.is_verified(), "a returned vector keeps its length; got: {o}");
}

/// A user type spelled `Vec` is not the container, and its *methods* are answered
/// by their own bodies.
///
/// `names_vec_impl` accepts a call whose path contains `vec::Vec` or starts with a
/// bare `Vec::` -- the shortened spelling rustc prints for the *standard*
/// container when nothing shadows it. A user `Vec` also prints `Vec::<u8>::new`
/// at the crate root, so the predicate alone cannot separate them. What does is
/// the executor's same-dump lookup, which resolves the user body *before* the
/// model: `body_wins` returns 7 from its own `len` and verifies against `ret == 7`,
/// while the twin claiming the container model's `0` is refuted. This pins that
/// the model is not consulted for a call that names a body in the dump.
#[test]
fn a_user_vec_method_is_answered_by_its_own_body() {
    let src = r#"macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }

pub struct Vec<T>(pub T);
impl Vec<u8> {
    pub fn new() -> Self { Vec(0) }
    pub fn len(&self) -> usize { 7 }
}

spec! {
    requires: true,
    ensures: ret == 7,
}
/// The user body returns 7, so this verifies -- the model's `0` would refute it.
pub fn body_wins() -> usize { let v = Vec::new(); v.len() }

spec! {
    requires: true,
    ensures: ret == 0,
}
/// The twin: the container model would answer `0` and prove this, so a refutation
/// is the evidence the user body was used.
pub fn model_would_say_zero() -> usize { let v = Vec::new(); v.len() }
"#;
    let Some(o) = verify_src("body_wins", src) else { return };
    assert!(
        o.is_verified(),
        "a user `Vec` method must be answered by its own body; got: {o}"
    );
    let Some(o) = verify_src("model_would_say_zero", src) else { return };
    assert!(
        matches!(o, FnOutcome::Failed { .. }),
        "the container model must not answer a user `Vec` call, so `ret == 0` must be \
         refuted; got: {o}"
    );
}

/// A `&str` argument's length is a symbolic input, exactly as a `&[T]` slice's is.
///
/// `str` is unsized, so its length lives in the fat pointer and is a caller
/// choice, not a constant in the type. `s.len()`/`s.is_empty()` read only that
/// length, so they are modelled the same way the slice ones are -- the same two
/// intrinsics, a different path (`core::str::<impl str>::len`). The length is
/// bound by the contract binder, so a body's `s.len()` and a contract's `s.len()`
/// name one variable: a `requires: s.len() == 3` pins the body's read, and the
/// false twin is refuted.
///
/// `String` is deliberately *not* covered: it is a different type
/// (`alloc::string::String`), so `String::len` stays an honest refusal rather
/// than being answered with a length the model does not hold.
#[test]
fn a_str_length_is_a_symbolic_input() {
    let src = r#"macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }

spec! {
    requires: true,
    ensures: ret <= s.len(),
}
pub fn len_bound(s: &str) -> usize { s.len() }

spec! {
    requires: s.len() == 3,
    ensures: ret == 3,
}
/// The length is pinned by the precondition, so the body's read is that value.
pub fn pinned_len(s: &str) -> usize { s.len() }

spec! {
    requires: true,
    ensures: ret == 4,
}
/// The false twin: an unbounded `&str` length is not 4.
pub fn wrong_len_cannot_prove_this(s: &str) -> usize { s.len() }

spec! {
    requires: s.len() == 0,
    ensures: ret,
}
/// `is_empty` reads the same variable, so it agrees with `len`.
pub fn empty_is_empty(s: &str) -> bool { s.is_empty() }
"#;
    for name in ["len_bound", "pinned_len", "empty_is_empty"] {
        let Some(o) = verify_src(name, src) else { return };
        assert!(o.is_verified(), "`{name}` must verify; got: {o}");
    }
    let Some(o) = verify_src("wrong_len_cannot_prove_this", src) else { return };
    assert!(
        matches!(o, FnOutcome::Failed { .. }),
        "an unbounded `&str` length must not be provable as `4`; got: {o}"
    );
}

/// `Option::unwrap_or_default` is total, and its default is the payload's own zero.
///
/// The default of every scalar `Default` this tool models is the zero of the
/// payload's width (`0u8`, `0i32`, `false`), so the failure variant yields that
/// word rather than a fresh input. The `x.is_none()` pin makes the body's read the
/// default on every path, and the false twin (`ret == 7`) is refuted because the
/// `Some` path can carry any payload.
#[test]
fn option_unwrap_or_default_is_the_payload_or_zero() {
    let src = r#"macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }

spec! {
    requires: true,
    ensures: ret <= 255,
}
pub fn or_zero(x: Option<u8>) -> u8 { x.unwrap_or_default() }

spec! {
    requires: x.is_none(),
    ensures: ret == 0,
}
pub fn none_is_zero(x: Option<u8>) -> u8 { x.unwrap_or_default() }

spec! {
    requires: true,
    ensures: ret == 7,
}
pub fn cannot_prove_this(x: Option<u8>) -> u8 { x.unwrap_or_default() }
"#;
    for name in ["or_zero", "none_is_zero"] {
        let Some(o) = verify_src(name, src) else { return };
        assert!(o.is_verified(), "`{name}` must verify; got: {o}");
    }
    let Some(o) = verify_src("cannot_prove_this", src) else { return };
    assert!(
        matches!(o, FnOutcome::Failed { .. }),
        "`unwrap_or_default` must not be provable as a fixed `7`; got: {o}"
    );
}

/// A user type named `str` is not the primitive `str`.
///
/// Rust lets a crate shadow a primitive name, and rustc prints a crate-root type
/// as the bare name, so a `pub struct str { .. }` argument is spelled `&str`
/// exactly as the primitive is. Binding the primitive's symbolic length *before*
/// consulting the declared layout therefore bound a field-less type over the
/// user's struct, and a contract naming its field could not resolve. The declared
/// layout now wins; the primitive is meant only when nothing declares `str`.
///
/// The primitive direction is asserted too, so a fix that simply refused every
/// `str` argument would fail this test.
#[test]
fn a_user_type_named_str_is_not_the_primitive_str() {
    let src = r#"macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }

pub struct str { pub value: u8 }

spec! {
    requires: s.value == 3,
    ensures: ret == 3,
}
pub fn read(s: &str) -> u8 { s.value }

spec! {
    requires: true,
    ensures: ret == 3,
}
pub fn read_cannot_prove_this(s: &str) -> u8 { s.value }
"#;
    let Some(o) = verify_src("read", src) else { return };
    assert!(
        o.is_verified(),
        "a field of a user `str` must resolve; got: {o}"
    );
    let Some(o) = verify_src("read_cannot_prove_this", src) else { return };
    assert!(
        matches!(o, FnOutcome::Failed { .. }),
        "the user `str`'s field is an input, so a fixed value must be refuted; got: {o}"
    );

    // The primitive is still modelled when nothing declares `str`.
    let prim = r#"macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }

spec! {
    requires: s.len() == 3,
    ensures: ret == 3,
}
pub fn len_is_symbolic(s: &str) -> usize { s.len() }

spec! {
    requires: true,
    ensures: ret == 4,
}
pub fn wrong_len_cannot_prove_this(s: &str) -> usize { s.len() }
"#;
    let Some(o) = verify_src("len_is_symbolic", prim) else { return };
    assert!(o.is_verified(), "the primitive `str` length must resolve; got: {o}");
    let Some(o) = verify_src("wrong_len_cannot_prove_this", prim) else { return };
    assert!(
        matches!(o, FnOutcome::Failed { .. }),
        "an unpinned `str` length must not be provable as `4`; got: {o}"
    );
}

/// A `&mut [u8]` slice write must reach the store the read consults.
///
/// This is the gap the `&raw const (fake) (*_1)` parse closed. Before it, the
/// bounds check on a `&mut [u8]` argument read its length through a raw reborrow
/// the parser did not recognise, so the function was `incomplete` before any
/// write was reached -- the write was never even evaluated. Now the true half
/// must *verify* (a write then a read of the same index returns what was
/// written) and the tripwire -- the same body claiming the pre-write value --
/// must be refuted, since a model that dropped the write would leave the seeded
/// element readable and prove it.
#[test]
fn a_slice_element_write_reaches_the_store_the_read_consults() {
    let src = r#"macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }

spec! {
    requires: s.len() > 0,
    ensures: ret == v,
}
pub fn slice_written_then_read(s: &mut [u8], v: u8) -> u8 {
    s[0] = v;
    s[0]
}

spec! {
    requires: s.len() > 0,
    ensures: ret == s[0],
}
pub fn slice_written_then_read_tripwire(s: &mut [u8], v: u8) -> u8 {
    s[0] = v;
    s[0]
}
"#;
    let Some(o) = verify_src("slice_written_then_read", src) else { return };
    assert!(
        o.is_verified(),
        "a write then a read of the same slice index returns what was written; got: {o}"
    );
    let Some(o) = verify_src("slice_written_then_read_tripwire", src) else { return };
    assert!(
        matches!(o, FnOutcome::Failed { .. }),
        "the pre-write element must not stay readable after a slice write; got: {o}"
    );
}

/// The `&mut [u8]` *symbolic* read must see the slice write too.
///
/// The array form of this shape is [`an_element_write_through_a_mutable_reference_reaches_the_chain`];
/// the slice form differs in the bounds check, not the store, so the same
/// requirement holds -- the false contract must be refuted rather than left
/// undecided, which is what a missing `PtrMetadata` would have made it.
#[test]
fn a_slice_element_write_reaches_the_symbolic_chain() {
    let src = r#"macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }

spec! {
    requires: i < s.len() && s.len() <= 64 && i == 0,
    ensures: ret == s[i],
}
pub fn slice_written_then_symbolic(s: &mut [u8], i: usize) -> u8 {
    s[0] = 0;
    s[i]
}
"#;
    let Some(o) = verify_src("slice_written_then_symbolic", src) else { return };
    assert!(
        matches!(o, FnOutcome::Failed { .. }),
        "a slice write must not leave a stale element readable by the chain; got: {o}"
    );
}

/// `slice::first`/`get` produce `Option<&T>`, and the payload is the slice's
/// *own* element, so `*xs.first().unwrap()` and `xs[0]` agree -- and a claim
/// about a different element is refuted rather than proved.
///
/// The tag is a predicate (`len == 0` for `first`, `len <= k` for `get`), so the
/// `unwrap`'s panic obligation is real: `get(1)` past the end must fail, not be
/// assumed `Some`. Both directions are pinned, because a model that answered
/// every `first` with the element would prove the true contracts and the false
/// ones alike.
#[test]
fn slice_first_and_get_reach_the_slices_own_element() {
    let src = r#"macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }

spec! {
    requires: xs.len() > 0 && xs[0] == 5,
    ensures: ret == 5,
}
pub fn first_reads_the_element(xs: &[u8]) -> u8 {
    *xs.first().unwrap()
}

spec! {
    requires: xs.len() > 0 && xs[0] == 5,
    ensures: ret == 6,
}
pub fn first_wrong_element_cannot_prove_this(xs: &[u8]) -> u8 {
    *xs.first().unwrap()
}

spec! {
    requires: xs.len() == 0,
    ensures: ret,
}
pub fn first_is_none_when_empty(xs: &[u8]) -> bool {
    xs.first().is_none()
}

spec! {
    requires: xs.len() > 1 && xs[1] == 7,
    ensures: ret == 7,
}
pub fn get_reads_the_indexed_element(xs: &[u8]) -> u8 {
    *xs.get(1).unwrap()
}

spec! {
    requires: xs.len() > 1 && xs[1] == 7,
    ensures: ret == 8,
}
pub fn get_wrong_element_cannot_prove_this(xs: &[u8]) -> u8 {
    *xs.get(1).unwrap()
}

spec! {
    requires: xs.len() > 0,
    ensures: ret == 7,
}
pub fn get_out_of_range_unwrap_panics(xs: &[u8]) -> u8 {
    *xs.get(1).unwrap()
}

spec! {
    requires: xs.len() > 0 && xs[0] == 5,
    ensures: ret == 5,
}
pub fn first_match_reads_the_element(xs: &[u8]) -> u8 {
    match xs.first() { Some(v) => *v, None => 0 }
}

spec! {
    requires: xs.len() > 0 && xs[0] == 5,
    ensures: ret == 6,
}
pub fn first_match_tripwire(xs: &[u8]) -> u8 {
    match xs.first() { Some(v) => *v, None => 0 }
}

spec! {
    requires: xs.len() == 0,
    ensures: ret == 0,
}
pub fn first_match_empty_takes_none(xs: &[u8]) -> u8 {
    match xs.first() { Some(v) => *v, None => 0 }
}

spec! {
    requires: i < xs.len() && xs.len() <= 64,
    ensures: true,
}
pub fn get_symbolic_index_is_refused(xs: &[u8], i: usize) -> u8 {
    *xs.get(i).unwrap()
}
"#;
    let Some(o) = verify_src("first_reads_the_element", src) else { return };
    assert!(
        o.is_verified(),
        "`*xs.first().unwrap()` is `xs[0]`, so the true contract must verify; got: {o}"
    );
    let Some(o) = verify_src("first_wrong_element_cannot_prove_this", src) else { return };
    assert!(
        matches!(o, FnOutcome::Failed { .. }),
        "the payload is the slice's own element, so a claim about a different value \
         must be refuted, not proved; got: {o}"
    );
    let Some(o) = verify_src("first_is_none_when_empty", src) else { return };
    assert!(
        o.is_verified(),
        "`first` is `None` exactly when the slice is empty; got: {o}"
    );
    let Some(o) = verify_src("get_reads_the_indexed_element", src) else { return };
    assert!(
        o.is_verified(),
        "`*xs.get(1).unwrap()` is `xs[1]`, so the true contract must verify; got: {o}"
    );
    let Some(o) = verify_src("get_wrong_element_cannot_prove_this", src) else { return };
    assert!(
        matches!(o, FnOutcome::Failed { .. }),
        "`get`'s payload is the indexed element, so a claim about another value must \
         be refuted; got: {o}"
    );
    let Some(o) = verify_src("get_out_of_range_unwrap_panics", src) else { return };
    assert!(
        matches!(&o, FnOutcome::Failed { kind, .. } if kind.starts_with("panic")),
        "`get(1)` past the end is `None`, so `unwrap` panics -- a real obligation, not \
         an assumed `Some`; got: {o}"
    );
    // The `match` form reads the payload through a downcast temp, a different
    // desugaring from `unwrap`; both must decide, and a `match` on the empty
    // slice must take the `None` arm.
    let Some(o) = verify_src("first_match_reads_the_element", src) else { return };
    assert!(
        o.is_verified(),
        "a `match` on `first()` names the same element `unwrap` does; got: {o}"
    );
    let Some(o) = verify_src("first_match_tripwire", src) else { return };
    assert!(
        matches!(o, FnOutcome::Failed { .. }),
        "the `match` payload is the slice's element, so a claim about another value \
         must be refuted; got: {o}"
    );
    let Some(o) = verify_src("first_match_empty_takes_none", src) else { return };
    assert!(
        o.is_verified(),
        "an empty slice takes the `None` arm, which returns the constant; got: {o}"
    );
    // A symbolic index has no finite domain on a slice, so the read is refused
    // rather than guessed -- a wrong value would be worse than a missing one.
    let Some(o) = verify_src("get_symbolic_index_is_refused", src) else { return };
    assert!(
        !o.is_verified()
            && matches!(&o, FnOutcome::Incomplete { .. } | FnOutcome::Skipped { .. }),
        "`xs.get(i)` for a symbolic `i` must be refused, not modelled; got: {o}"
    );
}

/// A generic `&[T]` has a *length* even though its element width is unknown.
///
/// `T` is an unbound type parameter, so there is no element width to model and
/// every element operation must stay refused. But `len`/`is_empty` read only the
/// length, which is a `usize` input whatever `T` is — so the length is bound and
/// those functions decide. This pins both halves: the length functions verify, and
/// an element-naming contract is a refusal, never a proof. The two tripwires are
/// the teeth: `generic_slice_len_is_zero_cannot_prove_this` must be *refuted*
/// (the length is a real value, not a constant `0`), and
/// `generic_slice_element_cannot_prove_this` must not be *verified* (the element
/// has no width, so a claim about it cannot be discharged).
#[test]
fn a_generic_slices_length_is_modelled_but_its_element_is_not() {
    let src = r#"macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }

spec! {
    requires: true,
    ensures: ret == v.len(),
}
pub fn len_of<T: Copy>(v: &[T]) -> usize {
    v.len()
}

spec! {
    requires: true,
    ensures: ret == (v.len() == 0),
}
pub fn empty_of<T: Copy>(v: &[T]) -> bool {
    v.is_empty()
}

spec! {
    requires: true,
    ensures: true,
}
pub fn first_or_of<T: Copy>(v: &[T], d: T) -> T {
    if v.is_empty() { d } else { v[0] }
}

spec! {
    requires: true,
    ensures: ret == 0,
}
pub fn len_is_zero_cannot_prove_this<T: Copy>(v: &[T]) -> usize {
    v.len()
}

spec! {
    requires: true,
    ensures: ret == d,
}
pub fn element_cannot_prove_this<T: Copy>(v: &[T], d: T) -> T {
    if v.is_empty() { d } else { v[0] }
}
"#;
    let Some(o) = verify_src("len_of", src) else { return };
    assert!(
        o.is_verified(),
        "`v.len()` on a `&[T]` reads only the length, which is modelled; got: {o}"
    );
    let Some(o) = verify_src("empty_of", src) else { return };
    assert!(
        o.is_verified(),
        "`v.is_empty()` is `len == 0`, decided by the length alone; got: {o}"
    );
    let Some(o) = verify_src("first_or_of", src) else { return };
    assert!(
        o.is_verified(),
        "`first_or` is guarded by `is_empty`, which the length decides, so the \
         bounds check on `v[0]` is discharged; got: {o}"
    );
    // The teeth: a false length claim is refuted (the length is a real input),
    // and an element claim is not proved (the element has no width).
    let Some(o) = verify_src("len_is_zero_cannot_prove_this", src) else { return };
    assert!(
        matches!(o, FnOutcome::Failed { .. }),
        "`v.len() == 0` is false for a non-empty slice, so this must be refuted \
         (a verified verdict would mean the length was folded to the constant 0); \
         got: {o}"
    );
    let Some(o) = verify_src("element_cannot_prove_this", src) else { return };
    assert!(
        !o.is_verified(),
        "a generic slice's element has no width, so `ret == d` must never be \
         reported verified; got: {o}"
    );
}

/// A whole-struct reassignment must invalidate the fields bound from it.
///
/// `c = d; c.lo` returns `d.lo`, so `ret == c.lo` is false unless `c.lo == d.lo`.
/// The field bindings are keyed by local and a plain `locals` write does not
/// touch them, so if the assignment left them the contract's `c.lo` would still
/// name the argument's field and this false contract would verify. It must be
/// refused or refuted -- anything but `Verified`.
#[test]
fn a_whole_struct_assignment_invalidates_its_fields() {
    let src = r#"macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }

pub struct B { pub lo: u8 }

spec! {
    requires: true,
    ensures: ret == c.lo,
}
pub fn reassigned_then_field(mut c: B, d: B) -> u8 {
    c = d;
    c.lo
}
"#;
    let Some(o) = verify_src("reassigned_then_field", src) else { return };
    assert!(
        !o.is_verified(),
        "a reassigned struct's field binding is stale and must not prove a contract; got: {o}"
    );
}

/// A one-line struct body must not swallow the functions that follow it.
///
/// `struct_head` accepted any line containing `{` and opened a multi-line field
/// cursor, so `pub struct B { pub lo: u8 }` left the cursor open with no closing
/// brace left to read: every following line -- including the `///` contracts and
/// the functions -- was consumed as struct fields. The functions then vanished
/// from the spec table and were silently *not checked at all*, which is worse
/// than a wrong verdict because nothing reports it. The test above used a one-line
/// struct and was therefore passing vacuously.
///
/// The struct path now mirrors the enum path: a body that closes on the head line
/// is read inline, and a cursor is opened only for a body that does not. This
/// pins the function after the struct as still present and still decided.
///
/// The `None` case is a *skip*, not a failure, like every other `rustc`-dependent
/// test in this file: `verify_src` returns `None` when no `VERIF_RUSTC` is
/// named, and there is nothing to assert about a compiler that is not there. This
/// one used to `panic!` on `None`, which made the whole suite fail on a machine
/// with no `VERIF_RUSTC` -- the one test in the file that read "no compiler"
/// as "a bug", so the suite's verdict depended on the environment rather than on
/// the code. It is a skip now, and the reason is worth keeping: the property is
/// real and worth pinning, but a missing toolchain is not evidence against it.
#[test]
fn a_one_line_struct_does_not_swallow_the_functions_after_it() {
    let src = r#"macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }

pub struct B { pub lo: u8 }

spec! {
    requires: true,
    ensures: ret == 0,
}
pub fn after_inline_struct(x: u8) -> u8 {
    0
}
"#;
    let Some(o) = verify_src("after_inline_struct", src) else { return };
    assert!(o.is_verified(), "the function must still be read and decided; got: {o}");
}

/// A generated binding name must not be able to shadow a real argument.
///
/// The contract's `a[i]` is rewritten to a binding named `a__chain`, and this
/// function has an argument of exactly that name. If the generated binding were
/// inserted unconditionally it would shadow the argument, so `ret == a__chain`
/// would be checked against the array's chain instead of the argument. With
/// `i == 0` and `a__chain == 0` but `a[0] != 0` the contract is false, so
/// `Verified` would be a false proof. Refusing the binding is the fix; this pins
/// it.
#[test]
fn a_generated_binding_cannot_shadow_an_argument() {
    let src = r#"macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }

spec! {
    requires: i < 4,
    ensures: ret == a__chain,
}
pub fn shadow_probe(a: [u8; 4], i: usize, a__chain: u8) -> u8 {
    a[i]
}
"#;
    let Some(o) = verify_src("shadow_probe", src) else { return };
    assert!(
        !o.is_verified(),
        "`a__chain` is a real argument and must not be replaced by the generated binding; got: {o}"
    );
}

/// A `__len` collision is refused the same way, and the array's length binding is
/// what is dropped rather than the argument.
#[test]
fn a_generated_length_binding_cannot_shadow_an_argument() {
    let src = r#"macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }

spec! {
    requires: a.len() == 4,
    ensures: ret == a__len,
}
pub fn shadow_len_probe(a: [u8; 4], a__len: usize) -> usize {
    a.len()
}
"#;
    let Some(o) = verify_src("shadow_len_probe", src) else { return };
    assert!(
        !o.is_verified(),
        "`a__len` is a real argument and must not be replaced by the generated length; got: {o}"
    );
}

#[test]
fn a_false_symbolic_index_postcondition_is_caught() {
    let src = r#"macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }

spec! {
    requires: i < 4,
    ensures: ret <= 9,
}
pub fn chain_must_be_read(a: [u8; 4], i: usize) -> u8 {
    a[i]
}
"#;
    let Some(rustc) = rustc() else { return };
    let dir = std::env::temp_dir().join(format!("verif-chain-{}", std::process::id()));
    let _ = std::fs::create_dir_all(&dir);
    let file = dir.join("chain.rs");
    std::fs::write(&file, src).expect("write probe");
    let report = verify::verify_file_with_opts(&rustc, &file, &[], &probe_opts()).expect("frontend");
    let o = outcome(&report, "chain_must_be_read");
    match o {
        FnOutcome::Failed { detail, .. } => assert!(
            !detail.is_empty(),
            "a failed symbolic-index obligation must carry a counterexample"
        ),
        other => panic!("`chain_must_be_read` must FAIL (ret <= 9 is false), got: {other}"),
    }
    let _ = std::fs::remove_dir_all(&dir);
}

/// Stage 2, enums: a `match` is a `switchInt` on a symbolic tag.
///
/// A `match` lowers to `_2 = discriminant(_1); switchInt(move _2) -> [...]`, so
/// the tag is an input. Before this it was read as unknown and every `match` was
/// `incomplete` with `unmodelled switch discriminant`. The tag's domain is the
/// enum's arity, which is a fact about the *type*, so assuming it is sound and is
/// what makes the `otherwise` arm -- lowered to `unreachable` -- genuinely
/// unreachable rather than a spurious panic.
///
/// The tripwire is what makes this evidence rather than a claim: a constant tag
/// (or one read as zero) would prove `enum_match_tripwire`, which is false.
#[test]
fn an_enum_match_is_a_switch_on_a_symbolic_tag() {
    let Some(report) = verify_realistic(&["enum_match_is_bounded"]) else { return };
    let o = outcome(&report, "enum_match_is_bounded");
    assert!(o.is_verified(), "`enum_match_is_bounded` must verify, got: {o}");
}

/// A variant payload is read through the downcast and is a symbolic input.
///
/// `Msg::Data(x)` lowers to `_3 = copy ((_1 as Data).0)`, so the payload is field
/// 0 of variant 1. The contract's `m.Data` is bound to the same variable the
/// downcast reads, so `ret == m.Data` on that arm is a claim about one value.
#[test]
fn an_enum_payload_is_read_through_the_downcast() {
    let Some(report) = verify_realistic(&["enum_payload_reads_through"]) else { return };
    let o = outcome(&report, "enum_payload_reads_through");
    assert!(o.is_verified(), "`enum_payload_reads_through` must verify, got: {o}");
}

/// A `match` arm *guard* is decided by the payload its borrow of a downcast reads.
///
/// `Msg::Data(n) if n < 10` lowers to `_4 = &((_1 as Data).0: u8); _6 = copy
/// (*_4)`, and the rvalue contains the word ` as ` inside its parentheses. A
/// parser that checked for a cast before a reference split that at the ` as `, so
/// the borrow got no address, `copy (*_4)` read `Unknown`, and the `switchInt` on
/// the guard reported "unmodelled switch discriminant". The true contract must
/// verify and the tripwire -- which claims the guard's *other* arm ran -- must be
/// refuted with a witness naming the payload.
#[test]
fn a_match_guard_reads_its_payload_through_a_borrow() {
    let Some(report) = verify_realistic(&["guarded_match_reads_its_payload"]) else { return };
    let o = outcome(&report, "guarded_match_reads_its_payload");
    assert!(o.is_verified(), "`guarded_match_reads_its_payload` must verify, got: {o}");

    let Some(report) = verify_realistic(&["guarded_match_tripwire"]) else { return };
    let t = outcome(&report, "guarded_match_tripwire");
    match t {
        FnOutcome::Failed { kind, .. } => assert_eq!(kind, "postcondition"),
        other => panic!("`guarded_match_tripwire` must be refuted, got: {other}"),
    }
}

/// A locally constructed variant has a *known* tag, so its `match` decides.
///
/// `let m = Mode::Idle;` lowers to `_1 = Mode::Idle;`, a bare path constant with
/// no `const ` prefix, and `Mode::Idle` resolves to the enum's declaration index.
/// The tag is therefore a constant, only the `Idle` arm is reachable, and the
/// contract is decided rather than left as a coverage gap. The companion
/// tripwire claims the `Off` arm ran, which the same model must refute.
#[test]
fn a_locally_constructed_variant_has_a_known_tag() {
    let Some(report) = verify_realistic(&["enum_constructed_locally"]) else { return };
    let o = outcome(&report, "enum_constructed_locally");
    assert!(o.is_verified(), "`enum_constructed_locally` must verify, got: {o}");
}

/// A constructed variant's payload is written where the downcast reads it.
///
/// `Msg::Data(9)` lowers to `_1 = Msg::Data(const 9_u8);`, so the tag is `1` and
/// the payload is field 0 of that variant. The match's `Data(x)` arm reads
/// `((_1 as Data).0: u8)`, which is the same field, so `ret == 9` is a claim
/// about the value the construction wrote -- not about a fresh input.
#[test]
fn a_constructed_variants_payload_is_read_through_the_downcast() {
    let Some(report) = verify_realistic(&["enum_constructed_payload_reads_through"]) else { return };
    let o = outcome(&report, "enum_constructed_payload_reads_through");
    assert!(o.is_verified(), "`enum_constructed_payload_reads_through` must verify, got: {o}");
}

/// A unit variant has no payload, so naming one is refused rather than bound.
///
/// `m.Ping` is a unit variant. There is no value to read, so the contract must be
/// refused -- a model that bound every variant to the same payload would prove
/// this false contract. The refusal is `Skipped`, which is reported, not a
/// silent pass.
#[test]
fn a_unit_variant_has_no_payload_to_name() {
    let Some(report) = verify_realistic(&["enum_payload_tripwire"]) else { return };
    let o = outcome(&report, "enum_payload_tripwire");
    assert!(
        !o.is_verified(),
        "`enum_payload_tripwire` names a unit variant's payload and must be refused, got: {o}"
    );
}

/// `count_ones` is modelled, and its result is *widened* to the return type.
///
/// The operand is `u8` and the result is `u32`, so this is the one modelled call
/// whose result width differs from its operand's. A model that built an 8-bit
/// term and wrote it to a `u32` local would be ill-formed, and a contract at the
/// top of the count's range would not decide. Both entries must verify.
#[test]
fn count_ones_is_modelled_and_widens_its_result() {
    let Some(report) = verify_realistic(&[
        "popcount_of_nonzero",
        "popcount_bounded_by_width",
    ]) else {
        return;
    };
    for name in ["popcount_of_nonzero", "popcount_bounded_by_width"] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify, got: {o}");
    }
}

/// The tripwire: if `count_ones` were left unmodelled, both entries would be
/// `Skipped` rather than decided, and if it were modelled *wrongly* the false
/// claims would pass. A failure on the tripwires is the evidence the model was
/// contradicted; a decision at all is the evidence it is modelled.
#[test]
fn the_count_ones_tripwires_are_refuted_not_skipped() {
    let Some(report) = verify_realistic(&[
        "popcount_of_nonzero_tripwire",
        "popcount_bounded_by_width_tripwire",
    ]) else {
        return;
    };
    for name in ["popcount_of_nonzero_tripwire", "popcount_bounded_by_width_tripwire"] {
        let o = outcome(&report, name);
        let FnOutcome::Failed { kind, .. } = o else {
            panic!("`{name}` must FAIL: a `Skipped` means `count_ones` is not modelled, got: {o}");
        };
        assert_eq!(kind, "postcondition", "`{name}` got: {o}");
    }
}

/// `wrapping_abs` is modelled, unary, and reads its operand's sign.
///
/// The verifying entry is `ret >= x`, which holds at every `i8` including `MIN`
/// (where `abs` wraps back to `MIN`). The tripwire is `ret > x`, false at `x == 0`
/// where `abs` is the identity. Both must be *decided*: a `Skipped` would mean the
/// unary shape is not modelled at all, which is the gap this closed.
#[test]
fn wrapping_abs_is_modelled_unary_and_signed() {
    let Some(report) = verify_realistic(&["abs_never_below_operand"]) else { return };
    let o = outcome(&report, "abs_never_below_operand");
    assert!(o.is_verified(), "`abs_never_below_operand` must verify, got: {o}");

    let Some(report) = verify_realistic(&["abs_never_below_operand_tripwire"]) else { return };
    let o = outcome(&report, "abs_never_below_operand_tripwire");
    let FnOutcome::Failed { kind, detail } = o else {
        panic!("`abs_never_below_operand_tripwire` must FAIL, got: {o}");
    };
    assert_eq!(kind, "postcondition", "got: {o}");
    // The counterexample must be `x == 0`, the one point where `abs` is the
    // identity: if the model were wrong on the negative side the witness would
    // name a negative value instead.
    assert!(
        detail.contains("= 0"),
        "the witness should be x == 0 (where abs is the identity), got: {detail}"
    );
}

/// The zero-counts beside `count_ones` are modelled, and `leading_zeros` of a
/// zero operand is the *full* width.
///
/// `leading_zeros` is the shape where an off-by-one hides: every non-zero operand
/// has at most `w - 1` leading zeros, so a model that counted only those would
/// agree with `core` everywhere except `x == 0`, and that is exactly the input the
/// tripwire names. Both entries must be *decided* -- a `Skipped` would mean the
/// name is not modelled at all.
#[test]
fn the_zero_counts_are_modelled() {
    let Some(report) = verify_realistic(&[
        "leading_zeros_of_a_small_value",
        "trailing_zeros_of_a_top_bit_value",
    ]) else {
        return;
    };
    for name in ["leading_zeros_of_a_small_value", "trailing_zeros_of_a_top_bit_value"] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify, got: {o}");
    }

    let Some(report) = verify_realistic(&[
        "leading_zeros_of_a_small_value_tripwire",
        "trailing_zeros_of_a_top_bit_value_tripwire",
    ]) else {
        return;
    };
    for name in [
        "leading_zeros_of_a_small_value_tripwire",
        "trailing_zeros_of_a_top_bit_value_tripwire",
    ] {
        let o = outcome(&report, name);
        let FnOutcome::Failed { kind, .. } = o else {
            panic!("`{name}` must FAIL: a `Skipped` means the name is not modelled, got: {o}");
        };
        assert_eq!(kind, "postcondition", "`{name}` got: {o}");
    }
    // The `leading_zeros` witness must be `x == 0`, the only input where the count
    // is the full width. A model that counted `w - 1` there would name a non-zero
    // witness instead.
    let o = outcome(&report, "leading_zeros_of_a_small_value_tripwire");
    let FnOutcome::Failed { detail, .. } = o else { panic!("got: {o}") };
    assert!(
        detail.contains("= 0"),
        "the `leading_zeros` witness should be x == 0, got: {detail}"
    );
}

/// The four `pow` families are modelled, and the exponent is a `u32` *beside* a
/// base of the method's own width.
///
/// `pow` is the first modelled call whose two operands have different types. The
/// verifying entries pin the fold where the base's width matters (`2^7 == 128`
/// fits a `u8`, `2^8` wraps), and each tripwire states a *neighbouring* value --
/// one step off the true result -- so a model that folded at the wrong width, or
/// stopped a step early, refutes it. The signed entries pin the *clamp
/// direction*: `saturating_pow` of a negative base with an odd exponent clamps to
/// `MIN`, and a "clamp everything to `MAX`" model proves the `MAX` tripwire
/// instead of refuting it.
///
/// Every tripwire must be `Failed`, not `Skipped`: a `Skipped` would mean the
/// name is not modelled at all.
#[test]
fn the_pow_families_are_modelled_with_a_wide_exponent() {
    let Some(report) = verify_realistic(&[
        "wrapping_pow_two_to_seven_is_128",
        "checked_pow_two_to_seven_is_some_128",
        "saturating_pow_three_to_five_is_243",
        "overflowing_pow_two_to_seven_did_not_overflow",
        "wrapping_pow_negative_two_to_seven_is_min",
        "saturating_pow_negative_base_clamps_to_min",
        "wrapping_pow_zero_exponent_is_one",
    ]) else {
        return;
    };
    for name in [
        "wrapping_pow_two_to_seven_is_128",
        "checked_pow_two_to_seven_is_some_128",
        "saturating_pow_three_to_five_is_243",
        "overflowing_pow_two_to_seven_did_not_overflow",
        "wrapping_pow_negative_two_to_seven_is_min",
        "saturating_pow_negative_base_clamps_to_min",
        "wrapping_pow_zero_exponent_is_one",
    ] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify, got: {o}");
    }

    let Some(report) = verify_realistic(&[
        "wrapping_pow_two_to_eight_is_256_tripwire",
        "checked_pow_two_to_eight_is_none_tripwire",
        "saturating_pow_three_to_six_clamps_tripwire",
        "overflowing_pow_two_to_eight_flag_tripwire",
        "wrapping_pow_negative_two_to_eight_wraps_tripwire",
        "saturating_pow_negative_base_to_max_tripwire",
        "wrapping_pow_one_exponent_is_one_tripwire",
    ]) else {
        return;
    };
    for name in [
        "wrapping_pow_two_to_eight_is_256_tripwire",
        "checked_pow_two_to_eight_is_none_tripwire",
        "saturating_pow_three_to_six_clamps_tripwire",
        "overflowing_pow_two_to_eight_flag_tripwire",
        "wrapping_pow_negative_two_to_eight_wraps_tripwire",
        "saturating_pow_negative_base_to_max_tripwire",
        "wrapping_pow_one_exponent_is_one_tripwire",
    ] {
        let o = outcome(&report, name);
        let FnOutcome::Failed { kind, .. } = o else {
            panic!("`{name}` must FAIL: a `Skipped` means the name is not modelled, got: {o}");
        };
        assert_eq!(kind, "postcondition", "`{name}` got: {o}");
    }
}

/// The plain inherent `pow` is modelled, and its failure is a *panic* obligation
/// rather than a wrapped value.
///
/// This is the polarity the shape exists to pin: the families' `pow` expresses
/// overflow as a value (wrap, clamp, `None`, flag), but the plain `pow` panics, so
/// a model that treated it as total would discharge `plain_pow_panic_is_reachable`
/// as panic-free -- a false proof that an overflowing `pow` is safe. The two
/// verifying entries are the in-range case and the exact-boundary case; the two
/// failing entries must fail on the *panic* obligation, not on a postcondition, so
/// a model that wrapped instead of panicking is caught by the kind rather than by
/// a value that happens to differ.
#[test]
fn the_plain_pow_panics_where_it_overflows() {
    let Some(report) = verify_realistic(&[
        "plain_pow_three_to_five_is_243",
        "plain_pow_two_to_seven_does_not_panic",
    ]) else {
        return;
    };
    for name in ["plain_pow_three_to_five_is_243", "plain_pow_two_to_seven_does_not_panic"] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify, got: {o}");
    }

    let Some(report) = verify_realistic(&[
        "plain_pow_panic_is_reachable",
        "plain_pow_two_to_eight_would_panic_tripwire",
    ]) else {
        return;
    };
    for name in ["plain_pow_panic_is_reachable", "plain_pow_two_to_eight_would_panic_tripwire"] {
        let o = outcome(&report, name);
        let FnOutcome::Failed { kind, .. } = o else {
            panic!("`{name}` must FAIL on a panic, got: {o}");
        };
        // The panic must be *`pow`'s overflow*, not merely any panic: a model that
        // wrapped and then tripped a later shift, or a divide, would pass a bare
        // `starts_with("panic")`. The message names the method, so pin both.
        assert!(
            kind.starts_with("panic") && kind.contains("pow"),
            "`{name}` must fail on the `pow` overflow panic, got: {o}"
        );
    }
}

/// The four unary `neg`/`abs` families are modelled, and they disagree about
/// `MIN` -- which is what proves each is the right one and not a shared stand-in.
///
/// `saturating_neg` and `saturating_abs` clamp at `MIN`, so their results are
/// never negative; `wrapping_neg` and `wrapping_abs` return `MIN` there, which
/// *is* negative. The verifying entries state the clamp; the tripwires state the
/// same claim about the wrapping family, where `MIN` refutes it. If the four
/// shared one definition, either the clamps would fail or the tripwires would
/// pass, and both are caught here.
#[test]
fn the_unary_neg_and_abs_families_disagree_at_min() {
    let Some(report) = verify_realistic(&[
        "saturating_neg_of_positive_is_negative",
        "saturating_neg_of_non_positive_is_non_negative",
        "saturating_abs_is_never_negative",
        "saturating_abs_of_non_negative_is_identity",
    ]) else {
        return;
    };
    for name in [
        "saturating_neg_of_positive_is_negative",
        "saturating_neg_of_non_positive_is_non_negative",
        "saturating_abs_is_never_negative",
        "saturating_abs_of_non_negative_is_identity",
    ] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify, got: {o}");
    }

    let Some(report) = verify_realistic(&[
        "wrapping_neg_is_negative_tripwire",
        "wrapping_neg_of_non_positive_is_non_negative_tripwire",
        "wrapping_abs_is_never_negative_tripwire",
        "saturating_abs_is_identity_tripwire",
    ]) else {
        return;
    };
    for name in [
        "wrapping_neg_is_negative_tripwire",
        "wrapping_neg_of_non_positive_is_non_negative_tripwire",
        "wrapping_abs_is_never_negative_tripwire",
        "saturating_abs_is_identity_tripwire",
    ] {
        let o = outcome(&report, name);
        let FnOutcome::Failed { kind, .. } = o else {
            panic!("`{name}` must FAIL: a `Skipped` means the shape is not modelled, got: {o}");
        };
        assert_eq!(kind, "postcondition", "`{name}` got: {o}");
    }
    // The `abs`-at-`MIN` tripwire must name `x == 128`, which is `i8::MIN` read
    // unsigned -- the one input where the magnitude is unrepresentable. Naming
    // any other witness would mean the model disagrees with `core` elsewhere.
    let o = outcome(&report, "wrapping_abs_is_never_negative_tripwire");
    let FnOutcome::Failed { detail, .. } = o else { panic!("got: {o}") };
    assert!(
        detail.contains("= 128"),
        "the `wrapping_abs` witness should be x == i8::MIN (bits 128), got: {detail}"
    );
}

/// A `match` on a standard-library enum *argument* is modelled.
///
/// `Option` and `Result` are the two enums a real function reads without any
/// source file declaring them: `checked_add` returns `Option<u8>`, and `?` threads
/// a `Result`. An *argument* of one of those types had no layout, so its tag was
/// never seeded and the `switchInt` on it came back an unmodelled switch
/// discriminant -- a refusal, not a verdict. The declared-enum argument (`Mode`)
/// and the locally constructed `Option` both worked; the argument form is the gap.
///
/// Both halves matter: the true contracts must verify (the tag and the payload are
/// seeded) and the tripwires must be refuted (the tag and the payload are *read*,
/// not free variables the contract could make true). A model that seeded a
/// constant tag would take one arm always and prove a false tripwire.
#[test]
fn a_standard_enum_argument_is_a_switch_on_a_seeded_tag() {
    let Some(report) = verify_realistic(&[
        "option_arg_match",
        "option_arg_match_tripwire",
        "result_arg_match",
        "result_arg_match_tripwire",
        "option_arg_payload_passthrough",
        "option_arg_payload_tripwire",
    ]) else {
        return;
    };
    for name in ["option_arg_match", "result_arg_match", "option_arg_payload_passthrough"] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify, got: {o}");
    }
    for name in
        ["option_arg_match_tripwire", "result_arg_match_tripwire", "option_arg_payload_tripwire"]
    {
        let o = outcome(&report, name);
        let FnOutcome::Failed { kind, .. } = o else {
            panic!("`{name}` must FAIL: a `Skipped` means the shape is not modelled, got: {o}");
        };
        assert_eq!(kind, "postcondition", "`{name}` got: {o}");
    }
}

/// The same shape in an isolated file, so the fix is pinned even if the corpus
/// entry is edited: a `match` on `Option<u8>` must produce a verdict, not the
/// "unmodelled switch discriminant" refusal.
///
/// The nested-type spelling is the part worth pinning separately -- the payload is
/// the type argument as *one* type, so `Option<u8>` carries a `u8` and a
/// `Result<u8, u16>` carries a `u8` for `Ok` and a `u16` for `Err`. Splitting the
/// argument list at the wrong level (or reading the payload as the whole `u8, u16`
/// text) would size the payload wrongly and the width assertion in the contract
/// would fail.
#[test]
fn a_standard_enum_argument_payload_has_its_own_width() {
    let src = r#"macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }

        spec! {
            requires: true,
            ensures: ret <= 255,
        }
        pub fn opt_payload(x: Option<u8>) -> u8 {
            match x { Some(v) => v, None => 0 }
        }
        spec! {
            requires: true,
            ensures: ret <= 1,
        }
        pub fn res_payload(r: Result<u8, u16>) -> u8 {
            match r { Ok(_) => 0, Err(_) => 1 }
        }
    "#;
    let Some(o) = verify_src("opt_payload", src) else { return };
    assert!(o.is_verified(), "`opt_payload` must verify, got: {o}");
    let Some(o) = verify_src("res_payload", src) else { return };
    assert!(o.is_verified(), "`res_payload` must verify, got: {o}");
}

/// A cast in a *contract* (`x as u32`) is readable, and it extends the way Rust
/// does: `-1i8 as i16` is `-1`, and `-1i8 as u16` is `65535` (sign-extended even
/// into an unsigned target), not the zero-extended `255`.
///
/// The body has modelled `as` for a while; the contract language had none, so a
/// contract relating a narrow argument to a wide return was unreadable. The
/// direction is the whole point: a contract cast that zero-filled a signed source
/// would refute the true `ret == 65535` and prove the false `ret == 255`, so the
/// true entries verifying *and* the tripwires being refuted is what pins it.
#[test]
fn a_cast_in_a_contract_extends_like_rust() {
    let Some(report) = verify_realistic(&[
        "contract_cast_widens_unsigned",
        "contract_cast_widens_unsigned_tripwire",
        "contract_cast_widens_signed",
        "contract_cast_signed_to_unsigned",
        "contract_cast_signed_to_unsigned_tripwire",
        "contract_cast_narrows",
        "contract_cast_narrows_tripwire",
        "contract_cast_chain",
        "contract_cast_chain_tripwire",
    ]) else {
        return;
    };
    for name in [
        "contract_cast_widens_unsigned",
        "contract_cast_widens_signed",
        "contract_cast_signed_to_unsigned",
        "contract_cast_narrows",
        "contract_cast_chain",
    ] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify, got: {o}");
    }
    for name in [
        "contract_cast_widens_unsigned_tripwire",
        "contract_cast_signed_to_unsigned_tripwire",
        "contract_cast_narrows_tripwire",
        "contract_cast_chain_tripwire",
    ] {
        let o = outcome(&report, name);
        let FnOutcome::Failed { kind, .. } = o else {
            panic!("`{name}` must FAIL: a `Skipped` means the cast is not modelled, got: {o}");
        };
        assert_eq!(kind, "postcondition", "`{name}` got: {o}");
    }
    // The sign-extension witness must be `-1`, the one input where the two
    // extension rules differ; naming any other input would mean the model
    // disagrees with `core` elsewhere.
    let o = outcome(&report, "contract_cast_signed_to_unsigned_tripwire");
    let FnOutcome::Failed { detail, .. } = o else { panic!("got: {o}") };
    assert!(
        detail.contains("= 255"),
        "the sign-extension witness should be x == -1 (bits 255), got: {detail}"
    );
}

/// The cast shape in an isolated file, so the parse is pinned even if the corpus
/// entry is edited: `ret == x as u32` must be *readable* (a cast the parser cannot
/// read is a `Skipped`, i.e. a dropped obligation) and must hold.
#[test]
fn a_contract_cast_is_read_and_proved() {
    let src = r#"macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }

        spec! {
            requires: true,
            ensures: ret == x as u32,
        }
        pub fn widen(x: u8) -> u32 {
            x as u32
        }
        spec! {
            requires: true,
            ensures: ret == x as u8,
        }
        pub fn narrow(x: u16) -> u8 {
            x as u8
        }
    "#;
    let Some(o) = verify_src("widen", src) else { return };
    assert!(o.is_verified(), "`widen` must verify, got: {o}");
    let Some(o) = verify_src("narrow", src) else { return };
    assert!(o.is_verified(), "`narrow` must verify, got: {o}");
}

/// A cast in a contract is a *value*, so a relational postcondition about it is
/// decided rather than refused.
///
/// The equality entries pin that a cast *extends* like Rust (`-1i8 as i16` is
/// `-1`, not `255`), but they only ever meet the cast with `==`. A cast the order
/// operator cannot read would be a silently dropped obligation -- the failure mode
/// the tripwire rule exists to catch -- so this pairs three true relational
/// contracts with two that must be refuted. The pair also separates
/// sign-extension from zero-filling *through the order*: `(y as i16) >= 0` for an
/// `i8` `y` is false exactly when the source's sign is kept, and
/// `(y as i16) <= 127` is true exactly when it is.
///
/// The evidence behind the entries is a differential sweep over every cast of a
/// small type into every wider target, compared against an exhaustive Python
/// model: 2160 cases decided, 0 unsound, 0 discrepancies.
#[test]
fn a_cast_in_a_contract_can_be_ordered() {
    let names = [
        "contract_cast_relational_nonneg",
        "contract_cast_relational_lt_width",
        "contract_cast_relational_signed_bound",
        "contract_cast_relational_signed_nonneg_tripwire",
        "contract_cast_relational_positive_tripwire",
    ];
    let Some(report) = verify_realistic(&names) else { return };
    for name in [
        "contract_cast_relational_nonneg",
        "contract_cast_relational_lt_width",
        "contract_cast_relational_signed_bound",
    ] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify, got: {o}");
    }
    for name in [
        "contract_cast_relational_signed_nonneg_tripwire",
        "contract_cast_relational_positive_tripwire",
    ] {
        let o = outcome(&report, name);
        assert!(
            matches!(o, FnOutcome::Failed { .. }),
            "`{name}` is false for every negative (resp. zero) input; got: {o}"
        );
    }
}

/// The `.wrapping_*` methods are the method-call spelling of the wrapping
/// operators the contract language already models, and reading them is what lets a
/// contract say what a body that uses them *means*.
///
/// Before this, every `x.wrapping_add(y)` in a contract was "cannot read contract",
/// which leaves the function silently outside the check set -- the outcome the
/// frontend exists to avoid. A two-variable relational sweep (208 pairs of
/// `requires`/`ensures` over `u8` `x`, `y`, exhaustive ground truth over all 2^16
/// pairs) went from **120 decided / 88 skipped** to **208 decided / 0 skipped**,
/// with 0 unsound and 0 discrepancies, so the desugaring agrees with Rust on every
/// pair it now reads.
///
/// The two refusals are the teeth: `x.wrapping_add(300)` is a rustc error for its
/// `300` (the wrapping semantics excuse the overflow of the *result*, not an
/// out-of-range literal), and `1.wrapping_add(2)` is `E0689` (an ambiguous
/// `{integer}` receiver). Both must be refused, never given a verdict.
#[test]
fn a_wrapping_method_in_a_contract_is_read_as_the_operator_it_denotes() {
    let names = [
        "contract_wrapping_add_reads",
        "contract_wrapping_add_is_a_value_tripwire",
        "contract_wrapping_sub_mul_chain_reads",
        "contract_wrapping_agrees_with_the_plain_operators",
        "contract_wrapping_neg_on_unsigned_reads",
        "contract_wrapping_neg_on_signed_reads",
        "contract_wrapping_add_of_constants_wraps",
    ];
    let Some(report) = verify_realistic(&names) else { return };
    for name in [
        "contract_wrapping_add_reads",
        "contract_wrapping_sub_mul_chain_reads",
        "contract_wrapping_agrees_with_the_plain_operators",
        "contract_wrapping_neg_on_unsigned_reads",
        "contract_wrapping_neg_on_signed_reads",
        "contract_wrapping_add_of_constants_wraps",
    ] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify, got: {o}");
    }
    let o = outcome(&report, "contract_wrapping_add_is_a_value_tripwire");
    assert!(
        matches!(o, FnOutcome::Failed { .. }),
        "the wrapping add is a value, so `+ 1` on it is false for every input; got: {o}"
    );

    for name in [
        "contract_wrapping_add_of_an_out_of_range_literal_is_refused",
        "contract_wrapping_add_on_an_untyped_receiver_is_refused",
    ] {
        let Some(report) = verify_realistic(&[name]) else { return };
        let o = outcome(&report, name);
        assert!(
            matches!(o, FnOutcome::Skipped { .. }),
            "`{name}` is a rustc error and must be refused, never given a verdict, got: {o}"
        );
    }
}

/// `wrapping_shl`/`wrapping_shr` are modelled as a shift by the amount reduced
/// modulo the width, with `shr` arithmetic on a signed receiver and logical on an
/// unsigned one. The model was checked against Rust's own methods over the whole
/// `u8`/`i8` operand domain in `intrinsics::tests::wrapping_shifts_agree_with_rust`
/// (a mutation to a logical shift, and one that drops the amount reduction, each
/// fail it); these corpus entries pin the *end-to-end* verdicts, so a regression
/// in the wiring -- not just the model -- is caught.
///
/// Each true entry has a false twin that a wrong model would *prove*, and each
/// pair is *discriminating*: the left-shift pair pins the amount reduction (the
/// true entry claims `1`, the twin claims the saturated `0`, so only a reducing
/// model proves one and refutes the other -- a `ret <= 255` bound would satisfy
/// both), the signed right-shift pair pins the shift *kind* (`-4` vs the logical
/// `124`), and the unsigned right-shift entry pins both at once (`64`: a sign fill
/// would give `192`, a saturating shift `0`).
#[test]
fn wrapping_shifts_are_modelled_with_the_amount_reduced_modulo_the_width() {
    let Some(report) = verify_realistic(&[
        "wrapping_shl_reduces_its_amount",
        "wrapping_shl_amount_is_not_saturated_tripwire",
        "wrapping_shr_signed_is_arithmetic",
        "wrapping_shr_signed_is_not_logical_tripwire",
        "wrapping_shr_unsigned_is_logical",
    ]) else {
        return;
    };
    for name in [
        "wrapping_shl_reduces_its_amount",
        "wrapping_shr_signed_is_arithmetic",
        "wrapping_shr_unsigned_is_logical",
    ] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify, got: {o}");
    }
    for name in [
        "wrapping_shl_amount_is_not_saturated_tripwire",
        "wrapping_shr_signed_is_not_logical_tripwire",
    ] {
        let o = outcome(&report, name);
        assert!(
            matches!(o, FnOutcome::Failed { .. }),
            "`{name}` is false under the correct model and must be refuted, got: {o}"
        );
    }
}

/// The contract operators that were missing (`/ % << >>`), and the precedence
/// they need. A missing operator is a dropped obligation -- the contract is
/// unreadable and the function silently leaves the check set -- so each entry
/// pairs a true contract with a tripwire, and the precedence entries pin the
/// *order*, not merely that the operator parses.
///
/// The signed entries are the ones with teeth: `>>` on a signed value must be
/// arithmetic (`-8 >> 1 == -4`, not a large positive) and `/` must be the signed
/// quotient (`-7 / 2 == -3`, not a huge positive). The unsigned engine would give
/// a wrong value, so those entries separate the two engines.
#[test]
fn contract_operators_and_their_precedence() {
    let Some(report) = verify_realistic(&[
        "contract_shr_unsigned",
        "contract_shr_unsigned_tripwire",
        "contract_shl",
        "contract_shr_signed_is_arithmetic",
        "contract_shr_signed_is_arithmetic_tripwire",
        "contract_udiv",
        "contract_urem",
        "contract_udiv_tripwire",
        "contract_sdiv",
        "contract_srem",
        "contract_sdiv_tripwire",
        "contract_precedence_and_over_add",
        "contract_precedence_and_over_add_tripwire",
        "contract_precedence_add_over_shl",
        "contract_precedence_add_over_shl_tripwire",
        "contract_precedence_and_over_or",
        "contract_precedence_and_over_or_tripwire",
        "contract_precedence_shift_over_eq",
        "contract_precedence_shift_over_eq_tripwire",
        "contract_precedence_cast_over_add",
        "contract_precedence_neg_over_cast",
        "contract_precedence_neg_over_rem",
        "contract_precedence_mul_rem_left_assoc",
    ]) else {
        return;
    };
    for name in [
        "contract_shr_unsigned",
        "contract_shl",
        "contract_shr_signed_is_arithmetic",
        "contract_udiv",
        "contract_urem",
        "contract_sdiv",
        "contract_srem",
        "contract_precedence_and_over_add",
        "contract_precedence_add_over_shl",
        "contract_precedence_and_over_or",
        "contract_precedence_shift_over_eq",
        "contract_precedence_cast_over_add",
        "contract_precedence_neg_over_cast",
        "contract_precedence_neg_over_rem",
        "contract_precedence_mul_rem_left_assoc",
    ] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify, got: {o}");
    }
    for name in [
        "contract_shr_unsigned_tripwire",
        "contract_shr_signed_is_arithmetic_tripwire",
        "contract_udiv_tripwire",
        "contract_sdiv_tripwire",
        "contract_precedence_and_over_add_tripwire",
        "contract_precedence_add_over_shl_tripwire",
        "contract_precedence_and_over_or_tripwire",
        "contract_precedence_shift_over_eq_tripwire",
    ] {
        let o = outcome(&report, name);
        let FnOutcome::Failed { kind, .. } = o else {
            panic!("`{name}` must FAIL: a `Skipped` means the operator is not modelled, got: {o}");
        };
        assert_eq!(kind, "postcondition", "`{name}` got: {o}");
    }
    // `-8 >> 1` is `-4`, so a *logical* reading (a large positive) is what the
    // tripwire refutes; the witness `248` is the `-8` bit pattern, which pins
    // that the parser read the sign, not merely the value.
    let o = outcome(&report, "contract_shr_signed_is_arithmetic_tripwire");
    let FnOutcome::Failed { detail, .. } = o else { panic!("got: {o}") };
    assert!(detail.contains("= 248"), "the witness should be the `-8` pattern, got: {detail}");
    // The `&`/`+` precedence tripwire must be refuted at `x == 1`, the input
    // where `x & (1 + 1)` and `(x & 1) + 1` differ.
    let o = outcome(&report, "contract_precedence_and_over_add_tripwire");
    let FnOutcome::Failed { detail, .. } = o else { panic!("got: {o}") };
    assert!(detail.contains("= 1"), "the witness should be `x == 1`, got: {detail}");
}

/// A `u128`/`i128` function must be *refused*, not panicked. The engine's words
/// are 1..=64 bits and every width-taking constructor asserts that, so a 128-bit
/// argument, return, local or cast target has no width to model. Before the
/// guard, whichever of the argument seeding, the loop havoc or the contract
/// bindings ran first hit that assertion and took the whole run down — a panic
/// reports nothing, where a refusal reports the function. This pins the refusal
/// on each path, and that a 64-bit neighbour still verifies.
#[test]
fn a_128_bit_function_is_refused_not_panicked() {
    let Some(report) = verify_realistic(&[
        "wide_arg",
        "wide_return",
        "wide_local_in_loop",
        "wide_cast_in_contract",
        "narrow_64_still_works",
    ]) else {
        return;
    };
    for name in ["wide_arg", "wide_return", "wide_local_in_loop", "wide_cast_in_contract"] {
        let o = outcome(&report, name);
        let FnOutcome::Skipped { reason } = o else {
            panic!("`{name}` must be refused, got: {o}");
        };
        assert!(
            reason.contains("64-bit") || reason.contains("wider"),
            "`{name}` should say why, got: {reason}"
        );
    }
    // The guard must be about the function's own width, not a blanket refusal of
    // anything mentioning 128 bits: a `u64` neighbour still verifies.
    let o = outcome(&report, "narrow_64_still_works");
    assert!(o.is_verified(), "a 64-bit function must still verify, got: {o}");
}

/// The contract parser must refuse a cast *to* a 128-bit type on its own, since a
/// function whose own types are all 64-bit would otherwise reach `truncate` and
/// panic there. Pinned in isolation so the refusal does not depend on the corpus.
#[test]
fn a_contract_cast_to_128_bits_is_refused() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nspec! {\n    requires: true,\n    ensures: ret == x as u128,\n}\npub fn f(x: u8) -> u64 { x as u64 }";
    let Some(o) = verify_src("f", src) else { return };
    let FnOutcome::Skipped { reason } = o else {
        panic!("a cast to `u128` must be refused, got: {o}");
    };
    assert!(reason.contains("u128"), "the refusal should name the type, got: {reason}");
}

/// The operator shapes in an isolated file, so the *parse* is pinned even if the
/// corpus entry is edited. Each is one line, so a regression says exactly which
/// operator stopped being read.
#[test]
fn contract_operators_parse_in_isolation() {
    let cases: &[(&str, &str)] = &[
        ("shr", "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nspec! {\n    requires: true,\n    ensures: ret == x >> 1,\n}\npub fn shr(x: u8) -> u8 { x >> 1 }"),
        ("shl", "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nspec! {\n    requires: true,\n    ensures: ret == x << 2,\n}\npub fn shl(x: u8) -> u8 { x << 2 }"),
        ("udiv", "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nspec! {\n    requires: true,\n    ensures: ret == x / 3,\n}\npub fn udiv(x: u32) -> u32 { x / 3 }"),
        ("urem", "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nspec! {\n    requires: true,\n    ensures: ret == x % 10,\n}\npub fn urem(x: u32) -> u32 { x % 10 }"),
        (
            "prec",
            "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nspec! {\n    requires: true,\n    ensures: ret == x & 1 + 1,\n}\npub fn prec(x: u8) -> u8 { x & 2 }",
        ),
    ];
    for (name, src) in cases {
        let Some(o) = verify_src(name, src) else { return };
        assert!(o.is_verified(), "`{name}` must verify, got: {o}");
    }
}

/// A constant signed expression, with no symbolic operand, must use the *signed*
/// engine for `%`, `/` and `>>`. Signedness is inferred from the names in an
/// expression, and there are none here, so the negated literal is the only
/// evidence — and it used to report itself unsigned. The mistake was a *wrong
/// value*, not a refusal: the unsigned `%` gives `4294967289 % 3 == 0` where Rust
/// says `-1`, and the tripwires below would then have *verified*, which is the
/// direction that manufactures a false proof. Each pair is asserted both ways, so
/// a fix that merely flipped the flag the other way fails here too.
#[test]
fn a_constant_signed_expression_uses_the_signed_engine() {
    let srcs = [
        (
            "srem",
            "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nspec! {\n    requires: true,\n    ensures: ret == -7 % 3,\n}\npub fn srem() -> i32 { -7i32 % 3i32 }",
            "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nspec! {\n    requires: true,\n    ensures: ret == 2,\n}\npub fn srem() -> i32 { -7i32 % 3i32 }",
        ),
        (
            "sdiv",
            "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nspec! {\n    requires: true,\n    ensures: ret == -7 / 2,\n}\npub fn sdiv() -> i32 { -7i32 / 2i32 }",
            "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nspec! {\n    requires: true,\n    ensures: ret == 3,\n}\npub fn sdiv() -> i32 { -7i32 / 2i32 }",
        ),
        (
            "ashr",
            "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nspec! {\n    requires: true,\n    ensures: ret == -8 >> 1,\n}\npub fn ashr() -> i32 { -8i32 >> 1 }",
            "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nspec! {\n    requires: true,\n    ensures: ret == -1,\n}\npub fn ashr() -> i32 { -8i32 >> 1 }",
        ),
    ];
    for (name, true_src, false_src) in srcs {
        let Some(o) = verify_src(name, true_src) else { return };
        assert!(o.is_verified(), "`{name}` (true) must verify, got: {o}");
        let Some(o) = verify_src(name, false_src) else { return };
        assert!(!o.is_verified(), "`{name}` (tripwire) must be refuted, got: {o}");
    }
}

/// Unary `-` binds *tighter* than `as`, so `-x as u8` has type `u8` — unsigned.
/// Reading it the other way (`-(x as u8)`, signed) made `-x as u8 > 127`
/// unsatisfiable, so a function that is verifiable was reported *vacuous*. Both
/// directions are asserted: the satisfiable one must verify, and the tripwire —
/// which the signed reading would wrongly *verify* — must be refuted.
#[test]
fn a_negation_binds_tighter_than_a_cast() {
    let cases = [
        (
            "neg_cast_gt",
            "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nspec! {\n    requires: -x as u8 > 127,\n    ensures: ret == 1,\n}\npub fn neg_cast_gt(x: i8) -> u8 { 1 }",
            true,
        ),
        (
            "neg_cast_lt_tripwire",
            "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nspec! {\n    requires: x == 1,\n    ensures: (-x as u8) < 127,\n}\npub fn neg_cast_lt_tripwire(x: i8) -> u8 { 1 }",
            false,
        ),
        (
            "neg_cast_value",
            "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nspec! {\n    requires: x == -5,\n    ensures: ret == -x as u8,\n}\npub fn neg_cast_value(x: i8) -> u8 { -x as u8 }",
            true,
        ),
    ];
    for (name, src, want_verified) in cases {
        let Some(o) = verify_src(name, src) else { return };
        assert_eq!(
            o.is_verified(),
            want_verified,
            "`{name}`: expected verified={want_verified}, got: {o}"
        );
    }
}

/// A differential oracle for the contract expression language, against rustc.
///
/// The five existing oracles (z3, Kani, Lean, Coq, Verus) all build their terms with
/// `smt::` calls, so they start *below* the contract reader — the layer where three
/// bugs in a row lived. This closes that gap using the one decision procedure that
/// defines Rust: the compiler.
///
/// Two constructions, because the two bugs this found needed different ones:
///
/// * **Value** (`guard: None`). The body *is* the expression, so `ret` is rustc's
///   value of `E` at `x == v`; the postcondition `ret == E` asserts the reader's value
///   of `E` at the same point. This catches a wrong *value* — the signedness bug, where
///   `-7 % 3` was read as `0`.
///
/// * **Guard** (`guard: Some(t)`). The precondition is `x == v && E > t`, so the
///   comparison is decided at exactly `v`, and the body returns `1` with `ensures: ret
///   == 1`. The function verifies iff rustc says `E > t` at `v`. This is what catches a
///   wrong *type*: `-x as u8` and `-(x as u8)` have the same value (negation and
///   truncation commute), so no value check sees the difference — but only one of them
///   is `u8`, and the comparison is what reads the type. Pinning `x == v` makes the
///   expected verdict exact, so a *vacuous* result is a failure and not a grey area.
///
/// The grid is the point. A single point per expression would have missed the
/// signedness bug, so each case runs every value where a signed/unsigned confusion is
/// visible: the extremes of the type, `-1`, `0`, `1`. Each case lists its own grid,
/// because some expressions overflow at the extreme (`-x` for `x == i8::MIN` panics,
/// and `(x + 1) << 1` overflows `u8` at `x == 127`) — a panic is a *true* verdict, but
/// not the agreement this test is for.
#[test]
fn the_contract_reader_agrees_with_rustc_on_a_grid() {
    // (expression, argument type, return type, grid, guard threshold)
    type Case = (&'static str, &'static str, &'static str, &'static [i64], Option<u8>);
    let cases: &[Case] = &[
        // Signed remainder, division and arithmetic shift, over the grid. The negative
        // points are what separate the signed engine from the unsigned one.
        ("x % 3", "i8", "i8", &[-128, -7, -1, 0, 1, 7, 127], None),
        ("x / 2", "i8", "i8", &[-128, -7, -1, 0, 1, 7, 127], None),
        ("x >> 1", "i8", "i8", &[-128, -7, -1, 0, 1, 7, 127], None),
        // A negated literal, the shape the first bug was about.
        ("-7 % 3", "i8", "i8", &[0], None),
        ("-7 / 2", "i8", "i8", &[0], None),
        ("-8 >> 1", "i8", "i8", &[0], None),
        // Unary `-` before the cast, as a *value*: both readings agree, so this half
        // only guards against a fix that broke the value.
        ("-x as u8", "i8", "u8", &[-7, -1, 0, 1, 127], None),
        // The same expression as a *guard*, which is where the type is observable:
        // `(-x) as u8 > 127` is true at `x == 1` (255), false under a signed reading.
        ("-x as u8", "i8", "u8", &[-7, -1, 0, 1, 127], Some(127)),
        // Casts: widening sign-extends, narrowing keeps the low bits, a chain is
        // left to right.
        ("x as i16", "i8", "i16", &[-128, -1, 0, 1, 127], None),
        ("x as i8", "i16", "i8", &[-32768, -129, -1, 0, 1, 127, 128, 32767], None),
        ("x as i8 as i32", "i16", "i32", &[-32768, -129, -1, 0, 1, 127, 128, 32767], None),
        ("x as u16", "u8", "u16", &[0, 1, 128, 255], None),
        // Precedence and associativity, on the grid.
        ("x & 1 + 1", "u8", "u8", &[0, 1, 2, 3, 255], None),
        ("x + 1 << 1", "u8", "u8", &[0, 1, 63, 126], None),
        ("x | 1 ^ 1", "u8", "u8", &[0, 1, 255], None),
        ("x & 3 | 4", "u8", "u8", &[0, 1, 3, 255], None),
        ("x ^ 1 & 3", "u8", "u8", &[0, 1, 3, 255], None),
        ("x | 1 ^ 2", "u8", "u8", &[0, 1, 2, 255], None),
        // Unary `!` stacked, and `!` feeding a shift: both are bitwise, so `!!x` is
        // `x` and `!x >> 1` shifts the complement.
        ("!!x", "u8", "u8", &[0, 1, 5, 250, 255], None),
        ("!x >> 1", "u8", "u8", &[0, 1, 250, 255], None),
        // A shift and a bitwise `|` in one expression: `x << 1 | 1` is
        // `(x << 1) | 1`, because `<<` binds tighter than `|`.
        ("x << 1 | 1", "u8", "u8", &[0, 1, 2, 63], None),
        // Unary `-` feeding a shift, and unary `-` feeding `+`.
        ("-x >> 1", "i8", "i8", &[-127, -1, 0, 1, 127], None),
        ("-x + 1", "i8", "i8", &[-126, -1, 0, 1, 126], None),
        // A cast feeding a bitwise `&`, and a double cast.
        ("x as u16 & 1", "i8", "u16", &[-128, -1, 0, 1, 127], None),
        ("x as u8 as u16", "i8", "u16", &[-128, -1, 0, 1, 127], None),
        // `*` binds tighter than `+`, and a repeated shift is left-associative.
        ("1 + x * 2", "u8", "u8", &[0, 1, 5, 63], None),
        ("x >> 1 >> 1", "u8", "u8", &[0, 1, 255], None),
        // A cast binds tighter than `+`, and unary `-` binds tighter than a cast. The
        // guard mode reads the *type* of the result, which is where `(x as i16) + 1`
        // and `x as (i16 + 1)` would differ. (`*` is left out: an `i16 * i16` body has
        // an overflow assert the engine declines to model at that width, so the probe
        // itself is undecided and the guard mode cannot read the comparison.)
        ("x as i16 + 1", "i8", "i16", &[-1, 0, 5, 126], Some(6)),
        ("-x as i16", "i8", "i16", &[-1, 0, 1, 127], None),
        ("x << 2", "u8", "u8", &[0, 1, 63, 255], None),
        ("8 - 4 - 2", "i32", "i32", &[0], None),
        ("8 / 4 / 2", "u32", "u32", &[0], None),
        // A negation of a name, and a double negation, which is the value again.
        ("-x", "i8", "i8", &[-127, -1, 0, 1, 127], None),
        ("- -x", "i8", "i8", &[-127, -1, 0, 1, 127], None),
        // `0 - x` is a subtraction, not a negation, so it *overflows* at `x ==
        // i8::MIN` (the body panics, a true verdict but not an agreement point).
        ("0 - x", "i8", "i8", &[-127, -1, 0, 1, 127], None),
        // The signedness of a comparison on a variable, over the whole grid.
        ("x", "i8", "i8", &[-128, -7, -1, 0, 1, 7, 127], Some(0)),
        ("x % 3", "i8", "i8", &[-128, -7, -1, 0, 1, 7, 127], Some(0)),
        // Unary `!` on a value is *bitwise* NOT, and binds tighter than `==`:
        // `!x == 5` is `(!x) == 5` (`x == 250`), not `!(x == 5)` (`x != 5`). The
        // predicate reading is a different predicate, so it is a wrong verdict.
        ("!x", "u8", "u8", &[0, 1, 128, 255], None),
        ("!!x", "u8", "u8", &[0, 1, 128, 255], None),
        ("!x + 1", "u8", "u8", &[1, 128, 255], None),
        ("x & !1", "u8", "u8", &[0, 1, 3, 255], None),
        // The guard form is the discriminator: `(!x) == 5` holds only at `x == 250`,
        // where `!(x == 5)` would hold everywhere else. Pinning `x == v` makes the
        // two readings differ at every point but one.
        ("!x", "u8", "u8", &[0, 1, 5, 250, 255], Some(5)),
        ("!x", "u8", "u8", &[0, 1, 5, 250, 255], Some(4)),
        // Parentheses do not make it a predicate: `!(x + 1)` holds *arithmetic*, so
        // `!` is still the value form and `!(x + 1) == 5` is `(!(x + 1)) == 5`, not a
        // negation of `(x + 1) == 5` (which is not even well-typed). This is the case
        // that separates `bang_is_predicate`'s lookahead from "a `(` follows".
        ("!(x + 1)", "u8", "u8", &[0, 1, 5, 249, 254], None),
        // A shift amount is a constant *expression*: the reader and the body must
        // both fold `1 + 1` to `2`, not treat it as a symbolic barrel-shifter
        // amount. The value mode builds `x >> 1 + 1` as the body *and* the
        // postcondition, so an unreadable contract (a non-folded reader) shows up
        // as a non-verified outcome here.
        ("x >> 1 + 1", "u8", "u8", &[0, 1, 2, 4, 8, 16, 128, 255], None),
        // A radix literal is the expression in *both* the body and the
        // postcondition: rustc compiles the body `0xFF` to `255`, and the reader
        // must read the contract `0xFF` as `255` for `ret == 0xFF` to hold. A
        // decimal-only reader read it as `0`, so the postcondition became
        // `ret == 0` and the tool *proved* it for a body returning `255` -- a false
        // proof, which this row turns into a disagreement. The `x == 0` guard makes
        // the function total so the only thing under test is the literal.
        ("0xFF", "u8", "u8", &[0], None),
        ("0b1010", "u8", "u8", &[0], None),
        ("0o17", "u8", "u8", &[0], None),
        ("0xFFu8", "u8", "u8", &[0], None),
        ("0xFF_FF", "u16", "u16", &[0], None),
        ("0x10", "u8", "u8", &[0], None),
    ];

    let mut checked = 0u32;
    for (expr, arg_ty, ret_ty, grid, guard) in cases {
        for &v in *grid {
            // A stable, identifier-safe name: the index, not the expression (which
            // contains characters Rust will not take in a name).
            let name = format!("oracle_{checked}");
            let (src, want) = match guard {
                // Value: body is the expression, postcondition asserts the reader's
                // value equals rustc's.
                None => (
                    format!(
                        "/// requires: x == {v}\n/// ensures: ret == {expr}\n\
                         pub fn {name}(x: {arg_ty}) -> {ret_ty} {{ {expr} }}\n"
                    ),
                    true,
                ),
                // Guard: the body returns `1` and the precondition is the comparison
                // pinned at `v`, so it verifies iff rustc's comparison is true.
                Some(t) => {
                    // Ask rustc for the comparison at this point, so the expectation is
                    // the compiler's, not mine.
                    let probe = format!(
                        "/// requires: x == {v}\n/// ensures: ret == 1\n\
                         pub fn {name}(x: {arg_ty}) -> u8 {{ if {expr} > {t} {{ 1 }} else {{ 0 }} }}\n"
                    );
                    // The probe's body is rustc's comparison; the value it returns is
                    // what `expect_verified` must match. `1` means satisfiable.
                    let o = verify_src(&name, &probe);
                    let Some(o) = o else { return };
                    // The probe verifies iff the body always returns `1`, which is true
                    // only when the comparison holds at `v`.
                    let truth = o.is_verified();
                    let src = format!(
                        "/// requires: x == {v} && {expr} > {t}\n/// ensures: ret == 1\n\
                         pub fn {name}(x: {arg_ty}) -> u8 {{ 1 }}\n"
                    );
                    (src, truth)
                }
            };
            let Some(o) = verify_src(&name, &src) else { return };
            assert_eq!(
                o.is_verified(),
                want,
                "the reader disagrees with rustc: `{expr}` at x == {v} ({arg_ty}), guard {guard:?} — expected verified={want}, got: {o}"
            );
            checked += 1;
        }
    }
    // A run that checked nothing is not evidence.
    assert!(checked >= 60, "the oracle checked only {checked} points");
}

/// A shift amount or a divisor may be a *constant expression*, not only a bare
/// literal, and the reader must fold it before deciding what it is.
///
/// `x >> 1 + 1` is `x >> 2` and `x % (1 - 1)` is `x % 0`; Rust evaluates both
/// amounts at compile time. The reader tested the raw operand with `const_of`,
/// which only recognises a literal node, so a folded amount was misclassified in
/// two opposite directions: a constant shift amount looked symbolic (the body built
/// a barrel shifter and the engine could not decide), and a constant *zero* divisor
/// slipped past the divide-by-zero guard and produced a definite verdict from an
/// undefined `srem(x, 0)` -- a wrong verdict, the one outcome a verifier must not
/// produce. The fix folds with `const_value`/`evaluate` on both sides.
#[test]
fn a_folded_constant_is_read_as_a_constant() {
    // A folded shift amount verifies: the body shifts by the same constant.
    let Some(o) = verify_src(
        "folded_shift_amount",
        "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nspec! {\n    requires: x == 8,\n    ensures: ret == x >> 1 + 1,\n}\npub fn folded_shift_amount(x: u8) -> u8 { x >> 2 }\n",
    ) else {
        return;
    };
    assert!(o.is_verified(), "a folded shift amount must be read, got: {o}");

    // A folded *zero* divisor is refused, exactly as the literal `0` is. The
    // requirement is that it is not *decided*: a verdict built on `srem(x, 0)`
    // would be a verdict about nothing.
    let Some(o) = verify_src(
        "folded_zero_divisor",
        "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nspec! {\n    requires: x == 3,\n    ensures: ret == x % (1 - 1),\n}\npub fn folded_zero_divisor(x: u8) -> u8 { x % 3 }\n",
    ) else {
        return;
    };
    assert!(
        matches!(o, FnOutcome::Skipped { .. }),
        "a folded zero divisor must be refused, not decided, got: {o}"
    );

    // A folded *non-zero* divisor still reads and models exactly: `4 / (1 + 1)`
    // is `4 / 2` is `2`, the same as the body, so the two agree.
    let Some(o) = verify_src(
        "folded_nonzero_divisor",
        "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nspec! {\n    requires: x == 4,\n    ensures: ret == x / (1 + 1),\n}\npub fn folded_nonzero_divisor(x: u8) -> u8 { x / 2 }\n",
    ) else {
        return;
    };
    assert!(
        o.is_verified(),
        "a folded non-zero divisor must be read and modelled, got: {o}"
    );

    // And the *body* folds the same way: `x >> 1 + 1` lowers to a constant shift,
    // not a barrel shifter, so the query is decided rather than left incomplete.
    let Some(o) = verify_src(
        "folded_body_shift",
        "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nspec! {\n    requires: x == 8,\n    ensures: ret == 2,\n}\npub fn folded_body_shift(x: u8) -> u8 { x >> 1 + 1 }\n",
    ) else {
        return;
    };
    assert!(
        o.is_verified(),
        "a folded shift amount in the body must be lowered as a constant, got: {o}"
    );
}

/// A radix literal must be read at its radix, and the *control* is the point.
///
/// `0xFF` used to read as `0`, so `requires: x == 0xFF` became `x == 0` and
/// `ensures: ret == 0` was **proved** for a function returning `255` -- a false
/// proof, the worst outcome this crate can produce. The verifying entries show the
/// value is now right; the control shows the truncated value is gone, because it is
/// the entry that used to be a false proof and must now be refuted.
#[test]
fn a_radix_literal_is_read_at_its_radix() {
    let verified = [
        "contract_hex_literal_is_255",
        "contract_binary_literal_is_ten",
        "contract_octal_literal_is_fifteen",
        "contract_hex_underscore_is_65535",
        "contract_hex_with_suffix_is_255",
        "contract_hex_arithmetic_is_folded",
        "contract_hex_shift_amount",
        "contract_hex_mask_keeps_low_nibble",
        "contract_negative_hex_literal",
        "contract_uppercase_hex_prefix",
    ];
    let Some(report) = verify_realistic(&verified) else { return };
    for name in verified {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify, got: {o}");
    }
    let Some(report) = verify_realistic(&["contract_hex_literal_is_not_zero_control"]) else {
        return;
    };
    let o = outcome(&report, "contract_hex_literal_is_not_zero_control");
    let FnOutcome::Failed { kind, detail, .. } = o else {
        panic!("the control must be refuted -- verifying it is the false proof, got: {o}");
    };
    assert_eq!(kind, "postcondition", "the control got: {o}");
    // The witness is `255`, the value `0xFF` *should* be. Under the old truncating
    // read it would have been a proof, so the witness is the evidence the literal
    // is now read at radix 16.
    assert!(detail.contains("= 255"), "the witness should be `255`, got: {detail}");
}

/// A literal that does not fit the operand's signed width is refused, and the
/// false proof it used to allow is the point.
///
/// `128` for an `i8` is the bit pattern `0x80`, which is `-128` signed. A reader
/// that asked only whether the literal fits in eight *unsigned* bits accepted it, so
/// `x <= 128` was read as `x <= -128` and `ensures: ret <= 127` was *proved* for a
/// body returning `x`. rustc rejects `x <= 128` for an `i8` ("literal out of range
/// for `i8`"), so the correct outcome is a refusal. The `i8::MIN` boundary shares
/// the bit pattern and must still read, which is what keeps the fix from refusing
/// every `0x80`.
#[test]
fn an_out_of_range_literal_is_refused_not_wrapped() {
    let refused = [
        "contract_out_of_range_literal_is_refused",
        "contract_out_of_range_negation_is_refused",
        "contract_constant_vs_constant_out_of_range",
        "contract_constant_sum_overflow_is_refused",
        "contract_constant_underflow_is_refused",
        "contract_constant_div_overflow_is_refused",
        "contract_constant_signed_sum_overflow_is_refused",
        "contract_signed_literal_vs_unsigned_is_refused",
        "contract_signed_constant_sum_vs_unsigned_is_refused",
        "contract_signed_constant_mask_vs_unsigned_is_refused",
        // A negation of a constant is checked against the value it denotes, so a
        // negation that overflows its own type is refused (`-(i8::MIN)`, and the
        // wrapping shift `-(3 << 7)` that shares its bit pattern).
        "contract_negation_of_min_is_refused",
        "contract_negation_of_wrapping_shift_is_refused",
        // A shift *amount* is a value of its own `i32`, so an amount that wraps at
        // the operand's width but not at `i32` (`2 << 7` is `256`) is refused. A
        // nested suffix is in the inner shift's amount, so it does not fix the
        // outer amount's type (`2 << 7u8` is still the `i32` `256`), and a cast
        // fixes the type and overrides what it casts (`(1u8) as u16` is a `u16`,
        // so the amount is `257u16`).
        "contract_nested_shift_amount_overflow_is_refused",
        "contract_nested_shift_amount_sum_overflow_is_refused",
        "contract_nested_amount_suffix_does_not_fix_the_outer_width",
        "contract_cast_amount_operand_fixes_the_width",
    ];
    let Some(report) = verify_realistic(&refused) else { return };
    for name in refused {
        let o = outcome(&report, name);
        assert!(
            !o.is_verified(),
            "`{name}` must be refused, not verified -- verifying it is the false proof, got: {o}"
        );
        assert!(
            matches!(o, FnOutcome::Skipped { .. }),
            "`{name}` must be skipped (unreadable contract), got: {o}"
        );
    }
    let read = [
        "contract_min_bound_still_reads",
        "contract_negative_bound_reads_at_i16",
        "contract_constant_vs_constant_in_range",
        "contract_constant_sum_in_range_reads",
        "contract_constant_sum_wider_operand_reads",
        "contract_unsigned_literal_still_reads",
        "contract_unsigned_constant_sum_still_reads",
        "contract_unsigned_mask_still_reads",
        // The controls for the two classes above: an in-range negation and an
        // amount that fits `i32` both still read, so the refusals are pinned to
        // the overflow and not to the shape. A *typed* amount operand fixes the
        // amount's width, so `1u8 + (2 << 7)` is a `u8` amount (`2 << 7` is `0u8`)
        // and the shift rustc accepts must read rather than be refused.
        "contract_negation_of_max_still_reads",
        "contract_fitting_shift_amount_still_reads",
        "contract_typed_amount_operand_fixes_the_width",
    ];
    let Some(report) = verify_realistic(&read) else { return };
    for name in read {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must still read and verify, got: {o}");
    }
}

/// A signed element of a slice or array must be read as signed *in the contract*.
///
/// `bind_indexed_elem` (and the symbolic-index chain) recorded an element's
/// signedness for the executor's seeds but not for the parser's name table, so
/// `is_signed_name` reported an `i8` element as unsigned. A postcondition that
/// compares it with zero then reads as an unsigned comparison, which is *always
/// true* -- so `ensures: xs[0] >= 0` was reported verified for a body that returns
/// the element, a false proof with `i8::MIN` as the counterexample.
///
/// The existing `signed_slice_element_tripwire` did not catch this: its body reads
/// the element itself, so the *body's* (correct, executor-side) reading dominated
/// the verdict and it was already FAILED for the right reason. The bug only shows
/// when the element appears on the contract side alone, which is what these two
/// entries pin. Both are sensitive: removing either sign record makes them verify.
#[test]
fn a_signed_element_is_read_as_signed_in_the_contract() {
    let refuted = [
        "signed_slice_element_ensures_tripwire",
        "signed_array_chain_ensures_tripwire",
    ];
    let Some(report) = verify_realistic(&refuted) else { return };
    for name in refuted {
        let o = outcome(&report, name);
        let FnOutcome::Failed { kind, detail, .. } = o else {
            panic!(
                "`{name}` is false at `i8::MIN` and must be refuted -- verifying it is \
                 the unsigned-reading false proof, got: {o}"
            );
        };
        assert_eq!(kind, "postcondition", "`{name}` got: {o}");
        // The witness is `128`, the bit pattern of `i8::MIN`. Under the unsigned
        // reading the comparison is trivially true and there is no witness at all.
        assert!(
            detail.contains("= 128"),
            "the witness should be `128` (`i8::MIN`), got: {detail}"
        );
    }
}

/// The corpus entries that pin *where* `!` binds, asserted end to end.
///
/// `!` is bitwise NOT on a value and negation on a predicate, and it binds tighter
/// than `==`, `&` and `as`. Each entry below separates one of those bindings, so a
/// reader that got one wrong would either fail to parse or return the other value.
/// The control is the `==` case: it is *false* under Rust's reading and must be
/// refuted, which is what makes the verifying entry next to it evidence rather than
/// a tool that proves everything.
#[test]
fn a_bang_binds_by_rusts_precedence_in_the_corpus() {
    let verified = [
        "contract_bang_binds_tighter_than_eq",
        "contract_bang_is_bitwise_not",
        "contract_bang_still_negates_a_predicate",
        "contract_bang_binds_tighter_than_a_cast",
        "contract_bang_binds_tighter_than_and",
        "contract_bang_paren_arithmetic_is_a_value",
    ];
    let Some(report) = verify_realistic(&verified) else { return };
    for name in verified {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify, got: {o}");
    }
    let Some(report) = verify_realistic(&["contract_bang_binds_tighter_than_eq_control"]) else {
        return;
    };
    let o = outcome(&report, "contract_bang_binds_tighter_than_eq_control");
    let FnOutcome::Failed { .. } = o else {
        panic!("`contract_bang_binds_tighter_than_eq_control` is false and must be refuted, got: {o}");
    };
}


/// A contract above a struct or enum belongs to that item, not the next function.
///
/// The reader matched a `struct`/`enum` head and `continue`d without clearing the
/// pending contract, so a doc comment above a layout carried over to the next `fn`.
/// A leaked `requires` is a false proof when it is satisfiable: `requires: x == 0`
/// above a struct made `ensures: ret == 0` for a body returning `x` *verify*,
/// because the check is only made for the inputs the precondition admits.
///
/// Both entries below carry the identical function and contract; the first has a
/// struct above it with a `requires: x == 0`, the second does not. Both must be
/// refuted, and refuted the *same way*. The pair is the evidence: verifying the
/// tripwire would be the false proof, and refuting only the control would not show
/// the leak was dropped.
#[test]
fn a_contract_above_a_layout_does_not_leak_into_the_next_function() {
    let names = ["leaked_requires_tripwire", "leaked_requires_is_dropped"];
    let Some(report) = verify_realistic(&names) else { return };
    for name in names {
        let o = outcome(&report, name);
        let FnOutcome::Failed { kind, .. } = o else {
            panic!("`{name}` proves a false contract; got: {o}");
        };
        assert_eq!(kind, "postcondition", "`{name}` must fail on its postcondition, got: {o}");
    }
}

/// Two names of different signedness are refused, and the same-signedness pair
/// still reads.
///
/// `a > b` for an `i8` and a `u8` is `E0308: mismatched types` in rustc. The
/// reader used to pick a sign and decide it; the body reads each operand at its
/// own signedness, so the contract and the body could disagree about the operator's
/// meaning. Refusing keeps the answer "the tool could not run" rather than a verdict
/// on a program that does not compile. The control is the same comparison with both
/// operands `i8`: it is legal and must still verify, so the refusal is not a blanket
/// ban on two-name comparisons.
#[test]
fn a_mixed_signedness_comparison_is_refused_and_the_same_signedness_pair_reads() {
    let refused = [
        "contract_mixed_signedness_compare_is_refused",
        "contract_mixed_signedness_add_is_refused",
    ];
    let Some(report) = verify_realistic(&refused) else { return };
    for name in refused {
        let o = outcome(&report, name);
        assert!(
            matches!(o, FnOutcome::Skipped { .. }),
            "`{name}` must be refused (skipped), not given a verdict, got: {o}"
        );
    }
    let Some(report) = verify_realistic(&["contract_same_signedness_compare_still_reads"]) else {
        return;
    };
    let o = outcome(&report, "contract_same_signedness_compare_still_reads");
    assert!(o.is_verified(), "the same-signedness control must still verify, got: {o}");
}

/// An untyped constant's complement is refused in an *equality*, not just an
/// ordering.
///
/// `!0` has no type of its own. Rust types an untyped constant as `i32`, so `!0`
/// is `-1` and `!0 == 255` is `false`: the postcondition holds for no input. The
/// reader carried the complement as a bit pattern at the scope's width, built
/// `255u8 == 255`, and *verified* it -- a false proof. `==`/`!=` "compare bit
/// patterns" was the reason given for leaving them readable, but here the two
/// readings are different *values* (`-1` vs `255`), so the shape is refused. The
/// control fixes the reading with a typed operand and must still verify.
#[test]
fn an_untyped_complement_equality_is_refused_and_a_typed_partner_reads() {
    let Some(report) = verify_realistic(&["contract_untyped_complement_equality_is_refused"]) else {
        return;
    };
    let o = outcome(&report, "contract_untyped_complement_equality_is_refused");
    assert!(
        matches!(o, FnOutcome::Skipped { .. }),
        "`!0 == 255` is `false` in Rust; it must be refused, never given a verdict, got: {o}"
    );
    let Some(report) =
        verify_realistic(&["contract_complement_of_literal_with_typed_partner_reads"])
    else {
        return;
    };
    let o = outcome(&report, "contract_complement_of_literal_with_typed_partner_reads");
    assert!(o.is_verified(), "a typed partner fixes the reading and must verify, got: {o}");
}

/// The complement of an *out-of-range* literal is refused, while a fitting one
/// still reads and a wider operand is untouched.
///
/// `!128` for an `i8` is a rustc `literal out of range for i8` error -- `128` does
/// not fit a signed eight-bit type -- even though the complement's *value* `127`
/// does. The reader built `!128` as the constant `127` and let the resize truncate
/// it with no check, so `y <= !128` read as `y <= 127` (`i8::MAX`) and *verified*
/// for a program rustc rejects. Two controls keep the refusal at the range: `!127`
/// fits an `i8`, and `!128` fits an `i16`, so both must be *decided* (each is a
/// real predicate, not a tautology, so the verdict is `Failed` with a witness) --
/// a blanket ban on `!`-of-`128` would leave them `Skipped`.
#[test]
fn a_complement_of_an_out_of_range_literal_is_refused() {
    let Some(report) = verify_realistic(&[
        "contract_complement_of_out_of_range_literal_is_refused",
        "contract_fitting_complement_still_reads",
        "contract_complement_reads_at_a_wider_operand",
    ]) else {
        return;
    };
    let o = outcome(&report, "contract_complement_of_out_of_range_literal_is_refused");
    assert!(
        matches!(o, FnOutcome::Skipped { .. }),
        "`y <= !128` for an `i8` is a rustc range error; it must be refused, never given a \
         verdict, got: {o}"
    );
    for name in [
        "contract_fitting_complement_still_reads",
        "contract_complement_reads_at_a_wider_operand",
    ] {
        let o = outcome(&report, name);
        assert!(
            !matches!(o, FnOutcome::Skipped { .. }),
            "{name} fits the operand type and must be decided, got: {o}"
        );
    }
}

/// Unary `-` on an *unsigned* name is refused, and a *signed* negation still reads.
///
/// `-x` for a `u8` is `E0600: cannot apply unary operator `-` to type `u8``. The
/// reader modelled it as `0 - x`, a value Rust never produces, so the shape is
/// refused rather than decided -- the same answer as a mixed-signedness pair. The
/// control negates a signed name, which is legal, and must still verify.
#[test]
fn an_unsigned_negation_is_refused_and_a_signed_negation_reads() {
    let Some(report) = verify_realistic(&["contract_unsigned_negation_is_refused"]) else { return };
    let o = outcome(&report, "contract_unsigned_negation_is_refused");
    assert!(
        matches!(o, FnOutcome::Skipped { .. }),
        "`-x` for a `u8` does not compile and must be refused, got: {o}"
    );
    let Some(report) = verify_realistic(&["contract_signed_negation_still_reads"]) else { return };
    let o = outcome(&report, "contract_signed_negation_still_reads");
    assert!(o.is_verified(), "a negation of a signed name must still verify, got: {o}");
}

/// A contract literal widened to meet a *signed* operand zero-fills.
///
/// The fill comes from the constant's own signedness, not the operand it meets.
/// Rust's `(255u8 as i16)` is `255`, so `(x as i16) == 255` holds for every `u8`
/// `x`. Widening the literal from the operand's signed `i16` sign-extended it to
/// `65535` and *refuted* a postcondition Rust accepts -- a false refutation. The
/// control `254` is neither reading and must FAIL, so the fix is not "verify
/// everything".
#[test]
fn a_literal_widened_to_a_signed_operand_zero_fills() {
    let Some(report) = verify_realistic(&["contract_literal_widened_to_signed_zero_fills"]) else {
        return;
    };
    let o = outcome(&report, "contract_literal_widened_to_signed_zero_fills");
    assert!(o.is_verified(), "`255` widened to i16 is `255` and must verify, got: {o}");
    let Some(report) = verify_realistic(&["contract_literal_widened_to_signed_tripwire"]) else {
        return;
    };
    let o = outcome(&report, "contract_literal_widened_to_signed_tripwire");
    assert!(matches!(o, FnOutcome::Failed { .. }), "`254` is neither reading and must FAIL, got: {o}");
}

/// Every literal and every constant `+ - *` intermediate is range-checked against
/// the type the expression meets, not only the value the fold produces.
///
/// The reader folds a constant expression to one value and used to range-check
/// only that value. Rust checks each literal and each intermediate: `2 & 128` for
/// an `i8` is a `literal out of range` error although the fold's value is `0`, and
/// `128 - 63` is rejected although its value `65` fits. The reader decided both --
/// a wrong verdict on a program rustc rejects, where the previous fix's class was
/// a false *proof*. Two controls keep the refusal at the range: the same literals
/// under a `u8` partner fit, and `-128` is rescued by the unary `-` because the
/// negated value, not the magnitude, is what meets the type.
#[test]
fn a_literal_inside_a_constant_fold_is_range_checked_too() {
    let refused = [
        "contract_literal_in_fold_out_of_range_is_refused",
        "contract_masked_literal_out_of_range_is_refused",
        "contract_constant_sum_value_out_of_range_is_refused",
        "contract_literal_in_difference_out_of_range_is_refused",
        "contract_literal_in_negated_fold_is_refused",
        "contract_complement_shift_amount_is_refused",
    ];
    let Some(report) = verify_realistic(&refused) else { return };
    for name in refused {
        let o = outcome(&report, name);
        assert!(
            matches!(o, FnOutcome::Skipped { .. }),
            "`{name}` is a rustc range error and must be refused, never given a verdict, got: {o}"
        );
    }
    for name in [
        "contract_fold_literals_in_range_still_read",
        "contract_bare_negated_bound_still_reads",
    ] {
        let Some(report) = verify_realistic(&[name]) else { return };
        let o = outcome(&report, name);
        assert!(o.is_verified(), "{name} fits the type it meets and must verify, got: {o}");
    }
}

/// A constant fold that mixes an *untyped* complement with a plain operand is
/// refused when its value depends on the signedness Rust infers for it.
///
/// A complement has no signedness of its own: it takes the one the type it meets
/// gives it. The reader guesses it from the other operand, and when no operand
/// fixes a sign the guess can disagree with the reading the fold finally gets.
/// `!127 - 63` is `128 - 63 = 65` unsigned but `i8::MIN - 63` -- a rustc overflow --
/// signed, so the reader decided `65` for a program rustc rejects: a wrong verdict.
/// `!127 * 2` is the same shape. A negated bare literal beside a complement
/// (`!100 * -0`) is `E0277` for a `u16` -- legal only at a signed type -- so it is
/// refused too. Two controls keep the refusal at the ambiguity: the same fold under
/// a wider partner is in range under both readings and reads, and a fold that
/// agrees under both readings (`(!0) - 1`) still reads.
#[test]
fn a_sign_ambiguous_untyped_fold_is_refused() {
    for name in [
        "contract_sign_ambiguous_fold_is_refused",
        "contract_sign_ambiguous_mul_is_refused",
        "contract_complement_with_negated_literal_is_refused",
    ] {
        let Some(report) = verify_realistic(&[name]) else { return };
        let o = outcome(&report, name);
        assert!(
            matches!(o, FnOutcome::Skipped { .. }),
            "`{name}` is sign-ambiguous and must be refused, never given a verdict, got: {o}"
        );
    }
    for name in [
        "contract_sign_ambiguous_fold_at_wider_width_reads",
        "contract_agreeing_untyped_fold_still_reads",
    ] {
        let Some(report) = verify_realistic(&[name]) else { return };
        let o = outcome(&report, name);
        assert!(o.is_verified(), "{name} agrees under both readings and must verify, got: {o}");
    }
}

/// A complement's *provisional* unsigned value must not be range-checked as if it
/// were a written literal, while a literal *under* the fold still is.
///
/// `y == !(!1 + 1)` for an `i8` `y` is valid Rust: `!1 + 1` is `-1` at `i8` and
/// `!(-1)` is `0`, so the contract is `y == 0`. The reader evaluates the fold
/// unsigned (`255`) because its type is not known until it meets the `i8`, and
/// range-checking that guess refused the contract although no `255` is written. The
/// fold now records only its operands' envelopes, so the guess is not checked -- the
/// control shows a genuine literal under the fold (`!128`) is still range-checked
/// against the type the complement meets and is refused.
#[test]
fn an_opaque_folds_provisional_value_is_not_range_checked() {
    let Some(report) =
        verify_realistic(&["contract_opaque_fold_provisional_value_is_not_range_checked"])
    else {
        return;
    };
    let o = outcome(&report, "contract_opaque_fold_provisional_value_is_not_range_checked");
    assert!(o.is_verified(), "`y == !(!1 + 1)` is `y == 0` and must verify, got: {o}");
    let Some(report) =
        verify_realistic(&["contract_literal_under_complement_is_still_range_checked"])
    else {
        return;
    };
    let o = outcome(&report, "contract_literal_under_complement_is_still_range_checked");
    assert!(
        matches!(o, FnOutcome::Skipped { .. }),
        "`!128` is an `i8` range error and must be refused, got: {o}"
    );
}

/// The written magnitude of a suffixed literal is what Rust range-checks, and a
/// complement's operand envelope must not leak onto an unrelated literal that
/// shares the complement's value term.
///
/// `128i8` alone is a `literal out of range for i8` error and only `-128i8` is
/// legal, because the negation is what meets the type. Recording the *signed value*
/// (`-128`) instead of the written magnitude made the negation add `+128` to the
/// envelope and refuse the valid `-128i8`; it also made the positive `128i8`, which
/// rustc rejects, read as the in-range `-128` -- a wrong verdict in both directions.
/// The complement is worse: at eight bits `!128` is the constant `127`, which the
/// engine hash-conses with a separately written bare `127`, so recording the
/// operand envelope (`128`) on that shared id skipped the valid
/// `x == !128 && y == 127`. The envelope now travels in the operand's own payload.
///
/// Two follow-ups. A *nested* complement whose operand is a fold must union the
/// envelope it inherited with the one the fold recorded on its own term, or the
/// fold's out-of-range literal is dropped and the reader decides a contract rustc
/// rejects (`y == !(!1 - 200)` for an `i8`). And a *named bound* (`i8::MIN`) shares
/// its bit pattern with a bare or suffixed `128`, so the term-keyed envelope map
/// charged the literal's `128` to the bound and skipped a valid contract; the bound
/// is now built as a term of its own.
#[test]
fn a_suffixed_minimum_and_a_complement_operand_are_checked_where_they_are_written() {
    for name in [
        "contract_suffixed_positive_min_magnitude_is_refused",
        "contract_double_negated_min_is_refused",
        "contract_complement_operand_out_of_range_is_refused",
        "contract_negated_complement_operand_is_refused",
        "contract_nested_complement_fold_operand_is_refused",
    ] {
        let Some(report) = verify_realistic(&[name]) else { return };
        let o = outcome(&report, name);
        assert!(
            matches!(o, FnOutcome::Skipped { .. }),
            "`{name}` is a rustc range error and must be refused, never given a verdict, got: {o}"
        );
    }
    for name in [
        "contract_suffixed_min_literal_still_reads",
        "contract_suffixed_positive_literal_still_reads",
        "contract_complement_does_not_pollute_a_bare_literal",
        "contract_named_min_after_suffixed_min_reads",
        "contract_suffixed_min_after_named_min_reads",
        "contract_named_min_after_bare_128_reads",
    ] {
        let Some(report) = verify_realistic(&[name]) else { return };
        let o = outcome(&report, name);
        assert!(o.is_verified(), "{name} is valid Rust and must verify, got: {o}");
    }
}

/// An *opaque* constant (a complement) widened to a wider operand is refused, and
/// a *fold* is not.
///
/// A complement is taken at the *operand's* width, but the reader holds it at the
/// *scope's*. `!0` against a `u16` is `65535`, so `(x as u16 + !0) == 255` is false
/// in Rust; the reader held `255` and zero-extended it, and *verified* the equality
/// -- a false proof. The signed case fails the same way when the literal's top bit
/// is clear (`!128` against an `i16` is `-129`, not `127`). Both are refused. A
/// *fold* (`200 + 100`) is a value and must still verify.
#[test]
fn an_opaque_widening_is_refused_but_a_fold_widens() {
    let Some(report) = verify_realistic(&[
        "contract_opaque_widened_to_unsigned_is_refused",
        "contract_opaque_widened_to_signed_is_refused",
        "contract_fold_widens_to_unsigned_reads",
    ]) else {
        return;
    };
    for name in [
        "contract_opaque_widened_to_unsigned_is_refused",
        "contract_opaque_widened_to_signed_is_refused",
    ] {
        let o = outcome(&report, name);
        assert!(
            !o.is_verified(),
            "{name} must not verify -- the reader cannot model the complement at the operand width, got: {o}"
        );
    }
    let o = outcome(&report, "contract_fold_widens_to_unsigned_reads");
    assert!(o.is_verified(), "a fold widens exactly and must verify, got: {o}");
}

/// A *suffixed* literal keeps its own width, so a cast of it is not re-read from
/// the names in scope.
///
/// `128i16` is a sixteen-bit `128`, and `128i16 as i32` is `128` in Rust. Sizing
/// the literal from the scope instead -- eight bits here, the only names in scope
/// -- made `128i16` the bit pattern `0x80`, which the widening cast sign-extended
/// to `-128`, and *verified* `ensures: 128i16 as i32 == -128`, a postcondition Rust
/// calls false. The true counterpart (`== 128`) must verify, so the fix is not
/// "refuse every suffixed literal".
#[test]
fn a_suffixed_literal_keeps_its_own_width() {
    let Some(report) = verify_realistic(&[
        "contract_suffixed_literal_keeps_its_width",
        "contract_suffixed_literal_is_its_own_value",
    ]) else {
        return;
    };
    let o = outcome(&report, "contract_suffixed_literal_keeps_its_width");
    assert!(
        !o.is_verified(),
        "`128i16 as i32` is `128`, so `== -128` is false and must not verify, got: {o}"
    );
    let o = outcome(&report, "contract_suffixed_literal_is_its_own_value");
    assert!(o.is_verified(), "`128i16 as i32 == 128` is true and must verify, got: {o}");
}

/// A fold that touches a complement is a width-dependent bit pattern, so its
/// widening is refused; the valid masked and shifted shapes still read.
///
/// `!0 + 2` is `1` in Rust (the complement is `-1`), so
/// `(x as i16 + 2) == !0 + 2` is false; the reader held the complement at the
/// scope's width, added `2` to get `257`, and a later resize truncated it into
/// `i16`'s `1`, *verifying* the false equality. `!0 & 1` is `1` at any width and
/// `!0 >> 0` is `!0`, so both of those must still verify -- the refusal is drawn
/// at the width *depending* on the operand, not at the complement.
#[test]
fn a_complement_fold_widening_is_refused_but_masks_and_shifts_read() {
    let Some(report) = verify_realistic(&[
        "contract_complement_fold_widening_is_refused",
        "contract_complement_mask_widens_exactly",
        "contract_complement_shift_by_zero_reads",
        "contract_complement_division_with_typed_operand_reads",
    ]) else {
        return;
    };
    let o = outcome(&report, "contract_complement_fold_widening_is_refused");
    assert!(!o.is_verified(), "`(x as i16 + 2) == !0 + 2` is false and must not verify, got: {o}");
    for name in [
        "contract_complement_mask_widens_exactly",
        "contract_complement_shift_by_zero_reads",
        "contract_complement_division_with_typed_operand_reads",
    ] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "{name} is a valid contract and must verify, got: {o}");
    }
}

/// An untyped fold that *overflows* the operand width is refused, while a fold
/// that fits still reads.
///
/// `!0 + 1` is `256` at the scope's width; against a `u8` that is a rustc
/// `arithmetic operation will overflow` error (`!0` is `255`), but the reader
/// truncated the scope-width fold to the partner's width and read `(!0 + 1) < 1u8`
/// as `0 < 1` -- *verified* for a program rustc rejects. `(!0 + 2) == 1u8` failed
/// the same way (`257` truncated to `1`). Both must be refused. `200 + 100` is
/// `300` at any width holding it, so it is width-independent and must still read.
#[test]
fn an_untyped_fold_overflow_is_refused_but_a_fitting_fold_reads() {
    let Some(report) = verify_realistic(&[
        "contract_untyped_fold_overflow_is_refused",
        "contract_untyped_fold_overflow_equality_is_refused",
        "contract_fitting_fold_still_reads",
    ]) else {
        return;
    };
    for name in [
        "contract_untyped_fold_overflow_is_refused",
        "contract_untyped_fold_overflow_equality_is_refused",
    ] {
        let o = outcome(&report, name);
        assert!(
            !o.is_verified(),
            "{name} folds an untyped constant into a value that overflows the operand width \
             (`E0308`/overflow in Rust) and must be refused, got: {o}"
        );
    }
    let o = outcome(&report, "contract_fitting_fold_still_reads");
    assert!(
        o.is_verified(),
        "`200 + 100` is width-independent and must verify, got: {o}"
    );
}

/// A shift of an *untyped* constant whose width Rust infers is refused when it is
/// only valid at the scope's width, while a shift that is valid at every width
/// reads.
///
/// `255 >> 8` is fine at the scope's width (32, from a `u32` name in scope), but
/// the operand has no type of its own: against a `u8` partner `255` is a `u8`, and
/// a shift by its own width is a rustc `arithmetic operation will overflow`. The
/// reader verified `255 >> 8 <= x` on a program rustc rejects. `(255 >> 1) >= y`
/// for an `i8` `y` failed the same way (`255` is a rustc `literal out of range`).
/// Both must be refused. `1 << 4` is `16` at any width holding it, and a shift of a
/// typed name has a known width, so both must still read.
#[test]
fn an_untyped_shift_that_needs_a_wider_operand_is_refused_but_a_fitting_shift_reads() {
    let Some(report) = verify_realistic(&[
        "contract_untyped_shift_amount_over_narrow_width_is_refused",
        "contract_untyped_left_shift_amount_over_narrow_width_is_refused",
        "contract_untyped_shift_operand_out_of_narrow_range_is_refused",
        "contract_fitting_untyped_shift_still_reads",
        "contract_typed_shift_still_reads",
    ]) else {
        return;
    };
    for name in [
        "contract_untyped_shift_amount_over_narrow_width_is_refused",
        "contract_untyped_left_shift_amount_over_narrow_width_is_refused",
        "contract_untyped_shift_operand_out_of_narrow_range_is_refused",
    ] {
        let o = outcome(&report, name);
        assert!(
            !o.is_verified(),
            "{name} shifts an untyped constant by an amount (or with a value) that is only \
             valid at the scope's width, not the width Rust infers from the partner, and must \
             be refused, got: {o}"
        );
    }
    for name in [
        "contract_fitting_untyped_shift_still_reads",
        "contract_typed_shift_still_reads",
    ] {
        let o = outcome(&report, name);
        assert!(
            o.is_verified(),
            "{name} is a shift whose width is known or whose value fits every width, and must \
             verify, got: {o}"
        );
    }
}

/// A `u128`-suffixed literal is *refused*, not panicked.
///
/// The suffix records width 128, and the range check that guards the literal
/// computes `1u128 << w`. At `w == 128` that shift panics in a debug build (it
/// wraps to a wrong `max` in release, which then lets a 128-bit literal reach
/// `smt::word` -- itself an assertion). The refusal has to come *before* the
/// shift. The two literals here are both small, so the value is not the problem;
/// the *type* is. This pins that the reader reports the function as skipped
/// instead of taking the run down, which is what a panic does.
#[test]
fn a_u128_suffix_literal_is_refused_not_panicked() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nspec! {\n    requires: true,\n    ensures: 0u128 == 0u128,\n}\npub fn f(x: u8) -> u8 { x }";
    let Some(o) = verify_src("f", src) else { return };
    let FnOutcome::Skipped { reason } = o else {
        panic!("a `u128`-suffixed literal must be refused, got: {o}");
    };
    assert!(
        reason.contains("128") && reason.contains("64-bit"),
        "the refusal should name the width and the engine limit, got: {reason}"
    );
}

/// A *symbolic* masked operand keeps its type, so a signed/unsigned pairing is
/// refused rather than given a signed reading.
///
/// `x & 128` for a `u8` is a `u8`; masking does not change its operand's type.
/// Marking the result untyped (`Sign::Literal`) let `refuse_mixed_signedness`
/// accept `(x & 128) == y` for a signed `y` -- a pairing rustc rejects (E0308) --
/// and decide it as a signed comparison. The contract must be refused, and the
/// valid *constant* mask (`(!0 & 1)`, width-independent) must still read.
#[test]
fn a_symbolic_mask_keeps_its_type_but_a_constant_mask_still_widens() {
    let Some(report) = verify_realistic(&[
        "contract_symbolic_mask_keeps_its_type",
        "contract_complement_mask_widens_exactly",
    ]) else {
        return;
    };
    let o = outcome(&report, "contract_symbolic_mask_keeps_its_type");
    assert!(
        !o.is_verified(),
        "`(x & 128) == y` pairs a `u8` with an `i8` and must be refused, got: {o}"
    );
    let o = outcome(&report, "contract_complement_mask_widens_exactly");
    assert!(
        o.is_verified(),
        "a constant width-independent mask still widens and must verify, got: {o}"
    );
}

/// A constant whose *type is fixed* may not meet a partner of another width.
///
/// `-1i8` is an `i8`, so `-1i8 < (x as i16)` is `E0308` even though both operands
/// are signed; `refuse_mixed_signedness` passes them, so the width is the only
/// remaining evidence, and `align_widths` used to resize the constant and erase it
/// — *verifying* the contract. A *bare* negation (`-1`) has no width of its own, so
/// it still meets a wider operand. The mismatch and the valid control are asserted
/// together, because the rule must refuse only a fixed width.
#[test]
fn a_fixed_width_constant_may_not_meet_a_partner_of_another_width() {
    let Some(report) = verify_realistic(&[
        "contract_fixed_width_constant_vs_wider_cast_is_refused",
        "contract_two_fixed_width_constants_are_refused",
        "contract_bare_negation_meets_a_wider_operand",
    ]) else {
        return;
    };
    for name in [
        "contract_fixed_width_constant_vs_wider_cast_is_refused",
        "contract_two_fixed_width_constants_are_refused",
    ] {
        let o = outcome(&report, name);
        assert!(
            !o.is_verified(),
            "{name} compares a fixed-width constant with a differently-sized operand \
             (`E0308`) and must be refused, got: {o}"
        );
    }
    let o = outcome(&report, "contract_bare_negation_meets_a_wider_operand");
    assert!(
        o.is_verified(),
        "a bare negation infers its width and is well-typed, so it must verify, got: {o}"
    );
}

/// A *typed* constant mask keeps its type, so a pairing of the other signedness is
/// refused rather than decided — in **both** directions.
///
/// `-1i8 & 1` is an `i8` and `1u8 & 1` is a `u8`, even though both compute the
/// small positive `1`: the mask takes the typed operand's type and `==` never
/// coerces. Marking the constant result `Literal` hid the type, so a pairing with
/// the *other* signedness -- `E0308` in Rust -- was read as `1 == y` and
/// *verified*. The first fix excluded only a *signed* typed operand, leaving the
/// unsigned half open; both must be refused, and the same mask against a partner of
/// its own signedness must still verify.
#[test]
fn a_typed_constant_mask_keeps_its_type() {
    let Some(report) = verify_realistic(&[
        "contract_signed_mask_vs_unsigned_is_refused",
        "contract_signed_mask_with_signed_partner_reads",
        "contract_unsigned_mask_vs_signed_is_refused",
        "contract_unsigned_mask_with_unsigned_partner_reads",
    ]) else {
        return;
    };
    for name in [
        "contract_signed_mask_vs_unsigned_is_refused",
        "contract_unsigned_mask_vs_signed_is_refused",
    ] {
        let o = outcome(&report, name);
        assert!(!o.is_verified(), "{name} pairs a typed mask with the other signedness \
                                    (`E0308`) and must be refused, got: {o}");
    }
    for name in [
        "contract_signed_mask_with_signed_partner_reads",
        "contract_unsigned_mask_with_unsigned_partner_reads",
    ] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "{name} is well-typed and must verify, got: {o}");
    }
}

/// A pin is *transitive*: a right-hand side that names an earlier pinned variable is
/// pinned too, and the body folds as if the whole chain had been written as literals.
///
/// `a == 5 && b == a + 1 && c == b * 2` fixes `c` at `12` by transitivity, so `c / 4` is
/// `12 / 4` and the obligation is a fact about numbers. A single-pass extraction pinned
/// only `a`, left `b` and `c` symbolic, and built the full divider — `incomplete` however
/// small the contract was. The multiply is the second half of the shape: it makes the
/// *path condition* expensive even once every operand is a literal, which is what the
/// closed-goal check in `prove_implies` exists for.
///
/// The `cannot_prove_this` companion is the point: without it, an engine that answered
/// `Verified` for everything would pass, so the negative is what makes the positive a
/// proof rather than a vacuous `true`.
#[test]
fn a_transitive_pin_chain_folds_the_body() {
    let Some(report) = verify_realistic(&[
        "a_transitive_pin_chain_folds_the_body",
        "a_transitive_pin_chain_cannot_prove_this",
    ]) else {
        return;
    };
    let good = outcome(&report, "a_transitive_pin_chain_folds_the_body");
    assert!(good.is_verified(), "a transitive pin chain must fold the body; got: {good}");
    // The negative must be *refuted*, not merely "not verified". A refusal and a
    // counterexample are both "not verified", and only the second is evidence the
    // rewrite did anything: the point of rewriting the pins into the path is that the
    // multiply leaves the assumption and the false claim becomes decidable. Asserting
    // the weaker property would let a regression back to `incomplete` pass.
    let bad = outcome(&report, "a_transitive_pin_chain_cannot_prove_this");
    assert!(
        matches!(bad, FnOutcome::Failed { .. }),
        "a false claim about a transitively pinned chain must be refuted, not refused; got: {bad}"
    );
}

/// An unsigned division by a power of two is a shift, and the body over a *symbolic*
/// dividend is decided rather than refused for the divider's diagram.
///
/// The signed fast path existed; the unsigned side did not, so `n / 4` on a `u64` with
/// only a bound on `n` built the full restoring divider and came back `incomplete` in
/// ~0.9 s. The remainder is the other half of the same helper (`n & 3`), and it is here so
/// a future edit that wires only the quotient fails.
///
/// The `cannot_prove_this` companion is what keeps "decided" meaning "decided correctly":
/// without it, a fast path that returned a wrong value for every input could still pass.
#[test]
fn an_unsigned_power_of_two_division_is_a_shift_not_a_divider() {
    let Some(report) = verify_realistic(&[
        "an_unsigned_power_of_two_division_is_decided",
        "an_unsigned_power_of_two_division_cannot_prove_this",
        "an_unsigned_power_of_two_remainder_is_decided",
    ]) else {
        return;
    };
    for name in [
        "an_unsigned_power_of_two_division_is_decided",
        "an_unsigned_power_of_two_remainder_is_decided",
    ] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "an unsigned power-of-two op must be decided; `{name}` got: {o}");
    }
    let bad = outcome(&report, "an_unsigned_power_of_two_division_cannot_prove_this");
    assert!(
        !bad.is_verified(),
        "a false claim about an unsigned division must be refuted; got: {bad}"
    );
}

// ---------------------------------------------------------------------------
// Recursion by contract
// ---------------------------------------------------------------------------

/// A recursive function is proved by induction, not by unrolling.
///
/// The self-call is answered by the function's own contract: its precondition is
/// proved of `n - 1`, the `decreases:` measure is proved to shrink, and the
/// postcondition is assumed for the result. The base case is proved by the body,
/// which is what makes the induction well-founded rather than circular.
///
/// Before `decreases:` existed, this function hit `DEFAULT_INLINE_DEPTH` and came
/// back `Incomplete`, so a proof here is the whole point of the feature.
#[test]
fn a_recursive_function_is_proved_by_its_decreases_measure() {
    let Some(report) = verify_realistic(&["a_recursive_identity"]) else { return };
    let o = outcome(&report, "a_recursive_identity");
    assert!(
        o.is_verified(),
        "a recursive function with a decreasing measure must be proved by induction, got: {o}"
    );
}

/// The inductive assumption must not rescue a body that is simply wrong.
///
/// `a_recursive_lie_cannot_prove_this` has `a_recursive_identity`'s contract but a
/// body that returns `n + 1` in the recursive case. If the postcondition were
/// assumed at the base case too -- or if the `return` obligations were skipped --
/// it would come back `verified`, which is a false proof. It must be a definite
/// failure, with a counterexample.
#[test]
fn a_recursive_body_that_does_not_establish_its_postcondition_is_refuted() {
    let Some(report) = verify_realistic(&["a_recursive_lie_cannot_prove_this"]) else { return };
    let o = outcome(&report, "a_recursive_lie_cannot_prove_this");
    let FnOutcome::Failed { kind, .. } = o else {
        panic!("a recursive body that breaks its postcondition must fail, got: {o}");
    };
    assert_eq!(kind, "postcondition", "got: {o}");
}

/// An assumption with no decreasing measure is circular, so it must be refused.
///
/// `a_recursive_call_that_does_not_decrease_cannot_prove_this` calls itself on the
/// same `n`. Its precondition and postcondition are both true of the body, so
/// nothing but the measure check stands between it and a false proof. It must not
/// verify.
#[test]
fn a_recursive_call_without_a_decreasing_measure_is_not_assumed() {
    let Some(report) = verify_realistic(&["a_recursive_call_that_does_not_decrease_cannot_prove_this"])
    else {
        return;
    };
    let o = outcome(&report, "a_recursive_call_that_does_not_decrease_cannot_prove_this");
    assert!(
        !o.is_verified(),
        "an induction without a decreasing measure must not be assumed, got: {o}"
    );
}

/// The recursive call's *precondition* is an obligation, not a formality.
///
/// `a_recursive_call_out_of_range_cannot_prove_this` has `requires: n <= 3` and
/// calls itself on `n + 1`, leaving that range. Proving the precondition at the
/// call is what keeps the induction hypothesis applicable only where the contract
/// holds; without it the postcondition would be assumed for an input the function
/// never promised anything about.
#[test]
fn a_recursive_call_must_satisfy_its_own_precondition() {
    let Some(report) =
        verify_realistic(&["a_recursive_call_out_of_range_cannot_prove_this"])
    else {
        return;
    };
    let o = outcome(&report, "a_recursive_call_out_of_range_cannot_prove_this");
    assert!(
        !o.is_verified(),
        "a recursive call that violates its own precondition must not be assumed, got: {o}"
    );
}

/// A contract on an *inherent method* is resolved, and its postcondition is
/// actually checked.
///
/// The MIR dump names a method's body `<impl at FILE:L:C: L:C>::bump`, but the
/// source scanner keys the contract by the bare `bump`. Resolving only the exact
/// dump name matched nothing, and a missing contract is not a refusal:
/// `verify_function` derives `requires` and `ensures` from it, so the method was
/// checked against `true` and *no* postcondition and came back `verified` with
/// **zero obligations**. The obligation count is the load-bearing part of this
/// assertion -- a fix that merely matched the name but skipped the body would
/// still report `Verified`, and only the count catches that.
#[test]
fn a_contract_on_an_inherent_method_is_resolved_and_checked() {
    let Some(report) = verify_realistic(&["bump"]) else { return };
    let o = outcome(&report, "bump");
    let FnOutcome::Verified { obligations, .. } = o else {
        panic!("a method whose contract holds must verify, got: {o}");
    };
    assert!(
        *obligations > 0,
        "the method's postcondition must be checked, not silently dropped; got {obligations} \
         obligation(s)"
    );
}

/// The false direction: a method whose postcondition the body does not establish
/// must be refuted, never `verified`.
///
/// `bump_cannot_prove_this` has `bump`'s body but demands `ret == self.n + 2`, so
/// the claim is false on every input. Before the tail resolution this was
/// `Verified { obligations: 0 }` -- a false proof, because the contract never
/// reached the checker. The failure kind is pinned to the postcondition so a fix
/// that refused the method for an unrelated reason cannot pass this.
#[test]
fn a_contract_on_an_inherent_method_that_is_false_is_refuted() {
    let Some(report) = verify_realistic(&["bump_cannot_prove_this"]) else { return };
    let o = outcome(&report, "bump_cannot_prove_this");
    let FnOutcome::Failed { kind, .. } = o else {
        panic!("a method whose postcondition is false must fail, got: {o}");
    };
    assert_eq!(kind, "postcondition", "the failure must be the postcondition, got `{kind}`");
}

// ---------------------------------------------------------------------------
// Frontend soundness regressions (F1-F4)
//
// Each of these four was a confirmed defect in the MIR frontend, found by probe
// and fixed in the same change. They are pinned here so a later refactor cannot
// quietly reopen them.

/// F1: a free function named exactly like an intrinsic that calls the *intrinsic*
/// is not calling itself.
///
/// `wrapping_add` calls `u8::wrapping_add` on its non-base path. The intrinsic is
/// printed fully qualified (`core::num::<impl u8>::wrapping_add`), and the old
/// resolver matched call names to definitions on the last `::` component alone,
/// so the intrinsic call resolved to *this function*. `try_recursive_call` then
/// assumed this function's own postcondition for the result -- a genuine false
/// proof: `ret == 0` was "proved" while the body returns `n` for `n > 0`. The
/// resolver now also requires the method's `self` type to agree, so `u8` and the
/// free function are distinct and the intrinsic model handles the call.
///
/// The name must be the intrinsic's *exact* tail, and the function needs a
/// `decreases:` measure, or the recursion route that carries the false proof never
/// fires -- a name like `wrapping_add_cannot_prove_this` does not reproduce it.
#[test]
fn an_intrinsic_call_is_not_a_self_recursive_call() {
    let Some(report) = verify_realistic(&["wrapping_add"]) else { return };
    let o = outcome(&report, "wrapping_add");
    let FnOutcome::Failed { kind, .. } = o else {
        panic!(
            "a function named like an intrinsic that calls the intrinsic must be \
             refuted, not proved from its own postcondition; got: {o}"
        );
    };
    assert_eq!(kind, "postcondition", "the failure must be the postcondition, got `{kind}`");
}

/// F1 companion: a free function named like an intrinsic that *does* recurse must
/// still be proved by induction.
///
/// The guard that fixes F1 must not refuse a real self-call just because the name
/// matches an intrinsic. `wrapping_add_still_recurses` calls itself, printed bare
/// (`wrapping_add_still_recurses`), which matches it by exact name.
#[test]
fn a_free_recursion_named_like_an_intrinsic_still_recurses() {
    let Some(report) = verify_realistic(&["wrapping_add_still_recurses"]) else { return };
    let o = outcome(&report, "wrapping_add_still_recurses");
    assert!(
        o.is_verified(),
        "a free function named like an intrinsic that recurses must verify; got: {o}"
    );
}

/// F1 companion: a *static associated function* named like an intrinsic that
/// genuinely recurses must still be proved by induction.
///
/// The type check that fixes F1 must not refuse a real self-call just because the
/// tail matches an intrinsic name. `Recursing::wrapping_sub` calls itself, and a
/// `core` intrinsic's call name carries an `<impl` projection that the resolver
/// refuses, so the real self-call still fires.
#[test]
fn a_method_named_like_an_intrinsic_still_recurses() {
    let Some(report) = verify_realistic(&["wrapping_sub"]) else { return };
    let o = outcome(&report, "wrapping_sub");
    assert!(o.is_verified(), "a method named like an intrinsic that recurses must verify; got: {o}");
}

/// F4: a recursive call evaluates the callee's contract with the entry `const`
/// table, so a contract naming a constant resolves at the self-call.
///
/// `a_recursive_contract_naming_a_const_is_resolved` names `LIMIT` in both its
/// `requires` and its `ensures`. The recursive call rebuilt the contract
/// environment from `FieldEnv::default()`, whose `const` table is empty, so the
/// `requires` was unreadable *at the self-call* and the whole function came back
/// `Skipped` -- a lost proof. The call site now carries the entry constants.
#[test]
fn a_recursive_contract_naming_a_const_is_resolved() {
    let Some(report) = verify_realistic(&["a_recursive_contract_naming_a_const_is_resolved"])
    else {
        return;
    };
    let o = outcome(&report, "a_recursive_contract_naming_a_const_is_resolved");
    assert!(
        o.is_verified(),
        "a recursive contract naming a crate-level const must resolve at the self-call; got: {o}"
    );
}

/// F3: a crate-level `const` a *contract* names but no body uses is kept.
///
/// The dump's bare-use test is silent for a constant with no use site at all, so
/// the definition was dropped and the contract went unreadable -- a lost proof.
/// The source says the item is module-scoped, and `spec::crate_level_consts`
/// carries that to the parser.
#[test]
fn a_contract_only_const_is_resolved() {
    let Some(report) = verify_realistic(&["a_contract_only_const_is_resolved"]) else { return };
    let o = outcome(&report, "a_contract_only_const_is_resolved");
    assert!(
        o.is_verified(),
        "a crate-level const named only by a contract must resolve; got: {o}"
    );
}

/// F2: a string literal that spells `const NAME` must not leak a function-local
/// `const` into the program-wide table.
///
/// The bare-use scan reads `const NAME` out of the dump to tell a crate-level
/// constant from a function-local one. A `debug s => const "const LEAKY";`
/// binding and the `alloc` block behind the string spell those letters as *data*,
/// so a scan of the raw line saw a use and gave the local `LEAKY` a program-wide
/// key -- reopening the exact false proof
/// `a_local_const_is_not_in_scope_in_another_function` guards. The scan now drops
/// string literals and skips `alloc` blocks, so the contract's `LEAKY` resolves to
/// nothing and the function is `Skipped`, never `Verified`.
///
/// The local name is *unique* among the file's locals, which is what makes rustc
/// print its definition bare (`const LEAKY`) -- the spelling that collides with a
/// crate-level item. A name shared by two locals is printed qualified and does not
/// reproduce the shape.
#[test]
fn a_string_literal_does_not_leak_a_local_const() {
    let Some(report) =
        verify_realistic(&["a_string_literal_must_not_leak_a_unique_local_const"])
    else {
        return;
    };
    let o = outcome(&report, "a_string_literal_must_not_leak_a_unique_local_const");
    assert!(
        !o.is_verified(),
        "a string literal must not leak a function-local const into a contract; got: {o}"
    );
}

/// A local trait whose `from` shares `From`'s name must be answered by its own
/// body, never by the conversion model.
///
/// The model refuses a module-qualified trait path (`conv_shim::From`) by name, so
/// a call to it reaches the same-dump lookup. This is the false-proof direction:
/// the body returns `7`, so `ret == x` is false at `x == 0`. If the model answered
/// the call it would return `x` and *prove* the claim -- exactly the mistake the
/// trait-path check exists to prevent. A module is used (not a crate-root trait)
/// because a local `impl From<u8> for u16` at the crate root has a *separate*,
/// pre-existing hazard in `resolve_callee`'s owner-type match.
#[test]
fn a_local_trait_from_is_answered_by_its_body() {
    let src = r#"macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }

pub mod conv_shim {
    pub trait From<T> {
        fn from(t: T) -> Self;
    }
    impl From<u8> for u16 {
        fn from(_t: u8) -> u16 {
            7
        }
    }
}
spec! {
    requires: x == 0,
    ensures: ret == x as u16,
}
pub fn local_from_is_not_the_model(x: u8) -> u16 {
    conv_shim::From::from(x)
}
"#;
    let Some(o) = verify_src("local_from_is_not_the_model", src) else { return };
    let FnOutcome::Failed { ref kind, .. } = o else {
        panic!("the local trait's body must refute the claim, got: {o}");
    };
    assert_eq!(kind, "postcondition", "got: {o}");
}

/// A widening conversion agrees with a plain `as` cast of the same shape.
///
/// `u16::from(x)` and `x as u16` are the same widening, so their results must be
/// one term. The model builds the conversion through the same extension the cast
/// arm uses; if the two ever disagreed, `ret == x as u16` would be refuted here.
#[test]
fn a_conversion_agrees_with_a_cast_of_the_same_shape() {
    let src = r#"macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }

spec! {
    requires: true,
    ensures: ret == x as u16,
}
pub fn from_matches_cast(x: u8) -> u16 {
    u16::from(x)
}
spec! {
    requires: true,
    ensures: ret == x as i32,
}
pub fn into_signed_matches_cast(x: i8) -> i32 {
    x.into()
}
"#;
    let Some(o) = verify_src("from_matches_cast", src) else { return };
    assert!(o.is_verified(), "`u16::from(x)` must agree with `x as u16`; got: {o}");
    let Some(o) = verify_src("into_signed_matches_cast", src) else { return };
    assert!(o.is_verified(), "`x.into()` must agree with `x as i32`; got: {o}");
}

/// A call to an *unmodelled* conversion is reported unknown, not guessed.
///
/// `u32::from('a')` is a real `From` impl, but the source is a `char`, whose
/// conversion is not one of the integer widenings the model reads -- so it is
/// refused (rather than guessing a width from the destination alone), and the path
/// is reported unknown. This pins that the refusal is a *reported* gap rather than
/// a silent approximation. (`u8::from(bool)` used to be the example; it is now
/// modelled as the zero-extended bit.)
#[test]
fn an_unmodelled_conversion_is_reported_unknown_not_guessed() {
    // `u32::from(char)` is a real `From` impl whose source is not an integer the
    // engine holds (`char` is modelled as a 32-bit value, but a `char` *conversion*
    // is not one of the integer widenings), so it stays unmodelled. `u8::from(bool)`
    // used to be the example here and is now modelled (the zero-extended bit), so
    // the test names a conversion that is still a refusal.
    let src = r#"macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }

spec! {
    requires: true,
    ensures: ret == 1,
}
pub fn from_char_is_unmodelled(c: char) -> u32 {
    u32::from(c)
}
"#;
    let Some(o) = verify_src("from_char_is_unmodelled", src) else { return };
    assert!(
        !o.is_verified(),
        "a conversion the model does not hold must not verify; got: {o}"
    );
    let FnOutcome::Incomplete { .. } = o else {
        panic!("an unmodelled conversion must be `Incomplete`, got: {o}");
    };
}

/// F3: a call to an *external* static associated function must not be answered by
/// a local method that merely shares its tail.
///
/// A static associated function has no `self`, so its definition name
/// `<impl at FILE:L:C: L:C>::m` names no type the way an instance method's
/// receiver does. The old resolver accepted such a candidate on the unique tail
/// alone, so `<u16 as From<u8>>::from(x)` -- `core`'s conversion, which returns
/// `x` -- matched `StaticOwner::from`, whose body returns 99. The contract
/// `ret == 99` then came back verified, a genuine false proof.
///
/// The owner type now comes from the `impl` block's own source line, keyed by the
/// location the dump prints, so the external call no longer matches the local
/// method. The verdict is no longer `Verified`, and it is no longer merely
/// `Incomplete` either: the intrinsic model now *answers* the conversion, so the
/// fixture is refuted with a witness (`x == 0`, where `u16::from(x)` is `0`, not
/// `99`). That is the stronger evidence -- the call is answered by `core`'s own
/// conversion, and the local `from` is not consulted at all.
#[test]
fn an_external_static_call_is_not_answered_by_a_local_method() {
    let Some(report) = verify_realistic(&["a_static_call_resolves_to_its_own_impl_cannot_prove_this"])
    else {
        return;
    };
    let o = outcome(&report, "a_static_call_resolves_to_its_own_impl_cannot_prove_this");
    assert!(
        !o.is_verified(),
        "a call to `u16::from` must not be answered by a local `from`; got: {o}"
    );
    let FnOutcome::Failed { kind, .. } = o else {
        panic!("the modelled conversion must refute the fixture, got: {o}");
    };
    assert_eq!(kind, "postcondition", "got: {o}");
}

/// F3 companion: the owner-type check must not refuse a call to a *local* static
/// method, which does resolve to its own body.
#[test]
fn a_local_static_call_resolves_to_its_impl() {
    let Some(report) = verify_realistic(&["a_local_static_call_resolves_to_its_impl"]) else {
        return;
    };
    let o = outcome(&report, "a_local_static_call_resolves_to_its_impl");
    assert!(o.is_verified(), "a local static method call must resolve and verify; got: {o}");
}

/// F4: two `impl` blocks that each declare a method with the same tail must not
/// share a contract.
///
/// A method's contract was keyed by its bare name, so `TaggedLeft::tag` and
/// `TaggedRight::tag` produced one entry and the later overwrote the earlier. Both
/// methods then resolved to the survivor's claim: a lost proof for the one whose
/// contract was dropped, and -- once the claims are the false directions -- a
/// false proof for the other. The key is now the `impl`'s location.
///
/// Both methods are true here and must verify, which pins that the resolution
/// found *a* contract rather than dropping the method (a missing contract is not a
/// refusal: it means `requires: true` and no `ensures`).
#[test]
fn two_impls_with_a_shared_method_tail_keep_separate_contracts() {
    let Some(report) = verify_realistic(&["tag"]) else { return };
    for key in ["TaggedLeft::tag", "TaggedRight::tag"] {
        let o = outcome(&report, key);
        assert!(o.is_verified(), "{key} must be checked against its own contract; got: {o}");
    }
}

/// F4 companion: with the claims the false way round, each method must be
/// refuted by *its own* postcondition -- never "proved" by the other impl's.
///
/// The two `tag_cannot_prove_this` methods are the mirrors of each other: the left
/// body returns 1 and claims 2, the right returns 2 and claims 1. If the two
/// contracts were conflated, one of them would look true. Each must fail on the
/// postcondition.
#[test]
fn two_impls_with_a_shared_method_tail_do_not_conflate_contracts() {
    let Some(report) = verify_realistic(&["tag_cannot_prove_this"]) else { return };
    for key in ["TaggedLeft::tag_cannot_prove_this", "TaggedRight::tag_cannot_prove_this"] {
        let o = outcome(&report, key);
        let FnOutcome::Failed { kind, .. } = o else {
            panic!("{key} must be refuted by its own postcondition, got: {o}");
        };
        assert_eq!(kind, "postcondition", "{key}: the failure must be the postcondition, got `{kind}`");
    }
}

// ---------------------------------------------------------------------------
// Path-aware contract resolution (F1/F2/F3/F4)
//
// rustc names a function's *definition* by the shortest crate-unique path suffix,
// not by its bare name: a free function inside `a` is `a::f` when two modules
// declare `f`, a trait's default method is `T::value`, and a function nested in a
// function is `f::g`. The contract table used to key every non-method contract by
// the bare source name and match only the exact MIR name, so any nested item's
// contract was *dropped* -- and a dropped contract is not a refusal: the function
// was built with `requires: true` and no `ensures`, and a false postcondition was
// reported `Verified { obligations: 0 }`. Each test below is a false postcondition
// that must be *refuted*, or a pair of same-named items that must not share a
// contract.
// ---------------------------------------------------------------------------

/// A trait's default method must be checked against its own contract.
///
/// The MIR prints the definition `T::value`, but the contract was keyed `value`,
/// so it was dropped and the false postcondition verified with zero obligations.
#[test]
fn a_trait_default_method_is_checked_against_its_contract() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\npub trait T {\n    spec! {\n        ensures: ret == 9,\n    }\n    fn value(&self) -> u8 { 7 }\n}\n";
    let o = verify_src("T::value", src).expect("T::value must be verified, not dropped");
    let FnOutcome::Failed { kind, .. } = o else {
        panic!("a trait default with a false postcondition must be refuted, got: {o}");
    };
    assert_eq!(kind, "postcondition", "the failure must be the postcondition, got `{kind}`");
}

/// A free function nested in a module must be checked against its own contract.
///
/// The MIR prints `m::f`, but the contract was keyed `f`, so the false
/// postcondition verified with zero obligations.
#[test]
fn a_module_nested_free_function_is_checked_against_its_contract() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\npub mod m {\n    spec! {\n        ensures: ret == 9,\n    }\n    pub fn f() -> u8 { 7 }\n}\n";
    let o = verify_src("m::f", src).expect("m::f must be verified, not dropped");
    let FnOutcome::Failed { kind, .. } = o else {
        panic!("a module function with a false postcondition must be refuted, got: {o}");
    };
    assert_eq!(kind, "postcondition", "the failure must be the postcondition, got `{kind}`");
}

/// Two sibling modules that each declare `f` must not share one contract.
///
/// A bare key let the second `f` overwrite the first, so one module's body was
/// checked against the other's claim: a lost proof for one, a false proof for the
/// other. Here the left claim is false and the right true, and each must be judged
/// by its own.
#[test]
fn two_sibling_modules_do_not_share_a_contract() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\npub mod a {\n    spec! {\n        ensures: ret == 9,\n    }\n    pub fn f() -> u8 { 1 }\n}\npub mod b {\n    spec! {\n        ensures: ret == 2,\n    }\n    pub fn f() -> u8 { 2 }\n}\n";
    let left = verify_src("a::f", src).expect("a::f must be present");
    let FnOutcome::Failed { kind, .. } = left else {
        panic!("a::f has a false postcondition and must be refuted, got: {left}");
    };
    assert_eq!(kind, "postcondition", "a::f: the failure must be the postcondition, got `{kind}`");
    let right = verify_src("b::f", src).expect("b::f must be present");
    assert!(right.is_verified(), "b::f's own contract is true and must verify, got: {right}");
}

/// A contracted function whose body `#[cfg]` removes must not be paired with a
/// *different* function's body and reported `Verified`.
///
/// The contract key comes from the source, but `b::a::f` is not compiled, so its
/// suffix (`a::f`) lands on the compiled `a::f` in the dump. The unique-suffix
/// fallback then checked `a::f`'s body against `b::a::f`'s contract and answered
/// `Verified` -- a claim about a function that was never compiled, and a silent
/// concealment of the fact that it was never checked. Ambiguity must be refused
/// in *both* directions: a key that several bodies suffix-match, and a body that
/// several keys suffix-match.
#[test]
fn a_cfg_excluded_contract_does_not_steal_a_compiled_body() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\npub mod a {\n    spec! {\n        ensures: ret == 1,\n    }\n    pub fn f() -> u8 { 1 }\n}\npub mod b {\n    pub mod a {\n        spec! {\n            ensures: ret == 1,\n        }\n        #[cfg(any())]\n        pub fn f() -> u8 { 2 }\n    }\n}\n";
    let rustc = rustc().expect("rustc");
    let dir = std::env::temp_dir().join(format!("verif-cfg-steal-{}", std::process::id()));
    let _ = std::fs::create_dir_all(&dir);
    let file = dir.join("probe.rs");
    std::fs::write(&file, src).expect("write probe");
    let report = verify::verify_file_with_opts(&rustc, &file, &[], &probe_opts()).expect("frontend");
    let _ = std::fs::remove_dir_all(&dir);
    assert!(
        report.outcomes.is_empty(),
        "no body may be paired with the excluded `b::a::f`'s contract, got: {:?}",
        report.outcomes
    );
    for name in ["a::f", "b::a::f"] {
        assert!(
            report.missing.contains(&name.to_string()),
            "`{name}` must be reported missing rather than silently verified, got: {:?}",
            report.missing
        );
    }
}

/// The `cfg` case's neighbour: a genuine nested free function is still resolved
/// by the unique-suffix rule, so the refusal above is a narrowing, not a blanket
/// refusal of any qualified key.
#[test]
fn a_nested_free_function_is_still_resolved_by_its_suffix() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\npub mod a {\n    spec! {\n        ensures: ret == 9,\n    }\n    pub fn f() -> u8 { 1 }\n}\n";
    let o = verify_src("a::f", src).expect("a::f must be present");
    let FnOutcome::Failed { kind, .. } = o else {
        panic!("a::f has a false postcondition and must be refuted, got: {o}");
    };
    assert_eq!(kind, "postcondition", "a::f: the failure must be the postcondition, got `{kind}`");
}

/// A trait default and a free function that share a name must keep separate
/// contracts: `A::v` is the trait's, `v` the function's.
#[test]
fn a_trait_default_and_a_free_function_do_not_share_a_contract() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nspec! {\n    ensures: ret == 9,\n}\npub fn v() -> u8 { 1 }\npub trait A {\n    spec! {\n        ensures: ret == 2,\n    }\n    fn v(&self) -> u8 { 2 }\n}\n";
    let free = verify_src("v", src).expect("v must be present");
    let FnOutcome::Failed { kind, .. } = free else {
        panic!("the free `v` has a false postcondition and must be refuted, got: {free}");
    };
    assert_eq!(kind, "postcondition", "v: the failure must be the postcondition, got `{kind}`");
    let method = verify_src("A::v", src).expect("A::v must be present");
    assert!(method.is_verified(), "the trait default's own contract is true, got: {method}");
}

/// A contract on a *trait implementation's* method is resolved and checked.
///
/// The review flagged this as a possible drop: the premise was that rustc prints
/// the implementation as `<Type as Trait>::method`, a name the location-keyed
/// resolver would not match. It does not reproduce on any supported toolchain --
/// rustc prints a same-crate implementation as `<impl at FILE:L:C: L:C>::method`
/// (and a default as `Trait::method`), and that location is exactly what the
/// method key carries. The pin keeps both directions: the true contract verifies
/// and the false one is refuted, so a future naming change cannot quietly turn
/// this into a dropped contract.
#[test]
fn a_contract_on_a_trait_implementation_is_checked() {
    let header = "pub trait Stow { fn value(&self) -> u8; }\npub struct Slot;\n";
    let truth = format!(
        "{header}impl Stow for Slot {{\n    /// ensures: ret == 7\n    fn value(&self) -> u8 {{ 7 }}\n}}\n"
    );
    let o = verify_src("value", &truth).expect("the trait impl's method must be present");
    assert!(o.is_verified(), "`ret == 7` with a body of 7 must verify, got: {o}");
    let falsehood = format!(
        "{header}impl Stow for Slot {{\n    /// ensures: ret == 9\n    fn value(&self) -> u8 {{ 7 }}\n}}\n"
    );
    let o = verify_src("value", &falsehood).expect("the trait impl's method must be present");
    let FnOutcome::Failed { kind, .. } = o else {
        panic!("`ret == 9` with a body of 7 must be refuted, got: {o}");
    };
    assert_eq!(kind, "postcondition", "the failure must be the postcondition, got `{kind}`");
}

/// F1: an `impl` header whose `{` is on the next line still owns its methods.
///
/// A brace-on-the-next-line header raises no depth on its own line, so a pop
/// keyed on "depth has not increased" dropped it before its methods and each
/// contract was keyed as a free function's -- which then never matched the
/// `<impl at …>::m` name, and the false postcondition verified with zero
/// obligations.
#[test]
fn a_multiline_impl_header_still_owns_its_methods() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\npub struct Foo;\nimpl Foo\n{\n    spec! {\n        ensures: ret == 9,\n    }\n    pub fn m(&self) -> u8 { 1 }\n}\n";
    let o = verify_src("m", src).expect("m must be present");
    let FnOutcome::Failed { kind, .. } = o else {
        panic!("a method of a multiline impl with a false postcondition must be refuted, got: {o}");
    };
    assert_eq!(kind, "postcondition", "the failure must be the postcondition, got `{kind}`");
}

// ---------------------------------------------------------------------------
// A trait declaration's contract
//
// A trait method declaration has no body, so rustc emits no MIR for it and it can
// never be a target on its own. Its contract is a promise every implementation
// inherits, and the implementation is where it is kept or broken. Before this,
// the declaration's contract was filed under the free-function table, where its
// `Trait::method` key suffix-matched an *unrelated* free function of the same
// name -- so a free `fn bump() -> u8 { 9 }` was checked against the trait's
// `ensures: ret == 9`, passed, and was reported `Bump::bump: Verified`. That reads
// as "the trait's promise is discharged" when no implementation was ever checked.
// ---------------------------------------------------------------------------

/// An implementation that breaks the trait's declared postcondition is refuted.
#[test]
fn an_implementation_that_breaks_its_traits_contract_is_refuted() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\npub trait Bump {\n    spec! {\n        ensures: ret == 9,\n    }\n    fn bump(&self) -> u8;\n}\npub struct S;\nimpl Bump for S { fn bump(&self) -> u8 { 7 } }\n";
    let (outcomes, missing) = verify_all_outcomes(src);
    assert!(missing.is_empty(), "the impl carries the promise, so nothing is missing: {missing:?}");
    let o = pick(&outcomes, "bump");
    let FnOutcome::Failed { kind, .. } = o else {
        panic!("a body of 7 against `ret == 9` must be refuted, got: {o}");
    };
    assert_eq!(kind, "postcondition", "the failure must be the postcondition, got `{kind}`");
}

/// The same implementation with the promise kept must verify, so the test above
/// pins the refutation and not a blanket failure.
#[test]
fn an_implementation_that_keeps_its_traits_contract_verifies() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\npub trait Bump {\n    spec! {\n        ensures: ret == 9,\n    }\n    fn bump(&self) -> u8;\n}\npub struct S;\nimpl Bump for S { fn bump(&self) -> u8 { 9 } }\n";
    let (outcomes, _) = verify_all_outcomes(src);
    let o = pick(&outcomes, "bump");
    assert!(o.is_verified(), "a body of 9 against `ret == 9` must verify, got: {o}");
}

/// A free function that shares the trait method's name is not the trait's method
/// and must not be judged by the trait's contract.
///
/// This is the false proof the separate table closes: the free `bump` returns 9,
/// which satisfies the trait's claim, so before the fix the report said
/// `Bump::bump: Verified` -- naming the *trait method* as verified although no
/// implementation existed in the file at all.
#[test]
fn a_free_function_does_not_borrow_a_trait_declarations_contract() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\npub trait Bump {\n    spec! {\n        ensures: ret == 9,\n    }\n    fn bump(&self) -> u8;\n}\npub fn bump() -> u8 { 9 }\n";
    let (outcomes, missing) = verify_all_outcomes(src);
    for (key, _) in &outcomes {
        assert_ne!(
            key, "Bump::bump",
            "the trait declaration has no body, so it can never be reported as a verified \
             function: {outcomes:?}"
        );
    }
    assert!(
        missing.iter().any(|m| m.starts_with("Bump::bump")),
        "the trait's promise has no implementation here, which must be visible rather than \
         silently unenforced; missing was {missing:?}"
    );
}

/// A trait declaration with no implementation in the file is reported, not dropped.
///
/// It has no MIR, so it can never be a target; leaving it unmentioned would make
/// the run clean while the claim was never checked.
#[test]
fn a_trait_declaration_with_no_implementation_is_reported() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\npub trait Bump {\n    spec! {\n        ensures: ret == 9,\n    }\n    fn bump(&self) -> u8;\n}\n";
    let (outcomes, missing) = verify_all_outcomes(src);
    assert!(outcomes.is_empty(), "there is no body to verify, got: {outcomes:?}");
    assert!(
        missing.iter().any(|m| m.starts_with("Bump::bump")),
        "an unenforced trait contract must be reported, got: {missing:?}"
    );
}

/// When the impl *and* the trait both declare a contract, the run refuses.
///
/// `resolve` can return only one of the two, so whichever it dropped would vanish
/// without a word. The refusal names both sides instead.
#[test]
fn two_contracts_for_one_method_are_refused_not_silently_chosen() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\npub trait Bump {\n    spec! {\n        ensures: ret == 9,\n    }\n    fn bump(&self) -> u8;\n}\npub struct S;\nimpl Bump for S {\n    spec! {\n        ensures: ret == 7,\n    }\n    fn bump(&self) -> u8 { 7 }\n}\n";
    let (outcomes, _) = verify_all_outcomes(src);
    let o = pick(&outcomes, "bump");
    let FnOutcome::Skipped { reason } = o else {
        panic!("two contracts for one function must be refused, got: {o}");
    };
    assert!(
        reason.contains("Bump") && reason.contains("contract"),
        "the refusal must name the trait and say what conflicts, got: `{reason}`"
    );
}

/// Two implementations of one contracted trait are both checked, and stay
/// distinguishable in the report.
///
/// Both are keyed by the bare tail before the fix, so one `outcomes` entry hid the
/// other: one body's verdict was simply not reported.
#[test]
fn two_implementations_of_a_contracted_trait_are_both_checked() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\npub trait Bump {\n    spec! {\n        ensures: ret == 9,\n    }\n    fn bump(&self) -> u8;\n}\npub struct A; pub struct B;\nimpl Bump for A { fn bump(&self) -> u8 { 7 } }\nimpl Bump for B { fn bump(&self) -> u8 { 9 } }\n";
    let (outcomes, missing) = verify_all_outcomes(src);
    assert!(missing.is_empty(), "both impls carry the promise: {missing:?}");
    let a = pick(&outcomes, "A::bump");
    let b = pick(&outcomes, "B::bump");
    let FnOutcome::Failed { kind, .. } = a else {
        panic!("`A::bump` returns 7 against `ret == 9` and must be refuted, got: {a}");
    };
    assert_eq!(kind, "postcondition", "the failure must be the postcondition, got `{kind}`");
    assert!(b.is_verified(), "`B::bump` returns 9 and must verify, got: {b}");
}

/// A trait with no contract contributes nothing, so an impl that declares its own
/// contract is checked by that one alone.
#[test]
fn an_impl_of_an_uncontracted_trait_keeps_its_own_contract() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\npub trait Bump { fn bump(&self) -> u8; }\npub struct S;\nimpl Bump for S {\n    spec! {\n        ensures: ret == 7,\n    }\n    fn bump(&self) -> u8 { 7 }\n}\n";
    let (outcomes, _) = verify_all_outcomes(src);
    let o = pick(&outcomes, "bump");
    assert!(o.is_verified(), "the impl's own `ret == 7` with a body of 7 must verify, got: {o}");
}

/// A trait default whose *signature* contains a `;` -- `[u8; 4]` -- is still a
/// function, not a declaration.
///
/// The body detector stops at the first `;` outside brackets, and the `;` of an
/// array type comes before the body brace. Reading it as the declaration's
/// terminator filed this default as bodyless: its contract would then be inherited
/// by implementations (there are none here) and its own false postcondition would
/// never be checked, while the run still looked clean.
#[test]
fn a_trait_default_returning_an_array_is_still_checked() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\npub trait T {\n    spec! {\n        ensures: ret == 9,\n    }\n    fn v(&self, xs: [u8; 4]) -> u8 { 7 }\n}\n";
    let (outcomes, missing) = verify_all_outcomes(src);
    assert!(missing.is_empty(), "a default has a body, so nothing is missing: {missing:?}");
    let o = pick(&outcomes, "T::v");
    let FnOutcome::Failed { kind, .. } = o else {
        panic!("a default whose `ret` is 7 against `ret == 9` must be refuted, got: {o}");
    };
    assert_eq!(kind, "postcondition", "the failure must be the postcondition, got `{kind}`");
}

/// A trait *default* has a body and real MIR, so it stays an ordinary target and is
/// not swept into the declaration table.
#[test]
fn a_trait_default_method_is_not_treated_as_a_declaration() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\npub trait T {\n    spec! {\n        ensures: ret == 9,\n    }\n    fn value(&self) -> u8 { 7 }\n}\n";
    let (outcomes, missing) = verify_all_outcomes(src);
    assert!(missing.is_empty(), "a default has a body, so nothing is missing: {missing:?}");
    let o = pick(&outcomes, "T::value");
    let FnOutcome::Failed { kind, .. } = o else {
        panic!("a default with a false postcondition must be refuted, got: {o}");
    };
    assert_eq!(kind, "postcondition", "the failure must be the postcondition, got `{kind}`");
}

/// A one-line impl body must not leave the `impl` open over the rest of the file.
///
/// The header's brace opens and closes within its own line, so a pop keyed on the
/// final depth would never fire and every later function would be read as a
/// method of that `impl` -- keyed by location, and so never matched.
#[test]
fn a_one_line_impl_body_does_not_leak_its_scope() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\npub struct Foo;\nimpl Foo { pub fn m(&self) -> u8 { 1 } }\nspec! {\n    ensures: ret == 9,\n}\npub fn g() -> u8 { 1 }\n";
    let o = verify_src("g", src).expect("g must be present");
    let FnOutcome::Failed { kind, .. } = o else {
        panic!("g has a false postcondition and must be refuted, got: {o}");
    };
    assert_eq!(kind, "postcondition", "the failure must be the postcondition, got `{kind}`");
}

// ---------------------------------------------------------------------------
// The `const` scope scan (the const-generic leak)
// ---------------------------------------------------------------------------

/// A `const` generic parameter must not make a function-local `const` of the same
/// name look crate-level.
///
/// A const-generic's *uses* print as a bare `const N`, the same spelling a
/// crate-level item's use site gets, so the whole-crate bare-use test kept a
/// function-local `const N` under a bare program-wide key. A contract on an
/// unrelated function could then name `N` and resolve it to that local's value --
/// a false proof (or a spurious vacuous-precondition failure). The name is not an
/// item at all, so it must be ignored.
#[test]
fn a_const_generic_does_not_leak_a_local_const() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\npub fn cg<const N: usize>() -> u8 { N as u8 }\npub fn owner() -> u8 { const N: u8 = 3; N }\nspec! {\n    requires: N == 3,\n    ensures: ret == N,\n}\npub fn reads() -> u8 { 3 }\n";
    let o = verify_src("reads", src).expect("reads must be present");
    assert!(
        matches!(o, FnOutcome::Skipped { .. }),
        "a contract naming the leaked local `N` must be refused, not decided, got: {o}"
    );
}

/// The same leak with the parameter list split across lines.
#[test]
fn a_split_const_generic_does_not_leak_a_local_const() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\npub fn cg<\n    const N: usize,\n>() -> u8 { N as u8 }\npub fn owner() -> u8 { const N: u8 = 3; N }\nspec! {\n    requires: N == 3,\n    ensures: ret == N,\n}\npub fn reads() -> u8 { 3 }\n";
    let o = verify_src("reads", src).expect("reads must be present");
    assert!(
        matches!(o, FnOutcome::Skipped { .. }),
        "a contract naming the leaked local `N` must be refused, not decided, got: {o}"
    );
}

/// The same leak when a newline, tab or comment sits between `const` and the name.
///
/// All three are legal Rust for the same parameter, but the scan once matched a
/// literal `"const "`, so `const\n    N: usize` and `const/*c*/N: usize` were not
/// recognized. The name then stayed out of `const_generics`, its bare use made a
/// function-local `const N` look crate-level, and a contract naming `N` resolved
/// to that local's value -- a false proof.
#[test]
fn a_const_generic_after_whitespace_or_a_comment_does_not_leak_a_local_const() {
    for header in ["const\n    N: usize", "const\tN: usize", "const/*c*/N: usize"] {
        let src = format!(
            "pub fn cg<{header}>() -> u8 {{ N as u8 }}\n\
             pub fn owner() -> u8 {{ const N: u8 = 3; N }}\n\
             /// requires: N == 3\n/// ensures: ret == N\npub fn reads() -> u8 {{ 3 }}\n"
        );
        let o = verify_src("reads", &src).expect("reads must be present");
        assert!(
            matches!(o, FnOutcome::Skipped { .. }),
            "a contract naming the leaked local `N` must be refused, not decided, got: {o}"
        );
    }
}

/// A `{` inside an `impl` header's generic arguments must not be taken for the
/// body brace.
///
/// `impl Foo<{ 1 }>` writes a const expression in the header. Counting that brace
/// as the body marked the `impl` opened and popped it before the real body, so the
/// method lost its `impl` location, its contract was keyed as a free function's,
/// and it was reported *missing* -- a contracted function dropped from the check
/// set. The real body brace is the one reached with no `<` open.
#[test]
fn a_const_expression_in_an_impl_header_does_not_hide_its_method() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\npub struct Foo<const N: usize>;\nimpl Foo<{ 1 }>\n{\n    spec! {\n        ensures: ret == 9,\n    }\n    pub fn m(&self) -> u8 { 7 }\n}\n";
    let o = verify_src("m", src).expect("m must be present");
    let FnOutcome::Failed { kind, .. } = o else {
        panic!("a method with a false postcondition must be refuted, got: {o}");
    };
    assert_eq!(kind, "postcondition", "the failure must be the postcondition, got `{kind}`");
}

/// The scan must still read a genuine crate-level `const` that shares a name with
/// a const-generic parameter elsewhere, so the fix is a narrowing, not a blanket
/// refusal of the name.
#[test]
fn a_crate_level_const_is_still_read_beside_a_const_generic() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\npub const N: u8 = 5;\npub fn cg<const N: usize>() -> usize { N }\nspec! {\n    ensures: ret == N,\n}\npub fn reads() -> u8 { 5 }\n";
    let specs = spec::extract(src);
    assert!(specs.crate_consts.contains("N"), "the crate-level `N` must still be read");
    assert!(specs.const_generics.contains("N"), "the const-generic `N` must be recognized");
    let o = verify_src("reads", src).expect("reads must be present");
    assert!(o.is_verified(), "`ret == N` with crate `N == 5` must verify, got: {o}");
}

/// A function-local `const` of the same name as a crate-level one must not shadow
/// it in a contract on a *different* function.
///
/// With both items compiled, rustc prints the local's definition and use site
/// qualified (`const owner::K`), so the crate `const K` keeps its own value and
/// `reads` returning `3` can never be *proved* against it. This is the control for
/// [`a_cfg_disabled_crate_const_does_not_let_a_local_shadow_it`]: the same shape,
/// but the crate item is compiled. The property asserted is the soundness one --
/// the local's `3` must never be mistaken for the crate's `7` -- because on an
/// older compiler a crate-level constant prints a block body the parser does not
/// fold, leaving the name unresolved rather than decided.
#[test]
fn a_local_const_does_not_shadow_a_crate_const_in_another_contract() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\npub const K: u8 = 7;\npub fn owner() -> u8 { const K: u8 = 3; K }\nspec! {\n    ensures: ret == K,\n}\npub fn reads() -> u8 { 3 }\n";
    let o = verify_src("reads", src).expect("reads must be present");
    assert!(
        !o.is_verified(),
        "`K` is the crate's 7, so `ret == K` with ret 3 must not be proved against \
         `owner`'s local 3, got: {o}"
    );
    if let FnOutcome::Failed { kind, .. } = o {
        assert_eq!(kind, "postcondition", "the failure must be the postcondition, got `{kind}`");
    }
}

/// A `#[cfg]`-disabled crate-level `const` must not let a function-local `const`
/// of the same name become a program-wide name.
///
/// The source scanner still sees the disabled item, so it reports `K` as
/// crate-level. The compiler removed it, so the dump has no crate `const K`; the
/// only `K` is `owner`'s local, whose *definition* prints bare (`const K: u8 =
/// const 3_u8;`) while its *use* stays qualified (`const owner::K`). Recording
/// that bare definition under the crate key let an unrelated contract write
/// `ret == K` and be *proved* against another function's local -- a false proof
/// on a claim the function could not have written.
#[test]
fn a_cfg_disabled_crate_const_does_not_let_a_local_shadow_it() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\n#[cfg(any())]\npub const K: u8 = 7;\npub fn owner() -> u8 { const K: u8 = 3; K }\nspec! {\n    ensures: ret == K,\n}\npub fn reads() -> u8 { 3 }\n";
    let o = verify_src("reads", src).expect("reads must be present");
    assert!(
        matches!(o, FnOutcome::Skipped { .. }),
        "`K` is not a crate-level name here, so the contract must be refused, got: {o}"
    );
}

/// The `#[cfg]` case's neighbour: a `#[cfg]`-disabled `const` with no local twin
/// must also be refused, because the name is still not a crate-level one.
#[test]
fn a_cfg_disabled_crate_const_alone_is_not_a_program_wide_name() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\n#[cfg(any())]\npub const K: u8 = 7;\nspec! {\n    ensures: ret == K,\n}\npub fn reads() -> u8 { 3 }\n";
    let o = verify_src("reads", src).expect("reads must be present");
    assert!(
        matches!(o, FnOutcome::Skipped { .. }),
        "a disabled item is not a name the compiled crate has, got: {o}"
    );
}

/// A local `const` no crate-level item shares must not be readable by name: there
/// is no program-wide `K`, so the contract is refused rather than resolved to the
/// local's value.
#[test]
fn a_local_const_alone_is_not_a_program_wide_name() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\npub fn owner() -> u8 { const K: u8 = 3; K }\nspec! {\n    ensures: ret == K,\n}\npub fn reads() -> u8 { 3 }\n";
    let o = verify_src("reads", src).expect("reads must be present");
    assert!(
        matches!(o, FnOutcome::Skipped { .. }),
        "a name only a function-local `const` carries must be refused, not decided, got: {o}"
    );
}

/// An *associated* constant must not hide a compiled crate-level constant of the
/// same name.
///
/// rustc uses an associated constant's path (`const Foo::K`) but defines it by
/// its impl location (`const <impl at …>::K`), so the use path has no matching
/// definition. Reading that mismatch as "a function-local definition" -- as the
/// `#[cfg]`-disabled case needs -- also claims the bare tail of a perfectly good
/// associated use, and then discards the compiled crate `const K`, leaving a
/// contract that names it unreadable. The claim must be limited to uses whose
/// path prefix is a *function*.
#[test]
fn an_associated_const_does_not_hide_a_crate_const() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nconst K: u8 = 7;\npub struct Foo;\nimpl Foo { pub const K: u8 = 3; }\npub fn uses_assoc() -> u8 { Foo::K }\nspec! {\n    ensures: ret == K,\n}\npub fn reads() -> u8 { 7 }\n";
    let o = verify_src("reads", src).expect("reads must be present");
    assert!(
        o.is_verified(),
        "`K` is the compiled crate constant (7), so `ret == K` must be proved \
         despite the associated `Foo::K`, got: {o}"
    );
}

/// A function-local `const` inside a *method* or a nested function must still be
/// refused as a program-wide name, even though its use path is longer than
/// `owner::K` (`Foo::method::L`, `outer::inner::M`).
///
/// The rule must read the path's *owning* `::` segment as the function, not the
/// whole prefix: an exact-prefix match would read `Foo::method` as the owner and
/// drop the claim, letting the local `L` leak. A crate `const L` of the same name
/// is present so that a leak shows up as a *false proof* (`ret == L` proved
/// against the local's 4) rather than merely a different skip reason.
#[test]
fn a_nested_or_method_local_const_is_not_a_program_wide_name() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\npub const L: u8 = 40;\npub const M: u8 = 10;\npub struct Foo;\nimpl Foo { pub fn method(&self) -> u8 { const L: u8 = 4; L } }\npub fn outer() -> u8 { fn inner() -> u8 { const M: u8 = 1; M } inner() }\nspec! {\n    ensures: ret == L,\n}\npub fn reads_l() -> u8 { 4 }\nspec! {\n    ensures: ret == M,\n}\npub fn reads_m() -> u8 { 1 }\n";
    for (name, local) in [("reads_l", "L"), ("reads_m", "M")] {
        let o = verify_src(name, src).expect("contracted function must be present");
        assert!(
            matches!(o, FnOutcome::Skipped { .. }),
            "`{local}` in `{name}` must resolve to the crate constant, not the \
             method/nested local, so a body of the local's value must be refused, \
             got: {o}"
        );
    }
}

/// A `const` whose body branches between two literal returns must not fold to the
/// first arm.
///
/// `K = if false { 7 } else { 8 }` is `8`, and rustc prints a CTFE body with one
/// `_0` arm per branch (`bb1: _0 = const 7_u8`, `bb2: _0 = const 8_u8`). Folding
/// the first arm stores `K = 7`, so `ensures: ret == 7` on a function that returns
/// `K` is reported **verified** -- a false proof about a value the constant never
/// has -- while the true claim `ret == 8` is refused. The folder instead follows
/// the `switchInt` on the discriminant's actual value, so it takes `bb2` and
/// stores `K = 8`: the true claim decides and the false one is refuted. (An
/// earlier fix refused a branching body outright; resolving the branch by value is
/// strictly more coverage with the same soundness.)
///
/// This is the *reachable* form of the bug: rustc emits a brace-block for an
/// `if`-based constant on **current stable** as well as on 1.85, so the hole was
/// not confined to the MSRV the earlier fix was written for.
#[test]
fn a_branching_const_does_not_fold_to_its_first_arm() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\npub const K: u8 = if false { 7 } else { 8 };\nspec! {\n    ensures: ret == 8,\n}\npub fn true_claim() -> u8 { K }\nspec! {\n    ensures: ret == 7,\n}\npub fn false_claim() -> u8 { K }\n";
    let true_claim = verify_src("true_claim", src).expect("true_claim must be present");
    let false_claim = verify_src("false_claim", src).expect("false_claim must be present");
    assert!(
        !false_claim.is_verified(),
        "`K` is 8, so `ret == 7` must not be reported verified, got: {false_claim}"
    );
    assert!(
        true_claim.is_verified(),
        "a branching const body folds to the *taken* arm, so `ret == 8` decides; \
         got: {true_claim}"
    );
}

/// The front end must not panic on a source that ends inside an unclosed block
/// comment.
///
/// `verify_file_with` scans the source for contracts *before* it runs rustc, so a
/// malformed file reaches `const_generic_params`. A `const` generic parameter
/// followed by an unterminated `/*` runs the comment skip to end-of-file; if the
/// skip then steps past the buffer, the slice panics. Malformed source is rustc's
/// error to report, not a crash here.
#[test]
fn an_unclosed_block_comment_after_const_is_not_a_panic() {
    use verif::mir::spec;
    for src in [
        "fn f<const /*",
        "fn f<const /*x",
        "fn f<const /* ",
        "fn f<const /*/",
        "fn f<const /*x*",
        "fn f<const /*x*/",
        "fn f<const /*\n",
    ] {
        // The property is "does not panic", so the call itself is the assertion.
        let _ = spec::const_generic_params(src);
    }
}

/// The `(name, outcome)` pairs for a source written to a temp file, for a test
/// that needs more than one function out of the same file.
///
/// [`verify_src`] returns one outcome and asserts the function was not dropped,
/// which is what a single-function test wants. The modular-call tests need to
/// read a *caller* and confirm what happened to a *callee* in the same run, so
/// this hands back every outcome and lets the test pick.
fn verify_all_src(src: &str) -> Option<Vec<(String, FnOutcome)>> {
    let rustc = rustc()?;
    static SEQ: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
    let seq = SEQ.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
    let dir = std::env::temp_dir()
        .join(format!("verif-all-{}-{seq}", std::process::id()));
    let _ = std::fs::create_dir_all(&dir);
    let file = dir.join("probe.rs");
    std::fs::write(&file, src).expect("write probe");
    let report = verify::verify_file_with_opts(&rustc, &file, &[], &probe_opts()).expect("frontend");
    let _ = std::fs::remove_dir_all(&dir);
    Some(report.outcomes)
}

/// The outcome for `name` out of a multi-function run.
fn pick<'a>(outcomes: &'a [(String, FnOutcome)], name: &str) -> &'a FnOutcome {
    outcomes
        .iter()
        .find(|(n, _)| n == name)
        .map(|(_, o)| o)
        .unwrap_or_else(|| {
            let seen: Vec<&str> = outcomes.iter().map(|(n, _)| n.as_str()).collect();
            panic!("`{name}` missing from {seen:?}")
        })
}

// ---------------------------------------------------------------------------
// Modular verification: a call answered by the callee's contract
// ---------------------------------------------------------------------------

/// A call to a function whose body is not in the dump, but whose contract is,
/// is decided by the contract.
///
/// This is the gap the README used to state as "calls are inlined one level": a
/// callee with no body had no route to a verdict at all. The contract gives one,
/// and the two obligations it produces are visible in the count -- the
/// precondition proved, and (with the postcondition assumed) the caller's own
/// postcondition proved from it.
#[test]
fn a_callee_contract_decides_a_call_with_no_body() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nunsafe extern \"C\" {\n    spec! {\n        requires: n <= 10,\n        ensures: ret <= n + 1,\n    }\n    fn ext_bump(n: u32) -> u32;\n}\n\nspec! {\n    requires: x <= 10,\n    ensures: ret <= 11,\n}\npub fn caller_ok(x: u32) -> u32 { unsafe { ext_bump(x) } }\n";
    let Some(all) = verify_all_src(src) else { return };
    let o = pick(&all, "caller_ok");
    assert!(o.is_verified(), "the contract should decide this call, got: {o}");
    match o {
        FnOutcome::Verified { obligations, .. } => assert!(
            *obligations >= 2,
            "the precondition obligation and the caller's postcondition must both \
             be present, got {obligations}"
        ),
        _ => unreachable!(),
    }
}

/// A caller that does not establish the callee's precondition is refused.
///
/// The postcondition is *assumed*, so a caller that reaches the call with an
/// argument outside the precondition would be assuming something the callee
/// never promised. Only the precondition obligation catches this -- the
/// postcondition `ret <= 101` follows from the assumption -- which is exactly
/// why the precondition has to be a proof obligation rather than a comment.
#[test]
fn a_caller_that_does_not_establish_the_precondition_fails() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nunsafe extern \"C\" {\n    spec! {\n        requires: n <= 10,\n        ensures: ret <= n + 1,\n    }\n    fn ext_bump(n: u32) -> u32;\n}\n\nspec! {\n    requires: x <= 100,\n    ensures: ret <= 101,\n}\npub fn caller_pre_fails(x: u32) -> u32 { unsafe { ext_bump(x) } }\n";
    let Some(all) = verify_all_src(src) else { return };
    let o = pick(&all, "caller_pre_fails");
    match o {
        FnOutcome::Failed { kind, .. } => assert!(
            kind.contains("callee's precondition"),
            "the failure must name the precondition, got: {kind}"
        ),
        _ => panic!("a call outside the callee's precondition must FAIL, got: {o}"),
    }
}

/// A callee whose postcondition is unsatisfiable never returns, and the caller
/// must not be credited with a proof past the call.
///
/// `ensures false` describes a function that cannot return. Assuming it would
/// make every claim after the call vacuously true, so a caller whose body is
/// `diverge(x); unreachable` would "verify". This is the MIR twin of `wp`'s
/// `Rule::DivergentCall`, and the verdict is a *refutation* of the
/// postcondition -- `Unsat` -- so its discharge is `Unsat` rather than `Valid`.
#[test]
fn a_call_to_a_callee_whose_postcondition_is_unsatisfiable_fails() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nunsafe extern \"C\" {\n    spec! {\n        requires: n <= 10,\n        ensures: false,\n    }\n    fn ext_never_returns(n: u32) -> u32;\n}\n\nspec! {\n    requires: x <= 10,\n    ensures: ret == 0,\n}\npub fn caller_of_divergent(x: u32) -> u32 { unsafe { ext_never_returns(x) } }\n";
    let Some(all) = verify_all_src(src) else { return };
    let o = pick(&all, "caller_of_divergent");
    match o {
        FnOutcome::Failed { kind, .. } => assert!(
            kind.contains("postcondition is unsatisfiable"),
            "the failure must name the divergence, got: {kind}"
        ),
        _ => panic!("a divergent callee must FAIL the caller, got: {o}"),
    }
}

/// A callee with a precondition but no postcondition promises nothing about its
/// result, so there is nothing to assume and the call is incomplete.
///
/// The alternative reading -- no `ensures` means `ensures true` -- would let the
/// caller's own postcondition be proved from nothing at all. An absent clause is
/// the absence of a promise, not a promise of everything.
#[test]
fn a_callee_with_no_postcondition_is_not_assumed_to_promise_everything() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nunsafe extern \"C\" {\n    spec! {\n        requires: n <= 10,\n    }\n    fn ext_no_post(n: u32) -> u32;\n}\n\nspec! {\n    requires: x <= 10,\n    ensures: ret <= 11,\n}\npub fn caller_of_silent(x: u32) -> u32 { unsafe { ext_no_post(x) } }\n";
    let Some(all) = verify_all_src(src) else { return };
    let o = pick(&all, "caller_of_silent");
    assert!(
        !o.is_verified(),
        "a callee with no `ensures` gives nothing to assume, got: {o}"
    );
    match o {
        FnOutcome::Incomplete { reasons, .. } => assert!(
            reasons.iter().any(|r| r.contains("no `ensures`")),
            "the reason must say the callee has no postcondition, got: {reasons:?}"
        ),
        _ => panic!("expected Incomplete, got: {o}"),
    }
}

/// A callee whose signature the scanner cannot read is refused rather than bound
/// positionally to the wrong parameter.
///
/// rustc enforces a call's arity, so a *real* mismatch cannot come from valid
/// Rust. It comes from the scanner: a parameter written as a pattern (`_: u32`)
/// has no name for a contract to mention, so [`spec`]'s signature reader stops
/// and reports no parameters at all. The call still has an argument, the two
/// counts disagree, and the contract's `requires: n <= 10` has no parameter to
/// attach to. Binding it to the first argument anyway would prove a precondition
/// nobody stated, so the call is refused.
#[test]
fn a_signature_the_scanner_cannot_read_refuses_the_call() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nunsafe extern \"C\" {\n    spec! {\n        requires: n <= 10,\n        ensures: ret <= n + 1,\n    }\n    fn ext_bump(_: u32) -> u32;\n}\n\nspec! {\n    requires: x <= 10,\n    ensures: ret <= 11,\n}\npub fn caller_unnamed(x: u32) -> u32 { unsafe { ext_bump(x) } }\n";
    let Some(all) = verify_all_src(src) else { return };
    let o = pick(&all, "caller_unnamed");
    assert!(
        !o.is_verified(),
        "a parameter the scanner could not name must not be bound positionally, got: {o}"
    );
    match o {
        FnOutcome::Incomplete { reasons, .. } => assert!(
            reasons.iter().any(|r| r.contains("parameter(s)") || r.contains("signature")),
            "the reason must name the arity or the unreadable signature, got: {reasons:?}"
        ),
        _ => panic!("expected Incomplete, got: {o}"),
    }
}

/// A caller that passes a reference to a callee outside the dump is refused.
///
/// `Ty::Ref` is modelled as its referent, so a `&mut` argument handed to a callee
/// with no body would have its write vanish: the callee's contract talks about
/// the referent, the caller holds the value, and a postcondition about the write
/// would be checked against a state the callee never produced. No aliasing model,
/// no contract route.
#[test]
fn a_call_passing_a_reference_to_a_bodyless_callee_is_refused() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nunsafe extern \"C\" {\n    spec! {\n        requires: *r <= 10,\n        ensures: true,\n    }\n    fn ext_take(r: &mut u32);\n}\n\nspec! {\n    requires: x <= 10,\n    ensures: ret <= 10,\n}\npub fn caller_by_ref(x: u32) -> u32 {\n    let mut y = x;\n    unsafe { ext_take(&mut y) };\n    y\n}\n";
    let Some(all) = verify_all_src(src) else { return };
    let o = pick(&all, "caller_by_ref");
    assert!(
        !o.is_verified(),
        "a reference argument needs the aliasing model, got: {o}"
    );
    match o {
        FnOutcome::Incomplete { reasons, .. } => assert!(
            reasons.iter().any(|r| r.contains("passes a reference")),
            "the reason must name the reference, got: {reasons:?}"
        ),
        _ => panic!("expected Incomplete, got: {o}"),
    }
}

/// A callee whose body *is* in the dump is inlined, not answered from its
/// contract.
///
/// Inlining is strictly more precise -- it executes the callee rather than
/// trusting what it promised -- so the contract route must not shadow it. The
/// discriminating test is a callee whose contract is *weaker* than its body: the
/// caller's claim is only provable from the body, so a pass proves the body was
/// used. A contract-only route would leave it undecided.
#[test]
fn a_callee_with_a_body_is_inlined_rather_than_answered_from_its_contract() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nspec! {\n    ensures: ret <= 100,\n}\npub fn callee(a: u32) -> u32 { a }\n\nspec! {\n    requires: x <= 3,\n    ensures: ret <= 3,\n}\npub fn caller(x: u32) -> u32 { callee(x) }\n";
    let Some(all) = verify_all_src(src) else { return };
    let o = pick(&all, "caller");
    assert!(
        o.is_verified(),
        "the callee's body is in the dump, so inlining proves `ret <= 3`; a contract-only \
         route would only give `ret <= 100`, got: {o}"
    );
}

// ---------------------------------------------------------------------------
// Inherited trait contracts bind by declaration position, not by name
// ---------------------------------------------------------------------------

/// An implementation may name its parameters differently from the trait it
/// implements, and the declaration's contract means the *declaration's* names.
///
/// The two signatures are the same method, so their parameters correspond by
/// position: the declaration's first value parameter is the implementation's first
/// value parameter, whatever either is called. Binding the contract by the
/// implementation's own names instead attached `ret == a` to whichever argument the
/// implementation called `a`, which is the wrong value whenever the names are
/// swapped. That is a false proof, not a lost one: the body returned its *second*
/// argument and the tool reported the postcondition proved.
///
/// Both directions are pinned. The body that returns the implementation's `a` is
/// the trait's `b`, so it must be refuted; the body that returns the
/// implementation's `b` is the trait's `a`, so it must verify. A tool that bound by
/// name would get exactly these two backwards.
#[test]
fn an_inherited_contract_binds_parameters_by_position_not_by_name() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\npub trait T {\nspec! {\n    ensures: ret == a,\n}\nfn f(&self, a: u8, b: u8) -> u8;\n}\npub struct S;\nimpl T for S { fn f(&self, b: u8, a: u8) -> u8 { a } }\n";
    let (outcomes, missing) = verify_all_outcomes(src);
    assert!(missing.is_empty(), "the impl carries the declaration's contract, so nothing is missing: {missing:?}");
    let o = pick(&outcomes, "f");
    assert!(
        !o.is_verified(),
        "the body returns the implementation's `a`, which is the trait's `b`, so \
         `ret == a` (the trait's `a`) must be refuted, got: {o}"
    );

    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\npub trait T {\nspec! {\n    ensures: ret == a,\n}\nfn f(&self, a: u8, b: u8) -> u8;\n}\npub struct S;\nimpl T for S { fn f(&self, b: u8, a: u8) -> u8 { b } }\n";
    let (outcomes, _) = verify_all_outcomes(src);
    let o = pick(&outcomes, "f");
    assert!(
        o.is_verified(),
        "the body returns the implementation's `b`, which is the trait's `a`, so \
         `ret == a` holds, got: {o}"
    );
}

/// The same alignment holds for a field a contract names: `c.lo` means the
/// *declaration's* parameter `c`, at whatever position the implementation puts it.
///
/// A struct argument's fields are bound under the argument's name, so an inherited
/// contract that names `c.lo` has to find the fields under the declaration's `c`,
/// not the implementation's. Binding them under the implementation's name would
/// leave the contract unreadable -- refused rather than mis-proved, but still a
/// lost proof.
#[test]
fn an_inherited_contract_binds_a_field_to_the_declarations_parameter() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\npub struct Config { pub lo: u8, pub hi: u8 }\npub trait T {\nspec! {\n    requires: c.lo <= c.hi,\n    ensures: ret == c.hi,\n}\nfn hi_of(&self, c: Config) -> u8;\n}\npub struct S;\nimpl T for S { fn hi_of(&self, k: Config) -> u8 { k.hi } }\n";
    let (outcomes, missing) = verify_all_outcomes(src);
    assert!(missing.is_empty(), "nothing is missing here: {missing:?}");
    let o = pick(&outcomes, "hi_of");
    assert!(
        o.is_verified(),
        "the contract names the declaration's `c`, which is the implementation's \
         `k` by position, so `c.hi` is `k.hi` and the body satisfies it, got: {o}"
    );
}

/// An inherited contract that cannot be aligned is refused, not bound by guess.
///
/// A declaration whose parameter list the scanner cannot read whole -- here a
/// pattern (`_: u8`) with no name for a contract to mention -- leaves the positions
/// unknown, so "the Nth name means the Nth local" stops being true. Binding by
/// position anyway would attach the contract to an argument it may not mean, which
/// is the failure the alignment exists to prevent. The implementation is *skipped*
/// with a reason that names the unreadable signature, rather than verified against a
/// guess or silently dropped.
#[test]
fn an_inherited_contract_that_cannot_be_aligned_is_refused() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\npub trait T {\nspec! {\n    ensures: ret == 1,\n}\nfn f(&self, _: u8) -> u8;\n}\npub struct S;\nimpl T for S { fn f(&self, a: u8) -> u8 { let _ = a; 2 } }\n";
    let (outcomes, missing) = verify_all_outcomes(src);
    assert!(missing.is_empty(), "the impl is present, so nothing is missing: {missing:?}");
    let o = pick(&outcomes, "f");
    assert!(
        !o.is_verified(),
        "a contract whose declaration cannot be aligned must not be bound by guess, got: {o}"
    );
    match o {
        FnOutcome::Skipped { reason } => assert!(
            reason.contains("signature"),
            "the refusal must name the unreadable declaration signature, got: {reason}"
        ),
        _ => panic!("expected Skipped, got: {o}"),
    }
}

// ---------------------------------------------------------------------------
// A wrapped return type is not read as unsigned
// ---------------------------------------------------------------------------

/// A return type that wraps to the next line leaves the signature unreadable, and
/// an unreadable signature refuses the call.
///
/// The reader used to take everything between `->` and the end of the line, so
/// `fn ext() ->` followed by `i8;` produced an empty type, which reads as unsigned.
/// A caller testing `ext() < 0` then took the unsigned branch and a postcondition
/// that does not hold was reported proved. The whole signature is refused instead,
/// which is the same discipline the parameter list already followed.
#[test]
fn a_return_type_that_wraps_to_the_next_line_refuses_the_call() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nunsafe extern \"C\" {\nspec! {\n    ensures: ret == i8::MIN,\n}\nfn ext() ->\ni8;\n}\nspec! {\n    ensures: ret == 0,\n}\npub fn caller() -> u8 { if unsafe { ext() } < 0 { 1u8 } else { 0u8 } }\n";
    let (outcomes, _) = verify_all_outcomes(src);
    let o = pick(&outcomes, "caller");
    assert!(
        !o.is_verified(),
        "the return type was not read whole, so the call must be refused rather than \
         checked against an unsigned reading, got: {o}"
    );
    match o {
        FnOutcome::Incomplete { reasons, .. } => assert!(
            reasons.iter().any(|r| r.contains("signature")),
            "the reason must name the unreadable signature, got: {reasons:?}"
        ),
        _ => panic!("expected Incomplete, got: {o}"),
    }
}

/// The control: the same signature on one line *is* read, and the caller's false
/// postcondition is refuted rather than verified.
#[test]
fn a_return_type_on_one_line_is_read_and_refutes_the_false_claim() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nunsafe extern \"C\" {\nspec! {\n    ensures: ret == i8::MIN,\n}\nfn ext() -> i8;\n}\nspec! {\n    ensures: ret == 0,\n}\npub fn caller() -> u8 { if unsafe { ext() } < 0 { 1u8 } else { 0u8 } }\n";
    let (outcomes, _) = verify_all_outcomes(src);
    let o = pick(&outcomes, "caller");
    assert!(
        !o.is_verified(),
        "a signed return of i8::MIN makes `ext() < 0` true, so `ret == 0` is false \
         for that input, got: {o}"
    );
    match o {
        FnOutcome::Failed { .. } => {}
        _ => panic!("expected a definite counterexample, got: {o}"),
    }
}

// ---------------------------------------------------------------------------
// A trait path written with a relative root names the declared trait
// ---------------------------------------------------------------------------

/// `impl crate::T for S` is an implementation of `T`, so the declaration's contract
/// is checked on it.
///
/// The qualifier used to be kept as written, so the impl's trait path never matched
/// the declaration's module-qualified key: the impl was checked against nothing and
/// the declaration was reported as having no implementation. Both halves of that are
/// a silently unenforced promise. `crate::` at the root and `self::` inside a module
/// resolve to the same key as the bare name.
#[test]
fn a_trait_impl_written_with_a_relative_root_is_checked() {
    for (label, path) in [("crate::T", "crate::T"), ("self::T", "self::T")] {
        let src = format!(
            "pub trait T {{\n\
             /// ensures: ret == 9\n\
             fn f(&self) -> u8;\n\
             }}\n\
             pub struct S;\n\
             impl {path} for S {{ fn f(&self) -> u8 {{ 7 }} }}\n"
        );
        let (outcomes, missing) = verify_all_outcomes(&src);
        assert!(
            missing.is_empty(),
            "`{label}` names the declared trait, so nothing is missing: {missing:?}"
        );
        let o = pick(&outcomes, "f");
        assert!(
            !o.is_verified(),
            "the body returns 7 and the trait promises 9, so `{label}` must be \
             refuted, got: {o}"
        );
    }
}

// ---------------------------------------------------------------------------
// A bodyless declaration's contract is assumed, not missing
// ---------------------------------------------------------------------------

/// A file whose only external call verified is clean.
///
/// An `extern` item has no MIR by construction, so its contract is assumed at each
/// call rather than checked on a body. Reporting it `missing` made `is_clean()`
/// false for a file with nothing wrong, which is the false alarm that trains a
/// reader to ignore the report. The call itself must still be verified.
#[test]
fn a_bodyless_declaration_is_assumed_not_reported_missing() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nunsafe extern \"C\" {\nspec! {\n    ensures: ret == 1,\n}\nfn ext() -> u8;\n}\nspec! {\n    ensures: ret == 1,\n}\npub fn caller() -> u8 { unsafe { ext() } }\n";
    let (outcomes, missing) = verify_all_outcomes(src);
    assert!(
        missing.is_empty(),
        "an `extern` declaration is assumed at its calls, not missing: {missing:?}"
    );
    let o = pick(&outcomes, "caller");
    assert!(o.is_verified(), "the call is decided by the callee's contract, got: {o}");
}

/// A bodyless declaration that is *called* with an unsatisfiable postcondition is
/// still a failure -- being assumed is not being ignored.
#[test]
fn an_assumed_bodyless_contract_is_still_refuted_when_unsatisfiable() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nunsafe extern \"C\" {\nspec! {\n    ensures: ret == 1 && ret == 2,\n}\nfn ext() -> u8;\n}\nspec! {\n    ensures: ret == 1,\n}\npub fn caller() -> u8 { unsafe { ext() } }\n";
    let (outcomes, _) = verify_all_outcomes(src);
    let o = pick(&outcomes, "caller");
    assert!(
        !o.is_verified(),
        "a postcondition no input satisfies must not verify the caller, got: {o}"
    );
}

// ---------------------------------------------------------------------------
// An unreachable call site proves nothing about the callee
// ---------------------------------------------------------------------------

/// A call under contradictory guards is never executed, so it is not a divergence.
///
/// The divergence check asks whether any input satisfies the callee's postcondition,
/// and an unsatisfiable path condition makes that query `Unsat` for a reason that has
/// nothing to do with the callee: the path is dead. Reporting divergence there is a
/// false failure about a call that cannot happen. The dead path is dropped, and the
/// function verifies on the reachable ones.
#[test]
fn an_unreachable_call_is_not_a_divergence() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nunsafe extern \"C\" {\nspec! {\n    ensures: true,\n}\nfn ext() -> u8;\n}\nspec! {\n    ensures: ret == 0,\n}\npub fn caller(x: u8) -> u8 {\nif x == 0 { if x != 0 { unsafe { ext() }; } }\n0\n}\n";
    let (outcomes, missing) = verify_all_outcomes(src);
    assert!(missing.is_empty(), "the extern is assumed, so nothing is missing: {missing:?}");
    let o = pick(&outcomes, "caller");
    assert!(
        o.is_verified(),
        "the call sits under contradictory guards, so it cannot happen and is not a \
         divergence, got: {o}"
    );
}

// ---------------------------------------------------------------------------
// A helper call inside a measured loop keeps the measure
// ---------------------------------------------------------------------------

/// A measured loop whose step is a same-dump call still proves termination.
///
/// Entering a callee cleared the caller's `cut` *and* its measure, and only the cut
/// was restored on return. The resumed caller was still inside its loop with a cut
/// but no head value, so the back edge had nothing to compare and the loop was
/// reported incomplete -- "the loop head was never entered" -- even though its
/// measure was sufficient. A proof silently discarded, which this crate treats as a
/// defect. The two now travel together.
#[test]
fn a_helper_call_in_a_measured_loop_keeps_the_termination_measure() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nspec! {\n    requires: i < 255,\n    ensures: ret == i + 1,\n}\npub fn step(i: u8) -> u8 { i + 1 }\n\nspec! {\n    requires: n <= 200,\n    invariant: i <= n,\n    variant: n - i,\n    ensures: ret == n,\n}\npub fn f(n: u8) -> u8 {\nlet mut i = 0u8;\nwhile i < n { i = step(i); }\ni\n}\n";
    let (outcomes, missing) = verify_all_outcomes(src);
    assert!(missing.is_empty(), "both bodies are present: {missing:?}");
    let o = pick(&outcomes, "f");
    assert!(
        o.is_verified(),
        "the helper call must not erase the loop's measure, got: {o}"
    );
}

// ---------------------------------------------------------------------------
// A diverging call is a checked panic, not a dropped path
// ---------------------------------------------------------------------------

/// `assert!` is a promise, and the frontend now checks it instead of dropping it.
///
/// `assert!(x <= 200)` lowers to a `switchInt` that routes the *failing* case into
/// a diverging call to `core::panicking::panic`, with no `[return: bbN]` arm. The
/// parser only recognised the `[return:` shape, so the terminator came back
/// `Unknown("missing")`, the path was reported unmodelled, and the whole function
/// was `Incomplete` -- the assert, the most ordinary safety guard in Rust, was
/// never checked at all. Both halves are pinned here: the guard is *discharged*
/// under a precondition that implies it, and *refuted* with a real counterexample
/// when it is not.
#[test]
fn an_assert_is_checked_and_discharged_under_its_precondition() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nspec! {\n    requires: x <= 200,\n    ensures: ret == 1,\n}\npub fn f(x: u8) -> u8 { assert!(x <= 200); 1 }\n";
    let o = verify_src("f", src).expect("f must be in the report");
    assert!(
        o.is_verified(),
        "the guard follows from the precondition, so the assert cannot fire: {o}"
    );
}

/// The teeth: with no precondition the assert is a real panic, and it is reported.
///
/// The counterexample is a value above 200, which is exactly the input the assert
/// exists to reject. Reading the diverging call as a harmless drop would instead
/// have left the function `Incomplete`, which is a lost proof of nothing -- this
/// makes the missing precondition a *failure*, the sound direction.
#[test]
fn an_unproved_assert_is_refuted_with_a_counterexample() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nspec! {\n    ensures: ret == 1,\n}\npub fn f(x: u8) -> u8 { assert!(x <= 200); 1 }\n";
    let o = verify_src("f", src).expect("f must be in the report");
    match o {
        FnOutcome::Failed { kind, detail } => {
            assert!(
                kind.contains("assertion failed: x <= 200"),
                "the report should name the failed assertion, got `{kind}`"
            );
            assert!(!detail.is_empty(), "a failure must carry a counterexample");
        }
        other => panic!("an assert with no guard must fail, got: {other}"),
    }
}

/// An assert is *assumed* on the fall-through path, so the code after it can rely
/// on the guard -- the same rule the `assert` terminator already had.
///
/// Without this the guard would be checked and then thrown away, so `x + 1` after
/// `assert!(x <= 200)` would be reported as a possible overflow even though the
/// assert rules it out. The postcondition `ret == x + 1` is what makes the
/// assumption load-bearing: it only holds if the guard is carried forward.
#[test]
fn an_assert_guard_is_assumed_by_the_code_after_it() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nspec! {\n    requires: x <= 200,\n    ensures: ret == x + 1,\n}\npub fn f(x: u8) -> u8 { assert!(x <= 200); x + 1 }\n";
    let o = verify_src("f", src).expect("f must be in the report");
    assert!(o.is_verified(), "the assert must be assumed on the fall-through path: {o}");
}

/// `debug_assert!` lowers the same way, so it is checked and assumed too.
#[test]
fn a_debug_assert_is_checked_like_an_assert() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nspec! {\n    requires: x <= 200,\n    ensures: ret == 1,\n}\npub fn f(x: u8) -> u8 { debug_assert!(x <= 200); 1 }\n";
    let o = verify_src("f", src).expect("f must be in the report");
    assert!(o.is_verified(), "a debug_assert is the same terminator as an assert: {o}");
}

/// `panic!` is a reachable failure, so an unguarded one is refuted, and a guarded
/// one is proved unreachable.
///
/// The message is carried from the string literal the dump passes, so the report
/// names the panic rather than the mangled callee path.
#[test]
fn a_panic_is_refuted_unless_its_guard_excludes_it() {
    let unguarded = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nspec! {\n    ensures: ret == 1,\n}\npub fn f(x: u8) -> u8 { if x == 0 { panic!(\"zero\"); } 1 }\n";
    let o = verify_src("f", unguarded).expect("f must be in the report");
    match o {
        FnOutcome::Failed { kind, .. } => {
            assert!(kind.contains("zero"), "the report should name the panic, got `{kind}`");
        }
        other => panic!("an unguarded panic must fail, got: {other}"),
    }
    let guarded = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nspec! {\n    requires: x != 0,\n    ensures: ret == 1,\n}\npub fn f(x: u8) -> u8 { if x == 0 { panic!(\"zero\"); } 1 }\n";
    let o = verify_src("f", guarded).expect("f must be in the report");
    assert!(o.is_verified(), "the precondition excludes the panic: {o}");
}

/// A *formatted* panic message is recovered from the `core::fmt` chain, not from
/// a literal operand.
///
/// Edition 2024 never puts `panic!("bad {}", x)` in the MIR as one string: the
/// pieces are a promoted `&[&str]` and the placeholders are separate `rt::Argument`
/// values, assembled by `Arguments::new_v1` on 1.85 and by
/// `Arguments::new::<LEN, _>` (a single encoded byte template) on stable. Both
/// spellings have to be read, and this test is what pins that -- it runs on
/// whichever toolchain `VERIF_RUSTC` names, so the two shapes are covered by the
/// same assertion on the two toolchains the gate uses.
#[test]
fn a_formatted_panic_message_is_recovered_from_the_fmt_chain() {
    let unguarded = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nspec! {\n    ensures: ret == 1,\n}\npub fn f(x: u8) -> u8 { if x == 0 { panic!(\"bad {}\", x); } 1 }\n";
    let Some(o) = verify_src("f", unguarded) else { return };
    match o {
        FnOutcome::Failed { kind, .. } => {
            assert!(
                kind.contains("bad {}"),
                "the report should name the panic's pieces, got `{kind}`"
            );
        }
        other => panic!("a formatted panic with no guard must fail, got: {other}"),
    }
    // A formatted `assert!` is the same chain with the guard in front of it.
    let guarded = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nspec! {\n    requires: x <= 100,\n    ensures: ret == 1,\n}\npub fn f(x: u8) -> u8 { assert!(x <= 100, \"bad {}\", x); 1 }\n";
    let o = verify_src("f", guarded).expect("f must be in the report");
    assert!(o.is_verified(), "the precondition implies the formatted assert: {o}");
    let misguarded = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nspec! {\n    ensures: ret == 1,\n}\npub fn f(x: u8) -> u8 { assert!(x <= 100, \"bad {}\", x); 1 }\n";
    let o = verify_src("f", misguarded).expect("f must be in the report");
    match o {
        FnOutcome::Failed { kind, .. } => assert!(
            kind.contains("bad {}"),
            "the formatted assert should be named by its pieces, got `{kind}`"
        ),
        other => panic!("an unguarded formatted assert must fail, got: {other}"),
    }
    // `panic!("{}", x)` has no literal piece at all -- it lowers to a
    // `panic_display::<u8>` / `panic_cold_display::<u8>` call -- so there is no
    // message to recover and the callee's own name is the fallback. It must be the
    // *name*, read through the generics, and not the type arguments: the report
    // used to read `<u8>`, which is a mangled path rather than a fallback. The
    // two toolchains spell the callee differently, so only the shape is pinned.
    let no_pieces = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nspec! {\n    ensures: ret == 1,\n}\npub fn f(x: u8) -> u8 { if x == 0 { panic!(\"{}\", x); } 1 }\n";
    let o = verify_src("f", no_pieces).expect("f must be in the report");
    match o {
        FnOutcome::Failed { kind, .. } => assert!(
            kind.contains("display") && !kind.contains('<'),
            "the fallback should be the callee's name, not its generics, got `{kind}`"
        ),
        other => panic!("an unguarded panic must fail, got: {other}"),
    }
}

/// The other two panicking macros name their own message too.
///
/// `unreachable!()` and `todo!()` pass a *literal*, so they take the
/// `core::panicking::panic(const "…")` / `Arguments::from_str` path rather than the
/// `core::fmt` chain, and the report should carry the text the programmer wrote --
/// not the mangled callee. They are here because a fix aimed at the formatted case
/// must not regress the plain one.
#[test]
fn a_literal_panic_macro_names_its_message() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nspec! {\n    ensures: ret == 1,\n}\npub fn f(x: u8) -> u8 { if x == 0 { unreachable!(); } 1 }\n";
    let Some(o) = verify_src("f", src) else { return };
    match o {
        FnOutcome::Failed { kind, .. } => assert!(
            kind.contains("entered unreachable code"),
            "the report should carry the macro's text, got `{kind}`"
        ),
        other => panic!("an unguarded `unreachable!` must fail, got: {other}"),
    }
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nspec! {\n    ensures: ret == 1,\n}\npub fn f(x: u8) -> u8 { if x == 0 { todo!(); } 1 }\n";
    let o = verify_src("f", src).expect("f must be in the report");
    match o {
        FnOutcome::Failed { kind, .. } => assert!(
            kind.contains("not yet implemented"),
            "the report should carry the macro's text, got `{kind}`"
        ),
        other => panic!("an unguarded `todo!` must fail, got: {other}"),
    }
}

/// The bounds check rustc inserts for `xs[0]` is a diverging call too, and it is
/// proved unreachable from the length precondition.
///
/// This is the shape an ordinary slice access lowers to, so the check is what
/// makes a `&[T]` argument safe to reason about at all.
#[test]
fn an_index_bounds_check_is_proved_unreachable() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\nspec! {\n    requires: xs.len() >= 1,\n    ensures: ret == 0,\n}\npub fn f(xs: &[u8]) -> u8 { xs[0] * 0 }\n";
    let o = verify_src("f", src).expect("f must be in the report");
    assert!(o.is_verified(), "the length precondition rules the bounds check out: {o}");
}

/// A diverging call in its *cleanup* form is recognised too (review C1b).
///
/// With a live `Drop` local, rustc prints the diverging call's single successor as
/// its cleanup edge (`_4 = core::panicking::panic(…) -> bb4`, with `bb4 (cleanup)`)
/// rather than the `-> unwind continue` arm the parser first recognised. That line
/// matched neither the `-> [return:` nor the `-> unwind` shape, so the whole path
/// was still dropped -- an `assert!` in any function with a live RAII local (`Vec`,
/// `String`, `Box`, a lock guard) was never checked. The parser now combines the
/// `!`-typed destination with the `(cleanup)` marker and reads it as a diverging
/// call, so the assert is a real failure again.
///
/// The `drop(_2)` line is still unmodelled (review C1), so a *guarded* version is
/// `incomplete` for that reason -- but the *unguarded* one is a definite `FAILED`
/// with the counterexample, which is the sound direction: a missing precondition
/// is a failure, not a silently dropped check.
#[test]
fn a_diverging_call_in_a_function_with_a_drop_local_is_checked() {
    let src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\npub struct Guard(pub u8);\nimpl Drop for Guard {\n    fn drop(&mut self) {}\n}\nspec! {\n    ensures: ret == 1,\n}\npub fn f(x: u8) -> u8 { let _g = Guard(0); assert!(x < 200); 1 }\n";
    let o = verify_src("f", src).expect("f must be in the report");
    match o {
        FnOutcome::Failed { kind, detail } => {
            assert!(
                kind.contains("assertion failed: x < 200"),
                "the cleanup-form assert must be named, got `{kind}`"
            );
            assert!(!detail.is_empty(), "a failure must carry a counterexample");
        }
        other => panic!(
            "an unguarded assert with a live Drop local must be a failure, not a \
             dropped path, got: {other}"
        ),
    }
}

/// `iN::abs`, `iN::unsigned_abs` and `Ord::clamp` are modelled, and `abs`'s
/// *panic* is modelled too.
///
/// `abs` and `clamp` are the first modelled methods whose failure is a **panic**
/// rather than a value or an `Option`, so the two halves have to be checked
/// apart. A model that returned `|x|` with no panic predicate would verify
/// `abs_min_can_overflow` (which panics at `i8::MIN`); a model that returned a
/// constant would prove the value tripwires. Each true direction is asserted
/// `Verified` and each false direction `Failed`, and the panic entries are pinned
/// to a `panic` kind so an unrelated refusal cannot satisfy them.
///
/// `unsigned_abs` is the contrast that makes `abs`'s panic meaningful: the same
/// magnitude, read as the unsigned type, so it is *total* even at `MIN`
/// (`i8::MIN.unsigned_abs()` is `128u8`). Its `requires: true` control must
/// verify, which a model that gave it `abs`'s failure predicate could not.
#[test]
fn abs_and_clamp_are_modelled_with_their_panics() {
    let names = [
        "abs_small_reads",
        "abs_small_is_a_value_cannot_prove_this",
        "abs_min_can_overflow",
        "unsigned_abs_reads",
        "unsigned_abs_of_min_is_the_magnitude",
        "unsigned_abs_of_min_is_panic_free",
        "unsigned_abs_is_a_value_cannot_prove_this",
        "clamp_builtin_reads",
        "clamp_builtin_can_panic",
        "clamp_builtin_is_a_value_cannot_prove_this",
        "clamp_builtin_signed_reads",
    ];
    let Some(report) = verify_realistic(&names) else { return };

    for name in [
        "abs_small_reads",
        "unsigned_abs_reads",
        "unsigned_abs_of_min_is_the_magnitude",
        "unsigned_abs_of_min_is_panic_free",
        "clamp_builtin_reads",
        "clamp_builtin_signed_reads",
    ] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify, got: {o}");
    }

    // The value tripwires: the call is a value, not an uninterpreted one. The
    // failure *kind* is asserted, not just `Failed`, because both entries carry a
    // precondition that discharges the panic -- so a refutation on the panic
    // instead of the postcondition would mean the entry is not testing the value
    // at all (the `abs` panic fires at `i32::MIN`, before the value is read).
    for name in [
        "abs_small_is_a_value_cannot_prove_this",
        "unsigned_abs_is_a_value_cannot_prove_this",
        "clamp_builtin_is_a_value_cannot_prove_this",
    ] {
        let o = outcome(&report, name);
        match o {
            FnOutcome::Failed { kind, .. } => assert!(
                kind.starts_with("postcondition"),
                "`{name}` must be refuted on the value (postcondition), got `{kind}`"
            ),
            other => panic!("`{name}` claims a false value and must be refuted, got: {other}"),
        }
    }

    // The panic obligations: the call *can* panic, so the function fails on the
    // panic, not on a postcondition.
    for name in ["abs_min_can_overflow", "clamp_builtin_can_panic"] {
        let o = outcome(&report, name);
        match o {
            FnOutcome::Failed { kind, .. } => {
                assert!(kind.starts_with("panic"), "`{name}` must fail on the panic, got `{kind}`")
            }
            other => panic!("`{name}` must be a failing panic, got: {other}"),
        }
    }
}

/// The ASCII class tests on `u8` are modelled, and each one is a *predicate*
/// rather than an uninterpreted value.
///
/// The shape is the one `is_power_of_two` already had -- a `bool`-valued method
/// whose destination is a `bool`, not a one-bit word -- with one difference: the
/// receiver is `&self`, so the operand is the *referent* of a reference the body
/// materialises (`_3 = &_1`). The three tripwires are the evidence the predicates
/// are read at all: a model that returned a constant would prove them.
#[test]
fn the_ascii_classes_are_modelled_with_their_boundaries() {
    let names = [
        "ascii_digit_value",
        "ascii_digit_is_not_every_byte_cannot_prove_this",
        "ascii_class_discriminates",
        "ascii_class_hex_arm_is_not_every_byte_cannot_prove_this",
        "ascii_is_ascii_boundary",
        "ascii_is_ascii_is_not_every_byte_cannot_prove_this",
    ];
    let Some(report) = verify_realistic(&names) else { return };

    for name in ["ascii_digit_value", "ascii_class_discriminates", "ascii_is_ascii_boundary"] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify, got: {o}");
    }

    // Each tripwire must be refuted on the *postcondition*, with a witness at a
    // byte that really takes the other arm -- so an unrelated refusal cannot
    // satisfy it, and the model cannot be passing by answering a constant.
    for (name, byte) in [
        ("ascii_digit_is_not_every_byte_cannot_prove_this", 48u64),
        ("ascii_class_hex_arm_is_not_every_byte_cannot_prove_this", 80),
        ("ascii_is_ascii_is_not_every_byte_cannot_prove_this", 128),
    ] {
        let o = outcome(&report, name);
        match o {
            FnOutcome::Failed { kind, .. } => assert!(
                kind.starts_with("postcondition"),
                "`{name}` must be refuted on the postcondition, got `{kind}`"
            ),
            other => panic!("`{name}` claims a false value and must be refuted, got: {other}"),
        }
        // The witness names the byte that takes the else arm: `b'0'` for the digit
        // test, `b'P'` (80) for the hex arm, and `0x80` for `is_ascii`.
        let detail = format!("{o}");
        assert!(
            detail.contains(&byte.to_string()),
            "`{name}`'s witness should name the byte {byte}, got: {detail}"
        );
    }
}

/// `abs_diff` and the rotations are modelled, and each has a tripwire that a
/// wrong model would *prove*.
///
/// `abs_diff`'s result is the *unsigned* magnitude, so the model computes the
/// difference at one bit of headroom and takes the absolute value there; a model
/// that returned the signed difference would read a negative result as a huge
/// unsigned one. The rotations reduce the amount modulo the width (`rotate by the
/// width` is the identity, `by w + 1` is `by 1`), which is the property a shift
/// model gets wrong. Both tripwires claim the shape a *shift or signed-difference*
/// model would produce, so they must be refuted.
#[test]
fn abs_diff_and_rotations_are_modelled_with_their_boundaries() {
    let names = [
        "abs_diff_ordered",
        "abs_diff_is_symmetric_cannot_prove_this",
        "rotate_left_wraps_the_top_bit",
        "rotate_is_not_a_shift_cannot_prove_this",
        "rotate_by_the_width_is_the_identity",
        "rotate_by_more_than_the_width_wraps",
    ];
    let Some(report) = verify_realistic(&names) else { return };

    for name in [
        "abs_diff_ordered",
        "rotate_left_wraps_the_top_bit",
        "rotate_by_the_width_is_the_identity",
        "rotate_by_more_than_the_width_wraps",
    ] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify, got: {o}");
    }

    // The tripwires must be refuted on the *postcondition*, not by an unrelated
    // refusal, so the model cannot pass by answering a constant.
    for name in ["abs_diff_is_symmetric_cannot_prove_this", "rotate_is_not_a_shift_cannot_prove_this"]
    {
        let o = outcome(&report, name);
        match o {
            FnOutcome::Failed { kind, .. } => assert!(
                kind.starts_with("postcondition"),
                "`{name}` must be refuted on the postcondition, got `{kind}`"
            ),
            other => panic!("`{name}` claims a false value and must be refuted, got: {other}"),
        }
    }
}

/// The next batch of `core` value methods is modelled: `signum`, the Euclidean
/// pair (`div_euclid`/`rem_euclid`), `div_ceil`, the byte permutations
/// (`reverse_bits`/`swap_bytes`), the byte orders (`to_be`/`to_le`), the ASCII
/// case conversions and `is_multiple_of`.
///
/// Each shape has a true pin and a tripwire that claims the value a *plausible
/// wrong model* would produce (the truncating `/` for the Euclidean pair, the
/// identity for the permutations, an unconditional `+32` for the ASCII case, an
/// unguarded `%` for `is_multiple_of`). The tripwires must be refuted on the
/// *postcondition*, so the model cannot pass by answering a constant, and the
/// signedness pins (`signum_of_min`) fail for a model that read the top bit
/// unsigned.
#[test]
fn the_next_core_value_methods_are_modelled_with_their_tripwires() {
    let names = [
        "signum_of_min_is_minus_one",
        "signum_of_a_negative_cannot_prove_this",
        "cast_unsigned_reinterprets_negative_as_large",
        "cast_unsigned_reinterprets_negative_cannot_prove_this",
        "cast_signed_reinterprets_large_as_negative",
        "cast_signed_reinterprets_large_cannot_prove_this",
        "cast_unsigned_makes_a_comparison_unsigned",
        "div_euclid_rounds_toward_negative_infinity",
        "div_euclid_is_not_truncating_division_cannot_prove_this",
        "rem_euclid_is_non_negative",
        "rem_euclid_is_not_the_truncating_remainder_cannot_prove_this",
        "unsigned_rem_euclid_is_the_plain_remainder",
        "unsigned_rem_euclid_is_not_the_signed_adjustment_cannot_prove_this",
        "unsigned_div_euclid_is_the_plain_quotient",
        "unsigned_div_euclid_is_not_the_signed_adjustment_cannot_prove_this",
        "div_ceil_rounds_toward_positive_infinity",
        "div_ceil_is_not_truncating_division_cannot_prove_this",
        "reverse_bits_of_the_top_bit",
        "reverse_bits_is_not_the_identity_cannot_prove_this",
        "swap_bytes_reverses_the_bytes",
        "swap_bytes_is_not_the_identity_cannot_prove_this",
        "to_be_on_a_little_endian_target_swaps",
        "to_be_is_not_the_identity_here_cannot_prove_this",
        "to_le_on_a_little_endian_target_is_the_identity",
        "from_be_on_a_little_endian_target_swaps",
        "from_be_is_not_the_identity_here_cannot_prove_this",
        "from_le_on_a_little_endian_target_is_the_identity",
        "to_be_then_from_be_is_the_identity",
        "to_le_then_from_be_is_not_the_identity_cannot_prove_this",
        "to_ascii_uppercase_of_a_lowercase_letter",
        "to_ascii_uppercase_leaves_uppercase_cannot_prove_this",
        "to_ascii_lowercase_of_an_uppercase_letter",
        "is_multiple_of_six_by_three",
        "is_multiple_of_seven_by_three_cannot_prove_this",
        "is_multiple_of_zero_by_zero",
        "is_multiple_of_five_by_zero",
    ];
    let Some(report) = verify_realistic(&names) else { return };

    for name in [
        "signum_of_min_is_minus_one",
        "cast_unsigned_reinterprets_negative_as_large",
        "cast_signed_reinterprets_large_as_negative",
        "cast_unsigned_makes_a_comparison_unsigned",
        "div_euclid_rounds_toward_negative_infinity",
        "rem_euclid_is_non_negative",
        "unsigned_rem_euclid_is_the_plain_remainder",
        "unsigned_div_euclid_is_the_plain_quotient",
        "div_ceil_rounds_toward_positive_infinity",
        "reverse_bits_of_the_top_bit",
        "swap_bytes_reverses_the_bytes",
        "to_be_on_a_little_endian_target_swaps",
        "to_le_on_a_little_endian_target_is_the_identity",
        "from_be_on_a_little_endian_target_swaps",
        "from_le_on_a_little_endian_target_is_the_identity",
        "to_be_then_from_be_is_the_identity",
        "to_ascii_uppercase_of_a_lowercase_letter",
        "to_ascii_lowercase_of_an_uppercase_letter",
        "is_multiple_of_six_by_three",
        "is_multiple_of_zero_by_zero",
        "is_multiple_of_five_by_zero",
    ] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify, got: {o}");
    }

    for name in [
        "signum_of_a_negative_cannot_prove_this",
        "cast_unsigned_reinterprets_negative_cannot_prove_this",
        "cast_signed_reinterprets_large_cannot_prove_this",
        "div_euclid_is_not_truncating_division_cannot_prove_this",
        "rem_euclid_is_not_the_truncating_remainder_cannot_prove_this",
        "unsigned_rem_euclid_is_not_the_signed_adjustment_cannot_prove_this",
        "unsigned_div_euclid_is_not_the_signed_adjustment_cannot_prove_this",
        "div_ceil_is_not_truncating_division_cannot_prove_this",
        "reverse_bits_is_not_the_identity_cannot_prove_this",
        "swap_bytes_is_not_the_identity_cannot_prove_this",
        "to_be_is_not_the_identity_here_cannot_prove_this",
        "from_be_is_not_the_identity_here_cannot_prove_this",
        "to_le_then_from_be_is_not_the_identity_cannot_prove_this",
        "to_ascii_uppercase_leaves_uppercase_cannot_prove_this",
        "is_multiple_of_seven_by_three_cannot_prove_this",
    ] {
        let o = outcome(&report, name);
        match o {
            FnOutcome::Failed { kind, .. } => assert!(
                kind.starts_with("postcondition"),
                "`{name}` must be refuted on the postcondition, got `{kind}`"
            ),
            other => panic!("`{name}` claims a false value and must be refuted, got: {other}"),
        }
    }
}

/// The `u32` product surface is modelled: `wrapping_mul` as a value, and a
/// `saturating_mul` whose widened `2w == 64` failure predicate is built.
///
/// `MUL_MAX_WIDTH` was 16, so every `u32` product reported "past what the model
/// covers" and `hash_step` — the flagship `u32` example — was `incomplete`. At 32
/// the value half (`wrapping_mul`) is a plain `w`-bit multiply and settles at once,
/// and the constant-multiplier `saturating_mul` folds rather than blasting the
/// 64-bit multiplier. The tripwires pin that the value is the *product* rather
/// than a constant: a model that answered `0` or the operand would prove them.
///
/// The pinned-argument equality is the discriminating half: `100000 * 31` is
/// `3_100_000`, which does not fit a 16-bit result, so a model that wrapped
/// narrower would refute the true fixture instead of proving it.
#[test]
fn the_u32_product_surface_is_modelled() {
    let names = [
        "u32_wrapping_product",
        "u32_wrapping_product_tripwire",
        "u32_saturating_product",
        "u32_saturating_product_tripwire",
    ];
    let Some(report) = verify_realistic(&names) else { return };

    for name in ["u32_wrapping_product", "u32_saturating_product"] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify, got: {o}");
    }

    for name in ["u32_wrapping_product_tripwire", "u32_saturating_product_tripwire"] {
        let o = outcome(&report, name);
        match o {
            FnOutcome::Failed { kind, .. } => assert!(
                kind.starts_with("postcondition"),
                "`{name}` must be refuted on the postcondition, got `{kind}`"
            ),
            other => panic!("`{name}` claims a false value and must be refuted, got: {other}"),
        }
    }
}

/// C1: a `drop` terminator is modelled, not dropped.
///
/// The review's exit criterion for C1 was a `guarded`/`misguarded` pair on a
/// function holding a live `Drop` local: the first verifies, the second is
/// refuted. It runs here on inline source so the shape is pinned even if the
/// corpus is edited, and it asserts the *verdict*, not merely "not incomplete" --
/// a fix that refused every `drop` would pass the second and fail the first.
#[test]
fn a_drop_terminator_is_modelled_and_its_destructor_runs() {
    let src = r#"macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }

pub struct Guard(pub u8);
impl Drop for Guard {
    fn drop(&mut self) {}
}

spec! {
    requires: x < 200,
    ensures: ret == 1,
}
pub fn guarded(x: u8) -> u8 {
    let _g = Guard(0);
    assert!(x < 200);
    1
}

spec! {
    requires: true,
    ensures: ret == 1,
}
pub fn misguarded(x: u8) -> u8 {
    let _g = Guard(0);
    assert!(x < 200);
    1
}
"#;
    let Some(g) = verify_src("guarded", src) else { return };
    assert!(
        g.is_verified(),
        "a function holding a live `Drop` local with a satisfied assert must verify, got: {g}"
    );
    let Some(m) = verify_src("misguarded", src) else { return };
    match m {
        FnOutcome::Failed { kind, .. } => assert!(
            kind.starts_with("panic"),
            "the misguarded assert must be refuted as a panic, got `{kind}`"
        ),
        other => panic!("the misguarded assert is reachable and must be refuted, got: {other}"),
    }
}

/// C1 soundness: a destructor's *body* is executed, so its panic is an obligation.
///
/// This is the direction that makes the model safe. Treating every `drop` as a
/// no-op -- the tempting way to make RAII locals decide -- would report the
/// unguarded function `verified`, but the destructor panics for `x >= 10`. The
/// guarded companion is the positive control: refusing every `drop` would leave
/// it undecided, so the pair pins both that the destructor runs and that the tool
/// is not merely refusing the shape.
#[test]
fn a_destructor_body_is_executed_and_its_panic_is_an_obligation() {
    let src = r#"macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }

pub struct PanicGuard { pub v: u8 }
impl Drop for PanicGuard {
    fn drop(&mut self) {
        assert!(self.v < 10);
    }
}

spec! {
    requires: true,
    ensures: ret == 1,
}
pub fn unguarded(x: u8) -> u8 {
    let _p = PanicGuard { v: x };
    1
}

spec! {
    requires: x < 10,
    ensures: ret == 1,
}
pub fn guarded(x: u8) -> u8 {
    let _p = PanicGuard { v: x };
    1
}
"#;
    let Some(u) = verify_src("unguarded", src) else { return };
    match u {
        FnOutcome::Failed { kind, detail } => {
            assert!(
                kind.starts_with("panic"),
                "the destructor's assert must be a refuted panic, got `{kind}`"
            );
            assert!(
                kind.contains("v < 10"),
                "the refuted obligation must be the destructor's own assert, got: `{kind}` \
                 with witness {detail}"
            );
        }
        other => panic!(
            "a destructor that panics for `x >= 10` must be refuted, got: {other} -- \
             treating the drop as a no-op would have proved it"
        ),
    }
    let Some(g) = verify_src("guarded", src) else { return };
    assert!(g.is_verified(), "the guarded destructor cannot panic, got: {g}");
}

/// C1: a container whose destructor the tool cannot represent is *refused*.
///
/// A `String` local frees the heap, which is not a bit-vector fact. Assuming the
/// `drop` were a no-op would be a false proof whenever the destructor's effect is
/// observable, so the honest answer is a refusal -- not a verdict. This is the
/// case the dump cannot tell apart from a struct holding a user-`Drop` field (both
/// print `drop(_x) -> [return: …]`), which is why the no-op is not taken here.
#[test]
fn an_unmodelled_drop_is_refused_not_assumed_away() {
    let src = r#"macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }

spec! {
    requires: true,
    ensures: ret == 1,
}
pub fn owns_string(s: String) -> u8 {
    let _owned = s;
    1
}
"#;
    let Some(o) = verify_src("owns_string", src) else { return };
    match o {
        FnOutcome::Incomplete { .. } | FnOutcome::Skipped { .. } => {}
        other => panic!(
            "a `String` drop is not representable and must be refused, got: {other} -- \
             a verdict here would be a false proof"
        ),
    }
}

/// C1, on the corpus: the `Drop` fixtures decide, and the false ones are refuted.
///
/// The review's exit criterion is stated on `guard_guarded` (verifies) and
/// `guard_misguarded` (refuted). This asserts the corpus copies of those, plus the
/// destructor-panic pair, so a regression in the corpus cannot pass unnoticed --
/// `realistic.rs` is scored by a floor, and a fixture that flipped to `incomplete`
/// would lower the score without failing the floor test.
#[test]
fn the_drop_fixtures_decide_and_the_false_ones_are_refuted() {
    let names = [
        "guard_is_dropped_cleanly",
        "guard_guarded",
        "destructor_panic_guarded",
        "guard_misguarded_cannot_prove_this",
        "destructor_panic_cannot_prove_this",
        "string_drop_is_refused",
    ];
    let Some(report) = verify_realistic(&names) else { return };

    for name in [
        "guard_is_dropped_cleanly",
        "guard_guarded",
        "destructor_panic_guarded",
    ] {
        let o = outcome(&report, name);
        assert!(o.is_verified(), "`{name}` must verify, got: {o}");
    }
    for name in [
        "guard_misguarded_cannot_prove_this",
        "destructor_panic_cannot_prove_this",
    ] {
        let o = outcome(&report, name);
        match o {
            FnOutcome::Failed { kind, .. } => assert!(
                kind.starts_with("panic"),
                "`{name}` must be refuted as a reachable panic, got `{kind}`"
            ),
            other => panic!(
                "`{name}` has a reachable panic and must be refuted, got: {other} -- \
                 treating the `drop` as a no-op, or dropping the path at it, would hide it"
            ),
        }
    }
    // A container drop is a refusal, not a verdict.
    match outcome(&report, "string_drop_is_refused") {
        FnOutcome::Incomplete { .. } | FnOutcome::Skipped { .. } => {}
        other => panic!("a `String` drop must be refused, got: {other}"),
    }
}

/// A combinator that *destroys* its unused operand must respect that operand's
/// drop glue, or it is a false proof.
///
/// `Result::unwrap_or_default` consumes the receiver and destroys the error value
/// on the `Err` branch. The model checked only that the payload and the default
/// were scalar words, so a `Result<u8, Boom>` whose `Boom` destructor panics was
/// reported panic-free -- while a real call unwinds. The same holds for
/// `unwrap_or`, `or`, and `and`, which drop the operand they do not return. Each
/// is now refused when the dropped type's glue is not representable, which is the
/// honest answer rather than an assumed no-op.
#[test]
fn a_combinator_that_destroys_a_value_respects_its_drop_glue() {
    let src = r#"macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }

pub struct Boom;
impl Drop for Boom {
    fn drop(&mut self) {
        panic!("boom");
    }
}

spec! {
    requires: true,
    ensures: ret <= 255,
}
pub fn boom_default(r: Result<u8, Boom>) -> u8 {
    r.unwrap_or_default()
}

spec! {
    requires: true,
    ensures: ret <= 255,
}
pub fn boom_or(r: Result<u8, Boom>) -> u8 {
    r.unwrap_or(7)
}
"#;
    for name in ["boom_default", "boom_or"] {
        let Some(o) = verify_src(name, src) else { return };
        // Pinned as `Incomplete`, not `Incomplete | Skipped`: the contracts here
        // (`ret <= 255`) are readable, so a `Skipped` would mean the contract
        // stopped being read -- a different defect that this check should not
        // silently accept. Measured, both are `Incomplete` (the destructor's glue
        // is not representable), so the exact kind is the honest pin.
        match o {
            FnOutcome::Incomplete { .. } => {}
            other => panic!(
                "`{name}` destroys a `Boom` whose destructor panics and must be refused \
                 as `Incomplete`, got: {other} -- a `verified` here is a false proof"
            ),
        }
    }
}

/// Review round: field drop glue, `unwrap_or`, unwind accounting, edition
/// resolution, and rotate widths.
///
/// Nine findings from an adversarial review, each reproduced before being fixed.
/// They fall into four families: drop glue (F1/F2/F3), unwinding (F4/F5), edition
/// resolution (F6/F7), and width/precision (F8/F9).
mod review_round {
    use super::*;

    /// F1/F3: a struct whose field has its own destructor must not be treated as
    /// fully dropped after the outer user destructor.
    ///
    /// `Outer { inner: Inner }` with `impl Drop for Outer` and `impl Drop for
    /// Inner` destroys *both*: the user body runs, then each field's glue. The
    /// model ran the user body and stopped, so a panicking `Inner::drop` was never
    /// seen and the function was a false `Verified`. The fix refuses the shape --
    /// a field whose type has glue this tool cannot execute -- which is the honest
    /// answer: the drop is unmodelled, not proved safe.
    #[test]
    fn a_field_with_its_own_destructor_is_refused_not_assumed_safe() {
        let src = r#"macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }

pub struct Inner { pub v: u8 }
impl Drop for Inner {
    fn drop(&mut self) { assert!(self.v < 10); }
}
pub struct Outer { pub inner: Inner }
impl Drop for Outer {
    fn drop(&mut self) {}
}

spec! {
    requires: true,
    ensures: ret == 1,
}
pub fn outer_with_panicking_field(x: u8) -> u8 {
    let _o = Outer { inner: Inner { v: x } };
    1
}

spec! {
    requires: x < 10,
    ensures: ret == 1,
}
pub fn outer_guarded(x: u8) -> u8 {
    let _o = Outer { inner: Inner { v: x } };
    1
}
"#;
        // The unguarded one must NOT be `Verified`: the field's destructor panics
        // for `x >= 10`, and the model cannot run it, so it is a refusal.
        let Some(u) = verify_src("outer_with_panicking_field", src) else { return };
        assert!(
            !u.is_verified(),
            "a struct whose field has a panicking destructor must not be `Verified`, got: {u}"
        );
        // The guarded companion is refused too (same shape), which is the point:
        // the refusal is about the *shape*, not about whether the panic is
        // reachable. It must not be a false `Verified`.
        let Some(g) = verify_src("outer_guarded", src) else { return };
        assert!(
            !g.is_verified(),
            "the guarded companion is the same unmodelled shape and must not verify, got: {g}"
        );
    }

    /// F2: `Result::unwrap_or` destroys the error value on the `Err` branch, so a
    /// panicking error destructor must not be assumed away.
    ///
    /// `r.unwrap_or(0)` on `Result<u8, E>` with `impl Drop for E { panic }` panics
    /// on the `Err` branch, but the model treated `unwrap_or` as total (it has no
    /// panic obligation for the *tag*). The fix reads the error type from the
    /// receiver and refuses when its glue is unmodelled; a scalar error type still
    /// decides, which is the positive control that the refusal is not blanket.
    #[test]
    fn unwrap_or_refuses_an_error_type_whose_drop_is_unmodelled() {
        let src = r#"macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }

pub struct Boom;
impl Drop for Boom {
    fn drop(&mut self) { assert!(false); }
}

spec! {
    requires: true,
    ensures: ret <= 255,
}
pub fn unwrap_or_dropping_error(r: Result<u8, Boom>) -> u8 {
    r.unwrap_or(0)
}

spec! {
    requires: true,
    ensures: ret <= 255,
}
pub fn unwrap_or_scalar_error(r: Result<u8, u8>) -> u8 {
    r.unwrap_or(0)
}
"#;
        let Some(d) = verify_src("unwrap_or_dropping_error", src) else { return };
        assert!(
            !d.is_verified(),
            "`unwrap_or` on an error type with a panicking destructor must not verify, got: {d}"
        );
        let Some(s) = verify_src("unwrap_or_scalar_error", src) else { return };
        assert!(
            s.is_verified(),
            "`unwrap_or` on a scalar error type must still verify (positive control), got: {s}"
        );
    }

    /// F4/F5: the unwind bound counts *latch traversals*, not raw block entries.
    ///
    /// A loop that runs exactly `N` iterations needs `N + 1` entries to its header
    /// (the last evaluates the guard and exits), so `unwind: N` rejected it. A
    /// loop-free helper called more than once from one function also tripped the
    /// bound, because the counter was keyed by block without regard to *which*
    /// invocation. Both are fixed by counting back-edge traversals; the bound is
    /// now the number of iterations, which is what a caller means.
    #[test]
    fn the_unwind_bound_counts_iterations_not_header_entries() {
        let src = r#"macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }

spec! {
    requires: true,
    unwind: 3,
    ensures: ret == 3,
}
pub fn while_exact() -> u32 {
    let mut i = 0u32;
    let mut s = 0u32;
    while i < 3 { s += 1; i += 1; }
    s
}

spec! {
    requires: true,
    unwind: 3,
    ensures: ret == 3,
}
pub fn for_exact() -> u32 {
    let mut s = 0u32;
    for _i in 0..3u32 { s += 1; }
    s
}

fn helper(x: u8) -> u8 { if x == 0 { 0 } else { 1 } }

spec! {
    requires: x == 0,
    unwind: 1,
    ensures: ret == 0,
}
pub fn loop_free_helper_calls(x: u8) -> u8 {
    let a = helper(x);
    let b = helper(0);
    a + b
}
"#;
        for name in ["while_exact", "for_exact", "loop_free_helper_calls"] {
            let Some(o) = verify_src(name, src) else { return };
            assert!(
                o.is_verified(),
                "`{name}` must verify: the bound counts iterations, so `unwind: N` \
                 admits a loop that runs `N` times, and helper calls do not consume it; got: {o}"
            );
        }
    }

    /// F4: a bound one short of the iterations must still fail.
    ///
    /// The fix must not simply relax the bound: `unwind: 2` on a three-iteration
    /// loop is a claim the loop violates, and it has to be refuted. Without this
    /// the latch fix could pass by accepting every bound.
    #[test]
    fn an_unwind_bound_below_the_iterations_still_fails() {
        let src = r#"macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }

spec! {
    requires: true,
    unwind: 2,
    ensures: ret == 3,
}
pub fn for_too_few() -> u32 {
    let mut s = 0u32;
    for _i in 0..3u32 { s += 1; }
    s
}
"#;
        let Some(o) = verify_src("for_too_few", src) else { return };
        match o {
            FnOutcome::Failed { kind, .. } => assert!(
                kind.contains("unwind"),
                "a too-small bound must fail on the unwind obligation, got `{kind}`"
            ),
            other => panic!("`unwind: 2` on a 3-iteration loop must be refuted, got: {other}"),
        }
    }

    /// F6/F7: edition resolution reads a quoted key and an explicit workspace path.
    ///
    /// Two spellings Cargo accepts and the reader did not: a quoted key
    /// (`"edition" = "2021"`) read as *no* edition, so the 2015 default applied;
    /// and `package.workspace = "../ws"` pointing at a root that is not an
    /// ancestor, which the ancestor search could not find. Both are resolved from
    /// the manifest text, so they are unit-tested here rather than end to end.
    #[test]
    fn a_quoted_edition_key_and_an_explicit_workspace_path_are_resolved() {
        let dir = std::env::temp_dir().join(format!("verif-edition-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&dir);
        std::fs::create_dir_all(dir.join("member")).unwrap();
        std::fs::create_dir_all(dir.join("ws")).unwrap();
        std::fs::write(dir.join("ws/Cargo.toml"), "[workspace]\n[workspace.package]\nedition = \"2021\"\n").unwrap();
        std::fs::write(
            dir.join("member/Cargo.toml"),
            "[package]\nname = \"m\"\n\"edition\" = \"2024\"\n",
        )
        .unwrap();
        std::fs::write(dir.join("member/src.rs"), "").unwrap();
        let member = dir.join("member/src.rs");
        assert_eq!(
            verify::edition_from_manifest(&member),
            Some(verify::Edition::Rust2024),
            "a quoted `\"edition\"` key must be read, not defaulted to 2015"
        );

        std::fs::write(
            dir.join("member/Cargo.toml"),
            "[package]\nname = \"m\"\nworkspace = \"../ws\"\nedition.workspace = true\n",
        )
        .unwrap();
        assert_eq!(
            verify::edition_from_manifest(&member),
            Some(verify::Edition::Rust2021),
            "`package.workspace = \"../ws\"` must resolve to that root's edition"
        );
        let _ = std::fs::remove_dir_all(&dir);
    }

    /// C3: the `for`-loop desugaring over a range is modelled, both directions.
    ///
    /// A `for i in a..b` lowers to `Range::new`/`RangeInclusive::new`, an
    /// `IntoIterator::into_iter` identity and a `next(&mut range)` loop, and all
    /// three are answered from the range's struct fields. The `unwind:` bound
    /// decides the loop without an invariant, and an `invariant:` that does not
    /// name the loop variable decides it too (`tri_invariant`). An invariant that
    /// *names* the loop variable is the one shape still refused: the element `i`
    /// is a `debug` binding inside a nested `scope` block that is not live at the
    /// loop header (`next` has not returned yet on entry), so `invariant_names`
    /// does not resolve it -- recorded in `ROADMAP.md` C3. The false twin is the
    /// evidence the range state is *read*: a model that returned a constant would
    /// prove it.
    ///
    /// The range's iterator state is written *through `&mut`*, so the loop header
    /// must havoc the range local (and its parts). When it did not, the exit guard
    /// folded to `false`, the exit edge was pruned as unreachable, and a false
    /// postcondition came back `Verified` -- the reason `tri_invariant` now uses an
    /// inductive invariant (`s = i.min(5050)`, not `s += i`) and
    /// `tri_invariant_cannot_prove_this` is asserted to be refuted.
    #[test]
    fn the_for_loop_range_desugaring_is_modelled() {
        let src = r#"macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }

spec! {
    requires: n <= 3,
    unwind: 4,
    ensures: ret <= 6,
}
pub fn for_inclusive_sum(n: u32) -> u32 {
    let mut s = 0;
    for i in 0..=n { s += i; }
    s
}

spec! {
    requires: n <= 3,
    unwind: 4,
    ensures: ret <= 5,
}
pub fn for_inclusive_sum_cannot_prove_this(n: u32) -> u32 {
    let mut s = 0;
    for i in 0..=n { s += i; }
    s
}

spec! {
    requires: true,
    unwind: 3,
    ensures: ret == 3,
}
pub fn for_exclusive_counts() -> u32 {
    let mut s = 0;
    for _i in 0..3u32 { s += 1; }
    s
}

spec! {
    requires: n <= 100,
    invariant: s <= 5050,
    ensures: ret <= 5050,
}
pub fn tri_invariant(n: u32) -> u32 {
    let mut s = 0;
    for i in 0..=n { s = i.min(5050); }
    s
}

spec! {
    requires: n <= 100,
    invariant: s <= 5050,
    ensures: ret <= 3,
}
pub fn tri_invariant_cannot_prove_this(n: u32) -> u32 {
    let mut s = 0;
    for i in 0..=n { s = i.min(5050); }
    s
}
"#;
        for name in ["for_inclusive_sum", "for_exclusive_counts", "tri_invariant"] {
            let Some(o) = verify_src(name, src) else { return };
            assert!(
                o.is_verified(),
                "`{name}` must verify: the range desugaring is modelled and the loop is \
                 decided by `unwind:` or by an invariant, got: {o}"
            );
        }
        let Some(f) = verify_src("for_inclusive_sum_cannot_prove_this", src) else { return };
        match f {
            FnOutcome::Failed { kind, .. } => assert!(
                kind.contains("postcondition"),
                "the false twin must fail on its postcondition, got `{kind}`"
            ),
            other => panic!(
                "the false twin claims `ret <= 5` for a sum that reaches 6 and must be \
                 refuted; a model that ignored the range state would prove it, got: {other}"
            ),
        }
        // The invariant tripwire: a `for` loop whose postcondition is false. The
        // loop's *iterator state* is written through `&mut`, so the havoc at the
        // header must reach it. When it did not, the exit guard folded to `false`,
        // the exit edge was pruned as unreachable, and this function came back
        // `Verified` -- a false proof. It must now be refuted.
        //
        // The assertion is `Failed` on the *postcondition*, not merely
        // `!is_verified()`. A refusal (`Incomplete`/`Skipped`) also satisfies
        // "not verified", so a model that gave up on the range state would pass a
        // weaker check while checking nothing -- the tripwire would be vacuous. A
        // refuted postcondition is the evidence the loop was modelled and its exit
        // state refuted the false claim.
        let Some(g) = verify_src("tri_invariant_cannot_prove_this", src) else { return };
        match g {
            FnOutcome::Failed { kind, .. } => assert!(
                kind.contains("postcondition"),
                "the invariant tripwire must fail on its postcondition, got `{kind}`"
            ),
            other => panic!(
                "a `for` loop whose postcondition is false must be refuted, not merely \
                 undecided; a model that does not havoc the iterator state proves it, and \
                 one that refuses it checks nothing, got: {other}"
            ),
        }
    }

    /// A loop whose body calls a *recursive* function must terminate the write-set
    /// discovery, and a reference-carrying recursive callee must still have its
    /// write propagated.
    ///
    /// The write-set walk follows each resolved callee to find what it writes
    /// through a reference. Before it was cycle-aware, each invocation allocated
    /// its own block-visit set, which stops a CFG cycle within one function and
    /// cannot stop a call cycle *between* invocations: a loop calling a
    /// self-recursive function followed the self-call forever and overflowed the
    /// stack, aborting the whole run. The walk now carries the set of functions on
    /// the call stack, which is what terminates it. On a cycle it also includes the
    /// call's reference arguments -- a conservative superset, since a callee can
    /// only reach caller storage through a reference; the reference twin below pins
    /// that the write still reaches the loop's havoc set, whichever mechanism
    /// carries it.
    #[test]
    fn a_loop_that_calls_a_recursive_function_terminates_and_is_sound() {
        let src = r#"macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }

spec! {
    requires: true,
    decreases: n,
    ensures: ret == n,
}
pub fn count_down(n: u8) -> u8 {
    if n == 0 { 0 } else { count_down(n - 1) + 1 }
}

spec! {
    requires: true,
    decreases: n,
    ensures: ret == 0,
}
pub fn walk(n: u8, c: &mut u8) -> u8 {
    if n == 0 { 0 } else { *c = c.wrapping_add(1); walk(n - 1, c) }
}

spec! {
    requires: n <= 3,
    invariant: i <= 3,
    ensures: ret <= 3,
}
pub fn loop_calls_recursive(n: u8) -> u8 {
    let mut i = 0u8; let mut s = 0u8;
    while i < 3 { s = count_down(n); i += 1; }
    s
}

spec! {
    requires: n <= 3,
    invariant: i <= 3,
    ensures: ret == 0,
}
pub fn loop_calls_recursive_cannot_prove_this(n: u8) -> u8 {
    let mut i = 0u8; let mut s = 0u8;
    while i < 3 { s = count_down(n); i += 1; }
    s
}

spec! {
    requires: x == 0,
    invariant: i <= 3,
    ensures: ret <= 255,
}
/// A loop calling a *reference-carrying* recursive callee: the call cycle is
/// refused rather than followed, and the call's reference argument is added
/// directly. `x` is written through `walk`, so the exit state cannot keep `0`.
pub fn loop_calls_ref_recursive(x: &mut u8) -> u8 {
    let mut i = 0u8;
    while i < 3 { walk(1, x); i += 1; }
    *x
}

spec! {
    requires: x == 0,
    invariant: i <= 3,
    ensures: ret == 0,
}
/// The false twin: `walk` writes `x`, so `ret == 0` is false. A model that
/// dropped the reference from the cycle's havoc would prove it.
pub fn loop_calls_ref_recursive_cannot_prove_this(x: &mut u8) -> u8 {
    let mut i = 0u8;
    while i < 3 { walk(1, x); i += 1; }
    *x
}

spec! {
    requires: n <= 8,
    invariant: i <= 3,
    ensures: ret <= 1,
}
/// A *mutual* recursion cycle reached from an invariant loop: `even_w` calls
/// `odd_w` calls `even_w`. The write-set walk must terminate on the cycle
/// (`visiting` holds the functions on the stack), and neither callee writes a
/// reference, so the loop's only write is its own counter. Before the cycle
/// guard this was an unbounded recursion between *invocations* and overflowed the
/// stack, aborting the whole run.
pub fn loop_calls_mutual_recursive(n: u8) -> u8 {
    let mut i = 0u8;
    let mut s = 0u8;
    while i < 3 { s = even_w(n); i += 1; }
    s
}

spec! {
    requires: true,
    decreases: n,
    ensures: ret <= 1,
}
pub fn even_w(n: u8) -> u8 {
    if n == 0 { 1 } else { odd_w(n - 1) }
}

spec! {
    requires: true,
    decreases: n,
    ensures: ret <= 1,
}
pub fn odd_w(n: u8) -> u8 {
    if n == 0 { 0 } else { even_w(n - 1) }
}
"#;
        // The crash vehicle: a scalar-only call into a self-recursive callee. The
        // walk must *return* (a stack overflow aborts the binary, so reaching the
        // assertion at all is half the evidence), and the loop must be decided --
        // the false twin refuted on its postcondition, not skipped.
        for name in [
            "loop_calls_recursive",
            "loop_calls_recursive_cannot_prove_this",
            "loop_calls_ref_recursive",
            "loop_calls_ref_recursive_cannot_prove_this",
        ] {
            let Some(o) = verify_src(name, src) else { return };
            assert!(
                !matches!(o, FnOutcome::Skipped { .. } | FnOutcome::Incomplete { .. }),
                "`{name}` must be decided, not refused: a cycle-aware write-set walk \
                 terminates; a refusal here means the walk gave up, got: {o}"
            );
        }
        // A loop reaching a *mutually* recursive callee. Mutual recursion has no
        // single decreasing measure here, so it is not *proved* -- but the
        // write-set walk must still terminate on the cycle (reaching this
        // assertion at all is the evidence: an unbounded walk aborts the binary),
        // and the loop must never come back `Verified`, which would be a proof of
        // an unmodelled call.
        let Some(m) = verify_src("loop_calls_mutual_recursive", src) else { return };
        assert!(
            !m.is_verified(),
            "a loop reaching a mutually recursive callee must not be *proved* (mutual \
             recursion is not modelled), got: {m}"
        );
        let Some(f) = verify_src("loop_calls_recursive_cannot_prove_this", src) else {
            return;
        };
        match f {
            FnOutcome::Failed { kind, .. } => assert!(
                kind.contains("postcondition"),
                "the false twin must fail on its postcondition, got `{kind}`"
            ),
            other => panic!(
                "the false twin claims `ret == 0` for a loop returning `n` and must be \
                 refuted, got: {other}"
            ),
        }
        // The reference-carrying twin is the assertion the earlier version of this
        // test lacked: a loop calling a *reference-carrying* recursive callee must
        // still have that reference havoced at the header, so the exit state cannot
        // keep the entry `0`. It pins the *effect* (the reference write reaches the
        // exit), not which mechanism carries it: measured, the callee's own direct
        // scan folded back positionally is what carries it for this shape, and the
        // cycle branch's reference inclusion is a conservative superset over it.
        let Some(g) = verify_src("loop_calls_ref_recursive_cannot_prove_this", src) else {
            return;
        };
        match g {
            FnOutcome::Failed { kind, .. } => assert!(
                kind.contains("postcondition"),
                "the reference twin must fail on its postcondition, got `{kind}`"
            ),
            other => panic!(
                "`walk` writes `x` through its reference argument, so `ret == 0` must be \
                 refuted; a `verified` here means the reference write did not reach the \
                 loop's havoc set, got: {other}"
            ),
        }
    }

    /// C3 exit criterion, second half: a loop with no contract is reported as a
    /// *missing loop contract*, not as an unmodelled construct.
    ///
    /// `tri` in `everyday.rs` declares no `invariant:` or `unwind:`, so its loop is
    /// unwound to the default and the path stops. That is an honest incompleteness,
    /// but the message has to point at the remedy -- the loop is understood, and the
    /// tool is declining to guess an exit -- rather than reading as "the frontend
    /// could not model this".
    #[test]
    fn a_loop_without_a_contract_names_the_missing_contract() {
        let src = r#"macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }

spec! {
    requires: true,
    ensures: true,
}
pub fn tri(n: u32) -> u32 {
    let mut s = 0;
    for i in 0..=n { s += i; }
    s
}
"#;
        let Some(o) = verify_src("tri", src) else { return };
        match o {
            FnOutcome::Incomplete { reasons, .. } => {
                let joined = reasons.join(" | ");
                assert!(
                    joined.contains("missing loop contract")
                        && joined.contains("invariant:")
                        && joined.contains("unwind:"),
                    "the reason must name the missing loop contract and the two ways to \
                     supply it, got: {joined}"
                );
            }
            other => panic!(
                "`tri` declares no loop contract and must be an honest incompleteness, \
                 got: {other}"
            ),
        }
    }
}

/// Regression tests for the signed-inclusive-range review (R1–R6).
///
/// These live outside `review_round` because they pin a *different* surface:
/// the `RangeInclusive::next` model's boundary arithmetic (R1), its state on an
/// exhausted range (R4), the unwind counter's key (R2/R3), and the struct layout
/// the drop-glue check reads (R5/R6). Every case was cross-checked against a real
/// `rustc` execution oracle, so the expected counts are Rust's, not the model's.
mod range_and_layout_review {
    use super::*;

    /// R1/R4: a signed inclusive range's `next` must stop at the boundary and must
    /// not advance an exhausted range.
    ///
    /// The successor's overflow flag was tested only for the *unsigned* rule
    /// (`succ == 0`), which never fires on a signed range: `127i8..=127i8` kept
    /// yielding past `127` into `-128`, `-127`, ... until the `unwind:` bound, so a
    /// true contract became a bound failure. The flag must test the *signed* `MIN`.
    /// And `next` on an empty range must leave the range unchanged (Rust does not
    /// advance `start` on `None`), or a later call resurrects an element.
    ///
    /// The counts are Rust's own, read from a compiled oracle, so the assertion is
    /// the language's semantics rather than the model's.
    #[test]
    fn a_signed_inclusive_range_stops_at_its_boundary() {
        let src = r#"macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }

spec! {
    requires: true,
    unwind: 8,
    ensures: ret == 1,
}
pub fn single_signed_incl() -> u32 {
    let mut c = 0u32;
    for _ in 127i8..=127i8 { c += 1; }
    c
}

spec! {
    requires: true,
    unwind: 8,
    ensures: ret == 2,
}
pub fn two_signed_incl() -> u32 {
    let mut c = 0u32;
    for _ in 126i8..=127i8 { c += 1; }
    c
}

spec! {
    requires: true,
    unwind: 8,
    ensures: ret == 3,
}
pub fn three_signed_incl() -> u32 {
    let mut c = 0u32;
    for _ in (-128i8)..=(-126i8) { c += 1; }
    c
}

spec! {
    requires: true,
    unwind: 4,
    ensures: ret == 1,
}
pub fn single_unsigned_incl() -> u32 {
    let mut c = 0u32;
    for _ in 255u8..=255u8 { c += 1; }
    c
}
"#;
        for (name, want) in [
            ("single_signed_incl", 1u32),
            ("two_signed_incl", 2),
            ("three_signed_incl", 3),
            ("single_unsigned_incl", 1),
        ] {
            let Some(o) = verify_src(name, src) else { return };
            assert!(
                o.is_verified(),
                "`{name}` must verify with {want} iteration(s); a signed range whose \
                 overflow flag tests zero keeps yielding past its bound, got: {o}"
            );
        }
    }

    /// R4: `next` on an empty inclusive range is idempotent.
    ///
    /// `255u8..=254u8` is empty, so both `next` calls are `None`. A model that
    /// advanced `start` on the first `None` would resurrect an element on the
    /// second call (`Some(0)`), turning a true contract into a refutation.
    #[test]
    fn an_empty_inclusive_range_stays_empty() {
        let src = r#"macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }

spec! {
    requires: true,
    ensures: ret == 0,
}
pub fn empty_incl_twice() -> u8 {
    let mut r = 255u8..=254u8;
    let _a = r.next();
    let b = r.next();
    match b { Some(_) => 1, None => 0 }
}

spec! {
    requires: true,
    ensures: ret == 0,
}
pub fn empty_excl_twice() -> u8 {
    let mut r = 255u8..255u8;
    let _a = r.next();
    let b = r.next();
    match b { Some(_) => 1, None => 0 }
}
"#;
        for name in ["empty_incl_twice", "empty_excl_twice"] {
            let Some(o) = verify_src(name, src) else { return };
            assert!(
                o.is_verified(),
                "`{name}` must verify: `next` leaves an empty range unchanged, so the \
                 second call is `None` too, got: {o}"
            );
        }
    }

    /// R2/R3: the unwind bound counts iterations of the *loop*, not of one latch.
    ///
    /// A loop body with two back-edge blocks (an `if` whose arms both reach the
    /// header) had each latch counted separately, so the combined trip count could
    /// exceed a declared `unwind:` while neither latch's own count did -- a
    /// two-iteration loop verified under `unwind: 1`. The counter is now keyed by
    /// the loop header, and a `switchInt` block no longer clears it.
    #[test]
    fn the_unwind_bound_counts_the_whole_loop_not_one_latch() {
        let src = r#"macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }

spec! {
    requires: n == 2,
    unwind: 1,
    ensures: ret == 2,
}
pub fn two_latches(n: u8) -> u8 {
    let mut i = 0u8;
    while i < n {
        if i == 0 { i += 1; } else { i += 1; }
    }
    i
}

spec! {
    requires: n == 1,
    unwind: 1,
    ensures: ret == 1,
}
pub fn one_latch(n: u8) -> u8 {
    let mut i = 0u8;
    while i < n {
        if i == 0 { i += 1; } else { i += 1; }
    }
    i
}
"#;
        let Some(f) = verify_src("two_latches", src) else { return };
        assert!(
            !f.is_verified(),
            "a two-iteration loop under `unwind: 1` must run off the bound, got: {f}"
        );
        let Some(o) = verify_src("one_latch", src) else { return };
        assert!(
            o.is_verified(),
            "a one-iteration loop under `unwind: 1` must verify (positive control), got: {o}"
        );
    }

    /// R5: two structs of the same bare name in different modules must not share a
    /// layout.
    ///
    /// The struct table was keyed by the bare name, so `m::Outer` and `n::Outer`
    /// collided and the drop-glue check read whichever was recorded last. When that
    /// one is glue-free, a panicking field destructor vanishes and the function is
    /// a false `Verified`. The layout is now keyed by the module-qualified path.
    #[test]
    fn two_same_named_structs_do_not_share_a_layout() {
        let src = r#"macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }

pub mod m {
    pub struct Outer { pub inner: Inner }
    pub struct Inner(pub u8);
    impl Drop for Inner {
        fn drop(&mut self) { if self.0 == 0 { panic!("inner"); } }
    }
    impl Drop for Outer { fn drop(&mut self) {} }
}
pub mod n {
    pub struct Outer { pub only: u8 }
}

spec! {
    requires: true,
    ensures: ret == 1,
}
pub fn same_name() -> u8 {
    let _g = m::Outer { inner: m::Inner(0) };
    1
}
"#;
        let Some(o) = verify_src("same_name", src) else { return };
        assert!(
            !o.is_verified(),
            "`m::Outer` has a field with a panicking destructor; sharing a layout with \
             `n::Outer` would hide it and prove this, got: {o}"
        );
    }

    /// A **qualified** type name must not be answered by a *bare* layout.
    ///
    /// The dump prints a type by its shortest crate-unique path, so a qualified
    /// spelling (`m::Outer`) means rustc could **not** shorten it -- at least two
    /// types share that tail. The bare key, when it exists at all, is then some
    /// other type's layout, and reading it proves a contract about the wrong
    /// fields. This was a false proof: with a crate-root `Outer { lo, hi }` and a
    /// one-line `pub mod m { pub struct Outer { hi, lo } }` (whose inline struct
    /// the line-oriented scanner never sees), the only layout recorded was the
    /// crate-root one, both field projections resolved to *it*, and
    /// `requires: o.hi == 5; ensures: ret == 5` on a body returning `o.lo` was
    /// **verified**. Rustc's own execution returns `7` for `{ hi: 5, lo: 7 }`.
    ///
    /// Both field directions are asserted, so the fix cannot pass by resolving
    /// one projection and refusing the other. The *positive* control is the next
    /// test: the same shapes with the module's struct on its own lines do verify,
    /// which is what shows this is about the one-line spelling and not about
    /// qualified names in general.
    #[test]
    fn a_qualified_type_is_not_answered_by_a_bare_layout() {
        let src = r#"macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }

pub struct Outer { pub lo: u8, pub hi: u8 }
pub mod m { pub struct Outer { pub hi: u8, pub lo: u8 } }

spec! {
    requires: o.hi == 5,
    ensures: ret == 5,
}
pub fn reads_the_other_field(o: &m::Outer) -> u8 { o.lo }

spec! {
    requires: o.lo == 5,
    ensures: ret == 5,
}
pub fn reads_the_other_field_mirror(o: &m::Outer) -> u8 { o.hi }
"#;
        for name in ["reads_the_other_field", "reads_the_other_field_mirror"] {
            let Some(o) = verify_src(name, src) else { return };
            assert!(
                !o.is_verified(),
                "`m::Outer`'s layout is not in the source the scanner read, so a contract \
                 about its fields must be refused -- proving it means the crate-root \
                 `Outer`'s fields were read instead. Got: {o}"
            );
        }
    }

    /// A multi-line module's struct *is* read, so the refusal above is about the
    /// one-line spelling and not about modules in general. Without this control a
    /// fix that simply refused every `m::`-qualified name would pass the test
    /// above.
    #[test]
    fn a_module_struct_on_its_own_lines_still_reads_its_fields() {
        let src = r#"macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }

pub struct Outer { pub lo: u8, pub hi: u8 }
pub mod m {
    pub struct Outer { pub hi: u8, pub lo: u8 }
}

spec! {
    requires: o.hi == 5,
    ensures: ret == 5,
}
pub fn reads_its_own_field(o: &m::Outer) -> u8 { o.hi }
"#;
        let Some(o) = verify_src("reads_its_own_field", src) else { return };
        assert!(
            o.is_verified(),
            "a module struct whose body is on its own lines has a real layout, so a true \
             contract about it must verify. Got: {o}"
        );
    }

    /// R6: a unit struct with a `Drop` impl is a zero-field layout, not an unknown
    /// container.
    ///
    /// `struct Guard;` was not read as a struct at all, so the drop-glue check saw
    /// an unknown type and refused the function. A unit struct has no fields, so its
    /// layout is empty and its `Drop` body is the only glue -- which runs, and a
    /// panicking one must be a real obligation.
    #[test]
    fn a_unit_struct_runs_its_destructor() {
        let src = r#"macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }

pub struct Guard;
impl Drop for Guard { fn drop(&mut self) {} }

spec! {
    requires: true,
    ensures: ret == 1,
}
pub fn unit_drop() -> u8 {
    let _g = Guard;
    1
}

pub struct Boom;
impl Drop for Boom { fn drop(&mut self) { panic!("boom"); } }

spec! {
    requires: true,
    ensures: ret == 1,
}
pub fn panics_on_drop() -> u8 {
    let _g = Boom;
    1
}
"#;
        let Some(o) = verify_src("unit_drop", src) else { return };
        assert!(
            o.is_verified(),
            "a unit struct with a no-op destructor must verify, got: {o}"
        );
        let Some(p) = verify_src("panics_on_drop", src) else { return };
        assert!(
            !p.is_verified(),
            "a unit struct whose destructor panics must not verify, got: {p}"
        );
    }
}

/// A comment *before* a clause's colon is whitespace, and Rust's matcher allows it.
///
/// The matcher is `$($clause:ident : $body:expr,)*`, so `requires/**/: n > 0` is a
/// precondition. The separator fix turns the comment into a space -- `requires : n > 0`
/// -- and `take_clause` matched the literal `requires:`, so the clause was refused as
/// unknown and the whole function was *skipped*: a lost proof on valid syntax. The
/// contract here is true, so only reading both clauses can verify it; the second
/// function carries a false postcondition behind the same spelling, so "verified"
/// cannot be the clauses being dropped.
#[test]
fn a_comment_before_a_clause_colon_is_whitespace_not_a_dropped_clause() {
    let macros = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\n\n";
    let true_src = format!("{macros}spec! {{\n    requires/**/: n > 0,\n    ensures/**/: ret == n,\n}}\npub fn f(n: u8) -> u8 {{ n }}\n");
    let outcome = verify_src("f", &true_src).expect("the function must be reported");
    assert!(
        matches!(outcome, FnOutcome::Verified { .. }),
        "a comment before the colon must not drop the clause, got: {outcome:?}"
    );
    let false_src = format!("{macros}spec! {{\n    requires/**/: n > 0,\n    ensures/**/: ret == n + 1,\n}}\npub fn g(n: u8) -> u8 {{ n }}\n");
    let bad = verify_src("g", &false_src).expect("the function must be reported");
    assert!(
        matches!(bad, FnOutcome::Failed { .. }),
        "a false postcondition behind a comment must still be refuted, got: {bad:?}"
    );
}

/// A comment between two tokens separates them, and a non-ASCII identifier is read
/// as written -- both are false-proof shapes when the block reader gets them wrong.
///
/// `ensures: ret == n/**/as/**/u8` is the cast `n as u8`. Deleting the comment glued
/// the tokens into `nasu8`, so a body returning an *argument* of that name satisfied
/// the clause while the real postcondition (`ret == n as u8`) is false. The `µ` case
/// is the same defect through `byte as char`: `µ` decoded to `µ`, a different
/// identifier, and a body returning an argument named `µ` verified it.
///
/// Both bodies here return the *mangled* spelling, so the verdict discriminates:
/// read correctly the clause is false and must be refuted; read under the bug it is
/// true and would be proved. That is the shape the ledger calls a false proof.
#[test]
fn a_comment_and_a_non_ascii_name_cannot_smuggle_a_false_proof() {
    let comment_src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\n\nspec! {\n    requires: true,\n    ensures: ret == n/**/as/**/u8,\n}\npub fn f(n: u8, nasu8: u8) -> u8 { let _ = n; nasu8 }\n";
    let uni_src = "macro_rules! spec { ($($c:ident : $b:expr,)*) => {}; }\n\nspec! {\n    requires: true,\n    ensures: ret == \u{b5},\n}\npub fn g(\u{b5}: u8, \u{c2}\u{b5}: u8) -> u8 { let _ = \u{b5}; \u{c2}\u{b5} }\n";

    for (name, src) in [("f", comment_src), ("g", uni_src)] {
        let outcome = verify_src(name, src).expect("the function must be reported");
        assert!(
            matches!(outcome, FnOutcome::Failed { .. }),
            "a clause naming a variable the body does not return must be refuted, got: {outcome:?}"
        );
    }
}