hunyi 0.5.0

渾儀 (Hunyi) — Tianheng's semantic (AST/syn) observation dimension, the complement of the static import boundary. Declare in Rust how a module's public surface must behave: what its API must not expose (types — including named public re-exports and, opt-in, a trait impl's impl-site positions — and no dyn / impl Trait or async fn seam), where a trait may be implemented, that it declares no bare pub, and which markers a type must not acquire — observed via syn, reacted in CI. The heavy syn dependency is quarantined here, never in the core.
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
use super::super::*;
use super::async_exposure::async_mod;
use super::async_exposure::async_subtree_labels;
use super::dyn_trait::dyn_mod;
use super::forbidden_marker::marker_findings;
use super::helpers::*;
use super::impl_trait::impl_trait_mod;
use super::trait_impl::locality_findings;
use super::unsafe_confinement::unsafe_labels;
use super::visibility::vis_findings;
// --- transparent macro (`cfg_if!`) arm observation --------------------------
//
// `cfg_if!` wraps human-authored items in arms without transforming their identities, so 圭表 reads
// them as real code. 渾儀 did not: `syn` parses the invocation as an opaque `Item::Macro`, and every
// capability matches concrete item variants — a measured exposure false negative on ordinary,
// compilable source (the identical function reacted at module top level and passed inside an arm).
// The shapes below are the ten a feasibility spike measured, plus the controls without which an
// emptiness assertion could pass vacuously.

/// Shape 1: `if` / `else`. BOTH arms are observed — the union is cfg-blind, so a violation in the
/// arm this build does not select still reacts (a stated bound: knowing which arm compiles requires
/// evaluating the whole feature/target resolution, cargo's job, not a scanner's).
#[test]
pub(super) fn cfg_if_if_else_arms_both_expose_forbidden_types() {
    let out = findings(
        "cfg-if-both-arms",
        &[
            ("lib.rs", "pub mod api;\npub mod infra;\n"),
            (
                "api.rs",
                "cfg_if::cfg_if! {\n\
                      if #[cfg(unix)] {\n\
                          pub fn unix_leak() -> crate::infra::Secret { loop {} }\n\
                      } else {\n\
                          pub fn fallback_leak() -> crate::infra::Secret { loop {} }\n\
                      }\n\
                 }\n",
            ),
            ("infra.rs", "pub struct Secret;\n"),
        ],
        "crate::api",
        &["crate::infra"],
    )
    .unwrap();
    assert_eq!(
        out,
        vec![
            "crate::infra::Secret exposed by fn crate::api::fallback_leak",
            "crate::infra::Secret exposed by fn crate::api::unix_leak",
        ],
        "both cfg_if arms' exposures must react (cfg-blind union): {out:?}"
    );
}

/// Shape 2: a single `if` arm with no `else`.
#[test]
pub(super) fn cfg_if_if_only_arm_exposes_a_forbidden_type() {
    let out = findings(
        "cfg-if-if-only",
        &[
            ("lib.rs", "pub mod api;\npub mod infra;\n"),
            (
                "api.rs",
                "cfg_if::cfg_if! {\n\
                      if #[cfg(unix)] {\n\
                          pub fn leak() -> crate::infra::Secret { loop {} }\n\
                      }\n\
                 }\n",
            ),
            ("infra.rs", "pub struct Secret;\n"),
        ],
        "crate::api",
        &["crate::infra"],
    )
    .unwrap();
    assert_eq!(
        out,
        vec!["crate::infra::Secret exposed by fn crate::api::leak"],
        "an if-only cfg_if arm must be observed: {out:?}"
    );
}

/// Shape 3: an `else if` chain — every arm, not just the first and last.
#[test]
pub(super) fn cfg_if_else_if_chain_exposes_every_arm() {
    let out = findings(
        "cfg-if-chain",
        &[
            ("lib.rs", "pub mod api;\npub mod infra;\n"),
            (
                "api.rs",
                "cfg_if::cfg_if! {\n\
                      if #[cfg(unix)] {\n\
                          pub fn a_leak() -> crate::infra::Secret { loop {} }\n\
                      } else if #[cfg(windows)] {\n\
                          pub fn b_leak() -> crate::infra::Secret { loop {} }\n\
                      } else {\n\
                          pub fn c_leak() -> crate::infra::Secret { loop {} }\n\
                      }\n\
                 }\n",
            ),
            ("infra.rs", "pub struct Secret;\n"),
        ],
        "crate::api",
        &["crate::infra"],
    )
    .unwrap();
    assert_eq!(
        out,
        vec![
            "crate::infra::Secret exposed by fn crate::api::a_leak",
            "crate::infra::Secret exposed by fn crate::api::b_leak",
            "crate::infra::Secret exposed by fn crate::api::c_leak",
        ],
        "every arm of an else-if chain must be observed: {out:?}"
    );
}

/// Shape 4: a `cfg_if!` nested inside an arm — the flattening recurses.
#[test]
pub(super) fn nested_cfg_if_inside_an_arm_exposes_a_forbidden_type() {
    let out = findings(
        "cfg-if-nested",
        &[
            ("lib.rs", "pub mod api;\npub mod infra;\n"),
            (
                "api.rs",
                "cfg_if::cfg_if! {\n\
                      if #[cfg(unix)] {\n\
                          cfg_if::cfg_if! {\n\
                              if #[cfg(target_pointer_width = \"64\")] {\n\
                                  pub fn inner_leak() -> crate::infra::Secret { loop {} }\n\
                              }\n\
                          }\n\
                      }\n\
                 }\n",
            ),
            ("infra.rs", "pub struct Secret;\n"),
        ],
        "crate::api",
        &["crate::infra"],
    )
    .unwrap();
    assert_eq!(
        out,
        vec!["crate::infra::Secret exposed by fn crate::api::inner_leak"],
        "a nested cfg_if's arm must be observed: {out:?}"
    );
}

/// Shape 5: an arm-declared **file** module enters the module walk. Were the declaration invisible,
/// the anchor would not resolve at all (exit 2, "unknown module") — and in the whole-crate walk the
/// entire subtree beneath it would go unobserved.
#[test]
pub(super) fn a_cfg_if_arm_declared_file_module_is_walked() {
    let out = findings(
        "cfg-if-arm-mod",
        &[
            (
                "lib.rs",
                "pub mod infra;\n\
                 cfg_if::cfg_if! {\n\
                      if #[cfg(unix)] {\n\
                          pub mod api;\n\
                      }\n\
                 }\n",
            ),
            (
                "api.rs",
                "pub fn leak() -> crate::infra::Secret { loop {} }\n",
            ),
            ("infra.rs", "pub struct Secret;\n"),
        ],
        "crate::api",
        &["crate::infra"],
    )
    .unwrap();
    assert_eq!(
        out,
        vec!["crate::infra::Secret exposed by fn crate::api::leak"],
        "a mod declared only inside a cfg_if arm must be descended: {out:?}"
    );
}

/// Shape 6: an arm-declared **inline** module.
#[test]
pub(super) fn a_cfg_if_arm_declared_inline_module_is_walked() {
    let out = findings(
        "cfg-if-arm-inline-mod",
        &[
            (
                "lib.rs",
                "pub mod infra;\n\
                 cfg_if::cfg_if! {\n\
                      if #[cfg(unix)] {\n\
                          pub mod api {\n\
                              pub fn leak() -> crate::infra::Secret { loop {} }\n\
                          }\n\
                      }\n\
                 }\n",
            ),
            ("infra.rs", "pub struct Secret;\n"),
        ],
        "crate::api",
        &["crate::infra"],
    )
    .unwrap();
    assert_eq!(
        out,
        vec!["crate::infra::Secret exposed by fn crate::api::leak"],
        "an inline mod declared inside a cfg_if arm must be descended: {out:?}"
    );
}

/// Shape 7: the outer delimiter of the invocation is irrelevant — `cfg_if!( … );` is the same macro.
#[test]
pub(super) fn a_paren_delimited_cfg_if_invocation_is_transparent() {
    let out = findings(
        "cfg-if-paren",
        &[
            ("lib.rs", "pub mod api;\npub mod infra;\n"),
            (
                "api.rs",
                "cfg_if::cfg_if!(\n\
                      if #[cfg(unix)] {\n\
                          pub fn leak() -> crate::infra::Secret { loop {} }\n\
                      }\n\
                 );\n",
            ),
            ("infra.rs", "pub struct Secret;\n"),
        ],
        "crate::api",
        &["crate::infra"],
    )
    .unwrap();
    assert_eq!(
        out,
        vec!["crate::infra::Secret exposed by fn crate::api::leak"],
        "a paren-delimited cfg_if invocation must be observed: {out:?}"
    );
}

/// Shape 8: a `cfg_if!` written **inside an inline module** — the flattening applies at every level
/// the walk descends to, not only a file's top level.
#[test]
pub(super) fn a_cfg_if_inside_an_inline_module_is_transparent() {
    let out = findings(
        "cfg-if-in-inline-mod",
        &[
            ("lib.rs", "pub mod api;\npub mod infra;\n"),
            (
                "api.rs",
                "pub mod inner {\n\
                      cfg_if::cfg_if! {\n\
                          if #[cfg(unix)] {\n\
                              pub fn leak() -> crate::infra::Secret { loop {} }\n\
                          }\n\
                      }\n\
                 }\n",
            ),
            ("infra.rs", "pub struct Secret;\n"),
        ],
        "crate::api::inner",
        &["crate::infra"],
    )
    .unwrap();
    assert_eq!(
        out,
        vec!["crate::infra::Secret exposed by fn crate::api::inner::leak"],
        "a cfg_if inside an inline module must be observed: {out:?}"
    );
}

/// Shape 9: an arm body that does not parse as items yields nothing — never a scan error. The
/// invocation is `cfg_if!`-named, so the arm walk runs; its body is statements, which `syn::File`
/// rejects. The forbidden exposure at top level is the control: it proves the fixture is otherwise
/// live, so the arm's emptiness is not an artifact of nothing being observed at all.
#[test]
pub(super) fn a_cfg_if_arm_body_that_does_not_parse_as_items_is_not_a_scan_error() {
    let out = findings(
        "cfg-if-unparseable-arm",
        &[
            ("lib.rs", "pub mod api;\npub mod infra;\n"),
            (
                "api.rs",
                "pub fn control_leak() -> crate::infra::Secret { loop {} }\n\
                 cfg_if::cfg_if! {\n\
                      if #[cfg(unix)] {\n\
                          let _not_an_item = 1;\n\
                      }\n\
                 }\n",
            ),
            ("infra.rs", "pub struct Secret;\n"),
        ],
        "crate::api",
        &["crate::infra"],
    )
    .unwrap();
    assert_eq!(
        out,
        vec!["crate::infra::Secret exposed by fn crate::api::control_leak"],
        "an unparseable arm body must yield no items and no error, leaving the rest observed: {out:?}"
    );
}

/// Shape 10: the **name gate**, and why it is load-bearing rather than conservative. Arm extraction
/// reads every top-level brace group of the body as an arm; in an arbitrary macro's body an
/// `impl Foo { … }`'s braces ARE such a group, so an unnamed-gated walk would recover a `fn hidden`
/// this macro may never emit verbatim — a false positive. Paired with the control below, which
/// proves the identical function DOES react when written as an item.
#[test]
pub(super) fn an_arbitrary_macro_body_is_not_read_as_transparent_arms() {
    let out = findings(
        "arbitrary-macro-body",
        &[
            ("lib.rs", "pub mod api;\npub mod infra;\n"),
            (
                "api.rs",
                "generate_wrapper! {\n\
                      impl Foo {\n\
                          pub fn hidden() -> crate::infra::Secret { loop {} }\n\
                      }\n\
                 }\n",
            ),
            ("infra.rs", "pub struct Secret;\n"),
        ],
        "crate::api",
        &["crate::infra"],
    )
    .unwrap();
    assert!(
        out.is_empty(),
        "only cfg_if is transparent: an arbitrary macro's body must not be read as arms: {out:?}"
    );
}

/// The control for the name gate: the same `fn hidden` written as a real item reacts. Without this,
/// the emptiness above could hold for any reason at all — an unresolvable anchor, a forbidden set
/// that matches nothing — rather than because the macro body was left unread.
#[test]
pub(super) fn the_same_exposure_written_as_an_item_reacts() {
    let out = findings(
        "arbitrary-macro-control",
        &[
            ("lib.rs", "pub mod api;\npub mod infra;\n"),
            (
                "api.rs",
                "pub struct Foo;\n\
                 impl Foo {\n\
                      pub fn hidden() -> crate::infra::Secret { loop {} }\n\
                 }\n\
                 pub fn hidden() -> crate::infra::Secret { loop {} }\n",
            ),
            ("infra.rs", "pub struct Secret;\n"),
        ],
        "crate::api",
        &["crate::infra"],
    )
    .unwrap();
    assert_eq!(
        out,
        vec![
            "crate::infra::Secret exposed by fn <crate::api::Foo>::hidden",
            "crate::infra::Secret exposed by fn crate::api::hidden",
        ],
        "the control exposure must react as an item — both the inherent-impl method the macro \
         fixture wrapped and the free function: {out:?}"
    );
}

/// An arm-declared module is **cfg-conditional**: every arm is gated by a predicate in the macro
/// header, so an absent conventional file is a legitimate configuration, not a broken declaration —
/// 圭表's settled rule for the same shape, adopted rather than re-derived.
#[test]
pub(super) fn a_cfg_if_arm_declared_module_with_no_source_file_is_tolerated() {
    let out = findings(
        "cfg-if-arm-absent-file",
        &[
            (
                "lib.rs",
                "pub mod api;\npub mod infra;\n\
                 cfg_if::cfg_if! {\n\
                      if #[cfg(unix)] {\n\
                          pub mod unix_only;\n\
                      }\n\
                 }\n",
            ),
            (
                "api.rs",
                "pub fn leak() -> crate::infra::Secret { loop {} }\n",
            ),
            ("infra.rs", "pub struct Secret;\n"),
        ],
        "crate::api",
        &["crate::infra"],
    )
    .unwrap();
    assert_eq!(
        out,
        vec!["crate::infra::Secret exposed by fn crate::api::leak"],
        "an arm-declared module with no file must be tolerated, not a scan error: {out:?}"
    );
}

/// The control for that tolerance: the identical declaration **without** the arm still fails loud.
/// Otherwise the tolerance above could be indistinguishable from never having read the declaration.
#[test]
pub(super) fn the_same_module_declaration_outside_an_arm_still_fails_loud() {
    let error = findings(
        "cfg-if-arm-absent-control",
        &[
            (
                "lib.rs",
                "pub mod api;\npub mod infra;\npub mod unix_only;\n",
            ),
            (
                "api.rs",
                "pub fn leak() -> crate::infra::Secret { loop {} }\n",
            ),
            ("infra.rs", "pub struct Secret;\n"),
        ],
        "crate::api",
        &["crate::infra"],
    )
    .unwrap_err();
    assert!(
        error.contains("unix_only") || error.contains("no source file"),
        "an unconditional declaration with no file must stay a scan error: {error}"
    );
}

/// Arm membership tolerates an ABSENCE; it never tolerates an **ambiguity**. No predicate value
/// makes two conventional files compile as one module, so a dual-backed arm-declared module stays a
/// constitution error — the same ordering all three dimensions apply to this shape.
#[test]
pub(super) fn a_cfg_if_arm_declared_dual_backed_module_is_still_a_scan_error() {
    let error = findings(
        "cfg-if-arm-dual-backed",
        &[
            (
                "lib.rs",
                "pub mod api;\npub mod infra;\n\
                 cfg_if::cfg_if! {\n\
                      if #[cfg(unix)] {\n\
                          pub mod dual;\n\
                      }\n\
                 }\n",
            ),
            (
                "api.rs",
                "pub fn leak() -> crate::infra::Secret { loop {} }\n",
            ),
            ("infra.rs", "pub struct Secret;\n"),
            ("dual.rs", "pub struct A;\n"),
            ("dual/mod.rs", "pub struct A;\n"),
        ],
        "crate::api",
        &["crate::infra"],
    )
    .unwrap_err();
    assert!(
        error.contains("resolves to both"),
        "a dual-backed arm-declared module must stay a scan error: {error}"
    );
}

/// The whole-crate walks read arms too, not only the anchored descent: an `unsafe` site written
/// inside a `cfg_if!` arm is confined like any other. This exercises `walk_unsafe`, a separate
/// descent from the one every test above goes through.
#[test]
pub(super) fn an_unsafe_site_inside_a_cfg_if_arm_is_confined() {
    let out = unsafe_labels(
        "cfg-if-arm",
        &[
            ("lib.rs", "pub mod ffi;\npub mod net;\n"),
            ("ffi.rs", ""),
            (
                "net.rs",
                "cfg_if::cfg_if! {\n\
                      if #[cfg(unix)] {\n\
                          pub unsafe fn decode() {}\n\
                      }\n\
                 }\n",
            ),
        ],
        &["crate::ffi"],
    )
    .unwrap();
    assert_eq!(
        out,
        vec!["unsafe fn decode in crate::net"],
        "an unsafe site inside a cfg_if arm must react: {out:?}"
    );
}

/// A trait impl inside an arm enters the crate-wide impl scan (`walk_module`), which feeds
/// trait-impl locality and the re-export/alias closures every other capability resolves through.
#[test]
pub(super) fn a_trait_impl_inside_a_cfg_if_arm_is_located() {
    let out = locality_findings(
        "cfg-if-arm",
        &[
            ("lib.rs", "pub mod command;\npub mod domain;\n"),
            ("command.rs", "pub trait Command {}\n"),
            (
                "domain.rs",
                "use crate::command::Command;\n\
                 pub struct Foo;\n\
                 cfg_if::cfg_if! {\n\
                      if #[cfg(unix)] {\n\
                          impl Command for Foo {}\n\
                      }\n\
                 }\n",
            ),
        ],
        "crate::command::Command",
        &["crate::commands"],
    )
    .unwrap();
    assert_eq!(
        out,
        vec!["crate::domain (impl crate::command::Command for crate::domain::Foo)"],
        "a trait impl inside a cfg_if arm must be located: {out:?}"
    );
}

// Task-1 verification rather than assumption: the four single-module-anchored capabilities share one
// resolution entry, but "they share it" is a claim about the code, so each is measured on an
// arm-wrapped construct of its own shape. The two crate-wide walks are covered above
// (`an_unsafe_site_inside_a_cfg_if_arm_is_confined`, `a_trait_impl_inside_a_cfg_if_arm_is_located`).

/// Visibility: a bare `pub` inside an arm is declared surface like any other.
#[test]
pub(super) fn a_bare_pub_inside_a_cfg_if_arm_reacts() {
    let out = vis_findings(
        "cfg-if-arm",
        &[
            ("lib.rs", "pub mod m;\n"),
            (
                "m.rs",
                "cfg_if::cfg_if! {\n\
                 if #[cfg(unix)] {\n\
                 pub fn wide() {}\n\
                 }\n\
                 }\n",
            ),
        ],
        "crate::m",
    )
    .unwrap();
    assert_eq!(
        out,
        vec!["pub fn wide"],
        "a bare pub inside a cfg_if arm must react: {out:?}"
    );
}

/// dyn-trait: a `dyn` seam inside an arm crosses the boundary like any other.
#[test]
pub(super) fn a_dyn_seam_inside_a_cfg_if_arm_reacts() {
    let out = dyn_mod(
        "cfg-if-arm",
        "pub trait Port {}\n\
         cfg_if::cfg_if! {\n\
         if #[cfg(unix)] {\n\
         pub fn get() -> Box<dyn Port> { loop {} }\n\
         }\n\
         }\n",
    )
    .unwrap();
    assert_eq!(
        out.len(),
        1,
        "a dyn seam inside a cfg_if arm must react: {out:?}"
    );
}

/// impl-trait: an existential return inside an arm is exposed like any other.
#[test]
pub(super) fn an_impl_trait_seam_inside_a_cfg_if_arm_reacts() {
    let out = impl_trait_mod(
        "cfg-if-arm",
        "pub trait Port {}\n\
         cfg_if::cfg_if! {\n\
         if #[cfg(unix)] {\n\
         pub fn get() -> impl Port { loop {} }\n\
         }\n\
         }\n",
    )
    .unwrap();
    assert_eq!(
        out.len(),
        1,
        "an impl-trait seam inside a cfg_if arm must react: {out:?}"
    );
}

/// async-exposure: a public `async fn` inside an arm is an async seam like any other.
#[test]
pub(super) fn an_async_fn_inside_a_cfg_if_arm_reacts() {
    let out = async_mod(
        "cfg-if-arm",
        "cfg_if::cfg_if! {\n\
         if #[cfg(unix)] {\n\
         pub async fn serve() {}\n\
         }\n\
         }\n",
    )
    .unwrap();
    assert_eq!(
        out.len(),
        1,
        "an async fn inside a cfg_if arm must react: {out:?}"
    );
}

/// forbidden-marker: a derive inside an arm is acquired like any other. This capability walks the
/// whole crate (`scan_crate`), so it also pins the type-definition half of that walk.
#[test]
pub(super) fn a_forbidden_marker_inside_a_cfg_if_arm_reacts() {
    let out = marker_findings(
        "cfg-if-arm",
        &[
            ("lib.rs", "pub mod domain;\n"),
            (
                "domain.rs",
                "cfg_if::cfg_if! {\n\
                 if #[cfg(unix)] {\n\
                 #[derive(serde::Serialize)]\n\
                 pub struct Order;\n\
                 }\n\
                 }\n",
            ),
        ],
        "crate::domain",
        &["serde::Serialize"],
    )
    .unwrap();
    assert_eq!(
        out.len(),
        1,
        "a forbidden derive inside a cfg_if arm must react: {out:?}"
    );
}

/// The subtree walk (`including_submodules`) records each module's items itself, on a descent
/// separate from the anchored one every test above uses — so it needs its own arm coverage: an
/// `async fn` inside a submodule's `cfg_if!` arm must react at the subtree scope.
#[test]
pub(super) fn a_subtree_scope_observes_an_async_fn_inside_a_cfg_if_arm() {
    let out = async_subtree_labels(
        "cfg-if-arm",
        &[
            ("lib.rs", "pub mod net;\n"),
            (
                "net.rs",
                "cfg_if::cfg_if! {\n\
                 if #[cfg(unix)] {\n\
                 pub async fn connect() {}\n\
                 }\n\
                 }\n",
            ),
        ],
        "crate",
    );
    assert_eq!(
        out.len(),
        1,
        "an async fn inside a submodule's cfg_if arm must react at the subtree scope: {out:?}"
    );
}

/// The unqualified spelling. Every test above writes `cfg_if::cfg_if!`; an adopter who wrote
/// `use cfg_if::cfg_if;` invokes it as a bare `cfg_if!`, and the name test matches the path's LAST
/// segment precisely so both spellings are one shape (圭表 matches the same way).
#[test]
pub(super) fn an_unqualified_cfg_if_invocation_is_transparent() {
    let out = findings(
        "cfg-if-unqualified",
        &[
            ("lib.rs", "pub mod api;\npub mod infra;\n"),
            (
                "api.rs",
                "use cfg_if::cfg_if;\n\
                 cfg_if! {\n\
                 if #[cfg(unix)] {\n\
                 pub fn leak() -> crate::infra::Secret { loop {} }\n\
                 }\n\
                 }\n",
            ),
            ("infra.rs", "pub struct Secret;\n"),
        ],
        "crate::api",
        &["crate::infra"],
    )
    .unwrap();
    assert_eq!(
        out,
        vec!["crate::infra::Secret exposed by fn crate::api::leak"],
        "the unqualified cfg_if! spelling must be transparent too: {out:?}"
    );
}

/// Two arms declaring the SAME module name — the per-platform shim spelled with one file. Both
/// declarations resolve to the identical file, so the walkers' dedup must collapse them: flattening
/// must not inflate one real violation into two findings (the false-positive direction of this
/// change).
#[test]
pub(super) fn two_cfg_if_arms_declaring_one_module_name_do_not_double_report() {
    let out = findings(
        "cfg-if-arm-twin-mod",
        &[
            (
                "lib.rs",
                "pub mod infra;\n\
                 cfg_if::cfg_if! {\n\
                 if #[cfg(unix)] {\n\
                 pub mod api;\n\
                 } else {\n\
                 pub mod api;\n\
                 }\n\
                 }\n",
            ),
            (
                "api.rs",
                "pub fn leak() -> crate::infra::Secret { loop {} }\n",
            ),
            ("infra.rs", "pub struct Secret;\n"),
        ],
        "crate::api",
        &["crate::infra"],
    )
    .unwrap();
    assert_eq!(
        out,
        vec!["crate::infra::Secret exposed by fn crate::api::leak"],
        "two arms declaring the same module must dedup to one finding: {out:?}"
    );
}

/// A `cfg_if!` inside an `impl` block: transparency is for **item position**, where `syn` gives an
/// `Item::Macro` whose arms parse as items. Inside an `impl` (or `trait`) body the invocation is an
/// `ImplItem::Macro` whose arms are impl items, a different flattening across a different set of
/// walkers — its own change, not silently smuggled into this one. Pinned as a **stated bound** so
/// the hole is discoverable rather than latent; when it closes, this test is the one that fails.
#[test]
pub(super) fn a_cfg_if_inside_an_impl_body_is_a_stated_bound() {
    let out = findings(
        "cfg-if-impl-body",
        &[
            ("lib.rs", "pub mod api;\npub mod infra;\n"),
            (
                "api.rs",
                "pub struct Api;\n\
                 impl Api {\n\
                 cfg_if::cfg_if! {\n\
                 if #[cfg(unix)] {\n\
                 pub fn leak() -> crate::infra::Secret { loop {} }\n\
                 }\n\
                 }\n\
                 }\n",
            ),
            ("infra.rs", "pub struct Secret;\n"),
        ],
        "crate::api",
        &["crate::infra"],
    )
    .unwrap();
    assert!(
        out.is_empty(),
        "an impl-body cfg_if is a stated bound, not a claimed reaction: {out:?}"
    );
}

/// The other absence site reads the same gate: an **unconditional** `#[path = "…"]` inside an arm
/// whose target file does not exist is tolerated, because the arm's predicate removes the whole item
/// — `#[path]` included — exactly as a bare `#[cfg]` beside it would. Without this the two absence
/// outcomes would drift apart, which is the divergence the shared gate exists to prevent (and 圭表
/// states the same rule for its own walker).
#[test]
pub(super) fn an_absent_path_remap_target_inside_a_cfg_if_arm_is_tolerated() {
    let out = findings(
        "cfg-if-arm-absent-path",
        &[
            (
                "lib.rs",
                "pub mod api;\npub mod infra;\n\
                 cfg_if::cfg_if! {\n\
                 if #[cfg(windows)] {\n\
                 #[path = \"windows_impl.rs\"]\n\
                 pub mod imp;\n\
                 }\n\
                 }\n",
            ),
            (
                "api.rs",
                "pub fn leak() -> crate::infra::Secret { loop {} }\n",
            ),
            ("infra.rs", "pub struct Secret;\n"),
        ],
        "crate::api",
        &["crate::infra"],
    )
    .unwrap();
    assert_eq!(
        out,
        vec!["crate::infra::Secret exposed by fn crate::api::leak"],
        "an absent unconditional #[path] target inside an arm must be tolerated: {out:?}"
    );
}

/// Two-crate reproduction of the audit-sweep finding at hunyi's own composed dedup
/// (`driver::outcome_from`, the analogue of guibiao's `evaluate`) — not just the per-fact catalog
/// tests. Identical governed module path + rule declared against two different workspace members
/// must survive as two distinct violations, mirroring the guibiao-side regression in
/// `crates/guibiao/src/tests/`.
#[test]
pub(super) fn two_crates_with_the_identical_async_exposure_boundary_stay_distinct_violations() {
    fn tree_and_metadata(label: &str, package: &str) -> (TempSrcTree, Value) {
        let tree = TempSrcTree::new(label);
        tree.write_all(&[("lib.rs", "pub mod registry;\npub async fn register() {}\n")]);
        let metadata = serde_json::json!({
            "packages": [{
                "name": package,
                "dependencies": [],
                "targets": [{ "kind": ["lib"], "src_path": tree.root().to_string_lossy().into_owned() }],
            }],
        });
        (tree, metadata)
    }

    let (_alpha_tree, alpha_metadata) = tree_and_metadata("async-identity-alpha", "alpha");
    let (_beta_tree, beta_metadata) = tree_and_metadata("async-identity-beta", "beta");
    let combined_metadata = serde_json::json!({
        "packages": [
            alpha_metadata["packages"][0].clone(),
            beta_metadata["packages"][0].clone(),
        ],
    });

    fn boundary_for(package: &str) -> AsyncExposureBoundary {
        AsyncExposureBoundary::in_crate(package)
            .module("crate")
            .must_not_expose_async_fn()
            .because("no async seam here")
    }

    let mut violations = Vec::new();
    eval_into(
        &combined_metadata,
        &[boundary_for("alpha"), boundary_for("beta")],
        check_async_exposure_boundary,
        &mut violations,
    )
    .expect("both boundaries resolve");
    let outcome = outcome_from(violations, 1, 1);
    let report = match outcome {
        Outcome::Violations(report) => report,
        other => panic!("expected two violations, got {other:?}"),
    };
    assert_eq!(
        report.violations.len(),
        2,
        "each crate's async-exposure violation must survive dedup: {:?}",
        report.violations
    );
    let ids: std::collections::BTreeSet<_> = report.violations.iter().map(Violation::id).collect();
    assert_eq!(
        ids.len(),
        2,
        "identity must differ by crate, not collapse to one"
    );
}

/// A `pub fn` inside an `extern` block is a real item in the module's own namespace — as public as
/// a same-shaped ordinary `fn` — so a forbidden type named only in its signature must react exactly
/// like an ordinary function's would, not escape the query because the declaration has no body.
#[test]
pub(super) fn a_forbidden_type_in_an_extern_block_pub_fn_signature_is_observed() {
    let out = semantic_findings(
        "extern-block-fn-exposure",
        &[
            ("lib.rs", "pub mod infra;\npub mod api;\n"),
            ("infra.rs", "pub struct Secret;\n"),
            (
                "api.rs",
                "extern \"C\" {\n    pub fn handle() -> crate::infra::Secret;\n}\n",
            ),
        ],
        "crate::api",
        &["crate::infra"],
        false,
        &[],
    )
    .unwrap();
    assert_eq!(
        out,
        vec!["crate::infra::Secret exposed by fn crate::api::handle"]
    );
}

/// The identical shape for `pub static` inside an `extern` block.
#[test]
pub(super) fn a_forbidden_type_in_an_extern_block_pub_static_is_observed() {
    let out = semantic_findings(
        "extern-block-static-exposure",
        &[
            ("lib.rs", "pub mod infra;\npub mod api;\n"),
            ("infra.rs", "pub struct Secret;\n"),
            (
                "api.rs",
                "extern \"C\" {\n    pub static S: crate::infra::Secret;\n}\n",
            ),
        ],
        "crate::api",
        &["crate::infra"],
        false,
        &[],
    )
    .unwrap();
    assert_eq!(
        out,
        vec!["crate::infra::Secret exposed by static crate::api::S"]
    );
}

/// A private (no `vis`) `fn`/`static` inside an `extern` block must NOT react — extern-block items
/// default to the enclosing block's own item visibility exactly like a module-level item does, and
/// only `pub` ones are on the module's public surface.
#[test]
pub(super) fn a_non_pub_extern_block_item_is_not_observed() {
    let out = semantic_findings(
        "extern-block-private-not-observed",
        &[
            ("lib.rs", "pub mod infra;\npub mod api;\n"),
            ("infra.rs", "pub struct Secret;\n"),
            (
                "api.rs",
                "extern \"C\" {\n    fn handle() -> crate::infra::Secret;\n    static S: crate::infra::Secret;\n}\n",
            ),
        ],
        "crate::api",
        &["crate::infra"],
        false,
        &[],
    )
    .unwrap();
    assert_eq!(out, Vec::<String>::new());
}

/// A forbidden-marker impl's self type reached through a `type X = Y;` alias whose target `Y` is
/// itself a mutually-exclusive `#[cfg]`-gated `use` alias: the self-type landing must react
/// regardless of which cfg branch is declared first. Before the fix, `scan.alias_targets` was a
/// single-valued `HashMap<String, String>` populated via the resolver's then-single-candidate lookup,
/// so only one landing candidate for `X` was ever recorded (found on adversarial review of
/// `hunyi-cfg-branch-use-reexport-merging`).
#[test]
pub(super) fn forbidden_marker_self_type_landing_reacts_when_the_forbidden_alias_is_declared_first()
{
    let out = marker_findings(
        "self-type-landing-cfg-forbidden-first",
        &[
            ("lib.rs", "pub mod domain;\npub mod wire;\n"),
            ("domain.rs", "pub struct Order;\n"),
            (
                "wire.rs",
                "#[cfg(unix)]\nuse crate::domain::Order as Y;\n#[cfg(not(unix))]\nuse crate::domain::NotOrder as Y;\ntype X = Y;\nimpl serde::Serialize for X {}\n",
            ),
        ],
        "crate::domain",
        &["serde::Serialize"],
    )
    .unwrap();
    assert_eq!(
        out,
        ["impl serde::Serialize for crate::wire::X in crate::wire"],
        "the marker-acquisition REACTION now checks every landing candidate (X resolves to the \
         governed crate::domain::Order under one cfg branch), even though the finding's OWNER \
         label renders the self type as written (`X`), a separate, deliberate identity concern \
         from the landing/gating check: {out:?}"
    );
}

/// The identical shape with the resolvable (defined) alias target declared SECOND. Before the fix
/// this silently passed (`Ok([])`): only the first-declared (undefined `NotOrder`) landing was
/// recorded, which fails the `defined` check, so the genuinely governed self type was never seen.
#[test]
pub(super) fn forbidden_marker_self_type_landing_reacts_when_the_forbidden_alias_is_declared_second()
 {
    let out = marker_findings(
        "self-type-landing-cfg-forbidden-second",
        &[
            ("lib.rs", "pub mod domain;\npub mod wire;\n"),
            ("domain.rs", "pub struct Order;\n"),
            (
                "wire.rs",
                "#[cfg(not(unix))]\nuse crate::domain::NotOrder as Y;\n#[cfg(unix)]\nuse crate::domain::Order as Y;\ntype X = Y;\nimpl serde::Serialize for X {}\n",
            ),
        ],
        "crate::domain",
        &["serde::Serialize"],
    )
    .unwrap();
    assert_eq!(
        out,
        ["impl serde::Serialize for crate::wire::X in crate::wire"],
        "the marker-acquisition REACTION now checks every landing candidate (X resolves to the \
         governed crate::domain::Order under one cfg branch), even though the finding's OWNER \
         label renders the self type as written (`X`), a separate, deliberate identity concern \
         from the landing/gating check: {out:?}"
    );
}

/// A forbidden exposure reached through a `type X = Y;` alias whose target `Y` is itself a
/// mutually-exclusive `#[cfg]`-gated `use` alias: the exposure must react regardless of which cfg
/// branch is declared first. Before the fix, `scan.aliases` (the exposure-pipeline `AliasMap`) was
/// populated via the resolver's then-single-candidate lookup even though the map itself was already
/// multi-valued, so only one landing candidate for `X` was ever pushed (found on adversarial
/// review of `hunyi-cfg-branch-use-reexport-merging`).
#[test]
pub(super) fn type_alias_exposure_reacts_when_the_forbidden_alias_is_declared_first() {
    let out = semantic_findings(
        "type-alias-cfg-forbidden-first",
        &[
            ("lib.rs", "pub mod safe;\npub mod infra;\npub mod api;\n"),
            ("safe.rs", "pub struct Handle;\n"),
            ("infra.rs", "pub struct Secret;\n"),
            (
                "api.rs",
                "#[cfg(unix)]\nuse crate::infra::Secret as Y;\n#[cfg(not(unix))]\nuse crate::safe::Handle as Y;\ntype X = Y;\npub fn leak() -> X { loop {} }\n",
            ),
        ],
        "crate::api",
        &["crate::infra"],
        false,
        &[],
    )
    .unwrap();
    assert_eq!(
        out,
        vec!["crate::infra::Secret exposed by fn crate::api::leak"]
    );
}

/// The identical shape with the forbidden alias declared SECOND. Before the fix this silently
/// passed (`Ok([])`).
#[test]
pub(super) fn type_alias_exposure_reacts_when_the_forbidden_alias_is_declared_second() {
    let out = semantic_findings(
        "type-alias-cfg-forbidden-second",
        &[
            ("lib.rs", "pub mod safe;\npub mod infra;\npub mod api;\n"),
            ("safe.rs", "pub struct Handle;\n"),
            ("infra.rs", "pub struct Secret;\n"),
            (
                "api.rs",
                "#[cfg(not(unix))]\nuse crate::safe::Handle as Y;\n#[cfg(unix)]\nuse crate::infra::Secret as Y;\ntype X = Y;\npub fn leak() -> X { loop {} }\n",
            ),
        ],
        "crate::api",
        &["crate::infra"],
        false,
        &[],
    )
    .unwrap();
    assert_eq!(
        out,
        vec!["crate::infra::Secret exposed by fn crate::api::leak"]
    );
}

/// A cfg_attr(path)-hidden module's own `pub use` re-export must still be folded into the
/// crate-wide re-export closure `exposure.rs`'s own `scan_crate` call builds — the same
/// `resolve_child_modules` mechanism fixed by `hunyi-cfg-attr-path-module-loss`. Before the fix,
/// `facade.rs` never entered the crate-wide scan (only reachable via the `cfg_attr`-wrapped
/// `#[path]`'s conventional form), so its re-export was missing from `scan.reexports` and
/// `crate::facade::Secret` never canonicalized to the real, forbidden `crate::infra::Secret`.
#[test]
pub(super) fn signature_coupling_reacts_through_a_cfg_attr_path_hidden_reexport() {
    let out = semantic_findings(
        "cfg-attr-exposure-reexport",
        &[
            (
                "lib.rs",
                "pub mod infra;\n#[cfg_attr(windows, path = \"weird.rs\")]\npub mod facade;\npub mod api;\n",
            ),
            ("infra.rs", "pub struct Secret;\n"),
            ("facade.rs", "pub use crate::infra::Secret;\n"),
            (
                "api.rs",
                "pub fn leak() -> crate::facade::Secret { loop {} }\n",
            ),
        ],
        "crate::api",
        &["crate::infra"],
        false,
        &[],
    )
    .unwrap();
    assert_eq!(
        out,
        vec!["crate::infra::Secret exposed by fn crate::api::leak"]
    );
}

/// `module_resolve.rs::descend`'s targeted, single-module-anchored resolution now follows a
/// `cfg_attr`-wrapped `#[path]` target exactly like `scan::resolve_child_modules`'s crate-wide walk —
/// found on adversarial review of `hunyi-cfg-attr-path-module-loss`, whose own commit claimed this
/// function was "already correct, fails loud on this shape," a claim that did not survive scrutiny:
/// even a LONE such declaration (no resolving sibling at all) previously never followed its target,
/// no matter whether the file existed. Now it does, closing the same false-negative class this
/// change already closed for the crate-wide walk, at this second, previously-unfixed entry point.
#[test]
pub(super) fn cfg_attr_wrapped_path_resolves_through_its_own_target_with_no_sibling_at_all() {
    let out = semantic_findings(
        "cfg-attr-lone-target-resolves",
        &[
            (
                "lib.rs",
                "pub mod infra;\n#[cfg_attr(windows, path = \"win.rs\")]\nmod foo;\n",
            ),
            ("infra.rs", "pub struct Secret;\n"),
            (
                "win.rs",
                "pub fn leak() -> crate::infra::Secret { loop {} }\n",
            ),
        ],
        "crate::foo",
        &["crate::infra"],
        false,
        &[],
    )
    .unwrap();
    assert_eq!(
        out,
        vec!["crate::infra::Secret exposed by fn crate::foo::leak"]
    );
}

/// A `cfg_attr`-wrapped `#[path]` sibling (a mutually-exclusive `#[cfg]`/`cfg_if!` co-declaration of
/// the identical module name) must still react through its own file, not be silently absorbed by a
/// sibling's successful resolution. Before this fix, `has_path_attr`'s `continue` only skipped that
/// one declaration; when ANY sibling for the same name resolved, the empty-branch check that would
/// otherwise trigger a fail-loud error never fired, so the `cfg_attr` target's own file — and
/// everything it exposes — silently vanished with exit 0 instead. Fixed by extending the same union
/// `scan::resolve_child_modules` already applies: the `cfg_attr` target is read alongside the
/// sibling's own resolution, not skipped.
#[test]
pub(super) fn cfg_attr_wrapped_path_sibling_reacts_through_its_own_file_not_absorbed_by_a_sibling()
{
    let out = semantic_findings(
        "cfg-attr-sibling-anchor",
        &[
            ("lib.rs", "pub mod infra;\n#[cfg(windows)]\n#[cfg_attr(target_arch = \"x86\", path = \"foo_x86.rs\")]\nmod foo;\n#[cfg(not(windows))]\nmod foo;\n"),
            ("infra.rs", "pub struct Secret;\n"),
            ("foo.rs", "pub fn safe() {}\n"),
            (
                "foo_x86.rs",
                "pub fn leak() -> crate::infra::Secret { loop {} }\n",
            ),
        ],
        "crate::foo",
        &["crate::infra"],
        false,
        &[],
    )
    .unwrap();
    assert_eq!(
        out,
        vec!["crate::infra::Secret exposed by fn crate::foo::leak"]
    );
}

/// The identical shape via `cfg_if!` arms rather than bare `#[cfg]` siblings.
#[test]
pub(super) fn cfg_attr_wrapped_path_sibling_reacts_through_a_cfg_if_arm() {
    let out = semantic_findings(
        "cfg-attr-sibling-anchor-cfg-if",
        &[
            (
                "lib.rs",
                "pub mod infra;\ncfg_if::cfg_if! {\n    if #[cfg(windows)] {\n        #[cfg_attr(target_arch = \"x86\", path = \"foo_x86.rs\")]\n        mod foo;\n    } else {\n        mod foo;\n    }\n}\n",
            ),
            ("infra.rs", "pub struct Secret;\n"),
            ("foo.rs", "pub fn safe() {}\n"),
            (
                "foo_x86.rs",
                "pub fn leak() -> crate::infra::Secret { loop {} }\n",
            ),
        ],
        "crate::foo",
        &["crate::infra"],
        false,
        &[],
    )
    .unwrap();
    assert_eq!(
        out,
        vec!["crate::infra::Secret exposed by fn crate::foo::leak"]
    );
}

/// A LONE `cfg_attr`-wrapped `#[path]` declaration (no resolving sibling) still fails loud when
/// neither the conventional file nor the `cfg_attr` target exists — the genuinely fail-loud case
/// that survives from the original bound, now reached only when `has_backing_source` is false.
#[test]
pub(super) fn cfg_attr_wrapped_path_with_no_sibling_and_no_backing_file_still_fails_loud() {
    let err = semantic_findings(
        "cfg-attr-lone-fails-loud",
        &[(
            "lib.rs",
            "#[cfg_attr(windows, path = \"win.rs\")]\nmod foo;\n",
        )],
        "crate::foo",
        &[],
        false,
        &[],
    )
    .unwrap_err();
    assert!(
        err.contains("not found") || err.contains("could not"),
        "a lone unbacked cfg_attr(path) declaration must still fail loud: {err}"
    );
}

/// A module carrying TWO SEPARATE (not nested) `cfg_attr`-wrapped `#[path]` attributes — one per
/// platform predicate, the natural 3+-way per-platform shim shape — must have EVERY candidate's
/// target read, not only the first-declared one. Found on a fourth adversarial review of
/// `hunyi-cfg-attr-path-module-loss`: `cfg_attr_path_value`'s `find_map` silently returned only the
/// first matching attribute's target, dropping every other stacked candidate — the identical
/// cfg-blind-union false negative this whole change closes, one level deeper (a module can stack
/// attributes, not just nest them). Exercises both `descend` (`module_resolve.rs`, this test) and
/// the crate-wide `resolve_child_modules` (`scan.rs`, shares the identical fixed helper).
#[test]
pub(super) fn stacked_cfg_attr_wrapped_path_attributes_are_all_read_not_only_the_first() {
    let out = semantic_findings(
        "stacked-cfg-attr-path",
        &[
            ("lib.rs", "pub mod infra;\n#[cfg_attr(windows, path = \"win.rs\")]\n#[cfg_attr(target_os = \"macos\", path = \"mac.rs\")]\nmod foo;\n"),
            ("infra.rs", "pub struct Secret;\n"),
            (
                "mac.rs",
                "pub fn leak() -> crate::infra::Secret { loop {} }\n",
            ),
        ],
        "crate::foo",
        &["crate::infra"],
        false,
        &[],
    )
    .unwrap();
    assert_eq!(
        out,
        vec!["crate::infra::Secret exposed by fn crate::foo::leak"]
    );
}

/// Two mutually-exclusive `#[cfg]`-gated `use ... as Name;` declarations for the identical local
/// name, in the same file, must both react (cfg-blind: observation cannot know which is live).
/// Before the fix, the second `use` silently overwrote the first in `UseMap` — a single
/// `HashMap<String, String>` — so the verdict depended on source order rather than on whether
/// either branch's binding was genuinely forbidden.
#[test]
pub(super) fn mutually_exclusive_cfg_gated_use_aliases_both_react() {
    let out = semantic_findings(
        "cfg-use-alias-merge-forbidden-first",
        &[
            ("lib.rs", "pub mod safe;\npub mod infra;\npub mod api;\n"),
            ("safe.rs", "pub struct Handle;\n"),
            ("infra.rs", "pub struct Secret;\n"),
            (
                "api.rs",
                "#[cfg(unix)]\nuse crate::infra::Secret as Handle;\n#[cfg(not(unix))]\nuse crate::safe::Handle;\npub fn leak() -> Handle { loop {} }\n",
            ),
        ],
        "crate::api",
        &["crate::infra"],
        false,
        &[],
    )
    .unwrap();
    assert_eq!(
        out,
        vec!["crate::infra::Secret exposed by fn crate::api::leak"]
    );
}

/// The identical shape with the two `use` declarations in the OPPOSITE order — the forbidden
/// binding declared second. Before the fix this silently passed (`Ok([])`): the resolver took
/// only the first `use`-map candidate, and here that candidate was the non-forbidden one.
#[test]
pub(super) fn mutually_exclusive_cfg_gated_use_aliases_react_regardless_of_declaration_order() {
    let out = semantic_findings(
        "cfg-use-alias-merge-forbidden-second",
        &[
            ("lib.rs", "pub mod safe;\npub mod infra;\npub mod api;\n"),
            ("safe.rs", "pub struct Handle;\n"),
            ("infra.rs", "pub struct Secret;\n"),
            (
                "api.rs",
                "#[cfg(not(unix))]\nuse crate::safe::Handle;\n#[cfg(unix)]\nuse crate::infra::Secret as Handle;\npub fn leak() -> Handle { loop {} }\n",
            ),
        ],
        "crate::api",
        &["crate::infra"],
        false,
        &[],
    )
    .unwrap();
    assert_eq!(
        out,
        vec!["crate::infra::Secret exposed by fn crate::api::leak"]
    );
}

/// The identical `use`-alias collision expressed as a `cfg_if!` macro invocation rather than bare
/// `#[cfg]` attributes — the audit's cited "identical shape via the 0.3.1 transparency" form, now
/// that `cfg_if!` arm bodies are read as real code (a separate, already-closed finding).
#[test]
pub(super) fn mutually_exclusive_cfg_if_use_aliases_both_react() {
    let out = semantic_findings(
        "cfg-if-use-alias-merge",
        &[
            ("lib.rs", "pub mod safe;\npub mod infra;\npub mod api;\n"),
            ("safe.rs", "pub struct Handle;\n"),
            ("infra.rs", "pub struct Secret;\n"),
            (
                "api.rs",
                "cfg_if::cfg_if! { if #[cfg(unix)] { use crate::infra::Secret as Handle; } else { use crate::safe::Handle; } }\npub fn leak() -> Handle { loop {} }\n",
            ),
        ],
        "crate::api",
        &["crate::infra"],
        false,
        &[],
    )
    .unwrap();
    assert_eq!(
        out,
        vec!["crate::infra::Secret exposed by fn crate::api::leak"]
    );
}

/// Two mutually-exclusive `pub use ... as X;` re-export targets for the identical local name, in
/// the same file, must both canonicalize correctly through the crate-wide `ReexportMap` — a facade
/// path reached through either branch's binding must resolve to ITS OWN target, not collapse to
/// whichever branch was declared second. Before the fix, `ReexportMap` was a single
/// `HashMap<String, String>`.
#[test]
pub(super) fn mutually_exclusive_reexport_targets_both_canonicalize_correctly() {
    let out = semantic_findings(
        "reexport-map-merge-forbidden-first",
        &[
            (
                "lib.rs",
                "pub mod safe;\npub mod infra;\npub mod api;\npub mod facade;\n",
            ),
            ("safe.rs", "pub struct Thing;\n"),
            ("infra.rs", "pub struct Secret;\n"),
            (
                "api.rs",
                "#[cfg(unix)]\npub use crate::infra::Secret as Handle;\n#[cfg(not(unix))]\npub use crate::safe::Thing as Handle;\n",
            ),
            ("facade.rs", "pub fn f() -> crate::api::Handle { loop {} }\n"),
        ],
        "crate::facade",
        &["crate::infra"],
        false,
        &[],
    )
    .unwrap();
    assert_eq!(
        out,
        vec!["crate::infra::Secret exposed by fn crate::facade::f"]
    );
}

/// The identical re-export collision with the two `pub use` declarations in the OPPOSITE order —
/// the forbidden target declared second.
#[test]
pub(super) fn mutually_exclusive_reexport_targets_react_regardless_of_declaration_order() {
    let out = semantic_findings(
        "reexport-map-merge-forbidden-second",
        &[
            (
                "lib.rs",
                "pub mod safe;\npub mod infra;\npub mod api;\npub mod facade;\n",
            ),
            ("safe.rs", "pub struct Thing;\n"),
            ("infra.rs", "pub struct Secret;\n"),
            (
                "api.rs",
                "#[cfg(not(unix))]\npub use crate::safe::Thing as Handle;\n#[cfg(unix)]\npub use crate::infra::Secret as Handle;\n",
            ),
            ("facade.rs", "pub fn f() -> crate::api::Handle { loop {} }\n"),
        ],
        "crate::facade",
        &["crate::infra"],
        false,
        &[],
    )
    .unwrap();
    assert_eq!(
        out,
        vec!["crate::infra::Secret exposed by fn crate::facade::f"]
    );
}

/// The identical `use`-alias collision, this time exercising `resolve_principal`
/// (`crates/hunyi/src/crate_scope.rs`) — the shared principal-trait resolver dyn-trait and
/// impl-trait's *operand-scoped* boundaries both use (per `matches_forbidden_principal`'s own doc:
/// "Both the single-module and subtree operand reactions use this leaf so their resolution
/// semantics cannot drift"). Discovered while fixing the signature-coupling instance of this bug —
/// not named in the original audit findings, but the identical mechanism, independently reproduced
/// here before being fixed, per this project's own reproduce-before-fixing discipline.
/// A cfg_attr(path)-hidden module's own `pub use` re-export must still be observed and folded into
/// the crate-wide re-export closure `resolve_principal` (`crate_scope.rs::extern_resolution`)
/// consumes — the identical `scan_crate` mechanism `dyn_operand_module_findings` and
/// `impl_trait_operand_module_findings` share with signature-coupling, forbidden-marker, and
/// trait-impl-locality, all fixed by the same `resolve_child_modules` change
/// (`hunyi-cfg-attr-path-module-loss`). Independently reproduced here before being folded into
/// that change's verification, per this project's reproduce-before-fixing discipline.
#[test]
pub(super) fn dyn_trait_operand_resolution_reacts_through_a_cfg_attr_path_hidden_reexport() {
    let tree = TempSrcTree::new("dyn-trait-cfg-attr-path-reexport");
    tree.write_all(&[
        (
            "lib.rs",
            "pub mod infra;\n#[cfg_attr(windows, path = \"weird.rs\")]\npub mod facade;\npub mod api;\n",
        ),
        ("infra.rs", "pub trait Port {}\n"),
        ("facade.rs", "pub use crate::infra::Port;\n"),
        (
            "api.rs",
            "pub fn f() -> Box<dyn crate::facade::Port> { loop {} }\n",
        ),
    ]);
    let out = crate::dyn_trait::dyn_operand_module_findings(
        tree.src(),
        &tree.root(),
        "crate::api",
        &["crate::infra::Port".to_string()],
        "x",
        &[],
    )
    .unwrap();
    assert_eq!(out.len(), 1, "{out:?}");
    assert_eq!(
        out[0].0,
        crate::finding::SemanticFact::Exposed {
            kind: crate::finding::ExposureKind::DynTrait,
            subject: "dyn crate::facade::Port".to_string(),
            seam: crate::finding::PublicSeam::FreeFn {
                module: "crate::api".to_string(),
                name: "f".to_string(),
            },
        }
    );
}

#[test]
pub(super) fn dyn_trait_operand_resolution_reacts_regardless_of_cfg_gated_use_alias_order() {
    let tree = TempSrcTree::new("dyn-trait-principal-cfg-use-merge");
    tree.write_all(&[
        ("lib.rs", "pub mod safe;\npub mod infra;\npub mod api;\n"),
        ("safe.rs", "pub trait SafePort {}\n"),
        ("infra.rs", "pub trait Port {}\n"),
        (
            "api.rs",
            "#[cfg(not(unix))]\nuse crate::safe::SafePort as P;\n#[cfg(unix)]\nuse crate::infra::Port as P;\npub fn f() -> Box<dyn P> { loop {} }\n",
        ),
    ]);
    let out = crate::dyn_trait::dyn_operand_module_findings(
        tree.src(),
        &tree.root(),
        "crate::api",
        &["crate::infra::Port".to_string()],
        "x",
        &[],
    )
    .unwrap();
    assert_eq!(out.len(), 1, "{out:?}");
    assert_eq!(
        out[0].0,
        crate::finding::SemanticFact::Exposed {
            kind: crate::finding::ExposureKind::DynTrait,
            subject: "dyn P".to_string(),
            seam: crate::finding::PublicSeam::FreeFn {
                module: "crate::api".to_string(),
                name: "f".to_string(),
            },
        }
    );
}

/// The identical shape at impl-trait's own operand-scoped boundary — same shared
/// `resolve_principal`/`matches_forbidden_principal` leaf as the dyn-trait test above.
#[test]
pub(super) fn impl_trait_operand_resolution_reacts_regardless_of_cfg_gated_use_alias_order() {
    let tree = TempSrcTree::new("impl-trait-principal-cfg-use-merge");
    tree.write_all(&[
        ("lib.rs", "pub mod safe;\npub mod infra;\npub mod api;\n"),
        ("safe.rs", "pub trait SafePort {}\n"),
        ("infra.rs", "pub trait Port {}\n"),
        (
            "api.rs",
            "#[cfg(not(unix))]\nuse crate::safe::SafePort as P;\n#[cfg(unix)]\nuse crate::infra::Port as P;\npub fn f() -> impl P { loop {} }\n",
        ),
    ]);
    let out = crate::impl_trait::impl_trait_operand_module_findings(
        tree.src(),
        &tree.root(),
        "crate::api",
        &["crate::infra::Port".to_string()],
        "x",
        &[],
    )
    .unwrap();
    assert_eq!(out.len(), 1, "{out:?}");
    assert_eq!(
        out[0].0,
        crate::finding::SemanticFact::Exposed {
            kind: crate::finding::ExposureKind::ImplTrait,
            subject: "impl P".to_string(),
            seam: crate::finding::PublicSeam::FreeFn {
                module: "crate::api".to_string(),
                name: "f".to_string(),
            },
        }
    );
}

/// The reverse cfg declaration order of `dyn_trait_operand_resolution_reacts_regardless_of_cfg_gated_use_alias_order`
/// above: `#[cfg(unix)]` written first, `#[cfg(not(unix))]` second. Both orderings must resolve
/// the alias identically — a merge that silently favored "whichever `use` was declared first"
/// would pass the original test and still be wrong.
#[test]
pub(super) fn dyn_trait_operand_resolution_reacts_regardless_of_cfg_gated_use_alias_order_reversed()
{
    let tree = TempSrcTree::new("dyn-trait-principal-cfg-use-merge-reversed");
    tree.write_all(&[
        ("lib.rs", "pub mod safe;\npub mod infra;\npub mod api;\n"),
        ("safe.rs", "pub trait SafePort {}\n"),
        ("infra.rs", "pub trait Port {}\n"),
        (
            "api.rs",
            "#[cfg(unix)]\nuse crate::infra::Port as P;\n#[cfg(not(unix))]\nuse crate::safe::SafePort as P;\npub fn f() -> Box<dyn P> { loop {} }\n",
        ),
    ]);
    let out = crate::dyn_trait::dyn_operand_module_findings(
        tree.src(),
        &tree.root(),
        "crate::api",
        &["crate::infra::Port".to_string()],
        "x",
        &[],
    )
    .unwrap();
    assert_eq!(out.len(), 1, "{out:?}");
    assert_eq!(
        out[0].0,
        crate::finding::SemanticFact::Exposed {
            kind: crate::finding::ExposureKind::DynTrait,
            subject: "dyn P".to_string(),
            seam: crate::finding::PublicSeam::FreeFn {
                module: "crate::api".to_string(),
                name: "f".to_string(),
            },
        }
    );
}

/// The reverse cfg declaration order of `impl_trait_operand_resolution_reacts_regardless_of_cfg_gated_use_alias_order`
/// above — see the dyn-trait twin's doc comment for why both orders must be pinned separately.
#[test]
pub(super) fn impl_trait_operand_resolution_reacts_regardless_of_cfg_gated_use_alias_order_reversed()
 {
    let tree = TempSrcTree::new("impl-trait-principal-cfg-use-merge-reversed");
    tree.write_all(&[
        ("lib.rs", "pub mod safe;\npub mod infra;\npub mod api;\n"),
        ("safe.rs", "pub trait SafePort {}\n"),
        ("infra.rs", "pub trait Port {}\n"),
        (
            "api.rs",
            "#[cfg(unix)]\nuse crate::infra::Port as P;\n#[cfg(not(unix))]\nuse crate::safe::SafePort as P;\npub fn f() -> impl P { loop {} }\n",
        ),
    ]);
    let out = crate::impl_trait::impl_trait_operand_module_findings(
        tree.src(),
        &tree.root(),
        "crate::api",
        &["crate::infra::Port".to_string()],
        "x",
        &[],
    )
    .unwrap();
    assert_eq!(out.len(), 1, "{out:?}");
    assert_eq!(
        out[0].0,
        crate::finding::SemanticFact::Exposed {
            kind: crate::finding::ExposureKind::ImplTrait,
            subject: "impl P".to_string(),
            seam: crate::finding::PublicSeam::FreeFn {
                module: "crate::api".to_string(),
                name: "f".to_string(),
            },
        }
    );
}

// --- body-nested impl observation (const-eval trick / fn-body sibling) ----
//
// `const _: () = { impl Foo { … } };` is a common "const-eval trick" idiom (forcing a
// compile-time trait assertion or a doctest/dogfooding scratch impl); `fn _also() { impl Foo {
// … } }` is its fn-body-nested sibling. Both wrap a real `impl` block — inherent or trait —
// inside a body that every capability below previously treated as opaque, the same way it
// correctly treats a body-nested `mod` (see `async_subtree_does_not_observe_a_body_nested_module`
// above) as unreachable. Unlike a `mod`, an `impl` is not scoped by where it is lexically
// written — Rust binds it to its self type's own coherence set regardless of nesting — so
// `Svc::leak`/`Svc::run` below are real, externally callable public API the instant `Svc` itself
// is module-level, and every capability had a genuine false negative here. See
// `syn_util::body_nested_impls` for the extraction and its stated one-level/`const`-or-`fn`-only
// bound; the tests after the reaction cases below pin that bound so it does not silently widen.

#[test]
pub(super) fn signature_coupling_reacts_on_a_const_wrapped_inherent_impl() {
    assert_eq!(
        findings(
            "body-nested-const-signature",
            &[
                ("lib.rs", "pub mod infra;\npub mod api;\n"),
                ("infra.rs", "pub struct Db;\n"),
                (
                    "api.rs",
                    "pub struct Svc;\nconst _: () = {\n    impl Svc {\n        pub fn leak(&self) -> crate::infra::Db { unimplemented!() }\n    }\n};\n",
                ),
            ],
            "crate::api",
            &["crate::infra"],
        )
        .unwrap(),
        ["crate::infra::Db exposed by fn <crate::api::Svc>::leak"],
    );
}

#[test]
pub(super) fn signature_coupling_reacts_on_a_fn_body_wrapped_inherent_impl() {
    assert_eq!(
        findings(
            "body-nested-fnbody-signature",
            &[
                ("lib.rs", "pub mod infra;\npub mod api;\n"),
                ("infra.rs", "pub struct Db;\n"),
                (
                    "api.rs",
                    "pub struct Svc;\nfn _also() {\n    impl Svc {\n        pub fn leak(&self) -> crate::infra::Db { unimplemented!() }\n    }\n}\n",
                ),
            ],
            "crate::api",
            &["crate::infra"],
        )
        .unwrap(),
        ["crate::infra::Db exposed by fn <crate::api::Svc>::leak"],
    );
}

#[test]
pub(super) fn async_exposure_reacts_on_a_const_wrapped_inherent_impl() {
    assert_eq!(
        async_mod(
            "body-nested-const-async",
            "pub struct Svc;\nconst _: () = {\n    impl Svc {\n        pub async fn run(&self) {}\n    }\n};\n",
        )
        .unwrap(),
        ["async fn <crate::m::Svc>::run(&self)"],
    );
}

#[test]
pub(super) fn async_exposure_reacts_on_a_fn_body_wrapped_inherent_impl() {
    assert_eq!(
        async_mod(
            "body-nested-fnbody-async",
            "pub struct Svc;\nfn _also() {\n    impl Svc {\n        pub async fn run(&self) {}\n    }\n}\n",
        )
        .unwrap(),
        ["async fn <crate::m::Svc>::run(&self)"],
    );
}

#[test]
pub(super) fn dyn_trait_reacts_on_a_const_wrapped_inherent_impl() {
    assert_eq!(
        dyn_mod(
            "body-nested-const-dyn",
            "pub struct Svc;\nconst _: () = {\n    impl Svc {\n        pub fn dynamic(&self) -> Box<dyn crate::Port> { unimplemented!() }\n    }\n};\n",
        )
        .unwrap(),
        ["dyn crate::Port exposed by fn <crate::m::Svc>::dynamic"],
    );
}

#[test]
pub(super) fn dyn_trait_reacts_on_a_fn_body_wrapped_inherent_impl() {
    assert_eq!(
        dyn_mod(
            "body-nested-fnbody-dyn",
            "pub struct Svc;\nfn _also() {\n    impl Svc {\n        pub fn dynamic(&self) -> Box<dyn crate::Port> { unimplemented!() }\n    }\n}\n",
        )
        .unwrap(),
        ["dyn crate::Port exposed by fn <crate::m::Svc>::dynamic"],
    );
}

#[test]
pub(super) fn impl_trait_reacts_on_a_const_wrapped_inherent_impl() {
    assert_eq!(
        impl_trait_mod(
            "body-nested-const-impltrait",
            "pub struct Svc;\nconst _: () = {\n    impl Svc {\n        pub fn existential(&self) -> impl crate::Port { unimplemented!() }\n    }\n};\n",
        )
        .unwrap(),
        ["impl crate::Port exposed by fn <crate::m::Svc>::existential"],
    );
}

#[test]
pub(super) fn impl_trait_reacts_on_a_fn_body_wrapped_inherent_impl() {
    assert_eq!(
        impl_trait_mod(
            "body-nested-fnbody-impltrait",
            "pub struct Svc;\nfn _also() {\n    impl Svc {\n        pub fn existential(&self) -> impl crate::Port { unimplemented!() }\n    }\n}\n",
        )
        .unwrap(),
        ["impl crate::Port exposed by fn <crate::m::Svc>::existential"],
    );
}

#[test]
pub(super) fn trait_impl_locality_reacts_on_a_const_wrapped_trait_impl() {
    let out = locality_findings(
        "body-nested-const-locality",
        &[
            (
                "lib.rs",
                "pub mod command;\npub mod commands;\npub mod rogue;\n",
            ),
            ("command.rs", "pub trait Command { fn run(&self); }\n"),
            (
                "commands.rs",
                "pub struct Ok1;\nimpl crate::command::Command for Ok1 { fn run(&self) {} }\n",
            ),
            (
                "rogue.rs",
                "pub struct Rogue;\nconst _: () = {\n    impl crate::command::Command for Rogue {\n        fn run(&self) {}\n    }\n};\n",
            ),
        ],
        "crate::command::Command",
        &["crate::commands"],
    )
    .unwrap();
    assert_eq!(
        out,
        ["crate::rogue (impl crate::command::Command for crate::rogue::Rogue)"],
    );
}

#[test]
pub(super) fn trait_impl_locality_reacts_on_a_fn_body_wrapped_trait_impl() {
    let out = locality_findings(
        "body-nested-fnbody-locality",
        &[
            (
                "lib.rs",
                "pub mod command;\npub mod commands;\npub mod rogue;\n",
            ),
            ("command.rs", "pub trait Command { fn run(&self); }\n"),
            (
                "commands.rs",
                "pub struct Ok1;\nimpl crate::command::Command for Ok1 { fn run(&self) {} }\n",
            ),
            (
                "rogue.rs",
                "pub struct Rogue2;\nfn _also() {\n    impl crate::command::Command for Rogue2 {\n        fn run(&self) {}\n    }\n}\n",
            ),
        ],
        "crate::command::Command",
        &["crate::commands"],
    )
    .unwrap();
    assert_eq!(
        out,
        ["crate::rogue (impl crate::command::Command for crate::rogue::Rogue2)"],
    );
}

#[test]
pub(super) fn forbidden_marker_reacts_on_a_const_wrapped_hand_impl() {
    // The impl form shares `scan.impls` with trait-impl-locality, so it closes the identical
    // gap for `ForbiddenMarkerBoundary`'s hand-impl acquisition form.
    let out = marker_findings(
        "body-nested-const-marker",
        &[
            ("lib.rs", "pub mod domain;\npub mod wire;\n"),
            ("domain.rs", "pub struct Order;\n"),
            (
                "wire.rs",
                "const _: () = {\n    impl serde::Serialize for crate::domain::Order {}\n};\n",
            ),
        ],
        "crate::domain",
        &["serde::Serialize"],
    )
    .unwrap();
    assert_eq!(
        out,
        ["impl serde::Serialize for crate::domain::Order in crate::wire"],
    );
}

#[test]
pub(super) fn forbidden_marker_reacts_on_a_fn_body_wrapped_hand_impl() {
    let out = marker_findings(
        "body-nested-fnbody-marker",
        &[
            ("lib.rs", "pub mod domain;\npub mod wire;\n"),
            ("domain.rs", "pub struct Order;\n"),
            (
                "wire.rs",
                "fn _also() {\n    impl serde::Serialize for crate::domain::Order {}\n}\n",
            ),
        ],
        "crate::domain",
        &["serde::Serialize"],
    )
    .unwrap();
    assert_eq!(
        out,
        ["impl serde::Serialize for crate::domain::Order in crate::wire"],
    );
}

// --- body-nested impl observation: the control and the stated scope bounds ---

#[test]
pub(super) fn signature_coupling_control_the_identical_unwrapped_impl_also_reacts() {
    // Control: proves the fixture shape is sound on its own (not a false pass from an unrelated
    // fixture error) — the identical `leak()` written as an ordinary top-level inherent impl.
    assert_eq!(
        findings(
            "body-nested-control-unwrapped",
            &[
                ("lib.rs", "pub mod infra;\npub mod api;\n"),
                ("infra.rs", "pub struct Db;\n"),
                (
                    "api.rs",
                    "pub struct Svc;\nimpl Svc {\n    pub fn leak(&self) -> crate::infra::Db { unimplemented!() }\n}\n",
                ),
            ],
            "crate::api",
            &["crate::infra"],
        )
        .unwrap(),
        ["crate::infra::Db exposed by fn <crate::api::Svc>::leak"],
    );
}

#[test]
pub(super) fn a_plain_fn_directly_in_a_const_body_stays_a_stated_bound() {
    // Scope bound: `body_nested_impls` extracts ONLY `impl` blocks. A plain `pub fn` written
    // directly in a const/fn body (no enclosing `impl`) is genuinely scoped to that body and
    // unreachable as `crate::…` — exactly like the existing body-nested-`mod` bound — so it must
    // stay unobserved; recovering it would be a NEW, unaudited claim, not the fix this change
    // makes.
    assert_eq!(
        findings(
            "body-nested-bound-plain-fn",
            &[
                ("lib.rs", "pub mod infra;\npub mod api;\n"),
                ("infra.rs", "pub struct Db;\n"),
                (
                    "api.rs",
                    "const _: () = {\n    pub fn also_hidden() -> crate::infra::Db { unimplemented!() }\n};\n",
                ),
            ],
            "crate::api",
            &["crate::infra"],
        )
        .unwrap(),
        Vec::<String>::new(),
    );
}

#[test]
pub(super) fn an_impl_nested_one_level_further_stays_a_stated_bound() {
    // Scope bound: only an `impl` that is a DIRECT statement of the const/fn's own outermost
    // block is recovered. One level further in (here, inside an `if` block within the fn) is
    // out of scope — the audited trigger shapes are both exactly one level deep, and recursing
    // into arbitrary expression trees would invent tolerance for a shape nobody has shown.
    assert_eq!(
        findings(
            "body-nested-bound-two-levels",
            &[
                ("lib.rs", "pub mod infra;\npub mod api;\n"),
                ("infra.rs", "pub struct Db;\n"),
                (
                    "api.rs",
                    "pub struct Svc;\nfn _also() {\n    if true {\n        impl Svc {\n            pub fn leak(&self) -> crate::infra::Db { unimplemented!() }\n        }\n    }\n}\n",
                ),
            ],
            "crate::api",
            &["crate::infra"],
        )
        .unwrap(),
        Vec::<String>::new(),
    );
}

#[test]
pub(super) fn a_static_wrapped_impl_stays_a_stated_bound() {
    // Scope bound: only `const`/`fn` bodies are inspected, not `static`. The const-eval trick is
    // specifically about `const` (compile-time evaluation even when never read); no audited
    // idiom uses `static` for it, so widening to `static` would be unaudited tolerance.
    assert_eq!(
        findings(
            "body-nested-bound-static",
            &[
                ("lib.rs", "pub mod infra;\npub mod api;\n"),
                ("infra.rs", "pub struct Db;\n"),
                (
                    "api.rs",
                    "pub struct Svc;\nstatic S: () = {\n    impl Svc {\n        pub fn leak(&self) -> crate::infra::Db { unimplemented!() }\n    }\n};\n",
                ),
            ],
            "crate::api",
            &["crate::infra"],
        )
        .unwrap(),
        Vec::<String>::new(),
    );
}

#[test]
pub(super) fn a_struct_directly_in_a_const_body_stays_a_stated_bound() {
    // Scope bound: `body_nested_impls` extracts ONLY `impl` blocks. A struct DEFINITION written
    // directly in a const/fn body has no enclosing `impl` to recover and is genuinely scoped to
    // that body -- exactly like the plain-fn bound above -- so it must stay unobserved;
    // recovering it would be a NEW, unaudited claim, not the fix this change makes.
    assert_eq!(
        findings(
            "body-nested-bound-struct",
            &[
                ("lib.rs", "pub mod infra;\npub mod api;\n"),
                ("infra.rs", "pub struct Db;\n"),
                (
                    "api.rs",
                    "const _: () = {\n    pub struct AlsoHidden { pub field: crate::infra::Db }\n};\n",
                ),
            ],
            "crate::api",
            &["crate::infra"],
        )
        .unwrap(),
        Vec::<String>::new(),
    );
}

#[test]
pub(super) fn a_mod_directly_in_a_const_body_stays_a_stated_bound() {
    // Scope bound: `body_nested_impls` extracts ONLY `impl` blocks. A `mod` declared directly in
    // a const/fn body is unreachable as `crate::...` for the same reason a body-nested struct is
    // (no enclosing `impl` to recover) -- this recovery's own bound, distinct from
    // `async_subtree_does_not_observe_a_body_nested_module`'s unrelated async-subtree coverage,
    // which was the only pre-existing test to even incidentally touch a body-nested `mod`.
    assert_eq!(
        findings(
            "body-nested-bound-mod",
            &[
                ("lib.rs", "pub mod infra;\npub mod api;\n"),
                ("infra.rs", "pub struct Db;\n"),
                (
                    "api.rs",
                    "const _: () = {\n    pub mod inner {\n        pub struct AlsoHidden { pub field: crate::infra::Db }\n    }\n};\n",
                ),
            ],
            "crate::api",
            &["crate::infra"],
        )
        .unwrap(),
        Vec::<String>::new(),
    );
}