alef 0.67.0

Opinionated polyglot binding generator for Rust libraries
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
use super::helpers::is_test_gated;
use crate::core::ir::{DefaultValue, FieldDef, TypeRef};
use ahash::AHashMap;
use quote::ToTokens;
use syn;

/// Every associated function of every inherent `impl` block in one module, keyed by
/// `(type name, function name)`.
///
/// Exists so [`extract_default_values`] can follow a `fn default()` that delegates to one of
/// its own constructors instead of spelling a struct literal. Scoped to a single module for
/// the same reason [`collect_literal_consts`] is: `impl Default` and the `fn new` it calls sit
/// next to each other in the overwhelmingly common case, and resolving a constructor from
/// another module would need a crate-wide index. ~keep
pub(crate) type ConstructorIndex<'a> = AHashMap<(String, String), &'a syn::ImplItemFn>;

/// How many `Self::a() -> Self::b() -> ...` hops [`extract_default_values`] will follow.
///
/// A bound rather than a visited-set because the bound is also the honest limit of the
/// technique: past a few hops the "constructor" is a pipeline, not a constant. It doubles as
/// the cycle guard, so `fn new() { Self::fresh() }` / `fn fresh() { Self::new() }` terminates
/// instead of recursing forever. ~keep
const MAX_DELEGATION_DEPTH: usize = 4;

/// Extract concrete default values from an `impl Default for T` block.
///
/// Finds the `fn default() -> Self` method and reads its body one of three ways:
///
/// 1. a struct literal (`Self { field: expr, ... }`), each initializer lowered to a
///    [`DefaultValue`]; or
/// 2. a delegation to one of `T`'s own constructors (`Self::new("en")`), whose parameters are
///    bound to the literal arguments the delegation passed and whose struct literal is then
///    read against that binding; or
/// 3. for a single-field type, a bare associated-const path (`Self::ONE`, `Weight::ONE`) whose
///    initializer is itself a foldable literal or single-argument tuple-struct literal of the
///    same type (`const ONE: Weight = Weight(1);`) — see [`single_field_const_tail_default`].
///
/// A body that is none of these writes [`DefaultValue::Unresolved`] to every field. That is
/// **not** the same as [`DefaultValue::Empty`]: `Empty` claims the default *is* the type's
/// zero, `Unresolved` records that alef could not read it. Collapsing the two is what let six
/// backends emit their type-zero underneath a doc comment quoting the real Rust value; see
/// [`DefaultValue::Unresolved`] and `cli::pipeline::generate::validation`, which refuses to
/// generate rather than guess. ~keep
///
/// `literal_consts` resolves a field initializer that references a sibling
/// `const NAME: T = <literal>;` declared in the same module (e.g. `NAME`, `NAME.to_string()`,
/// or `Type::NAME`) to that constant's actual value. See [`collect_literal_consts`].
pub(crate) fn extract_default_values(
    item: &syn::ItemImpl,
    self_type: &str,
    fields: &mut [FieldDef],
    literal_consts: &AHashMap<String, DefaultValue>,
    constructors: &ConstructorIndex<'_>,
) {
    let default_fn = item.items.iter().find_map(|impl_item| {
        if let syn::ImplItem::Fn(method) = impl_item
            && method.sig.ident == "default"
        {
            return Some(method);
        }
        None
    });

    let Some(default_fn) = default_fn else {
        mark_unresolved(fields, "impl Default block without a `fn default()` item");
        return;
    };

    // The declared type of each field, so a two-segment path initializer can be checked against
    // it before being lowered to `DefaultValue::EnumVariant`. See [`admits_enum_variant`]. ~keep
    let field_types: AHashMap<String, TypeRef> = fields
        .iter()
        .map(|field| (field.name.clone(), field.ty.clone()))
        .collect();
    let scope = EvalScope::new(self_type, literal_consts, &field_types);

    let defaults = if let Some(struct_expr) = find_struct_expr(&default_fn.block) {
        struct_expr_defaults(struct_expr, &scope)
    } else if let Some(delegated) = follow_delegation(&default_fn.block, self_type, constructors, &scope, 0) {
        delegated
    } else if let Some(single_field) = single_field_const_tail_default(&default_fn.block, fields, &scope) {
        single_field
    } else {
        let body = default_fn.block.to_token_stream().to_string();
        tracing::warn!(
            target: "alef::extract::defaults",
            rust_type = self_type,
            body = %body,
            "`impl Default` body is neither a struct literal nor a constant-foldable delegation; \
             field defaults are unresolved"
        );
        mark_unresolved(fields, &body);
        return;
    };

    for field in fields.iter_mut() {
        if let Some(default_val) = defaults.get(&field.name) {
            field.typed_default = Some(default_val.clone());
        } else {
            field.typed_default = Some(DefaultValue::Empty);
        }
    }
}

fn mark_unresolved(fields: &mut [FieldDef], body: &str) {
    for field in fields.iter_mut() {
        field.typed_default = Some(DefaultValue::Unresolved(body.to_string()));
    }
}

/// Collects the associated (receiver-less) functions of every inherent `impl` block in
/// `items`, so a delegating `fn default()` can be followed to the constructor it calls.
///
/// Trait impls are skipped: `impl Default for T` is the caller, and no other trait method is
/// a plausible delegation target. Methods with a `self` receiver are skipped because
/// `Self::name(..)` in a `fn default()` cannot reach one. ~keep
pub(crate) fn collect_constructors(items: &[syn::Item]) -> ConstructorIndex<'_> {
    let mut index = ConstructorIndex::new();
    for item in items {
        let syn::Item::Impl(item_impl) = item else {
            continue;
        };
        // A `#[cfg(test)]` impl block is not part of the binding surface — `extractor::mod` skips
        // it when extracting — so a test-only `fn new` must not shadow the real constructor. ~keep
        if item_impl.trait_.is_some() || is_test_gated(&item_impl.attrs) {
            continue;
        }
        let Some(type_name) = path_type_name(&item_impl.self_ty) else {
            continue;
        };
        for impl_item in &item_impl.items {
            let syn::ImplItem::Fn(method) = impl_item else {
                continue;
            };
            if matches!(method.sig.inputs.first(), Some(syn::FnArg::Receiver(_))) {
                continue;
            }
            index.insert((type_name.clone(), method.sig.ident.to_string()), method);
        }
    }
    index
}

fn path_type_name(ty: &syn::Type) -> Option<String> {
    match ty {
        syn::Type::Path(path) => path.path.segments.last().map(|segment| segment.ident.to_string()),
        _ => None,
    }
}

/// Collects every literal-valued `const` visible to an `impl Default` in the same module, so
/// [`extract_default_values`] can resolve a field initializer that references one instead of
/// collapsing it to `DefaultValue::Empty`.
///
/// Both module-level `const NAME: T = <literal>;` (keyed by the bare identifier) and associated
/// consts of inherent `impl` blocks (keyed `Type::NAME`) are collected. The associated-const key
/// carries the owning type because two types in one module may each declare `const DEFAULT`, and
/// a bare `Self` key would let one silently answer for the other. ~keep
///
/// Every literal kind is collected, not only `&str`. `max_pages_ceiling: DEFAULT_MAX_PAGES`
/// against `const DEFAULT_MAX_PAGES: usize = 500;` is the dominant shape of unreadable field
/// default in the consumer crates, and a numeric const is exactly as readable as a string one —
/// alef was rendering `0` for several of these, underneath a doc comment quoting the real value.
/// A const whose initializer is not a literal (`Duration::from_secs(5)`, a `concat!`) stays out:
/// evaluating it would be interpretation, not reading. ~keep
///
/// Deliberately scoped to the items of a single module/file: `impl Default` and
/// the const it references are the overwhelmingly common shape (`refresh.rs`'s
/// `DEFAULT_CATALOG_URL` alongside `CatalogRefreshConfig`'s `impl Default`), and
/// resolving a `use`-imported const from another module would need a full
/// crate-wide const index. ~keep
pub(crate) fn collect_literal_consts(items: &[syn::Item]) -> AHashMap<String, DefaultValue> {
    let mut consts = AHashMap::new();
    for item in items {
        match item {
            syn::Item::Const(item_const) => {
                if let Some(value) = const_literal_value(&item_const.expr) {
                    consts.insert(item_const.ident.to_string(), value);
                }
            }
            // Trait impls are skipped for the same reason [`collect_constructors`] skips them,
            // and a `#[cfg(test)]` impl is not part of the binding surface. ~keep
            syn::Item::Impl(item_impl) if item_impl.trait_.is_none() && !is_test_gated(&item_impl.attrs) => {
                let Some(type_name) = path_type_name(&item_impl.self_ty) else {
                    continue;
                };
                for impl_item in &item_impl.items {
                    if let syn::ImplItem::Const(assoc_const) = impl_item
                        && let Some(value) = const_literal_value(&assoc_const.expr)
                    {
                        consts.insert(format!("{type_name}::{}", assoc_const.ident), value);
                    }
                }
            }
            _ => {}
        }
    }
    consts
}

/// The value of a `const NAME: T = <literal>;`, or `None` for any other const initializer.
fn const_literal_value(expr: &syn::Expr) -> Option<DefaultValue> {
    match expr {
        syn::Expr::Lit(lit) => match &lit.lit {
            syn::Lit::Str(s) => Some(DefaultValue::StringLiteral(s.value())),
            syn::Lit::Char(c) => Some(DefaultValue::StringLiteral(c.value().to_string())),
            syn::Lit::Bool(b) => Some(DefaultValue::BoolLiteral(b.value)),
            syn::Lit::Int(i) => i.base10_parse::<i64>().ok().map(DefaultValue::IntLiteral),
            syn::Lit::Float(f) => f.base10_parse::<f64>().ok().map(DefaultValue::FloatLiteral),
            _ => None,
        },
        syn::Expr::Unary(unary) if matches!(unary.op, syn::UnOp::Neg(_)) => match const_literal_value(&unary.expr)? {
            DefaultValue::IntLiteral(v) => Some(DefaultValue::IntLiteral(-v)),
            DefaultValue::FloatLiteral(v) => Some(DefaultValue::FloatLiteral(-v)),
            _ => None,
        },
        // A single-argument tuple-struct/newtype literal (`Weight(1)`) folds to the literal it
        // wraps: `DefaultValue` has no struct-shaped variant, and for a single-field newtype
        // that literal *is* the field's own default once flattened (see
        // `single_field_const_tail_default`). Gated on an upper-case callee so an ordinary
        // function call (`compute(1)`, conventionally snake_case) is never mistaken for a
        // tuple-struct constructor and folded into a guess; a lower-case callee stays `None`
        // rather than risk it. ~keep
        syn::Expr::Call(call) if call.args.len() == 1 => {
            let syn::Expr::Path(path) = &*call.func else {
                return None;
            };
            let name = path.path.segments.last()?.ident.to_string();
            if !name.starts_with(|c: char| c.is_ascii_uppercase()) {
                return None;
            }
            const_literal_value(call.args.first()?)
        }
        _ => None,
    }
}

/// Everything a field initializer can be resolved against.
///
/// `literal_consts` is module-wide and constant. `params` is populated only while reading a
/// constructor's body on behalf of a delegating `fn default()`: it binds that constructor's
/// parameters to the literal arguments the delegation passed, which is what turns
/// `fn default() { Self::new("en") }` plus `fn new(lang: &str) { Self { lang: lang.into(), .. } }`
/// into `lang = "en"` rather than a guess. ~keep
struct EvalScope<'a> {
    /// The type whose `impl Default` is being read, so `Self::NAME` can be resolved against the
    /// `Type::NAME`-keyed associated consts in `literal_consts`. ~keep
    self_type: &'a str,
    literal_consts: &'a AHashMap<String, DefaultValue>,
    /// Declared type per field name, used only to decide whether a two-segment path initializer
    /// can be an enum variant. Empty while reading a constructor body on behalf of a delegating
    /// `fn default()` would be wrong, so it is carried across `with_params` unchanged. ~keep
    field_types: &'a AHashMap<String, TypeRef>,
    params: AHashMap<String, DefaultValue>,
}

impl<'a> EvalScope<'a> {
    fn new(
        self_type: &'a str,
        literal_consts: &'a AHashMap<String, DefaultValue>,
        field_types: &'a AHashMap<String, TypeRef>,
    ) -> Self {
        Self {
            self_type,
            literal_consts,
            field_types,
            params: AHashMap::new(),
        }
    }

    fn with_params(&self, params: AHashMap<String, DefaultValue>) -> EvalScope<'a> {
        EvalScope {
            self_type: self.self_type,
            literal_consts: self.literal_consts,
            field_types: self.field_types,
            params,
        }
    }

    /// Resolves `Owner::NAME` (and `Self::NAME`, against the type being read) to the value of an
    /// associated literal const declared in the same module.
    fn associated_const(&self, owner: &str, name: &str) -> Option<DefaultValue> {
        let owner = if owner == "Self" { self.self_type } else { owner };
        self.literal_consts.get(&format!("{owner}::{name}")).cloned()
    }
}

/// True for the `DefaultValue`s that carry an actual value, as opposed to recording the
/// absence of one. Only these may be bound to a constructor parameter: binding `Empty` or
/// `Unresolved` would substitute a guess into the callee's body and lose the very
/// distinction this module exists to keep. ~keep
fn carries_value(value: &DefaultValue) -> bool {
    matches!(
        value,
        DefaultValue::BoolLiteral(_)
            | DefaultValue::StringLiteral(_)
            | DefaultValue::IntLiteral(_)
            | DefaultValue::FloatLiteral(_)
            | DefaultValue::EnumVariant(_)
            | DefaultValue::TupleVariant(_, _)
            | DefaultValue::StructVariant(_, _)
            | DefaultValue::ListLiteral(_)
    )
}

/// Follow a `fn default()` whose body is a call to one of the type's own associated
/// functions — `Self::new("en")`, `PaddleOcrConfig::for_language("en")` — and read the
/// callee's struct literal with its parameters bound to the arguments passed.
///
/// This is a constant fold, not an interpreter, and the boundary is deliberate. A callee that
/// computes a field (`side_len: base * scale_for(lang)`), branches on its argument, or builds
/// through a builder is not followed; those fields stay unresolved and get reported rather
/// than guessed. The technique covers the shape it was written for — a constructor taking
/// literal arguments and returning a struct literal — and nothing beyond it. ~keep
fn follow_delegation(
    block: &syn::Block,
    self_type: &str,
    constructors: &ConstructorIndex<'_>,
    scope: &EvalScope<'_>,
    depth: usize,
) -> Option<AHashMap<String, DefaultValue>> {
    if depth >= MAX_DELEGATION_DEPTH {
        return None;
    }

    let call = tail_call_expr(block)?;
    let syn::Expr::Path(path) = &*call.func else {
        return None;
    };
    let segments: Vec<String> = path.path.segments.iter().map(|s| s.ident.to_string()).collect();
    // `Self::new(..)` and `PaddleOcrConfig::new(..)` name the same function. A longer path
    // leaves the module `constructors` indexes, so it cannot be resolved here. ~keep
    let [owner, fn_name] = segments.as_slice() else {
        return None;
    };
    if owner.as_str() != "Self" && owner.as_str() != self_type {
        return None;
    }
    // `Self::default()` inside `fn default()` is unbounded recursion in the source itself.
    if fn_name.as_str() == "default" {
        return None;
    }

    let target = constructors.get(&(self_type.to_string(), fn_name.clone()))?;

    let mut params = AHashMap::new();
    let mut arguments = call.args.iter();
    for input in &target.sig.inputs {
        let syn::FnArg::Typed(pat_type) = input else {
            return None;
        };
        let argument = arguments.next()?;
        let syn::Pat::Ident(pat_ident) = pat_type.pat.as_ref() else {
            continue;
        };
        let value = expr_to_default_value(argument, scope, None);
        if carries_value(&value) {
            params.insert(pat_ident.ident.to_string(), value);
        }
    }
    // An arity mismatch means the index resolved the wrong function (or the source does not
    // compile); either way, reading its body would invent values. ~keep
    if arguments.next().is_some() {
        return None;
    }

    let inner = scope.with_params(params);
    if let Some(struct_expr) = find_struct_expr(&target.block) {
        return Some(struct_expr_defaults(struct_expr, &inner));
    }
    follow_delegation(&target.block, self_type, constructors, &inner, depth + 1)
}

/// The tail expression of a block, unwrapped to a call. Only the tail is considered: an
/// earlier statement is not what the function returns.
fn tail_call_expr(block: &syn::Block) -> Option<&syn::ExprCall> {
    match block.stmts.last()? {
        syn::Stmt::Expr(expr, _) => unwrap_to_call_expr(expr),
        _ => None,
    }
}

fn unwrap_to_call_expr(expr: &syn::Expr) -> Option<&syn::ExprCall> {
    match expr {
        syn::Expr::Call(call) => Some(call),
        syn::Expr::Block(b) => tail_call_expr(&b.block),
        syn::Expr::Return(ret) => ret.expr.as_deref().and_then(unwrap_to_call_expr),
        _ => None,
    }
}

/// `fn default() -> Self { Self::ONE }` for a single-field newtype (`pub struct Weight(u32);`):
/// the tail expression is a bare associated-const path rather than a struct literal or a
/// delegation call, but the const names a value of `Self` itself, and `Self` has exactly one
/// field — so the const's value, once folded to a scalar by [`const_literal_value`], *is* that
/// field's default.
///
/// Scoped to single-field types deliberately: a multi-field struct offers no way to know which
/// field a lone `Self::NAME` scalar belongs to (`Self::NAME` could itself be a full struct
/// literal, which [`const_literal_value`] does not attempt to read — see its doc comment).
/// Guessing a field mapping would be worse than reporting `Unresolved`. ~keep
fn single_field_const_tail_default(
    block: &syn::Block,
    fields: &[FieldDef],
    scope: &EvalScope<'_>,
) -> Option<AHashMap<String, DefaultValue>> {
    let [field] = fields else {
        return None;
    };
    let path = tail_path_expr(block)?;
    let segments: Vec<String> = path.path.segments.iter().map(|s| s.ident.to_string()).collect();
    let [owner, name] = segments.as_slice() else {
        return None;
    };
    let value = scope.associated_const(owner, name)?;
    let mut defaults = AHashMap::new();
    defaults.insert(field.name.clone(), value);
    Some(defaults)
}

/// The tail expression of a block, unwrapped to a bare path. Mirrors [`tail_call_expr`] for the
/// `Self::NAME` shape [`single_field_const_tail_default`] reads.
fn tail_path_expr(block: &syn::Block) -> Option<&syn::ExprPath> {
    match block.stmts.last()? {
        syn::Stmt::Expr(expr, _) => unwrap_to_path_expr(expr),
        _ => None,
    }
}

fn unwrap_to_path_expr(expr: &syn::Expr) -> Option<&syn::ExprPath> {
    match expr {
        syn::Expr::Path(path) => Some(path),
        syn::Expr::Block(b) => tail_path_expr(&b.block),
        syn::Expr::Return(ret) => ret.expr.as_deref().and_then(unwrap_to_path_expr),
        _ => None,
    }
}

/// Map each initializer of a struct literal to a `DefaultValue`.
fn struct_expr_defaults(struct_expr: &syn::ExprStruct, scope: &EvalScope<'_>) -> AHashMap<String, DefaultValue> {
    let mut defaults = AHashMap::new();
    for field in &struct_expr.fields {
        let Some(ident) = &field.member_named() else {
            continue;
        };
        let name = ident.to_string();
        let value = expr_to_default_value(&field.expr, scope, scope.field_types.get(&name));
        if let DefaultValue::Unresolved(source) = &value {
            tracing::debug!(
                target: "alef::extract::defaults",
                rust_type = scope.self_type,
                field = %name,
                initializer = %source,
                "field initializer is not constant-foldable; its default is unresolved"
            );
        }
        defaults.insert(name, value);
    }
    defaults
}

/// Recursively search a block for a struct expression (`Self { ... }` or `Name { ... }`).
fn find_struct_expr(block: &syn::Block) -> Option<&syn::ExprStruct> {
    for stmt in block.stmts.iter().rev() {
        match stmt {
            syn::Stmt::Expr(expr, _) => {
                if let Some(s) = unwrap_to_struct_expr(expr) {
                    return Some(s);
                }
            }
            syn::Stmt::Local(local) => {
                if let Some(init) = &local.init
                    && let Some(s) = unwrap_to_struct_expr(&init.expr)
                {
                    return Some(s);
                }
            }
            _ => {}
        }
    }
    None
}

/// Try to unwrap an expression to a struct expression, looking through blocks.
fn unwrap_to_struct_expr(expr: &syn::Expr) -> Option<&syn::ExprStruct> {
    match expr {
        syn::Expr::Struct(s) => Some(s),
        syn::Expr::Block(b) => find_struct_expr(&b.block),
        _ => None,
    }
}

/// Helper trait to extract the named member from a `FieldValue`.
trait FieldMemberExt {
    fn member_named(&self) -> Option<&syn::Ident>;
}

impl FieldMemberExt for syn::FieldValue {
    fn member_named(&self) -> Option<&syn::Ident> {
        match &self.member {
            syn::Member::Named(ident) => Some(ident),
            syn::Member::Unnamed(_) => None,
        }
    }
}

/// Records that this initializer could not be read, carrying its source text so the diagnostic
/// in `cli::pipeline::generate::validation` can name the expression it refused.
fn unreadable(expr: &syn::Expr) -> DefaultValue {
    DefaultValue::Unresolved(expr.to_token_stream().to_string())
}

/// Whether a field of this declared type could hold an enum variant.
///
/// `Expr::Path` with two segments is ambiguous in Rust source: `Mode::Fast` names an enum
/// variant, `Self::DEFAULT_MODEL` names an associated const, `Duration::ZERO` names an
/// associated const of a struct. Lowering all three to [`DefaultValue::EnumVariant`] let
/// `codegen::config_gen::shared` render the *snake-cased variant name* as a string literal
/// whenever the field's type was `String`, so `model: Self::DEFAULT_MODEL` shipped as
/// `"default_model"` — a value that appears nowhere in the source crate and looks entirely
/// plausible in generated output.
///
/// `Named` is admitted rather than checked against an enum index because the enum is frequently
/// declared in a different module from the `impl Default` that names one of its variants, and
/// this pass is module-scoped by construction (see [`collect_literal_consts`]). Every non-`Named`
/// type is refused, which is where the fabrication lived.
///
/// `None` means the expression is not in a field position — a constructor argument being bound
/// by [`follow_delegation`], where no declared type is in reach — and keeps the prior reading. ~keep
fn admits_enum_variant(field_ty: Option<&TypeRef>) -> bool {
    match field_ty {
        None | Some(TypeRef::Named(_)) => true,
        Some(TypeRef::Optional(inner) | TypeRef::Vec(inner)) => admits_enum_variant(Some(&**inner)),
        Some(_) => false,
    }
}

/// Convert an expression to a `DefaultValue`.
///
/// `field_ty` is the declared type of the field being initialized, or `None` where the
/// expression is not a field initializer. It is consulted only by [`admits_enum_variant`].
///
/// Recognizes:
/// - `true` / `false` → `BoolLiteral`
/// - Integer literals → `IntLiteral`
/// - Float literals → `FloatLiteral`
/// - `"str".to_string()`, `String::from("str")`, `"str".into()` → `StringLiteral`
/// - `String::new()` → `StringLiteral("")`
/// - `'c'` (char literal) → `StringLiteral("c")`
/// - `Vec::new()`, `vec![]` → `Empty`
/// - `SomeType::default()`, `Default::default()` → `Empty`
/// - `SomeEnum::Variant`, where the field's declared type can hold one → `EnumVariant("Variant")`
/// - `SomeEnum::Variant(a, b)`, an upper-case-callee call with foldable arguments →
///   `TupleVariant("Variant", [a, b])`
/// - `SomeEnum::Variant { a: .., b: .. }`, a struct expression with foldable fields →
///   `StructVariant("Variant", [(a, ..), (b, ..)])`
/// - `CONST_NAME.to_string()` / `.to_owned()` / `.into()`, or a bare `CONST_NAME`,
///   where `CONST_NAME` resolves via `scope.literal_consts` → the constant's value
/// - `Self::CONST_NAME` / `Type::CONST_NAME`, where the associated literal const is declared in
///   the same module → the constant's value
/// - a bare constructor parameter, or `param.to_string()` / `.to_owned()` / `.into()`, where
///   `param` is bound in `scope.params` → the value the delegation passed for it
/// - Anything else → [`DefaultValue::Unresolved`]
///
/// Note the last line. `Empty` is reserved for the initializers that are *known* to be the
/// type's zero — `Vec::new()`, `Default::default()`, `vec![]` — and asserts as much. Every
/// other shape records that alef could not read the value, which is the same distinction
/// [`extract_default_values`] draws for a whole unreadable `fn default()` body, applied one
/// level down. Before this, an unreadable initializer inside a readable struct literal wrote
/// `Empty` and every backend rendered its own type-zero for it. ~keep
fn expr_to_default_value(expr: &syn::Expr, scope: &EvalScope<'_>, field_ty: Option<&TypeRef>) -> DefaultValue {
    match expr {
        syn::Expr::Lit(lit) => match &lit.lit {
            syn::Lit::Bool(b) => DefaultValue::BoolLiteral(b.value),
            syn::Lit::Int(i) => {
                if let Ok(val) = i.base10_parse::<i64>() {
                    DefaultValue::IntLiteral(val)
                } else {
                    unreadable(expr)
                }
            }
            syn::Lit::Float(f) => {
                if let Ok(val) = f.base10_parse::<f64>() {
                    DefaultValue::FloatLiteral(val)
                } else {
                    unreadable(expr)
                }
            }
            syn::Lit::Char(c) => DefaultValue::StringLiteral(c.value().to_string()),
            syn::Lit::Str(s) => DefaultValue::StringLiteral(s.value()),
            _ => unreadable(expr),
        },

        // `&"en"` and `&CONST` reach a constructor parameter unchanged; the reference is not
        // part of the value. Parentheses and macro-expansion groups are likewise not part of
        // the value, and refusing to see through them would refuse a readable `(0.5)`. ~keep
        syn::Expr::Reference(syn::ExprReference { expr: inner, .. })
        | syn::Expr::Paren(syn::ExprParen { expr: inner, .. })
        | syn::Expr::Group(syn::ExprGroup { expr: inner, .. }) => expr_to_default_value(inner, scope, field_ty),

        syn::Expr::Unary(unary) if matches!(unary.op, syn::UnOp::Neg(_)) => {
            match expr_to_default_value(&unary.expr, scope, field_ty) {
                DefaultValue::IntLiteral(v) => DefaultValue::IntLiteral(-v),
                DefaultValue::FloatLiteral(v) => DefaultValue::FloatLiteral(-v),
                _ => unreadable(expr),
            }
        }

        syn::Expr::Binary(bin) => {
            let lhs = expr_to_default_value(&bin.left, scope, field_ty);
            let rhs = expr_to_default_value(&bin.right, scope, field_ty);
            match (lhs, rhs) {
                (DefaultValue::IntLiteral(a), DefaultValue::IntLiteral(b)) => match bin.op {
                    syn::BinOp::Add(_) => a
                        .checked_add(b)
                        .map(DefaultValue::IntLiteral)
                        .unwrap_or_else(|| unreadable(expr)),
                    syn::BinOp::Sub(_) => a
                        .checked_sub(b)
                        .map(DefaultValue::IntLiteral)
                        .unwrap_or_else(|| unreadable(expr)),
                    syn::BinOp::Mul(_) => a
                        .checked_mul(b)
                        .map(DefaultValue::IntLiteral)
                        .unwrap_or_else(|| unreadable(expr)),
                    syn::BinOp::Div(_) if b != 0 => DefaultValue::IntLiteral(a / b),
                    syn::BinOp::Rem(_) if b != 0 => DefaultValue::IntLiteral(a % b),
                    syn::BinOp::Shl(_) if (0..63).contains(&b) => a
                        .checked_shl(b as u32)
                        .map(DefaultValue::IntLiteral)
                        .unwrap_or_else(|| unreadable(expr)),
                    syn::BinOp::Shr(_) if (0..63).contains(&b) => DefaultValue::IntLiteral(a >> (b as u32)),
                    syn::BinOp::BitOr(_) => DefaultValue::IntLiteral(a | b),
                    syn::BinOp::BitAnd(_) => DefaultValue::IntLiteral(a & b),
                    syn::BinOp::BitXor(_) => DefaultValue::IntLiteral(a ^ b),
                    _ => unreadable(expr),
                },
                (DefaultValue::FloatLiteral(a), DefaultValue::FloatLiteral(b)) => match bin.op {
                    syn::BinOp::Add(_) => DefaultValue::FloatLiteral(a + b),
                    syn::BinOp::Sub(_) => DefaultValue::FloatLiteral(a - b),
                    syn::BinOp::Mul(_) => DefaultValue::FloatLiteral(a * b),
                    syn::BinOp::Div(_) if b != 0.0 => DefaultValue::FloatLiteral(a / b),
                    _ => unreadable(expr),
                },
                _ => unreadable(expr),
            }
        }

        syn::Expr::MethodCall(mc) => {
            let method_name = mc.method.to_string();
            match method_name.as_str() {
                "to_string" | "to_owned" | "into" => {
                    if let syn::Expr::Lit(lit) = &*mc.receiver
                        && let syn::Lit::Str(s) = &lit.lit
                    {
                        return DefaultValue::StringLiteral(s.value());
                    }
                    match resolve_ident(&mc.receiver, scope) {
                        // `.to_string()` / `.to_owned()` on a non-string is a *conversion*, so
                        // only a string receiver survives them unchanged. `.into()` is
                        // identity-preserving for every value kind alef can represent. ~keep
                        Some(value @ DefaultValue::StringLiteral(_)) => value,
                        Some(value) if method_name == "into" => value,
                        _ => unreadable(expr),
                    }
                }
                _ => unreadable(expr),
            }
        }

        syn::Expr::Call(call) => {
            if let syn::Expr::Path(path) = &*call.func {
                let segments: Vec<String> = path.path.segments.iter().map(|s| s.ident.to_string()).collect();

                if (segments == ["Some"] || segments == ["Option", "Some"])
                    && call.args.len() == 1
                    && let Some(inner) = call.args.first()
                {
                    return expr_to_default_value(inner, scope, field_ty);
                }

                if segments == ["String", "from"] && call.args.len() == 1 {
                    if let Some(syn::Expr::Lit(lit)) = call.args.first()
                        && let syn::Lit::Str(s) = &lit.lit
                    {
                        return DefaultValue::StringLiteral(s.value());
                    }
                    if let Some(argument) = call.args.first()
                        && let Some(value @ DefaultValue::StringLiteral(_)) = resolve_ident(argument, scope)
                    {
                        return value;
                    }
                    return unreadable(expr);
                }

                if segments == ["String", "new"] && call.args.is_empty() {
                    return DefaultValue::StringLiteral(String::new());
                }

                // `Cow::Borrowed("")` carries the value in its argument; the `Cow` itself is a
                // representation the binding layer already erases (`FieldDef::core_wrapper`), so
                // reading through it is not a guess. ~keep
                if let [.., owner, variant] = segments.as_slice()
                    && owner == "Cow"
                    && matches!(variant.as_str(), "Borrowed" | "Owned")
                    && call.args.len() == 1
                    && let Some(inner) = call.args.first()
                {
                    // The boundary the erasure argument does not cross: it holds for a value alef
                    // actually read. `Cow::Owned(detect_language())` names a core-private function,
                    // and a binding that cannot call it would render the name as the default. ~keep
                    return match expr_to_default_value(inner, scope, field_ty) {
                        DefaultValue::Unresolved(_) | DefaultValue::FunctionCall(_) => unreadable(expr),
                        resolved => resolved,
                    };
                }

                // The one family of calls whose result really is the type's zero, so `Empty` is an
                // assertion here rather than a fallback. ~keep
                if segments.len() == 2 && segments[1] == "new" && call.args.is_empty() {
                    let type_name = &segments[0];
                    if matches!(
                        type_name.as_str(),
                        "Vec" | "HashMap" | "HashSet" | "BTreeMap" | "BTreeSet" | "AHashMap" | "AHashSet"
                    ) {
                        return DefaultValue::Empty;
                    }
                }

                if segments == ["Duration", "from_secs"] && call.args.len() == 1 {
                    if let Some(syn::Expr::Lit(lit)) = call.args.first()
                        && let syn::Lit::Int(i) = &lit.lit
                        && let Ok(val) = i.base10_parse::<i64>()
                    {
                        return DefaultValue::IntLiteral(val * 1000);
                    }
                    return unreadable(expr);
                }

                if segments == ["Duration", "from_millis"] && call.args.len() == 1 {
                    if let Some(syn::Expr::Lit(lit)) = call.args.first()
                        && let syn::Lit::Int(i) = &lit.lit
                        && let Ok(val) = i.base10_parse::<i64>()
                    {
                        return DefaultValue::IntLiteral(val);
                    }
                    return unreadable(expr);
                }

                // `T::default()` / `Default::default()` is the type's zero by definition. ~keep
                if segments.last().is_some_and(|s| s == "default") {
                    return DefaultValue::Empty;
                }

                // A tuple-variant or tuple-struct constructor call whose arguments each fold
                // independently (`Mode::Custom(5)`, `Weight(1)`). Gated on an upper-case callee
                // for the same reason `const_literal_value` gates its tuple-struct fold: a
                // lower-case callee is a function by Rust convention, and reading through it
                // would be interpretation, not reading. All-or-nothing like `ListLiteral`: one
                // unfoldable argument leaves the whole call unreadable rather than a
                // partially-known payload. ~keep
                if !call.args.is_empty()
                    && let Some(variant) = segments.last()
                    && variant.starts_with(|c: char| c.is_ascii_uppercase())
                {
                    let mut values = Vec::with_capacity(call.args.len());
                    for argument in &call.args {
                        let value = expr_to_default_value(argument, scope, None);
                        if !carries_value(&value) {
                            return unreadable(expr);
                        }
                        values.push(value);
                    }
                    return DefaultValue::TupleVariant(variant.clone(), values);
                }

                if call.args.is_empty() {
                    return DefaultValue::FunctionCall(segments.join("::"));
                }
            }
            unreadable(expr)
        }

        // A struct-variant enum default (`Kind::Curated { label: "x".to_string() }`) or a plain
        // nested struct-literal default, each named field folded independently. Same
        // all-or-nothing rule as `TupleVariant`/`ListLiteral`: one unfoldable field, or a `..`
        // base that could carry fields this pass never saw, leaves the whole expression
        // unreadable rather than a partially-known payload. ~keep
        syn::Expr::Struct(struct_expr) => {
            if struct_expr.rest.is_some() {
                return unreadable(expr);
            }
            let Some(variant) = struct_expr.path.segments.last().map(|s| s.ident.to_string()) else {
                return unreadable(expr);
            };
            let mut fields = Vec::with_capacity(struct_expr.fields.len());
            for field_value in &struct_expr.fields {
                let Some(name) = field_value.member_named() else {
                    return unreadable(expr);
                };
                let value = expr_to_default_value(&field_value.expr, scope, None);
                if !carries_value(&value) {
                    return unreadable(expr);
                }
                fields.push((name.to_string(), value));
            }
            DefaultValue::StructVariant(variant, fields)
        }

        syn::Expr::Path(path) => {
            // `resolve_ident` covers the *readable* paths, including the associated const that
            // makes `model: Self::DEFAULT_MODEL` a string rather than a variant named
            // `DEFAULT_MODEL`. A value alef can resolve always beats a classification it can
            // only infer, so this runs before the enum reading below. ~keep
            if let Some(value) = resolve_ident(expr, scope) {
                return value;
            }
            let segments: Vec<String> = path.path.segments.iter().map(|s| s.ident.to_string()).collect();
            if segments.len() == 1 && segments[0] == "None" {
                return DefaultValue::None;
            }
            // A variant may be named through any number of module segments
            // (`crate::types::ResultFormat::Unified`), and only the last one is the variant. A
            // single segment is a bare identifier `resolve_ident` already failed to bind. ~keep
            if segments.len() >= 2
                && admits_enum_variant(field_ty)
                && let Some(name) = segments.last()
            {
                return DefaultValue::EnumVariant(name.clone());
            }
            unreadable(expr)
        }

        syn::Expr::Macro(mac) => {
            let macro_name = mac
                .mac
                .path
                .segments
                .last()
                .map(|s| s.ident.to_string())
                .unwrap_or_default();
            if !matches!(macro_name.as_str(), "vec" | "hashmap" | "hashset") {
                return unreadable(expr);
            }
            // An empty collection macro really is the type's zero. ~keep
            if mac.mac.tokens.is_empty() {
                return DefaultValue::Empty;
            }
            // Only `vec!` is destructured. `hashmap!`/`hashset!` carry key-value and set
            // semantics `DefaultValue` cannot represent, so a populated one is unreadable rather
            // than flattened into a list that would render wrongly. ~keep
            if macro_name != "vec" {
                return unreadable(expr);
            }
            let Ok(elements) = mac
                .mac
                .parse_body_with(syn::punctuated::Punctuated::<syn::Expr, syn::Token![,]>::parse_terminated)
            else {
                // `vec![expr; N]` is not a comma list and fails to parse as one. ~keep
                return unreadable(expr);
            };
            if elements.is_empty() {
                return DefaultValue::Empty;
            }
            let mut lowered = Vec::with_capacity(elements.len());
            for element in &elements {
                let value = expr_to_default_value(element, scope, field_ty);
                // Only self-contained values may sit in an element position. A function-call
                // default cannot be evaluated at generation time, and `Empty`/`None`/
                // `Unresolved` carry no element value at all; any of them makes the whole
                // literal non-representable. Lowering a partial list would hand a backend a
                // default that silently differs from the Rust one. ~keep
                if !carries_value(&value) {
                    return unreadable(expr);
                }
                lowered.push(value);
            }
            DefaultValue::ListLiteral(lowered)
        }

        _ => unreadable(expr),
    }
}

/// Resolves a path expression against the scope: a bound constructor parameter first, then a
/// module-level string constant, then — for a two-segment path — an associated `&str` const of
/// the named type.
///
/// Parameters win because they are the narrower binding — inside a constructor body an
/// identifier that shadows a module const refers to the parameter. ~keep
///
/// The two-segment case has to live here rather than only in the `Expr::Path` arm of
/// [`expr_to_default_value`], because `Self::DEFAULT_MODEL.to_string()` reaches the extractor as
/// a *method call* whose receiver is the path, and the method-call arm resolves its receiver
/// through this function. ~keep
fn resolve_ident(expr: &syn::Expr, scope: &EvalScope<'_>) -> Option<DefaultValue> {
    let syn::Expr::Path(path) = expr else {
        return None;
    };
    let segments: Vec<String> = path.path.segments.iter().map(|s| s.ident.to_string()).collect();
    match segments.as_slice() {
        [ident] => {
            if let Some(value) = scope.params.get(ident) {
                return Some(value.clone());
            }
            scope.literal_consts.get(ident).cloned()
        }
        [.., owner, name] => scope.associated_const(owner, name),
        [] => None,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn default_value_of(expr_src: &str) -> DefaultValue {
        default_value_of_with_consts(expr_src, &[])
    }

    fn default_value_of_with_consts(expr_src: &str, consts: &[(&str, &str)]) -> DefaultValue {
        let expr: syn::Expr = syn::parse_str(expr_src).expect("valid expr");
        let literal_consts: AHashMap<String, DefaultValue> = consts
            .iter()
            .map(|(k, v)| (k.to_string(), DefaultValue::StringLiteral(v.to_string())))
            .collect();
        let field_types = AHashMap::new();
        expr_to_default_value(&expr, &EvalScope::new("Subject", &literal_consts, &field_types), None)
    }

    #[test]
    fn some_int_literal_unwraps_to_inner_int() {
        assert_eq!(
            default_value_of("Some(50 * 1024 * 1024)"),
            DefaultValue::IntLiteral(52_428_800)
        );
    }

    #[test]
    fn some_string_literal_unwraps_to_inner_string() {
        assert_eq!(
            default_value_of(r#"Some("hi".to_string())"#),
            DefaultValue::StringLiteral("hi".to_string())
        );
    }

    #[test]
    fn qualified_option_some_unwraps() {
        assert_eq!(default_value_of("Option::Some(5)"), DefaultValue::IntLiteral(5));
    }

    #[test]
    fn bare_none_stays_none() {
        assert_eq!(default_value_of("None"), DefaultValue::None);
    }

    #[test]
    fn zero_argument_function_call_preserves_its_path() {
        assert_eq!(
            default_value_of("defaults::retry_limit()"),
            DefaultValue::FunctionCall("defaults::retry_limit".to_string())
        );
    }

    #[test]
    fn const_to_string_resolves_to_the_consts_literal_value() {
        assert_eq!(
            default_value_of_with_consts(
                "DEFAULT_CATALOG_URL.to_string()",
                &[("DEFAULT_CATALOG_URL", "https://example.com/catalog.json")]
            ),
            DefaultValue::StringLiteral("https://example.com/catalog.json".to_string())
        );
    }

    #[test]
    fn const_into_resolves_to_the_consts_literal_value() {
        assert_eq!(
            default_value_of_with_consts("HOST.into()", &[("HOST", "localhost")]),
            DefaultValue::StringLiteral("localhost".to_string())
        );
    }

    #[test]
    fn bare_const_path_resolves_to_the_consts_literal_value() {
        assert_eq!(
            default_value_of_with_consts("HOST", &[("HOST", "localhost")]),
            DefaultValue::StringLiteral("localhost".to_string())
        );
    }

    #[test]
    fn unresolvable_const_reference_is_unresolved_not_empty() {
        // No matching entry in `literal_consts`: alef does not know the value. `Empty` would
        // assert the default *is* the empty string, which for a const named `UNKNOWN_CONST` it
        // demonstrably is not.
        assert!(
            matches!(
                default_value_of("UNKNOWN_CONST.to_string()"),
                DefaultValue::Unresolved(_)
            ),
            "an unresolvable const reference must be reported, not silently zeroed"
        );
    }

    #[test]
    fn collect_literal_consts_collects_every_literal_kind_and_nothing_computed() {
        let file: syn::File = syn::parse_str(
            r#"
                pub const DEFAULT_CATALOG_URL: &str = "https://example.com/catalog.json";
                const CACHE_DIR_NAME: &str = "sample-crate";
                const RETRY_LIMIT: u32 = 3;
                const DET_DB_THRESH: f32 = 0.3;
                const VERBOSE: bool = false;
                const MIN_OFFSET: i32 = -5;
                const COMPUTED: &str = some_fn();
                const WINDOW: Duration = Duration::from_secs(5);
            "#,
        )
        .expect("valid file");

        let consts = collect_literal_consts(&file.items);

        assert_eq!(
            consts.get("DEFAULT_CATALOG_URL"),
            Some(&DefaultValue::StringLiteral(
                "https://example.com/catalog.json".to_string()
            ))
        );
        assert_eq!(
            consts.get("CACHE_DIR_NAME"),
            Some(&DefaultValue::StringLiteral("sample-crate".to_string()))
        );
        // A numeric const is exactly as readable as a string one, and leaving it out made alef
        // render `0` for the single most common unreadable-default shape in the consumer crates.
        assert_eq!(consts.get("RETRY_LIMIT"), Some(&DefaultValue::IntLiteral(3)));
        assert_eq!(consts.get("DET_DB_THRESH"), Some(&DefaultValue::FloatLiteral(0.3)));
        assert_eq!(consts.get("VERBOSE"), Some(&DefaultValue::BoolLiteral(false)));
        assert_eq!(consts.get("MIN_OFFSET"), Some(&DefaultValue::IntLiteral(-5)));
        assert_eq!(
            consts.get("COMPUTED"),
            None,
            "non-literal initializers must not be collected"
        );
        assert_eq!(
            consts.get("WINDOW"),
            None,
            "evaluating a const-fn initializer would be interpretation, not reading"
        );
    }

    /// `pub const ONE: Weight = Weight(1);` — a single-argument tuple-struct literal — folds to
    /// the literal it wraps. `DefaultValue` has no struct-shaped variant, so this is the only way
    /// `Self::ONE` inside `impl Default for Weight` can ever resolve to anything but `Unresolved`.
    #[test]
    fn collect_literal_consts_folds_a_tuple_struct_literal_to_its_inner_scalar() {
        let file: syn::File = syn::parse_str(
            r#"
                impl Weight {
                    pub const ONE: Weight = Weight(1);
                    pub const MAX: Weight = Weight(u32::MAX);
                }
            "#,
        )
        .expect("valid file");

        let consts = collect_literal_consts(&file.items);

        assert_eq!(consts.get("Weight::ONE"), Some(&DefaultValue::IntLiteral(1)));
        assert_eq!(
            consts.get("Weight::MAX"),
            None,
            "the inner expression `u32::MAX` is not itself a literal; guessing its value is worse \
             than leaving the const unindexed"
        );
    }

    /// A lower-case callee is a function by Rust convention, not a tuple-struct constructor, so
    /// folding `compute(3)` the same way as `Weight(1)` would be interpreting code, not reading
    /// a literal.
    #[test]
    fn collect_literal_consts_does_not_fold_a_lowercase_call_as_a_tuple_struct_literal() {
        let file: syn::File = syn::parse_str(r#"const RETRY_LIMIT: u32 = compute(3);"#).expect("valid file");

        let consts = collect_literal_consts(&file.items);

        assert_eq!(
            consts.get("RETRY_LIMIT"),
            None,
            "a snake_case call is a function invocation and must not be folded"
        );
    }

    /// Drive the whole extractor over a module source, returning the resolved defaults for
    /// the named type's `impl Default`. Reproduces exactly what `extractor::mod` does: build
    /// the const and constructor indexes from the module's items, then read the `impl Default`
    /// against them.
    fn defaults_for(source: &str, type_name: &str, field_names: &[&str]) -> Vec<(String, DefaultValue)> {
        let fields: Vec<(&str, TypeRef)> = field_names.iter().map(|name| (*name, TypeRef::Unit)).collect();
        defaults_for_typed(source, type_name, &fields)
    }

    /// As [`defaults_for`], but with each field's declared type spelled out. Only the two-segment
    /// path lowering consults it, so every other case can keep using the untyped helper. ~keep
    fn defaults_for_typed(source: &str, type_name: &str, fields: &[(&str, TypeRef)]) -> Vec<(String, DefaultValue)> {
        let file: syn::File = syn::parse_str(source).expect("valid module source");
        let literal_consts = collect_literal_consts(&file.items);
        let constructors = collect_constructors(&file.items);

        let default_impl = file
            .items
            .iter()
            .find_map(|item| match item {
                syn::Item::Impl(item_impl)
                    if item_impl
                        .trait_
                        .as_ref()
                        .is_some_and(|(path, _)| path.segments.last().is_some_and(|s| s.ident == "Default"))
                        && path_type_name(&item_impl.self_ty).as_deref() == Some(type_name) =>
                {
                    Some(item_impl)
                }
                _ => None,
            })
            .expect("module declares `impl Default` for the type");

        let mut fields: Vec<FieldDef> = fields
            .iter()
            .map(|(name, ty)| FieldDef {
                name: (*name).to_string(),
                ty: ty.clone(),
                ..Default::default()
            })
            .collect();

        extract_default_values(default_impl, type_name, &mut fields, &literal_consts, &constructors);

        fields
            .into_iter()
            .map(|field| {
                let value = field.typed_default.expect("every field is assigned a default");
                (field.name, value)
            })
            .collect()
    }

    /// The reported defect, reduced. `PaddleOcrConfig` really is
    /// `impl Default { fn default() -> Self { Self::new("en") } }`, and before this the
    /// extractor wrote `Empty` to all seven fields, which C#, Java, Kotlin, Swift, Python and
    /// Go each rendered as their own type-zero — `0.0f` for `det_db_thresh`, sitting under a
    /// generated doc comment reading "(default: 0.3)". ~keep
    #[test]
    fn a_default_delegating_to_a_constructor_recovers_the_constructors_literals() {
        let resolved = defaults_for(
            r#"
                pub struct PaddleOcrConfig {
                    pub language: String,
                    pub det_db_thresh: f32,
                    pub det_limit_side_len: u32,
                    pub use_angle_cls: bool,
                }

                impl PaddleOcrConfig {
                    pub fn new(language: &str) -> Self {
                        Self {
                            language: language.to_string(),
                            det_db_thresh: 0.3,
                            det_limit_side_len: 1024,
                            use_angle_cls: true,
                        }
                    }
                }

                impl Default for PaddleOcrConfig {
                    fn default() -> Self {
                        Self::new("en")
                    }
                }
            "#,
            "PaddleOcrConfig",
            &["language", "det_db_thresh", "det_limit_side_len", "use_angle_cls"],
        );

        assert_eq!(
            resolved,
            vec![
                ("language".to_string(), DefaultValue::StringLiteral("en".to_string())),
                ("det_db_thresh".to_string(), DefaultValue::FloatLiteral(0.3)),
                ("det_limit_side_len".to_string(), DefaultValue::IntLiteral(1024)),
                ("use_angle_cls".to_string(), DefaultValue::BoolLiteral(true)),
            ],
            "a delegating `fn default()` must yield the constructor's literals, never a type-zero"
        );
    }

    /// The same recovery through the type's own name rather than `Self`, and through a
    /// constructor whose parameter is consumed by `.into()` rather than `.to_string()`.
    #[test]
    fn a_delegation_named_by_the_type_and_consumed_by_into_also_recovers() {
        let resolved = defaults_for(
            r#"
                pub struct Client { pub endpoint: String, pub retries: u32 }

                impl Client {
                    pub fn for_endpoint(endpoint: &str) -> Self {
                        Self { endpoint: endpoint.into(), retries: 5 }
                    }
                }

                impl Default for Client {
                    fn default() -> Self {
                        Client::for_endpoint("https://api.example.com")
                    }
                }
            "#,
            "Client",
            &["endpoint", "retries"],
        );

        assert_eq!(
            resolved,
            vec![
                (
                    "endpoint".to_string(),
                    DefaultValue::StringLiteral("https://api.example.com".to_string())
                ),
                ("retries".to_string(), DefaultValue::IntLiteral(5)),
            ]
        );
    }

    /// A delegation whose argument is a module const, resolved through the same const index
    /// the direct path already uses.
    #[test]
    fn a_delegation_passing_a_module_const_resolves_it() {
        let resolved = defaults_for(
            r#"
                const DEFAULT_LANG: &str = "en";

                pub struct Ocr { pub language: String }

                impl Ocr {
                    pub fn new(language: &str) -> Self {
                        Self { language: language.to_string() }
                    }
                }

                impl Default for Ocr {
                    fn default() -> Self { Self::new(DEFAULT_LANG) }
                }
            "#,
            "Ocr",
            &["language"],
        );

        assert_eq!(
            resolved,
            vec![("language".to_string(), DefaultValue::StringLiteral("en".to_string()))]
        );
    }

    /// Two hops, which the delegation follower is bounded to allow.
    #[test]
    fn a_delegation_chained_through_a_second_constructor_still_resolves() {
        let resolved = defaults_for(
            r#"
                pub struct Cfg { pub level: u32 }

                impl Cfg {
                    pub fn new() -> Self { Self::with_level(7) }
                    pub fn with_level(level: u32) -> Self { Self { level } }
                }

                impl Default for Cfg {
                    fn default() -> Self { Self::new() }
                }
            "#,
            "Cfg",
            &["level"],
        );

        assert_eq!(resolved, vec![("level".to_string(), DefaultValue::IntLiteral(7))]);
    }

    /// A mutually recursive constructor pair must terminate rather than blow the stack, and
    /// must report the failure instead of inventing values.
    #[test]
    fn a_cyclic_delegation_terminates_and_reports_unresolved() {
        let resolved = defaults_for(
            r#"
                pub struct Cfg { pub level: u32 }

                impl Cfg {
                    pub fn new() -> Self { Self::fresh() }
                    pub fn fresh() -> Self { Self::new() }
                }

                impl Default for Cfg {
                    fn default() -> Self { Self::new() }
                }
            "#,
            "Cfg",
            &["level"],
        );

        assert!(
            matches!(resolved.as_slice(), [(_, DefaultValue::Unresolved(_))]),
            "a cycle must resolve to `Unresolved`, got {resolved:?}"
        );
    }

    /// The honest boundary of the technique, pinned so nobody mistakes the fold for an
    /// interpreter. A constructor that *computes* a field is not followed, and the outcome is
    /// `Unresolved` — reported — rather than a type-zero.
    #[test]
    fn a_default_delegating_to_a_builder_is_unresolved_not_a_type_zero() {
        let resolved = defaults_for(
            r#"
                pub struct Cfg { pub level: u32 }

                impl Cfg {
                    pub fn builder() -> CfgBuilder { CfgBuilder::new() }
                }

                impl Default for Cfg {
                    fn default() -> Self { Self::builder().level(9).build() }
                }
            "#,
            "Cfg",
            &["level"],
        );

        assert!(
            matches!(resolved.as_slice(), [(_, DefaultValue::Unresolved(_))]),
            "an unfollowable body must be reported, not silently zeroed; got {resolved:?}"
        );
        assert_ne!(
            resolved[0].1,
            DefaultValue::Empty,
            "`Empty` would claim the default *is* the type-zero, which is the conflation this fixes"
        );
    }

    /// The general shape the reported `Weight` warning is an instance of: a single-field
    /// newtype (`pub struct Weight(pub u32)`) plus `pub const ONE: Weight = Weight(1);` plus
    /// `impl Default { fn default() -> Self { Self::ONE } }`. `Self::ONE` is neither a struct
    /// literal nor a delegation call — it names an associated const of `Self` — but `Weight` has
    /// exactly one field and the const's tuple-struct literal folds all the way down to `1`.
    ///
    /// Note: the *inner field* must be `pub` for a real `pub struct Weight(u32)` to reach this
    /// path through the full extractor — `extract_struct` (`extract::extractor::types`) only
    /// extracts a single-unnamed-field tuple struct's field when `is_pub` holds for that field's
    /// own visibility, not just the struct's. A consumer newtype whose inner field is *private*
    /// is extracted as a fully opaque type with zero `FieldDef`s — the warning
    /// still fires (this fn's `Unresolved` fallback logs unconditionally), but it is inert there:
    /// `unreadable_field_default_diagnostics` iterates `typ.fields`, which is empty, so no
    /// diagnostic and no wrong-`0` ever reaches a backend for that specific type today. This test
    /// exercises `extract_default_values` directly (as every sibling test in this file does,
    /// bypassing `extract_struct`'s field-visibility gate) to prove the fold works for the shape
    /// that *does* reach a backend: any single-field newtype whose one field is `pub`. ~keep
    #[test]
    fn a_single_field_types_associated_const_tail_folds_to_the_consts_scalar() {
        let resolved = defaults_for(
            r#"
                pub struct Weight(pub u32);

                impl Weight {
                    pub const ONE: Weight = Weight(1);
                }

                impl Default for Weight {
                    fn default() -> Self {
                        Self::ONE
                    }
                }
            "#,
            "Weight",
            &["_0"],
        );

        assert_eq!(
            resolved,
            vec![("_0".to_string(), DefaultValue::IntLiteral(1))],
            "a foldable associated-const tail must recover the real default, not `Unresolved` and not `0`"
        );
    }

    /// Negative control for the same shape: when the const's own initializer is not itself
    /// foldable (`Weight(u32::MAX)` — the inner expression is a path, not a literal), the field
    /// must stay `Unresolved`. Without this, the fix above could pass by making every
    /// `Self::NAME` tail fold to *something*, which is the failure mode that matters most here.
    #[test]
    fn an_associated_consts_unfoldable_inner_value_stays_unresolved_not_a_type_zero() {
        let resolved = defaults_for(
            r#"
                pub struct Weight(pub u32);

                impl Weight {
                    pub const MAX: Weight = Weight(u32::MAX);
                }

                impl Default for Weight {
                    fn default() -> Self {
                        Self::MAX
                    }
                }
            "#,
            "Weight",
            &["_0"],
        );

        assert!(
            matches!(resolved.as_slice(), [(_, DefaultValue::Unresolved(_))]),
            "an unfoldable const initializer must be reported, not guessed; got {resolved:?}"
        );
        assert_ne!(
            resolved[0].1,
            DefaultValue::IntLiteral(0),
            "collapsing to a zero would be silently wrong: `u32::MAX` is not `0`"
        );
    }

    /// The direct path is untouched: a `fn default()` that spells its own struct literal still
    /// reads exactly as before, including the per-field `Empty` for an initializer that is
    /// genuinely the type's zero.
    #[test]
    fn a_struct_literal_default_is_unchanged_and_keeps_empty_for_genuine_zeros() {
        let resolved = defaults_for(
            r#"
                pub struct Cfg { pub level: u32, pub tags: Vec<String> }

                impl Default for Cfg {
                    fn default() -> Self {
                        Self { level: 3, tags: Vec::new() }
                    }
                }
            "#,
            "Cfg",
            &["level", "tags"],
        );

        assert_eq!(
            resolved,
            vec![
                ("level".to_string(), DefaultValue::IntLiteral(3)),
                ("tags".to_string(), DefaultValue::Empty),
            ]
        );
    }

    /// An arity mismatch means the constructor index resolved something other than the
    /// function actually called; reading its body would invent values.
    #[test]
    fn a_delegation_with_mismatched_arity_is_unresolved() {
        let resolved = defaults_for(
            r#"
                pub struct Cfg { pub level: u32 }

                impl Cfg {
                    pub fn new(level: u32, name: &str) -> Self { Self { level } }
                }

                impl Default for Cfg {
                    fn default() -> Self { Self::new(4) }
                }
            "#,
            "Cfg",
            &["level"],
        );

        assert!(
            matches!(resolved.as_slice(), [(_, DefaultValue::Unresolved(_))]),
            "got {resolved:?}"
        );
    }

    /// A constructor parameter that is not a foldable literal must not be bound: binding a
    /// placeholder would put a guessed value in a field that reads the parameter. The field that
    /// *does* read it is reported unresolved rather than zeroed; its sibling is untouched.
    #[test]
    fn a_delegation_with_an_unfoldable_argument_reports_only_the_field_that_reads_it() {
        let resolved = defaults_for(
            r#"
                pub struct Cfg { pub name: String, pub level: u32 }

                impl Cfg {
                    pub fn new(name: &str) -> Self { Self { name: name.to_string(), level: 2 } }
                }

                impl Default for Cfg {
                    fn default() -> Self { Self::new(compute_name()) }
                }
            "#,
            "Cfg",
            &["name", "level"],
        );

        assert!(
            matches!(
                resolved.as_slice(),
                [
                    (name, DefaultValue::Unresolved(_)),
                    (level, DefaultValue::IntLiteral(2)),
                ] if name == "name" && level == "level"
            ),
            "the unfoldable argument must not poison the sibling field it does not reach, and the \
             field it does reach must be reported rather than zeroed; got {resolved:?}"
        );
    }

    #[test]
    fn collect_constructors_indexes_associated_fns_and_skips_methods_and_trait_impls() {
        let file: syn::File = syn::parse_str(
            r#"
                impl Cfg {
                    pub fn new() -> Self { Self {} }
                    pub fn tweak(&self) -> Self { Self {} }
                }
                impl Default for Cfg {
                    fn default() -> Self { Self::new() }
                }
            "#,
        )
        .expect("valid file");

        let constructors = collect_constructors(&file.items);

        assert!(constructors.contains_key(&("Cfg".to_string(), "new".to_string())));
        assert!(
            !constructors.contains_key(&("Cfg".to_string(), "tweak".to_string())),
            "a `&self` method cannot be reached by `Self::name(..)` in `fn default()`"
        );
        assert!(
            !constructors.contains_key(&("Cfg".to_string(), "default".to_string())),
            "trait impls must not be indexed as constructors"
        );
    }

    /// The parameter binding must not leak past the constructor it belongs to: a module const
    /// with the same name as a parameter is shadowed inside the callee, and the parameter's
    /// bound value is the one that applies.
    #[test]
    fn a_constructor_parameter_shadows_a_module_const_of_the_same_name() {
        let resolved = defaults_for(
            r#"
                const language: &str = "shadowed";

                pub struct Cfg { pub language: String }

                impl Cfg {
                    pub fn new(language: &str) -> Self { Self { language: language.to_string() } }
                }

                impl Default for Cfg {
                    fn default() -> Self { Self::new("en") }
                }
            "#,
            "Cfg",
            &["language"],
        );

        assert_eq!(
            resolved,
            vec![("language".to_string(), DefaultValue::StringLiteral("en".to_string()))]
        );
    }

    /// What `codegen::config_gen::shared` writes into a generated binding for this field, which
    /// is where the fabrication was observable: an `EnumVariant` on a `String`-typed field is
    /// rendered as the *snake-cased variant name*, so `EnumVariant("DEFAULT_MODEL")` shipped the
    /// string `"default_model"` — a value that appears nowhere in the source crate. ~keep
    fn rendered_python_default(name: &str, ty: TypeRef, value: &DefaultValue) -> String {
        let field = FieldDef {
            name: name.to_string(),
            ty,
            typed_default: Some(value.clone()),
            ..Default::default()
        };
        crate::codegen::config_gen::default_value_for_field(&field, "python")
    }

    /// The reported defect. `model: Self::DEFAULT_MODEL` is a two-segment path just like
    /// `Mode::Fast`, and the extractor lowered both to `EnumVariant`. On a `String` field that
    /// rendered as `"default_model"`.
    ///
    /// The const is readable, so the honest answer is not "unresolved" but the const's own value.
    #[test]
    fn an_associated_const_default_on_a_string_field_resolves_to_the_consts_value() {
        let resolved = defaults_for_typed(
            r#"
                pub struct LlmConfig { pub model: String }

                impl LlmConfig {
                    pub const DEFAULT_MODEL: &str = "claude-sonnet-4-5";
                }

                impl Default for LlmConfig {
                    fn default() -> Self {
                        Self { model: Self::DEFAULT_MODEL.to_string() }
                    }
                }
            "#,
            "LlmConfig",
            &[("model", TypeRef::String)],
        );

        assert_eq!(
            resolved,
            vec![(
                "model".to_string(),
                DefaultValue::StringLiteral("claude-sonnet-4-5".to_string())
            )]
        );
        assert_ne!(
            rendered_python_default("model", TypeRef::String, &resolved[0].1),
            "\"default_model\"",
            "the snake-cased const name is a fabricated value; it must not reach a binding"
        );
    }

    /// A bare `Self::CONST` — no `.to_string()` — takes the same route.
    #[test]
    fn a_bare_associated_const_path_resolves_through_the_owning_type() {
        let resolved = defaults_for_typed(
            r#"
                pub struct LlmConfig { pub base_url: String }

                impl LlmConfig {
                    const DEFAULT_BASE_URL: &'static str = "https://api.anthropic.com";
                }

                impl Default for LlmConfig {
                    fn default() -> Self {
                        Self { base_url: LlmConfig::DEFAULT_BASE_URL.into() }
                    }
                }
            "#,
            "LlmConfig",
            &[("base_url", TypeRef::String)],
        );

        assert_eq!(
            resolved,
            vec![(
                "base_url".to_string(),
                DefaultValue::StringLiteral("https://api.anthropic.com".to_string())
            )]
        );
    }

    /// The same shape with the const out of reach — declared in another module, or not a string
    /// literal at all. There is no value to recover, so the answer is `Unresolved`. What it must
    /// never be is an `EnumVariant`, because the field's declared type cannot hold one and the
    /// renderer would invent `"default_model"` from the const's name.
    #[test]
    fn an_unreachable_associated_const_on_a_string_field_is_unresolved_not_an_enum_variant() {
        let resolved = defaults_for_typed(
            r#"
                pub struct LlmConfig { pub model: String }

                impl Default for LlmConfig {
                    fn default() -> Self {
                        Self { model: Self::DEFAULT_MODEL.to_string() }
                    }
                }
            "#,
            "LlmConfig",
            &[("model", TypeRef::String)],
        );

        let value = &resolved[0].1;
        assert!(
            matches!(value, DefaultValue::Unresolved(_)),
            "an unreadable initializer must be reported, got {value:?}"
        );
        assert_ne!(
            value,
            &DefaultValue::EnumVariant("DEFAULT_MODEL".to_string()),
            "a `String` field cannot hold an enum variant, so this lowering was never sound"
        );
        assert_ne!(
            rendered_python_default("model", TypeRef::String, value),
            "\"default_model\"",
            "the fabricated snake-cased const name must be absent from generated output"
        );
    }

    /// The control for the fix: a two-segment path on a field that really is enum-typed must
    /// still lower to `EnumVariant`. Breaking this would make every genuine enum default
    /// unresolved and arm the refusal across the fleet.
    #[test]
    fn a_genuine_enum_variant_default_still_lowers_to_an_enum_variant() {
        let resolved = defaults_for_typed(
            r#"
                pub struct Cfg { pub mode: Mode, pub fallback: Option<Mode>, pub stages: Vec<Mode> }

                impl Default for Cfg {
                    fn default() -> Self {
                        Self {
                            mode: Mode::Fast,
                            fallback: Some(Mode::Slow),
                            stages: vec![Mode::Fast, Mode::Slow],
                        }
                    }
                }
            "#,
            "Cfg",
            &[
                ("mode", TypeRef::Named("Mode".to_string())),
                (
                    "fallback",
                    TypeRef::Optional(Box::new(TypeRef::Named("Mode".to_string()))),
                ),
                ("stages", TypeRef::Vec(Box::new(TypeRef::Named("Mode".to_string())))),
            ],
        );

        assert_eq!(
            resolved,
            vec![
                ("mode".to_string(), DefaultValue::EnumVariant("Fast".to_string())),
                ("fallback".to_string(), DefaultValue::EnumVariant("Slow".to_string())),
                (
                    "stages".to_string(),
                    DefaultValue::ListLiteral(vec![
                        DefaultValue::EnumVariant("Fast".to_string()),
                        DefaultValue::EnumVariant("Slow".to_string()),
                    ])
                ),
            ],
            "an enum-typed field — bare, optional or in a list — must keep its variant default"
        );
    }

    /// Two types in one module may each declare a const of the same name. Keying the index by
    /// the owning type is what stops one from answering for the other, which would substitute a
    /// value that is wrong rather than merely missing.
    #[test]
    fn an_associated_const_of_another_type_does_not_answer_for_this_one() {
        let resolved = defaults_for_typed(
            r#"
                pub struct Other { pub model: String }
                pub struct LlmConfig { pub model: String }

                impl Other {
                    pub const DEFAULT_MODEL: &str = "not-this-one";
                }

                impl Default for LlmConfig {
                    fn default() -> Self {
                        Self { model: Self::DEFAULT_MODEL.to_string() }
                    }
                }
            "#,
            "LlmConfig",
            &[("model", TypeRef::String)],
        );

        assert!(
            matches!(resolved.as_slice(), [(_, DefaultValue::Unresolved(_))]),
            "a same-named const on a different type must not be substituted; got {resolved:?}"
        );
    }

    /// The field-granular half of the `Empty`/`Unresolved` split: an initializer alef cannot read
    /// inside an otherwise-readable struct literal. Each of these previously wrote `Empty`, which
    /// licensed every backend to emit its own type-zero for a value it had never read.
    #[test]
    fn an_unreadable_field_initializer_is_unresolved_not_empty() {
        let resolved = defaults_for(
            r#"
                pub struct Cfg {
                    pub threshold: f32,
                    pub name: String,
                    pub root: PathBuf,
                    pub window: [u32; 2],
                    pub mode: u8,
                }

                impl Default for Cfg {
                    fn default() -> Self {
                        Self {
                            threshold: compute().clamp(0.0, 1.0),
                            name: make_name(1, 2),
                            root: PathBuf::from("/tmp"),
                            window: [1, 2],
                            mode: if cfg!(unix) { 1 } else { 2 },
                        }
                    }
                }
            "#,
            "Cfg",
            &["threshold", "name", "root", "window", "mode"],
        );

        for (name, value) in &resolved {
            assert!(
                matches!(value, DefaultValue::Unresolved(_)),
                "`{name}` is not readable, so it must be reported rather than zeroed; got {value:?}"
            );
        }
    }

    /// The control that must survive the relabelling. `Empty` still means "the default *is* this
    /// type's zero", and these three initializers still assert exactly that. Widening
    /// `Unresolved` over them would arm the refusal on every crate in the fleet.
    #[test]
    fn genuine_type_zero_initializers_stay_empty() {
        let resolved = defaults_for(
            r#"
                pub struct Cfg {
                    pub tags: Vec<String>,
                    pub index: AHashMap<String, u32>,
                    pub count: u32,
                    pub stages: Vec<String>,
                }

                impl Default for Cfg {
                    fn default() -> Self {
                        Self {
                            tags: Vec::new(),
                            index: AHashMap::new(),
                            count: u32::default(),
                            stages: vec![],
                        }
                    }
                }
            "#,
            "Cfg",
            &["tags", "index", "count", "stages"],
        );

        assert_eq!(
            resolved,
            vec![
                ("tags".to_string(), DefaultValue::Empty),
                ("index".to_string(), DefaultValue::Empty),
                ("count".to_string(), DefaultValue::Empty),
                ("stages".to_string(), DefaultValue::Empty),
            ],
            "a known type-zero must stay `Empty`; only an unread value becomes `Unresolved`"
        );
    }

    /// The dominant unreadable-default shape in the consumer crates, measured across every repo
    /// with an `alef.toml`: a field initialized from a module-level const of non-`&str` type.
    /// Nine of the eighteen would-be-unresolved fields fleet-wide are exactly this, and two of
    /// them already ship as `0` in generated Python against Rust values of `1024` and `6`. ~keep
    #[test]
    fn a_module_const_of_any_literal_type_resolves_to_its_value() {
        let resolved = defaults_for(
            r#"
                const DEFAULT_DETECTION_LIMIT_SIDE_LEN: u32 = 1024;
                const DEFAULT_RECOGNITION_BATCH_SIZE: usize = 6;
                const DEFAULT_DB_THRESH: f32 = 0.3;
                const DEFAULT_VERBOSE: bool = true;

                pub struct PaddleOcrConfig {
                    pub det_limit_side_len: u32,
                    pub rec_batch_num: usize,
                    pub det_db_thresh: f32,
                    pub verbose: bool,
                }

                impl Default for PaddleOcrConfig {
                    fn default() -> Self {
                        Self {
                            det_limit_side_len: DEFAULT_DETECTION_LIMIT_SIDE_LEN,
                            rec_batch_num: DEFAULT_RECOGNITION_BATCH_SIZE,
                            det_db_thresh: DEFAULT_DB_THRESH,
                            verbose: DEFAULT_VERBOSE,
                        }
                    }
                }
            "#,
            "PaddleOcrConfig",
            &["det_limit_side_len", "rec_batch_num", "det_db_thresh", "verbose"],
        );

        assert_eq!(
            resolved,
            vec![
                ("det_limit_side_len".to_string(), DefaultValue::IntLiteral(1024)),
                ("rec_batch_num".to_string(), DefaultValue::IntLiteral(6)),
                ("det_db_thresh".to_string(), DefaultValue::FloatLiteral(0.3)),
                ("verbose".to_string(), DefaultValue::BoolLiteral(true)),
            ],
            "a numeric module const is readable; substituting the type-zero for it is the same \
             fabrication as substituting one for an unread default"
        );
    }

    /// A variant may be named through any number of module segments. Only the last segment is the
    /// variant, and stopping at exactly two made three fleet-wide enum defaults unreadable.
    #[test]
    fn a_fully_qualified_enum_path_still_lowers_to_its_last_segment() {
        let resolved = defaults_for_typed(
            r#"
                pub struct ExtractionConfig { pub result_format: ResultFormat }

                impl Default for ExtractionConfig {
                    fn default() -> Self {
                        Self { result_format: crate::types::ResultFormat::Unified }
                    }
                }
            "#,
            "ExtractionConfig",
            &[("result_format", TypeRef::Named("ResultFormat".to_string()))],
        );

        assert_eq!(
            resolved,
            vec![(
                "result_format".to_string(),
                DefaultValue::EnumVariant("Unified".to_string())
            )]
        );
    }

    /// `Cow` is a representation the binding layer already erases via `FieldDef::core_wrapper`,
    /// so the value is whatever it wraps. Reading through it is not a guess, and refusing to
    /// would turn a field that generates the correct `""` today into a generation error.
    #[test]
    fn a_cow_wrapped_literal_resolves_to_the_literal_it_wraps() {
        let resolved = defaults_for_typed(
            r#"
                pub struct ProcessConfig { pub language: Cow<'static, str>, pub tag: Cow<'static, str> }

                impl Default for ProcessConfig {
                    fn default() -> Self {
                        Self {
                            language: Cow::Borrowed(""),
                            tag: std::borrow::Cow::Borrowed("stable"),
                        }
                    }
                }
            "#,
            "ProcessConfig",
            &[("language", TypeRef::String), ("tag", TypeRef::String)],
        );

        assert_eq!(
            resolved,
            vec![
                ("language".to_string(), DefaultValue::StringLiteral(String::new())),
                ("tag".to_string(), DefaultValue::StringLiteral("stable".to_string())),
            ]
        );
    }

    /// The boundary the `Cow` reading must not cross: a `Cow` around something alef cannot read
    /// is still unread.
    #[test]
    fn a_cow_wrapping_an_unreadable_expression_stays_unresolved() {
        let resolved = defaults_for_typed(
            r#"
                pub struct ProcessConfig { pub language: Cow<'static, str> }

                impl Default for ProcessConfig {
                    fn default() -> Self {
                        Self { language: Cow::Owned(detect_language()) }
                    }
                }
            "#,
            "ProcessConfig",
            &[("language", TypeRef::String)],
        );

        assert!(
            matches!(resolved.as_slice(), [(_, DefaultValue::Unresolved(_))]),
            "got {resolved:?}"
        );
    }

    #[test]
    fn collect_literal_consts_indexes_associated_consts_under_their_owning_type() {
        let file: syn::File = syn::parse_str(
            r#"
                impl LlmConfig {
                    pub const DEFAULT_MODEL: &str = "claude-sonnet-4-5";
                    pub const MAX_TOKENS: u32 = 4096;
                }
                impl Default for LlmConfig {
                    const NOT_A_CONSTRUCTOR: &str = "trait-impl";
                    fn default() -> Self { Self {} }
                }
                #[cfg(test)]
                impl LlmConfig {
                    pub const DEFAULT_MODEL: &str = "test-only";
                }
            "#,
        )
        .expect("valid file");

        let consts = collect_literal_consts(&file.items);

        assert_eq!(
            consts.get("LlmConfig::DEFAULT_MODEL"),
            Some(&DefaultValue::StringLiteral("claude-sonnet-4-5".to_string())),
            "a `#[cfg(test)]` impl must not shadow the real associated const"
        );
        assert_eq!(
            consts.get("LlmConfig::MAX_TOKENS"),
            Some(&DefaultValue::IntLiteral(4096))
        );
        assert_eq!(
            consts
                .get("Default::NOT_A_CONSTRUCTOR")
                .or(consts.get("LlmConfig::NOT_A_CONSTRUCTOR")),
            None,
            "trait-impl associated consts are not inherent consts of the type"
        );
    }

    /// The reported defect, reduced with synthetic names. A tagged enum used as a field default
    /// via its struct variant — the ordinary way to spell "the default is this preset" — must
    /// fold into its own field values rather than leaving the whole field `Unresolved`.
    #[test]
    fn a_struct_variant_default_folds_with_its_field_values() {
        let resolved = defaults_for_typed(
            r#"
                pub struct Cfg { pub kind: Kind }

                impl Default for Cfg {
                    fn default() -> Self {
                        Self { kind: Kind::Curated { label: "balanced".to_string(), weight: 3 } }
                    }
                }
            "#,
            "Cfg",
            &[("kind", TypeRef::Named("Kind".to_string()))],
        );

        assert_eq!(
            resolved,
            vec![(
                "kind".to_string(),
                DefaultValue::StructVariant(
                    "Curated".to_string(),
                    vec![
                        ("label".to_string(), DefaultValue::StringLiteral("balanced".to_string())),
                        ("weight".to_string(), DefaultValue::IntLiteral(3)),
                    ],
                ),
            )],
            "a struct-variant enum default must fold into its own field values, not stay Unresolved"
        );
    }

    /// A tuple-variant enum default folds the same way, keyed by argument position rather than
    /// field name.
    #[test]
    fn a_tuple_variant_default_folds_with_its_argument_values() {
        let resolved = defaults_for_typed(
            r#"
                pub struct Cfg { pub kind: Kind }

                impl Default for Cfg {
                    fn default() -> Self {
                        Self { kind: Kind::Scaled(5, "x".to_string()) }
                    }
                }
            "#,
            "Cfg",
            &[("kind", TypeRef::Named("Kind".to_string()))],
        );

        assert_eq!(
            resolved,
            vec![(
                "kind".to_string(),
                DefaultValue::TupleVariant(
                    "Scaled".to_string(),
                    vec![
                        DefaultValue::IntLiteral(5),
                        DefaultValue::StringLiteral("x".to_string())
                    ],
                ),
            )],
            "a tuple-variant enum default must fold into its own argument values, not stay Unresolved"
        );
    }

    /// The control: a bare unit-variant path already folded to `EnumVariant` before this change
    /// (see `a_genuine_enum_variant_default_still_lowers_to_an_enum_variant`); this pins that a
    /// unit variant reached as the sole field of a struct literal is unaffected by adding
    /// `TupleVariant`/`StructVariant`.
    #[test]
    fn a_unit_variant_default_still_folds_to_a_bare_enum_variant() {
        let resolved = defaults_for_typed(
            r#"
                pub struct Cfg { pub kind: Kind }

                impl Default for Cfg {
                    fn default() -> Self {
                        Self { kind: Kind::Auto }
                    }
                }
            "#,
            "Cfg",
            &[("kind", TypeRef::Named("Kind".to_string()))],
        );

        assert_eq!(
            resolved,
            vec![("kind".to_string(), DefaultValue::EnumVariant("Auto".to_string()))],
            "a unit-variant default already folded before this change and must keep doing so"
        );
    }

    /// The invariant this whole feature exists to protect: a struct variant with even one
    /// unfoldable field must stay `Unresolved` as a whole, never `Empty` and never a
    /// partially-known payload that silently drops the field it could not read.
    #[test]
    fn a_struct_variants_unfoldable_field_keeps_the_whole_default_unresolved_not_empty() {
        let resolved = defaults_for_typed(
            r#"
                pub struct Cfg { pub kind: Kind }

                impl Default for Cfg {
                    fn default() -> Self {
                        Self { kind: Kind::Curated { label: compute_label() } }
                    }
                }
            "#,
            "Cfg",
            &[("kind", TypeRef::Named("Kind".to_string()))],
        );

        assert!(
            matches!(resolved.as_slice(), [(_, DefaultValue::Unresolved(_))]),
            "an unfoldable inner field must leave the whole variant default Unresolved; got {resolved:?}"
        );
        assert_ne!(
            resolved[0].1,
            DefaultValue::Empty,
            "collapsing an unfoldable struct-variant field to `Empty` is the conflation this fixes"
        );
    }

    /// The same invariant for the tuple-variant fold.
    #[test]
    fn a_tuple_variants_unfoldable_argument_keeps_the_whole_default_unresolved_not_empty() {
        let resolved = defaults_for_typed(
            r#"
                pub struct Cfg { pub kind: Kind }

                impl Default for Cfg {
                    fn default() -> Self {
                        Self { kind: Kind::Scaled(compute_scale()) }
                    }
                }
            "#,
            "Cfg",
            &[("kind", TypeRef::Named("Kind".to_string()))],
        );

        assert!(
            matches!(resolved.as_slice(), [(_, DefaultValue::Unresolved(_))]),
            "an unfoldable argument must leave the whole variant default Unresolved; got {resolved:?}"
        );
    }

    /// A `..base` spread inside a struct-variant literal could carry fields this pass never
    /// saw (`Kind::Curated { label: "x".to_string(), ..Default::default() }` might have more
    /// fields than `label` alone). Folding without accounting for `rest` would be a guess, so
    /// the whole expression stays `Unresolved` instead.
    #[test]
    fn a_struct_variant_with_a_rest_base_stays_unresolved() {
        let resolved = defaults_for_typed(
            r#"
                pub struct Cfg { pub kind: Kind }

                impl Default for Cfg {
                    fn default() -> Self {
                        Self { kind: Kind::Curated { label: "x".to_string(), ..Default::default() } }
                    }
                }
            "#,
            "Cfg",
            &[("kind", TypeRef::Named("Kind".to_string()))],
        );

        assert!(
            matches!(resolved.as_slice(), [(_, DefaultValue::Unresolved(_))]),
            "a `..base` spread can carry fields this pass never saw; folding without it would guess; \
             got {resolved:?}"
        );
    }
}