pklr 5.2.2

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

use super::*;

/// What a module inherits from the module it amends or extends.
#[derive(Default)]
struct ModuleBase {
    /// The base module's members: this module's starting values.
    members: ObjectMap,
    /// The base's properties, evaluated again once this module's overrides
    /// are in scope.
    late_properties: Vec<Arc<Property>>,
    /// The base module's members, which `super` reads.
    super_members: Option<ObjectMap>,
}

/// The members a module body evaluated.
struct ModuleMembers<'b> {
    /// The classes, and type aliases to classes, the body declares.
    class_names: std::collections::HashSet<Name>,
    /// The non-local properties that evaluated, by name.
    evaluated: HashMap<&'b str, Value>,
    /// The members that failed, with their errors.
    failed: HashMap<&'b str, String>,
}

impl Evaluator {
    pub(super) fn eval_module(
        &mut self,
        module: &Module,
        path: &Path,
        depth: usize,
    ) -> Result<Value> {
        self.eval_module_with_scope(module, path, depth, None, None)
    }

    pub(super) fn layer_evaluated_module_scope(&self, path: &Path, scope: &mut Scope) {
        let Some(inherited) = self.module_scopes.get(path) else {
            return;
        };
        for (name, value) in &inherited.values {
            if &**name != "this" && &**name != "module" {
                scope.set_name(name.clone(), value.clone());
            }
        }
        for (name, ty) in &inherited.type_aliases {
            scope.set_type_alias(name.clone(), ty.clone());
        }
    }

    pub(super) fn inherited_late_properties(&self, path: &Path) -> Vec<Arc<Property>> {
        self.module_scopes
            .get(path)
            .map(|snapshot| snapshot.late_properties.clone())
            .unwrap_or_default()
    }

    /// The member view a derived module exposes through `super`.
    ///
    /// A module's rendered value omits hidden properties and methods, but Pkl
    /// makes hidden properties available through `super`. Use the completed
    /// module scope and its declared properties instead of its rendered object
    /// so classes, locals, and output declarations remain absent. A module
    /// being amended cannot invoke base supermethods, unlike one extending it.
    pub(super) fn module_super_members(
        &self,
        path: &Path,
        fallback: &ObjectMap,
        include_methods: bool,
    ) -> ObjectMap {
        let Some(snapshot) = self.module_scopes.get(path) else {
            return fallback.clone();
        };

        let mut members = ObjectMap::default();
        for prop in &snapshot.late_properties {
            if prop.is_method && !include_methods {
                continue;
            }
            if let Some(value) = snapshot.values.get(prop.name.as_ref()) {
                members.insert(prop.name.clone(), value.clone());
            }
        }
        members
    }

    pub(super) fn eval_module_with_scope(
        &mut self,
        module: &Module,
        path: &Path,
        depth: usize,
        inherited_scope: Option<Scope>,
        requested_fields: Option<HashSet<Name>>,
    ) -> Result<Value> {
        if depth > self.max_depth {
            return Err(Error::Eval(format!(
                "max import depth {} exceeded",
                self.max_depth
            )));
        }
        self.check_cancelled()?;
        self.prefetch_remote_imports(module, path);
        if let Some(name) = &module.name {
            let identity = self.module_type_namespace(path);
            self.module_names.insert(identity, name.clone());
        }
        if module
            .body
            .iter()
            .any(|entry| matches!(entry, Entry::Elem(_)))
        {
            return Err(Error::Eval("Invalid property definition".into()));
        }
        let type_namespace = self.module_type_namespace(path);
        let mut scope = Scope {
            type_namespace: Some(type_namespace),
            ..Scope::default()
        };
        seed_builtins(&mut scope);
        // Evaluated as the base of an amending or extending module, whose
        // reads of this module's bindings are not analyzed here.
        let evaluated_as_base = inherited_scope.is_some();
        if let Some(inherited_scope) = inherited_scope {
            for (key, value) in inherited_scope.flatten() {
                scope.set_name(key, value);
            }
            for (key, ty) in inherited_scope.flatten_type_aliases() {
                scope.set_type_alias(key, ty);
            }
            for (key, identity) in inherited_scope.flatten_module_identities() {
                scope.set_module_identity(key, identity);
            }
        }
        let mut inherited_type_names = HashSet::default();
        let inherited_references =
            self.inherited_reference_roots(module, path, depth + 1, &mut inherited_type_names)?;
        // Checks here also resolve the type aliases of the modules this one
        // amends or extends, which may redefine a built-in.
        let inherited_builtins: Vec<&str> = BINDING_BUILTIN_TYPES
            .iter()
            .copied()
            .filter(|name| inherited_type_names.contains(*name))
            .collect();
        let requested_output_fields = requested_fields
            .as_ref()
            .map(|fields| expand_requested_fields(&module.body, fields, &inherited_builtins));
        let analysis_entries =
            analysis_entries_for_requested_fields(&module.body, requested_output_fields.as_ref());
        let mut referenced_imports = referenced_roots(&analysis_entries);
        referenced_imports.extend(inherited_references);
        let import_field_uses = import_field_uses(&analysis_entries);

        let inherited_local_paths: Vec<_> = module
            .amends
            .iter()
            .chain(module.extends.iter())
            .filter(|uri| local_module_path(path, uri).is_some())
            .map(|uri| self.local_file_path(path, uri))
            .collect::<Result<_>>()?;
        let deferred_inherited_imports = self.bind_imports(
            module,
            path,
            depth,
            evaluated_as_base,
            &mut scope,
            &referenced_imports,
            &import_field_uses,
            &inherited_local_paths,
        )?;

        let mut base = ModuleBase::default();
        self.amend_base_module(
            module,
            path,
            depth,
            &mut scope,
            &deferred_inherited_imports,
            &mut base,
        )?;

        self.bind_amended_base_classes(module, path, depth, &mut scope, &mut base)?;

        self.extend_base_module(
            module,
            path,
            depth,
            &mut scope,
            &deferred_inherited_imports,
            &mut base,
        )?;
        let ModuleBase {
            members: base_obj,
            late_properties: late_inherited_properties,
            mut super_members,
        } = base;

        if (module.amends.is_some() || module.extends.is_some()) && super_members.is_none() {
            super_members = Some(base_obj.clone());
        }
        let super_bound = super_members.is_some();
        if let Some(members) = super_members {
            scope.set("super", Value::Object(Arc::new(members), None));
        }
        // Properties of an amending module are object members. Their modifier
        // applicability is a declaration error, so it must not depend on
        // whether a narrowed import evaluates that property successfully.
        if module.amends.is_some() {
            for entry in module.body.iter() {
                let Entry::Property(prop) = entry else {
                    continue;
                };
                if has_modifier(&prop.modifiers, Modifier::Local) {
                    continue;
                }
                if has_modifier(&prop.modifiers, Modifier::Fixed) {
                    return Err(Error::Eval(
                        "Modifier `fixed` is not applicable to object members.".into(),
                    ));
                }
                if has_modifier(&prop.modifiers, Modifier::Const) {
                    return Err(Error::Eval(
                        "Modifier `const` can only be applied to object members that are also `local`."
                            .into(),
                    ));
                }
            }
        }
        // An amending module cannot repeat a non-local property's type
        // annotation. Keep the inherited declaration on its replacement so
        // an untyped `new` continues to amend the base property's default
        // rather than constructing a Dynamic object. This is the module
        // counterpart of `inherit_property_types` for classes.
        let typed_body = module.amends.as_ref().and_then(|_| {
            self.inherit_module_property_types(&module.body, &late_inherited_properties, &mut scope)
        });
        let body = typed_body.as_ref().unwrap_or(&module.body);
        // These annotations were reconstructed from the amended module, not
        // written in this body. They guide `new` and body evaluation above.
        // A failed check remains attached to its member rather than eagerly
        // rejecting an import that reads a different property, matching Pkl's
        // lazy evaluation of inherited property values. A constraint that
        // cannot yet be evaluated stays lazy too.
        let synthesized_inherited_types: HashSet<&str> = typed_body
            .as_ref()
            .map(|typed| {
                module
                    .body
                    .iter()
                    .zip(typed.iter())
                    .filter_map(|(original, typed)| match (original, typed) {
                        (Entry::Property(original), Entry::Property(typed))
                            if original.type_ann.is_none() && typed.type_ann.is_some() =>
                        {
                            Some(typed.name.as_ref())
                        }
                        _ => None,
                    })
                    .collect()
            })
            .unwrap_or_default();

        let requested_eval_fields = requested_output_fields.as_ref().map(|fields| {
            let mut dependency_entries = late_inherited_properties
                .iter()
                .cloned()
                .map(Entry::Property)
                .collect::<Vec<_>>();
            dependency_entries.extend(body.iter().cloned());
            expand_requested_fields(&dependency_entries, fields, &inherited_builtins)
        });

        // Inherited members are visible by name and through `this`/`module`
        // from the start; this module's members join them as they evaluate.
        let mut all_props = Arc::new(base_obj.clone());
        for (k, v) in &base_obj {
            scope.set(k, v.clone());
        }
        scope.set("this", Value::Object(Arc::clone(&all_props), None));
        scope.set("module", Value::Object(Arc::clone(&all_props), None));

        let ModuleMembers {
            class_names,
            mut evaluated,
            failed,
        } = self.eval_module_members(
            module,
            path,
            body,
            depth,
            &mut scope,
            &mut all_props,
            &base_obj,
            &late_inherited_properties,
            &requested_eval_fields,
            &synthesized_inherited_types,
        )?;

        // The planner may evaluate an annotation class or alias after the
        // annotated property. Validate once the final module scope is ready.
        check_module_annotations(module, &scope)?;

        // Assemble the module object in declaration order: inherited members,
        // then classes (exported so they're accessible via dotted paths, e.g.
        // `import "helpers.pkl"` → `helpers.ClassName`), then properties. A
        // failed property that would be rendered fails the module.
        let mut out = base_obj;
        let mut poisoned_members = IndexMap::new();
        for entry in body.iter() {
            // A typealias to a class is a module member too, so an importer
            // can write `new Alias.Name {}`.
            let exported = match entry {
                Entry::ClassDef(name, ..) => Some(name),
                Entry::TypeAlias(name, _) if class_names.contains(name.as_str()) => Some(name),
                _ => None,
            };
            if let Some(name) = exported {
                if let Some(value) = scope.get(name) {
                    out.insert(name.as_str().into(), value.clone());
                } else if let Some(message) = failed.get(name.as_str()) {
                    poisoned_members.insert(
                        nm(name),
                        PoisonedMember {
                            message: message.clone(),
                            rendered: false,
                        },
                    );
                }
            }
        }
        for entry in body.iter() {
            let Entry::Property(prop) = entry else {
                continue;
            };
            // Locals participate in failure convergence but are never module
            // members or rendered output.
            if has_modifier(&prop.modifiers, Modifier::Local) {
                continue;
            }
            let rendered = !has_modifier(&prop.modifiers, Modifier::Hidden)
                && requested_output_fields
                    .as_ref()
                    .is_none_or(|fields| fields.contains(&prop.name));
            if let Some(message) = failed.get(prop.name.as_ref()) {
                if rendered && depth == 0 {
                    return Err(Error::Eval(message.clone()));
                }
                self.rendered_member_failed |= rendered;
                poisoned_members.insert(
                    prop.name.clone(),
                    PoisonedMember {
                        message: message.clone(),
                        rendered,
                    },
                );
                continue;
            }
            if let Some(value) = evaluated.remove(prop.name.as_ref())
                && rendered
                && (depth > 0 || should_render_property_value(prop, &value))
            {
                out.insert(prop.name.as_ref().into(), value);
            }
        }

        // Pkl properties are late-bound. Re-evaluate inherited expressions after
        // child overrides have populated the scope (for example `uses` derived
        // from an action module's overridden `action` and `version`).
        let child_property_names: HashSet<&str> = body
            .iter()
            .filter_map(|entry| match entry {
                Entry::Property(prop) => Some(prop.name.as_ref()),
                _ => None,
            })
            .collect();
        if depth == 0
            && !module_is_abstract(module)
            && let Some(prop) = late_inherited_properties.iter().find(|prop| {
                has_modifier(&prop.modifiers, Modifier::Abstract)
                    && prop.value.is_none()
                    && prop.body.is_none()
                    && !child_property_names.contains(prop.name.as_ref())
            })
        {
            return Err(Error::Eval(format!(
                "abstract property '{}' must be assigned a value in {}",
                prop.name,
                path.display()
            )));
        }
        let child_candidates = body
            .iter()
            .filter_map(|entry| match entry {
                Entry::Property(prop)
                    if &*prop.name != "output" && (prop.value.is_some() || prop.body.is_some()) =>
                {
                    Some(prop)
                }
                _ => None,
            })
            .collect::<Vec<_>>();
        let mut late_bound_names = late_inherited_properties
            .iter()
            .map(|prop| prop.name.clone())
            .collect::<HashSet<_>>();
        // A body amending an inherited object is late-bound like the inherited
        // expression it amends: the object is rebuilt once overrides settle.
        let composed_amendments: Vec<Arc<Property>> = child_candidates
            .iter()
            .filter(|prop| prop.body.is_some() && prop.value.is_none())
            .filter_map(|prop| {
                let inherited = late_inherited_properties
                    .iter()
                    .rfind(|inherited| inherited.name == prop.name && inherited.value.is_some())?;
                let composed = compose_amended_object(inherited, prop, None);
                composed.value.is_some().then_some(composed)
            })
            .collect();
        let mut late_child_properties: Vec<&Property> = Vec::new();
        for composed in &composed_amendments {
            late_bound_names.insert(composed.name.clone());
            late_child_properties.push(composed);
        }
        loop {
            let mut added = false;
            for prop in &child_candidates {
                if late_child_properties
                    .iter()
                    .any(|selected: &&Property| selected.name == prop.name)
                {
                    continue;
                }
                let refs = property_reference_names(prop);
                if refs.iter().any(|name| late_bound_names.contains(name))
                    || (refs.contains(DYNAMIC_SIBLING_REF) && !late_bound_names.is_empty())
                {
                    late_bound_names.insert(prop.name.clone());
                    late_child_properties.push(*prop);
                    added = true;
                }
            }
            if !added {
                break;
            }
        }
        self.run_late_binding_passes(
            module,
            depth,
            &mut scope,
            &mut all_props,
            &mut out,
            &late_inherited_properties,
            &late_child_properties,
            &child_property_names,
            super_bound,
            &requested_eval_fields,
            &requested_output_fields,
            &synthesized_inherited_types,
        )?;

        if depth == 0 {
            for prop in &late_inherited_properties {
                if let Some(value) = all_props.get(prop.name.as_ref())
                    && !should_render_property_value(prop, value)
                    && !child_property_names.contains(prop.name.as_ref())
                {
                    out.shift_remove(prop.name.as_ref());
                }
            }
        }

        // Check declarations only after this module's final scope is complete:
        // constraints and aliases may refer to members declared later.
        if !evaluated_as_base {
            // These are the effective non-local properties after an amendment:
            // an override in this body replaces its inherited declaration.
            // Dependents may themselves be inherited, so error propagation must
            // consider both sets rather than only the child body below.
            let effective_properties = late_inherited_properties
                .iter()
                .map(|prop| prop.as_ref())
                .filter(|prop| !child_property_names.contains(prop.name.as_ref()))
                .chain(body.iter().filter_map(|entry| match entry {
                    Entry::Property(prop) => Some(prop.as_ref()),
                    _ => None,
                }))
                .collect::<Vec<_>>();
            let mut inherited_type_failures =
                self.inherited_type_failures(body, &scope, &synthesized_inherited_types, depth)?;

            // Values are evaluated before these late inherited checks. Carry a
            // constrained member's failure through direct and transitive reads
            // so a dependent output cannot retain a stale value. Probe the
            // expression with failed members hidden rather than relying on a
            // syntactic reference list: an unselected `if` branch, or a
            // caught failed read, must remain usable.
            if !inherited_type_failures.is_empty() {
                self.propagate_inherited_type_failures(
                    &mut inherited_type_failures,
                    body,
                    &scope,
                    &all_props,
                    &late_inherited_properties,
                    &effective_properties,
                    &requested_eval_fields,
                    depth,
                )?;
            }
            for prop in effective_properties {
                if let Some(message) = inherited_type_failures.get(prop.name.as_ref()) {
                    let rendered = !has_modifier(&prop.modifiers, Modifier::Hidden)
                        && requested_output_fields
                            .as_ref()
                            .is_none_or(|fields| fields.contains(&prop.name));
                    if rendered && depth == 0 {
                        return Err(Error::Eval(message.clone()));
                    }
                    out.shift_remove(prop.name.as_ref());
                    scope.set_member_poison(&prop.name, Some(message.clone()));
                    self.rendered_member_failed |= rendered;
                    poisoned_members.insert(
                        prop.name.clone(),
                        PoisonedMember {
                            message: message.clone(),
                            rendered,
                        },
                    );
                }
            }
        }

        // A top-level amending or extending module renders data, not the
        // classes it inherited. An imported module keeps those classes so
        // dotted access (for example `child.Person`) remains available.
        if depth == 0 {
            out.retain(|name, _| !inherited_type_names.contains(&**name));
        }

        // At the top level (depth 0), strip class definitions and lambdas from
        // the serialized output — they're schema/functions, not data.
        // Imported modules (depth > 0) keep them so dotted access works
        // (e.g., `helpers.ClassName`).
        if depth == 0 {
            for name in &class_names {
                out.shift_remove(name.as_ref());
            }
            out.retain(|_, v| !matches!(v, Value::Lambda(..)));
        }
        if let Some(fields) = &requested_output_fields {
            out.retain(|name, _| fields.contains(&**name));
        }
        // If the module declares any `@Deprecated` properties, attach a
        // minimal ObjectSource carrying just the deprecation map so field
        // access can warn lazily. Modules without @Deprecated keep `None`
        // source to avoid changing amend behavior in the common case.
        // An abstract module also keeps a source, so `new` can reject it.
        let deprecated = collect_deprecated(body);
        let is_abstract = module_is_abstract(module);
        let source = if deprecated.is_empty() && poisoned_members.is_empty() && !is_abstract {
            None
        } else {
            Some(Arc::new(ObjectSource {
                entries: Vec::new().into(),
                captured: SourceScope::default(),
                body_members: HashSet::default(),
                is_open: true,
                is_abstract,
                type_name: None,
                // An abstract module records its name for the `new` error.
                type_identity: is_abstract.then(|| module_display_name(module, path)),
                parent_type_names: Vec::new(),
                parent_type_identities: Vec::new(),
                entry_scopes: Vec::new(),
                evaluated_properties: Vec::new(),
                elements: Vec::new(),
                mapping_value_types: Vec::new(),
                deprecated,
                poisoned_members: (!poisoned_members.is_empty())
                    .then(|| Arc::new(poisoned_members)),
                kind: ObjectKind::Object,
                is_parsed_json: false,
                prototype: None,
            }))
        };
        let mut effective_late_properties = IndexMap::new();
        let template_locals = module_local_bindings(body, &scope);
        for (prop, inherited) in late_inherited_properties
            .iter()
            .map(|prop| (prop, true))
            .chain(body.iter().filter_map(|entry| match entry {
                Entry::Property(prop)
                    if !has_modifier(&prop.modifiers, Modifier::Local)
                        && &*prop.name != "output" =>
                {
                    Some((prop, false))
                }
                _ => None,
            }))
        {
            let locals = (!inherited).then_some(template_locals.as_ref()).flatten();
            let prop = match effective_late_properties.get(&prop.name) {
                Some(base) => compose_amended_object(base, prop, locals),
                None => match (&prop.value, locals) {
                    (Some(value), Some(_)) => Arc::new(Property {
                        value: Some(with_template_locals(locals, prop, value.clone())),
                        ..(**prop).clone()
                    }),
                    _ => prop.clone(),
                },
            };
            effective_late_properties.insert(prop.name.clone(), prop);
        }
        self.module_scopes.insert(
            path.to_path_buf(),
            ModuleScopeSnapshot {
                values: scope.flatten(),
                type_aliases: scope.flatten_type_aliases(),
                module_identities: scope.flatten_module_identities(),
                late_properties: effective_late_properties.into_values().collect(),
            },
        );
        // An imported module whose failed property this output renders fails
        // the output, as rendering that property directly would.
        if depth == 0
            && self.rendered_member_failed
            && let Some(message) = out.values().find_map(rendered_member_failure)
        {
            return Err(Error::Eval(message.to_string()));
        }
        let module_map = Arc::new(out);
        if depth == 0 {
            self.eval_module_output(module, &module_map, &scope, path, depth)?;
        }
        Ok(Value::Object(module_map, source))
    }

    /// Check the declared types of `body`'s evaluated non-local properties.
    /// Returns the constraint failures of types inherited from the amended
    /// module, which stay attached to their members; any other failure is an
    /// error.
    fn inherited_type_failures(
        &mut self,
        body: &Body,
        scope: &Scope,
        synthesized_inherited_types: &HashSet<&str>,
        depth: usize,
    ) -> Result<IndexMap<Name, String>> {
        let mut inherited_type_failures = IndexMap::new();
        for entry in body.iter() {
            if let Entry::Property(prop) = entry
                && !has_modifier(&prop.modifiers, Modifier::Local)
                && (prop.value.is_some() || prop.body.is_some())
                && prop.type_ann.is_some()
                && scope.is_declared(&prop.name)
                && let Some(value) = scope.get(&prop.name)
            {
                if synthesized_inherited_types.contains(prop.name.as_ref()) {
                    let ty = prop.type_ann.as_ref().expect("checked above");
                    if !type_has_constraint(ty, scope) {
                        continue;
                    }
                    let failure = if type_is_runtime_checkable(ty, scope) {
                        match self.eval_type_check(value, ty, scope, depth + 1) {
                            Ok(true) => None,
                            Ok(false) => Some(format!(
                                "property '{}' expected {}, got {}",
                                prop.name,
                                display_type_expr(ty),
                                value_type_name(value)
                            )),
                            // An explicit Pkl `throw` is observable after the
                            // member is materialized. Keep other evaluator
                            // limitations deferred, as before.
                            Err(Error::Eval(message))
                                if type_constraint_has_explicit_throw(ty, scope)
                                    && !is_deferred_constraint_evaluator_error(&message) =>
                            {
                                Some(message)
                            }
                            Err(Error::Eval(_)) => None,
                            Err(error) => return Err(error),
                        }
                    } else {
                        None
                    };
                    if let Some(message) = failure {
                        inherited_type_failures.insert(prop.name.clone(), message);
                    }
                } else {
                    self.check_declared_property_type(prop, value, scope, depth)?;
                }
            }
        }
        Ok(inherited_type_failures)
    }

    /// Add to `inherited_type_failures` every effective property whose
    /// evaluation fails with one of those failures once the failed members are
    /// hidden, until no more fail.
    #[allow(clippy::too_many_arguments)]
    fn propagate_inherited_type_failures(
        &mut self,
        inherited_type_failures: &mut IndexMap<Name, String>,
        body: &Body,
        scope: &Scope,
        all_props: &ObjectMap,
        late_inherited_properties: &[Arc<Property>],
        effective_properties: &[&Property],
        requested_eval_fields: &Option<HashSet<Name>>,
        depth: usize,
    ) -> Result<()> {
        let mut dependency_entries = late_inherited_properties
            .iter()
            .cloned()
            .map(Entry::Property)
            .collect::<Vec<_>>();
        dependency_entries.extend(body.iter().cloned());
        let dependency_plan = module_evaluation_plan(&dependency_entries);
        while !inherited_type_failures.is_empty() {
            // Find every member that can reach a failed member. This
            // follows local helpers transitively. Restricting probes
            // to that closure preserves narrowed evaluation's
            // laziness without leaving a value computed before its
            // inherited type check stale.
            let mut failure_dependents = inherited_type_failures
                .keys()
                .cloned()
                .collect::<HashSet<_>>();
            loop {
                let mut expanded = false;
                for entry in &dependency_entries {
                    let Entry::Property(prop) = entry else {
                        continue;
                    };
                    let refs = property_reference_names(prop);
                    if refs.iter().any(|name| failure_dependents.contains(name))
                        && failure_dependents.insert(prop.name.clone())
                    {
                        expanded = true;
                    }
                }
                if !expanded {
                    break;
                }
            }

            let mut added = false;
            let mut probe_scope = scope.child();
            let mut probe_properties = (*all_props).clone();
            for (name, message) in inherited_type_failures.iter() {
                probe_scope.poison(name.clone(), message.clone());
                probe_scope.set_member_poison(name, Some(message.clone()));
                probe_properties.shift_remove(name.as_ref());
            }
            let probe_snapshot = Value::Object(Arc::new(probe_properties), None);
            probe_scope.set("this", probe_snapshot.clone());
            probe_scope.set("module", probe_snapshot);
            // A local helper can retain an already-computed value,
            // and a method captures its scope when it is built.
            // Rebuild affected locals and lambdas in the probe scope,
            // so they observe the poisoned member rather than the
            // prior successful value.
            for &index in &dependency_plan.order {
                let Entry::Property(prop) = &dependency_entries[index] else {
                    continue;
                };
                if !has_modifier(&prop.modifiers, Modifier::Local)
                    && !matches!(prop.value.as_ref(), Some(Expr::Lambda(..)))
                    || !failure_dependents.contains(&prop.name)
                {
                    continue;
                }
                let Some(value) = &prop.value else {
                    continue;
                };
                match self.eval_expr(value, &probe_scope, depth + 1) {
                    Ok(value) => probe_scope.declare(&prop.name, value),
                    Err(Error::Eval(message)) => {
                        probe_scope.declare_poisoned(prop.name.clone(), message)
                    }
                    Err(error) => return Err(error),
                }
            }
            for prop in effective_properties {
                if has_modifier(&prop.modifiers, Modifier::Local)
                    || inherited_type_failures.contains_key(prop.name.as_ref())
                    || requested_eval_fields
                        .as_ref()
                        .is_some_and(|fields| !fields.contains(&prop.name))
                    || !failure_dependents.contains(&prop.name)
                {
                    continue;
                }
                if let Err(Error::Eval(message)) = self.eval_property(prop, &probe_scope, depth + 1)
                    && inherited_type_failures
                        .values()
                        .any(|failure| failure == &message)
                {
                    inherited_type_failures.insert(prop.name.clone(), message.clone());
                    added = true;
                }
            }
            if !added {
                break;
            }
        }
        Ok(())
    }

    /// Evaluate the late-bound properties again until their values settle:
    /// the inherited properties this module does not override, and the
    /// child properties that read a late-bound one. Each pass updates
    /// `scope`, the module's members and its rendered `out`.
    #[allow(clippy::too_many_arguments)]
    fn run_late_binding_passes(
        &mut self,
        module: &Module,
        depth: usize,
        scope: &mut Scope,
        all_props: &mut Arc<ObjectMap>,
        out: &mut ObjectMap,
        late_inherited_properties: &[Arc<Property>],
        late_child_properties: &[&Property],
        child_property_names: &HashSet<&str>,
        super_bound: bool,
        requested_eval_fields: &Option<HashSet<Name>>,
        requested_output_fields: &Option<HashSet<Name>>,
        synthesized_inherited_types: &HashSet<&str>,
    ) -> Result<()> {
        let late_passes = late_inherited_properties.len() + late_child_properties.len();
        // `super` in a base module's property is that module's own base, not
        // this module's: those keep the value the base computed.
        let reads_super: HashSet<&str> = if super_bound {
            late_inherited_properties
                .iter()
                .filter(|prop| property_reads_module_super(prop))
                .map(|prop| prop.name.as_ref())
                .collect()
        } else {
            HashSet::default()
        };
        // A pass that reproduces every value, as `settled_eq` compares them,
        // is the last: the next would start from the same state and repeat it.
        //
        // A value that holds an object or a lambda is never reproduced in that
        // sense, since each evaluation makes a new one that captures the
        // scope as it then stands. Such a value can still be the same data,
        // and a pass whose values all are is quiet: it changes nothing a
        // reader can see. It is not necessarily the last. An object or
        // lambda keeps the scope it was made in, so a change reaches a
        // property that reads it through such a capture one pass later for
        // every forward reference on the way, and no value changes while it
        // travels. A property listed before a reader is read as this pass
        // has just evaluated it, one listed after it as the previous pass
        // left it.
        //
        // `forward_reference_chain` finds the longest chain of such forward
        // references. As many quiet passes in a row as that are enough if
        // the model holds, and `QUIET_PASS_MARGIN` more run as slack. The
        // values are what end the loop, and they are the safety net for reads
        // the model misses: a pass that changes the data keeps it running.
        // The loop never runs past `late_passes`.
        //
        // Pass 0 never counts as quiet: it starts from the values the base
        // module computed.
        let chain_memo = std::cell::Cell::new(None);
        let settled_after = || -> usize {
            if let Some(settled_after) = chain_memo.get() {
                return settled_after;
            }
            let late_order: Vec<&Property> = late_inherited_properties
                .iter()
                .map(|prop| &**prop)
                .filter(|prop| {
                    !child_property_names.contains(prop.name.as_ref())
                        && !reads_super.contains(prop.name.as_ref())
                })
                .chain(late_child_properties.iter().copied())
                .collect();
            let settled_after = forward_reference_chain(&late_order) + QUIET_PASS_MARGIN;
            chain_memo.set(Some(settled_after));
            settled_after
        };
        // Quiet passes are counted after pass 0 and the loop breaks before
        // counting at `late_passes`, so at most `late_passes - 1` can be
        // counted. If `settled_after` is not below `late_passes`, no run of
        // quiet passes can end the loop, and telling a quiet pass from a
        // changed one decides nothing: only `settled_eq` can end it. Then
        // `same_data` is not called, which matters for values that capture a
        // large scope.
        let can_stop = || settled_after() < late_passes;
        let mut pass = 0;
        let mut quiet_passes = 0;
        loop {
            #[cfg(test)]
            {
                self.late_passes_run += 1;
            }
            let mut change = Change::None;
            let mut errored = false;
            for prop in late_inherited_properties {
                if child_property_names.contains(prop.name.as_ref())
                    || reads_super.contains(prop.name.as_ref())
                {
                    continue;
                }
                match self.eval_property_with(prop, scope, depth, true) {
                    Ok(Some(value)) => {
                        change = change.then(scope.get(&prop.name), &value, can_stop).then(
                            all_props.get(prop.name.as_ref()),
                            &value,
                            can_stop,
                        );
                        scope.set(&prop.name, value.clone());
                        module_props_insert(scope, all_props, prop.name.clone(), value.clone());
                        if !has_modifier(&prop.modifiers, Modifier::Hidden)
                            && (depth > 0 || should_render_property_value(prop, &value))
                            && requested_output_fields
                                .as_ref()
                                .is_none_or(|fields| fields.contains(&prop.name))
                        {
                            out.insert(prop.name.as_ref().into(), value);
                        }
                        let snapshot = Value::Object(Arc::clone(all_props), None);
                        scope.set("this", snapshot.clone());
                        scope.set("module", snapshot);
                    }
                    Ok(None) => {}
                    Err(Error::Eval(message))
                        if pass < late_passes
                            || (module_is_abstract(module)
                                && is_unresolved_template_error(&message)) =>
                    {
                        errored = true;
                    }
                    Err(error) => return Err(error),
                }
            }
            for prop in late_child_properties {
                if requested_eval_fields
                    .as_ref()
                    .is_some_and(|fields| !fields.contains(&prop.name))
                {
                    continue;
                }
                match self
                    .eval_property_with(prop, scope, depth, true)
                    .and_then(|value| {
                        if synthesized_inherited_types.contains(prop.name.as_ref())
                            && let (Some(value), Some(ty)) = (&value, &prop.type_ann)
                        {
                            self.check_inherited_mapping_value_types(value, ty, scope, depth)?;
                        }
                        Ok(value)
                    }) {
                    Ok(Some(value)) => {
                        change = change.then(scope.get(&prop.name), &value, can_stop);
                        scope.set(&prop.name, value.clone());
                        if has_modifier(&prop.modifiers, Modifier::Local) {
                            continue;
                        }
                        change = change.then(all_props.get(prop.name.as_ref()), &value, can_stop);
                        module_props_insert(scope, all_props, prop.name.clone(), value.clone());
                        if !has_modifier(&prop.modifiers, Modifier::Hidden)
                            && (depth > 0 || should_render_property_value(prop, &value))
                            && requested_output_fields
                                .as_ref()
                                .is_none_or(|fields| fields.contains(&prop.name))
                        {
                            out.insert(prop.name.as_ref().into(), value);
                        }
                        let snapshot = Value::Object(Arc::clone(all_props), None);
                        scope.set("this", snapshot.clone());
                        scope.set("module", snapshot);
                    }
                    Ok(None) => {}
                    Err(Error::Eval(message))
                        if pass < late_passes
                            || (module_is_abstract(module)
                                && is_unresolved_template_error(&message)) =>
                    {
                        errored = true;
                    }
                    Err(error) => return Err(error),
                }
            }
            if pass >= late_passes {
                break;
            }
            let settled = match change {
                Change::None => true,
                Change::Data => {
                    quiet_passes = 0;
                    false
                }
                Change::Capture => {
                    if pass > 0 {
                        quiet_passes += 1;
                    }
                    quiet_passes >= settled_after()
                }
            };
            if !settled {
                pass += 1;
            } else if errored {
                // Errors are only reported on the last pass, so a settled
                // pass that still failed goes straight there.
                pass = late_passes;
            } else {
                break;
            }
        }
        Ok(())
    }

    /// Evaluate locals, classes, type aliases and properties in dependency
    /// order, so a member can read one declared after it. A member that
    /// fails is poisoned rather than failing the module: as in Pkl, the
    /// error surfaces only where the member is used (or rendered).
    #[allow(clippy::too_many_arguments)]
    fn eval_module_members<'b>(
        &mut self,
        module: &Module,
        path: &Path,
        body: &'b Body,
        depth: usize,
        scope: &mut Scope,
        all_props: &mut Arc<ObjectMap>,
        base_obj: &ObjectMap,
        late_inherited_properties: &[Arc<Property>],
        requested_eval_fields: &Option<HashSet<Name>>,
        synthesized_inherited_types: &HashSet<&str>,
    ) -> Result<ModuleMembers<'b>> {
        // Evaluate locals, classes, type aliases and properties in dependency
        // order, so a member can read one declared after it. A member that
        // fails is poisoned rather than failing the module: as in Pkl, the
        // error surfaces only where the member is used (or rendered, below).
        let mut class_names: std::collections::HashSet<Name> = std::collections::HashSet::default();
        let mut evaluated: HashMap<&str, Value> = HashMap::default();
        let mut failed: HashMap<&str, String> = HashMap::default();
        let mut failed_indices: HashSet<usize> = HashSet::default();
        let plan = Arc::clone(
            &self
                .plan_cache
                .entry(Arc::as_ptr(body) as usize)
                .or_insert_with(|| (Arc::clone(body), Arc::new(module_evaluation_plan(body))))
                .1,
        );
        let mut todo = plan.order.clone();
        // A member can fail because it read another before that one could be
        // evaluated: through a dynamic `module[key]`, which the order can't
        // account for. Members that failed are evaluated again, along with
        // the members that read them, until a pass changes nothing.
        let mut previous_failures: Option<HashMap<&str, String>> = None;
        for _ in 0..=plan.order.len() {
            for index in todo {
                let entry = &body[index];
                if let Some(name) = module_member_name(entry) {
                    failed.remove(name);
                    failed_indices.remove(&index);
                }
                match entry {
                    Entry::Property(prop)
                        if has_modifier(&prop.modifiers, Modifier::Local)
                            && prop.value.is_some() =>
                    {
                        match self.eval_expr(prop.value.as_ref().unwrap(), scope, depth) {
                            Ok(val) => scope.declare(&prop.name, val),
                            Err(Error::Eval(message)) => {
                                scope.declare_poisoned(prop.name.clone(), message.clone());
                                failed.insert(&prop.name, message);
                                failed_indices.insert(index);
                            }
                            Err(error) => return Err(error),
                        }
                    }
                    Entry::ClassDef(name, class_mods, parent, body) => {
                        class_names.insert(nm(name));
                        // Reject invalid inheritance eagerly using the same
                        // structural validation as `eval_class_def`. Other
                        // evaluation errors can depend on members declared
                        // later in the module and remain retryable.
                        if let Some(parent_name) = parent.as_deref() {
                            let parent_val = resolve_dotted(scope, parent_name);
                            check_supertype(scope, name, parent_name, parent_val.as_ref())?;
                        }
                        match self.eval_class_def(
                            name,
                            class_mods,
                            parent.as_deref(),
                            body,
                            scope,
                            depth,
                        ) {
                            Ok(defaults) => {
                                scope.set_member_poison(name, None);
                                scope.declare(name, defaults.clone());
                                module_props_insert(scope, all_props, name.clone(), defaults);
                                let snapshot = Value::Object(Arc::clone(all_props), None);
                                scope.set("this", snapshot.clone());
                                scope.set("module", snapshot);
                            }
                            Err(Error::Eval(message)) => {
                                scope.set_member_poison(name, Some(message.clone()));
                                scope.declare_poisoned(name.clone(), message.clone());
                                failed.insert(name, message);
                                failed_indices.insert(index);
                            }
                            Err(error) => return Err(error),
                        }
                    }
                    Entry::TypeAlias(name, ty) => {
                        self.eval_type_alias(name, ty, scope);
                        // A non-local alias to a class is a module member, like
                        // the class, and its value is schema, not data. An
                        // inherited property of the same name stays data.
                        if !module.local_type_aliases.contains(name)
                            && !base_obj.contains_key(name.as_str())
                            && matches!(
                                scope.get(name),
                                Some(Value::Object(_, Some(source)))
                                    if source.kind == ObjectKind::Class
                            )
                        {
                            class_names.insert(nm(name));
                        }
                    }
                    Entry::Property(prop) => {
                        let mods = &prop.modifiers;
                        if has_modifier(mods, Modifier::Local) {
                            continue;
                        }
                        // Extract renderer converters from the `output` block AST,
                        // then skip it (it's not included in the output).
                        // Clear any base-inherited converters so child overrides take precedence.
                        if &*prop.name == "output" {
                            if depth == 0 {
                                self.output_props.push(Arc::clone(prop));
                                self.extract_converters_from_ast(prop, scope, depth);
                            }
                            continue;
                        }
                        if let Some(fields) = &requested_eval_fields
                            && !fields.contains(&prop.name)
                        {
                            continue;
                        }
                        // abstract/external properties must have a value (or be overridden)
                        if (has_modifier(mods, Modifier::Abstract)
                            || has_modifier(mods, Modifier::External))
                            && prop.value.is_none()
                            && prop.body.is_none()
                        {
                            if let Some(v) = base_obj.get(prop.name.as_ref()) {
                                // Satisfied by base — add to scope so other properties can reference it
                                scope.set(&prop.name, v.clone());
                            } else if has_modifier(mods, Modifier::External) {
                                return Err(Error::Eval(format!(
                                    "external property '{}' must be assigned a value in {}",
                                    prop.name,
                                    path.display()
                                )));
                            } else if depth == 0 && !module_is_abstract(module) {
                                return Err(Error::Eval(format!(
                                    "abstract property '{}' must be assigned a value in {}",
                                    prop.name,
                                    path.display()
                                )));
                            }
                            continue;
                        }
                        let property_result = self
                            .eval_property_with(prop, scope, depth, true)
                            .and_then(|value| {
                                if synthesized_inherited_types.contains(prop.name.as_ref())
                                    && let (Some(value), Some(ty)) = (&value, &prop.type_ann)
                                {
                                    self.check_inherited_mapping_value_types(
                                        value, ty, scope, depth,
                                    )?;
                                }
                                Ok(value)
                            });
                        let val = match property_result {
                            Ok(value) => value,
                            // Module properties are late-bound. Keep an unresolved
                            // template expression deferred until a consumer actually
                            // requires it; poisoned locals still surface that error.
                            Err(Error::Eval(message))
                                if is_unresolved_template_error(&message)
                                    && (module_is_abstract(module)
                                        || property_reference_names(prop).iter().any(|name| {
                                            late_inherited_properties
                                                .iter()
                                                .any(|inherited| inherited.name == *name)
                                        })) =>
                            {
                                None
                            }
                            Err(Error::Eval(message)) => {
                                scope.declare_poisoned(prop.name.clone(), message.clone());
                                failed.insert(&prop.name, message);
                                failed_indices.insert(index);
                                continue;
                            }
                            Err(error) => return Err(error),
                        };
                        if let Some(v) = val {
                            if let Some(message) = late_inherited_properties
                                .iter()
                                .filter(|inherited| inherited.name == prop.name)
                                .find_map(|inherited| immutable_member_error(prop, inherited))
                            {
                                return Err(Error::Eval(message));
                            }
                            // Always add to scope so other properties can reference it
                            scope.declare(&prop.name, v.clone());
                            // Track in all_props (including hidden) for `this`/`module`
                            module_props_insert(scope, all_props, prop.name.clone(), v.clone());
                            // Update `this` and `module` with all properties (including hidden)
                            let snapshot = Value::Object(Arc::clone(all_props), None);
                            scope.set("this", snapshot.clone());
                            scope.set("module", snapshot);
                            evaluated.insert(&prop.name, v);
                        }
                    }
                    _ => {}
                }
            }
            if failed_indices.is_empty() || previous_failures.as_ref() == Some(&failed) {
                break;
            }
            previous_failures = Some(failed.clone());
            todo = plan.affected_by(&failed_indices);
        }
        Ok(ModuleMembers {
            class_names,
            evaluated,
            failed,
        })
    }

    /// Load the module `module` amends, whose members are this module's
    /// starting values.
    fn amend_base_module(
        &mut self,
        module: &Module,
        path: &Path,
        depth: usize,
        scope: &mut Scope,
        deferred_inherited_imports: &[(String, PathBuf)],
        base: &mut ModuleBase,
    ) -> Result<()> {
        let ModuleBase {
            members: base_obj,
            late_properties: late_inherited_properties,
            super_members,
        } = base;
        if let Some(amends_uri) = &module.amends {
            // A relative amends inside a remote module resolves against that URL.
            let resolved_amends = resolve_remote_relative(path, amends_uri);
            let uri: &str = resolved_amends.as_deref().unwrap_or(amends_uri);
            if uri.starts_with("https://") || uri.starts_with("http://") {
                // HTTP amends
                self.check_not_self(module, path, Path::new(uri), "amend")?;
                let source = self.fetch_source(uri)?;
                let tokens = lexer::lex_named(&source, uri)?;
                let base_module = parser::parse_named(&tokens, &source, uri)?;
                let base_val = self.eval_module_with_scope(
                    &base_module,
                    Path::new(uri),
                    depth + 1,
                    Some(scope.clone()),
                    None,
                )?;
                self.layer_evaluated_module_scope(Path::new(uri), scope);
                late_inherited_properties.extend(self.inherited_late_properties(Path::new(uri)));
                if let Value::Object(m, _) = base_val {
                    *base_obj = (*m).clone();
                }
                *super_members = Some(self.module_super_members(Path::new(uri), base_obj, false));
            } else if uri.starts_with("package://") {
                let pkg = self.resolve_package(uri)?;
                if let PackageSource::Zip(zip_url, entry) = &pkg {
                    #[cfg(feature = "package-zip")]
                    {
                        let pkg_dir = self.extract_package_zip(zip_url)?;
                        let local_path = pkg_dir.join(entry);
                        self.check_not_self(module, path, &local_path, "amend")?;
                        let source = self.read_to_string_io(&local_path)?;
                        let name = local_path.display().to_string();
                        let tokens = lexer::lex_named(&source, &name)?;
                        let base_module = parser::parse_named(&tokens, &source, &name)?;
                        let base_val = self.eval_module_with_scope(
                            &base_module,
                            &local_path,
                            depth + 1,
                            Some(scope.clone()),
                            None,
                        )?;
                        self.layer_evaluated_module_scope(&local_path, scope);
                        late_inherited_properties
                            .extend(self.inherited_late_properties(&local_path));
                        if let Value::Object(m, _) = base_val {
                            *base_obj = (*m).clone();
                        }
                        *super_members =
                            Some(self.module_super_members(&local_path, base_obj, false));
                    }
                    #[cfg(not(feature = "package-zip"))]
                    {
                        let _ = entry;
                        return Err(Error::Unsupported(format!(
                            "package zip imports require pklr's 'package-zip' feature: {zip_url}"
                        )));
                    }
                } else if let PackageSource::Direct { url, root } = &pkg {
                    self.check_not_self(module, path, Path::new(url.as_str()), "amend")?;
                    let source = self.fetch_direct_package_source(url, root)?;
                    let tokens = lexer::lex_named(&source, url)?;
                    let base_module = parser::parse_named(&tokens, &source, url)?;
                    let base_val = self.eval_module_with_scope(
                        &base_module,
                        Path::new(url.as_str()),
                        depth + 1,
                        Some(scope.clone()),
                        None,
                    )?;
                    self.layer_evaluated_module_scope(Path::new(url.as_str()), scope);
                    late_inherited_properties
                        .extend(self.inherited_late_properties(Path::new(url.as_str())));
                    if let Value::Object(m, _) = base_val {
                        *base_obj = (*m).clone();
                    }
                    *super_members =
                        Some(self.module_super_members(Path::new(url.as_str()), base_obj, false));
                }
            } else if uri == "pkl:test" {
                inherit_stdlib_module("test", base_obj, scope);
            } else if !uri.starts_with("pkl:")
                && (!uri.contains("://") || uri.starts_with("file://"))
            {
                let amends_path = self.local_file_path(path, uri)?;
                if self.path_exists_io(&amends_path)? {
                    self.check_not_self(module, path, &amends_path, "amend")?;
                    let base_val =
                        self.eval_file_with_scope(&amends_path, depth + 1, Some(scope.clone()))?;
                    self.layer_evaluated_module_scope(&amends_path, scope);
                    late_inherited_properties.extend(self.inherited_late_properties(&amends_path));
                    if let Value::Object(m, _) = &base_val {
                        self.bind_deferred_inherited_imports(
                            deferred_inherited_imports,
                            &amends_path,
                            &base_val,
                            scope,
                        )?;
                        *base_obj = (**m).clone();
                    }
                    *super_members = Some(self.module_super_members(&amends_path, base_obj, false));
                }
            }
        }
        Ok(())
    }

    /// Bind the classes of the module `module` amends, which its evaluated
    /// value omits, and collect its non-local properties as late-bound.
    fn bind_amended_base_classes(
        &mut self,
        module: &Module,
        path: &Path,
        depth: usize,
        scope: &mut Scope,
        base: &mut ModuleBase,
    ) -> Result<()> {
        let ModuleBase {
            members: base_obj,
            late_properties: late_inherited_properties,
            super_members: _,
        } = base;
        // Inject class definitions from amends base into scope so the amending
        // module can reference them (e.g., `new Step { ... }`).
        // We re-parse the base module to find ClassDef entries, then evaluate
        // them directly (similar to extends handling), because eval_module strips
        // class definitions from its return value.
        // Also remove inherited class definitions from base_obj so they don't
        // appear in the amending module's data output.
        if let Some(amends_uri) = &module.amends {
            let resolved_amends = resolve_remote_relative(path, amends_uri);
            let uri: &str = resolved_amends.as_deref().unwrap_or(amends_uri);
            if let Some((base_module, source_path)) = self.load_parsed_module(uri, path)? {
                let mut base_scope = scope.clone();
                base_scope.type_namespace =
                    Some(self.module_type_namespace(Path::new(&source_path)));
                for entry in base_module.body.iter() {
                    if let Entry::ClassDef(name, class_mods, parent, body) = entry {
                        let defaults = self.eval_class_def(
                            name,
                            class_mods,
                            parent.as_deref(),
                            body,
                            &base_scope,
                            depth,
                        )?;
                        scope.set(name, defaults.clone());
                        if depth == 0 {
                            base_obj.shift_remove(name.as_str());
                        } else {
                            base_obj.insert(name.as_str().into(), defaults.clone());
                        }
                    }
                    // Extract converters from the base module's output block
                    // (the amending module inherits them; child overrides if present).
                    if let Entry::Property(prop) = entry
                        && &*prop.name == "output"
                        && depth == 0
                    {
                        self.extract_converters_from_ast(prop, scope, depth);
                    }
                    if let Entry::Property(prop) = entry
                        && !has_modifier(&prop.modifiers, Modifier::Local)
                        && &*prop.name != "output"
                        && !self
                            .inherited_late_properties(Path::new(&source_path))
                            .iter()
                            .any(|known| known.name == prop.name)
                    {
                        push_base_property(late_inherited_properties, prop);
                    }
                }
            }
            // Functions inherited from the base stay members, so `module.f`
            // and `this.f` reach them. They are dropped when the module is
            // rendered (see the end of this function).
        }
        Ok(())
    }

    /// Inherit the members, scope and late-bound properties of the module
    /// `module` extends.
    fn extend_base_module(
        &mut self,
        module: &Module,
        path: &Path,
        depth: usize,
        scope: &mut Scope,
        deferred_inherited_imports: &[(String, PathBuf)],
        base: &mut ModuleBase,
    ) -> Result<()> {
        let ModuleBase {
            members: base_obj,
            late_properties: late_inherited_properties,
            super_members,
        } = base;
        // Process extends: load base module, inherit all members and scope
        if let Some(extends_uri) = &module.extends {
            // A relative extends inside a remote module resolves against that URL.
            let resolved_extends = resolve_remote_relative(path, extends_uri);
            let uri: &str = resolved_extends.as_deref().unwrap_or(extends_uri);
            if uri == "pkl:test" {
                inherit_stdlib_module("test", base_obj, scope);
                *super_members = Some(base_obj.clone());
            }
            if !uri.contains("://") || uri.starts_with("file://") {
                let extends_path = self.local_file_path(path, uri)?;
                if self.path_exists_io(&extends_path)? {
                    self.check_not_self(module, path, &extends_path, "extend")?;
                    let base_module = self.parse_file(&extends_path)?;
                    if base_module.amends.is_some() {
                        return Err(Error::Eval(format!(
                            "Module `{}` cannot be extended or used as type because it amends another module.",
                            self.amended_module_name(&base_module, &extends_path)
                        )));
                    }
                    let ext_val =
                        self.eval_file_with_scope(&extends_path, depth + 1, Some(scope.clone()))?;
                    self.layer_evaluated_module_scope(&extends_path, scope);
                    late_inherited_properties.extend(self.inherited_late_properties(&extends_path));
                    let name = extends_path.display().to_string();
                    let source = self.read_to_string_io(&extends_path)?;
                    let tokens = lexer::lex_named(&source, &name)?;
                    let ext_module = parser::parse_named(&tokens, &source, &name)?;
                    if !ext_module.annotations.iter().any(|annotation| {
                        matches!(
                            annotation.name.as_str(),
                            "pklr:module:Open" | "pklr:module:Abstract"
                        )
                    }) {
                        return Err(Error::Eval(format!(
                            "Cannot extend non-open module `{}`.",
                            extends_path
                                .file_stem()
                                .unwrap_or_default()
                                .to_string_lossy()
                        )));
                    }
                    if let Value::Object(m, _) = &ext_val {
                        self.bind_deferred_inherited_imports(
                            deferred_inherited_imports,
                            &extends_path,
                            &ext_val,
                            scope,
                        )?;
                        *base_obj = (**m).clone();
                    }
                    let mut base_scope = scope.clone();
                    base_scope.type_namespace = Some(self.module_type_namespace(&extends_path));
                    // Also evaluate the base module's scope (classes, locals) into our scope
                    // by re-processing its body entries
                    for entry in ext_module.body.iter() {
                        match entry {
                            Entry::ClassDef(cls_name, cls_mods, parent, body) => {
                                let defaults = self.eval_class_def(
                                    cls_name,
                                    cls_mods,
                                    parent.as_deref(),
                                    body,
                                    &base_scope,
                                    depth,
                                )?;
                                scope.set(cls_name, defaults.clone());
                                if depth == 0 {
                                    base_obj.shift_remove(cls_name.as_str());
                                } else {
                                    base_obj.insert(cls_name.as_str().into(), defaults.clone());
                                }
                            }
                            Entry::TypeAlias(name, ty) => {
                                self.eval_type_alias(name, ty, scope);
                            }
                            Entry::Property(prop) if &*prop.name == "output" && depth == 0 => {
                                self.extract_converters_from_ast(prop, scope, depth);
                            }
                            Entry::Property(prop)
                                if !has_modifier(&prop.modifiers, Modifier::Local)
                                    && &*prop.name != "output" =>
                            {
                                push_base_property(late_inherited_properties, prop);
                                if let Ok(Some(value)) = self.eval_property(prop, scope, depth) {
                                    scope.set(&prop.name, value);
                                }
                            }
                            _ => {}
                        }
                    }
                    *super_members = Some(self.module_super_members(&extends_path, base_obj, true));
                }
            } else if uri.starts_with("https://") || uri.starts_with("http://") {
                self.check_not_self(module, path, Path::new(uri), "extend")?;
                let source = self.fetch_source(uri)?;
                let tokens = lexer::lex_named(&source, uri)?;
                let ext_module = parser::parse_named(&tokens, &source, uri)?;
                if ext_module.amends.is_some() {
                    return Err(Error::Eval(format!(
                        "Module `{}` cannot be extended or used as type because it amends another module.",
                        self.amended_module_name(&ext_module, Path::new(uri))
                    )));
                }
                if !ext_module.annotations.iter().any(|annotation| {
                    matches!(
                        annotation.name.as_str(),
                        "pklr:module:Open" | "pklr:module:Abstract"
                    )
                }) {
                    return Err(Error::Eval(format!(
                        "Cannot extend non-open module `{}`.",
                        Path::new(uri)
                            .file_stem()
                            .unwrap_or_default()
                            .to_string_lossy()
                    )));
                }
                let ext_val = self.eval_module_with_scope(
                    &ext_module,
                    Path::new(uri),
                    depth + 1,
                    Some(scope.clone()),
                    None,
                )?;
                self.layer_evaluated_module_scope(Path::new(uri), scope);
                late_inherited_properties.extend(self.inherited_late_properties(Path::new(uri)));
                if let Value::Object(m, _) = ext_val {
                    *base_obj = (*m).clone();
                }
                let mut base_scope = scope.clone();
                base_scope.type_namespace = Some(self.module_type_namespace(Path::new(uri)));
                // Inject class definitions from HTTP base into scope
                for entry in ext_module.body.iter() {
                    if let Entry::ClassDef(cls_name, cls_mods, parent, body) = entry {
                        let defaults = self.eval_class_def(
                            cls_name,
                            cls_mods,
                            parent.as_deref(),
                            body,
                            &base_scope,
                            depth,
                        )?;
                        scope.set(cls_name, defaults.clone());
                        if depth == 0 {
                            base_obj.shift_remove(cls_name.as_str());
                        } else {
                            base_obj.insert(cls_name.as_str().into(), defaults.clone());
                        }
                    }
                    if let Entry::Property(prop) = entry
                        && !has_modifier(&prop.modifiers, Modifier::Local)
                        && &*prop.name != "output"
                    {
                        push_base_property(late_inherited_properties, prop);
                        if let Ok(Some(value)) = self.eval_property(prop, scope, depth) {
                            scope.set(&prop.name, value);
                        }
                    }
                }
                *super_members = Some(self.module_super_members(Path::new(uri), base_obj, true));
            }
        }
        Ok(())
    }

    /// Bind the imports `module` reads in `scope`. Returns the imports of a
    /// module this one amends or extends, by alias and path: those bind once
    /// that base is evaluated.
    #[allow(clippy::too_many_arguments)]
    fn bind_imports(
        &mut self,
        module: &Module,
        path: &Path,
        depth: usize,
        evaluated_as_base: bool,
        scope: &mut Scope,
        referenced_imports: &HashSet<Name>,
        import_field_uses: &HashMap<String, ImportUse>,
        inherited_local_paths: &[PathBuf],
    ) -> Result<Vec<(String, PathBuf)>> {
        let mut deferred_inherited_imports = Vec::new();

        // Process imports
        for import in &module.imports {
            // A relative import inside a remote module resolves against that URL.
            let resolved_uri = resolve_remote_relative(path, &import.uri);
            let uri: &str = resolved_uri.as_deref().unwrap_or(&import.uri);

            // Handle glob imports: import* "dir/*.pkl" as Alias
            if import.is_glob {
                let alias = import
                    .alias
                    .clone()
                    .ok_or_else(|| Error::Eval("import* requires an alias".into()))?;
                if !referenced_imports.contains(alias.as_str()) {
                    continue;
                }

                // Code in other modules can read the alias without being analyzed
                // here: an amended or extended base reads it from this module's
                // scope, and a module amending or extending this one inherits it.
                let requested =
                    if module.amends.is_none() && module.extends.is_none() && !evaluated_as_base {
                        glob_index_keys(import_field_uses, &alias)
                    } else {
                        None
                    };
                let mapping = self.eval_glob_import(uri, path, depth, requested.as_ref())?;
                scope.declare(alias, mapping);
                continue;
            }

            if uri.starts_with("https://") || uri.starts_with("http://") {
                // HTTP import
                let alias = import.alias.clone().unwrap_or_else(|| {
                    uri.rsplit('/')
                        .next()
                        .unwrap_or(uri)
                        .strip_suffix(".pkl")
                        .unwrap_or(uri)
                        .to_string()
                });
                if !referenced_imports.contains(alias.as_str()) {
                    continue;
                }
                let requested = requested_fields_for_import(import_field_uses, &alias);
                let source = self.fetch_source(uri)?;
                let imported_val = {
                    let tokens = lexer::lex_named(&source, uri)?;
                    let imp_module = parser::parse_named(&tokens, &source, uri)?;
                    self.eval_module_with_scope(
                        &imp_module,
                        Path::new(uri),
                        depth + 1,
                        None,
                        requested,
                    )?
                };
                scope.declare(&alias, imported_val);
                scope.set_module_identity(alias, canonical_remote_module_identity(uri));
                continue;
            }

            if uri.starts_with("package://") {
                let pkg = self.resolve_package(uri)?;
                let fragment = uri.split_once('#').map(|(_, f)| f).unwrap_or("");
                let file_path = fragment.strip_prefix('/').unwrap_or(fragment);
                let alias = import.alias.clone().unwrap_or_else(|| {
                    file_path
                        .rsplit('/')
                        .next()
                        .unwrap_or(file_path)
                        .strip_suffix(".pkl")
                        .unwrap_or(file_path)
                        .to_string()
                });
                if !referenced_imports.contains(alias.as_str()) {
                    continue;
                }
                let requested = requested_fields_for_import(import_field_uses, &alias);
                // For zip packages, extract to temp dir and eval as local file
                if let PackageSource::Zip(zip_url, _) = &pkg {
                    #[cfg(feature = "package-zip")]
                    {
                        let pkg_dir = self.extract_package_zip(zip_url)?;
                        let local_path = pkg_dir.join(file_path);
                        let imported_val = self.eval_file_with_requested_fields(
                            &local_path,
                            depth + 1,
                            requested,
                        )?;
                        let identity = self.module_type_namespace(&local_path);
                        scope.declare(&alias, imported_val);
                        scope.set_module_identity(alias, identity);
                        continue;
                    }
                    #[cfg(not(feature = "package-zip"))]
                    {
                        return Err(Error::Unsupported(format!(
                            "package zip imports require pklr's 'package-zip' feature: {zip_url}"
                        )));
                    }
                }
                let url = match &pkg {
                    PackageSource::Direct { url, .. } => url.clone(),
                    PackageSource::Zip(..) => unreachable!(),
                };
                let root = match &pkg {
                    PackageSource::Direct { root, .. } => root,
                    PackageSource::Zip(..) => unreachable!(),
                };
                let source = self.fetch_direct_package_source(&url, root)?;
                let imported_val = {
                    let tokens = lexer::lex_named(&source, &url)?;
                    let imp_module = parser::parse_named(&tokens, &source, &url)?;
                    self.eval_module_with_scope(
                        &imp_module,
                        Path::new(&url),
                        depth + 1,
                        None,
                        requested,
                    )?
                };
                scope.declare(&alias, imported_val);
                scope.set_module_identity(alias, url);
                continue;
            }

            // Handle pkl: standard library imports
            if let Some(module_name) = uri.strip_prefix("pkl:") {
                let stdlib_val = self.stdlib_module(module_name, depth)?;
                let alias = import
                    .alias
                    .clone()
                    .unwrap_or_else(|| module_name.to_string());
                if !referenced_imports.contains(alias.as_str()) {
                    continue;
                }
                scope.declare(alias, stdlib_val);
                continue;
            }

            // Skip other non-local imports
            if uri.contains("://") && !uri.starts_with("file://") {
                continue;
            }

            let import_path = self.local_file_path(path, uri)?;
            let alias = import.alias.clone().unwrap_or_else(|| {
                import_path
                    .file_stem()
                    .unwrap_or_default()
                    .to_string_lossy()
                    .to_string()
            });
            if !referenced_imports.contains(alias.as_str()) {
                // Unused imports are intentionally lazy: missing local paths are
                // reported only if the imported binding is actually referenced.
                continue;
            }
            if !self.path_exists_io(&import_path)? {
                return Err(Error::ImportNotFound(import_path.display().to_string()));
            }
            let mut inherited_path = None;
            for candidate in inherited_local_paths {
                if self.same_local_path(candidate, &import_path)? {
                    inherited_path = Some(candidate);
                    break;
                }
            }
            if let Some(inherited_path) = inherited_path {
                deferred_inherited_imports.push((alias, inherited_path.clone()));
                continue;
            }
            {
                let requested = requested_fields_for_import(import_field_uses, &alias);
                let imported_val =
                    self.eval_file_with_requested_fields(&import_path, depth + 1, requested)?;
                let identity = self.module_type_namespace(&import_path);
                scope.declare(&alias, imported_val);
                scope.set_module_identity(alias, identity);
            }
        }
        Ok(deferred_inherited_imports)
    }
}

/// Standard library types that are not annotation classes.
pub(super) const STDLIB_NON_ANNOTATION_TYPES: &[&str] = &[
    "Any",
    "Null",
    "Boolean",
    "Number",
    "Int",
    "Float",
    "String",
    "Duration",
    "DataSize",
    "Regex",
    "Object",
    "Dynamic",
    "Typed",
    "Listing",
    "Mapping",
    "List",
    "Set",
    "Map",
    "Pair",
    "IntSeq",
    "Function",
    "Function0",
    "Function1",
    "Function2",
    "Function3",
    "Function4",
    "Function5",
    "VarArgs",
    "Bytes",
    "Collection",
    "Module",
    "Class",
    "TypeAlias",
];

/// Check that each annotation on a module property names an annotation
/// class: a subclass of `Annotation`, not another class or a value.
pub(super) fn check_module_annotations(module: &Module, scope: &Scope) -> Result<()> {
    let annotated = module
        .body
        .iter()
        .any(|entry| matches!(entry, Entry::Property(prop) if !prop.annotations.is_empty()));
    if !annotated {
        return Ok(());
    }
    let classes: HashMap<&str, Option<&str>> = module
        .body
        .iter()
        .filter_map(|entry| match entry {
            Entry::ClassDef(name, _, parent, _) => Some((name.as_str(), parent.as_deref())),
            _ => None,
        })
        .collect();
    let properties: HashSet<&str> = module
        .body
        .iter()
        .filter_map(|entry| match entry {
            Entry::Property(prop) => Some(prop.name.as_ref()),
            _ => None,
        })
        .collect();
    let resolve_alias = |name: &str| -> Option<String> {
        let mut current = name.to_string();
        let mut seen = HashSet::default();
        while let Some(ty) = scope.get_type_alias(&current) {
            match ty {
                crate::parser::TypeExpr::Named(target)
                    if !target.starts_with('*') && seen.insert(current.clone()) =>
                {
                    current = target.clone();
                }
                _ => return None,
            }
        }
        Some(current)
    };
    let is_annotation_class = |name: &str| {
        let mut current = name.to_string();
        for _ in 0..=classes.len() {
            match classes.get(current.as_str()) {
                Some(Some(parent)) => match resolve_alias(parent) {
                    Some(parent) => current = parent,
                    None => return false,
                },
                Some(None) => return false,
                None => return !STDLIB_NON_ANNOTATION_TYPES.contains(&current.as_str()),
            }
        }
        false
    };
    for entry in module.body.iter() {
        let Entry::Property(prop) = entry else {
            continue;
        };
        for annotation in &prop.annotations {
            if annotation.name.contains('.') {
                continue;
            }
            let Some(resolved) = resolve_alias(&annotation.name) else {
                return Err(Error::Eval("Expected an annotation class.".into()));
            };
            let name = resolved.as_str();
            if classes.contains_key(name) {
                if !is_annotation_class(name) {
                    return Err(Error::Eval("Expected an annotation class.".into()));
                }
                continue;
            }
            if scope.get_type_alias(name).is_some() {
                continue;
            }
            if name == annotation.name && properties.contains(name) {
                return Err(Error::Eval(format!(
                    "Expected `{name}` to be a type, but it is not."
                )));
            }
            match scope.get(name) {
                Some(Value::String(marker)) if &**marker == name => {
                    if name != "Annotation" && STDLIB_NON_ANNOTATION_TYPES.contains(&name) {
                        return Err(Error::Eval("Expected an annotation class.".into()));
                    }
                }
                Some(Value::Object(_, Some(source)))
                    if source.type_name.as_deref() == Some(name) => {}
                Some(_) => {
                    return Err(Error::Eval(format!(
                        "Expected `{name}` to be a type, but it is not."
                    )));
                }
                None if STDLIB_NON_ANNOTATION_TYPES.contains(&name) => {
                    return Err(Error::Eval("Expected an annotation class.".into()));
                }
                None => {}
            }
        }
    }
    Ok(())
}

/// Make standard-library module members available through both the module
/// object and bare names in a module that amends or extends it.
pub(super) fn inherit_stdlib_module(name: &str, base_obj: &mut ObjectMap, scope: &mut Scope) {
    if let Value::Object(members, _) = stdlib_module(name) {
        for (member, value) in members.iter() {
            base_obj.insert(member.clone(), value.clone());
            scope.set(member, value.clone());
        }
    }
}

/// Whether re-evaluating a late-bound property reproduced `old`.
///
/// Cheap and conservative: a lambda or an object that carries its definition
/// scope only counts as unchanged when it is the very same allocation, since
/// comparing the scopes they capture is as costly as the evaluation itself.
/// A false "changed" only costs another pass.
pub(super) fn settled_eq(old: Option<&Value>, new: Option<&Value>) -> bool {
    match (old, new) {
        (None, None) => true,
        (Some(old), Some(new)) => match (old, new) {
            (Value::Object(left, left_source), Value::Object(right, right_source)) => {
                let same_source = match (left_source, right_source) {
                    (None, None) => true,
                    (Some(left), Some(right)) => Arc::ptr_eq(left, right),
                    _ => false,
                };
                same_source
                    && (Arc::ptr_eq(left, right)
                        || (left.len() == right.len()
                            && left.iter().zip(right.iter()).all(
                                |((left_key, left_value), (right_key, right_value))| {
                                    left_key == right_key
                                        && settled_eq(Some(left_value), Some(right_value))
                                },
                            )))
            }
            (Value::List(left), Value::List(right)) => {
                left.kind() == right.kind()
                    && left.len() == right.len()
                    && left
                        .iter()
                        .zip(right.iter())
                        .all(|(left, right)| settled_eq(Some(left), Some(right)))
            }
            (Value::Float(left), Value::Float(right)) => left.to_bits() == right.to_bits(),
            (Value::Lambda(..), _) | (_, Value::Lambda(..)) => false,
            _ => old == new,
        },
        _ => false,
    }
}

#[cfg(test)]
thread_local! {
    /// How many times the late-binding passes called `same_data`, on this thread.
    static SAME_DATA_CHECKS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
}

pub(super) fn same_data_counted(old: &Value, new: &Value) -> bool {
    #[cfg(test)]
    SAME_DATA_CHECKS.with(|checks| checks.set(checks.get() + 1));
    old.same_data(new)
}

/// How the values a late-binding pass produced compare with the ones they
/// replace, the largest over all of them.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub(super) enum Change {
    /// Each value is one `settled_eq` accepts.
    None,
    /// Each value is the same data, but one holds an object or lambda made
    /// anew, which captured the scope as it now stands.
    Capture,
    /// A value is different data.
    Data,
}

impl Change {
    /// The larger of `self` and how `new` compares with `old`. Only a value
    /// that holds an object, list or lambda can be the same data without being
    /// one `settled_eq` accepts, so a scalar that differs is different data
    /// at once. `same_data` is only called if `can_stop` says a quiet pass
    /// could end the loop.
    fn then(self, old: Option<&Value>, new: &Value, can_stop: impl Fn() -> bool) -> Self {
        if self == Self::Data {
            return self;
        }
        let this = if settled_eq(old, Some(new)) {
            Self::None
        } else if matches!(new, Value::Object(..) | Value::List(_) | Value::Lambda(..))
            && old.is_some_and(|old| can_stop() && same_data_counted(old, new))
        {
            Self::Capture
        } else {
            Self::Data
        };
        self.max(this)
    }
}

pub(super) fn stdlib_module(name: &str) -> Value {
    if let Some(module) = stdlib::module(name) {
        return module;
    }
    let mut map = ObjectMap::default();
    if name == "base" {
        map.insert("Regex".into(), Value::String("Regex".into()));
    }
    if name == "test" {
        for (method, builtin) in [("catch", TEST_CATCH), ("catchOrNull", TEST_CATCH_OR_NULL)] {
            map.insert(method.into(), builtin_function(builtin, &["fun"]));
        }
    }
    Value::Object(Arc::new(map), None)
}

/// Late-binding passes re-evaluate every inherited property. They must stop
/// once the values settle, not run once per inherited property.
#[cfg(all(test, feature = "native-io"))]
mod late_binding_pass_tests {
    use super::{Entry, Evaluator, Property, forward_reference_chain};

    /// How many properties the template declares.
    const PROPERTIES: usize = 60;

    /// A pass per property over `PROPERTIES` properties takes more than
    /// `PROPERTIES` passes; settling takes a few.
    const MAX_PASSES: usize = PROPERTIES / 4;

    /// A directory of modules, removed when dropped.
    struct Modules(std::path::PathBuf);

    impl Drop for Modules {
        fn drop(&mut self) {
            let _ = std::fs::remove_dir_all(&self.0);
        }
    }

    /// Evaluate `Child.pkl` from the modules in `files`, and return the result
    /// with the number of late-binding passes it ran.
    fn try_evaluate_modules(
        files: &[(&str, &str)],
    ) -> (Result<serde_json::Value, crate::Error>, usize) {
        let dir = Modules(std::env::temp_dir().join(format!(
            "pklr-late-passes-{}-{:?}",
            std::process::id(),
            std::thread::current().id()
        )));
        std::fs::create_dir_all(&dir.0).unwrap();
        for (name, source) in files {
            std::fs::write(dir.0.join(name), source).unwrap();
        }
        let mut evaluator = Evaluator::default();
        let value = evaluator.eval_file(&dir.0.join("Child.pkl"));
        (
            value.map(|value| value.to_json()),
            evaluator.late_passes_run,
        )
    }

    /// [`try_evaluate_modules`] for modules that evaluate.
    fn evaluate_modules(files: &[(&str, &str)]) -> (serde_json::Value, usize) {
        let (value, passes) = try_evaluate_modules(files);
        (value.unwrap(), passes)
    }

    /// Evaluate `Child.pkl`, which amends `Template.pkl`.
    fn evaluate_amendment(template: &str, child: &str) -> (serde_json::Value, usize) {
        evaluate_modules(&[("Template.pkl", template), ("Child.pkl", child)])
    }

    #[test]
    fn amending_module_does_not_run_a_pass_per_inherited_property() {
        // `base`, then `p1 = base + 1`, `p2 = p1 + 1`, and so on.
        let mut template = String::from("base = 1\n");
        for n in 1..=PROPERTIES {
            let previous = if n == 1 {
                "base".to_string()
            } else {
                format!("p{}", n - 1)
            };
            template.push_str(&format!("p{n} = {previous} + 1\n"));
        }
        let (json, passes) = evaluate_amendment(&template, "amends \"Template.pkl\"\nbase = 10\n");

        assert_eq!(json[format!("p{PROPERTIES}")], 10 + PROPERTIES);
        assert!(
            passes <= MAX_PASSES,
            "{passes} late-binding passes for {PROPERTIES} properties (at most {MAX_PASSES} expected)"
        );
    }

    /// A template of objects and a lambda, which keep the scope they were made
    /// in and so are never the same allocation twice, settles in a few passes
    /// as one of plain values does.
    #[test]
    fn amending_a_template_of_objects_does_not_run_a_pass_per_property() {
        let mut template = String::from("base = 1\nadd = (x) -> x + base\n");
        for n in 1..=PROPERTIES {
            template.push_str(&format!(
                "o{n} = new {{ v = base; w = add.apply({n}); items = new Listing {{ base }} }}\n"
            ));
        }
        let (json, passes) = evaluate_amendment(&template, "amends \"Template.pkl\"\nbase = 10\n");

        assert_eq!(json[format!("o{PROPERTIES}")]["v"], 10);
        assert_eq!(json[format!("o{PROPERTIES}")]["w"], 10 + PROPERTIES);
        assert_eq!(json["o1"]["items"][0], 10);
        assert!(
            passes <= MAX_PASSES,
            "{passes} late-binding passes for {PROPERTIES} objects (at most {MAX_PASSES} expected)"
        );
    }

    /// The same, where a module between them amends each object, so that the
    /// amendments are late-bound too, and the last module overrides what the
    /// objects read.
    #[test]
    fn amending_amended_objects_does_not_run_a_pass_per_property() {
        let mut template = String::from("base = 1\n");
        let mut middle = String::from("amends \"Template.pkl\"\n");
        for n in 1..=PROPERTIES {
            template.push_str(&format!("o{n} = new {{ a = {n}; b = base }}\n"));
            middle.push_str(&format!("o{n} {{ a = {} }}\n", n + 1));
        }
        let (json, passes) = evaluate_modules(&[
            ("Template.pkl", &template),
            ("Middle.pkl", &middle),
            ("Child.pkl", "amends \"Middle.pkl\"\nbase = 10\n"),
        ]);

        assert_eq!(json[format!("o{PROPERTIES}")]["a"], PROPERTIES + 1);
        assert_eq!(json[format!("o{PROPERTIES}")]["b"], 10);
        assert_eq!(json["o1"]["b"], 10);
        assert!(
            passes <= 2 * MAX_PASSES,
            "{passes} late-binding passes for {PROPERTIES} amended objects"
        );
    }

    /// `x0 = x1`, `x1 = x2`, and so on to `x{reads} = 1`, with an unrelated
    /// `w` for the amending module to override, so no value ever changes.
    fn chain_of_reads(reads: usize) -> String {
        let mut template = String::new();
        for n in 0..reads {
            template.push_str(&format!("x{n} = x{}\n", n + 1));
        }
        template.push_str(&format!("x{reads} = 1\nw = 0\n"));
        template
    }

    /// The same chain, of objects: each is made anew by every pass, capturing
    /// the scope as it then stands.
    fn chain_of_objects(reads: usize) -> String {
        let mut template = String::new();
        for n in 0..reads {
            template.push_str(&format!("x{n} = new {{ v = x{}.v }}\n", n + 1));
        }
        template.push_str(&format!("x{reads} = new {{ v = 1 }}\nw = 0\n"));
        template
    }

    /// A chain of lambdas that each read the whole module, `f1` to `f{n}`.
    fn chain_of_lambdas_reading_the_module(n: usize) -> String {
        let mut template = String::from("res = f1.apply(1)\n");
        for k in 1..n {
            template.push_str(&format!(
                "hidden f{k} = (x) -> module.f{}.apply(x)\n",
                k + 1
            ));
        }
        template.push_str(&format!("hidden f{n} = (x) -> x + 1\n"));
        template
    }

    /// Where the chain of forward references plus the margin is as long as
    /// the passes can run, no run of quiet passes can end them, so only
    /// `settled_eq` can. The lambdas here capture the module and are never
    /// reproduced, so every pass runs, as on a build without the quiet-pass
    /// rule, and `same_data` is not called, which would walk everything
    /// each captured scope holds on every pass.
    #[test]
    fn a_chain_no_quiet_pass_can_end_never_compares_by_data() {
        let child = "amends \"Template.pkl\"\nbase = 10\n";
        let (json, passes) = evaluate_amendment(&chain_of_lambdas_reading_the_module(30), child);
        assert_eq!(json["res"], 2);
        // The template's own pass and pass 0 of the amending module, then a pass for
        // each of the inherited properties, and one more.
        assert_eq!(passes, 2 + 31, "every pass runs");
        assert_eq!(super::SAME_DATA_CHECKS.with(std::cell::Cell::get), 0);
    }

    /// The chain and the margin as long as the passes, to the pass: the last
    /// quiet pass would be the last pass, so nothing is compared either. Six
    /// properties, a chain of four reads and two passes of margin.
    #[test]
    fn a_chain_exactly_as_long_as_the_passes_is_never_compared_by_data() {
        let child = "amends \"Template.pkl\"\nw = 5\n";
        let (_, passes) = evaluate_amendment(&chain_of_objects(4), child);
        assert_eq!(passes, 1 + 1 + 6, "every pass runs");
        assert_eq!(super::SAME_DATA_CHECKS.with(std::cell::Cell::get), 0);
    }

    /// The guard above must not turn the comparison off where it can end the
    /// passes: a chain of four reads and the margin are fewer passes than
    /// the template has properties.
    #[test]
    fn a_chain_a_quiet_pass_can_end_is_compared_by_data() {
        let child = "amends \"Template.pkl\"\nw = 5\n";
        let mut template = chain_of_objects(4);
        for n in 0..8 {
            template.push_str(&format!("y{n} = {n}\n"));
        }
        let (_, passes) = evaluate_amendment(&template, child);
        assert!(passes < 2 * 6, "{passes} passes");
        assert!(super::SAME_DATA_CHECKS.with(std::cell::Cell::get) > 0);
    }

    /// The same chain, of objects that differ from one pass to the next only
    /// in their bare elements, which are not in the member map: each holds
    /// the elements of the next, and the last holds `base`.
    fn chain_of_elements(reads: usize) -> String {
        let mut template = String::from("base = 1\n");
        for n in 0..reads {
            template.push_str(&format!("x{n} = new Dynamic {{ w = 0; ...x{} }}\n", n + 1));
        }
        template.push_str(&format!("x{reads} = new Dynamic {{ w = 0; base }}\n"));
        template
    }

    /// A change that shows only in the elements of an object is a change, and
    /// keeps the passes running as one in a member does.
    #[test]
    fn a_change_only_in_elements_restarts_the_quiet_count() {
        let child = "amends \"Template.pkl\"\nbase = 10\n";
        let (_, short) = evaluate_amendment(&chain_of_elements(3), child);
        let (_, long) = evaluate_amendment(&chain_of_elements(9), child);
        // The template's own pass and pass 0, then the passes the last change
        // takes to arrive (one per link, as the elements travel the chain)
        // and the quiet ones that follow it: the chain and the margin.
        assert_eq!((short, long), (7, 13));
    }

    /// Values that every pass reproduces end the passes at the first that
    /// changes nothing, however long the chain: that pass would be repeated.
    /// Counted: the template's own pass and pass 0 of the amending module.
    #[test]
    fn reproduced_values_end_the_passes_at_the_first_that_changes_nothing() {
        let child = "amends \"Template.pkl\"\nw = 5\n";
        let (_, short) = evaluate_amendment(&chain_of_reads(0), child);
        let (_, long) = evaluate_amendment(&chain_of_reads(40), child);
        assert_eq!((short, long), (2, 2));
    }

    /// Readers of the whole module read every property, and two of them read
    /// each other. That must not keep the passes of a template of plain values
    /// running: nothing in it captures a scope.
    #[test]
    fn readers_of_the_module_do_not_keep_plain_values_from_settling() {
        let mut template = String::from("base = 1\n");
        for n in 0..PROPERTIES {
            template.push_str(&format!("p{n} = base + {n}\n"));
        }
        template.push_str("hidden m0 = module.base\nhidden m1 = this.base\n");
        let (json, passes) = evaluate_amendment(&template, "amends \"Template.pkl\"\nbase = 10\n");
        assert_eq!(json["p0"], 10);
        assert!(passes <= 3, "{passes} late-binding passes");
    }

    /// Passes that change nothing the data shows still run until more of them
    /// than the longest chain of forward references have passed, and that is
    /// what carries a change through a capture that lags behind. Counted: the
    /// template's own pass, pass 0 of the amending module, a pass for each
    /// of the four reads in the chain, and two quiet passes beyond it.
    #[test]
    fn quiet_passes_run_past_the_longest_forward_chain() {
        let child = "amends \"Template.pkl\"\nw = 5\n";
        // Properties enough that the passes are not capped by their number.
        let with_filler = |mut template: String| {
            for n in 0..12 {
                template.push_str(&format!("y{n} = {n}\n"));
            }
            template
        };
        let (_, without_reads) = evaluate_amendment(&with_filler(chain_of_objects(0)), child);
        let (_, with_reads) = evaluate_amendment(&with_filler(chain_of_objects(4)), child);
        assert_eq!((without_reads, with_reads), (1 + 1 + 2, 1 + 1 + 4 + 2));
    }

    /// A pass that fails and changes nothing is not repeated: errors are only
    /// reported on the last pass, so the passes go straight to it. Counted: the
    /// template's pass, pass 0 and the quiet passes of the amending module
    /// (two beyond a chain of none), and the last.
    #[test]
    fn a_settled_pass_that_still_fails_goes_straight_to_the_last() {
        let mut template = String::from("base = 1\nbad = base + throw(\"boom\")\n");
        for n in 0..PROPERTIES {
            template.push_str(&format!("o{n} = new {{ v = {n} }}\n"));
        }
        let (result, passes) = try_evaluate_modules(&[
            ("Template.pkl", &template),
            ("Child.pkl", "amends \"Template.pkl\"\nbase = 2\n"),
        ]);
        let message = result.unwrap_err().to_string();
        assert!(message.contains("boom"), "{message}");
        assert!(passes <= MAX_PASSES, "{passes} late-binding passes");
    }

    /// An override in the amending module ends a chain of reads: what it reads
    /// is not the template's property any more.
    #[test]
    fn an_overridden_property_ends_the_chain_of_reads() {
        let template = chain_of_objects(4);
        let (_, kept) = evaluate_amendment(&template, "amends \"Template.pkl\"\nw = 5\n");
        let (_, cut) = evaluate_amendment(
            &template,
            "amends \"Template.pkl\"\nw = 5\nx2 = new { v = 7 }\n",
        );
        assert!(
            cut < kept,
            "{cut} passes with the chain cut, {kept} without"
        );
    }

    /// A change can arrive after quiet passes. Each `o{n}` reads the next
    /// through an object it amends, which keeps the scope it was made in, so
    /// an override of `base` reaches `res` one capture per link without any
    /// value showing it on the way. Pass 3 changes `res` after passes 1 and 2
    /// found nothing different; the quiet passes are counted again from it,
    /// and six follow it: the chain of four forward references, and the
    /// margin of two. Dropping the reset ends the passes before `res` is right.
    #[test]
    fn a_change_after_quiet_passes_restarts_the_count() {
        let mut template = String::from("res = (o1) { go = true }.v\n");
        for n in 1..6 {
            template.push_str(&format!(
                "o{n} = new {{ go = false; v = if (go) (o{}) {{ go = true }}.v else 0 }}\n",
                n + 1
            ));
        }
        template
            .push_str("o6 = new { go = false; v = if (go) r else 0 }\nbase = 1\nr = base + 1\n");
        let (json, passes) = evaluate_amendment(&template, "amends \"Template.pkl\"\nbase = 10\n");
        assert_eq!(json["res"], 11);
        // The template's pass, and passes 0 to 9 of the amending module.
        assert_eq!(passes, 1 + 10);
    }

    /// The longest chain among the properties of `source`, listed `times`
    /// times in a row as an amending module lists those it inherits.
    fn longest_chain_listed(source: &str, times: usize) -> usize {
        let tokens = crate::lexer::lex(source).unwrap();
        let module = crate::parser::parse(&tokens).unwrap();
        let once: Vec<&Property> = module
            .body
            .iter()
            .filter_map(|entry| match entry {
                Entry::Property(prop) => Some(&**prop),
                _ => None,
            })
            .collect();
        let late: Vec<&Property> = (0..times).flat_map(|_| once.iter().copied()).collect();
        forward_reference_chain(&late)
    }

    fn longest_chain(source: &str) -> usize {
        longest_chain_listed(source, 1)
    }

    fn references(source: &str) -> Vec<String> {
        let tokens = crate::lexer::lex(source).unwrap();
        let module = crate::parser::parse(&tokens).unwrap();
        let Some(Entry::Property(prop)) = module.body.first() else {
            panic!("expected a property");
        };
        let mut names: Vec<String> = super::property_reference_names(prop)
            .iter()
            .map(|name| name.to_string())
            .collect();
        names.sort();
        names
    }

    /// A read by computed key names the module it indexes, so the chain does
    /// not need to treat `DYNAMIC_SIBLING_REF` itself as a read of everything.
    #[test]
    fn a_read_by_computed_key_names_the_module_it_reads_through() {
        for source in [
            "m = module[\"B\".toLowerCase()]\n",
            "m = this[\"B\".toLowerCase()]\n",
            "local m = module\n",
            "local m = this\n",
        ] {
            let names = references(source);
            assert!(
                names.iter().any(|name| name == "module" || name == "this"),
                "{source:?} references only {names:?}"
            );
        }
    }

    /// Which earlier copy of a property a later one reads changes how many
    /// forward references stand in its way: `b` reads the copy of `d` just
    /// before it, whose own read of `a` is not forward, and not the first.
    #[test]
    fn a_property_reads_the_latest_earlier_copy_of_what_it_reads() {
        let source = "d = a\nc = b + d\nb = d\na = 1\n";
        assert_eq!(longest_chain_listed(source, 2), 1);
    }

    #[test]
    fn properties_declared_after_what_they_read_have_no_forward_chain() {
        assert_eq!(longest_chain("a = 1\nb = a\nc = b + a\n"), 0);
    }

    #[test]
    fn each_read_of_a_later_property_adds_one_to_the_chain() {
        assert_eq!(longest_chain("c = b\nb = a\na = 1\n"), 2);
        assert_eq!(longest_chain("c = b\nb = 1\na = 1\n"), 1);
        assert_eq!(longest_chain("c = a\nb = a\na = 1\n"), 1);
    }

    #[test]
    fn a_property_that_reads_the_module_reads_every_other_property() {
        assert_eq!(longest_chain("m = module\nb = 1\n"), 1);
        assert_eq!(longest_chain("m = module\nb = a\na = 1\n"), 2);
        assert_eq!(longest_chain("b = 1\nm = module\n"), 0);
        assert_eq!(longest_chain("m = this\nb = 1\n"), 1);
    }

    #[test]
    fn a_property_that_reads_a_computed_key_reads_every_other_property() {
        assert_eq!(longest_chain("m = module[\"B\".toLowerCase()]\nb = 1\n"), 1);
    }

    #[test]
    fn properties_that_read_each_other_have_the_longest_possible_chain() {
        assert_eq!(longest_chain("a = b\nb = a\nc = 1\n"), 3);
    }

    #[test]
    fn a_property_that_reads_itself_has_the_longest_possible_chain() {
        assert_eq!(longest_chain("f = (x) -> f.apply(x)\nb = 1\n"), 2);
    }

    #[test]
    fn a_property_that_reads_through_outer_reads_every_other_property() {
        assert_eq!(longest_chain("o = new { v = outer.x }\nx = 1\n"), 1);
        assert_eq!(longest_chain("o = new { v = 1 }\nx = 1\n"), 0);
    }
}