monty 0.0.21

A sandboxed, snapshotable Python interpreter written in Rust.
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
#![expect(
    unsafe_code,
    reason = "Paged arena hands out typed, aliasable views into UnsafeCell entries"
)]

#[cfg(feature = "ref-count-return")]
use std::collections::HashSet;
use std::{
    cell::{Cell, UnsafeCell},
    collections::BTreeMap,
    fmt,
    iter::once,
    marker::PhantomData,
    mem::ManuallyDrop,
    ops::{Deref, DerefMut},
    ptr::{self, NonNull},
    sync::Arc,
};

use monty_types::{ResourceError, ResourceTracker};
use serde::ser::SerializeStruct;

// Re-export items moved to `heap_traits` so that `crate::heap::DropGuard` etc. continue
// to resolve (used by the `defer_drop!` macros and throughout the codebase).
pub(crate) use crate::heap_data::HeapData;
pub(crate) use crate::heap_traits::{ContainsHeap, DropGuard, DropWithContext, HeapItem};
#[cfg(feature = "ref-count-return")]
use crate::types::Type;
use crate::{
    asyncio::{Awaiter, Coroutine, ExternalFuture, ExternalFutureState, GatherFuture, GatherState},
    exception_private::SimpleException,
    heap_data::{CellValue, Closure, FunctionDefaults},
    modules::dataclasses::DataclassField,
    types::{
        BoundMethod, Bytes, BytesIterator, Class, Dataclass, Deque, Dict, DictItemIterator, DictItemsView,
        DictKeyIterator, DictKeysView, DictValueIterator, DictValuesView, ExtFunction, FrozenSet, Instance,
        ItertoolsIter, List, LongInt, Module, NamedTuple, NamedTupleClass, OpenFile, Path, Range, RangeIterator,
        ReMatch, RePattern, Set, SetIterator, Slice, Str, StringIterator, TimeZone, Tuple, TupleIterator,
        callable_iterator::CallableIterator, date, datetime, deque::DequeIterator, list::ListIterator, timedelta,
        timezone,
    },
    value::Value,
};

mod free_list;
mod stable_heap;
use stable_heap::StableHeap;

/// Unique identifier for values stored inside the heap arena.
///
/// The ID does not encode ownership. Local IDs should normally be borrowed or
/// wrapped immediately in `Value::Ref`; owned fields must document and release
/// their reference through `HeapItem` or `DropWithContext` cleanup.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub struct HeapId(usize);

impl HeapId {
    /// Creates a `HeapId` from a raw index.
    #[inline]
    pub(crate) fn from_index(index: usize) -> Self {
        Self(index)
    }

    /// Returns the raw index value.
    #[inline]
    pub fn index(self) -> usize {
        self.0
    }
}

/// The empty tuple is a singleton which is allocated at startup.
const EMPTY_TUPLE_ID: HeapId = HeapId(0);

/// Color tag used by the trial-deletion cycle collector (Bacon–Rajan, ECOOP 2001).
///
/// Each [`HeapEntry`] carries a color that represents what the collector currently
/// believes about the entry. Outside of a running collection, every reachable
/// entry is either [`Black`](Self::Black) (live, not part of any suspected cycle)
/// or [`Purple`](Self::Purple) (a candidate cycle root discovered by `dec_ref`,
/// awaiting investigation). [`Gray`](Self::Gray) and [`White`](Self::White) are
/// transient states used only during a [`Heap::collect_cycles`] call.
///
/// The encoding fits in a single byte and is serialized as part of every
/// [`HeapEntry`]: a snapshot taken with cycles pending must round-trip through
/// serde so the entries stay enrolled as candidates after restore (otherwise a
/// graph that becomes garbage just before snapshot would leak permanently).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, serde::Serialize, serde::Deserialize)]
pub(crate) enum CcColor {
    /// Live and not currently a cycle candidate. Default state for every newly
    /// allocated entry.
    #[default]
    Black,
    /// Visited by `MarkGray` during a collection cycle. Children's refcounts
    /// have been provisionally decremented; a later `Scan` pass decides whether
    /// to resurrect (back to [`Black`](Self::Black)) or condemn
    /// ([`White`](Self::White)) the entry.
    Gray,
    /// Confirmed unreachable by the current collection: every reference into
    /// the entry comes from another condemned entry. `CollectWhite` will free
    /// it. Only seen mid-collection.
    White,
    /// Candidate cycle root. Set by `dec_ref` whenever a GC-tracked entry's
    /// refcount drops to a non-zero value — the only situation in which a new
    /// reference cycle can become unreachable. The collector seeds its work
    /// from every entry currently flagged Purple.
    Purple,
}

/// This structure allows for reading into the heap more efficiently than repeated calls to `Heap::get` and
/// `Heap::get_mut` by performing the indexing and type lookup once, and then using the borrow checker to
/// safely deference the resulting pointers for short-lived borrows.
///
/// The safety boundary is primarily that `HeapRead` pointers generated by the `HeapReader::read` API must remain valid
/// for their lifetime, see the safety notes in `HeapRead::get` for how that is guaranteed.
pub(crate) struct HeapReader<'a> {
    pub(crate) heap: &'a mut Heap,
    /// Makes the lifetime `'a` invariant.
    phantom: PhantomData<fn(&'a ()) -> &'a ()>,
}

impl HeapReader<'_> {
    /// The ONLY way to get a `HeapReader`. By only providing an API which takes a closure which
    /// must be satisfied for all `'a`, it's impossible to create other `HeapReader` with the
    /// exact same lifetime `'a`.
    ///
    /// To allow other data to be borrowed alongside the `HeapReader`, the closure is given a
    /// `&'a mut D` with the same lifetime as the `HeapReader`, which is forwarded from the
    /// `&mut D` passed to this function. This rebranding lets callers (most notably the
    /// `VM`) hold borrows whose lifetime matches the `HeapReader`'s invariant brand `'a`,
    /// which is what allows the `VM` to be parameterized by a single lifetime.
    pub fn with<R, D: ?Sized>(
        heap: &mut Heap,
        data: &mut D,
        f: impl for<'a> FnOnce(&'a mut HeapReader<'a>, &'a mut D) -> R,
    ) -> R {
        f(
            &mut HeapReader {
                heap: &mut *heap,
                phantom: PhantomData,
            },
            data,
        )
    }
}

impl<'a> HeapReader<'a> {
    /// Resolves a `HeapId` to a stable, branded [`HeapPtr<'a>`] for its entry.
    ///
    /// The returned `HeapPtr` can be used for efficient repeated access to the same entry
    /// without needing to re-index into the paged storage on every access.
    ///
    /// # Panics
    ///
    /// Panics if `id` is out of bounds.
    pub(crate) fn read_ptr(&self, id: HeapId) -> HeapPtr<'a> {
        // SAFETY: [DH] - `HeapPtr` prevents holding reference to freed slots across calls to allocate; it
        // always hands out either live `&HeapData` or `None`, never `&Option<HeapData>`.
        let slot = unsafe { self.heap.entries.slot_at(id) }.expect("HeapReader::read_ptr - id out of bounds");
        HeapPtr {
            inner: NonNull::from(slot),
            brand: PhantomData,
        }
    }

    /// Indexes into the heap.
    ///
    /// Thin wrapper around [`HeapPtr::read`]: resolves `id` to a `HeapPtr` and
    /// delegates the typed match/reader-count logic there. Panics if `id` is out
    /// of bounds or the slot is currently freed.
    pub fn read(&self, id: HeapId) -> HeapReadOutput<'a> {
        self.read_ptr(id).read(self)
    }

    #[expect(clippy::unused_self, reason = "'a lifetime is used to create the safety guarantees")]
    pub fn protect<'t, U: ?Sized>(&mut self, value: &'t U) -> BorrowedHeapRead<'t, 'a, U> {
        BorrowedHeapRead {
            inner: ManuallyDrop::new(HeapRead {
                value: NonNull::from(value),
                readers: NonNull::dangling(),
                borrow: PhantomData,
            }),
            original: PhantomData,
        }
    }

    #[expect(clippy::unused_self, reason = "'a lifetime is used to create the safety guarantees")]
    pub fn protect_mut<'t, U: ?Sized>(&mut self, value: &'t mut U) -> BorrowedHeapReadMut<'t, 'a, U> {
        BorrowedHeapReadMut {
            inner: ManuallyDrop::new(HeapRead {
                value: NonNull::from(value),
                readers: NonNull::dangling(),
                borrow: PhantomData,
            }),
            original: PhantomData,
        }
    }
}

impl ContainsHeap for HeapReader<'_> {
    fn heap(&self) -> &Heap {
        self.heap.heap()
    }
    fn heap_mut(&mut self) -> &mut Heap {
        self.heap.heap_mut()
    }
}

impl Deref for HeapReader<'_> {
    type Target = Heap;

    fn deref(&self) -> &Self::Target {
        self.heap
    }
}

impl DerefMut for HeapReader<'_> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.heap
    }
}

pub enum HeapReadOutput<'a> {
    Str(HeapRead<'a, Str>),
    Bytes(HeapRead<'a, Bytes>),
    List(HeapRead<'a, List>),
    Deque(HeapRead<'a, Deque>),
    Tuple(HeapRead<'a, Tuple>),
    NamedTuple(HeapRead<'a, NamedTuple>),
    NamedTupleClass(HeapRead<'a, NamedTupleClass>),
    Dict(HeapRead<'a, Dict>),
    DictItemsView(HeapRead<'a, DictItemsView>),
    DictKeysView(HeapRead<'a, DictKeysView>),
    DictValuesView(HeapRead<'a, DictValuesView>),
    Set(HeapRead<'a, Set>),
    FrozenSet(HeapRead<'a, FrozenSet>),
    Closure(HeapRead<'a, Closure>),
    FunctionDefaults(HeapRead<'a, FunctionDefaults>),
    ExtFunction(HeapRead<'a, ExtFunction>),
    Cell(HeapRead<'a, CellValue>),
    Range(HeapRead<'a, Range>),
    Slice(HeapRead<'a, Slice>),
    Exception(HeapRead<'a, SimpleException>),
    Dataclass(HeapRead<'a, Dataclass>),
    Class(HeapRead<'a, Class>),
    Instance(HeapRead<'a, Instance>),
    BoundMethod(HeapRead<'a, BoundMethod>),
    DataclassField(HeapRead<'a, DataclassField>),
    ListIterator(HeapRead<'a, ListIterator>),
    DequeIterator(HeapRead<'a, DequeIterator>),
    TupleIterator(HeapRead<'a, TupleIterator>),
    StringIterator(HeapRead<'a, StringIterator>),
    BytesIterator(HeapRead<'a, BytesIterator>),
    RangeIterator(HeapRead<'a, RangeIterator>),
    DictKeyIterator(HeapRead<'a, DictKeyIterator>),
    DictItemIterator(HeapRead<'a, DictItemIterator>),
    DictValueIterator(HeapRead<'a, DictValueIterator>),
    SetIterator(HeapRead<'a, SetIterator>),
    CallableIterator(HeapRead<'a, CallableIterator>),
    Itertools(HeapRead<'a, ItertoolsIter>),
    LongInt(HeapRead<'a, LongInt>),
    Module(HeapRead<'a, Module>),
    Coroutine(HeapRead<'a, Coroutine>),
    GatherFuture(HeapRead<'a, GatherFuture>),
    ExternalFuture(HeapRead<'a, ExternalFuture>),
    Path(HeapRead<'a, Path>),
    OpenFile(HeapRead<'a, OpenFile>),
    RePattern(HeapRead<'a, RePattern>),
    ReMatch(HeapRead<'a, ReMatch>),
    Date(HeapRead<'a, date::Date>),
    DateTime(HeapRead<'a, datetime::DateTime>),
    TimeDelta(HeapRead<'a, timedelta::TimeDelta>),
    TimeZone(HeapRead<'a, timezone::TimeZone>),
}

pub struct HeapRead<'a, T: ?Sized> {
    value: NonNull<T>,
    /// Pointer to the `readers` counter in the owning `HeapValue`.
    ///
    /// Incremented on creation, decremented on drop. This ensures `dec_ref`
    /// cannot free the entry while any `HeapRead` pointing into it exists.
    readers: NonNull<Cell<usize>>,
    /// Makes the lifetime `'a` invariant. In combination with the invariant lifetime
    /// on `HeapReader` and the `HeapReader::with` API, this guarantees that this
    /// `HeapRead` originated from that matching `HeapReader` (there is no way to
    /// construct another `HeapReader` with the same lifetime).
    borrow: PhantomData<fn(&'a T) -> &'a T>,
}

impl<T: ?Sized> Drop for HeapRead<'_, T> {
    fn drop(&mut self) {
        // SAFETY: (DH) the readers pointer is valid for the lifetime of the HeapValue,
        // which is guaranteed by the paged storage (addresses never move) and the
        // reader count itself (dec_ref cannot free an entry with active readers).
        let cell = unsafe { self.readers.as_ref() };
        cell.set(cell.get() - 1);
    }
}

impl<'a, T: ?Sized> HeapRead<'a, T> {
    /// Accesses the value contained in this reference.
    pub fn get<'r>(&self, _: &'r HeapReader<'a>) -> &'r T {
        // SAFETY: (DH)
        //  - The HeapReader has an invariant lifetime 'a which guarantees that this HeapRead
        //    came from the heap borrowed by this HeapReader.
        //  - The address of the `HeapValue` never changes because entries are stored in
        //    paged storage (`HeapEntries`) where each page is never reallocated or moved.
        //  - The HeapRead holds a strong reader reference (via the `readers` counter in
        //    `HeapValue`) which guarantees the entry will never be freed by `dec_ref`
        //    or `collect_cycles` while this `HeapRead` exists. The cycle collector's
        //    `Scan` phase treats `readers > 0` as an external reference and resurrects
        //    the entry to Black instead of condemning it as White.
        //  - The type of the `HeapValue` can never change once allocated. This is
        //    guaranteed by never exposing `&mut HeapData` outside of this module.
        //  - The borrow on `HeapReader` guarantees that there are no mutable borrows on any heap
        //    data while the return value of this function is alive.
        unsafe { self.value.as_ref() }
    }

    /// Mutably accesses the value contained in this reference.
    pub fn get_mut<'r>(&mut self, _: &'r mut HeapReader<'a>) -> &'r mut T {
        // SAFETY: see same constraints as in get() above.
        unsafe { self.value.as_mut() }
    }

    /// Casts this reader to a field of type `U` at some `offset` within the struct.
    ///
    /// Transfers ownership of the reader count from `self` to the returned `HeapRead`.
    ///
    /// # Safety
    ///   - The field of type `U` must ALWAYS exist at `offset` within `T` (i.e. `T` cannot be an enum, union etc)
    unsafe fn cast_as_member_ref<U>(&self, offset: usize) -> BorrowedHeapRead<'_, 'a, U> {
        BorrowedHeapRead {
            // SAFETY: (DH) - caller of this function guarantees the offset & cast is valid
            inner: ManuallyDrop::new(HeapRead {
                // SAFETY: caller guarantees offset points to a valid field of type U within T
                value: unsafe { self.value.byte_add(offset) }.cast(),
                // dangling is fine because this heapread will never be dropped, and it is
                // also not `Clone` so there's no risk of this value ever being used
                readers: NonNull::dangling(),
                borrow: PhantomData,
            }),
            original: PhantomData,
        }
    }

    /// Casts this reader to a field of type `U` at some `offset` within the struct.
    ///
    /// Transfers ownership of the reader count from `self` to the returned `HeapRead`.
    ///
    /// # Safety
    ///   - The field of type `U` must ALWAYS exist at `offset` within `T` (i.e. `T` cannot be an enum, union etc)
    unsafe fn cast_as_member_ref_mut<U>(&mut self, offset: usize) -> BorrowedHeapReadMut<'_, 'a, U> {
        BorrowedHeapReadMut {
            // SAFETY: (DH) - caller of this function guarantees the offset & cast is valid
            inner: ManuallyDrop::new(HeapRead {
                // SAFETY: caller guarantees offset points to a valid field of type U within T
                value: unsafe { self.value.byte_add(offset) }.cast(),
                // dangling is fine because this heapread will never be dropped, and it is
                // also not `Clone` so there's no risk of this value ever being used
                readers: NonNull::dangling(),
                borrow: PhantomData,
            }),
            original: PhantomData,
        }
    }
}

impl<'a, T> HeapRead<'a, Vec<T>> {
    pub fn as_slice(&self, reader: &HeapReader<'a>) -> BorrowedHeapRead<'_, 'a, [T]> {
        BorrowedHeapRead {
            inner: ManuallyDrop::new(HeapRead {
                value: NonNull::from(self.get(reader).as_slice()),
                readers: NonNull::dangling(),
                borrow: PhantomData,
            }),
            original: PhantomData,
        }
    }
}

impl<'a, T: ?Sized> HeapRead<'a, Box<T>> {
    pub fn as_box_value(&self, reader: &HeapReader<'a>) -> BorrowedHeapRead<'_, 'a, T> {
        BorrowedHeapRead {
            inner: ManuallyDrop::new(HeapRead {
                value: NonNull::from(self.get(reader).as_ref()),
                readers: NonNull::dangling(),
                borrow: PhantomData,
            }),
            original: PhantomData,
        }
    }
}

/// Represents the reborrow of a `HeapRead` as a reference to a field of the original type.
pub struct BorrowedHeapRead<'original, 'a, U: ?Sized> {
    // inner is a projected HeapRead which will never be dropped
    inner: ManuallyDrop<HeapRead<'a, U>>,
    original: PhantomData<&'original U>,
}

// NB no DerefMut - would need to have a `BorrowedHeapReadMut`
impl<'a, U: ?Sized> Deref for BorrowedHeapRead<'_, 'a, U> {
    type Target = HeapRead<'a, U>;

    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

/// Unsafe helper for `heap_read_as_field`, do not use. Same safety invariants as `HeapRead::cast_as_member`.
pub(crate) unsafe fn cast_as_member_ref_type_hinted<'r, 'a, T, U>(
    heap_read: &'r HeapRead<'a, T>,
    offset: usize,
    _type_hint: impl for<'s> Fn(&'s HeapRead<'a, T>) -> *const U,
) -> BorrowedHeapRead<'r, 'a, U> {
    // SAFETY: (DH) - caller upholds `cast_as_member` contract
    unsafe { heap_read.cast_as_member_ref(offset) }
}

macro_rules! heap_read_ref_as_field {
    ($heap_read:ident, $ty:ty, $field:tt) => {{
        let offset = std::mem::offset_of!($ty, $field);
        #[expect(unreachable_code)]
        let type_hint = |read: &$crate::heap::HeapRead<'_, $ty>| &raw const read.get(unreachable!()).$field;
        // SAFETY: (DH)
        //  - `std::mem::offset_of!` guarantees there is a field at fixed offset
        //  - `type_hint` guarantees that the field is of type `U` for the safety contract
        #[expect(unsafe_code)]
        unsafe {
            $crate::heap::cast_as_member_ref_type_hinted($heap_read, offset, type_hint)
        }
    }};
}

pub(crate) use heap_read_ref_as_field;

/// Represents the reborrow of a `HeapRead` as a reference to a field of the original type.
pub struct BorrowedHeapReadMut<'original, 'a, U: ?Sized> {
    // inner is a projected HeapRead which will never be dropped
    inner: ManuallyDrop<HeapRead<'a, U>>,
    original: PhantomData<&'original mut U>,
}

impl<'a, U: ?Sized> Deref for BorrowedHeapReadMut<'_, 'a, U> {
    type Target = HeapRead<'a, U>;

    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

impl<U: ?Sized> DerefMut for BorrowedHeapReadMut<'_, '_, U> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.inner
    }
}

/// Unsafe helper for `heap_read_as_field`, do not use. Same safety invariants as `HeapRead::cast_as_member`.
pub(crate) unsafe fn cast_as_member_ref_mut_type_hinted<'r, 'a, T, U>(
    heap_read: &'r mut HeapRead<'a, T>,
    offset: usize,
    _type_hint: impl for<'s> Fn(&'s HeapRead<'a, T>) -> *const U,
) -> BorrowedHeapReadMut<'r, 'a, U> {
    // SAFETY: (DH) - caller upholds `cast_as_member` contract
    unsafe { heap_read.cast_as_member_ref_mut(offset) }
}

macro_rules! heap_read_ref_as_field_mut {
    ($heap_read:ident, $ty:ty, $field:tt) => {{
        let offset = std::mem::offset_of!($ty, $field);
        #[expect(unreachable_code)]
        let type_hint = |read: &$crate::heap::HeapRead<'_, $ty>| &raw const read.get(unreachable!()).$field;
        // SAFETY: (DH)
        //  - `std::mem::offset_of!` guarantees there is a field at fixed offset
        //  - `type_hint` guarantees that the field is of type `U` for the safety contract
        #[expect(unsafe_code)]
        unsafe {
            $crate::heap::cast_as_member_ref_mut_type_hinted($heap_read, offset, type_hint)
        }
    }};
}

pub(crate) use heap_read_ref_as_field_mut;

/// Stable, branded pointer to a heap slot's `Option<HeapEntry>`.
///
/// `HeapPtr<'a>` carries the same invariant lifetime `'a` as the [`HeapReader<'a>`]
/// that produced it (via [`HeapReader::entry_ptr`]). The brand mechanism is identical
/// to the one on [`HeapRead`]: pointers minted by one reader cannot be dereferenced
/// via a reader from a different `HeapReader::with` scope (or, looking ahead, a
/// different [`Heap`]), so same-heap origin is checked at compile time rather than
/// trusted by convention.
///
/// The pointer addresses the *slot's `Option<HeapEntry>`*, not the inner `HeapEntry`
/// directly. This makes it well-defined across the full lifetime of the slot — even
/// across `dec_ref`-driven frees and subsequent reuse by `allocate` — because the
/// slot's memory location never moves; only its `Some`/`None` state changes.
///
/// This mirrors the semantics of [`Heap::get`]/[`StableHeap::entry`] where the outer
/// `Option` is the live/freed signal.
///
/// Used as a fast handle inside paths that would otherwise re-index into paged storage
/// per access — currently the cycle collector, which converts `HeapId → HeapPtr` once
/// at push-time and uses the pointer directly on every subsequent pop, avoiding the
/// `pages[page_idx][slot_idx]` lookup chain.
#[derive(Copy, Clone)]
#[repr(transparent)]
pub(crate) struct HeapPtr<'a> {
    inner: NonNull<Option<HeapEntry>>,
    /// Makes `'a` invariant. Matches the brand on [`HeapReader<'a>`] / [`HeapRead<'a, T>`]
    /// so a `HeapPtr` cannot be reborrowed under a different reader scope.
    brand: PhantomData<fn(&'a ()) -> &'a ()>,
}

impl<'a> HeapPtr<'a> {
    /// Returns the live [`HeapEntry`] this pointer refers to, panicking if the slot
    /// has been freed.
    ///
    /// All `HeapEntry` fields are interior-mutable — `refcount`/`readers`/`color`
    /// via `Cell` and `data` via `UnsafeCell` — so callers can mutate them through
    /// the returned `&HeapEntry` without ever needing `&mut HeapEntry`. That's
    /// what makes a `&self`-derived `HeapPtr` (with Shared provenance) sound to
    /// dereference: we never derive `&mut` from it, so the SB/TB rules permit
    /// interior mutation via the embedded `Cell`/`UnsafeCell`.
    ///
    /// Use [`Self::try_entry`] for code paths that may legitimately encounter
    /// freed slots (e.g. linear scans over `0..heap.entries.len()`).
    pub fn entry<'r>(self, reader: &'r HeapReader<'a>) -> &'r HeapEntry {
        self.try_entry(reader).expect("HeapPtr::entry: slot has been freed")
    }

    /// Returns the [`HeapEntry`] this pointer refers to, or `None` if the slot is
    /// currently freed.
    ///
    /// Use where a freed slot is part of the expected state (linear scans, root
    /// reseeds, etc.). Mutation paths (cycle collector mark/scan inner loops) should
    /// prefer [`Self::entry`] so that an unexpectedly-freed entry surfaces as a
    /// loud panic rather than a silent skip.
    pub(crate) fn try_entry<'r>(self, _reader: &'r HeapReader<'a>) -> Option<&'r HeapEntry> {
        // SAFETY:
        //  - The invariant `'a` on `_reader` matches this pointer's brand, which is
        //    only settable inside `HeapReader::with`. That guarantees same-heap
        //    origin: a `HeapPtr<'a>` from a different reader scope cannot satisfy
        //    this signature.
        //  - `StableHeap::entry_ptr` only returns pointers to initialized slots, so
        //    the `Option<HeapEntry>` behind the pointer is always a valid place.
        //  - The `&HeapReader` borrow excludes any `&mut HeapReader` op that could
        //    free the slot during the returned reference's lifetime.
        unsafe { self.inner.as_ref() }.as_ref()
    }

    /// Returns the entry's [`HeapData`] payload via the `UnsafeHeapData` interior-
    /// mutability boundary.
    ///
    /// `UnsafeCell::get` produces a `*mut HeapData` with `SharedReadWrite` permission,
    /// so the returned `&HeapData` is sound even though the underlying `HeapPtr` was
    /// minted via `&self`-derived `entry_ptr`. Mutation of the payload still requires
    /// `&mut HeapReader` via the [`HeapRead`] machinery.
    pub fn data<'r>(self, reader: &'r HeapReader<'a>) -> &'r HeapData {
        let entry = self.entry(reader);
        // SAFETY: `UnsafeCell::get` yields a `SharedReadWrite` pointer; the `&HeapReader`
        // borrow on `reader` excludes any concurrent `&mut HeapReader` operation that
        // could mutate the payload during the returned reference's lifetime.
        unsafe { &*entry.data.0.get() }
    }

    /// Returns the typed [`HeapReadOutput`] for this entry, incrementing the
    /// reader count so the produced [`HeapRead<T>`] handles participate in the
    /// reader-count GC safety net (they are decremented on `Drop`).
    ///
    /// All `HeapRead<T>` handle pointers are derived through the `UnsafeHeapData`
    /// `UnsafeCell`, so they retain `SharedReadWrite` permission and remain valid
    /// for both read and mutable access (the latter via `HeapRead::get_mut`,
    /// which requires `&mut HeapReader`).
    pub fn read(self, reader: &HeapReader<'a>) -> HeapReadOutput<'a> {
        /// Computes a `HeapRead` from the raw `UnsafeCell` pointer and a shared reference
        /// to the variant field. The `&T` is only used to compute the field's byte offset
        /// within the `HeapData` enum; the returned `NonNull` is derived from the original
        /// `*mut HeapData` pointer so it inherits the `SharedReadWrite` permission from
        /// the `UnsafeCell`, allowing both reads and writes.
        #[inline]
        fn heap_read<'a, T>(base: *mut HeapData, field: &T, readers: NonNull<Cell<usize>>) -> HeapRead<'a, T> {
            let base_addr = base as usize;
            let field_addr = ptr::from_ref(field) as usize;
            let offset = field_addr - base_addr;
            HeapRead {
                // SAFETY: The pointer is derived from the UnsafeCell's `*mut` via byte
                // offset, preserving the `SharedReadWrite` permission. No reference retag
                // occurs — we only use the `&T` for its address, not to derive the pointer.
                value: unsafe { NonNull::new_unchecked(base.byte_add(offset).cast::<T>()) },
                readers,
                borrow: PhantomData,
            }
        }

        /// Like `heap_read` but for `Box<T>` fields inside `HeapData` variants.
        ///
        /// The `&Box<T>` is only used to locate the box field inside the enum; the
        /// inner pointer is *loaded out of* that field rather than derived by
        /// dereferencing the box. Dereferencing (`boxed.as_ref()`) would create a
        /// shared `&T` whose `SharedReadOnly` provenance makes later
        /// `HeapRead::get_mut` writes UB; the loaded pointer instead carries the
        /// box's own stored (writeable) provenance.
        #[expect(
            clippy::borrowed_box,
            reason = "We intentionally take &Box<T> to signal this is for boxed HeapData variants; &T would lose that context"
        )]
        fn heap_read_boxed<'a, T>(
            base: *mut HeapData,
            boxed: &Box<T>,
            readers: NonNull<Cell<usize>>,
        ) -> HeapRead<'a, T> {
            let base_addr = base as usize;
            let field_addr = ptr::from_ref(boxed) as usize;
            let offset = field_addr - base_addr;
            // SAFETY: `offset` locates the live `Box<T>` field within the enum, and the
            // pointer to it is derived from the `UnsafeCell`'s `*mut` (not from `&Box<T>`),
            // preserving `SharedReadWrite` permission for the load below. `Box<T>` with
            // `T: Sized` is guaranteed to be represented as a single non-null pointer, so
            // loading the field as `*mut T` yields the box's data pointer together with
            // the read/write provenance it was stored with.
            let value = unsafe { NonNull::new_unchecked(base.byte_add(offset).cast::<*mut T>().read()) };
            HeapRead {
                value,
                readers,
                borrow: PhantomData,
            }
        }

        let entry = self.entry(reader);
        // Increment the reader count for this entry. The corresponding decrement
        // happens in `HeapRead::drop`.
        entry.readers.set(entry.readers.get() + 1);
        let readers = NonNull::from(&entry.readers);
        // Get the raw pointer from the UnsafeCell — this has SharedReadWrite permission.
        let base: *mut HeapData = entry.data.0.get();
        // SAFETY: Match on a shared reference (`&*base`) to read the discriminant without
        // creating a Unique retag. The shared retag is compatible with existing
        // SharedReadWrite permissions from prior `read()` calls into the same UnsafeCell.
        // The `heap_read` helper then derives the NonNull from `base` (not from `&T`),
        // so the returned pointer retains full SharedReadWrite permission.
        match unsafe { &*base } {
            HeapData::Str(s) => HeapReadOutput::Str(heap_read(base, s, readers)),
            HeapData::Bytes(bytes) => HeapReadOutput::Bytes(heap_read(base, bytes, readers)),
            HeapData::List(list) => HeapReadOutput::List(heap_read(base, list, readers)),
            HeapData::Deque(deque) => HeapReadOutput::Deque(heap_read(base, deque, readers)),
            HeapData::Tuple(tuple) => HeapReadOutput::Tuple(heap_read(base, tuple, readers)),
            HeapData::NamedTuple(named_tuple) => {
                HeapReadOutput::NamedTuple(heap_read_boxed(base, named_tuple, readers))
            }
            HeapData::NamedTupleClass(class) => HeapReadOutput::NamedTupleClass(heap_read_boxed(base, class, readers)),
            HeapData::Dict(dict) => HeapReadOutput::Dict(heap_read(base, dict, readers)),
            HeapData::DictItemsView(v) => HeapReadOutput::DictItemsView(heap_read(base, v, readers)),
            HeapData::DictKeysView(v) => HeapReadOutput::DictKeysView(heap_read(base, v, readers)),
            HeapData::DictValuesView(v) => HeapReadOutput::DictValuesView(heap_read(base, v, readers)),
            HeapData::Set(set) => HeapReadOutput::Set(heap_read(base, set, readers)),
            HeapData::FrozenSet(frozen_set) => HeapReadOutput::FrozenSet(heap_read(base, frozen_set, readers)),
            HeapData::Closure(closure) => HeapReadOutput::Closure(heap_read(base, closure, readers)),
            HeapData::FunctionDefaults(function_defaults) => {
                HeapReadOutput::FunctionDefaults(heap_read(base, function_defaults, readers))
            }
            HeapData::ExtFunction(name) => HeapReadOutput::ExtFunction(heap_read(base, name, readers)),
            HeapData::Cell(cell_value) => HeapReadOutput::Cell(heap_read(base, cell_value, readers)),
            HeapData::Range(range) => HeapReadOutput::Range(heap_read(base, range, readers)),
            HeapData::Slice(slice) => HeapReadOutput::Slice(heap_read(base, slice, readers)),
            HeapData::Exception(simple_exception) => {
                HeapReadOutput::Exception(heap_read(base, simple_exception, readers))
            }
            HeapData::Dataclass(dataclass) => HeapReadOutput::Dataclass(heap_read_boxed(base, dataclass, readers)),
            HeapData::Class(class) => HeapReadOutput::Class(heap_read_boxed(base, class, readers)),
            HeapData::Instance(instance) => HeapReadOutput::Instance(heap_read_boxed(base, instance, readers)),
            HeapData::BoundMethod(bound_method) => HeapReadOutput::BoundMethod(heap_read(base, bound_method, readers)),
            HeapData::DataclassField(field) => HeapReadOutput::DataclassField(heap_read(base, field, readers)),
            HeapData::ListIterator(iter) => HeapReadOutput::ListIterator(heap_read(base, iter, readers)),
            HeapData::DequeIterator(iter) => HeapReadOutput::DequeIterator(heap_read(base, iter, readers)),
            HeapData::TupleIterator(iter) => HeapReadOutput::TupleIterator(heap_read(base, iter, readers)),
            HeapData::StringIterator(iter) => HeapReadOutput::StringIterator(heap_read(base, iter, readers)),
            HeapData::BytesIterator(iter) => HeapReadOutput::BytesIterator(heap_read(base, iter, readers)),
            HeapData::RangeIterator(iter) => HeapReadOutput::RangeIterator(heap_read(base, iter, readers)),
            HeapData::DictKeyIterator(iter) => HeapReadOutput::DictKeyIterator(heap_read(base, iter, readers)),
            HeapData::DictItemIterator(iter) => HeapReadOutput::DictItemIterator(heap_read(base, iter, readers)),
            HeapData::DictValueIterator(iter) => HeapReadOutput::DictValueIterator(heap_read(base, iter, readers)),
            HeapData::SetIterator(iter) => HeapReadOutput::SetIterator(heap_read(base, iter, readers)),
            HeapData::CallableIterator(c) => HeapReadOutput::CallableIterator(heap_read(base, c, readers)),
            HeapData::Itertools(i) => HeapReadOutput::Itertools(heap_read(base, i, readers)),
            HeapData::LongInt(l) => HeapReadOutput::LongInt(heap_read(base, l, readers)),
            HeapData::Module(module) => HeapReadOutput::Module(heap_read_boxed(base, module, readers)),
            HeapData::Coroutine(coroutine) => HeapReadOutput::Coroutine(heap_read(base, coroutine, readers)),
            HeapData::GatherFuture(gather_future) => {
                HeapReadOutput::GatherFuture(heap_read_boxed(base, gather_future, readers))
            }
            HeapData::ExternalFuture(external_future) => {
                HeapReadOutput::ExternalFuture(heap_read_boxed(base, external_future, readers))
            }
            HeapData::Path(path) => HeapReadOutput::Path(heap_read(base, path, readers)),
            HeapData::OpenFile(file) => HeapReadOutput::OpenFile(heap_read_boxed(base, file, readers)),
            HeapData::RePattern(re_pattern) => HeapReadOutput::RePattern(heap_read_boxed(base, re_pattern, readers)),
            HeapData::ReMatch(re_match) => HeapReadOutput::ReMatch(heap_read_boxed(base, re_match, readers)),
            HeapData::Date(d) => HeapReadOutput::Date(heap_read(base, d, readers)),
            HeapData::DateTime(d) => HeapReadOutput::DateTime(heap_read(base, d, readers)),
            HeapData::TimeDelta(d) => HeapReadOutput::TimeDelta(heap_read(base, d, readers)),
            HeapData::TimeZone(d) => HeapReadOutput::TimeZone(heap_read(base, d, readers)),
        }
    }
}

/// A single entry inside the heap arena, storing refcount and payload.
///
/// Hashing state lives on the per-type structs that benefit from it
/// ([`Str`], [`Bytes`], [`Tuple`], [`NamedTuple`], [`FrozenSet`] (via
/// `SetStorage`), [`Path`]). Cheap-to-hash types ([`Range`], [`Slice`],
/// dates etc.) recompute on demand. Unhashable types ([`List`], [`Dict`],
/// [`Set`]) return `None` from `py_hash` directly. None of these need
/// per-entry metadata.
///
/// The `color` field encodes the entry's state for the trial-deletion cycle
/// collector (see [`CcColor`]). Outside of a running collection, every live
/// entry is either Black (uninteresting) or Purple (a cycle-root candidate
/// queued for investigation); Gray and White are transient states only seen
/// during [`Heap::collect_cycles`]. Cell-typed for symmetry with `refcount`
/// — every write goes through an `&mut Heap` path (`dec_ref` and the
/// collector's `mark_gray`/`scan`/`scan_black`).
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct HeapEntry {
    refcount: Cell<usize>,
    /// Number of active `HeapRead` pointers into this entry's data.
    ///
    /// Incremented when `HeapReader::read` creates a `HeapRead`, decremented when
    /// the `HeapRead` is dropped. `dec_ref` panics if it would free an entry that
    /// still has active readers — this guarantees that `HeapRead` pointers remain
    /// valid for as long as they exist.
    #[serde(skip, default)] // should always be 0 during serde ops
    readers: Cell<usize>,
    /// The payload data
    data: UnsafeHeapData,
    /// Cycle-collector color. See [`CcColor`].
    ///
    /// Round-trips through serde because a snapshot taken between bytecode
    /// instructions can capture entries in the [`Purple`](CcColor::Purple)
    /// pending-collection state; dropping the color on restore would leak
    /// any cycle that became unreachable just before the snapshot.
    #[serde(default)]
    color: Cell<CcColor>,
}

/// This wrapper containing `UnsafeCell` exists to allow for data inside of `HeapValue`
/// to be safely pointed to via the `HeapReader` API.
///
/// The safety invariants are protected by the `Heap` / `HeapReader` API:
///   - It is never possible to alias mutable and immutable borrows into heap values,
///     whether they are the same or different value.
///   - When a mutable borrow of a heap value exists, no other heap value may be
///     borrowed. (See `Heap::get_mut` and `HeapRead::get`, which both require a `&mut`
///     borrow on the heap.)
struct UnsafeHeapData(UnsafeCell<HeapData>);

impl fmt::Debug for UnsafeHeapData {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // SAFETY: (DH) Debug formatting is read-only and never called concurrently
        // with mutation. This matches the safety invariants of the HeapReader API.
        let data = unsafe { &*self.0.get() };
        f.debug_tuple("UnsafeHeapData").field(data).finish()
    }
}

impl serde::Serialize for UnsafeHeapData {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        // SAFETY: when heap data is being serialized, there is no mutable borrow
        // possible on any data contents
        HeapData::serialize(unsafe { &*self.0.get() }, serializer)
    }
}

impl<'de> serde::Deserialize<'de> for UnsafeHeapData {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        Ok(Self(UnsafeCell::new(HeapData::deserialize(deserializer)?)))
    }
}

/// Reference-counted arena that backs all heap-only runtime values.
///
/// Uses a free list to reuse slots from freed values, keeping memory usage
/// constant for long-running loops that repeatedly allocate and free values.
/// When an value is freed via `dec_ref`, its slot ID is added to the free list.
/// New allocations pop from the free list when available, otherwise append.
///
/// Cycle collection uses Bacon–Rajan trial deletion: candidates come from
/// `dec_ref` (every container whose refcount drops to a non-zero value is
/// flagged [`Purple`](CcColor::Purple)), so the VM does not enumerate live
/// roots — refcount math itself proves reachability and values held only on
/// the Rust stack are correctly preserved by their non-zero refcount.
///
/// Owns the [`ResourceTracker`] that enforces memory/time limits and schedules
/// GC; unconfigured limits reduce every check to a single predictable branch.
#[derive(Debug)]
pub(crate) struct Heap {
    /// Paged storage for heap entries with integrated free list.
    entries: StableHeap<HeapEntry>,
    /// Resource tracker for enforcing limits and scheduling GC.
    tracker: ResourceTracker,
    /// Number of entries currently flagged [`Purple`](CcColor::Purple) — i.e.,
    /// suspected cycle roots awaiting collection.
    ///
    /// Used as an early-out: when zero, `collect_cycles` has no candidates
    /// and skips its heap walk entirely. The actual GC *frequency* is still
    /// driven by `allocations_since_gc` against the configured interval, so
    /// programs that produce no cycle candidates pay no collector cost
    /// regardless of how many allocations they perform.
    ///
    /// All `dec_ref` paths that mutate this counter take `&mut self`, so a
    /// plain `usize` is sufficient (no interior mutability needed).
    purple_count: usize,
    /// Number of GC-applicable allocations since the last cycle collection.
    ///
    /// Incremented for every GC-tracked allocation (see [`HeapData::is_gc_tracked`])
    /// and reset to zero at the end of every successful [`collect_cycles`]
    /// call. Combined with [`purple_count`](Self::purple_count) to gate
    /// automatic collections in [`should_gc`](Self::should_gc).
    ///
    /// Uses `Cell` for interior mutability so that `allocate(&self)` can
    /// increment.
    allocations_since_gc: Cell<u32>,
    /// When true, [`should_gc`](Self::should_gc) returns false regardless of
    /// the candidate count, suppressing automatic cycle-collection passes.
    /// Toggled by the `gc.disable()` / `gc.enable()` Python helpers (only
    /// registered under the `test-hooks` feature). Explicit `gc.collect()`
    /// calls still run.
    #[cfg(feature = "test-hooks")]
    gc_disabled: bool,
    /// Cached HeapId for the `datetime.timezone.utc` singleton.
    ///
    /// Lazily allocated on first access to `timezone.utc`. Once created, the refcount
    /// is incremented on each access so the caller can drop their reference normally.
    timezone_utc: Option<HeapId>,
    /// Live external functions indexed by name without owning heap references.
    ///
    /// Uses `BTreeMap` to avoid large residual capacity from spikes of `ExtFunction` allocations.
    ext_function_cache: BTreeMap<Arc<str>, HeapId>,
}

impl serde::Serialize for Heap {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        let mut state = serializer.serialize_struct("Heap", 5)?;
        state.serialize_field("entries", &self.entries)?;
        state.serialize_field("tracker", &self.tracker)?;
        state.serialize_field("purple_count", &self.purple_count)?;
        state.serialize_field("allocations_since_gc", &self.allocations_since_gc.get())?;
        state.serialize_field("timezone_utc", &self.timezone_utc)?;
        state.end()
    }
}

impl<'de> serde::Deserialize<'de> for Heap {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        #[derive(serde::Deserialize)]
        struct HeapFields {
            entries: StableHeap<HeapEntry>,
            tracker: ResourceTracker,
            #[serde(default)]
            purple_count: usize,
            #[serde(default)]
            allocations_since_gc: u32,
            #[serde(default)]
            timezone_utc: Option<HeapId>,
        }
        let fields = HeapFields::deserialize(deserializer)?;
        let mut entries = fields.entries;
        let mut ext_function_cache = BTreeMap::new();
        for index in 0..entries.len() {
            let id = HeapId::from_index(index);
            if let Some(mut entry) = entries.entry(id)
                && let HeapData::ExtFunction(function) = entry.get_mut().data.0.get_mut()
            {
                ext_function_cache.insert(function.cache_key(), id);
            }
        }
        Ok(Self {
            entries,
            tracker: fields.tracker,
            purple_count: fields.purple_count,
            allocations_since_gc: Cell::new(fields.allocations_since_gc),
            #[cfg(feature = "test-hooks")]
            gc_disabled: false,
            timezone_utc: fields.timezone_utc,
            ext_function_cache,
        })
    }
}

/// Default GC interval — run cycle collection every 100 000 GC-tracked
/// allocations unless the configured resource tracker overrides it.
///
/// The trial-deletion collector additionally short-circuits the trace when
/// `purple_count == 0`, so programs that produce no cycle candidates pay no
/// collector cost regardless of their allocation rate.
///
/// When the `memory-model-checks` feature is enabled, this is reduced to 1 to
/// stress-test GC behavior on every allocation.
const DEFAULT_GC_INTERVAL: usize = if cfg!(feature = "memory-model-checks") {
    1
} else {
    100_000
};

impl Heap {
    /// Creates a new heap with the given resource tracker.
    ///
    /// Use this to create heaps with custom resource limits or GC scheduling.
    pub fn new(capacity: usize, tracker: ResourceTracker) -> Self {
        let this = Self {
            entries: StableHeap::with_capacity(capacity),
            tracker,
            purple_count: 0,
            allocations_since_gc: Cell::new(0),
            #[cfg(feature = "test-hooks")]
            gc_disabled: false,
            timezone_utc: None,
            ext_function_cache: BTreeMap::new(),
        };

        // The empty-tuple singleton starts with refcount = 1 — that single ref *is* the
        // permanent heap-owned reference. `get_empty_tuple` bumps the refcount on each
        // hand-out so callers can `dec_ref` normally; the heap-owned ref keeps the
        // singleton's rc ≥ 1 forever, which is why trial deletion needs no special-case
        // rooting for it (a debug_assert in `dec_ref` enforces the invariant).
        let empty_tuple = HeapData::Tuple(Tuple::default());
        let new_entry = HeapEntry {
            refcount: Cell::new(1),
            readers: Cell::new(0),
            data: UnsafeHeapData(UnsafeCell::new(empty_tuple)),
            color: Cell::new(CcColor::Black),
        };

        let empty_tuple = this.entries.allocate(new_entry);
        debug_assert_eq!(empty_tuple, EMPTY_TUPLE_ID);
        this
    }

    /// Returns a reference to the resource tracker.
    pub fn tracker(&self) -> &ResourceTracker {
        &self.tracker
    }

    /// Returns a mutable reference to the resource tracker.
    pub fn tracker_mut(&mut self) -> &mut ResourceTracker {
        &mut self.tracker
    }

    /// Checks whether a configured time or memory limit has been exceeded.
    ///
    /// Delegates to the resource tracker's `check_time()`, which polls
    /// allocator-backed usage against `max_memory` and elapsed execution time
    /// against `max_duration` (each a no-op when unset).
    ///
    /// Call this inside Rust-side loops (builtins, sort, iterator collection)
    /// that execute within a single bytecode instruction and would otherwise
    /// bypass the VM's per-instruction checkpoint.
    #[inline]
    pub fn check_time(&self) -> Result<(), ResourceError> {
        self.tracker.check_time()
    }

    /// Number of entries in the heap (including freed slots).
    pub fn size(&self) -> usize {
        self.entries.len()
    }

    /// Returns the number of GC-tracked allocations since the last cycle
    /// collection. Reset to zero by [`collect_cycles`](Self::collect_cycles).
    ///
    /// Used by `run_ref_counts` to expose the GC trigger metric to tests:
    /// a value much smaller than the configured `gc_interval` after many
    /// allocations is evidence that collection ran.
    #[cfg(feature = "ref-count-return")]
    pub fn get_allocations_since_gc(&self) -> u32 {
        self.allocations_since_gc.get()
    }

    /// Allocates a new heap entry.
    ///
    /// GC-tracked types bump `allocations_since_gc` so that
    /// [`should_gc`](Self::should_gc) eventually fires; trial deletion's own
    /// candidate enrollment happens later, at `dec_ref` time. Leaf types
    /// (strings, bytes, …) cannot participate in cycles and don't count
    /// against the GC interval.
    pub fn allocate(&self, data: HeapData) -> HeapId {
        if data.is_gc_tracked() {
            self.allocations_since_gc
                .set(self.allocations_since_gc.get().wrapping_add(1));
        }

        let new_entry = HeapEntry {
            refcount: Cell::new(1),
            readers: Cell::new(0),
            data: UnsafeHeapData(UnsafeCell::new(data)),
            color: Cell::new(CcColor::Black),
        };

        self.entries.allocate(new_entry)
    }

    /// Returns the singleton empty tuple.
    ///
    /// In Python, `() is ()` is always `True` because empty tuples are interned.
    /// This method provides the same optimization by returning the same `HeapId`
    /// for all empty tuple allocations.
    ///
    /// The returned `Value` has its reference count incremented, so the caller
    /// owns a reference and must call `dec_ref` when done.
    pub fn get_empty_tuple(&self) -> Value {
        // Return existing singleton with incremented refcount
        self.inc_ref(EMPTY_TUPLE_ID);
        Value::Ref(EMPTY_TUPLE_ID)
    }

    /// Returns the external function for `name`, reusing a live object when possible.
    ///
    /// The cache holds no reference count of its own. Its entry is removed when the
    /// last owning reference is dropped, before the heap slot can be reused.
    pub fn get_ext_function(&mut self, name: &str) -> Value {
        if let Some(id) = self.ext_function_cache.get(name).copied() {
            self.inc_ref(id);
            Value::Ref(id)
        } else {
            let function = ExtFunction::new(name);
            let cache_key = function.cache_key();
            let id = self.allocate(HeapData::ExtFunction(function));
            let previous = self.ext_function_cache.insert(cache_key, id);
            debug_assert!(previous.is_none());
            Value::Ref(id)
        }
    }

    /// Removes `id` from the weak cache without disturbing a duplicate-name winner.
    fn remove_ext_function_cache_entry(cache: &mut BTreeMap<Arc<str>, HeapId>, name: &str, id: HeapId) {
        if cache.get(name) == Some(&id) {
            cache.remove(name);
        }
    }

    /// Returns the cached `datetime.timezone.utc` singleton, lazily creating it on first access.
    ///
    /// The returned `Value::Ref` has its refcount incremented so the caller can drop
    /// it normally. The singleton itself is kept alive by the `timezone_utc` field.
    pub fn get_timezone_utc(&mut self) -> Value {
        if let Some(id) = self.timezone_utc {
            self.inc_ref(id);
            Value::Ref(id)
        } else {
            let tz = TimeZone::utc();
            let id = self.allocate(HeapData::TimeZone(tz));
            // Keep an extra refcount for the singleton cache
            self.inc_ref(id);
            self.timezone_utc = Some(id);
            Value::Ref(id)
        }
    }

    /// Increments the reference count for an existing heap entry.
    ///
    /// # Panics
    /// Panics if the value ID is invalid or the value has already been freed.
    pub fn inc_ref(&self, id: HeapId) {
        let value = self.entries.get(id);
        value.refcount.update(|r| r + 1);
    }

    /// Decrements the reference count and frees the value (plus children) once it hits zero.
    ///
    /// This is the low-level release operation for an owned raw `HeapId`. Ordinary
    /// control flow should instead keep local ownership in `Value::Ref` and use
    /// `defer_drop!` or `DropGuard`. Heap-stored owners declare child references in
    /// `HeapItem::py_dec_ref_ids`; direct calls are appropriate in cleanup for other
    /// structures that own raw IDs.
    ///
    /// Uses an iterative work stack instead of recursion to avoid Rust stack overflow
    /// when freeing deeply nested containers (e.g., a list nested 10,000 levels deep).
    /// This is analogous to CPython's "trashcan" mechanism for safe deallocation.
    ///
    /// Implements the candidate-enrollment side of Bacon–Rajan trial deletion: any
    /// GC-tracked entry whose refcount survives the decrement gets flagged
    /// [`Purple`](CcColor::Purple), so the next [`collect_cycles`](Self::collect_cycles)
    /// can investigate it. Entries that drop to zero are freed immediately on the
    /// existing fast path; if such an entry was Purple, the heap-wide
    /// `purple_count` is rebalanced so it stays in sync with the actual number
    /// of Purple entries.
    ///
    /// # Panics
    /// Panics if the value ID is invalid, the value has already been freed, or
    /// the refcount would reach zero while active `HeapRead` readers exist.
    pub fn dec_ref(&mut self, id: HeapId) {
        HeapReader::with(self, &mut (), |reader, ()| {
            let mut current_id = id;
            // A fresh Vec is deliberate: it costs nothing unless children are
            // actually pushed, whereas pooling it on the Heap was measured
            // (CodSpeed, PR #536) to add take/restore traffic to every call.
            let mut work_stack = Vec::new();
            loop {
                // Using `HeapPtr` avoids the possibility of aliasing with live borrows
                // held by `HeapRead` handles.
                let ptr = reader.read_ptr(current_id);
                let heap_entry = ptr.entry(reader);
                if heap_entry.refcount.get() > 1 {
                    heap_entry.refcount.update(|r| r - 1);

                    let is_gc_tracked = ptr.data(reader).is_gc_tracked();
                    if is_gc_tracked && heap_entry.color.get() != CcColor::Purple {
                        // The refcount survived — a newly unreachable cycle could
                        // now be hiding. Flag it as a candidate for the next `collect_cycles`.
                        heap_entry.color.set(CcColor::Purple);
                        reader.heap.purple_count += 1;
                    }
                } else {
                    debug_assert!(
                        current_id != EMPTY_TUPLE_ID,
                        "Heap::dec_ref: empty-tuple singleton's heap-owned refcount must never reach zero",
                    );
                    assert!(
                        heap_entry.readers.get() == 0,
                        "Heap::dec_ref: cannot free HeapId({}) with {} active reader(s)",
                        current_id.index(),
                        heap_entry.readers.get(),
                    );
                    // If the entry was a pending cycle candidate, decrement
                    // `purple_count` to reflect that it is leaving the heap before
                    // the collector reaches it.
                    if heap_entry.color.get() == CcColor::Purple {
                        reader.heap.purple_count -= 1;
                    }
                    // Remove weak-cache entries before the slot becomes available for reuse.
                    let ext_function_name = match ptr.data(reader) {
                        HeapData::ExtFunction(function) => Some(function.cache_key()),
                        _ => None,
                    };
                    // Clear the cache (only if it points to this exact function, it's possible for
                    // snapshot deserialization to create duplicate functions with the same name)
                    if let Some(name) = ext_function_name {
                        Self::remove_ext_function_cache_entry(&mut reader.heap.ext_function_cache, &name, current_id);
                    }

                    // It is not possible to free from `HeapPtr` because it is created through
                    // a &self borrow on `StableHeap`. At least this repeated lookup is already
                    // on the slow path.
                    let mut value = reader.heap.entries.entry(current_id).expect("already looked up").free();

                    // Collect child IDs and push onto work stack for iterative processing
                    py_dec_ref_ids_for_data(value.data.0.get_mut(), &mut work_stack);
                }

                let Some(next_id) = work_stack.pop() else {
                    break;
                };
                current_id = next_id;
            }
        });
    }

    /// Returns an immutable reference to the heap data stored at the given ID. This can be more efficient
    /// than `.read()` for short-lived borrows that need read-only access (avoids reader bookkeeping).
    ///
    /// # Panics
    /// Panics if the value ID is invalid, the value has already been freed,
    /// or the data is currently borrowed via `with_entry_mut`/`call_attr`.
    #[must_use]
    pub fn get(&self, id: HeapId) -> &HeapData {
        let data = &self.entries.get(id).data;
        // SAFETY: (DH) no mutable references into `HeapData` is possible while the heap is borrowed
        unsafe { &*data.0.get() }
    }

    /// Returns the reference count for the heap entry at the given ID.
    ///
    /// This is primarily used for testing reference counting behavior.
    ///
    /// # Panics
    /// Panics if the value ID is invalid or the value has already been freed.
    #[must_use]
    #[cfg(feature = "ref-count-return")]
    pub fn get_refcount(&self, id: HeapId) -> usize {
        self.entries.get(id).refcount.get()
    }

    /// Returns the number of live (non-freed) values on the heap.
    ///
    /// This is primarily used for testing to verify that all heap entries
    /// are accounted for in reference count tests.
    ///
    /// Excludes the empty tuple singleton since it's an internal optimization
    /// detail that persists even when not explicitly used by user code.
    #[must_use]
    #[cfg(feature = "ref-count-return")]
    pub fn entry_count(&self) -> usize {
        // Skip index 0 which is the empty tuple singleton
        self.entries.iter().skip(1).count()
    }

    /// Returns whether cycle collection should run.
    ///
    /// True when the configured allocation interval has elapsed *and* at
    /// least one [`Purple`](CcColor::Purple) candidate is pending. The
    /// alloc-count check sets the maximum collector frequency the user
    /// asked for; the `purple_count` check is an additional early-out so
    /// programs that produce no cycle candidates pay no collector cost
    /// regardless of their allocation rate.
    ///
    /// Always returns false when [`disable_gc`](Self::disable_gc) has been
    /// called without a matching [`enable_gc`](Self::enable_gc); explicit
    /// [`collect_cycles`](Self::collect_cycles) calls still run regardless.
    #[inline]
    pub fn should_gc(&self) -> bool {
        #[cfg(feature = "test-hooks")]
        if self.gc_disabled {
            return false;
        }
        if self.purple_count == 0 {
            return false;
        }
        let interval = self.tracker.gc_interval().unwrap_or(DEFAULT_GC_INTERVAL);
        (self.allocations_since_gc.get() as usize) >= interval
    }

    /// Suppresses automatic garbage collection until [`enable_gc`](Self::enable_gc)
    /// is called.
    ///
    /// Explicit [`collect_cycles`](Self::collect_cycles) calls still run while
    /// disabled, so a script can build a known amount of garbage and then time
    /// exactly one collection pass.
    #[cfg(feature = "test-hooks")]
    pub fn disable_gc(&mut self) {
        self.gc_disabled = true;
    }

    /// Resumes automatic garbage collection after a prior [`disable_gc`](Self::disable_gc).
    ///
    /// Calling [`enable_gc`](Self::enable_gc) on an already-enabled heap is a no-op.
    #[cfg(feature = "test-hooks")]
    pub fn enable_gc(&mut self) {
        self.gc_disabled = false;
    }

    /// Runs Bacon–Rajan trial-deletion cycle collection.
    ///
    /// Walks every entry currently flagged [`Purple`](CcColor::Purple) (the
    /// candidates accumulated by `dec_ref`) and frees any references that turn
    /// out to live entirely inside an unreachable cycle. Refcount math itself
    /// proves liveness — entries reachable from outside the candidate set
    /// (including those held only on the Rust stack and those with active
    /// `HeapRead` readers) survive automatically because their refcount or
    /// reader count remains non-zero — so no explicit root walk is required.
    ///
    /// Phases:
    ///
    /// 1. **`MarkRoots`** — single linear pass over `entries` that finds
    ///    Purple entries, runs `MarkGray` on each, and collects the resulting
    ///    seed list. Purple entries reached transitively by an earlier seed's
    ///    `MarkGray` turn Gray and are correctly skipped, so each cycle root
    ///    is only seeded once.
    /// 2. **`Scan`** — for each seed, decide whether the subtree is alive
    ///    (`s.refcount > 0 || s.readers > 0`, resurrect to Black) or condemned
    ///    (mark White and recurse).
    /// 3. **`CollectWhite`** — free White entries iteratively. Child
    ///    refcounts were already balanced by `MarkGray`/`ScanBlack`, so this
    ///    phase does **not** call `dec_ref` on children — it only walks them
    ///    to free transitively.
    ///
    /// All four phases iterate via explicit work stacks instead of recursion
    /// (the textbook formulation is recursive); a 10 000-deep nested cycle
    /// must collect without a Rust stack overflow.
    ///
    /// Returns the number of unreachable entries that were freed during the sweep.
    ///
    /// # Caller Responsibility
    /// The caller should check [`should_gc`](Self::should_gc) before calling
    /// this method. With `purple_count == 0` the function returns immediately
    /// without touching the heap.
    pub fn collect_cycles(&mut self) -> usize {
        if self.purple_count == 0 {
            return 0;
        }
        // The mark/scan phases work in terms of `HeapPtr<'a>` to avoid re-indexing
        // paged storage on every entry access; the brand `'a` comes from the
        // surrounding `HeapReader::with` scope.
        HeapReader::with(self, &mut (), |reader, ()| reader.collect_cycles_inner())
    }

    fn collect_white(&mut self, roots: Vec<HeapId>) -> usize {
        let mut work_stack = roots;
        let mut freed = 0;
        while let Some(id) = work_stack.pop() {
            let Some(mut entry) = self.entries.entry(id) else {
                // Already freed via another seed's traversal — ignore.
                continue;
            };
            let heap_entry = entry.get_mut();
            if heap_entry.color.get() != CcColor::White {
                // Either resurrected to Black by `Scan` or never visited
                // (still Black/Gray from somewhere). Don't free.
                continue;
            }
            debug_assert!(
                heap_entry.readers.get() == 0,
                "collect_white: cannot free HeapId({}) with {} active reader(s)",
                id.index(),
                heap_entry.readers.get(),
            );
            let mut value = entry.free();
            // Clear weak entries before freeing their slots, just as `dec_ref`.
            if let HeapData::ExtFunction(function) = value.data.0.get_mut() {
                Self::remove_ext_function_cache_entry(&mut self.ext_function_cache, &function.cache_key(), id);
            }
            freed += 1;
            // Walk children, marking child `Value::Ref`s as `Dereferenced`
            // under `memory-model-checks` so dropping the freed entry's data
            // doesn't trip a Drop-panic on a live `Value::Ref` payload. The
            // pushed child IDs feed the work stack so we recursively walk
            // White grandchildren — we do *not* `dec_ref` these children
            // (`MarkGray`/`ScanBlack` already balanced their refcounts).
            py_dec_ref_ids_for_data(value.data.0.get_mut(), &mut work_stack);
        }
        freed
    }
}

/// Cycle-collection inner phases.
///
/// Implemented on [`HeapReader<'a>`] so the work stacks can carry
/// [`HeapPtr<'a>`] instead of [`HeapId`]: once an entry is reached, every
/// subsequent access is a pointer deref rather than a paged-storage lookup.
/// Entered only via [`Heap::collect_cycles`], which establishes the brand `'a`
/// through [`HeapReader::with`]. [`Heap::collect_white`] is intentionally kept
/// in `HeapId` space because freeing a slot requires consulting the free list,
/// which is keyed by id.
impl<'a> HeapReader<'a> {
    /// Implementation of [`Heap::collect_cycles`]. Phase 1 (discover Purple
    /// roots and `mark_gray` their subtrees), Phase 2 (`scan`), and Phase 3
    /// (`collect_white`) run sequentially. After `scan` the `HeapPtr` work
    /// stack is drained, so no branded pointer escapes into Phase 3.
    ///
    /// Each phase iterates `for_each_child_id` inline rather than staging child
    /// `HeapId`s through a scratch `Vec`. Mutation of entry fields goes through
    /// the `Cell`/`UnsafeCell`-backed interior mutability on `HeapEntry`, so the
    /// `&HeapEntry`/`&HeapData` borrows from [`HeapPtr::entry`]/[`HeapPtr::data`]
    /// can coexist with the `&self.entry_ptr` calls in the closure body.
    fn collect_cycles_inner(&mut self) -> usize {
        let mut roots: Vec<HeapId> = Vec::new();
        let mut work_stack: Vec<HeapPtr<'a>> = Vec::new();

        // 1. Discover roots by finding Purple entries. Mark each root (and its subtree) Gray.
        //    The linear scan may legitimately encounter freed slots, so we use
        //    `try_entry` to skip them rather than panicking.
        for i in 0..self.heap.entries.len() {
            let id = HeapId(i);
            let ptr = self.read_ptr(id);
            let Some(entry) = ptr.try_entry(self) else {
                continue;
            };
            if entry.color.get() != CcColor::Purple {
                continue;
            }
            if entry.readers.get() > 0 {
                // This entry cannot possibly be a root since it has active readers; reset
                // to Black so it won't be a candidate in the next cycle.
                entry.color.set(CcColor::Black);
                continue;
            }
            roots.push(id);
            self.mark_gray(ptr, &mut work_stack);
            debug_assert!(work_stack.is_empty(), "mark_gray must drain its work stack");
        }

        // 2. For each root, scan and resurrect if alive (refcount > 0 or active readers).
        //    Roots were live at Phase 1 and nothing has freed them between then and
        //    now, so `entry_ptr` returning `None` would indicate a bug.
        work_stack.extend(roots.iter().map(|&id| self.read_ptr(id)));
        self.scan(&mut work_stack);
        debug_assert!(work_stack.is_empty(), "scan must drain its work stack");

        // 3. Collect each root's White children as unreachable garbage. This phase
        //    keeps working in HeapId-space because `entry.free()` pushes to the
        //    free list, which is keyed by id.
        let freed = self.heap.collect_white(roots);

        // After this pass no Purple entries remain in the heap; zero the
        // counter so the next `dec_ref` event re-seeds from a clean baseline,
        // and reset the alloc-count gate so the next interval starts now.
        self.heap.purple_count = 0;
        self.heap.allocations_since_gc.set(0);
        freed
    }

    /// `MarkGray` (iterative): paint `s` and its transitive children Gray,
    /// decrementing each child's refcount once per traversal edge.
    ///
    /// After this completes for every root, every Gray entry's refcount equals
    /// the count of *external* references into it (refs originating outside
    /// the candidate subgraph). `Scan` uses that property to decide
    /// alive/condemned.
    fn mark_gray(&mut self, mut ptr: HeapPtr<'a>, work_stack: &mut Vec<HeapPtr<'a>>) {
        ptr.entry(self).color.set(CcColor::Gray);
        loop {
            for_each_child_id(ptr.data(self), |child_id| {
                let child_ptr = self.read_ptr(child_id);
                let entry = child_ptr.entry(self);
                debug_assert!(entry.refcount.get() > 0, "mark_gray: refcount underflow");
                entry.refcount.update(|r| r - 1);
                if entry.color.replace(CcColor::Gray) == CcColor::Gray {
                    // Already marked via another edge, don't push again
                    return;
                }
                work_stack.push(child_ptr);
            });
            let Some(next_ptr) = work_stack.pop() else {
                break;
            };
            ptr = next_ptr;
        }
    }

    /// `Scan` (iterative): each Gray entry is either resurrected via
    /// `ScanBlack` (external reference exists — refcount > 0 or active
    /// `HeapRead` reader) or painted White and its Gray children recursed.
    fn scan(&mut self, work_stack: &mut Vec<HeapPtr<'a>>) {
        let mut black_work_stack: Vec<HeapPtr<'a>> = Vec::new();
        while let Some(ptr) = work_stack.pop() {
            let entry = ptr.entry(self);
            if entry.color.get() != CcColor::Gray {
                // Already processed via another edge
                continue;
            }
            if entry.refcount.get() == 0 && entry.readers.get() == 0 {
                entry.color.set(CcColor::White);
                for_each_child_id(ptr.data(self), |child_id| {
                    let child_ptr = self.read_ptr(child_id);
                    let entry = child_ptr.entry(self);
                    if entry.color.get() != CcColor::Gray {
                        // Already processed via another edge
                        return;
                    }
                    work_stack.push(child_ptr);
                });
            } else {
                // External reference exists (either a refcount we couldn't
                // account for inside the candidate set, or a live `HeapRead`
                // pointing into the entry). Resurrect this entry and its
                // transitive Gray children back to Black via `mark_black`.
                self.mark_black(ptr, &mut black_work_stack);
                debug_assert!(black_work_stack.is_empty());
            }
        }
    }

    /// `ScanBlack` (iterative): resurrect a subtree by re-incrementing
    /// children's refcounts that `MarkGray` previously decremented, restoring
    /// the heap to the state it would have had if no cycle was suspected.
    ///
    /// Children's refcounts are incremented once per traversal edge — even if
    /// the child is already Black — so multi-edge graphs (a child reachable
    /// from two parents in the resurrected subtree) balance the matching
    /// per-edge decrements `MarkGray` performed. Recursion only descends into
    /// non-Black children so each entry is processed at most once.
    fn mark_black(&mut self, mut ptr: HeapPtr<'a>, work_stack: &mut Vec<HeapPtr<'a>>) {
        ptr.entry(self).color.set(CcColor::Black);
        loop {
            for_each_child_id(ptr.data(self), |child_id| {
                let child_ptr = self.read_ptr(child_id);
                let entry = child_ptr.entry(self);
                entry.refcount.update(|r| r + 1);
                if entry.color.replace(CcColor::Black) == CcColor::Black {
                    // Already marked via another edge
                    return;
                }
                work_stack.push(child_ptr);
            });
            let Some(next_ptr) = work_stack.pop() else {
                break;
            };
            ptr = next_ptr;
        }
    }
}

/// Leak detection for reference-counting tests.
///
/// Implemented on [`HeapReader`] because reading an entry's children needs
/// [`HeapPtr::data`]. It borrows the cycle collector's [`for_each_child_id`] to
/// walk edges but is not part of collection: nothing here mutates the heap, and
/// it is reached only from `run_ref_counts`, never from [`Heap::collect_cycles`].
#[cfg(feature = "ref-count-return")]
impl HeapReader<'_> {
    /// Returns live heap entries unreachable from `roots`, with each entry's type
    /// so callers holding `Interns` can name it ([`Type::name`]).
    ///
    /// `run_ref_counts` uses this to prove a test leaked nothing: an entry that
    /// is alive but reachable from no named variable is a missed `drop_with`.
    /// The walk is transitive, so an object owned by another object — a class's
    /// `__annotations__`, a nested list, an instance attribute — is accounted
    /// for by its owner and need not be bound to a name by the test itself.
    ///
    /// Unlike the cycle collector's [`mark_gray`](Self::mark_gray) this touches
    /// no colors or refcounts, so it cannot perturb the state under test.
    pub(crate) fn unreachable_entries(&self, roots: impl IntoIterator<Item = HeapId>) -> Vec<(HeapId, Type)> {
        let mut seen: HashSet<HeapId> = HashSet::new();
        let mut work_stack: Vec<HeapId> = Vec::new();
        for root in roots {
            if seen.insert(root) {
                work_stack.push(root);
            }
        }

        // A root's subtree is live by construction, but `try_entry` keeps the
        // walk total in case a test observes the heap mid-teardown.
        while let Some(id) = work_stack.pop() {
            let ptr = self.read_ptr(id);
            if ptr.try_entry(self).is_none() {
                continue;
            }
            for_each_child_id(ptr.data(self), |child_id| {
                if seen.insert(child_id) {
                    work_stack.push(child_id);
                }
            });
        }

        self.entries
            .iter()
            // The empty tuple singleton is an internal optimisation that is
            // always live and never named, so it is not a leak. See `entry_count`.
            .filter(|(id, _)| *id != EMPTY_TUPLE_ID && !seen.contains(id))
            .map(|(id, _)| (id, self.read_ptr(id).data(self).py_type()))
            .collect()
    }
}

// With `memory-model-checks` enabled, need to manually clean up the heap to avoid the
// bookkeeping causing panics at shutdown.
#[cfg(feature = "memory-model-checks")]
impl Drop for Heap {
    fn drop(&mut self) {
        for id in 0..self.entries.len() {
            if let Some(mut entry) = self.entries.entry(HeapId::from_index(id)) {
                // Mark all `Value::Ref` payloads as `Dereferenced` so they don't panic when dropped
                py_dec_ref_ids_for_data(entry.get_mut().data.0.get_mut(), &mut Vec::new());
                entry.free();
            }
        }
    }
}

/// Walks the GC-relevant children of a `HeapData` value and calls `on_child`
/// for each contained `HeapId`.
///
/// The cycle collector's mark/scan phases use this directly, combining the
/// child-id iteration with a `HeapId → HeapPtr` conversion in the closure
/// body — that's why this is closure-shaped rather than producing a `Vec`.
/// [`py_dec_ref_ids_for_data`] mirrors the same match arms for the
/// freeing/decref paths; the two must stay in sync.
fn for_each_child_id<F: FnMut(HeapId)>(data: &HeapData, mut on_child: F) {
    match data {
        HeapData::List(list) => {
            // Skip iteration if no refs - major GC optimization for lists of primitives
            if !list.contains_refs() {
                return;
            }
            for value in list.as_slice() {
                if let Value::Ref(id) = value {
                    on_child(*id);
                }
            }
        }
        // MUST report exactly the same ids as `Deque::py_dec_ref_ids` — reporting
        // fewer leaks, reporting more is a use-after-free.
        HeapData::Deque(deque) => {
            if !deque.contains_refs() {
                return;
            }
            for value in deque.iter() {
                if let Value::Ref(id) = value {
                    on_child(*id);
                }
            }
        }
        HeapData::Tuple(tuple) => {
            // Skip iteration if no refs - GC optimization for tuples of primitives
            if !tuple.contains_refs() {
                return;
            }
            for value in tuple.as_slice() {
                if let Value::Ref(id) = value {
                    on_child(*id);
                }
            }
        }
        HeapData::NamedTuple(nt) => {
            // Report the owned reference to the class object (factory instances
            // only) before the `contains_refs` early-out — that flag is computed
            // from `items` alone, so an all-primitive instance like `Point(1, 2)`
            // still has a live class edge. MUST report exactly the same ids as
            // `NamedTuple::py_dec_ref_ids`.
            if let Some(class_id) = nt.class_id() {
                on_child(class_id);
            }
            // Skip iteration if no refs - GC optimization for namedtuples of primitives
            if !nt.contains_refs() {
                return;
            }
            for value in nt.as_vec() {
                if let Value::Ref(id) = value {
                    on_child(*id);
                }
            }
        }
        // A namedtuple *class* owns its default values and its `__module__`.
        // MUST report the same ids as `NamedTupleClass::py_dec_ref_ids`.
        HeapData::NamedTupleClass(class) => {
            if !class.contains_refs() {
                return;
            }
            for value in class.defaults().iter().chain(once(class.module())) {
                if let Value::Ref(id) = value {
                    on_child(*id);
                }
            }
        }
        HeapData::Dict(dict) => {
            // Report the default_factory (a defaultdict with a heap-ref factory,
            // e.g. a lambda) before the `has_refs` early-out. MUST report exactly
            // the same ids as `Dict::py_dec_ref_ids`: reporting fewer leaks,
            // reporting more is a use-after-free.
            if let Some(Value::Ref(id)) = dict.default_factory() {
                on_child(*id);
            }
            // Skip iteration if no refs - major GC optimization for dicts of primitives
            if !dict.has_refs() {
                return;
            }
            for (k, v) in dict {
                if let Value::Ref(id) = k {
                    on_child(*id);
                }
                if let Value::Ref(id) = v {
                    on_child(*id);
                }
            }
        }
        HeapData::DictKeysView(view) => {
            on_child(view.dict_id());
        }
        HeapData::DictItemsView(view) => {
            on_child(view.dict_id());
        }
        HeapData::DictValuesView(view) => {
            on_child(view.dict_id());
        }
        HeapData::Set(set) => {
            for value in set.storage().iter() {
                if let Value::Ref(id) = value {
                    on_child(*id);
                }
            }
        }
        HeapData::FrozenSet(frozenset) => {
            for value in frozenset.storage().iter() {
                if let Value::Ref(id) = value {
                    on_child(*id);
                }
            }
        }
        HeapData::Closure(closure) => {
            // Add captured cells to work list
            for cell_id in &closure.cells {
                on_child(*cell_id);
            }
            // Add default values that are heap references
            for default in &closure.defaults {
                if let Value::Ref(id) = default {
                    on_child(*id);
                }
            }
        }
        HeapData::FunctionDefaults(fd) => {
            // Add default values that are heap references
            for default in &fd.defaults {
                if let Value::Ref(id) = default {
                    on_child(*id);
                }
            }
        }
        HeapData::Cell(cell) => {
            // Cell can contain a reference to another heap value
            if let Value::Ref(id) = &cell.0 {
                on_child(*id);
            }
        }
        HeapData::Dataclass(dc) => {
            // Dataclass attrs are stored in a Dict - iterate through entries
            for (k, v) in dc.attrs() {
                if let Value::Ref(id) = k {
                    on_child(*id);
                }
                if let Value::Ref(id) = v {
                    on_child(*id);
                }
            }
        }
        HeapData::Class(class) => {
            // The class namespace holds method/class-variable values.
            for (k, v) in class.namespace() {
                if let Value::Ref(id) = k {
                    on_child(*id);
                }
                if let Value::Ref(id) = v {
                    on_child(*id);
                }
            }
        }
        HeapData::Instance(instance) => {
            // An instance references its class plus its attribute dict's entries.
            on_child(instance.class());
            for (k, v) in instance.attrs() {
                if let Value::Ref(id) = k {
                    on_child(*id);
                }
                if let Value::Ref(id) = v {
                    on_child(*id);
                }
            }
        }
        HeapData::BoundMethod(bm) => {
            if let Value::Ref(id) = &bm.instance {
                on_child(*id);
            }
            if let Value::Ref(id) = &bm.func {
                on_child(*id);
            }
        }
        HeapData::DataclassField(field) => {
            // A captured default can reach back to the class the field belongs
            // to (`x: object = SomeInstanceOfIt`), closing a cycle.
            if let Value::Ref(id) = field.annotation() {
                on_child(*id);
            }
            if let Some(Value::Ref(id)) = field.default() {
                on_child(*id);
            }
        }
        HeapData::ListIterator(iter) => on_child(iter.list_id()),
        HeapData::DequeIterator(iter) => on_child(iter.deque_id()),
        HeapData::TupleIterator(iter) => on_child(iter.source_id()),
        HeapData::StringIterator(iter) => {
            if let Some(id) = iter.source_id() {
                on_child(id);
            }
        }
        HeapData::BytesIterator(iter) => {
            if let Some(id) = iter.source_id() {
                on_child(id);
            }
        }
        HeapData::RangeIterator(_) => {}
        HeapData::DictKeyIterator(iter) => on_child(iter.source_id()),
        HeapData::DictItemIterator(iter) => on_child(iter.source_id()),
        HeapData::DictValueIterator(iter) => on_child(iter.source_id()),
        HeapData::SetIterator(iter) => on_child(iter.source_id()),
        HeapData::CallableIterator(iter) => iter.for_each_child_id(on_child),
        HeapData::Itertools(iter) => iter.for_each_child_id(on_child),
        HeapData::Module(m) => {
            // Module attrs can contain references to heap values
            if !m.has_refs() {
                return;
            }
            for (k, v) in m.attrs() {
                if let Value::Ref(id) = k {
                    on_child(*id);
                }
                if let Value::Ref(id) = v {
                    on_child(*id);
                }
            }
        }
        HeapData::Coroutine(coro) => {
            // Add namespace values that are heap references
            for value in &coro.namespace {
                if let Value::Ref(id) = value {
                    on_child(*id);
                }
            }
        }
        HeapData::GatherFuture(gather) => {
            // Add inc_ref'd item HeapIds. Both coroutines and external
            // futures are owned by the gather for its entire lifecycle.
            for item in &gather.items {
                on_child(*item);
            }
            // Walk per-state heap refs: in-flight slot results plus this
            // gather's own awaiter (if `GatherSlot`, it owns an inc_ref on
            // the outer gather), or the cached result list once the gather
            // has completed successfully. `Pending` and `Failed` carry no
            // heap refs.
            match &gather.state {
                GatherState::Awaited(awaited) => {
                    if let Awaiter::GatherSlot { gather, .. } = &awaited.awaiter {
                        on_child(*gather);
                    }
                    for result in awaited.results.iter().flatten() {
                        if let Value::Ref(id) = result {
                            on_child(*id);
                        }
                    }
                }
                GatherState::Completed(Value::Ref(id)) => on_child(*id),
                GatherState::Pending | GatherState::Failed(_) | GatherState::Completed(_) => {}
            }
        }
        HeapData::ExternalFuture(fut) => {
            // `Pending { awaiter: Some(GatherSlot { gather, .. }) }` owns an
            // inc_ref on `gather`. `Awaiter::Task` / `None` and the `Failed`
            // state carry no heap refs. `Resolved` owns the cached value.
            match &fut.state {
                ExternalFutureState::Resolved(Value::Ref(id)) => on_child(*id),
                ExternalFutureState::Pending {
                    awaiter: Some(Awaiter::GatherSlot { gather, .. }),
                } => on_child(*gather),
                _ => {}
            }
        }
        HeapData::DateTime(dt) => {
            // Aware datetimes retain a heap reference to the tzinfo object so that
            // `dt.tzinfo is tz` identity is preserved across attribute lookups.
            // GC must follow that reference, otherwise the timezone gets swept
            // while the datetime still points at the freed slot.
            if let Some(tz_id) = dt.tzinfo_ref() {
                on_child(tz_id);
            }
        }
        HeapData::OpenFile(file) => {
            // Kept in sync with `py_dec_ref_ids_for_data`: the file owns one
            // ref on its loaded buffer. (`OpenFile` is not GC-tracked today, so
            // this arm is not reached by the collector, but the two walkers must
            // mirror each other per the contract above.)
            if let Some(buffer_id) = file.buffer_id() {
                on_child(buffer_id);
            }
        }
        HeapData::ReMatch(m) => {
            // Mirror `py_dec_ref_ids_for_data`: a match holds one ref on its
            // shared subject string (`None` for an interned subject).
            if let Value::Ref(id) = m.subject_ref() {
                on_child(*id);
            }
        }
        // Leaf types with no heap references
        _ => {}
    }
}

fn py_dec_ref_ids_for_data(data: &mut HeapData, stack: &mut Vec<HeapId>) {
    match data {
        HeapData::Str(s) => s.py_dec_ref_ids(stack),
        HeapData::Bytes(b) => b.py_dec_ref_ids(stack),
        HeapData::List(l) => l.py_dec_ref_ids(stack),
        HeapData::Deque(d) => d.py_dec_ref_ids(stack),
        HeapData::Tuple(t) => t.py_dec_ref_ids(stack),
        HeapData::NamedTuple(nt) => nt.py_dec_ref_ids(stack),
        HeapData::NamedTupleClass(class) => class.py_dec_ref_ids(stack),
        HeapData::Dict(d) => d.py_dec_ref_ids(stack),
        HeapData::DictKeysView(view) => view.py_dec_ref_ids(stack),
        HeapData::DictItemsView(view) => view.py_dec_ref_ids(stack),
        HeapData::DictValuesView(view) => view.py_dec_ref_ids(stack),
        HeapData::Set(s) => s.py_dec_ref_ids(stack),
        HeapData::FrozenSet(fs) => fs.py_dec_ref_ids(stack),
        HeapData::Closure(closure) => {
            // Decrement ref count for captured cells
            stack.extend(closure.cells.iter().copied());
            // Decrement ref count for default values that are heap references
            for default in &mut closure.defaults {
                default.py_dec_ref_ids(stack);
            }
        }
        HeapData::FunctionDefaults(fd) => {
            // Decrement ref count for default values that are heap references
            for default in &mut fd.defaults {
                default.py_dec_ref_ids(stack);
            }
        }
        HeapData::Cell(cell) => cell.0.py_dec_ref_ids(stack),
        HeapData::Dataclass(dc) => dc.py_dec_ref_ids(stack),
        HeapData::Class(class) => class.py_dec_ref_ids(stack),
        HeapData::Instance(instance) => instance.py_dec_ref_ids(stack),
        HeapData::BoundMethod(bm) => bm.py_dec_ref_ids(stack),
        HeapData::DataclassField(field) => field.py_dec_ref_ids(stack),
        HeapData::ListIterator(iter) => iter.py_dec_ref_ids(stack),
        HeapData::DequeIterator(iter) => iter.py_dec_ref_ids(stack),
        HeapData::TupleIterator(iter) => iter.py_dec_ref_ids(stack),
        HeapData::StringIterator(iter) => iter.py_dec_ref_ids(stack),
        HeapData::BytesIterator(iter) => iter.py_dec_ref_ids(stack),
        HeapData::RangeIterator(iter) => iter.py_dec_ref_ids(stack),
        HeapData::DictKeyIterator(iter) => iter.py_dec_ref_ids(stack),
        HeapData::DictItemIterator(iter) => iter.py_dec_ref_ids(stack),
        HeapData::DictValueIterator(iter) => iter.py_dec_ref_ids(stack),
        HeapData::SetIterator(iter) => iter.py_dec_ref_ids(stack),
        HeapData::CallableIterator(iter) => iter.py_dec_ref_ids(stack),
        HeapData::Itertools(iter) => iter.py_dec_ref_ids(stack),
        HeapData::Module(m) => m.py_dec_ref_ids(stack),
        HeapData::Coroutine(coro) => {
            // Decrement ref count for namespace values that are heap references
            for value in &mut coro.namespace {
                value.py_dec_ref_ids(stack);
            }
        }
        HeapData::GatherFuture(gather) => {
            // Decrement ref count for owned item HeapIds (coroutines and
            // external futures are both owned by the gather).
            stack.extend(gather.items.iter().copied());
            // Release per-state heap refs: in-flight slot results plus this
            // gather's own awaiter (if `GatherSlot`, it owns an inc_ref on
            // the outer gather), or the cached result list once the gather
            // has completed successfully. `Pending` and `Failed` carry no
            // heap refs.
            match &mut gather.state {
                GatherState::Awaited(awaited) => {
                    if let Awaiter::GatherSlot { gather, .. } = &awaited.awaiter {
                        stack.push(*gather);
                    }
                    for result in awaited.results.iter_mut().flatten() {
                        result.py_dec_ref_ids(stack);
                    }
                }
                GatherState::Completed(value) => value.py_dec_ref_ids(stack),
                GatherState::Pending | GatherState::Failed(_) => {}
            }
        }
        HeapData::ExternalFuture(fut) => match &mut fut.state {
            ExternalFutureState::Resolved(value) => value.py_dec_ref_ids(stack),
            ExternalFutureState::Pending {
                awaiter: Some(Awaiter::GatherSlot { gather, .. }),
            } => stack.push(*gather),
            ExternalFutureState::Pending {
                awaiter: None | Some(Awaiter::Task(_)),
            }
            | ExternalFutureState::Failed(_) => {}
        },
        HeapData::DateTime(dt) => {
            // Mirror `for_each_child_id`: when an aware datetime is freed we must
            // also drop the retained tzinfo reference so its refcount is balanced.
            if let Some(tz_id) = dt.tzinfo_ref() {
                stack.push(tz_id);
            }
        }
        HeapData::OpenFile(f) => {
            // Kept in sync with `for_each_child_id`: release the file's owned
            // ref on its loaded buffer when the file is freed (e.g. read but
            // never `close()`d).
            f.py_dec_ref_ids(stack);
        }
        // Release the shared subject reference (mirrors `for_each_child_id`).
        HeapData::ReMatch(m) => m.py_dec_ref_ids(stack),
        // other types have no nested heap references
        _ => {}
    }
}

/// Compile-fail soundness tests for [`HeapReader`].
///
/// Gated behind `--cfg heap_reader_compile_fail_tests` so they are only compiled
/// when the integration test harness runs `cargo check` with the appropriate flags.
#[cfg(heap_reader_compile_fail_tests)]
#[path = "../../tests/heap_reader_compile_fail_cases/cases.rs"]
mod heap_reader_compile_fail_cases;

/// Cycle-collector unit tests.
///
/// These live inside `heap.rs` (rather than under `crates/monty/tests/`)
/// because they need to manipulate `Heap` state directly — building a cycle
/// without a VM, peeking at `purple_count`, and rooting an entry only via a
/// Rust local binding. The integration-test surface only exposes
/// Python-driven execution and cannot construct any of those scenarios.
///
/// In particular, the [`cstack_only_cycle_survives_collection`] test
/// validates the central correctness property of trial deletion: a heap
/// entry referenced *only* from the Rust C stack survives a cycle
/// collection because its non-zero refcount is itself proof of liveness.
/// That behavior was previously a known soundness gap of the explicit-roots
/// mark–sweep collector.
#[cfg(test)]
mod tests {
    use monty_types::ResourceTracker;

    use super::*;
    use crate::{
        types::{List, callable_iterator::CallableIterator},
        value::Value,
    };

    /// Returns whether a heap entry is still allocated at `id`.
    fn is_alive(heap: &Heap, id: HeapId) -> bool {
        heap.entries.iter().any(|(other, _)| other == id)
    }

    /// Allocates a self-referencing one-element list and returns its id.
    ///
    /// The list's items become `[Value::Ref(id)]` and its refcount is bumped
    /// to 2 to reflect both the caller's ref and the new self-reference.
    fn alloc_self_cycle(heap: &Heap) -> HeapId {
        let id = heap.allocate(HeapData::List(List::new(vec![])));
        let entry = heap
            .entries
            .iter()
            .find(|(other, _)| *other == id)
            .map(|(_, e)| e)
            .expect("entry just allocated");
        // SAFETY: no other borrow into this entry's data exists during the test.
        let data = unsafe { &mut *entry.data.0.get() };
        match data {
            HeapData::List(list) => {
                list.set_contains_refs();
                list.as_vec_mut().push(Value::Ref(id));
            }
            _ => unreachable!(),
        }
        // The new self-pointer counts as one more reference into the entry.
        heap.inc_ref(id);
        id
    }

    /// Allocates a two-element cycle where one direction has multiplicity 3:
    /// `P → [A, A, A]` and `A → [P]`. Returns `(p_id, a_id)`.
    ///
    /// Final refcounts: `P.rc = 2` (alloc + one edge from A), `A.rc = 4`
    /// (alloc + three edges from P). The caller "owns" one of each — both
    /// can be dropped via `dec_ref` to isolate the cycle.
    ///
    /// Exercises the duplicate-edges-within-one-element shape that
    /// `mark_gray`/`mark_black` must handle correctly: each edge from P
    /// to A is one independent decrement (or increment), but the work
    /// stack must not grow with edge multiplicity — otherwise the outer
    /// pop processes A's children multiple times and over-counts.
    fn alloc_dup_child_cycle(heap: &Heap) -> (HeapId, HeapId) {
        let p_id = heap.allocate(HeapData::List(List::new(vec![])));
        let a_id = heap.allocate(HeapData::List(List::new(vec![])));

        let push_refs = |target: HeapId, refs: &[HeapId]| {
            let entry = heap
                .entries
                .iter()
                .find(|(other, _)| *other == target)
                .map(|(_, e)| e)
                .expect("entry just allocated");
            // SAFETY: no other borrow into this entry's data exists during the test.
            let data = unsafe { &mut *entry.data.0.get() };
            match data {
                HeapData::List(list) => {
                    list.set_contains_refs();
                    for r in refs {
                        list.as_vec_mut().push(Value::Ref(*r));
                    }
                }
                _ => unreachable!(),
            }
            for r in refs {
                heap.inc_ref(*r);
            }
        };

        push_refs(p_id, &[a_id, a_id, a_id]);
        push_refs(a_id, &[p_id]);
        (p_id, a_id)
    }

    #[test]
    fn cstack_only_cycle_survives_collection() {
        let mut heap = Heap::new(16, ResourceTracker::default());
        let id = alloc_self_cycle(&heap);

        // Simulate a Rust-side local `Value::Ref` binding by bumping the
        // refcount one extra time. Then `dec_ref` it back down to 2 — that
        // dec_ref is what enrolls the entry as a Purple candidate, mimicking
        // exactly the situation under the old GC where the local binding
        // wasn't published in any explicit root set.
        heap.inc_ref(id); // rc = 3
        heap.dec_ref(id); // rc = 2, flagged Purple
        assert_eq!(heap.purple_count, 1);

        // Cycle collection must not free the entry: the local "C-stack" ref
        // contributes one of its two surviving refcount units, so trial
        // deletion sees rc > 0 after MarkGray and resurrects the subtree.
        heap.collect_cycles();
        assert_eq!(heap.purple_count, 0);
        assert!(is_alive(&heap, id), "C-stack-rooted cycle was freed");
        assert!(matches!(heap.get(id), HeapData::List(_)));

        // Drop the simulated Rust local. Now the cycle is genuinely isolated
        // (rc 1 = self-pointer only). The next collection must reclaim it.
        heap.dec_ref(id); // rc = 1, re-flagged Purple
        assert_eq!(heap.purple_count, 1);
        heap.collect_cycles();
        assert_eq!(heap.purple_count, 0);
        assert!(!is_alive(&heap, id), "isolated cycle should have been freed");
    }

    #[test]
    fn heap_read_rooted_cycle_survives_collection() {
        let mut heap = Heap::new(16, ResourceTracker::default());
        let id = alloc_self_cycle(&heap);

        // Bump `readers` manually to mimic a live `HeapRead` pointing into
        // the entry. The borrow checker prevents holding a real `HeapRead`
        // across `collect_cycles` (which requires `&mut Heap`), so we
        // splice the same counter that `HeapRead::Drop` decrements.
        let readers_before = heap.entries.get(id).readers.get();
        heap.entries.get(id).readers.set(readers_before + 1);

        // Drive the entry into Purple via dec_ref: rc 2 → 1. Without the
        // `readers > 0` special-case in `Scan`, the resulting cycle would
        // be condemned to White and freed.
        heap.dec_ref(id); // rc = 1, flagged Purple
        assert_eq!(heap.purple_count, 1);

        heap.collect_cycles();
        assert!(
            is_alive(&heap, id),
            "entry with active HeapRead reader was freed by collect_cycles"
        );

        // Restore the simulated reader so `Heap::drop` can clean up
        // without tripping the `dec_ref` active-readers assertion.
        heap.entries.get(id).readers.set(readers_before);
        // The entry is leaked here on purpose (rc = 1 from the self-ref,
        // no external root remains, but the collector ran already and the
        // color is Black — the next dec_ref would try to recurse into the
        // self-pointer after freeing the entry). `Heap::drop` walks every
        // slot and tears them down regardless of refcount, so leaking
        // here is safe for the duration of the test.
    }

    /// Regression test for a soundness gap in [`Heap::dec_ref`]'s trial-deletion
    /// candidate-enrollment path.
    ///
    /// The surviving-refcount branch calls `heap_entry.data.0.get_mut()` to
    /// check `is_gc_tracked`, even though [`HeapData::is_gc_tracked`] only
    /// needs `&self`. That `UnsafeCell::get_mut()` is a fresh `&mut HeapData`
    /// (Unique) retag of the same allocation the live `HeapRead` is already
    /// pointing into via a `SharedReadWrite` raw pointer, so it invalidates
    /// the prior pointer under Stacked / Tree Borrows.
    ///
    /// Pure `cargo test` cannot observe this — the pointer arithmetic still
    /// reads valid bytes — but `cargo +nightly miri test` flags the access.
    /// The branch is reachable from normal Monty code (e.g. `list.remove`
    /// on a self-referential list holds a `HeapRead<List>`, clones the
    /// matching element, and the deferred drop of that clone calls
    /// `dec_ref` on the same `HeapId` while the list reader is still live).
    #[test]
    fn dec_ref_must_not_invalidate_live_heap_read() {
        let mut heap = Heap::new(16, ResourceTracker::default());
        let id = heap.allocate(HeapData::List(List::new(vec![])));
        // Bump refcount so `dec_ref` enters the non-freeing branch where
        // the offending `data.0.get_mut()` lives. `List` is GC-tracked, so
        // `is_gc_tracked` returns true and the branch is fully exercised.
        heap.inc_ref(id);

        HeapReader::with(&mut heap, &mut (), |heap, ()| {
            let HeapReadOutput::List(list) = heap.read(id) else {
                unreachable!()
            };
            // Holding `list` does NOT borrow the heap — only `list.get(heap)`
            // does. That is what lets the borrow checker accept this
            // sequence, while the underlying raw-pointer aliasing is
            // nevertheless violated by the next call.
            heap.dec_ref(id);
            // Read through the now-invalidated `SharedReadWrite` raw pointer.
            // Miri's aliasing model fails here.
            let _ = list.get(heap).as_slice().len();
        });
    }

    #[test]
    fn isolated_simple_cycle_is_collected() {
        // Sanity check: a self-reference cycle with no external rooting
        // gets collected on the next `collect_cycles` call.
        let mut heap = Heap::new(16, ResourceTracker::default());
        let id = alloc_self_cycle(&heap);
        // After alloc_self_cycle: rc = 2 (allocate's 1 + self-ref's 1).
        // Drop the caller's reference. rc 2 → 1, marks Purple.
        heap.dec_ref(id);
        assert_eq!(heap.purple_count, 1);
        heap.collect_cycles();
        assert!(!is_alive(&heap, id));
        assert_eq!(heap.purple_count, 0);
    }

    #[test]
    fn empty_tuple_singleton_survives_collection() {
        // The empty-tuple singleton is no longer rooted explicitly by the
        // collector. Its refcount stays ≥ 1 forever (initial heap-owned
        // ref), which is what keeps it alive — verify the collector does
        // not accidentally free it even after spurious Purple flagging.
        let mut heap = Heap::new(16, ResourceTracker::default());
        // Fake a dec_ref event that would mark the empty tuple Purple.
        heap.inc_ref(EMPTY_TUPLE_ID);
        heap.dec_ref(EMPTY_TUPLE_ID);
        heap.collect_cycles();
        assert!(
            is_alive(&heap, EMPTY_TUPLE_ID),
            "empty tuple singleton must survive collection"
        );
    }

    #[test]
    fn pending_purple_cycle_round_trips_through_serde() {
        // A snapshot can be taken between any two bytecode instructions, so
        // entries flagged Purple by `dec_ref` but not yet visited by the
        // collector must survive serde round-trips. Otherwise a cycle that
        // becomes garbage just before snapshot would leak permanently after
        // restore (the post-restore VM would never re-touch it).
        let mut heap = Heap::new(16, ResourceTracker::default());
        let id = alloc_self_cycle(&heap);
        // Drop the caller's external ref so the entry is genuinely
        // unreachable except via its self-pointer. dec_ref flags Purple.
        heap.dec_ref(id); // rc 2 → 1
        assert_eq!(heap.purple_count, 1);
        assert_eq!(heap.entries.get(id).color.get(), CcColor::Purple);

        // Round-trip through postcard.
        let bytes = postcard::to_allocvec(&heap).expect("serialize");
        let mut restored: Heap = postcard::from_bytes(&bytes).expect("deserialize");

        // `purple_count` and the per-entry color must round-trip.
        assert_eq!(restored.purple_count, 1);
        assert_eq!(restored.entries.get(id).color.get(), CcColor::Purple);

        // Run the collector on the restored heap; the cycle is unreachable
        // and must be reclaimed.
        restored.collect_cycles();
        assert!(!is_alive(&restored, id));
        assert_eq!(restored.purple_count, 0);
    }

    #[test]
    fn isolated_cycle_with_duplicate_child_refs_is_collected() {
        // Regression: an unreachable cycle where one element references its
        // sibling multiple times within its child list. The mark phase must
        // (a) decrement the sibling's refcount once per edge, and (b) push
        // the sibling onto the work stack at most once, even when many
        // edges from the same parent target it.
        //
        // Without (b), the outer pop walks the sibling's children once per
        // duplicate edge, over-decrementing the *sibling's* children's
        // refcounts. In debug builds this trips `mark_gray`'s
        // refcount-underflow `debug_assert`; in release it wraps the
        // refcount to `usize::MAX` and `scan` resurrects the cycle —
        // either way, the cycle is not collected.
        let mut heap = Heap::new(16, ResourceTracker::default());
        let (p_id, a_id) = alloc_dup_child_cycle(&heap);
        // After construction: P.rc = 2, A.rc = 4.

        // Drop the caller's refs; the cycle is now genuinely unreachable.
        heap.dec_ref(p_id); // P.rc 2 → 1, flagged Purple
        heap.dec_ref(a_id); // A.rc 4 → 3, flagged Purple
        assert_eq!(heap.purple_count, 2);

        heap.collect_cycles();

        assert!(!is_alive(&heap, p_id), "P should be collected");
        assert!(!is_alive(&heap, a_id), "A should be collected");
        assert_eq!(heap.purple_count, 0);
    }

    #[test]
    fn cstack_rooted_cycle_with_duplicate_child_refs_collects_after_pin_dropped() {
        // Regression: when `scan` resurrects a Purple cycle via `mark_black`,
        // the per-edge refcount *increment* must mirror `mark_gray`'s
        // per-edge decrement. Duplicate edges from one element to its
        // sibling must not over-increment via repeated outer pops of the
        // sibling.
        //
        // We pin P externally (extra inc_ref) so `scan` is forced to take
        // the resurrect path through `mark_black`. After collection the
        // refcounts must be exactly the pre-collection values — verified by
        // dropping the pin and confirming the next `collect_cycles` reclaims
        // the cycle. If `mark_black` over-incremented during resurrection,
        // dropping the pin leaves the refcounts artificially high and the
        // cycle leaks.
        let mut heap = Heap::new(16, ResourceTracker::default());
        let (p_id, a_id) = alloc_dup_child_cycle(&heap);
        // After construction: P.rc = 2, A.rc = 4.

        // Pin P externally. After the inc_ref + dec_ref pair, P is Purple
        // but its refcount still includes one unit not accounted for by
        // any in-cycle edge — `scan` must resurrect via `mark_black`.
        heap.inc_ref(p_id); // P.rc = 3 (alloc + from A + pin)
        heap.dec_ref(p_id); // P.rc = 2, flagged Purple
        heap.dec_ref(a_id); // A.rc = 3, flagged Purple

        heap.collect_cycles();

        // First pass: cycle survives because of P's external pin.
        assert!(is_alive(&heap, p_id), "P should survive (external pin)");
        assert!(is_alive(&heap, a_id), "A should survive (reachable via P)");

        // Drop the pin. If `mark_black` correctly restored refcounts during
        // resurrection, the cycle is now isolated and the next collection
        // reclaims it. Over-incrementing in `mark_black` leaves P's
        // refcount artificially high so `scan` resurrects it again.
        heap.dec_ref(p_id);
        heap.collect_cycles();

        assert!(!is_alive(&heap, p_id), "P should be collected after pin dropped");
        assert!(!is_alive(&heap, a_id), "A should be collected after pin dropped");
    }

    /// The GC must see BOTH refs a `callable_iterator` owns, with correct
    /// multiplicity: under-tracing here would let a live object be collected.
    #[test]
    fn callable_iterator_traces_callable_and_sentinel() {
        let heap = Heap::new(16, ResourceTracker::default());
        let c = heap.allocate(HeapData::List(List::new(vec![])));
        let s = heap.allocate(HeapData::List(List::new(vec![])));

        heap.inc_ref(c);
        heap.inc_ref(s);
        let iter = heap.allocate(HeapData::CallableIterator(CallableIterator::new(
            Value::Ref(c),
            Value::Ref(s),
        )));

        let mut traced = vec![];
        for_each_child_id(heap.get(iter), |id| traced.push(id));
        assert_eq!(traced, vec![c, s], "callable and sentinel are both traced");

        // Multiplicity: when callable IS sentinel, two counted refs point at one
        // object, so the id must be reported twice or trial deletion
        // under-decrements and frees a live object.
        heap.inc_ref(c);
        heap.inc_ref(c);
        let shared = heap.allocate(HeapData::CallableIterator(CallableIterator::new(
            Value::Ref(c),
            Value::Ref(c),
        )));

        let mut dup = vec![];
        for_each_child_id(heap.get(shared), |id| dup.push(id));
        assert_eq!(dup, vec![c, c], "a shared callable/sentinel is traced twice");
    }

    /// End-to-end: a cycle through the callable must be collected and the
    /// non-cycle sentinel released — exercising the mark phase
    /// (`for_each_child_id`) and the free phase (`py_dec_ref_ids`) together.
    #[test]
    fn callable_iterator_cycle_is_collected() {
        let mut heap = Heap::new(16, ResourceTracker::default());
        let sentinel = heap.allocate(HeapData::List(List::new(vec![])));
        let list = heap.allocate(HeapData::List(List::new(vec![])));
        heap.inc_ref(list);
        heap.inc_ref(sentinel);
        let iter = heap.allocate(HeapData::CallableIterator(CallableIterator::new(
            Value::Ref(list),
            Value::Ref(sentinel),
        )));

        // Close the cycle: the callable list references the iterator back.
        heap.inc_ref(iter);
        HeapReader::with(&mut heap, &mut (), |reader, ()| {
            let HeapReadOutput::List(mut l) = reader.read(list) else {
                unreachable!("just allocated a list")
            };
            let l = l.get_mut(reader);
            l.set_contains_refs();
            l.as_vec_mut().push(Value::Ref(iter));
        });
        // list.rc = 2, iter.rc = 2, sentinel.rc = 2.

        // Drop the allocation refs: {iter, list} is now an unreachable cycle and
        // the sentinel is held only by the iterator.
        heap.dec_ref(list);
        heap.dec_ref(iter);
        heap.dec_ref(sentinel);

        heap.collect_cycles();

        assert!(!is_alive(&heap, iter), "callable_iterator in a cycle must be collected");
        assert!(!is_alive(&heap, list), "the cycle's other node must be collected");
        assert!(
            !is_alive(&heap, sentinel),
            "sentinel held only by the freed iterator must be released"
        );
    }
}