rustcall_julia_core 0.3.0

Shared Rust core for RustCall.jl: FFI manifest model, syn-based extraction, wrapper codegen, generic specialization
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
//! Scanning a crate that only carries PyO3 attributes (#275, Phase 1).
//!
//! A crate written for [PyO3](https://pyo3.rs) has no RustCall attribute
//! anywhere, but its `#[pyfunction]` / `#[pyclass]` / `#[pymethods]` items are
//! ordinary Rust items: a wrapper crate can call them exactly like any other
//! dependency (verified end to end in the #275 MWE). This module reports those
//! items in the manifest so that a Phase-2 wrapper crate can generate
//! `extern "C"` entry points for them.
//!
//! Three rules shape what is reported:
//!
//! * **The manifest describes what Phase 2 *will* generate.** Every scanned
//!   item carries the symbol the wrapper crate is going to export
//!   (`rustcall_<name>` / `rustcall_<Struct>_<method>`, the #279 scheme) with
//!   [`Function::exported`] = `false`, because today nothing emits it.
//! * **`#[julia]` owns an item it also marks.** `#[julia]` is additive since
//!   #279 and already exports `rustcall_<name>`; emitting a second wrapper for
//!   the same item from the PyO3 side would collide on that symbol, so the
//!   scan skips any item carrying a RustCall attribute (it is reported through
//!   the `#[julia]` path instead).
//! * **Fail closed.** Anything the wrapper crate could not compile — a
//!   non-`pub` item, a signature mentioning a type that needs a live Python
//!   interpreter, a generic — is still reported, but with a
//!   [`skip_reason`](crate::manifest::skip_reason) so `@rust_crate` can tell
//!   the user *why* an item is missing instead of silently dropping it.
//!
//! `PyResult<T>` is deliberately **not** a skip reason: creating and dropping a
//! `PyErr` without an interpreter is safe, only rendering one is not (it panics
//! inside pyo3, and the panic crossing `extern "C"` aborts the process). It is
//! recorded as [`ReturnKind::PyResult`] with the `Ok` type, so Phase 2 can lower
//! it to an opaque error flag.

use syn::spanned::Spanned;
use syn::{FnArg, ImplItem, ImplItemFn, Item, ItemFn, ItemStruct, ReturnType, Type};

use crate::attrs::{
    julia_owns_entry_point, pyo3_field_access, pyo3_marker, pyo3_method_markers, pyo3_name,
    pyo3_signature, visibility_string, Pyo3Marker, Pyo3MethodMarker, Pyo3ParameterKind,
};
use crate::cfg::predicate_string;
use crate::extract::fn_args;
use crate::manifest::{
    skip_reason, Attribute, Field, Function, Manifest, Method, ReturnKind, Struct,
};
use crate::paths::{
    import_of_type_alias, imports_of_use, locate, ImplHeader, Located, ScannedImport,
};
use crate::types::{
    extract_option_type, extract_result_type, generics_to_type_params, has_impl_trait,
    has_type_params, is_ffi_compatible_type, is_str_ref_type, is_string_type,
    return_type_to_string, type_to_string, unparen,
};

/// Append every PyO3-only item of `items` to `manifest`.
///
/// `items` is one level of a parsed file; inline `mod`s are visited
/// recursively and their names recorded in `module_path`, because a wrapper
/// crate has to name the item as `user_crate::module::item`.
///
/// Returns the **out-of-line** module declarations found on the way
/// (`pub mod api;` with no body): they live in another file, which only a
/// caller that can read files — `rustcall-extract --crate-root` — can follow.
/// Scanning each `.rs` file as its own root instead would report `api::deep` as
/// a crate-root item and miss a private parent module entirely (#275).
pub fn extract_pyo3_items(items: &[Item], manifest: &mut Manifest) -> Vec<PendingModule> {
    let mut scan = Pyo3Scan::new();
    let pending = scan.file(items, &[], true, &[], manifest);
    scan.finish(manifest);
    pending
}

/// One out-of-line `mod name;` declaration: where it sits in the module tree,
/// whether that position is reachable from outside the crate, where its file
/// lives relative to the declaring file's directory, and the `#[path = "..."]`
/// override if it has one.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PendingModule {
    /// Module path of the declaration itself, including its own name.
    pub module_path: Vec<String>,
    /// Whether every module on the way here (and this one) is `pub`.
    pub reachable: bool,
    /// The module's name, i.e. the last element of `module_path`.
    pub name: String,
    /// `#[path = "..."]` on the declaration, if present.
    pub path_attr: Option<String>,
    /// The **inline** modules enclosing the declaration within its own file.
    ///
    /// rustc resolves `mod outer { pub mod child; }` in `src/lib.rs` to
    /// `src/outer/child.rs`, not `src/child.rs`: an inline module contributes a
    /// directory to the search path of its out-of-line children. Empty when the
    /// declaration is at the top level of its file.
    pub dir_components: Vec<String>,
    /// The `#[cfg]` attributes of the declaration and of every module enclosing
    /// it, which every item in the module's file inherits (#300 review).
    pub cfg: Vec<syn::Attribute>,
}

/// Crate-wide state of a PyO3 scan.
///
/// `#[pyclass]` structs and their `#[pymethods]` blocks are collected
/// separately and married in [`Pyo3Scan::finish`], because Rust does not
/// require them to live together: `impl C` is legal in any module that has `C`
/// in scope, and in a multi-file crate the two are routinely in different
/// files. Matching them per file (or per module level) would silently drop the
/// methods of every such class.
#[derive(Debug, Default)]
pub struct Pyo3Scan {
    classes: Vec<ScannedClass>,
    impls: Vec<ScannedImpl>,
    imports: Vec<ScannedImport>,
    routes: crate::public_routes::PublicRoutes,
    edition_2015: bool,
    intrinsic_skips: std::collections::BTreeMap<(Vec<String>, String, usize, String), String>,
}

impl Pyo3Scan {
    pub fn with_edition(edition: &str) -> Self {
        Self {
            routes: crate::public_routes::PublicRoutes::with_edition(edition),
            edition_2015: edition == "2015",
            ..Self::default()
        }
    }
}

#[derive(Debug)]
struct ScannedClass {
    module_path: Vec<String>,
    entry: Struct,
    visibility: syn::Visibility,
    /// Accessors under the same field/type checks, before module reachability
    /// disables them. A public re-export can restore that reachability later.
    reachable_fields: Vec<Field>,
    /// The `#[cfg]` of the enclosing modules: what tells cfg-exclusive copies
    /// of one fragment apart, and what a `#[pymethods]` block written beside
    /// one copy shares with it (#357 review).
    cfg: Vec<syn::Attribute>,
}

#[derive(Debug)]
struct ScannedImpl {
    /// The type the block is for, the qualifier written in front of it
    /// (`impl a::C` names the class exactly when several modules define a
    /// `C`) and the module the block sits in — what the shared resolver in
    /// `crate::paths` matches on.
    header: ImplHeader,
    line: usize,
    funcs: Vec<ImplItemFn>,
    /// The `#[cfg]` of the block itself and of every module enclosing it: a
    /// `#[pymethods]` block may sit in a gated module far from its class, and
    /// its methods exist only under that predicate (#300 review).
    cfg: Vec<syn::Attribute>,
    /// The enclosing modules' `#[cfg]` alone; see [`ScannedClass::cfg`].
    enclosing_cfg: Vec<syn::Attribute>,
}

/// The classes a `#[pymethods]` block resolved to `index` attaches to, among
/// the cfg-exclusive copies of that class at that module path: the copy whose
/// enclosing `#[cfg]` is `enclosing` when there is one; else every copy whose
/// predicate can coexist with `enclosing` — a block written outside the
/// copies applies to whichever one rustc compiles; else the located one
/// (#357 review).
fn cfg_variants_of(
    classes: &[ScannedClass],
    index: usize,
    enclosing: &[syn::Attribute],
) -> Vec<usize> {
    let want = crate::cfg::predicate_string(enclosing);
    let here = &classes[index];
    let same =
        |c: &ScannedClass| c.entry.name == here.entry.name && c.module_path == here.module_path;
    if crate::cfg::predicate_string(&here.cfg) == want {
        return vec![index];
    }
    if let Some(exact) = classes
        .iter()
        .position(|c| same(c) && crate::cfg::predicate_string(&c.cfg) == want)
    {
        return vec![exact];
    }
    // Written outside every copy — at the root, or in a module gated on
    // something else: every copy whose predicate can hold together with the
    // block's, i.e. all but the provably exclusive ones (#357 review).
    let overlapping: Vec<usize> = classes
        .iter()
        .enumerate()
        .filter(|(_, c)| same(c) && !cfg_exclusive(&crate::cfg::predicate_string(&c.cfg), &want))
        .map(|(i, _)| i)
        .collect();
    if !overlapping.is_empty() {
        return overlapping;
    }
    vec![index]
}

impl Located for ScannedClass {
    fn name(&self) -> &str {
        &self.entry.name
    }

    fn module_path(&self) -> &[String] {
        &self.module_path
    }

    fn visible_from(&self, module_path: &[String]) -> bool {
        crate::paths::visible_from(&self.visibility, &self.module_path, module_path)
    }
}

impl Pyo3Scan {
    pub fn new() -> Self {
        Pyo3Scan::default()
    }

    /// Scan one file of the crate. `module_path` is where the file sits in the
    /// module tree (empty for the crate root) and `reachable` says whether
    /// every `mod` leading to it is `pub`.
    ///
    /// Free functions go straight into `manifest`; classes and `#[pymethods]`
    /// blocks are held until [`Pyo3Scan::finish`].
    ///
    /// `enclosing_cfg` is the `#[cfg]` of every module on the way to this
    /// file; every item found inherits it in its `cfg` / `cfg_features`, so a
    /// wrapper generated from a lenient scan refuses an item whose *module* is
    /// gated exactly as it refuses one gated itself (#300 review).
    pub fn file(
        &mut self,
        items: &[Item],
        module_path: &[String],
        reachable: bool,
        enclosing_cfg: &[syn::Attribute],
        manifest: &mut Manifest,
    ) -> Vec<PendingModule> {
        self.routes.file(items, module_path, enclosing_cfg);
        let mut path = module_path.to_vec();
        let mut dirs = Vec::new();
        let mut pending = Vec::new();
        self.level(
            items,
            &mut path,
            &mut dirs,
            reachable,
            enclosing_cfg,
            manifest,
            &mut pending,
            &[],
            false,
        );
        pending
    }

    #[allow(clippy::too_many_arguments)]
    fn level(
        &mut self,
        items: &[Item],
        module_path: &mut Vec<String>,
        dir_components: &mut Vec<String>,
        reachable: bool,
        enclosing_cfg: &[syn::Attribute],
        manifest: &mut Manifest,
        pending: &mut Vec<PendingModule>,
        python_path: &[String],
        inside_pymodule: bool,
    ) {
        for item in items {
            match item {
                Item::Type(alias) => {
                    if let Some(import) =
                        import_of_type_alias(alias, module_path, self.edition_2015)
                    {
                        self.imports.push(import);
                    }
                }
                Item::Fn(f) => {
                    if julia_owns_entry_point(&f.attrs) {
                        // Owned by `#[julia]`, which exports `rustcall_<name>`
                        // itself (#279): reporting it here would describe a
                        // second wrapper under the same symbol.
                        continue;
                    }
                    match pyo3_marker(&f.attrs) {
                        Some(Pyo3Marker::Function) => {
                            let intrinsic = function_entry(
                                f,
                                Attribute::PyFunction,
                                true,
                                module_path,
                                python_path,
                                enclosing_cfg,
                            );
                            self.intrinsic_skips.insert(
                                (
                                    module_path.clone(),
                                    intrinsic.name.clone(),
                                    intrinsic.line,
                                    intrinsic.cfg.clone(),
                                ),
                                intrinsic.skip_reason,
                            );
                            manifest.functions.push(function_entry(
                                f,
                                Attribute::PyFunction,
                                reachable,
                                module_path,
                                python_path,
                                enclosing_cfg,
                            ));
                        }
                        Some(Pyo3Marker::Module) => {
                            manifest.functions.push(function_entry(
                                f,
                                Attribute::PyModule,
                                reachable,
                                module_path,
                                &[],
                                enclosing_cfg,
                            ));
                        }
                        _ => {}
                    }
                }
                Item::Struct(s) => {
                    if julia_owns_entry_point(&s.attrs) {
                        continue;
                    }
                    if pyo3_marker(&s.attrs) == Some(Pyo3Marker::Class) {
                        self.classes.push(ScannedClass {
                            module_path: module_path.clone(),
                            entry: class_entry(
                                s,
                                reachable,
                                module_path,
                                python_path,
                                enclosing_cfg,
                            ),
                            reachable_fields: class_entry(
                                s,
                                true,
                                module_path,
                                python_path,
                                enclosing_cfg,
                            )
                            .fields,
                            visibility: s.vis.clone(),
                            cfg: enclosing_cfg.to_vec(),
                        });
                    }
                }
                Item::Impl(imp) => {
                    if pyo3_marker(&imp.attrs) != Some(Pyo3Marker::Methods) {
                        continue;
                    }
                    let Some(header) = ImplHeader::of(imp, module_path) else {
                        continue;
                    };
                    self.impls.push(ScannedImpl {
                        header,
                        line: imp.span().start().line,
                        cfg: crate::cfg::effective_cfg_attrs(enclosing_cfg, &imp.attrs),
                        enclosing_cfg: enclosing_cfg.to_vec(),
                        funcs: imp
                            .items
                            .iter()
                            .filter_map(|ii| match ii {
                                ImplItem::Fn(f) => Some(f.clone()),
                                _ => None,
                            })
                            .collect(),
                    });
                }
                Item::Use(u) => {
                    // What a bare `impl C` in this module could be referring to.
                    self.imports.extend(imports_of_use(u, module_path));
                }
                Item::Mod(m) => {
                    let inner_reachable = reachable && matches!(m.vis, syn::Visibility::Public(_));
                    let inner_cfg = crate::cfg::effective_cfg_attrs(enclosing_cfg, &m.attrs);
                    // `#[pymodule] mod name { ... }` is PyO3's declarative
                    // module: PyO3 registers its direct items itself, so the
                    // Python attribute path of an item is the declarative
                    // nesting below the imported module (#424). It is not a
                    // RustCall module and has no `PendingModule`.
                    if pyo3_marker(&m.attrs) == Some(Pyo3Marker::Module) {
                        manifest
                            .functions
                            .push(module_entry(m, module_path, enclosing_cfg));
                        // The outermost declarative module *is* the imported
                        // module, so its name is not part of the path below it;
                        // a nested one contributes its Python name (#424).
                        let inner_python = if inside_pymodule {
                            let mut path = python_path.to_vec();
                            path.push(_pyo3_module_python_name(&m.attrs, &m.ident));
                            path
                        } else {
                            Vec::new()
                        };
                        module_path.push(m.ident.to_string());
                        if let Some((_, inner)) = &m.content {
                            dir_components.push(m.ident.to_string());
                            self.level(
                                inner,
                                module_path,
                                dir_components,
                                inner_reachable,
                                &inner_cfg,
                                manifest,
                                pending,
                                &inner_python,
                                true,
                            );
                            dir_components.pop();
                        } else {
                            // `#[pymodule] mod name;` — the body lives in
                            // another file, which the caller follows without a
                            // Python scope (an out-of-line declarative module is
                            // not handled). Record it as an ordinary pending
                            // module so its items are still scanned.
                            pending.push(PendingModule {
                                module_path: module_path.clone(),
                                reachable: inner_reachable,
                                name: m.ident.to_string(),
                                path_attr: path_attribute(&m.attrs),
                                dir_components: dir_components.clone(),
                                cfg: inner_cfg,
                            });
                        }
                        module_path.pop();
                        continue;
                    }
                    module_path.push(m.ident.to_string());
                    match &m.content {
                        Some((_, inner)) => {
                            dir_components.push(m.ident.to_string());
                            self.level(
                                inner,
                                module_path,
                                dir_components,
                                inner_reachable,
                                &inner_cfg,
                                manifest,
                                pending,
                                python_path,
                                inside_pymodule,
                            );
                            dir_components.pop();
                        }
                        // `mod name;` — the body is in another file; record
                        // where it belongs so the caller can follow it.
                        None => pending.push(PendingModule {
                            module_path: module_path.clone(),
                            reachable: inner_reachable,
                            name: m.ident.to_string(),
                            path_attr: path_attribute(&m.attrs),
                            dir_components: dir_components.clone(),
                            cfg: inner_cfg,
                        }),
                    }
                    module_path.pop();
                }
                _ => {}
            }
        }
    }

    /// Attach every `#[pymethods]` block to its class and emit the structs.
    ///
    /// A block is matched, in order, to: the class named by an explicit
    /// qualifier (`impl a::C`, resolved against the impl's own module and
    /// against the crate root); the class of that name in the impl's own
    /// module; and finally the one class of that name anywhere in the crate.
    /// When two modules define classes of the same name and nothing
    /// disambiguates, the block is dropped rather than attached to a guess — a
    /// wrong `Struct::method` would not compile in Phase 2.
    ///
    /// Order is by (module path, line) so the result does not depend on the
    /// order the caller happened to visit files in.
    pub fn finish(mut self, manifest: &mut Manifest) {
        let mut route_cache = std::collections::BTreeMap::new();
        for function in &mut manifest.functions {
            if !function.attribute.is_pyo3_scan() {
                continue;
            }
            let mut canonical = function.module_path.clone();
            canonical.push(function.name.clone());
            let routes = route_cache
                .entry(function.cfg.clone())
                .or_insert_with(|| self.routes.resolve_for(&function.cfg));
            if let Some(route) =
                routes.get(&(crate::public_routes::Namespace::Value, canonical.clone()))
            {
                if function.skip_reason == skip_reason::NOT_PUBLIC {
                    if let Some(intrinsic) = self.intrinsic_skips.get(&(
                        function.module_path.clone(),
                        function.name.clone(),
                        function.line,
                        function.cfg.clone(),
                    )) {
                        function.skip_reason = intrinsic.clone();
                    }
                }
                if route.path != canonical {
                    function.callable_path = route.path.clone();
                }
                merge_route_cfg(&mut function.cfg, &mut function.cfg_features, route);
            }
        }
        for class in &mut self.classes {
            let mut canonical = class.module_path.clone();
            canonical.push(class.entry.name.clone());
            let routes = route_cache
                .entry(class.entry.cfg.clone())
                .or_insert_with(|| self.routes.resolve_for(&class.entry.cfg));
            if let Some(route) =
                routes.get(&(crate::public_routes::Namespace::Type, canonical.clone()))
            {
                if class.entry.skip_reason == skip_reason::NOT_PUBLIC {
                    class.entry.skip_reason = item_skip_reason(
                        &class.visibility,
                        true,
                        !class.entry.type_params.is_empty(),
                    )
                    .unwrap_or_default();
                    class.entry.fields = std::mem::take(&mut class.reachable_fields);
                }
                if route.path != canonical {
                    class.entry.callable_path = route.path.clone();
                }
                merge_route_cfg(&mut class.entry.cfg, &mut class.entry.cfg_features, route);
            }
        }
        self.impls.sort_by(|a, b| {
            a.header
                .module_path
                .cmp(&b.header.module_path)
                .then(a.line.cmp(&b.line))
        });

        for imp in &self.impls {
            // A block that names no class, or an ambiguous one, is dropped: the
            // PyO3 scan describes what a wrapper crate could wrap, and a wrong
            // `Struct::method` would not compile in Phase 2.
            let Ok(index) = locate(&self.classes, &imp.header, &self.imports) else {
                continue;
            };
            // Cfg-exclusive copies of one class: the block written beside one
            // copy belongs to that copy, not to the first `locate` saw; a block
            // written *outside* both — an unconditional `impl api::Gauge` at
            // the root — applies to whichever copy rustc compiles, so it
            // attaches to every one (#357 review). Same rule as
            // `CrateScan::cfg_variants_for`.
            let targets = cfg_variants_of(&self.classes, index, &imp.enclosing_cfg);
            for index in targets {
                let owner_skip = self.classes[index].entry.skip_reason.clone();
                // The symbol is the *class's*: an `impl a::C` written elsewhere
                // still wraps `a::C`'s methods (#300).
                let class_path = self.classes[index].module_path.clone();
                for func in &imp.funcs {
                    let returns_self = matches!(
                        &func.sig.output,
                        syn::ReturnType::Type(_, ty) if {
                            let value = py_result_ok_type(ty).unwrap_or_else(|| (**ty).clone());
                            returns_class(
                                &value, &self.classes[index], &imp.header.module_path,
                                &self.classes, &self.imports, self.edition_2015,
                            )
                        }
                    );
                    let class_ident =
                        syn::Ident::new(&self.classes[index].entry.name, imp.header.target.span());
                    let entry = method_entry(
                        &class_ident,
                        &class_path,
                        returns_self,
                        func,
                        &owner_skip,
                        &imp.cfg,
                    );
                    self.classes[index].entry.methods.push(entry);
                }
            }
        }

        manifest
            .structs
            .extend(self.classes.into_iter().map(|c| c.entry));
        // The Julia surface first: an item it refuses claims no symbol, so a
        // `fn User()` refused for the class `User`'s name does not also cost the
        // class its `String` getters' helper (#307 review).
        mark_julia_surface_collisions(manifest, &Emitted::new());
    }
}

/// The Julia surface a wrapper is bound to has a namespace of its own, apart
/// from the exported symbols, and the scan refuses what that namespace cannot
/// hold twice (`julia_name_collision:<earlier>`, #307 review):
///
/// * a class is a Julia type *and* its constructor function, so its name is
///   taken for every arity — a free function or a static method of that name
///   would redefine the constant (`function User()` before `mutable struct
///   User`) or graft an extra constructor onto the type. Classes claim first;
/// * a `#[staticmethod]` becomes a module-level Julia function named after the
///   method, with one untyped argument per Rust argument (`crate_bindings.jl`),
///   exactly as a `#[pyfunction]` does, so two classes with a `parse(s)` each,
///   or a class `parse(s)` next to a free `parse(s)`, define one Julia method
///   twice and the later silently replaces the earlier. Free functions claim
///   their name and arity next (a caller writing `parse(s)` means the free
///   function), then classes in manifest order.
///
/// The Rust symbols of all of these are distinct — class-qualified, or in
/// different modules — so [`mark_symbol_collisions`] lets them through.
/// Constructors are named after their class and instance methods dispatch on
/// `self::Class`; neither can collide this way.
fn mark_julia_surface_collisions(manifest: &mut Manifest, emitted: &Emitted) {
    // Resolve the user-facing owners before reserving their implementation
    // types. An owner skipped by a class or an earlier method emits no ABI
    // aggregate and must not take a valid class's name with it.
    let owners = manifest.clone();
    mark_julia_surface_collisions_pass(manifest, None);
    mark_symbol_collisions(manifest, emitted);
    loop {
        let mut next = owners.clone();
        mark_julia_surface_collisions_pass(&mut next, Some(manifest));
        // A symbol winner may have just lost its Julia name to an aggregate.
        // Re-evaluate symbol ownership too, so it cannot leave permanent
        // tombstones on methods that are now safe to emit.
        mark_symbol_collisions(&mut next, emitted);
        if next == *manifest {
            return;
        }
        *manifest = next;
    }
}

fn mark_julia_surface_collisions_pass(
    manifest: &mut Manifest,
    aggregate_owners: Option<&Manifest>,
) {
    // A linker-excluded entry emits no Julia binding either. It remains a
    // candidate in the fresh pass, but must not reserve a surface name until
    // symbol ownership allows it again. Indexing is stable across the clones.
    let symbol_excluded = |reason: &str| {
        reason.starts_with("symbol_collision:")
            || reason.starts_with("owner_skipped:symbol_collision:")
    };
    // The Julia surface is one namespace *per generated module*, and the
    // bindings lay one Julia module out per Rust module (#300), so every key
    // below carries the module path: `a::parse` and `b::parse` live in
    // `bindings.a` and `bindings.b` and never meet.
    // (module path, name) -> qualified owner; (module path, name, arity) -> owner.
    // Every key also carries the claimant's `#[cfg]`: cfg-exclusive copies of
    // one fragment name the same things and rustc never compiles them
    // together, so they do not take the name from each other (#357 review).
    type Scoped = (Vec<String>, String);
    type ScopedArity = (Vec<String>, String, usize);
    let class_names: Vec<(Scoped, String, String)> = manifest
        .structs
        .iter()
        .enumerate()
        .filter(|(i, s)| {
            s.attribute.is_pyo3_scan()
                && s.skip_reason.is_empty()
                && aggregate_owners
                    .is_none_or(|previous| !symbol_excluded(&previous.structs[*i].skip_reason))
        })
        .map(|(_, s)| {
            (
                (s.module_path.clone(), s.name.clone()),
                qualified(&s.module_path, &s.name),
                s.cfg.clone(),
            )
        })
        .collect();
    // Result/Option wrappers are Julia types in the same generated module as
    // their owner (`CResult_<fn>`, `COption_<fn>`). Reserve those names before
    // laying out classes and functions: otherwise a user-defined `pyclass`
    // with one of those names would redefine the aggregate after the wrapper
    // emitter had already declared it (#303).
    // Recompute from surviving owners, not from the manifest being marked:
    // excluding CResult_f also removes COption_CResult_f_method. Starting
    // each pass from the original candidates restores classes blocked only
    // by a now-absent aggregate. Symbol-excluded owners likewise release
    // their surface claims, allowing previously blocked candidates back in.
    let mut aggregate_names: Vec<(Scoped, String, String)> = aggregate_owners
        .into_iter()
        .flat_map(|owners| &owners.functions)
        .filter(|f| f.attribute.is_pyo3_scan() && f.skip_reason.is_empty())
        .filter(|f| {
            matches!(
                f.return_kind,
                ReturnKind::PyResult | ReturnKind::Result | ReturnKind::Option
            )
        })
        .flat_map(|f| {
            let prefix = if f.return_kind == ReturnKind::Option {
                "COption"
            } else {
                "CResult"
            };
            let defaults = f
                .args
                .iter()
                .rev()
                .take_while(|arg| !arg.python_default.is_empty())
                .count();
            (0..=defaults).map(move |omitted| {
                let owner = crate::claims::default_arity_name(&f.ffi_name, omitted);
                let name = format!("{prefix}_{owner}");
                ((f.module_path.clone(), name.clone()), name, f.cfg.clone())
            })
        })
        .collect();
    for s in aggregate_owners
        .into_iter()
        .flat_map(|owners| &owners.structs)
        .filter(|s| s.attribute.is_pyo3_scan() && s.skip_reason.is_empty())
    {
        for m in s.methods.iter().filter(|m| {
            m.skip_reason.is_empty()
                && matches!(
                    m.return_kind,
                    ReturnKind::PyResult | ReturnKind::Result | ReturnKind::Option
                )
        }) {
            let prefix = if m.return_kind == ReturnKind::Option {
                "COption"
            } else {
                "CResult"
            };
            let owner = crate::codegen::method_string_owner(&s.ffi_name, &m.name);
            let defaults = m
                .args
                .iter()
                .rev()
                .take_while(|arg| !arg.python_default.is_empty())
                .count();
            for omitted in 0..=defaults {
                let variant = crate::claims::default_arity_name(&owner, omitted);
                let name = format!("{prefix}_{variant}");
                aggregate_names.push(((s.module_path.clone(), name.clone()), name, m.cfg.clone()));
            }
        }
    }
    let aggregate_named = |path: &[String], name: &str, cfg: &str| {
        aggregate_names
            .iter()
            .find(|((p, n), _, c)| p == path && n == name && cfg_clash(c, cfg))
    };
    let class_named = |path: &[String], name: &str, cfg: &str| {
        class_names
            .iter()
            .find(|((p, n), _, c)| p == path && n == name && cfg_clash(c, cfg))
    };

    let mut taken: Vec<(ScopedArity, String, String)> = Vec::new();
    for (i, f) in manifest.functions.iter_mut().enumerate() {
        if !f.attribute.is_pyo3_scan() || !f.skip_reason.is_empty() {
            continue;
        }
        if let Some((_, class, _)) = class_named(&f.module_path, &f.name, &f.cfg) {
            f.skip_reason = skip_reason::detailed(skip_reason::JULIA_NAME_COLLISION, class);
            continue;
        }
        if let Some((_, aggregate, _)) = aggregate_named(&f.module_path, &f.name, &f.cfg) {
            f.skip_reason = skip_reason::detailed(skip_reason::JULIA_NAME_COLLISION, aggregate);
            continue;
        }
        if aggregate_owners
            .is_none_or(|previous| !symbol_excluded(&previous.functions[i].skip_reason))
        {
            taken.push((
                (f.module_path.clone(), f.name.clone(), f.args.len()),
                qualified(&f.module_path, &f.name),
                f.cfg.clone(),
            ));
        }
    }
    for (i, s) in manifest.structs.iter_mut().enumerate() {
        if !s.attribute.is_pyo3_scan() || !s.skip_reason.is_empty() {
            continue;
        }
        let owner = qualified(&s.module_path, &s.name);
        let s_cfg = s.cfg.clone();
        if let Some((_, aggregate, _)) = aggregate_named(&s.module_path, &s.name, &s_cfg) {
            let reason = skip_reason::detailed(skip_reason::JULIA_NAME_COLLISION, aggregate);
            s.skip_reason = reason.clone();
            for m in &mut s.methods {
                if m.skip_reason.is_empty() {
                    m.skip_reason = skip_reason::detailed(skip_reason::OWNER_SKIPPED, &reason);
                }
            }
            continue;
        }
        for (j, m) in s.methods.iter_mut().enumerate() {
            if !m.skip_reason.is_empty() || !m.is_static || m.is_constructor {
                continue;
            }
            if let Some((_, class, _)) = class_named(&s.module_path, &m.name, &m.cfg) {
                m.skip_reason = skip_reason::detailed(skip_reason::JULIA_NAME_COLLISION, class);
                continue;
            }
            if let Some((_, aggregate, _)) = aggregate_named(&s.module_path, &m.name, &m.cfg) {
                m.skip_reason = skip_reason::detailed(skip_reason::JULIA_NAME_COLLISION, aggregate);
                continue;
            }
            let key = (s.module_path.clone(), m.name.clone(), m.args.len());
            match taken
                .iter()
                .find(|(k, _, c)| *k == key && cfg_clash(c, &m.cfg))
            {
                Some((_, other, _)) => {
                    m.skip_reason = skip_reason::detailed(skip_reason::JULIA_NAME_COLLISION, other);
                }
                None => {
                    if aggregate_owners.is_none_or(|previous| {
                        !symbol_excluded(&previous.structs[i].methods[j].skip_reason)
                    }) {
                        taken.push((key, format!("{owner}::{}", m.name), m.cfg.clone()));
                    }
                }
            }
        }
    }
}

pub(crate) use crate::cfg::cfg_exclusive;

/// Whether two items whose symbols coincide really clash: they do unless their
/// predicates are provably exclusive — copies rustc never compiles together.
/// The same exemption `Manifest::symbol_owners` applies on the `#[julia]` side.
fn cfg_clash(a: &str, b: &str) -> bool {
    !cfg_exclusive(a, b)
}

/// What the generator actually emitted for an owner, keyed by its qualified
/// name (`module::item`, and `module::Class::method` for a method).
///
/// The symbol table below reserves every symbol an entry *will* emit, and it
/// runs in the scan — before `wrapper_crate` has had the chance to refuse that
/// entry for a signature it cannot lower. A reservation left standing for an
/// entry that then emits nothing costs a **valid** item its own name:
/// `foo(values = vec![])` reserves `rustcall_foo__default_1`, a function
/// literally named `foo__default_1` is marked a collision against it, and when
/// `foo` is then refused for its `Vec<i32>` argument neither one is emitted
/// (#392 review).
///
/// So `wrapper_crate` lowers, reports what came out, and asks for the analysis
/// again. An owner absent from the map is one nothing is known about yet —
/// the first pass, where the derived list stands as it always did.
///
/// **Only a refusal the *generator* made is a report.** An entry this analysis
/// refused emitted nothing for that very reason, and taking that as "the
/// generator refuses it" would drop its claim and hand the symbol to the item
/// it had just beaten — two `#[no_mangle]` definitions of one name in the
/// wrapper. Such an entry is left out of the map, so it claims everything
/// again and the analysis decides it afresh.
pub type Emitted = std::collections::HashMap<String, std::collections::HashSet<String>>;

/// The arities of `base` this owner emitted, as far as anything is known.
///
/// `None` means "no report yet, take every arity"; a report that lists none is
/// an owner the generator refused outright, and it claims nothing.
fn emitted_arities<'a>(
    emitted: &'a Emitted,
    owner: &str,
) -> Option<&'a std::collections::HashSet<String>> {
    emitted.get(owner)
}

/// Clear the skip reasons this analysis produces, so it can be run again from
/// the state the scan was in before it ran.
///
/// Only these three come from here. An `owner_skipped` is cleared exactly when
/// the owner's own reason (which it carries after the colon) is one of them —
/// a method whose class is not `pub` keeps its reason, a method whose class
/// lost a symbol does not.
fn from_collision(reason: &str) -> bool {
    let body = reason
        .strip_prefix(skip_reason::OWNER_SKIPPED)
        .and_then(|rest| rest.strip_prefix(':'))
        .unwrap_or(reason);
    body.starts_with(skip_reason::SYMBOL_COLLISION)
        || body.starts_with(skip_reason::JULIA_NAME_COLLISION)
}

fn clear_collision_reasons(manifest: &mut Manifest) {
    // Fields first, and unconditionally: a collision *erases* a field's
    // accessors rather than leaving a reason behind, so there is no skip reason
    // here to key the restoration on. `remember_accessors` recorded the
    // original at the first clearing, and restoring it is what lets the
    // analysis decide the field again once the entry that took its name turns
    // out to be refused (#392 review).
    for s in &mut manifest.structs {
        for f in &mut s.fields {
            f.restore_accessors();
        }
    }
    for f in &mut manifest.functions {
        if from_collision(&f.skip_reason) {
            f.skip_reason.clear();
        }
    }
    for s in &mut manifest.structs {
        if from_collision(&s.skip_reason) {
            s.skip_reason.clear();
        }
        for m in &mut s.methods {
            if from_collision(&m.skip_reason) {
                m.skip_reason.clear();
            }
        }
    }
}

/// Re-run the collision analysis knowing what the generator emitted.
///
/// Returns whether anything changed. `lowered` is the manifest `wrapper_crate`
/// produced; the owners it reports with an empty `skip_reason` are the ones
/// that really exist, and only those reserve a symbol here.
pub fn remark_collisions(manifest: &mut Manifest, lowered: &Manifest, known: &mut Emitted) -> bool {
    let mut emitted: Emitted = Emitted::new();
    for f in &lowered.functions {
        if !f.attribute.is_pyo3_scan() {
            continue;
        }
        if from_collision(&f.skip_reason) {
            continue;
        }
        let key = qualified(&f.module_path, &f.name);
        let slot = emitted.entry(key).or_default();
        if f.skip_reason.is_empty() {
            slot.insert(f.symbol.clone());
        }
    }
    for s in &lowered.structs {
        if !s.attribute.is_pyo3_scan() {
            continue;
        }
        let class = qualified(&s.module_path, &s.name);
        let class_refused_here = from_collision(&s.skip_reason);
        if !class_refused_here {
            let slot = emitted.entry(class.clone()).or_default();
            if s.skip_reason.is_empty() {
                slot.insert(s.ffi_name.clone());
            }
        }
        for m in &s.methods {
            if class_refused_here || from_collision(&m.skip_reason) {
                continue;
            }
            let slot = emitted.entry(format!("{class}::{}", m.name)).or_default();
            if s.skip_reason.is_empty() && m.skip_reason.is_empty() {
                slot.insert(m.symbol.clone());
            }
        }
    }
    // Accumulate, never replace. A generator refusal is a property of the entry
    // and does not change between passes, so once one is reported it holds for
    // good; a pass in which that entry happened not to be lowered simply says
    // nothing about it. Today such an entry cannot be skipped — claiming
    // nothing, it can neither win a symbol nor lose one, so it is lowered every
    // pass and re-reports — but that is an invariant of `mark_symbol_collisions`
    // rather than of this function, and relying on it silently is how the set
    // would start oscillating if it ever changed (#392 review).
    for (owner, symbols) in emitted {
        known.entry(owner).or_insert(symbols);
    }

    let before = manifest.clone();
    clear_collision_reasons(manifest);
    mark_julia_surface_collisions(manifest, known);
    *manifest != before
}

/// Flag every wrappable PyO3 entry whose exported symbol another one already
/// claims (#275).
///
/// Since #300 the symbol folds the module path in (`codegen::symbol_stem`:
/// `a::run` → `rustcall_a__run`), so two `pub fn run` in different modules no
/// longer meet. What is left is a coincidence the scheme cannot exclude — a
/// crate-root `fn a__run` next to `a::run`, or a `#[julia]` item and a PyO3
/// entry that land on one name — and a single wrapper crate cannot export a
/// symbol twice. The scan reports the clash (`skip_reason::SYMBOL_COLLISION`)
/// rather than emitting a manifest that cannot be built.
///
/// The first entry in manifest order keeps the symbol so the outcome does not
/// depend on which file was visited first.
fn mark_symbol_collisions(manifest: &mut Manifest, emitted: &Emitted) {
    // One table for every exported symbol of the whole manifest, whatever
    // produces it: a `#[julia]` function's wrapper, a `#[julia]` struct's
    // method and accessor wrappers, and the PyO3 entries the scan just added.
    // They all live in one `cdylib`, so `rustcall_C_f` from a `#[julia]`
    // `impl C { fn f }` and from a `#[pyclass] C` with `#[pymethods] fn f`
    // are the same symbol even though nothing else about them matches.
    let mut taken: Vec<(String, String, String)> = Vec::new();

    // Items already exported by a RustCall attribute own their symbols
    // outright: a PyO3 entry that wants one is the loser whatever the order.
    // What a RustCall-attributed entry claims is derived in exactly one place
    // (`crate::claims`, #338), so a name added to the codegen cannot be
    // remembered here and forgotten by the `#[julia]` duplicate check, or the
    // other way round.
    for f in manifest
        .functions
        .iter()
        .filter(|f| !f.attribute.is_pyo3_scan())
    {
        for claim in crate::claims::function_claims(f, crate::claims::Policy::PYO3_SCAN) {
            taken.push((
                claim.name,
                qualified(&f.module_path, &f.name),
                f.cfg.clone(),
            ));
        }
    }
    for s in manifest
        .structs
        .iter()
        .filter(|s| !s.attribute.is_pyo3_scan())
    {
        for claim in crate::claims::struct_claims(s, crate::claims::Policy::PYO3_SCAN) {
            taken.push((
                claim.name,
                qualified(&s.module_path, &s.name),
                s.cfg.clone(),
            ));
        }
    }

    let mut order: Vec<usize> = (0..manifest.functions.len()).collect();
    order.sort_by(|&a, &b| {
        let (x, y) = (&manifest.functions[a], &manifest.functions[b]);
        x.module_path
            .cmp(&y.module_path)
            .then(x.line.cmp(&y.line))
            .then(x.name.cmp(&y.name))
    });

    for i in order {
        let f = &manifest.functions[i];
        if !f.attribute.is_pyo3_scan() || !f.skip_reason.is_empty() || f.symbol.is_empty() {
            continue;
        }
        let strings =
            declares_string_helpers(&f.return_type, &f.ok_type, &f.err_type, &f.inner_type);
        // Every arity the wrapper crate will emit for this entry, not just the
        // full one. A defaulted `foo(value = 1)` also defines
        // `rustcall_foo__default_1` and its panic and string helpers, so a root
        // function named `foo__default_1` is a collision — and reserving only
        // `rustcall_foo` let both items through to emit the same symbol (#370).
        let actual = emitted_arities(emitted, &qualified(&f.module_path, &f.name));
        let mut symbols = Vec::new();
        for omitted in 0..=crate::claims::trailing_default_count(&f.args) {
            let arity = crate::claims::default_arity_name(&f.symbol, omitted);
            // An arity the generator did not emit reserves nothing: there is no
            // such symbol in the wrapper for anything to collide with.
            if actual.is_some_and(|a| !a.contains(&arity)) {
                continue;
            }
            symbols.extend(wrapper_symbols(&arity));
            if strings {
                symbols.extend(string_helper_symbols(&crate::claims::default_arity_name(
                    &f.ffi_name,
                    omitted,
                )));
            }
        }
        if symbols.is_empty() {
            // Emits nothing at all, so it can neither take a name nor lose one.
            continue;
        }
        let f_cfg = f.cfg.clone();
        if let Some((_, owner, _)) = taken
            .iter()
            .find(|(s, _, c)| symbols.contains(s) && cfg_clash(c, &f_cfg))
        {
            let owner = owner.clone();
            manifest.functions[i].skip_reason =
                skip_reason::detailed(skip_reason::SYMBOL_COLLISION, &owner);
        } else {
            let owner = qualified(&f.module_path, &f.name);
            for symbol in symbols {
                taken.push((symbol, owner.clone(), f_cfg.clone()));
            }
        }
    }

    // A class whose *FFI name* another struct entry already claimed collides
    // on every one of its symbols at once, so there the class is the unit.
    // Since #300 the FFI name carries the module path, so this is a same-module
    // clash (or a crate-root name spelling a qualified one). Any other clash —
    // with a free function, or between a class's own method and one of its
    // field accessors — is reported on the individual entry, which leaves the
    // rest of the class wrappable.
    let mut class_names: Vec<(String, String, String)> = manifest
        .structs
        .iter()
        .filter(|s| !s.attribute.is_pyo3_scan())
        .map(|s| {
            (
                s.ffi_name.clone(),
                qualified(&s.module_path, &s.name),
                s.cfg.clone(),
            )
        })
        .collect();

    let mut struct_order: Vec<usize> = (0..manifest.structs.len()).collect();
    struct_order.sort_by(|&a, &b| {
        let (x, y) = (&manifest.structs[a], &manifest.structs[b]);
        x.module_path
            .cmp(&y.module_path)
            .then(x.line.cmp(&y.line))
            .then(x.name.cmp(&y.name))
    });
    for i in struct_order {
        let s = &manifest.structs[i];
        if !s.attribute.is_pyo3_scan() || !s.skip_reason.is_empty() {
            continue;
        }
        let class_key = qualified(&s.module_path, &s.name);
        if emitted_arities(emitted, &class_key).is_some_and(|a| a.is_empty()) {
            // The generator refused the whole class, so there is no handle
            // type and no destructor; its methods are handled with it.
            continue;
        }
        let name = s.ffi_name.clone();
        let s_cfg = s.cfg.clone();
        let free = crate::codegen::struct_free_symbol(&s.ffi_name);
        let free_symbols = [crate::codegen::panic_symbol(&free), free];

        let class_conflict = class_names
            .iter()
            .find(|(n, _, c)| *n == name && cfg_clash(c, &s_cfg))
            .or_else(|| {
                taken.iter().find(|(symbol, _, cfg)| {
                    free_symbols.contains(symbol) && cfg_clash(cfg, &s_cfg)
                })
            });
        if let Some((_, owner, _)) = class_conflict {
            let reason = skip_reason::detailed(skip_reason::SYMBOL_COLLISION, owner);
            let s = &mut manifest.structs[i];
            s.skip_reason = reason.clone();
            for m in &mut s.methods {
                if m.skip_reason.is_empty() {
                    m.skip_reason = skip_reason::detailed(skip_reason::OWNER_SKIPPED, &reason);
                }
            }
            for f in &mut s.fields {
                f.remember_accessors();
                f.ffi_compatible = false;
                f.getter.clear();
                f.setter.clear();
                f.free_symbol.clear();
            }
            continue;
        }

        // Methods claim their symbols before field accessors do, so a
        // `#[setter(x)] fn set_x` and a `#[pyo3(set)] x` — which both want
        // `rustcall_C_set_x` — leave the method wrappable and drop the
        // accessor, rather than taking the whole class down.
        let owner = qualified(&s.module_path, &s.name);
        for symbol in free_symbols {
            taken.push((symbol, owner.clone(), s_cfg.clone()));
        }
        let s = &mut manifest.structs[i];
        let class_name = s.ffi_name.clone();
        let method_class = class_key;
        for m in &mut s.methods {
            if !m.skip_reason.is_empty() || m.symbol.is_empty() {
                continue;
            }
            // Every arity, exactly as for free functions above: a defaulted
            // method also defines `rustcall_C_foo__default_1` and its helpers,
            // so a method literally named `foo__default_1` in the same class is
            // a collision (#370, #392 review).
            let strings =
                declares_string_helpers(&m.return_type, &m.ok_type, &m.err_type, &m.inner_type);
            let string_owner = format!("{}_{}", class_name, m.name);
            let actual = emitted_arities(emitted, &format!("{method_class}::{}", m.name));
            let mut symbols = Vec::new();
            for omitted in 0..=crate::claims::trailing_default_count(&m.args) {
                let arity = crate::claims::default_arity_name(&m.symbol, omitted);
                if actual.is_some_and(|a| !a.contains(&arity)) {
                    continue;
                }
                symbols.extend(wrapper_symbols(&arity));
                if strings {
                    symbols.extend(string_helper_symbols(&crate::claims::default_arity_name(
                        &string_owner,
                        omitted,
                    )));
                }
            }
            if symbols.is_empty() {
                continue;
            }
            match taken
                .iter()
                .find(|(t, _, c)| symbols.contains(t) && cfg_clash(c, &s_cfg))
            {
                Some((_, other, _)) => {
                    m.skip_reason = skip_reason::detailed(skip_reason::SYMBOL_COLLISION, other);
                }
                None => {
                    for symbol in symbols {
                        taken.push((symbol, owner.clone(), s_cfg.clone()));
                    }
                }
            }
        }
        // The struct-level owned-string helper every `String` field getter of
        // the class shares (`<Class>_RustCallOwnedString`, `class_wrappers`)
        // is a declaration like any other: when another item already made it,
        // the `String` getters are what give way, and the rest of the class
        // stays wrappable (#307 review). A `#[pyfunction] fn User() -> String`
        // next to a `#[pyclass] User` used to be the case; it is now refused on
        // the Julia surface first (`mark_julia_surface_collisions`), so what
        // reaches this is an item whose own symbols spell a helper's name.
        if s.fields
            .iter()
            .any(|f| !f.getter.is_empty() && is_string_spelling(&f.rust_type))
        {
            let helpers = string_helper_symbols(&class_name);
            if taken
                .iter()
                .any(|(t, _, c)| helpers.contains(t) && cfg_clash(c, &s_cfg))
            {
                for f in &mut s.fields {
                    if is_string_spelling(&f.rust_type) {
                        f.remember_accessors();
                        f.ffi_compatible = false;
                        f.getter.clear();
                        f.setter.clear();
                        f.free_symbol.clear();
                    }
                }
            } else {
                for symbol in helpers {
                    taken.push((symbol, owner.clone(), s_cfg.clone()));
                }
            }
        }
        for f in &mut s.fields {
            if !f.getter.is_empty() {
                let mut symbols = vec![f.getter.clone(), crate::codegen::panic_symbol(&f.getter)];
                if f.abi == "vec" {
                    symbols.push(format!("{}_RustCallOwnedVec", f.getter));
                    symbols.push(f.free_symbol.clone());
                }
                if taken
                    .iter()
                    .any(|(t, _, c)| symbols.contains(t) && cfg_clash(c, &s_cfg))
                {
                    f.remember_accessors();
                    f.getter.clear();
                    f.free_symbol.clear();
                } else {
                    for symbol in symbols {
                        taken.push((symbol, owner.clone(), s_cfg.clone()));
                    }
                }
            }
            if !f.setter.is_empty() {
                let symbols = [f.setter.clone(), crate::codegen::panic_symbol(&f.setter)];
                if taken
                    .iter()
                    .any(|(t, _, c)| symbols.contains(t) && cfg_clash(c, &s_cfg))
                {
                    f.remember_accessors();
                    f.setter.clear();
                } else {
                    for symbol in symbols {
                        taken.push((symbol, owner.clone(), s_cfg.clone()));
                    }
                }
            }
            if f.getter.is_empty() && f.setter.is_empty() {
                f.ffi_compatible = false;
            }
        }
        class_names.push((name, owner, s_cfg));
    }
}

/// The string helpers a wrapper declares next to an item whose result — or
/// `Result` / `Option` payload — is a string: `<owner>_RustCallOwnedString` and
/// `<owner>_free_rust_string` for an owned buffer, `<owner>_RustCallBorrowedString`
/// for a view. The owner is the function's name, the class's name (for its
/// field getters) or `<Class>_<method>`, so two items with one owner declare
/// the same helpers twice and the generated crate does not compile; all three
/// names are reserved whenever the item may declare any of them (#307 review).
fn string_helper_symbols(owner: &str) -> [String; 3] {
    let [owned, free] = crate::claims::owned_string_names(owner);
    [owned, free, crate::claims::borrowed_string_name(owner)]
}

/// Whether a wrapper for an item with these manifest types declares a string
/// helper: a `String` / `&str` result, or such a payload.
fn declares_string_helpers(
    return_type: &str,
    ok_type: &str,
    err_type: &str,
    inner_type: &str,
) -> bool {
    [return_type, ok_type, err_type, inner_type]
        .iter()
        .any(|spelling| is_string_spelling(spelling))
}

fn is_string_spelling(spelling: &str) -> bool {
    !spelling.is_empty()
        && syn::parse_str::<Type>(spelling)
            .map(|ty| is_string_type(&ty) || is_str_ref_type(&ty))
            .unwrap_or(false)
}

/// Every name a wrapper derives from one manifest symbol: the entry point
/// itself, its panic-channel reader (`<symbol>_take_panic`,
/// `codegen::PANIC_SYMBOL_SUFFIX`), and the private thread-local slot that
/// reader drains — `__RUSTCALL_PANIC_<SYMBOL>`, the symbol upper-cased, which
/// `foo` and `FOO` would therefore share. A clash on any of them is a duplicate
/// definition in the generated crate — `foo` and `foo_take_panic` as two
/// `#[pyfunction]`s both want `rustcall_foo_take_panic` — so all are reserved
/// and all are checked (#307 review). A struct helper — destructor, `clone`,
/// field accessor — exports a reader too, but drains a hex-encoded slot
/// (`claims::helper_panic_slot`) that cannot collide, so it reserves only its
/// entry point and reader (`claims::helper_claims`).
fn wrapper_symbols(symbol: &str) -> [String; 3] {
    let claims = crate::claims::wrapper_claims(symbol);
    let mut names = claims.into_iter().map(|c| c.name);
    [
        names.next().expect("entry point"),
        names.next().expect("panic reader"),
        names.next().expect("panic slot"),
    ]
}

fn merge_route_cfg(
    cfg: &mut String,
    features: &mut Vec<String>,
    route: &crate::public_routes::PublicRoute,
) {
    let mut predicates = route.predicates.clone();
    if !cfg.is_empty() {
        predicates.insert(cfg.clone());
    }
    if predicates.is_empty() {
        return;
    }
    *cfg = if predicates.len() == 1 {
        predicates.into_iter().next().unwrap()
    } else {
        format!(
            "all({})",
            predicates.into_iter().collect::<Vec<_>>().join(",")
        )
    };
    let meta: syn::Meta =
        syn::parse_str(&format!("cfg({cfg})")).expect("cfg predicates came from syn");
    let attrs: Vec<syn::Attribute> = vec![syn::parse_quote!(#[#meta])];
    *features = crate::cfg::predicate_features(&attrs);
}

fn qualified(module_path: &[String], name: &str) -> String {
    if module_path.is_empty() {
        name.to_string()
    } else {
        format!("{}::{}", module_path.join("::"), name)
    }
}

/// The `#[path = "..."]` override of a `mod` declaration, if it has one.
fn path_attribute(attrs: &[syn::Attribute]) -> Option<String> {
    for attr in attrs {
        if !attr.path().is_ident("path") {
            continue;
        }
        if let syn::Meta::NameValue(nv) = &attr.meta {
            if let syn::Expr::Lit(lit) = &nv.value {
                if let syn::Lit::Str(s) = &lit.lit {
                    return Some(s.value());
                }
            }
        }
    }
    None
}

/// The Python-visible name of a declarative `#[pymodule] mod`: `name = "..."`
/// when given, otherwise the Rust module's own name (#424).
fn _pyo3_module_python_name(attrs: &[syn::Attribute], ident: &syn::Ident) -> String {
    let named = pyo3_name(attrs);
    if named.is_empty() {
        ident.to_string()
    } else {
        named
    }
}

/// Manifest entry of a declarative `#[pymodule] mod name { ... }` (#424). Only
/// its name matters to a consumer: it is what CPython imports, so the host path
/// reads it with [`function_entry`]'s `PyModule` origin. Its items carry their
/// own [`Function::python_path`] / [`Struct::python_path`].
fn module_entry(
    item: &syn::ItemMod,
    module_path: &[String],
    enclosing_cfg: &[syn::Attribute],
) -> Function {
    let name = item.ident.to_string();
    let effective_cfg = crate::cfg::effective_cfg_attrs(enclosing_cfg, &item.attrs);
    Function {
        name: name.clone(),
        ffi_name: crate::codegen::symbol_stem(module_path, &name),
        symbol: crate::codegen::function_symbol(module_path, &name),
        attribute: Attribute::PyModule,
        vis: visibility_string(&item.vis),
        skip_reason: skip_reason::PYMODULE.to_string(),
        python_name: pyo3_name(&item.attrs),
        python_path: Vec::new(),
        exported: false,
        cfg: predicate_string(&effective_cfg),
        cfg_features: crate::cfg::predicate_features(&effective_cfg),
        is_generic: false,
        type_params: Vec::new(),
        args: Vec::new(),
        return_type: String::new(),
        return_kind: ReturnKind::Unit,
        return_abi: String::new(),
        ok_type: String::new(),
        err_type: String::new(),
        inner_type: String::new(),
        ok_abi: String::new(),
        err_abi: String::new(),
        inner_abi: String::new(),
        has_owned_string_helper: false,
        has_borrowed_string_helper: false,
        source: String::new(),
        body_has_cfg: false,
        line: item.span().start().line,
        module_path: module_path.to_vec(),
        callable_path: Vec::new(),
    }
}

/// Manifest entry of a `#[pyfunction]` (or a `#[pymodule]` initialiser).
fn function_entry(
    func: &ItemFn,
    attribute: Attribute,
    reachable: bool,
    module_path: &[String],
    python_path: &[String],
    enclosing_cfg: &[syn::Attribute],
) -> Function {
    let name = func.sig.ident.to_string();
    let effective_cfg = crate::cfg::effective_cfg_attrs(enclosing_cfg, &func.attrs);
    let is_generic = has_type_params(&func.sig.generics) || has_impl_trait(&func.sig);
    let return_type = return_type_to_string(&func.sig.output);

    let (return_kind, ok_type, err_type, inner_type) = return_shape(&func.sig.output);

    let reason = if attribute == Attribute::PyModule {
        skip_reason::PYMODULE.to_string()
    } else {
        item_skip_reason(&func.vis, reachable, is_generic)
            .unwrap_or_else(|| signature_skip_reason(&func.sig, return_kind).unwrap_or_default())
    };

    Function {
        name: name.clone(),
        ffi_name: crate::codegen::symbol_stem(module_path, &name),
        // What Phase 2 will export, not what exists today.
        symbol: crate::codegen::function_symbol(module_path, &name),
        attribute,
        vis: visibility_string(&func.vis),
        skip_reason: reason,
        python_name: pyo3_name(&func.attrs),
        python_path: python_path.to_vec(),
        exported: false,
        cfg: predicate_string(&effective_cfg),
        cfg_features: crate::cfg::predicate_features(&effective_cfg),
        is_generic,
        type_params: generics_to_type_params(&func.sig.generics),
        args: pyo3_args(&func.sig, &func.attrs),
        return_type,
        return_kind,
        // `Arg::abi` follows from the argument type alone, so it is filled in
        // as usual; the *return* lowering depends on how Phase 2 chooses to
        // wrap a `PyResult`, so it stays empty until a wrapper exists.
        return_abi: String::new(),
        ok_type,
        err_type,
        inner_type,
        // Same reason: no wrapper exists yet, so no payload lowering (#268).
        ok_abi: String::new(),
        err_abi: String::new(),
        inner_abi: String::new(),
        has_owned_string_helper: false,
        has_borrowed_string_helper: false,
        source: String::new(),
        body_has_cfg: crate::cfg::body_has_cfg(&func.block),
        line: func.span().start().line,
        module_path: module_path.to_vec(),
        callable_path: Vec::new(),
    }
}

/// Manifest entry of a `#[pyclass]` struct: an opaque handle. A `#[pyclass]` is
/// never `#[repr(C)]` (pyo3 owns its layout), so fields are only reachable
/// through the accessors pyo3 itself declares with `#[pyo3(get, set)]`.
fn class_entry(
    item: &ItemStruct,
    reachable: bool,
    module_path: &[String],
    python_path: &[String],
    enclosing_cfg: &[syn::Attribute],
) -> Struct {
    let effective_cfg = crate::cfg::effective_cfg_attrs(enclosing_cfg, &item.attrs);
    let is_generic = has_type_params(&item.generics);
    let reason = item_skip_reason(&item.vis, reachable, is_generic).unwrap_or_default();
    let name = item.ident.to_string();
    let stem = crate::codegen::symbol_stem(module_path, &name);

    // `#[pyclass(get_all, set_all)]` exposes every field without a per-field
    // attribute, and `frozen` takes every setter away. The dual-binding shape
    // `docs/src/pyo3.md` recommends is exactly that, so a scan that read only
    // field attributes would drop those fields.
    let options = crate::attrs::pyo3_class_options(&item.attrs);
    let mut pyo3_options = Vec::new();
    if options.subclass {
        pyo3_options.push("subclass".to_string());
    }
    if options.dict {
        pyo3_options.push("dict".to_string());
    }
    if options.weakref {
        pyo3_options.push("weakref".to_string());
    }

    let mut fields = Vec::new();
    if let syn::Fields::Named(named) = &item.fields {
        for f in &named.named {
            let Some(ident) = f.ident.clone() else {
                continue;
            };
            let mut access = pyo3_field_access(&f.attrs);
            access.get |= options.get_all;
            access.set |= options.set_all;
            access.set &= !options.frozen;
            if !access.get && !access.set {
                continue;
            }
            // A `#[pyo3(get)]` on a private field still gives Python a
            // descriptor — pyo3 generates it inside the crate — but a wrapper
            // crate compiled outside cannot read `Struct::field` (E0603), so
            // only a `pub` field gets accessors. A skipped class has no handle
            // type, so its fields have none either, whatever their visibility.
            let field_is_public = matches!(f.vis, syn::Visibility::Public(_));
            let vec_element = crate::types::pyo3_vec_element_type(&f.ty);
            // A `String` field crosses as the owned-string ABI. A `Vec<T>` is
            // available when T has a concrete Julia FFI scalar representation;
            // its element and allocator-matched release export are manifest
            // data rather than guesses made by the consumer (#303).
            let usable = reason.is_empty()
                && field_is_public
                && pyo3_type_in(&f.ty).is_none()
                && (is_ffi_compatible_type(&f.ty)
                    || is_string_type(&f.ty)
                    || vec_element.is_some());
            let getter = if usable && access.get {
                crate::codegen::method_symbol_of(&stem, &format!("get_{ident}"))
            } else {
                String::new()
            };
            let vec_free = if vec_element.is_some() && !getter.is_empty() {
                format!("{getter}_free_rust_vec")
            } else {
                String::new()
            };
            fields.push(Field {
                name: ident.to_string(),
                rust_type: type_to_string(&f.ty),
                abi: if vec_element.is_some() {
                    "vec".to_string()
                } else {
                    crate::codegen::field_abi(&f.ty).to_string()
                },
                vec_element: vec_element.as_ref().map(type_to_string).unwrap_or_default(),
                free_symbol: vec_free,
                ffi_compatible: usable,
                getter,
                setter: if usable && access.set {
                    crate::codegen::method_symbol_of(&stem, &format!("set_{ident}"))
                } else {
                    String::new()
                },
                python_name: access.python_name,
                vis: visibility_string(&f.vis),
                // What PyO3 exposes through the object, independent of the
                // `ffi_compatible` decision a wrapper crate needs (#424).
                pyo3_get: access.get,
                pyo3_set: access.set,
                // Whatever `#[cfg]` survived pruning is one the scan could not
                // decide; the generator refuses the accessors under a lenient
                // scan (#307 review).
                cfg: predicate_string(&f.attrs),
                precollision: None,
            });
        }
    }

    Struct {
        name: name.clone(),
        ffi_name: stem,
        attribute: Attribute::PyClass,
        vis: visibility_string(&item.vis),
        skip_reason: reason,
        python_name: pyo3_name(&item.attrs),
        python_path: python_path.to_vec(),
        pyo3_extends: options.extends,
        pyo3_options,
        python_owned_handle: false,
        cfg: predicate_string(&effective_cfg),
        cfg_features: crate::cfg::predicate_features(&effective_cfg),
        type_params: generics_to_type_params(&item.generics),
        fields,
        methods: Vec::new(),
        derives: crate::attrs::derive_list(&item.attrs),
        has_clone: false,
        has_owned_string_helper: false,
        has_borrowed_string_helper: false,
        context_source: String::new(),
        generic_wrappers: Vec::new(),
        line: item.span().start().line,
        module_path: module_path.to_vec(),
        callable_path: Vec::new(),
    }
}

/// Manifest entry of one method of a `#[pymethods]` block.
fn method_entry(
    struct_ident: &syn::Ident,
    class_path: &[String],
    returns_self: bool,
    func: &ImplItemFn,
    owner_skip: &str,
    enclosing_cfg: &[syn::Attribute],
) -> Method {
    // The block's and its modules' predicates gate the method as much as its
    // own do (#300 review).
    let effective_cfg = crate::cfg::effective_cfg_attrs(enclosing_cfg, &func.attrs);
    let struct_stem = crate::codegen::symbol_stem(class_path, &struct_ident.to_string());
    let markers = pyo3_method_markers(&func.attrs);
    let has = |m: Pyo3MethodMarker| markers.contains(&m);
    let receiver = func.sig.inputs.iter().find_map(|a| match a {
        FnArg::Receiver(r) => Some(r),
        _ => None,
    });
    let name = func.sig.ident.to_string();
    let is_constructor = has(Pyo3MethodMarker::New);
    let accessor = if has(Pyo3MethodMarker::Getter) {
        "getter"
    } else if has(Pyo3MethodMarker::Setter) {
        "setter"
    } else {
        ""
    };

    let is_generic = has_type_params(&func.sig.generics) || has_impl_trait(&func.sig);
    // The same structured description a free function gets, so a Phase-2
    // wrapper can lower a `PyResult` method without re-reading the Rust type
    // spelling (Rust syntax is parsed only here, #264).
    let (return_kind, ok_type, err_type, inner_type) = return_shape(&func.sig.output);
    let reason = if !owner_skip.is_empty() {
        skip_reason::detailed(skip_reason::OWNER_SKIPPED, owner_skip)
    } else {
        item_skip_reason(&func.vis, true, is_generic)
            .unwrap_or_else(|| signature_skip_reason(&func.sig, return_kind).unwrap_or_default())
    };

    Method {
        trait_path: String::new(),
        name: name.clone(),
        symbol: crate::codegen::method_symbol_of(&struct_stem, &name),
        // `#[staticmethod]` and `#[classmethod]` are both static from the C
        // side: neither takes a `self` receiver. A `#[classmethod]` takes a
        // `&Bound<'_, PyType>` first argument instead, so it is normally
        // skipped for using a pyo3 type.
        is_static: receiver.is_none(),
        is_mutable: receiver.map(|r| r.mutability.is_some()).unwrap_or(false),
        is_constructor,
        is_classmethod: has(Pyo3MethodMarker::ClassMethod),
        vis: visibility_string(&func.vis),
        skip_reason: reason,
        python_name: pyo3_name(&func.attrs),
        accessor: accessor.to_string(),
        attribute: Attribute::PyMethods,
        // A scanned `#[pymethods]` method has no wrapper and so no string
        // buffers; the Phase-2 wrapper crate names the ones it declares when
        // it generates them (`crate::wrap::method_wrapper`).
        string_owner: String::new(),
        return_kind,
        ok_type,
        err_type,
        inner_type,
        // No `#[julia]` wrapper exists for a scanned PyO3 item, so no payload
        // lowering (#268).
        ok_abi: String::new(),
        err_abi: String::new(),
        inner_abi: String::new(),
        // Only a return payload that resolves to `Self` / the class is an
        // opaque handle this wrapper may box (`#[pyclass]` is never `repr(C)`).
        // The `#[new]` marker alone is insufficient: an inheritance
        // constructor returns `(Self, Base)` (possibly inside `PyResult`),
        // which cannot inhabit a `*mut Self` success slot (#303).
        returns_boxed_struct: returns_self,
        args: pyo3_args(&func.sig, &func.attrs),
        return_type: return_type_to_string(&func.sig.output),
        return_abi: String::new(),
        generic_wrapper: String::new(),
        generic_wrapper_name: String::new(),
        cfg: predicate_string(&effective_cfg),
    }
}

/// Rust arguments annotated with the Python call shape PyO3 generated.
/// Parsing failures are left to rustc/PyO3 (the target crate cannot build),
/// while a valid signature is matched by parameter name so the receiver —
/// absent from `fn_args` — never shifts the metadata onto another argument.
fn pyo3_args(sig: &syn::Signature, attrs: &[syn::Attribute]) -> Vec<crate::manifest::Arg> {
    let mut args = fn_args(sig);
    let Ok(Some(parameters)) = pyo3_signature(attrs) else {
        return args;
    };
    for parameter in parameters {
        let Some(arg) = args.iter_mut().find(|arg| arg.name == parameter.name) else {
            continue;
        };
        arg.python_default = parameter.default;
        arg.python_kind = match parameter.kind {
            Pyo3ParameterKind::PositionalOnly => "positional_only",
            Pyo3ParameterKind::PositionalOrKeyword => "positional_or_keyword",
            Pyo3ParameterKind::KeywordOnly => "keyword_only",
            Pyo3ParameterKind::VarArgs => "var_args",
            Pyo3ParameterKind::KwArgs => "kw_args",
        }
        .to_string();
    }
    args
}

/// Whether a method's return type names the class itself: `Self`, the bare
/// class name, or a `crate::` / `self::` / `super::`-anchored path ending in
/// it. A path anchored elsewhere — `std::string::String` on a `#[pyclass]
/// struct String` — is some other type that happens to share the last
/// segment, and boxing it as the class would not compile (#307 review).
/// `codegen::returns_boxed_struct`'s last-segment rule stays with the
/// `#[julia]` path, whose items live in the crate that defines the struct.
fn returns_class(
    ty: &Type,
    class: &ScannedClass,
    impl_path: &[String],
    classes: &[ScannedClass],
    imports: &[ScannedImport],
    edition_2015: bool,
) -> bool {
    let Type::Path(path) = unparen(ty) else {
        return false;
    };
    if path.qself.is_some() {
        return false;
    }
    // A leading `::` names the current crate only in edition 2015. In newer
    // editions it starts in the extern prelude, which this scanner cannot
    // resolve and must not confuse with a same-named local module.
    if path.path.leading_colon.is_some() && !edition_2015 {
        return false;
    }
    if path.path.is_ident("Self") {
        return true;
    }
    let Some(target) = path.path.segments.last() else {
        return false;
    };
    let mut qualifier = crate::paths::type_path_qualifier(ty);
    if path.path.leading_colon.is_some() {
        qualifier.anchor = crate::paths::PathAnchor::Crate;
    }
    let header = ImplHeader {
        target: target.ident.clone(),
        qualifier,
        module_path: impl_path.to_vec(),
    };
    crate::paths::locate_type(classes, &header, imports).is_ok_and(|index| {
        classes[index].entry.name == class.entry.name
            && classes[index].module_path == class.module_path
    })
}

/// Skip reason that follows from the item itself rather than its signature.
///
/// Precedence is deliberate: visibility first, because it is the hard
/// compile-time blocker a wrapper crate hits (`E0603`), then genericity.
fn item_skip_reason(vis: &syn::Visibility, reachable: bool, is_generic: bool) -> Option<String> {
    if !matches!(vis, syn::Visibility::Public(_)) || !reachable {
        return Some(skip_reason::NOT_PUBLIC.to_string());
    }
    if is_generic {
        return Some(skip_reason::GENERIC.to_string());
    }
    None
}

/// Skip reason that follows from the signature: any argument or return type
/// that only exists with a live Python interpreter.
///
/// A `PyResult<T>` return is exempt — the error is opaque, never rendered — but
/// its `Ok` type is still checked, so `PyResult<PyObject>` is skipped.
///
/// An `async fn` is refused outright: its declared output is what the future
/// *resolves to*, and a wrapper that called it would hand back the future
/// itself — a type error in the generated crate, which no `extern "C"` can
/// return anyway. pyo3 lets a `#[pyfunction]` be `async` (with its
/// `experimental-async` feature); the wrapper has no executor to drive it
/// (#307 review).
fn signature_skip_reason(sig: &syn::Signature, return_kind: ReturnKind) -> Option<String> {
    if sig.asyncness.is_some() {
        return Some(skip_reason::ASYNC_FN.to_string());
    }
    for input in &sig.inputs {
        let FnArg::Typed(pt) = input else { continue };
        if let Some(found) = pyo3_type_in(&pt.ty) {
            return Some(skip_reason::detailed(skip_reason::PYO3_TYPE, &found));
        }
    }
    if let ReturnType::Type(_, ty) = &sig.output {
        let checked = if return_kind == ReturnKind::PyResult {
            py_result_ok_type(ty).unwrap_or_else(|| (**ty).clone())
        } else {
            (**ty).clone()
        };
        if let Some(found) = pyo3_type_in(&checked) {
            return Some(skip_reason::detailed(skip_reason::PYO3_TYPE, &found));
        }
    }
    None
}

/// How a scanned PyO3 item returns: `(kind, ok_type, err_type, inner_type)`,
/// with the same vocabulary a `#[julia]` function's manifest entry uses.
///
/// `PyResult<T>` is [`ReturnKind::PyResult`] with `T` as the ok type and no
/// error type: a `PyErr` is opaque and must never be rendered, so there is
/// nothing for a consumer to name.
fn return_shape(output: &ReturnType) -> (ReturnKind, String, String, String) {
    let mut ok_type = String::new();
    let mut err_type = String::new();
    let mut inner_type = String::new();
    let kind = match output {
        ReturnType::Default => ReturnKind::Unit,
        ReturnType::Type(_, ty) => {
            if let Some(ok) = py_result_ok_type(ty) {
                ok_type = type_to_string(&ok);
                ReturnKind::PyResult
            } else if let Some(r) = extract_result_type(ty) {
                ok_type = type_to_string(&r.ok_type);
                err_type = type_to_string(&r.err_type);
                ReturnKind::Result
            } else if let Some(o) = extract_option_type(ty) {
                inner_type = type_to_string(&o.inner_type);
                ReturnKind::Option
            } else if return_type_to_string(output) == "()" {
                ReturnKind::Unit
            } else {
                ReturnKind::Plain
            }
        }
    };
    (kind, ok_type, err_type, inner_type)
}

/// `T` of a `PyResult<T>` (or `pyo3::PyResult<T>`), if the type is one.
pub fn py_result_ok_type(ty: &Type) -> Option<Type> {
    let Type::Path(path) = unparen(ty) else {
        return None;
    };
    let segment = path.path.segments.last()?;
    if segment.ident != "PyResult" {
        return None;
    }
    match &segment.arguments {
        syn::PathArguments::AngleBracketed(args) => args.args.iter().find_map(|a| match a {
            syn::GenericArgument::Type(t) => Some(t.clone()),
            _ => None,
        }),
        // `PyResult` with no argument is `PyResult<()>` only by alias default;
        // syn sees no argument, so report the unit type.
        _ => Some(syn::parse_quote!(())),
    }
}

/// The first type inside `ty` that only exists with a Python interpreter, as
/// written, or `None` when the type is interpreter-free.
///
/// Recognised: anything whose path starts with `pyo3`, the `Python` /
/// `GILGuard` / `Bound` / `Borrowed` handles, and any identifier starting with
/// `Py` (`PyObject`, `PyAny`, `PyRef`, `PyRefMut`, `PyErr`, `PyList`, ...).
/// The last rule is deliberately broad and can catch a user type named
/// `PyFoo`; erring towards *skipped with a reason* is the fail-closed side,
/// since the alternative is a wrapper crate that does not compile.
pub fn pyo3_type_in(ty: &Type) -> Option<String> {
    let mut found = None;
    walk_types(ty, &mut |t| {
        if found.is_some() {
            return;
        }
        if let Type::Path(p) = t {
            let first_is_pyo3 = p
                .path
                .segments
                .first()
                .map(|s| s.ident == "pyo3")
                .unwrap_or(false);
            let last = p.path.segments.last();
            let named = last
                .map(|s| {
                    let id = s.ident.to_string();
                    id.starts_with("Py")
                        || matches!(id.as_str(), "Python" | "GILGuard" | "Bound" | "Borrowed")
                })
                .unwrap_or(false);
            if first_is_pyo3 || named {
                found = Some(type_to_string(t));
            }
        }
    });
    found
}

/// Apply `f` to `ty` and to every type nested in it.
fn walk_types(ty: &Type, f: &mut impl FnMut(&Type)) {
    let ty = unparen(ty);
    f(ty);
    match ty {
        Type::Path(p) => {
            for segment in &p.path.segments {
                if let syn::PathArguments::AngleBracketed(args) = &segment.arguments {
                    for arg in &args.args {
                        if let syn::GenericArgument::Type(t) = arg {
                            walk_types(t, f);
                        }
                    }
                }
            }
        }
        Type::Reference(r) => walk_types(&r.elem, f),
        Type::Ptr(p) => walk_types(&p.elem, f),
        Type::Slice(s) => walk_types(&s.elem, f),
        Type::Array(a) => walk_types(&a.elem, f),
        Type::Group(g) => walk_types(&g.elem, f),
        Type::Tuple(t) => {
            for elem in &t.elems {
                walk_types(elem, f);
            }
        }
        _ => {}
    }
}