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

//! Partial paths are "snippets" of paths that we can precalculate for each file that we analyze.
//!
//! Stack graphs are _incremental_, since we can produce a subgraph for each file without having
//! to look at the contents of any other file in the repo, or in any upstream or downstream
//! dependencies.
//!
//! This is great, because it means that when we receive a new commit for a repository, we only
//! have to examine, and generate new stack subgraphs for, the files that are changed as part of
//! that commit.
//!
//! Having done that, one possible way to find name binding paths would be to load in all of the
//! subgraphs for the files that belong to the current commit, union them together into the
//! combined graph for that commit, and run the [path-finding algorithm][] on that combined graph.
//! However, we think that this will require too much computation at query time.
//!
//! [path-finding algorithm]: ../paths/index.html
//!
//! Instead, we want to precompute parts of the path-finding algorithm, by calculating _partial
//! paths_ for each file.  Because stack graphs have limited places where a path can cross from one
//! file into another, we can calculate all of the possible partial paths that reach those
//! “import/export” points.
//!
//! At query time, we can then load in the _partial paths_ for each file, instead of the files'
//! full stack graph structure.  We can efficiently [concatenate][] partial paths together,
//! producing the original "full" path that represents a name binding.
//!
//! [concatenate]: struct.PartialPath.html#method.concatenate

use std::convert::TryFrom;
use std::fmt::Display;
use std::num::NonZeroU32;

use controlled_option::ControlledOption;
use controlled_option::Niche;
use enumset::EnumSetType;
use smallvec::SmallVec;

use crate::arena::Deque;
use crate::arena::DequeArena;
use crate::arena::Handle;
use crate::graph::Edge;
use crate::graph::Node;
use crate::graph::NodeID;
use crate::graph::StackGraph;
use crate::graph::Symbol;
use crate::paths::PathResolutionError;
use crate::utils::cmp_option;
use crate::utils::equals_option;

//-------------------------------------------------------------------------------------------------
// Displaying stuff

/// This trait only exists because:
///
///   - we need `Display` implementations that dereference arena handles from our `StackGraph` and
///     `PartialPaths` bags o' crap,
///   - many of our arena-managed types can handles to _other_ arena-managed data, which we need to
///     recursively display as part of displaying the "outer" instance, and
///   - in particular, we sometimes need `&mut` access to the `PartialPaths` arenas.
///
/// The borrow checker is not very happy with us having all of these constraints at the same time —
/// in particular, the last one.
///
/// This trait gets around the problem by breaking up the display operation into two steps:
///
///   - First, each data instance has a chance to "prepare" itself with `&mut` access to whatever
///     arenas it needs.  (Anything containing a `Deque`, for instance, uses this step to ensure
///     that our copy of the deque is pointed in the right direction, since reversing requires
///     `&mut` access to the arena.)
///
///   - Once everything has been prepared, we return a value that implements `Display`, and
///     contains _non-mutable_ references to the arena.  Because our arena references are
///     non-mutable, we don't run into any problems with the borrow checker while recursively
///     displaying the contents of the data instance.
trait DisplayWithPartialPaths {
    fn prepare(&mut self, _graph: &StackGraph, _partials: &mut PartialPaths) {}

    fn display_with(
        &self,
        graph: &StackGraph,
        partials: &PartialPaths,
        f: &mut std::fmt::Formatter,
    ) -> std::fmt::Result;
}

/// Prepares and returns a `Display` implementation for a type `D` that implements
/// `DisplayWithPartialPaths`.  We only require `&mut` access to the `PartialPath` arenas while
/// creating the `Display` instance; the `Display` instance itself will only retain shared access
/// to the arenas.
fn display_with<'a, D>(
    mut value: D,
    graph: &'a StackGraph,
    partials: &'a mut PartialPaths,
) -> impl Display + 'a
where
    D: DisplayWithPartialPaths + 'a,
{
    value.prepare(graph, partials);
    DisplayWithPartialPathsWrapper {
        value,
        graph,
        partials,
    }
}

/// Returns a `Display` implementation that you can use inside of your `display_with` method to
/// display any recursive fields.  This assumes that the recursive fields have already been
/// prepared.
fn display_prepared<'a, D>(
    value: D,
    graph: &'a StackGraph,
    partials: &'a PartialPaths,
) -> impl Display + 'a
where
    D: DisplayWithPartialPaths + 'a,
{
    DisplayWithPartialPathsWrapper {
        value,
        graph,
        partials,
    }
}

#[doc(hidden)]
struct DisplayWithPartialPathsWrapper<'a, D> {
    value: D,
    graph: &'a StackGraph,
    partials: &'a PartialPaths,
}

impl<'a, D> Display for DisplayWithPartialPathsWrapper<'a, D>
where
    D: DisplayWithPartialPaths,
{
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        self.value.display_with(self.graph, self.partials, f)
    }
}

//-------------------------------------------------------------------------------------------------
// Symbol stack variables

/// Represents an unknown list of scoped symbols.
#[repr(transparent)]
#[derive(Clone, Copy, Debug, Eq, Hash, Niche, Ord, PartialEq, PartialOrd)]
pub struct SymbolStackVariable(#[niche] NonZeroU32);

impl SymbolStackVariable {
    pub fn new(variable: u32) -> Option<SymbolStackVariable> {
        NonZeroU32::new(variable).map(SymbolStackVariable)
    }

    /// Creates a new symbol stack variable.  This constructor is used when creating a new, empty
    /// partial path, since there aren't any other variables that we need to be fresher than.
    pub(crate) fn initial() -> SymbolStackVariable {
        SymbolStackVariable(unsafe { NonZeroU32::new_unchecked(1) })
    }

    /// Applies an offset to this variable.
    ///
    /// When concatenating partial paths, we have to ensure that the left- and right-hand sides
    /// have non-overlapping sets of variables.  To do this, we find the maximum value of any
    /// variable on the left-hand side, and add this “offset” to the values of all of the variables
    /// on the right-hand side.
    pub fn with_offset(self, symbol_variable_offset: u32) -> SymbolStackVariable {
        let offset_value = self.0.get() + symbol_variable_offset;
        SymbolStackVariable(unsafe { NonZeroU32::new_unchecked(offset_value) })
    }

    pub(crate) fn as_u32(self) -> u32 {
        self.0.get()
    }

    fn as_usize(self) -> usize {
        self.0.get() as usize
    }
}

impl Display for SymbolStackVariable {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "%{}", self.0.get())
    }
}

impl From<NonZeroU32> for SymbolStackVariable {
    fn from(value: NonZeroU32) -> SymbolStackVariable {
        SymbolStackVariable(value)
    }
}

impl Into<u32> for SymbolStackVariable {
    fn into(self) -> u32 {
        self.0.get()
    }
}

impl TryFrom<u32> for SymbolStackVariable {
    type Error = ();
    fn try_from(value: u32) -> Result<SymbolStackVariable, ()> {
        let value = NonZeroU32::new(value).ok_or(())?;
        Ok(SymbolStackVariable(value))
    }
}

//-------------------------------------------------------------------------------------------------
// Scope stack variables

/// Represents an unknown list of exported scopes.
#[repr(transparent)]
#[derive(Clone, Copy, Debug, Eq, Hash, Niche, Ord, PartialEq, PartialOrd)]
pub struct ScopeStackVariable(#[niche] NonZeroU32);

impl ScopeStackVariable {
    pub fn new(variable: u32) -> Option<ScopeStackVariable> {
        NonZeroU32::new(variable).map(ScopeStackVariable)
    }

    /// Creates a new scope stack variable.  This constructor is used when creating a new, empty
    /// partial path, since there aren't any other variables that we need to be fresher than.
    fn initial() -> ScopeStackVariable {
        ScopeStackVariable(unsafe { NonZeroU32::new_unchecked(1) })
    }

    /// Creates a new scope stack variable that is fresher than all other variables in a partial
    /// path.  (You must calculate the maximum variable number already in use.)
    fn fresher_than(max_used: u32) -> ScopeStackVariable {
        ScopeStackVariable(unsafe { NonZeroU32::new_unchecked(max_used + 1) })
    }

    /// Applies an offset to this variable.
    ///
    /// When concatenating partial paths, we have to ensure that the left- and right-hand sides
    /// have non-overlapping sets of variables.  To do this, we find the maximum value of any
    /// variable on the left-hand side, and add this “offset” to the values of all of the variables
    /// on the right-hand side.
    pub fn with_offset(self, scope_variable_offset: u32) -> ScopeStackVariable {
        let offset_value = self.0.get() + scope_variable_offset;
        ScopeStackVariable(unsafe { NonZeroU32::new_unchecked(offset_value) })
    }

    pub(crate) fn as_u32(self) -> u32 {
        self.0.get()
    }

    fn as_usize(self) -> usize {
        self.0.get() as usize
    }
}

impl Display for ScopeStackVariable {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "${}", self.0.get())
    }
}

impl From<NonZeroU32> for ScopeStackVariable {
    fn from(value: NonZeroU32) -> ScopeStackVariable {
        ScopeStackVariable(value)
    }
}

impl Into<u32> for ScopeStackVariable {
    fn into(self) -> u32 {
        self.0.get()
    }
}

impl TryFrom<u32> for ScopeStackVariable {
    type Error = ();
    fn try_from(value: u32) -> Result<ScopeStackVariable, ()> {
        let value = NonZeroU32::new(value).ok_or(())?;
        Ok(ScopeStackVariable(value))
    }
}

//-------------------------------------------------------------------------------------------------
// Partial symbol stacks

/// A symbol with an unknown, but possibly empty, list of exported scopes attached to it.
#[repr(C)]
#[derive(Clone, Copy)]
pub struct PartialScopedSymbol {
    pub symbol: Handle<Symbol>,
    // Note that not having an attached scope list is _different_ than having an empty attached
    // scope list.
    pub scopes: ControlledOption<PartialScopeStack>,
}

impl PartialScopedSymbol {
    /// Applies an offset to this scoped symbol.
    ///
    /// When concatenating partial paths, we have to ensure that the left- and right-hand sides
    /// have non-overlapping sets of variables.  To do this, we find the maximum value of any
    /// variable on the left-hand side, and add this “offset” to the values of all of the variables
    /// on the right-hand side.
    pub fn with_offset(mut self, scope_variable_offset: u32) -> PartialScopedSymbol {
        let scopes = self
            .scopes
            .into_option()
            .map(|stack| stack.with_offset(scope_variable_offset));
        self.scopes = ControlledOption::from_option(scopes);
        self
    }

    /// Matches this precondition symbol against another, unifying its contents with an existing
    /// set of bindings.
    pub fn unify(
        &mut self,
        partials: &mut PartialPaths,
        rhs: PartialScopedSymbol,
        scope_bindings: &mut PartialScopeStackBindings,
    ) -> Result<(), PathResolutionError> {
        if self.symbol != rhs.symbol {
            return Err(PathResolutionError::SymbolStackUnsatisfied);
        }
        match (self.scopes.into_option(), rhs.scopes.into_option()) {
            (Some(lhs), Some(rhs)) => {
                let unified = lhs.unify(partials, rhs, scope_bindings)?;
                self.scopes = ControlledOption::some(unified);
            }
            (None, None) => {}
            _ => return Err(PathResolutionError::SymbolStackUnsatisfied),
        }
        Ok(())
    }

    /// Returns whether two partial scoped symbols "match".  The symbols must be identical, and any
    /// attached scopes must also match.
    pub fn matches(self, partials: &mut PartialPaths, postcondition: PartialScopedSymbol) -> bool {
        if self.symbol != postcondition.symbol {
            return false;
        }

        // If one side has an attached scope but the other doesn't, then the scoped symbols don't
        // match.
        if self.scopes.is_none() != postcondition.scopes.is_none() {
            return false;
        }

        // Otherwise, if both sides have an attached scope, they have to be compatible.
        if let Some(precondition_scopes) = self.scopes.into_option() {
            if let Some(postcondition_scopes) = postcondition.scopes.into_option() {
                return precondition_scopes.matches(partials, postcondition_scopes);
            }
        }

        true
    }

    /// Applies a set of bindings to this partial scoped symbol, producing a new scoped symbol.
    pub fn apply_partial_bindings(
        mut self,
        partials: &mut PartialPaths,
        scope_bindings: &PartialScopeStackBindings,
    ) -> Result<PartialScopedSymbol, PathResolutionError> {
        let scopes = match self.scopes.into_option() {
            Some(scopes) => Some(scopes.apply_partial_bindings(partials, scope_bindings)?),
            None => None,
        };
        self.scopes = scopes.into();
        Ok(self)
    }

    pub fn equals(&self, partials: &mut PartialPaths, other: &PartialScopedSymbol) -> bool {
        self.symbol == other.symbol
            && equals_option(
                self.scopes.into_option(),
                other.scopes.into_option(),
                |a, b| a.equals(partials, b),
            )
    }

    pub fn cmp(
        &self,
        graph: &StackGraph,
        partials: &mut PartialPaths,
        other: &PartialScopedSymbol,
    ) -> std::cmp::Ordering {
        std::cmp::Ordering::Equal
            .then_with(|| graph[self.symbol].cmp(&graph[other.symbol]))
            .then_with(|| {
                cmp_option(
                    self.scopes.into_option(),
                    other.scopes.into_option(),
                    |a, b| a.cmp(partials, b),
                )
            })
    }

    pub fn display<'a>(
        self,
        graph: &'a StackGraph,
        partials: &'a mut PartialPaths,
    ) -> impl Display + 'a {
        display_with(self, graph, partials)
    }
}

impl DisplayWithPartialPaths for PartialScopedSymbol {
    fn prepare(&mut self, graph: &StackGraph, partials: &mut PartialPaths) {
        if let Some(mut scopes) = self.scopes.into_option() {
            scopes.prepare(graph, partials);
            self.scopes = scopes.into();
        }
    }

    fn display_with(
        &self,
        graph: &StackGraph,
        partials: &PartialPaths,
        f: &mut std::fmt::Formatter,
    ) -> std::fmt::Result {
        if let Some(scopes) = self.scopes.into_option() {
            write!(
                f,
                "{}/({})",
                self.symbol.display(graph),
                display_prepared(scopes, graph, partials)
            )
        } else {
            write!(f, "{}", self.symbol.display(graph))
        }
    }
}

/// A pattern that might match against a symbol stack.  Consists of a (possibly empty) list of
/// partial scoped symbols, along with an optional symbol stack variable.
#[repr(C)]
#[derive(Clone, Copy, Niche)]
pub struct PartialSymbolStack {
    #[niche]
    symbols: Deque<PartialScopedSymbol>,
    length: u32,
    variable: ControlledOption<SymbolStackVariable>,
}

impl PartialSymbolStack {
    /// Returns whether this partial symbol stack can match the empty symbol stack.
    #[inline(always)]
    pub fn can_match_empty(&self) -> bool {
        self.symbols.is_empty()
    }

    /// Returns whether this partial symbol stack can _only_ match the empty symbol stack.
    #[inline(always)]
    pub fn can_only_match_empty(&self) -> bool {
        self.symbols.is_empty() && self.variable.is_none()
    }

    /// Returns whether this partial symbol stack contains any symbols.
    #[inline(always)]
    pub fn contains_symbols(&self) -> bool {
        !self.symbols.is_empty()
    }

    /// Returns whether this partial symbol stack has a symbol stack variable.
    #[inline(always)]
    pub fn has_variable(&self) -> bool {
        self.variable.is_some()
    }

    #[inline(always)]
    pub fn len(&self) -> usize {
        self.length as usize
    }

    /// Returns an empty partial symbol stack.
    pub fn empty() -> PartialSymbolStack {
        PartialSymbolStack {
            symbols: Deque::empty(),
            length: 0,
            variable: ControlledOption::none(),
        }
    }

    /// Returns a partial symbol stack containing only a symbol stack variable.
    pub fn from_variable(variable: SymbolStackVariable) -> PartialSymbolStack {
        PartialSymbolStack {
            symbols: Deque::empty(),
            length: 0,
            variable: ControlledOption::some(variable),
        }
    }

    /// Returns whether this partial symbol stack is iterable in both directions without needing
    /// mutable access to the arena.
    pub fn have_reversal(&self, partials: &PartialPaths) -> bool {
        self.symbols.have_reversal(&partials.partial_symbol_stacks)
    }

    /// Applies an offset to this partial symbol stack.
    ///
    /// When concatenating partial paths, we have to ensure that the left- and right-hand sides
    /// have non-overlapping sets of variables.  To do this, we find the maximum value of any
    /// variable on the left-hand side, and add this “offset” to the values of all of the variables
    /// on the right-hand side.
    pub fn with_offset(
        mut self,
        partials: &mut PartialPaths,
        symbol_variable_offset: u32,
        scope_variable_offset: u32,
    ) -> PartialSymbolStack {
        let mut result = match self.variable.into_option() {
            Some(variable) => Self::from_variable(variable.with_offset(symbol_variable_offset)),
            None => Self::empty(),
        };
        while let Some(symbol) = self.pop_front(partials) {
            result.push_back(partials, symbol.with_offset(scope_variable_offset));
        }
        result
    }

    fn prepend(&mut self, partials: &mut PartialPaths, mut head: Deque<PartialScopedSymbol>) {
        while let Some(head) = head.pop_back(&mut partials.partial_symbol_stacks).copied() {
            self.push_front(partials, head);
        }
    }

    /// Pushes a new [`PartialScopedSymbol`][] onto the front of this partial symbol stack.
    pub fn push_front(&mut self, partials: &mut PartialPaths, symbol: PartialScopedSymbol) {
        self.length += 1;
        self.symbols
            .push_front(&mut partials.partial_symbol_stacks, symbol);
    }

    /// Pushes a new [`PartialScopedSymbol`][] onto the back of this partial symbol stack.
    pub fn push_back(&mut self, partials: &mut PartialPaths, symbol: PartialScopedSymbol) {
        self.length += 1;
        self.symbols
            .push_back(&mut partials.partial_symbol_stacks, symbol);
    }

    /// Removes and returns the [`PartialScopedSymbol`][] at the front of this partial symbol
    /// stack.  If the stack is empty, returns `None`.
    pub fn pop_front(&mut self, partials: &mut PartialPaths) -> Option<PartialScopedSymbol> {
        let result = self
            .symbols
            .pop_front(&mut partials.partial_symbol_stacks)
            .copied();
        if result.is_some() {
            self.length -= 1;
        }
        result
    }

    /// Removes and returns the [`PartialScopedSymbol`][] at the back of this partial symbol stack.
    /// If the stack is empty, returns `None`.
    pub fn pop_back(&mut self, partials: &mut PartialPaths) -> Option<PartialScopedSymbol> {
        let result = self
            .symbols
            .pop_back(&mut partials.partial_symbol_stacks)
            .copied();
        if result.is_some() {
            self.length -= 1;
        }
        result
    }

    pub fn display<'a>(
        self,
        graph: &'a StackGraph,
        partials: &'a mut PartialPaths,
    ) -> impl Display + 'a {
        display_with(self, graph, partials)
    }

    /// Returns whether two partial symbol stacks "match".  They must be the same length, and each
    /// respective partial scoped symbol must match.
    pub fn matches(mut self, partials: &mut PartialPaths, mut other: PartialSymbolStack) -> bool {
        while let Some(self_element) = self.pop_front(partials) {
            if let Some(other_element) = other.pop_front(partials) {
                if !self_element.matches(partials, other_element) {
                    return false;
                }
            } else {
                // Stacks aren't the same length.
                return false;
            }
        }
        if other.contains_symbols() {
            // Stacks aren't the same length.
            return false;
        }
        self.variable.into_option() == other.variable.into_option()
    }

    /// Applies a set of bindings to this partial symbol stack, producing a new partial symbol
    /// stack.
    pub fn apply_partial_bindings(
        mut self,
        partials: &mut PartialPaths,
        symbol_bindings: &PartialSymbolStackBindings,
        scope_bindings: &PartialScopeStackBindings,
    ) -> Result<PartialSymbolStack, PathResolutionError> {
        // If this partial symbol stack ends in a variable, see if we have a binding for it.  If
        // so, substitute that binding in.  If not, leave the variable as-is.
        let mut result = match self.variable.into_option() {
            Some(variable) => match symbol_bindings.get(variable) {
                Some(bound) => bound,
                None => PartialSymbolStack::from_variable(variable),
            },
            None => PartialSymbolStack::empty(),
        };

        // Then prepend all of the scoped symbols that appear at the beginning of this stack,
        // applying the bindings to any attached scopes as well.
        while let Some(partial_symbol) = self.pop_back(partials) {
            let partial_symbol = partial_symbol.apply_partial_bindings(partials, scope_bindings)?;
            result.push_front(partials, partial_symbol);
        }
        Ok(result)
    }

    /// Given two partial symbol stacks, returns the largest possible partial symbol stack such that
    /// any symbol stack that satisfies the result also satisfies both inputs.  This takes into
    /// account any existing variable assignments, and updates those variable assignments with
    /// whatever constraints are necessary to produce a correct result.
    ///
    /// Note that this operation is commutative.  (Concatenating partial paths, defined in
    /// [`PartialPath::concatenate`][], is not.)
    pub fn unify(
        self,
        partials: &mut PartialPaths,
        mut rhs: PartialSymbolStack,
        symbol_bindings: &mut PartialSymbolStackBindings,
        scope_bindings: &mut PartialScopeStackBindings,
    ) -> Result<PartialSymbolStack, PathResolutionError> {
        let mut lhs = self;

        // First, look at the shortest common prefix of lhs and rhs, and verify that they match.
        let mut head = Deque::empty();
        while lhs.contains_symbols() && rhs.contains_symbols() {
            let mut lhs_front = lhs.pop_front(partials).unwrap();
            let rhs_front = rhs.pop_front(partials).unwrap();
            lhs_front.unify(partials, rhs_front, scope_bindings)?;
            head.push_back(&mut partials.partial_symbol_stacks, lhs_front);
        }

        // Now at most one stack still has symbols.  Zero, one, or both of them have variables.
        // Let's do a case analysis on all of those possibilities.

        // CASE 1:
        // Both lhs and rhs have no more symbols.  The answer is always yes, and any variables that
        // are present get bound.  (If both sides have variables, then one variable gets bound to
        // the other, since both lhs and rhs will match _any other symbol stack_ at this point.  If
        // only one side has a variable, then the variable gets bound to the empty stack.)
        //
        //     lhs           rhs
        // ============  ============
        //  ()            ()            => yes either
        //  ()            () $2         => yes rhs, $2 => ()
        //  () $1         ()            => yes lhs, $1 => ()
        //  () $1         () $2         => yes lhs, $2 => $1
        if !lhs.contains_symbols() && !rhs.contains_symbols() {
            match (lhs.variable.into_option(), rhs.variable.into_option()) {
                (None, None) => {
                    lhs.prepend(partials, head);
                    return Ok(lhs);
                }
                (None, Some(var)) => {
                    symbol_bindings.add(
                        partials,
                        var,
                        PartialSymbolStack::empty(),
                        scope_bindings,
                    )?;
                    rhs.prepend(partials, head);
                    return Ok(rhs);
                }
                (Some(var), None) => {
                    symbol_bindings.add(
                        partials,
                        var,
                        PartialSymbolStack::empty(),
                        scope_bindings,
                    )?;
                    lhs.prepend(partials, head);
                    return Ok(lhs);
                }
                (Some(lhs_var), Some(rhs_var)) => {
                    symbol_bindings.add(
                        partials,
                        rhs_var,
                        PartialSymbolStack::from_variable(lhs_var),
                        scope_bindings,
                    )?;
                    lhs.prepend(partials, head);
                    return Ok(lhs);
                }
            }
        }

        // CASE 2:
        // One of the stacks contains symbols and the other doesn't, and the “empty” stack doesn't
        // have a variable.  Since there's no variable on the empty side to capture the remaining
        // content on the non-empty side, the answer is always no.
        //
        //     lhs           rhs
        // ============  ============
        //  ()            (stuff)       => NO
        //  ()            (stuff) $2    => NO
        //  (stuff)       ()            => NO
        //  (stuff) $1    ()            => NO
        if !lhs.contains_symbols() && lhs.variable.is_none() {
            return Err(PathResolutionError::SymbolStackUnsatisfied);
        }
        if !rhs.contains_symbols() && rhs.variable.is_none() {
            return Err(PathResolutionError::SymbolStackUnsatisfied);
        }

        // CASE 3:
        // One of the stacks contains symbols and the other doesn't, and the “empty” stack _does_
        // have a variable.  If both sides have the same variable, the answer is NO. Otherwise,
        // the answer is YES, and the “empty” side's variable needs to capture the entirety of the
        // non-empty side.
        //
        //     lhs           rhs
        // ============  ============
        //  (...) $1      (...) $1      => no
        //  () $1         (stuff)       => yes rhs,  $1 => rhs
        //  () $1         (stuff) $2    => yes rhs,  $1 => rhs
        //  (stuff)       () $2         => yes lhs,  $2 => lhs
        //  (stuff) $1    () $2         => yes lhs,  $2 => lhs
        match (lhs.variable.into_option(), rhs.variable.into_option()) {
            (Some(v1), Some(v2)) if v1 == v2 => {
                return Err(PathResolutionError::ScopeStackUnsatisfied)
            }
            _ => {}
        }
        if lhs.contains_symbols() {
            let rhs_variable = rhs.variable.into_option().unwrap();
            symbol_bindings.add(partials, rhs_variable, lhs, scope_bindings)?;
            lhs.prepend(partials, head);
            return Ok(lhs);
        }
        if rhs.contains_symbols() {
            let lhs_variable = lhs.variable.into_option().unwrap();
            symbol_bindings.add(partials, lhs_variable, rhs, scope_bindings)?;
            rhs.prepend(partials, head);
            return Ok(rhs);
        }

        unreachable!();
    }

    pub fn equals(mut self, partials: &mut PartialPaths, mut other: PartialSymbolStack) -> bool {
        while let Some(self_symbol) = self.pop_front(partials) {
            if let Some(other_symbol) = other.pop_front(partials) {
                if !self_symbol.equals(partials, &other_symbol) {
                    return false;
                }
            } else {
                return false;
            }
        }
        if !other.symbols.is_empty() {
            return false;
        }
        equals_option(
            self.variable.into_option(),
            other.variable.into_option(),
            |a, b| a == b,
        )
    }

    pub fn cmp(
        mut self,
        graph: &StackGraph,
        partials: &mut PartialPaths,
        mut other: PartialSymbolStack,
    ) -> std::cmp::Ordering {
        use std::cmp::Ordering;
        while let Some(self_symbol) = self.pop_front(partials) {
            if let Some(other_symbol) = other.pop_front(partials) {
                match self_symbol.cmp(graph, partials, &other_symbol) {
                    Ordering::Equal => continue,
                    result @ _ => return result,
                }
            } else {
                return Ordering::Greater;
            }
        }
        if !other.symbols.is_empty() {
            return Ordering::Less;
        }
        cmp_option(
            self.variable.into_option(),
            other.variable.into_option(),
            |a, b| a.cmp(&b),
        )
    }

    /// Returns an iterator over the contents of this partial symbol stack.
    pub fn iter<'a>(
        &self,
        partials: &'a mut PartialPaths,
    ) -> impl Iterator<Item = PartialScopedSymbol> + 'a {
        self.symbols
            .iter(&mut partials.partial_symbol_stacks)
            .copied()
    }

    /// Returns an iterator over the contents of this partial symbol stack, with no guarantee
    /// about the ordering of the elements.
    pub fn iter_unordered<'a>(
        &self,
        partials: &'a PartialPaths,
    ) -> impl Iterator<Item = PartialScopedSymbol> + 'a {
        self.symbols
            .iter_unordered(&partials.partial_symbol_stacks)
            .copied()
    }

    pub fn variable(&self) -> Option<SymbolStackVariable> {
        self.variable.clone().into_option()
    }

    fn ensure_both_directions(&mut self, partials: &mut PartialPaths) {
        self.symbols
            .ensure_backwards(&mut partials.partial_symbol_stacks);
        self.symbols
            .ensure_forwards(&mut partials.partial_symbol_stacks);
    }

    fn ensure_forwards(&mut self, partials: &mut PartialPaths) {
        self.symbols
            .ensure_forwards(&mut partials.partial_symbol_stacks);
    }

    /// Returns the largest value of any symbol stack variable in this partial symbol stack.
    pub fn largest_symbol_stack_variable(&self) -> u32 {
        self.variable
            .into_option()
            .map(SymbolStackVariable::as_u32)
            .unwrap_or(0)
    }

    /// Returns the largest value of any scope stack variable in this partial symbol stack.
    pub fn largest_scope_stack_variable(&self, partials: &PartialPaths) -> u32 {
        // We don't have to check the postconditions, because it's not valid for a postcondition to
        // refer to a variable that doesn't exist in the precondition.
        self.iter_unordered(partials)
            .filter_map(|symbol| symbol.scopes.into_option())
            .filter_map(|scopes| scopes.variable.into_option())
            .map(ScopeStackVariable::as_u32)
            .max()
            .unwrap_or(0)
    }
}

impl DisplayWithPartialPaths for PartialSymbolStack {
    fn prepare(&mut self, graph: &StackGraph, partials: &mut PartialPaths) {
        // Ensure that our deque is pointed forwards while we still have a mutable reference to the
        // arena.
        self.symbols
            .ensure_forwards(&mut partials.partial_symbol_stacks);
        // And then prepare each symbol in the stack.
        let mut symbols = self.symbols;
        while let Some(mut symbol) = symbols
            .pop_front(&mut partials.partial_symbol_stacks)
            .copied()
        {
            symbol.prepare(graph, partials);
        }
    }

    fn display_with(
        &self,
        graph: &StackGraph,
        partials: &PartialPaths,
        f: &mut std::fmt::Formatter,
    ) -> std::fmt::Result {
        for symbol in self.symbols.iter_reused(&partials.partial_symbol_stacks) {
            symbol.display_with(graph, partials, f)?;
        }
        if let Some(variable) = self.variable.into_option() {
            if self.symbols.is_empty() {
                write!(f, "{}", variable)?;
            } else {
                write!(f, ",{}", variable)?;
            }
        }
        Ok(())
    }
}

//-------------------------------------------------------------------------------------------------
// Partial scope stacks

/// A pattern that might match against a scope stack.  Consists of a (possibly empty) list of
/// exported scopes, along with an optional scope stack variable.
#[repr(C)]
#[derive(Clone, Copy, Niche)]
pub struct PartialScopeStack {
    #[niche]
    scopes: Deque<Handle<Node>>,
    length: u32,
    variable: ControlledOption<ScopeStackVariable>,
}

impl PartialScopeStack {
    /// Returns whether this partial scope stack can match the empty scope stack.
    #[inline(always)]
    pub fn can_match_empty(&self) -> bool {
        self.scopes.is_empty()
    }

    /// Returns whether this partial scope stack can _only_ match the empty scope stack.
    #[inline(always)]
    pub fn can_only_match_empty(&self) -> bool {
        self.scopes.is_empty() && self.variable.is_none()
    }

    /// Returns whether this partial scope stack contains any scopes.
    #[inline(always)]
    pub fn contains_scopes(&self) -> bool {
        !self.scopes.is_empty()
    }

    /// Returns whether this partial scope stack has a scope stack variable.
    #[inline(always)]
    pub fn has_variable(&self) -> bool {
        self.variable.is_some()
    }

    #[inline(always)]
    pub fn len(&self) -> usize {
        self.length as usize
    }

    /// Returns an empty partial scope stack.
    pub fn empty() -> PartialScopeStack {
        PartialScopeStack {
            scopes: Deque::empty(),
            length: 0,
            variable: ControlledOption::none(),
        }
    }

    /// Returns a partial scope stack containing only a scope stack variable.
    pub fn from_variable(variable: ScopeStackVariable) -> PartialScopeStack {
        PartialScopeStack {
            scopes: Deque::empty(),
            length: 0,
            variable: ControlledOption::some(variable),
        }
    }

    /// Returns whether this partial scope stack is iterable in both directions without needing
    /// mutable access to the arena.
    pub fn have_reversal(&self, partials: &PartialPaths) -> bool {
        self.scopes.have_reversal(&partials.partial_scope_stacks)
    }

    /// Applies an offset to this partial scope stack.
    ///
    /// When concatenating partial paths, we have to ensure that the left- and right-hand sides
    /// have non-overlapping sets of variables.  To do this, we find the maximum value of any
    /// variable on the left-hand side, and add this “offset” to the values of all of the variables
    /// on the right-hand side.
    pub fn with_offset(mut self, scope_variable_offset: u32) -> PartialScopeStack {
        match self.variable.into_option() {
            Some(variable) => {
                self.variable = ControlledOption::some(variable.with_offset(scope_variable_offset));
            }
            None => {}
        };
        self
    }

    /// Returns whether two partial scope stacks match exactly the same set of scope stacks.
    pub fn matches(mut self, partials: &mut PartialPaths, mut other: PartialScopeStack) -> bool {
        while let Some(self_element) = self.pop_front(partials) {
            if let Some(other_element) = other.pop_front(partials) {
                if self_element != other_element {
                    return false;
                }
            } else {
                // Stacks aren't the same length.
                return false;
            }
        }
        if other.contains_scopes() {
            // Stacks aren't the same length.
            return false;
        }
        self.variable.into_option() == other.variable.into_option()
    }

    /// Applies a set of partial scope stack bindings to this partial scope stack, producing a new
    /// partial scope stack.
    pub fn apply_partial_bindings(
        mut self,
        partials: &mut PartialPaths,
        scope_bindings: &PartialScopeStackBindings,
    ) -> Result<PartialScopeStack, PathResolutionError> {
        // If this partial scope stack ends in a variable, see if we have a binding for it.  If so,
        // substitute that binding in.  If not, leave the variable as-is.
        let mut result = match self.variable.into_option() {
            Some(variable) => match scope_bindings.get(variable) {
                Some(bound) => bound,
                None => PartialScopeStack::from_variable(variable),
            },
            None => PartialScopeStack::empty(),
        };

        // Then prepend all of the scopes that appear at the beginning of this stack.
        while let Some(scope) = self.pop_back(partials) {
            result.push_front(partials, scope);
        }
        Ok(result)
    }

    /// Given two partial scope stacks, returns the largest possible partial scope stack such that
    /// any scope stack that satisfies the result also satisfies both inputs.  This takes into
    /// account any existing variable assignments, and updates those variable assignments with
    /// whatever constraints are necessary to produce a correct result.
    ///
    /// Note that this operation is commutative.  (Concatenating partial paths, defined in
    /// [`PartialPath::concatenate`][], is not.)
    pub fn unify(
        self,
        partials: &mut PartialPaths,
        mut rhs: PartialScopeStack,
        bindings: &mut PartialScopeStackBindings,
    ) -> Result<PartialScopeStack, PathResolutionError> {
        let mut lhs = self;
        let original_rhs = rhs;

        // First, look at the shortest common prefix of lhs and rhs, and verify that they match.
        while lhs.contains_scopes() && rhs.contains_scopes() {
            let lhs_front = lhs.pop_front(partials).unwrap();
            let rhs_front = rhs.pop_front(partials).unwrap();
            if lhs_front != rhs_front {
                return Err(PathResolutionError::ScopeStackUnsatisfied);
            }
        }

        // Now at most one stack still has scopes.  Zero, one, or both of them have variables.
        // Let's do a case analysis on all of those possibilities.

        // CASE 1:
        // Both lhs and rhs have no more scopes.  The answer is always yes, and any variables that
        // are present get bound.  (If both sides have variables, then one variable gets bound to
        // the other, since both lhs and rhs will match _any other scope stack_ at this point.  If
        // only one side has a variable, then the variable gets bound to the empty stack.)
        //
        //     lhs           rhs
        // ============  ============
        //  ()            ()            => yes either
        //  ()            () $2         => yes rhs, $2 => ()
        //  () $1         ()            => yes lhs, $1 => ()
        //  () $1         () $2         => yes lhs, $2 => $1
        if !lhs.contains_scopes() && !rhs.contains_scopes() {
            match (lhs.variable.into_option(), rhs.variable.into_option()) {
                (None, None) => return Ok(self),
                (None, Some(var)) => {
                    bindings.add(partials, var, PartialScopeStack::empty())?;
                    return Ok(original_rhs);
                }
                (Some(var), None) => {
                    bindings.add(partials, var, PartialScopeStack::empty())?;
                    return Ok(self);
                }
                (Some(lhs_var), Some(rhs_var)) => {
                    bindings.add(partials, rhs_var, PartialScopeStack::from_variable(lhs_var))?;
                    return Ok(self);
                }
            }
        }

        // CASE 2:
        // One of the stacks contains scopes and the other doesn't, and the “empty” stack doesn't
        // have a variable.  Since there's no variable on the empty side to capture the remaining
        // content on the non-empty side, the answer is always no.
        //
        //     lhs           rhs
        // ============  ============
        //  ()            (stuff)       => NO
        //  ()            (stuff) $2    => NO
        //  (stuff)       ()            => NO
        //  (stuff) $1    ()            => NO
        if !lhs.contains_scopes() && lhs.variable.is_none() {
            return Err(PathResolutionError::ScopeStackUnsatisfied);
        }
        if !rhs.contains_scopes() && rhs.variable.is_none() {
            return Err(PathResolutionError::ScopeStackUnsatisfied);
        }

        // CASE 3:
        // One of the stacks contains scopes and the other doesn't, and the “empty” stack _does_
        // have a variable.  If both sides have the same variable, the answer is NO. Otherwise,
        // the answer is YES, and the “empty” side's variable needs to capture the entirety of the
        // non-empty side.
        //
        //     lhs           rhs
        // ============  ============
        //  (...) $1      (...) $1      => no
        //  () $1         (stuff)       => yes rhs,  $1 => rhs
        //  () $1         (stuff) $2    => yes rhs,  $1 => rhs
        //  (stuff)       () $2         => yes lhs,  $2 => lhs
        //  (stuff) $1    () $2         => yes lhs,  $2 => lhs
        match (lhs.variable.into_option(), rhs.variable.into_option()) {
            (Some(v1), Some(v2)) if v1 == v2 => {
                return Err(PathResolutionError::ScopeStackUnsatisfied)
            }
            _ => {}
        }
        if lhs.contains_scopes() {
            let rhs_variable = rhs.variable.into_option().unwrap();
            bindings.add(partials, rhs_variable, lhs)?;
            return Ok(self);
        }
        if rhs.contains_scopes() {
            let lhs_variable = lhs.variable.into_option().unwrap();
            bindings.add(partials, lhs_variable, rhs)?;
            return Ok(original_rhs);
        }

        unreachable!();
    }

    /// Pushes a new [`Node`][] onto the front of this partial scope stack.  The node must be an
    /// _exported scope node_.
    ///
    /// [`Node`]: ../graph/enum.Node.html
    pub fn push_front(&mut self, partials: &mut PartialPaths, node: Handle<Node>) {
        self.length += 1;
        self.scopes
            .push_front(&mut partials.partial_scope_stacks, node);
    }

    /// Pushes a new [`Node`][] onto the back of this partial scope stack.  The node must be an
    /// _exported scope node_.
    ///
    /// [`Node`]: ../graph/enum.Node.html
    pub fn push_back(&mut self, partials: &mut PartialPaths, node: Handle<Node>) {
        self.length += 1;
        self.scopes
            .push_back(&mut partials.partial_scope_stacks, node);
    }

    /// Removes and returns the [`Node`][] at the front of this partial scope stack.  If the stack
    /// does not contain any exported scope nodes, returns `None`.
    pub fn pop_front(&mut self, partials: &mut PartialPaths) -> Option<Handle<Node>> {
        let result = self
            .scopes
            .pop_front(&mut partials.partial_scope_stacks)
            .copied();
        if result.is_some() {
            self.length -= 1;
        }
        result
    }

    /// Removes and returns the [`Node`][] at the back of this partial scope stack.  If the stack
    /// does not contain any exported scope nodes, returns `None`.
    pub fn pop_back(&mut self, partials: &mut PartialPaths) -> Option<Handle<Node>> {
        let result = self
            .scopes
            .pop_back(&mut partials.partial_scope_stacks)
            .copied();
        if result.is_some() {
            self.length -= 1;
        }
        result
    }

    /// Returns the scope stack variable at the end of this partial scope stack.  If the stack does
    /// not contain a scope stack variable, returns `None`.
    pub fn variable(&self) -> Option<ScopeStackVariable> {
        self.variable.into_option()
    }

    pub fn equals(self, partials: &mut PartialPaths, other: PartialScopeStack) -> bool {
        self.scopes
            .equals_with(&mut partials.partial_scope_stacks, other.scopes, |a, b| {
                *a == *b
            })
            && equals_option(
                self.variable.into_option(),
                other.variable.into_option(),
                |a, b| a == b,
            )
    }

    pub fn cmp(self, partials: &mut PartialPaths, other: PartialScopeStack) -> std::cmp::Ordering {
        std::cmp::Ordering::Equal
            .then_with(|| {
                self.scopes
                    .cmp_with(&mut partials.partial_scope_stacks, other.scopes, |a, b| {
                        a.cmp(b)
                    })
            })
            .then_with(|| {
                cmp_option(
                    self.variable.into_option(),
                    other.variable.into_option(),
                    |a, b| a.cmp(&b),
                )
            })
    }

    /// Returns an iterator over the scopes in this partial scope stack.
    pub fn iter_scopes<'a>(
        &self,
        partials: &'a mut PartialPaths,
    ) -> impl Iterator<Item = Handle<Node>> + 'a {
        self.scopes
            .iter(&mut partials.partial_scope_stacks)
            .copied()
    }

    /// Returns an iterator over the contents of this partial scope stack, with no guarantee
    /// about the ordering of the elements.
    pub fn iter_unordered<'a>(
        &self,
        partials: &'a PartialPaths,
    ) -> impl Iterator<Item = Handle<Node>> + 'a {
        self.scopes
            .iter_unordered(&partials.partial_scope_stacks)
            .copied()
    }

    pub fn display<'a>(
        self,
        graph: &'a StackGraph,
        partials: &'a mut PartialPaths,
    ) -> impl Display + 'a {
        display_with(self, graph, partials)
    }

    fn ensure_both_directions(&mut self, partials: &mut PartialPaths) {
        self.scopes
            .ensure_backwards(&mut partials.partial_scope_stacks);
        self.scopes
            .ensure_forwards(&mut partials.partial_scope_stacks);
    }

    fn ensure_forwards(&mut self, partials: &mut PartialPaths) {
        self.scopes
            .ensure_forwards(&mut partials.partial_scope_stacks);
    }

    /// Returns the largest value of any scope stack variable in this partial scope stack.
    pub fn largest_scope_stack_variable(&self) -> u32 {
        self.variable
            .into_option()
            .map(ScopeStackVariable::as_u32)
            .unwrap_or(0)
    }
}

impl DisplayWithPartialPaths for PartialScopeStack {
    fn prepare(&mut self, _graph: &StackGraph, partials: &mut PartialPaths) {
        self.scopes
            .ensure_forwards(&mut partials.partial_scope_stacks);
    }

    fn display_with(
        &self,
        graph: &StackGraph,
        partials: &PartialPaths,
        f: &mut std::fmt::Formatter,
    ) -> std::fmt::Result {
        let mut first = true;
        for scope in self.scopes.iter_reused(&partials.partial_scope_stacks) {
            if first {
                first = false;
            } else {
                write!(f, ",")?;
            }
            write!(f, "{:#}", scope.display(graph))?;
        }
        if let Some(variable) = self.variable.into_option() {
            if self.scopes.is_empty() {
                write!(f, "{}", variable)?;
            } else {
                write!(f, ",{}", variable)?;
            }
        }
        Ok(())
    }
}

//-------------------------------------------------------------------------------------------------
// Partial symbol bindings

pub struct PartialSymbolStackBindings {
    bindings: SmallVec<[Option<PartialSymbolStack>; 4]>,
}

impl PartialSymbolStackBindings {
    /// Creates a new, empty set of partial symbol stack bindings.
    pub fn new() -> PartialSymbolStackBindings {
        PartialSymbolStackBindings {
            bindings: SmallVec::new(),
        }
    }

    /// Returns the partial symbol stack that a particular symbol stack variable matched.  Returns an
    /// error if that variable didn't match anything.
    pub fn get(&self, variable: SymbolStackVariable) -> Option<PartialSymbolStack> {
        let index = variable.as_usize();
        if self.bindings.len() < index {
            return None;
        }
        self.bindings[index - 1]
    }

    /// Adds a new binding from a symbol stack variable to the partial symbol stack that it
    /// matched.  Returns an error if you try to bind a particular variable more than once.
    pub fn add(
        &mut self,
        partials: &mut PartialPaths,
        variable: SymbolStackVariable,
        mut symbols: PartialSymbolStack,
        scope_bindings: &mut PartialScopeStackBindings,
    ) -> Result<(), PathResolutionError> {
        let index = variable.as_usize();
        if self.bindings.len() < index {
            self.bindings.resize_with(index, || None);
        }
        if let Some(old_binding) = self.bindings[index - 1] {
            symbols = symbols.unify(partials, old_binding, self, scope_bindings)?;
        }
        self.bindings[index - 1] = Some(symbols);
        Ok(())
    }

    pub fn display<'a>(
        &'a mut self,
        graph: &'a StackGraph,
        partials: &'a mut PartialPaths,
    ) -> impl Display + 'a {
        display_with(self, graph, partials)
    }
}

impl<'a> DisplayWithPartialPaths for &'a mut PartialSymbolStackBindings {
    fn prepare(&mut self, graph: &StackGraph, partials: &mut PartialPaths) {
        for binding in &mut self.bindings {
            if let Some(binding) = binding.as_mut() {
                binding.prepare(graph, partials);
            }
        }
    }

    fn display_with(
        &self,
        graph: &StackGraph,
        partials: &PartialPaths,
        f: &mut std::fmt::Formatter,
    ) -> std::fmt::Result {
        write!(f, "{{")?;
        let mut first = true;
        for (idx, binding) in self.bindings.iter().enumerate() {
            if let Some(binding) = binding.as_ref() {
                if first {
                    first = false;
                } else {
                    write!(f, ", ")?;
                }
                write!(
                    f,
                    "%{} => <{}>",
                    idx + 1,
                    display_prepared(*binding, graph, partials)
                )?;
            }
        }
        write!(f, "}}")
    }
}

//-------------------------------------------------------------------------------------------------
// Partial scope bindings

pub struct PartialScopeStackBindings {
    bindings: SmallVec<[Option<PartialScopeStack>; 4]>,
}

impl PartialScopeStackBindings {
    /// Creates a new, empty set of partial scope stack bindings.
    pub fn new() -> PartialScopeStackBindings {
        PartialScopeStackBindings {
            bindings: SmallVec::new(),
        }
    }

    /// Returns the partial scope stack that a particular scope stack variable matched.  Returns an error
    /// if that variable didn't match anything.
    pub fn get(&self, variable: ScopeStackVariable) -> Option<PartialScopeStack> {
        let index = variable.as_usize();
        if self.bindings.len() < index {
            return None;
        }
        self.bindings[index - 1]
    }

    /// Adds a new binding from a scope stack variable to the partial scope stack that it matched.  Returns
    /// an error if you try to bind a particular variable more than once.
    pub fn add(
        &mut self,
        partials: &mut PartialPaths,
        variable: ScopeStackVariable,
        mut scopes: PartialScopeStack,
    ) -> Result<(), PathResolutionError> {
        let index = variable.as_usize();
        if self.bindings.len() < index {
            self.bindings.resize_with(index, || None);
        }
        if let Some(old_binding) = self.bindings[index - 1] {
            scopes = scopes.unify(partials, old_binding, self)?;
        }
        self.bindings[index - 1] = Some(scopes);
        Ok(())
    }

    pub fn display<'a>(
        &'a mut self,
        graph: &'a StackGraph,
        partials: &'a mut PartialPaths,
    ) -> impl Display + 'a {
        display_with(self, graph, partials)
    }
}

impl<'a> DisplayWithPartialPaths for &'a mut PartialScopeStackBindings {
    fn prepare(&mut self, graph: &StackGraph, partials: &mut PartialPaths) {
        for binding in &mut self.bindings {
            if let Some(binding) = binding.as_mut() {
                binding.prepare(graph, partials);
            }
        }
    }

    fn display_with(
        &self,
        graph: &StackGraph,
        partials: &PartialPaths,
        f: &mut std::fmt::Formatter,
    ) -> std::fmt::Result {
        write!(f, "{{")?;
        let mut first = true;
        for (idx, binding) in self.bindings.iter().enumerate() {
            if let Some(binding) = binding.as_ref() {
                if first {
                    first = false;
                } else {
                    write!(f, ", ")?;
                }
                write!(
                    f,
                    "${} => ({})",
                    idx + 1,
                    display_prepared(*binding, graph, partials)
                )?;
            }
        }
        write!(f, "}}")
    }
}

//-------------------------------------------------------------------------------------------------
// Edge lists

#[repr(C)]
#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct PartialPathEdge {
    pub source_node_id: NodeID,
    pub precedence: i32,
}

impl PartialPathEdge {
    /// Returns whether one edge shadows another.  Note that shadowing is not commutative — if path
    /// A shadows path B, the reverse is not true.
    pub fn shadows(self, other: PartialPathEdge) -> bool {
        self.source_node_id == other.source_node_id && self.precedence > other.precedence
    }

    pub fn display<'a>(
        self,
        graph: &'a StackGraph,
        partials: &'a mut PartialPaths,
    ) -> impl Display + 'a {
        display_with(self, graph, partials)
    }
}

impl DisplayWithPartialPaths for PartialPathEdge {
    fn display_with(
        &self,
        graph: &StackGraph,
        _partials: &PartialPaths,
        f: &mut std::fmt::Formatter,
    ) -> std::fmt::Result {
        match graph.node_for_id(self.source_node_id) {
            Some(node) => write!(f, "{:#}", node.display(graph))?,
            None => write!(f, "[missing]")?,
        }
        if self.precedence != 0 {
            write!(f, "({})", self.precedence)?;
        }
        Ok(())
    }
}

/// The edges in a path keep track of precedence information so that we can correctly handle
/// shadowed definitions.
#[repr(C)]
#[derive(Clone, Copy, Niche)]
pub struct PartialPathEdgeList {
    #[niche]
    edges: Deque<PartialPathEdge>,
    length: u32,
}

impl PartialPathEdgeList {
    /// Returns whether this edge list is empty.
    #[inline(always)]
    pub fn is_empty(&self) -> bool {
        self.edges.is_empty()
    }

    #[inline(always)]
    pub fn len(&self) -> usize {
        self.length as usize
    }

    /// Returns whether this edge list is iterable in both directions without needing mutable
    /// access to the arena.
    pub fn have_reversal(&self, partials: &PartialPaths) -> bool {
        self.edges.have_reversal(&partials.partial_path_edges)
    }

    /// Returns an empty edge list.
    pub fn empty() -> PartialPathEdgeList {
        PartialPathEdgeList {
            edges: Deque::empty(),
            length: 0,
        }
    }

    /// Pushes a new edge onto the front of this edge list.
    pub fn push_front(&mut self, partials: &mut PartialPaths, edge: PartialPathEdge) {
        self.length += 1;
        self.edges
            .push_front(&mut partials.partial_path_edges, edge);
    }

    /// Pushes a new edge onto the back of this edge list.
    pub fn push_back(&mut self, partials: &mut PartialPaths, edge: PartialPathEdge) {
        self.length += 1;
        self.edges.push_back(&mut partials.partial_path_edges, edge);
    }

    /// Removes and returns the edge at the front of this edge list.  If the list is empty, returns
    /// `None`.
    pub fn pop_front(&mut self, partials: &mut PartialPaths) -> Option<PartialPathEdge> {
        let result = self.edges.pop_front(&mut partials.partial_path_edges);
        if result.is_some() {
            self.length -= 1;
        }
        result.copied()
    }

    /// Removes and returns the edge at the back of this edge list.  If the list is empty, returns
    /// `None`.
    pub fn pop_back(&mut self, partials: &mut PartialPaths) -> Option<PartialPathEdge> {
        let result = self.edges.pop_back(&mut partials.partial_path_edges);
        if result.is_some() {
            self.length -= 1;
        }
        result.copied()
    }

    pub fn display<'a>(
        self,
        graph: &'a StackGraph,
        partials: &'a mut PartialPaths,
    ) -> impl Display + 'a {
        display_with(self, graph, partials)
    }

    /// Returns whether one edge list shadows another.  Note that shadowing is not commutative — if
    /// path A shadows path B, the reverse is not true.
    pub fn shadows(mut self, partials: &mut PartialPaths, mut other: PartialPathEdgeList) -> bool {
        while let Some(self_edge) = self.pop_front(partials) {
            if let Some(other_edge) = other.pop_front(partials) {
                if self_edge.source_node_id != other_edge.source_node_id {
                    return false;
                } else if self_edge.shadows(other_edge) {
                    return true;
                }
            } else {
                return false;
            }
        }
        false
    }

    pub fn equals(mut self, partials: &mut PartialPaths, mut other: PartialPathEdgeList) -> bool {
        while let Some(self_edge) = self.pop_front(partials) {
            if let Some(other_edge) = other.pop_front(partials) {
                if self_edge != other_edge {
                    return false;
                }
            } else {
                return false;
            }
        }
        other.edges.is_empty()
    }

    pub fn cmp(
        mut self,
        partials: &mut PartialPaths,
        mut other: PartialPathEdgeList,
    ) -> std::cmp::Ordering {
        use std::cmp::Ordering;
        while let Some(self_edge) = self.pop_front(partials) {
            if let Some(other_edge) = other.pop_front(partials) {
                match self_edge.cmp(&other_edge) {
                    Ordering::Equal => continue,
                    result @ _ => return result,
                }
            } else {
                return Ordering::Greater;
            }
        }
        if other.edges.is_empty() {
            Ordering::Equal
        } else {
            Ordering::Less
        }
    }

    /// Returns an iterator over the contents of this edge list.
    pub fn iter<'a>(
        &self,
        partials: &'a mut PartialPaths,
    ) -> impl Iterator<Item = PartialPathEdge> + 'a {
        self.edges.iter(&mut partials.partial_path_edges).copied()
    }

    /// Returns an iterator over the contents of this edge list, with no guarantee about the
    /// ordering of the elements.
    pub fn iter_unordered<'a>(
        &self,
        partials: &'a PartialPaths,
    ) -> impl Iterator<Item = PartialPathEdge> + 'a {
        self.edges
            .iter_unordered(&partials.partial_path_edges)
            .copied()
    }

    fn ensure_both_directions(&mut self, partials: &mut PartialPaths) {
        self.edges
            .ensure_backwards(&mut partials.partial_path_edges);
        self.edges.ensure_forwards(&mut partials.partial_path_edges);
    }

    fn ensure_forwards(&mut self, partials: &mut PartialPaths) {
        self.edges.ensure_forwards(&mut partials.partial_path_edges);
    }
}

impl DisplayWithPartialPaths for PartialPathEdgeList {
    fn prepare(&mut self, graph: &StackGraph, partials: &mut PartialPaths) {
        self.edges.ensure_forwards(&mut partials.partial_path_edges);
        let mut edges = self.edges;
        while let Some(mut edge) = edges.pop_front(&mut partials.partial_path_edges).copied() {
            edge.prepare(graph, partials);
        }
    }

    fn display_with(
        &self,
        graph: &StackGraph,
        partials: &PartialPaths,
        f: &mut std::fmt::Formatter,
    ) -> std::fmt::Result {
        for edge in self.edges.iter_reused(&partials.partial_path_edges) {
            edge.display_with(graph, partials, f)?;
        }
        Ok(())
    }
}

//-------------------------------------------------------------------------------------------------
// Partial paths

/// A portion of a name-binding path.
///
/// Partial paths can be computed _incrementally_, in which case all of the edges in the partial
/// path belong to a single file.  At query time, we can efficiently concatenate partial paths to
/// yield a name-binding path.
///
/// Paths describe the contents of the symbol stack and scope stack at the end of the path.
/// Partial paths, on the other hand, have _preconditions_ and _postconditions_ for each stack.
/// The precondition describes what the stack must look like for us to be able to concatenate this
/// partial path onto the end of a path.  The postcondition describes what the resulting stack
/// looks like after doing so.
///
/// The preconditions can contain _scope stack variables_, which describe parts of the scope stack
/// (or parts of a scope symbol's attached scope list) whose contents we don't care about.  The
/// postconditions can _also_ refer to those variables, and describe how those variable parts of
/// the input scope stacks are carried through unmodified into the resulting scope stack.
#[repr(C)]
#[derive(Clone)]
pub struct PartialPath {
    pub start_node: Handle<Node>,
    pub end_node: Handle<Node>,
    pub symbol_stack_precondition: PartialSymbolStack,
    pub symbol_stack_postcondition: PartialSymbolStack,
    pub scope_stack_precondition: PartialScopeStack,
    pub scope_stack_postcondition: PartialScopeStack,
    pub edges: PartialPathEdgeList,
}

impl PartialPath {
    /// Creates a new empty partial path starting at a stack graph node.
    pub fn from_node(
        graph: &StackGraph,
        partials: &mut PartialPaths,
        node: Handle<Node>,
    ) -> PartialPath {
        let initial_symbol_stack = SymbolStackVariable::initial();
        let initial_scope_stack = ScopeStackVariable::initial();
        let mut symbol_stack_precondition = PartialSymbolStack::from_variable(initial_symbol_stack);
        let mut symbol_stack_postcondition =
            PartialSymbolStack::from_variable(initial_symbol_stack);
        let mut scope_stack_precondition = PartialScopeStack::from_variable(initial_scope_stack);
        let mut scope_stack_postcondition = PartialScopeStack::from_variable(initial_scope_stack);

        graph[node]
            .append_to_partial_stacks(
                graph,
                partials,
                &mut symbol_stack_precondition,
                &mut scope_stack_precondition,
                &mut symbol_stack_postcondition,
                &mut scope_stack_postcondition,
            )
            .expect("lifting single nodes to partial paths should not fail");

        PartialPath {
            start_node: node,
            end_node: node,
            symbol_stack_precondition,
            symbol_stack_postcondition,
            scope_stack_precondition,
            scope_stack_postcondition,
            edges: PartialPathEdgeList::empty(),
        }
    }

    /// Returns whether one path shadows another.  Note that shadowing is not commutative — if path
    /// A shadows path B, the reverse is not true.
    pub fn shadows(&self, partials: &mut PartialPaths, other: &PartialPath) -> bool {
        self.edges.shadows(partials, other.edges)
    }

    pub fn equals(&self, partials: &mut PartialPaths, other: &PartialPath) -> bool {
        self.start_node == other.start_node
            && self.end_node == other.end_node
            && self
                .symbol_stack_precondition
                .equals(partials, other.symbol_stack_precondition)
            && self
                .symbol_stack_postcondition
                .equals(partials, other.symbol_stack_postcondition)
            && self
                .scope_stack_precondition
                .equals(partials, other.scope_stack_precondition)
            && self
                .scope_stack_postcondition
                .equals(partials, other.scope_stack_postcondition)
    }

    pub fn cmp(
        &self,
        graph: &StackGraph,
        partials: &mut PartialPaths,
        other: &PartialPath,
    ) -> std::cmp::Ordering {
        std::cmp::Ordering::Equal
            .then_with(|| self.start_node.cmp(&other.start_node))
            .then_with(|| self.end_node.cmp(&other.end_node))
            .then_with(|| {
                self.symbol_stack_precondition
                    .cmp(graph, partials, other.symbol_stack_precondition)
            })
            .then_with(|| {
                self.symbol_stack_postcondition.cmp(
                    graph,
                    partials,
                    other.symbol_stack_postcondition,
                )
            })
            .then_with(|| {
                self.scope_stack_precondition
                    .cmp(partials, other.scope_stack_precondition)
            })
            .then_with(|| {
                self.scope_stack_postcondition
                    .cmp(partials, other.scope_stack_postcondition)
            })
    }

    /// Returns whether a partial path represents the start of a name binding from a reference to a
    /// definition.
    pub fn starts_at_reference(&self, graph: &StackGraph) -> bool {
        graph[self.start_node].is_reference()
            && self.symbol_stack_precondition.can_match_empty()
            && self.scope_stack_precondition.can_match_empty()
    }

    /// Returns whether a partial path represents the end of a name binding from a reference to a
    /// definition.
    pub fn ends_at_definition(&self, graph: &StackGraph) -> bool {
        graph[self.end_node].is_definition() && self.symbol_stack_postcondition.can_match_empty()
    }

    /// A _complete_ partial path represents a full name binding that resolves a reference to a
    /// definition.
    pub fn is_complete(&self, graph: &StackGraph) -> bool {
        self.starts_at_reference(graph) && self.ends_at_definition(graph)
    }

    pub fn starts_at_endpoint(&self, graph: &StackGraph) -> bool {
        graph[self.start_node].is_endpoint()
    }

    pub fn ends_at_endpoint(&self, graph: &StackGraph) -> bool {
        graph[self.end_node].is_endpoint()
    }

    pub fn ends_in_jump(&self, graph: &StackGraph) -> bool {
        graph[self.end_node].is_jump_to()
    }

    /// Returns whether a partial path is cyclic---that is, it starts and ends at the same node,
    /// and its postcondition is compatible with its precondition.  If the path is cyclic, a
    /// tuple is returned indicating whether cycle requires strengthening the pre- or postcondition.
    pub fn is_cyclic(&self, graph: &StackGraph, partials: &mut PartialPaths) -> Option<Cyclicity> {
        // StackGraph ensures that there are no nodes with duplicate IDs, so we can do a simple
        // comparison of node handles here.
        if self.start_node != self.end_node {
            return None;
        }

        let lhs = self;
        let mut rhs = self.clone();
        rhs.ensure_no_overlapping_variables(partials, lhs);

        let join = match Self::compute_join(graph, partials, lhs, &rhs) {
            Ok(join) => join,
            Err(_) => return None,
        };

        if lhs
            .symbol_stack_precondition
            .variable
            .into_option()
            .map_or(false, |v| join.symbol_bindings.get(v).iter().len() > 0)
            || lhs
                .scope_stack_precondition
                .variable
                .into_option()
                .map_or(false, |v| join.scope_bindings.get(v).iter().len() > 0)
        {
            Some(Cyclicity::StrengthensPrecondition)
        } else if rhs
            .symbol_stack_postcondition
            .variable
            .into_option()
            .map_or(false, |v| join.symbol_bindings.get(v).iter().len() > 0)
            || rhs
                .scope_stack_postcondition
                .variable
                .into_option()
                .map_or(false, |v| join.scope_bindings.get(v).iter().len() > 0)
        {
            Some(Cyclicity::StrengthensPostcondition)
        } else {
            Some(Cyclicity::Free)
        }
    }

    /// Ensures that the content of this partial path is available in both forwards and backwards
    /// directions.
    pub fn ensure_both_directions(&mut self, partials: &mut PartialPaths) {
        self.symbol_stack_precondition
            .ensure_both_directions(partials);
        self.symbol_stack_postcondition
            .ensure_both_directions(partials);
        self.scope_stack_precondition
            .ensure_both_directions(partials);
        self.scope_stack_postcondition
            .ensure_both_directions(partials);
        self.edges.ensure_both_directions(partials);

        let mut stack = self.symbol_stack_precondition;
        while let Some(symbol) = stack.pop_front(partials) {
            if let Some(mut scopes) = symbol.scopes.into_option() {
                scopes.ensure_both_directions(partials);
            }
        }

        let mut stack = self.symbol_stack_postcondition;
        while let Some(symbol) = stack.pop_front(partials) {
            if let Some(mut scopes) = symbol.scopes.into_option() {
                scopes.ensure_both_directions(partials);
            }
        }
    }

    /// Ensures that the content of this partial path is in forwards direction.
    pub fn ensure_forwards(&mut self, partials: &mut PartialPaths) {
        self.symbol_stack_precondition.ensure_forwards(partials);
        self.symbol_stack_postcondition.ensure_forwards(partials);
        self.scope_stack_precondition.ensure_forwards(partials);
        self.scope_stack_postcondition.ensure_forwards(partials);
        self.edges.ensure_forwards(partials);

        let mut stack = self.symbol_stack_precondition;
        while let Some(symbol) = stack.pop_front(partials) {
            if let Some(mut scopes) = symbol.scopes.into_option() {
                scopes.ensure_forwards(partials);
            }
        }

        let mut stack = self.symbol_stack_postcondition;
        while let Some(symbol) = stack.pop_front(partials) {
            if let Some(mut scopes) = symbol.scopes.into_option() {
                scopes.ensure_forwards(partials);
            }
        }
    }

    /// Returns the largest value of any symbol stack variable in this partial path.
    pub fn largest_symbol_stack_variable(&self) -> u32 {
        // We don't have to check the postconditions, because it's not valid for a postcondition to
        // refer to a variable that doesn't exist in the precondition.
        self.symbol_stack_precondition
            .largest_symbol_stack_variable()
    }

    /// Returns the largest value of any scope stack variable in this partial path.
    pub fn largest_scope_stack_variable(&self, partials: &PartialPaths) -> u32 {
        Self::largest_scope_stack_variable_for_partial_stacks(
            partials,
            &self.symbol_stack_precondition,
            &self.scope_stack_precondition,
        )
    }

    fn largest_scope_stack_variable_for_partial_stacks(
        partials: &PartialPaths,
        symbol_stack_precondition: &PartialSymbolStack,
        scope_stack_precondition: &PartialScopeStack,
    ) -> u32 {
        // We don't have to check the postconditions, because it's not valid for a postcondition to
        // refer to a variable that doesn't exist in the precondition.
        std::cmp::max(
            symbol_stack_precondition.largest_scope_stack_variable(partials),
            scope_stack_precondition.largest_scope_stack_variable(),
        )
    }

    /// Returns a fresh scope stack variable that is not already used anywhere in this partial
    /// path.
    pub fn fresh_scope_stack_variable(&self, partials: &PartialPaths) -> ScopeStackVariable {
        Self::fresh_scope_stack_variable_for_partial_stack(
            partials,
            &self.symbol_stack_precondition,
            &self.scope_stack_precondition,
        )
    }

    fn fresh_scope_stack_variable_for_partial_stack(
        partials: &PartialPaths,
        symbol_stack_precondition: &PartialSymbolStack,
        scope_stack_precondition: &PartialScopeStack,
    ) -> ScopeStackVariable {
        ScopeStackVariable::fresher_than(Self::largest_scope_stack_variable_for_partial_stacks(
            partials,
            symbol_stack_precondition,
            scope_stack_precondition,
        ))
    }

    pub fn display<'a>(
        &'a self,
        graph: &'a StackGraph,
        partials: &'a mut PartialPaths,
    ) -> impl Display + 'a {
        display_with(self, graph, partials)
    }
}

#[derive(Debug, EnumSetType)]
pub enum Cyclicity {
    /// The path can be freely concatenated to itself.
    Free,
    /// Concatenating the path to itself strengthens the precondition---symbols are eliminated from the stack.
    StrengthensPrecondition,
    /// Concatenating the path to itself strengthens the postcondition---symbols are introduced on the stack.
    StrengthensPostcondition,
}

impl<'a> DisplayWithPartialPaths for &'a PartialPath {
    fn prepare(&mut self, graph: &StackGraph, partials: &mut PartialPaths) {
        self.symbol_stack_precondition
            .clone()
            .prepare(graph, partials);
        self.symbol_stack_postcondition
            .clone()
            .prepare(graph, partials);
        self.scope_stack_precondition
            .clone()
            .prepare(graph, partials);
        self.scope_stack_postcondition
            .clone()
            .prepare(graph, partials);
    }

    fn display_with(
        &self,
        graph: &StackGraph,
        partials: &PartialPaths,
        f: &mut std::fmt::Formatter,
    ) -> std::fmt::Result {
        write!(
            f,
            "<{}> ({}) {} -> {} <{}> ({})",
            display_prepared(self.symbol_stack_precondition, graph, partials),
            display_prepared(self.scope_stack_precondition, graph, partials),
            self.start_node.display(graph),
            self.end_node.display(graph),
            display_prepared(self.symbol_stack_postcondition, graph, partials),
            display_prepared(self.scope_stack_postcondition, graph, partials),
        )
    }
}

impl PartialPath {
    /// Modifies this partial path so that it has no symbol or scope stack variables in common with
    /// another partial path.
    pub fn ensure_no_overlapping_variables(
        &mut self,
        partials: &mut PartialPaths,
        other: &PartialPath,
    ) {
        let symbol_variable_offset = other.largest_symbol_stack_variable();
        let scope_variable_offset = other.largest_scope_stack_variable(partials);
        self.symbol_stack_precondition = self.symbol_stack_precondition.with_offset(
            partials,
            symbol_variable_offset,
            scope_variable_offset,
        );
        self.symbol_stack_postcondition = self.symbol_stack_postcondition.with_offset(
            partials,
            symbol_variable_offset,
            scope_variable_offset,
        );
        self.scope_stack_precondition = self
            .scope_stack_precondition
            .with_offset(scope_variable_offset);
        self.scope_stack_postcondition = self
            .scope_stack_postcondition
            .with_offset(scope_variable_offset);
    }

    /// Replaces stack variables in the precondition with empty stacks.
    pub fn eliminate_precondition_stack_variables(&mut self, partials: &mut PartialPaths) {
        let mut symbol_bindings = PartialSymbolStackBindings::new();
        let mut scope_bindings = PartialScopeStackBindings::new();
        if let Some(symbol_variable) = self.symbol_stack_precondition.variable() {
            symbol_bindings
                .add(
                    partials,
                    symbol_variable,
                    PartialSymbolStack::empty(),
                    &mut scope_bindings,
                )
                .unwrap();
        }
        if let Some(scope_variable) = self.scope_stack_precondition.variable() {
            scope_bindings
                .add(partials, scope_variable, PartialScopeStack::empty())
                .unwrap();
        }

        self.symbol_stack_precondition = self
            .symbol_stack_precondition
            .apply_partial_bindings(partials, &symbol_bindings, &scope_bindings)
            .unwrap();
        self.scope_stack_precondition = self
            .scope_stack_precondition
            .apply_partial_bindings(partials, &scope_bindings)
            .unwrap();

        self.symbol_stack_postcondition = self
            .symbol_stack_postcondition
            .apply_partial_bindings(partials, &symbol_bindings, &scope_bindings)
            .unwrap();
        self.scope_stack_postcondition = self
            .scope_stack_postcondition
            .apply_partial_bindings(partials, &scope_bindings)
            .unwrap();
    }

    /// Attempts to append an edge to the end of a partial path.  If the edge is not a valid
    /// extension of this partial path, we return an error describing why.
    pub fn append(
        &mut self,
        graph: &StackGraph,
        partials: &mut PartialPaths,
        edge: Edge,
    ) -> Result<(), PathResolutionError> {
        if edge.source != self.end_node {
            return Err(PathResolutionError::IncorrectSourceNode);
        }

        graph[edge.sink].append_to_partial_stacks(
            graph,
            partials,
            &mut self.symbol_stack_precondition,
            &mut self.scope_stack_precondition,
            &mut self.symbol_stack_postcondition,
            &mut self.scope_stack_postcondition,
        )?;

        self.end_node = edge.sink;
        self.edges.push_back(
            partials,
            PartialPathEdge {
                source_node_id: graph[edge.source].id(),
                precedence: edge.precedence,
            },
        );

        self.resolve_from_postcondition(graph, partials)?;

        Ok(())
    }

    /// Attempts to resolve any _jump to scope_ node at the end of a partial path from the postcondition
    /// scope stack.  If the partial path does not end in a _jump to scope_ node, we do nothing.  If it
    /// does, and we cannot resolve it, then we return an error describing why.
    pub fn resolve_from_postcondition(
        &mut self,
        graph: &StackGraph,
        partials: &mut PartialPaths,
    ) -> Result<(), PathResolutionError> {
        if !graph[self.end_node].is_jump_to() {
            return Ok(());
        }
        if self.scope_stack_postcondition.can_only_match_empty() {
            return Err(PathResolutionError::EmptyScopeStack);
        }
        if !self.scope_stack_postcondition.contains_scopes() {
            return Ok(());
        }
        let top_scope = self.scope_stack_postcondition.pop_front(partials).unwrap();
        self.edges.push_back(
            partials,
            PartialPathEdge {
                source_node_id: graph[self.end_node].id(),
                precedence: 0,
            },
        );
        self.end_node = top_scope;
        Ok(())
    }

    /// Resolve any _jump to scope_ node at the end of a partial path to the given node, updating the
    /// precondition to include the given node.  If the partial path does not end in a _jump to scope_
    /// node, we do nothing.  If it does, and we cannot resolve it, then we return an error describing
    /// why.
    pub fn resolve_to_node(
        &mut self,
        graph: &StackGraph,
        partials: &mut PartialPaths,
        node: Handle<Node>,
    ) -> Result<(), PathResolutionError> {
        if !graph[self.end_node].is_jump_to() {
            return Ok(());
        }

        let scope_variable = match self.scope_stack_postcondition.variable() {
            Some(scope_variable) => scope_variable,
            None => return Err(PathResolutionError::ScopeStackUnsatisfied),
        };
        let mut scope_stack = PartialScopeStack::from_variable(scope_variable);
        scope_stack.push_front(partials, node);

        let symbol_bindings = PartialSymbolStackBindings::new();
        let mut scope_bindings = PartialScopeStackBindings::new();
        scope_bindings
            .add(partials, scope_variable, scope_stack)
            .unwrap();

        self.symbol_stack_precondition = self
            .symbol_stack_precondition
            .apply_partial_bindings(partials, &symbol_bindings, &scope_bindings)
            .unwrap();
        self.scope_stack_precondition = self
            .scope_stack_precondition
            .apply_partial_bindings(partials, &scope_bindings)
            .unwrap();

        self.end_node = node;

        Ok(())
    }
}

impl Node {
    /// Update the given partial path pre- and postconditions with the effect of
    /// appending this node to that partial path.
    pub(crate) fn append_to_partial_stacks(
        &self,
        graph: &StackGraph,
        partials: &mut PartialPaths,
        symbol_stack_precondition: &mut PartialSymbolStack,
        scope_stack_precondition: &mut PartialScopeStack,
        symbol_stack_postcondition: &mut PartialSymbolStack,
        scope_stack_postcondition: &mut PartialScopeStack,
    ) -> Result<(), PathResolutionError> {
        match self {
            Self::DropScopes(_) => {
                *scope_stack_postcondition = PartialScopeStack::empty();
            }
            Self::JumpTo(_) => {}
            Self::PopScopedSymbol(sink) => {
                // Ideally we want to pop sink's scoped symbol off from top of the symbol stack
                // postcondition.
                if let Some(top) = symbol_stack_postcondition.pop_front(partials) {
                    if top.symbol != sink.symbol {
                        return Err(PathResolutionError::IncorrectPoppedSymbol);
                    }
                    let new_scope_stack = match top.scopes.into_option() {
                        Some(scopes) => scopes,
                        None => return Err(PathResolutionError::MissingAttachedScopeList),
                    };
                    *scope_stack_postcondition = new_scope_stack;
                } else if symbol_stack_postcondition.has_variable() {
                    // If the symbol stack postcondition is empty but has a variable, then we can update
                    // the _precondition_ to indicate that the symbol stack needs to contain this scoped
                    // symbol in order to successfully use this partial path.
                    let scope_stack_variable =
                        PartialPath::fresh_scope_stack_variable_for_partial_stack(
                            partials,
                            symbol_stack_precondition,
                            scope_stack_precondition,
                        );
                    let precondition_symbol = PartialScopedSymbol {
                        symbol: sink.symbol,
                        scopes: ControlledOption::some(PartialScopeStack::from_variable(
                            scope_stack_variable,
                        )),
                    };
                    // We simply push to the precondition. The official procedure here
                    // is to bind the postcondition symbol stack variable to the symbol
                    // and a fresh variable, and apply that. However, because the variable
                    // can only be bound in the precondition symbol stack, this amounts to
                    // pushing the symbol there directly.
                    symbol_stack_precondition.push_back(partials, precondition_symbol);
                    *scope_stack_postcondition =
                        PartialScopeStack::from_variable(scope_stack_variable);
                } else {
                    // The symbol stack postcondition is empty and has no variable, so we cannot
                    // perform the operation.
                    return Err(PathResolutionError::SymbolStackUnsatisfied);
                }
            }
            Self::PopSymbol(sink) => {
                // Ideally we want to pop sink's symbol off from top of the symbol stack postcondition.
                if let Some(top) = symbol_stack_postcondition.pop_front(partials) {
                    if top.symbol != sink.symbol {
                        return Err(PathResolutionError::IncorrectPoppedSymbol);
                    }
                    if top.scopes.is_some() {
                        return Err(PathResolutionError::UnexpectedAttachedScopeList);
                    }
                } else if symbol_stack_postcondition.has_variable() {
                    // If the symbol stack postcondition is empty but has a variable, then we can update
                    // the _precondition_ to indicate that the symbol stack needs to contain this symbol
                    // in order to successfully use this partial path.
                    let precondition_symbol = PartialScopedSymbol {
                        symbol: sink.symbol,
                        scopes: ControlledOption::none(),
                    };
                    // We simply push to the precondition. The official procedure here
                    // is to bind the postcondition symbol stack variable to the symbol
                    // and a fresh variable, and apply that. However, because the variable
                    // can only be bound in the precondition symbol stack, this amounts to
                    // pushing the symbol there directly.
                    symbol_stack_precondition.push_back(partials, precondition_symbol);
                } else {
                    // The symbol stack postcondition is empty and has no variable, so we cannot
                    // perform the operation.
                    return Err(PathResolutionError::SymbolStackUnsatisfied);
                }
            }
            Self::PushScopedSymbol(sink) => {
                // The symbol stack postcondition is our representation of the path's symbol stack.
                // Pushing the scoped symbol onto our postcondition indicates that using this partial
                // path would push the scoped symbol onto the path's symbol stack.
                let sink_symbol = sink.symbol;
                let sink_scope = graph
                    .node_for_id(sink.scope)
                    .ok_or(PathResolutionError::UnknownAttachedScope)?;
                let mut attached_scopes = scope_stack_postcondition.clone();
                attached_scopes.push_front(partials, sink_scope);
                let postcondition_symbol = PartialScopedSymbol {
                    symbol: sink_symbol,
                    scopes: ControlledOption::some(attached_scopes),
                };
                symbol_stack_postcondition.push_front(partials, postcondition_symbol);
            }
            Self::PushSymbol(sink) => {
                // The symbol stack postcondition is our representation of the path's symbol stack.
                // Pushing the symbol onto our postcondition indicates that using this partial path
                // would push the symbol onto the path's symbol stack.
                let sink_symbol = sink.symbol;
                let postcondition_symbol = PartialScopedSymbol {
                    symbol: sink_symbol,
                    scopes: ControlledOption::none(),
                };
                symbol_stack_postcondition.push_front(partials, postcondition_symbol);
            }
            Self::Root(_) => {}
            Self::Scope(_) => {}
        }
        Ok(())
    }

    /// Ensure the given closed precondition stacks are half-open for this end node.
    ///
    /// Partial paths have closed (cf. a closed interval) pre- and postconditions, which means
    /// the start node is reflected in the precondition, and the end node is reflected in the
    /// postcondition. For example, a path starting with a pop node, has a precondition starting
    /// with the popped symbol. Similarly, a ending with a push node, has a postcondition ending
    /// with the pushed symbol.
    ///
    /// When concatenating two partial paths, their closed pre- and postconditions are not compatible,
    /// because the effect of the join node (i.e., the node shared between the two paths) is present
    /// in both the right and the left path. If two paths join at a push node, the right postcondition
    /// contains the pushed symbol, while the left precondition does not contain it, behaving as if the
    /// symbol was pushed twice. Similarly, when joining at a pop node, the right precondition contains
    /// the popped symbol, while the right postcondition will not anymore, because it was already popped.
    /// Unifying closed pre- and postconditions can result in incorrect concatenation results.
    ///
    /// We can make pre- and postconditions compatible again by making them half-open (cf. open intervals,
    /// but half because we only undo the effect of some node types). A precondition is half-open if it
    /// does not reflect the effect if a start pop node, Similarly, a postcondition is half-open if it
    /// does not reflect the effect of an end push node. Unifying half-open pre- and postconditions results
    /// in the correct behavior for path concatenation.
    fn halfopen_closed_partial_precondition(
        &self,
        partials: &mut PartialPaths,
        symbol_stack: &mut PartialSymbolStack,
        scope_stack: &mut PartialScopeStack,
    ) -> Result<(), PathResolutionError> {
        match self {
            Node::DropScopes(_) => {}
            Node::JumpTo(_) => {}
            Node::PopScopedSymbol(node) => {
                let symbol = symbol_stack
                    .pop_front(partials)
                    .ok_or(PathResolutionError::EmptySymbolStack)?;
                if symbol.symbol != node.symbol {
                    return Err(PathResolutionError::IncorrectPoppedSymbol);
                }
                *scope_stack = symbol.scopes.into_option().unwrap();
            }
            Node::PopSymbol(node) => {
                let symbol = symbol_stack
                    .pop_front(partials)
                    .ok_or(PathResolutionError::EmptySymbolStack)?;
                if symbol.symbol != node.symbol {
                    return Err(PathResolutionError::IncorrectPoppedSymbol);
                }
            }
            Node::PushScopedSymbol(_) => {}
            Node::PushSymbol(_) => {}
            Node::Root(_) => {}
            Node::Scope(_) => {}
        };
        Ok(())
    }

    /// Ensure the given closed postcondition stacks are half-open for this start node.
    ///
    /// Partial paths have closed (cf. a closed interval) pre- and postconditions, which means
    /// the start node is reflected in the precondition, and the end node is reflected in the
    /// postcondition. For example, a path starting with a pop node, has a precondition starting
    /// with the popped symbol. Similarly, a ending with a push node, has a postcondition ending
    /// with the pushed symbol.
    ///
    /// When concatenating two partial paths, their closed pre- and postconditions are not compatible,
    /// because the effect of the join node (i.e., the node shared between the two paths) is present
    /// in both the right and the left path. If two paths join at a push node, the right postcondition
    /// contains the pushed symbol, while the left precondition does not contain it, behaving as if the
    /// symbol was pushed twice. Similarly, when joining at a pop node, the right precondition contains
    /// the popped symbol, while the right postcondition will not anymore, because it was already popped.
    /// Unifying closed pre- and postconditions can result in incorrect concatenation results.
    ///
    /// We can make pre- and postconditions compatible again by making them half-open (cf. open intervals,
    /// but half because we only undo the effect of some node types). A precondition is half-open if it
    /// does not reflect the effect if a start pop node, Similarly, a postcondition is half-open if it
    /// does not reflect the effect of an end push node. Unifying half-open pre- and postconditions results
    /// in the correct behavior for path concatenation.
    fn halfopen_closed_partial_postcondition(
        &self,
        partials: &mut PartialPaths,
        symbol_stack: &mut PartialSymbolStack,
        _scope_stack: &mut PartialScopeStack,
    ) -> Result<(), PathResolutionError> {
        match self {
            Self::DropScopes(_) => {}
            Self::JumpTo(_) => {}
            Self::PopScopedSymbol(_) => {}
            Self::PopSymbol(_) => {}
            Self::PushScopedSymbol(node) => {
                let symbol = symbol_stack
                    .pop_front(partials)
                    .ok_or(PathResolutionError::EmptySymbolStack)?;
                if symbol.symbol != node.symbol {
                    return Err(PathResolutionError::IncorrectPoppedSymbol);
                }
            }
            Self::PushSymbol(node) => {
                let symbol = symbol_stack
                    .pop_front(partials)
                    .ok_or(PathResolutionError::EmptySymbolStack)?;
                if symbol.symbol != node.symbol {
                    return Err(PathResolutionError::IncorrectPoppedSymbol);
                }
            }
            Self::Root(_) => {}
            Self::Scope(_) => {}
        };
        Ok(())
    }
}

//-------------------------------------------------------------------------------------------------
// Extending partial paths with partial paths

impl PartialPath {
    /// Attempts to append a partial path to this one.  If the postcondition of the “left” partial path
    /// is not compatible with the precondition of the “right” path, we return an error describing why.
    ///
    /// If the left- and right-hand partial paths have any symbol or scope stack variables in
    /// common, then we ensure that the variables bind to the same values on both sides.  It's your
    /// responsibility to update the two partial paths so that they have no variables in common, if
    /// that's needed for your use case.
    #[cfg_attr(not(feature = "copious-debugging"), allow(unused_variables))]
    pub fn concatenate(
        &mut self,
        graph: &StackGraph,
        partials: &mut PartialPaths,
        rhs: &PartialPath,
    ) -> Result<(), PathResolutionError> {
        let lhs = self;

        #[cfg_attr(not(feature = "copious-debugging"), allow(unused_mut))]
        let mut join = Self::compute_join(graph, partials, lhs, rhs)?;
        #[cfg(feature = "copious-debugging")]
        {
            let unified_symbol_stack = join
                .unified_symbol_stack
                .display(graph, partials)
                .to_string();
            let unified_scope_stack = join
                .unified_scope_stack
                .display(graph, partials)
                .to_string();
            let symbol_bindings = join.symbol_bindings.display(graph, partials).to_string();
            let scope_bindings = join.scope_bindings.display(graph, partials).to_string();
            copious_debugging!(
                "       via <{}> ({}) {} {}",
                unified_symbol_stack,
                unified_scope_stack,
                symbol_bindings,
                scope_bindings,
            );
        }

        lhs.symbol_stack_precondition = lhs.symbol_stack_precondition.apply_partial_bindings(
            partials,
            &join.symbol_bindings,
            &join.scope_bindings,
        )?;
        lhs.symbol_stack_postcondition = rhs.symbol_stack_postcondition.apply_partial_bindings(
            partials,
            &join.symbol_bindings,
            &join.scope_bindings,
        )?;

        lhs.scope_stack_precondition = lhs
            .scope_stack_precondition
            .apply_partial_bindings(partials, &join.scope_bindings)?;
        lhs.scope_stack_postcondition = rhs
            .scope_stack_postcondition
            .apply_partial_bindings(partials, &join.scope_bindings)?;

        let mut edges = rhs.edges;
        while let Some(edge) = edges.pop_front(partials) {
            lhs.edges.push_back(partials, edge);
        }
        lhs.end_node = rhs.end_node;

        lhs.resolve_from_postcondition(graph, partials)?;

        Ok(())
    }

    /// Compute the bindings to join to partial paths. It is the caller's responsibility
    /// to ensure non-overlapping variables, if that is required.
    fn compute_join(
        graph: &StackGraph,
        partials: &mut PartialPaths,
        lhs: &PartialPath,
        rhs: &PartialPath,
    ) -> Result<Join, PathResolutionError> {
        if lhs.end_node != rhs.start_node {
            return Err(PathResolutionError::IncorrectSourceNode);
        }

        // Ensure the right post- and left precondition are half-open, so we can unify them.
        let mut lhs_symbol_stack_postcondition = lhs.symbol_stack_postcondition;
        let mut lhs_scope_stack_postcondition = lhs.scope_stack_postcondition;
        let mut rhs_symbol_stack_precondition = rhs.symbol_stack_precondition;
        let mut rhs_scope_stack_precondition = rhs.scope_stack_precondition;
        graph[lhs.end_node]
            .halfopen_closed_partial_postcondition(
                partials,
                &mut lhs_symbol_stack_postcondition,
                &mut lhs_scope_stack_postcondition,
            )
            .unwrap_or_else(|e| {
                panic!(
                    "failed to halfopen postcondition of {}: {:?}",
                    lhs.display(graph, partials),
                    e
                );
            });
        graph[rhs.start_node]
            .halfopen_closed_partial_precondition(
                partials,
                &mut rhs_symbol_stack_precondition,
                &mut rhs_scope_stack_precondition,
            )
            .unwrap_or_else(|e| {
                panic!(
                    "failed to halfopen postcondition of {}: {:?}",
                    rhs.display(graph, partials),
                    e
                );
            });

        let mut symbol_bindings = PartialSymbolStackBindings::new();
        let mut scope_bindings = PartialScopeStackBindings::new();
        let unified_symbol_stack = lhs_symbol_stack_postcondition.unify(
            partials,
            rhs_symbol_stack_precondition,
            &mut symbol_bindings,
            &mut scope_bindings,
        )?;
        let unified_scope_stack = lhs_scope_stack_postcondition.unify(
            partials,
            rhs_scope_stack_precondition,
            &mut scope_bindings,
        )?;

        Ok(Join {
            unified_symbol_stack,
            unified_scope_stack,
            symbol_bindings,
            scope_bindings,
        })
    }
}

struct Join {
    #[cfg_attr(not(feature = "copious-debugging"), allow(dead_code))]
    pub unified_symbol_stack: PartialSymbolStack,
    #[cfg_attr(not(feature = "copious-debugging"), allow(dead_code))]
    pub unified_scope_stack: PartialScopeStack,
    pub symbol_bindings: PartialSymbolStackBindings,
    pub scope_bindings: PartialScopeStackBindings,
}

//-------------------------------------------------------------------------------------------------
// Partial path resolution state

/// Manages the state of a collection of partial paths built up as part of the partial-path-finding
/// algorithm or path-stitching algorithm.
pub struct PartialPaths {
    pub(crate) partial_symbol_stacks: DequeArena<PartialScopedSymbol>,
    pub(crate) partial_scope_stacks: DequeArena<Handle<Node>>,
    pub(crate) partial_path_edges: DequeArena<PartialPathEdge>,
}

impl PartialPaths {
    pub fn new() -> PartialPaths {
        PartialPaths {
            partial_symbol_stacks: Deque::new_arena(),
            partial_scope_stacks: Deque::new_arena(),
            partial_path_edges: Deque::new_arena(),
        }
    }

    #[cfg_attr(not(feature = "storage"), allow(dead_code))]
    pub(crate) fn clear(&mut self) {
        self.partial_symbol_stacks.clear();
        self.partial_scope_stacks.clear();
        self.partial_path_edges.clear();
    }
}