seq-compiler 3.0.6

Compiler for the Seq programming language
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
//! Resource Leak Detection (Phase 2a)
//!
//! Data flow analysis to detect resource leaks within single word definitions.
//! Tracks resources (weave handles, channels) through stack operations and
//! control flow to ensure proper cleanup.
//!
//! # Architecture
//!
//! 1. **Resource Tagging**: Values from resource-creating words are tagged
//!    with their creation location.
//!
//! 2. **Stack Simulation**: Abstract interpretation tracks tagged values
//!    through stack operations (dup, swap, drop, etc.).
//!
//! 3. **Control Flow**: If/else and match branches must handle resources
//!    consistently - either all consume or all preserve.
//!
//! 4. **Escape Analysis**: Resources returned from a word are the caller's
//!    responsibility - no warning emitted.
//!
//! # Known Limitations
//!
//! - **`strand.resume` completion not tracked**: When `strand.resume` returns
//!   false, the weave completed and handle is consumed. We can't determine this
//!   statically, so we assume the handle remains active. Use pattern-based lint
//!   rules to catch unchecked resume results.
//!
//! - **Unknown word effects**: User-defined words and FFI calls have unknown
//!   stack effects. We conservatively leave the stack unchanged, which may
//!   cause false negatives if those words consume or create resources.
//!
//! - **Cross-word analysis is basic**: Resources returned from user-defined
//!   words are tracked via `ProgramResourceAnalyzer`, but external/FFI words
//!   with unknown effects are treated conservatively (no stack change assumed).

use crate::ast::{MatchArm, Program, Span, Statement, WordDef};
use crate::lint::{LintDiagnostic, Severity};
use std::collections::HashMap;
use std::path::Path;

/// Identifies a resource type for tracking
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) enum ResourceKind {
    /// Weave handle from `strand.weave`
    WeaveHandle,
    /// Channel from `chan.make`
    Channel,
}

impl ResourceKind {
    fn name(&self) -> &'static str {
        match self {
            ResourceKind::WeaveHandle => "WeaveHandle",
            ResourceKind::Channel => "Channel",
        }
    }

    fn cleanup_suggestion(&self) -> &'static str {
        match self {
            ResourceKind::WeaveHandle => "use `strand.weave-cancel` or resume to completion",
            ResourceKind::Channel => "use `chan.close` when done",
        }
    }
}

/// A tracked resource with its origin
#[derive(Debug, Clone)]
pub(crate) struct TrackedResource {
    /// What kind of resource this is
    pub kind: ResourceKind,
    /// Unique ID for this resource instance
    pub id: usize,
    /// Line where the resource was created (0-indexed)
    pub created_line: usize,
    /// The word that created this resource
    pub created_by: String,
}

/// A value on the abstract stack - either a resource or unknown
#[derive(Debug, Clone)]
pub(crate) enum StackValue {
    /// A tracked resource
    Resource(TrackedResource),
    /// An unknown value (literal, result of non-resource operation)
    Unknown,
}

/// State of the abstract stack during analysis
#[derive(Debug, Clone)]
pub(crate) struct StackState {
    /// The stack contents (top is last element)
    stack: Vec<StackValue>,
    /// Resources that have been properly consumed
    consumed: Vec<TrackedResource>,
    /// Next resource ID to assign
    next_id: usize,
}

impl Default for StackState {
    fn default() -> Self {
        Self::new()
    }
}

impl StackState {
    pub fn new() -> Self {
        StackState {
            stack: Vec::new(),
            consumed: Vec::new(),
            next_id: 0,
        }
    }

    /// Push an unknown value onto the stack
    pub fn push_unknown(&mut self) {
        self.stack.push(StackValue::Unknown);
    }

    /// Push a new tracked resource onto the stack
    pub fn push_resource(&mut self, kind: ResourceKind, line: usize, word: &str) {
        let resource = TrackedResource {
            kind,
            id: self.next_id,
            created_line: line,
            created_by: word.to_string(),
        };
        self.next_id += 1;
        self.stack.push(StackValue::Resource(resource));
    }

    /// Pop a value from the stack
    pub fn pop(&mut self) -> Option<StackValue> {
        self.stack.pop()
    }

    /// Peek at the top value without removing it
    pub fn peek(&self) -> Option<&StackValue> {
        self.stack.last()
    }

    /// Get stack depth
    pub fn depth(&self) -> usize {
        self.stack.len()
    }

    /// Mark a resource as consumed (properly cleaned up)
    pub fn consume_resource(&mut self, resource: TrackedResource) {
        self.consumed.push(resource);
    }

    /// Get all resources still on the stack (potential leaks)
    pub fn remaining_resources(&self) -> Vec<&TrackedResource> {
        self.stack
            .iter()
            .filter_map(|v| match v {
                StackValue::Resource(r) => Some(r),
                StackValue::Unknown => None,
            })
            .collect()
    }

    /// Merge two stack states (for branch unification)
    /// Returns resources that are leaked in one branch but not the other
    pub fn merge(&self, other: &StackState) -> BranchMergeResult {
        let self_resources: HashMap<usize, &TrackedResource> = self
            .stack
            .iter()
            .filter_map(|v| match v {
                StackValue::Resource(r) => Some((r.id, r)),
                StackValue::Unknown => None,
            })
            .collect();

        let other_resources: HashMap<usize, &TrackedResource> = other
            .stack
            .iter()
            .filter_map(|v| match v {
                StackValue::Resource(r) => Some((r.id, r)),
                StackValue::Unknown => None,
            })
            .collect();

        let self_consumed: std::collections::HashSet<usize> =
            self.consumed.iter().map(|r| r.id).collect();
        let other_consumed: std::collections::HashSet<usize> =
            other.consumed.iter().map(|r| r.id).collect();

        let mut inconsistent = Vec::new();

        // Find resources consumed in one branch but not the other
        for (id, resource) in &self_resources {
            if other_consumed.contains(id) && !self_consumed.contains(id) {
                // Consumed in 'other' branch, still on stack in 'self'
                inconsistent.push(InconsistentResource {
                    resource: (*resource).clone(),
                    consumed_in_else: true,
                });
            }
        }

        for (id, resource) in &other_resources {
            if self_consumed.contains(id) && !other_consumed.contains(id) {
                // Consumed in 'self' branch, still on stack in 'other'
                inconsistent.push(InconsistentResource {
                    resource: (*resource).clone(),
                    consumed_in_else: false,
                });
            }
        }

        BranchMergeResult { inconsistent }
    }

    /// Compute a lattice join of two stack states for continuation after branches.
    ///
    /// The join is conservative:
    /// - Resources present in EITHER branch are tracked (we don't know which path was taken)
    /// - Resources are only marked consumed if consumed in BOTH branches
    /// - The next_id is taken from the max of both states
    ///
    /// This ensures we don't miss potential leaks from either branch.
    pub fn join(&self, other: &StackState) -> StackState {
        // Collect resource IDs consumed in each branch
        let other_consumed: std::collections::HashSet<usize> =
            other.consumed.iter().map(|r| r.id).collect();

        // Resources consumed in BOTH branches are definitely consumed
        let definitely_consumed: Vec<TrackedResource> = self
            .consumed
            .iter()
            .filter(|r| other_consumed.contains(&r.id))
            .cloned()
            .collect();

        // For the stack, we need to be careful. After if/else, stacks should
        // have the same depth (Seq requires balanced stack effects in branches).
        // We take the union of resources - if a resource appears in either
        // branch's stack, it should be tracked.
        //
        // Since we can't know which branch was taken, we use the then-branch
        // stack structure but ensure any resource from either branch is present.
        let mut joined_stack = self.stack.clone();

        // Collect resources from other branch that might not be in self
        let other_resources: HashMap<usize, TrackedResource> = other
            .stack
            .iter()
            .filter_map(|v| match v {
                StackValue::Resource(r) => Some((r.id, r.clone())),
                StackValue::Unknown => None,
            })
            .collect();

        // For each position, if other has a resource that self doesn't, use other's
        for (i, val) in joined_stack.iter_mut().enumerate() {
            if matches!(val, StackValue::Unknown)
                && i < other.stack.len()
                && let StackValue::Resource(r) = &other.stack[i]
            {
                *val = StackValue::Resource(r.clone());
            }
        }

        // Also check if other branch has resources we should track
        // (in case stacks have different structures due to analysis imprecision)
        let self_resource_ids: std::collections::HashSet<usize> = joined_stack
            .iter()
            .filter_map(|v| match v {
                StackValue::Resource(r) => Some(r.id),
                StackValue::Unknown => None,
            })
            .collect();

        for (id, resource) in other_resources {
            if !self_resource_ids.contains(&id) && !definitely_consumed.iter().any(|r| r.id == id) {
                // Resource from other branch not in our stack - add it
                // This handles cases where branches have different stack shapes
                joined_stack.push(StackValue::Resource(resource));
            }
        }

        StackState {
            stack: joined_stack,
            consumed: definitely_consumed,
            next_id: self.next_id.max(other.next_id),
        }
    }
}

/// Result of merging two branch states
#[derive(Debug)]
pub(crate) struct BranchMergeResult {
    /// Resources handled inconsistently between branches
    pub inconsistent: Vec<InconsistentResource>,
}

/// A resource handled differently in different branches
#[derive(Debug)]
pub(crate) struct InconsistentResource {
    pub resource: TrackedResource,
    /// True if consumed in else branch but not then branch
    pub consumed_in_else: bool,
}

// ============================================================================
// Cross-Word Analysis (Phase 2b)
// ============================================================================

/// Information about a word's resource behavior
#[derive(Debug, Clone, Default)]
pub(crate) struct WordResourceInfo {
    /// Resource kinds this word returns (resources on stack at word end)
    pub returns: Vec<ResourceKind>,
}

/// Program-wide resource analyzer for cross-word analysis
///
/// This analyzer performs two passes:
/// 1. Collect resource information about each word (what resources it returns)
/// 2. Analyze each word with knowledge of callee behavior
pub struct ProgramResourceAnalyzer {
    /// Per-word resource information (populated in first pass)
    word_info: HashMap<String, WordResourceInfo>,
    /// File being analyzed
    file: std::path::PathBuf,
    /// Diagnostics collected during analysis
    diagnostics: Vec<LintDiagnostic>,
}

impl ProgramResourceAnalyzer {
    pub fn new(file: &Path) -> Self {
        ProgramResourceAnalyzer {
            word_info: HashMap::new(),
            file: file.to_path_buf(),
            diagnostics: Vec::new(),
        }
    }

    /// Analyze an entire program for resource leaks with cross-word tracking
    pub fn analyze_program(&mut self, program: &Program) -> Vec<LintDiagnostic> {
        self.diagnostics.clear();
        self.word_info.clear();

        // Pass 1: Collect resource information about each word
        for word in &program.words {
            let info = self.collect_word_info(word);
            self.word_info.insert(word.name.clone(), info);
        }

        // Pass 2: Analyze each word with cross-word context
        for word in &program.words {
            self.analyze_word_with_context(word);
        }

        std::mem::take(&mut self.diagnostics)
    }

    /// First pass: Determine what resources a word returns
    fn collect_word_info(&self, word: &WordDef) -> WordResourceInfo {
        let mut state = StackState::new();

        // Simple analysis without emitting diagnostics
        self.simulate_statements(&word.body, &mut state);

        // Collect resource kinds remaining on stack (these are "returned")
        let returns: Vec<ResourceKind> = state
            .remaining_resources()
            .into_iter()
            .map(|r| r.kind)
            .collect();

        WordResourceInfo { returns }
    }

    /// Simulate statements to track resources (no diagnostics)
    fn simulate_statements(&self, statements: &[Statement], state: &mut StackState) {
        for stmt in statements {
            self.simulate_statement(stmt, state);
        }
    }

    /// Simulate a single statement (simplified, no diagnostics)
    fn simulate_statement(&self, stmt: &Statement, state: &mut StackState) {
        match stmt {
            Statement::IntLiteral(_)
            | Statement::FloatLiteral(_)
            | Statement::BoolLiteral(_)
            | Statement::StringLiteral(_)
            | Statement::Symbol(_) => {
                state.push_unknown();
            }

            Statement::WordCall { name, span } => {
                self.simulate_word_call(name, span.as_ref(), state);
            }

            Statement::Quotation { .. } => {
                state.push_unknown();
            }

            Statement::If {
                then_branch,
                else_branch,
                span: _,
            } => {
                state.pop(); // condition
                let mut then_state = state.clone();
                let mut else_state = state.clone();
                self.simulate_statements(then_branch, &mut then_state);
                if let Some(else_stmts) = else_branch {
                    self.simulate_statements(else_stmts, &mut else_state);
                }
                *state = then_state.join(&else_state);
            }

            Statement::Match { arms, span: _ } => {
                state.pop();
                let mut arm_states: Vec<StackState> = Vec::new();
                for arm in arms {
                    let mut arm_state = state.clone();
                    self.simulate_statements(&arm.body, &mut arm_state);
                    arm_states.push(arm_state);
                }
                if let Some(joined) = arm_states.into_iter().reduce(|acc, s| acc.join(&s)) {
                    *state = joined;
                }
            }
        }
    }

    /// Simulate common word operations shared between first and second pass.
    ///
    /// Returns `true` if the word was handled, `false` if the caller should
    /// handle it (for pass-specific operations).
    ///
    /// The `on_resource_dropped` callback is invoked when a resource is dropped
    /// without being consumed. The second pass uses this to emit warnings.
    fn simulate_word_common<F>(
        name: &str,
        span: Option<&Span>,
        state: &mut StackState,
        word_info: &HashMap<String, WordResourceInfo>,
        mut on_resource_dropped: F,
    ) -> bool
    where
        F: FnMut(&TrackedResource),
    {
        let line = span.map(|s| s.line).unwrap_or(0);

        match name {
            // Resource-creating builtins
            "strand.weave" => {
                state.pop();
                state.push_resource(ResourceKind::WeaveHandle, line, name);
            }
            "chan.make" => {
                state.push_resource(ResourceKind::Channel, line, name);
            }

            // Resource-consuming builtins
            "strand.weave-cancel" => {
                if let Some(StackValue::Resource(r)) = state.pop()
                    && r.kind == ResourceKind::WeaveHandle
                {
                    state.consume_resource(r);
                }
            }
            "chan.close" => {
                if let Some(StackValue::Resource(r)) = state.pop()
                    && r.kind == ResourceKind::Channel
                {
                    state.consume_resource(r);
                }
            }

            // Stack operations
            "drop" => {
                let dropped = state.pop();
                if let Some(StackValue::Resource(r)) = dropped {
                    // Check if already consumed (e.g., via strand.spawn)
                    let already_consumed = state.consumed.iter().any(|c| c.id == r.id);
                    if !already_consumed {
                        on_resource_dropped(&r);
                    }
                }
            }
            "dup" => {
                // Only duplicate if there's something on the stack
                // Don't push unknown on empty - maintains original first-pass behavior
                if let Some(top) = state.peek().cloned() {
                    state.stack.push(top);
                }
            }
            "swap" => {
                let a = state.pop();
                let b = state.pop();
                if let Some(av) = a {
                    state.stack.push(av);
                }
                if let Some(bv) = b {
                    state.stack.push(bv);
                }
            }
            "over" => {
                // ( ..a x y -- ..a x y x )
                if state.depth() >= 2 {
                    let second = state.stack[state.depth() - 2].clone();
                    state.stack.push(second);
                }
            }
            "rot" => {
                // ( ..a x y z -- ..a y z x )
                let c = state.pop();
                let b = state.pop();
                let a = state.pop();
                if let Some(bv) = b {
                    state.stack.push(bv);
                }
                if let Some(cv) = c {
                    state.stack.push(cv);
                }
                if let Some(av) = a {
                    state.stack.push(av);
                }
            }
            "nip" => {
                // ( ..a x y -- ..a y ) - drops x, which may be a resource
                let b = state.pop();
                let a = state.pop();
                if let Some(StackValue::Resource(r)) = a {
                    let already_consumed = state.consumed.iter().any(|c| c.id == r.id);
                    if !already_consumed {
                        on_resource_dropped(&r);
                    }
                }
                if let Some(bv) = b {
                    state.stack.push(bv);
                }
            }
            "tuck" => {
                // ( ..a x y -- ..a y x y )
                let b = state.pop();
                let a = state.pop();
                if let Some(bv) = b.clone() {
                    state.stack.push(bv);
                }
                if let Some(av) = a {
                    state.stack.push(av);
                }
                if let Some(bv) = b {
                    state.stack.push(bv);
                }
            }

            // strand.spawn transfers resources
            "strand.spawn" => {
                state.pop();
                let resources: Vec<TrackedResource> = state
                    .stack
                    .iter()
                    .filter_map(|v| match v {
                        StackValue::Resource(r) => Some(r.clone()),
                        StackValue::Unknown => None,
                    })
                    .collect();
                for r in resources {
                    state.consume_resource(r);
                }
                state.push_unknown();
            }

            // Map operations that store values safely
            "map.set" => {
                // ( map key value -- map' ) - value is stored in map
                let value = state.pop();
                state.pop(); // key
                state.pop(); // map
                // Value is now safely stored in the map - consume if resource
                if let Some(StackValue::Resource(r)) = value {
                    state.consume_resource(r);
                }
                state.push_unknown(); // map'
            }

            // List operations that store values safely
            "list.push" | "list.prepend" => {
                // ( list value -- list' ) - value is stored in list
                let value = state.pop();
                state.pop(); // list
                if let Some(StackValue::Resource(r)) = value {
                    state.consume_resource(r);
                }
                state.push_unknown(); // list'
            }

            // User-defined words - check if we have info about them
            _ => {
                if let Some(info) = word_info.get(name) {
                    // Push resources that this word returns
                    for kind in &info.returns {
                        state.push_resource(*kind, line, name);
                    }
                    return true;
                }
                // Not handled - caller should handle pass-specific operations
                return false;
            }
        }
        true
    }

    /// Simulate a word call (for first pass)
    fn simulate_word_call(&self, name: &str, span: Option<&Span>, state: &mut StackState) {
        // First pass uses shared logic with no-op callback for dropped resources
        Self::simulate_word_common(name, span, state, &self.word_info, |_| {});
    }

    /// Second pass: Analyze a word with full cross-word context
    fn analyze_word_with_context(&mut self, word: &WordDef) {
        let mut state = StackState::new();

        self.analyze_statements_with_context(&word.body, &mut state, word);

        // Resources on stack at end are returned - no warning (escape analysis)
    }

    /// Analyze statements with diagnostics and cross-word tracking
    fn analyze_statements_with_context(
        &mut self,
        statements: &[Statement],
        state: &mut StackState,
        word: &WordDef,
    ) {
        for stmt in statements {
            self.analyze_statement_with_context(stmt, state, word);
        }
    }

    /// Analyze a single statement with cross-word context
    fn analyze_statement_with_context(
        &mut self,
        stmt: &Statement,
        state: &mut StackState,
        word: &WordDef,
    ) {
        match stmt {
            Statement::IntLiteral(_)
            | Statement::FloatLiteral(_)
            | Statement::BoolLiteral(_)
            | Statement::StringLiteral(_)
            | Statement::Symbol(_) => {
                state.push_unknown();
            }

            Statement::WordCall { name, span } => {
                self.analyze_word_call_with_context(name, span.as_ref(), state, word);
            }

            Statement::Quotation { .. } => {
                state.push_unknown();
            }

            Statement::If {
                then_branch,
                else_branch,
                span: _,
            } => {
                state.pop();
                let mut then_state = state.clone();
                let mut else_state = state.clone();

                self.analyze_statements_with_context(then_branch, &mut then_state, word);
                if let Some(else_stmts) = else_branch {
                    self.analyze_statements_with_context(else_stmts, &mut else_state, word);
                }

                // Check for inconsistent handling
                let merge_result = then_state.merge(&else_state);
                for inconsistent in merge_result.inconsistent {
                    self.emit_branch_inconsistency_warning(&inconsistent, word);
                }

                *state = then_state.join(&else_state);
            }

            Statement::Match { arms, span: _ } => {
                state.pop();
                let mut arm_states: Vec<StackState> = Vec::new();

                for arm in arms {
                    let mut arm_state = state.clone();
                    self.analyze_statements_with_context(&arm.body, &mut arm_state, word);
                    arm_states.push(arm_state);
                }

                // Check consistency
                if arm_states.len() >= 2 {
                    let first = &arm_states[0];
                    for other in &arm_states[1..] {
                        let merge_result = first.merge(other);
                        for inconsistent in merge_result.inconsistent {
                            self.emit_branch_inconsistency_warning(&inconsistent, word);
                        }
                    }
                }

                if let Some(joined) = arm_states.into_iter().reduce(|acc, s| acc.join(&s)) {
                    *state = joined;
                }
            }
        }
    }

    /// Analyze a word call with cross-word tracking
    fn analyze_word_call_with_context(
        &mut self,
        name: &str,
        span: Option<&Span>,
        state: &mut StackState,
        word: &WordDef,
    ) {
        // Collect dropped resources to emit warnings after shared simulation
        let mut dropped_resources: Vec<TrackedResource> = Vec::new();

        // Try shared logic first
        let handled = Self::simulate_word_common(name, span, state, &self.word_info, |r| {
            dropped_resources.push(r.clone())
        });

        // Emit warnings for any resources dropped without cleanup
        for r in dropped_resources {
            self.emit_drop_warning(&r, span, word);
        }

        if handled {
            return;
        }

        // Handle operations unique to the second pass
        match name {
            // strand.resume handling - can't be shared because it has complex stack behavior
            "strand.resume" => {
                let value = state.pop();
                let handle = state.pop();
                if let Some(h) = handle {
                    state.stack.push(h);
                } else {
                    state.push_unknown();
                }
                if let Some(v) = value {
                    state.stack.push(v);
                } else {
                    state.push_unknown();
                }
                state.push_unknown();
            }

            "2dup" => {
                if state.depth() >= 2 {
                    let b = state.stack[state.depth() - 1].clone();
                    let a = state.stack[state.depth() - 2].clone();
                    state.stack.push(a);
                    state.stack.push(b);
                } else {
                    state.push_unknown();
                    state.push_unknown();
                }
            }

            "3drop" => {
                for _ in 0..3 {
                    if let Some(StackValue::Resource(r)) = state.pop() {
                        let already_consumed = state.consumed.iter().any(|c| c.id == r.id);
                        if !already_consumed {
                            self.emit_drop_warning(&r, span, word);
                        }
                    }
                }
            }

            "pick" | "roll" => {
                state.pop();
                state.push_unknown();
            }

            "chan.send" | "chan.receive" => {
                state.pop();
                state.pop();
                state.push_unknown();
                state.push_unknown();
            }

            // Unknown words: leave stack unchanged (may cause false negatives)
            _ => {}
        }
    }

    fn emit_drop_warning(
        &mut self,
        resource: &TrackedResource,
        span: Option<&Span>,
        word: &WordDef,
    ) {
        let line = span
            .map(|s| s.line)
            .unwrap_or_else(|| word.source.as_ref().map(|s| s.start_line).unwrap_or(0));
        let column = span.map(|s| s.column);

        self.diagnostics.push(LintDiagnostic {
            id: format!("resource-leak-{}", resource.kind.name().to_lowercase()),
            message: format!(
                "{} from `{}` (line {}) dropped without cleanup - {}",
                resource.kind.name(),
                resource.created_by,
                resource.created_line + 1,
                resource.kind.cleanup_suggestion()
            ),
            severity: Severity::Warning,
            replacement: String::new(),
            file: self.file.clone(),
            line,
            end_line: None,
            start_column: column,
            end_column: column.map(|c| c + 4),
            word_name: word.name.clone(),
            start_index: 0,
            end_index: 0,
        });
    }

    fn emit_branch_inconsistency_warning(
        &mut self,
        inconsistent: &InconsistentResource,
        word: &WordDef,
    ) {
        let line = word.source.as_ref().map(|s| s.start_line).unwrap_or(0);
        let branch = if inconsistent.consumed_in_else {
            "else"
        } else {
            "then"
        };

        self.diagnostics.push(LintDiagnostic {
            id: "resource-branch-inconsistent".to_string(),
            message: format!(
                "{} from `{}` (line {}) is consumed in {} branch but not the other - all branches must handle resources consistently",
                inconsistent.resource.kind.name(),
                inconsistent.resource.created_by,
                inconsistent.resource.created_line + 1,
                branch
            ),
            severity: Severity::Warning,
            replacement: String::new(),
            file: self.file.clone(),
            line,
            end_line: None,
            start_column: None,
            end_column: None,
            word_name: word.name.clone(),
            start_index: 0,
            end_index: 0,
        });
    }
}

/// The resource leak analyzer (single-word analysis)
pub struct ResourceAnalyzer {
    /// Diagnostics collected during analysis
    diagnostics: Vec<LintDiagnostic>,
    /// File being analyzed
    file: std::path::PathBuf,
}

impl ResourceAnalyzer {
    pub fn new(file: &Path) -> Self {
        ResourceAnalyzer {
            diagnostics: Vec::new(),
            file: file.to_path_buf(),
        }
    }

    /// Analyze a word definition for resource leaks
    pub fn analyze_word(&mut self, word: &WordDef) -> Vec<LintDiagnostic> {
        self.diagnostics.clear();

        let mut state = StackState::new();

        // Analyze the word body
        self.analyze_statements(&word.body, &mut state, word);

        // Check for leaked resources at end of word
        // Note: Resources still on stack at word end could be:
        // 1. Intentionally returned (escape) - caller's responsibility
        // 2. Leaked - forgot to clean up
        //
        // For Phase 2a, we apply escape analysis: if a resource is still
        // on the stack at word end, it's being returned to the caller.
        // This is valid - the caller becomes responsible for cleanup.
        // We only warn about resources that are explicitly dropped without
        // cleanup, or handled inconsistently across branches.
        //
        // Phase 2b could add cross-word analysis to track if callers
        // properly handle returned resources.
        let _ = state.remaining_resources(); // Intentional: escape = no warning

        std::mem::take(&mut self.diagnostics)
    }

    /// Analyze a sequence of statements
    fn analyze_statements(
        &mut self,
        statements: &[Statement],
        state: &mut StackState,
        word: &WordDef,
    ) {
        for stmt in statements {
            self.analyze_statement(stmt, state, word);
        }
    }

    /// Analyze a single statement
    fn analyze_statement(&mut self, stmt: &Statement, state: &mut StackState, word: &WordDef) {
        match stmt {
            Statement::IntLiteral(_)
            | Statement::FloatLiteral(_)
            | Statement::BoolLiteral(_)
            | Statement::StringLiteral(_)
            | Statement::Symbol(_) => {
                state.push_unknown();
            }

            Statement::WordCall { name, span } => {
                self.analyze_word_call(name, span.as_ref(), state, word);
            }

            Statement::Quotation { body, .. } => {
                // Quotations capture the current stack conceptually but don't
                // execute immediately. For now, just push an unknown value
                // (the quotation itself). We could analyze the body when
                // we see `call`, but that's Phase 2b.
                let _ = body; // Acknowledge we're not analyzing the body yet
                state.push_unknown();
            }

            Statement::If {
                then_branch,
                else_branch,
                span: _,
            } => {
                self.analyze_if(then_branch, else_branch.as_ref(), state, word);
            }

            Statement::Match { arms, span: _ } => {
                self.analyze_match(arms, state, word);
            }
        }
    }

    /// Analyze a word call
    fn analyze_word_call(
        &mut self,
        name: &str,
        span: Option<&Span>,
        state: &mut StackState,
        word: &WordDef,
    ) {
        let line = span.map(|s| s.line).unwrap_or(0);

        match name {
            // Resource-creating words
            "strand.weave" => {
                // Pops quotation, pushes WeaveHandle
                state.pop(); // quotation
                state.push_resource(ResourceKind::WeaveHandle, line, name);
            }

            "chan.make" => {
                // Pushes a new channel
                state.push_resource(ResourceKind::Channel, line, name);
            }

            // Resource-consuming words
            "strand.weave-cancel" => {
                // Pops and consumes WeaveHandle
                if let Some(StackValue::Resource(r)) = state.pop()
                    && r.kind == ResourceKind::WeaveHandle
                {
                    state.consume_resource(r);
                }
            }

            "chan.close" => {
                // Pops and consumes Channel
                if let Some(StackValue::Resource(r)) = state.pop()
                    && r.kind == ResourceKind::Channel
                {
                    state.consume_resource(r);
                }
            }

            // strand.resume is special - it returns (handle value bool)
            // If bool is false, the weave completed and handle is consumed
            // We can't know statically, so we just track that the handle
            // is still in play (on the stack after resume)
            "strand.resume" => {
                // Pops (handle value), pushes (handle value bool)
                let value = state.pop(); // value to send
                let handle = state.pop(); // handle

                // Push them back plus the bool result
                if let Some(h) = handle {
                    state.stack.push(h);
                } else {
                    state.push_unknown();
                }
                if let Some(v) = value {
                    state.stack.push(v);
                } else {
                    state.push_unknown();
                }
                state.push_unknown(); // bool result
            }

            // Stack operations
            "drop" => {
                let dropped = state.pop();
                // If we dropped a resource without consuming it properly, that's a leak
                // But check if it was already consumed (e.g., transferred via strand.spawn)
                if let Some(StackValue::Resource(r)) = dropped {
                    let already_consumed = state.consumed.iter().any(|c| c.id == r.id);
                    if !already_consumed {
                        self.emit_drop_warning(&r, span, word);
                    }
                }
            }

            "dup" => {
                if let Some(top) = state.peek().cloned() {
                    state.stack.push(top);
                } else {
                    state.push_unknown();
                }
            }

            "swap" => {
                let a = state.pop();
                let b = state.pop();
                if let Some(av) = a {
                    state.stack.push(av);
                }
                if let Some(bv) = b {
                    state.stack.push(bv);
                }
            }

            "over" => {
                // ( a b -- a b a ) - copy second element to top
                if state.depth() >= 2 {
                    let second = state.stack[state.depth() - 2].clone();
                    state.stack.push(second);
                } else {
                    state.push_unknown();
                }
            }

            "rot" => {
                // ( a b c -- b c a )
                let c = state.pop();
                let b = state.pop();
                let a = state.pop();
                if let Some(bv) = b {
                    state.stack.push(bv);
                }
                if let Some(cv) = c {
                    state.stack.push(cv);
                }
                if let Some(av) = a {
                    state.stack.push(av);
                }
            }

            "nip" => {
                // ( a b -- b ) - drop second
                let b = state.pop();
                let a = state.pop();
                if let Some(StackValue::Resource(r)) = a {
                    let already_consumed = state.consumed.iter().any(|c| c.id == r.id);
                    if !already_consumed {
                        self.emit_drop_warning(&r, span, word);
                    }
                }
                if let Some(bv) = b {
                    state.stack.push(bv);
                }
            }

            "tuck" => {
                // ( a b -- b a b )
                let b = state.pop();
                let a = state.pop();
                if let Some(bv) = b.clone() {
                    state.stack.push(bv);
                }
                if let Some(av) = a {
                    state.stack.push(av);
                }
                if let Some(bv) = b {
                    state.stack.push(bv);
                }
            }

            "2dup" => {
                // ( a b -- a b a b )
                if state.depth() >= 2 {
                    let b = state.stack[state.depth() - 1].clone();
                    let a = state.stack[state.depth() - 2].clone();
                    state.stack.push(a);
                    state.stack.push(b);
                } else {
                    state.push_unknown();
                    state.push_unknown();
                }
            }

            "3drop" => {
                for _ in 0..3 {
                    if let Some(StackValue::Resource(r)) = state.pop() {
                        let already_consumed = state.consumed.iter().any(|c| c.id == r.id);
                        if !already_consumed {
                            self.emit_drop_warning(&r, span, word);
                        }
                    }
                }
            }

            "pick" => {
                // ( ... n -- ... value_at_n )
                // We can't know n statically, so just push unknown
                state.pop(); // pop n
                state.push_unknown();
            }

            "roll" => {
                // Similar to pick but also removes the item
                state.pop(); // pop n
                state.push_unknown();
            }

            // Channel operations that don't consume
            "chan.send" | "chan.receive" => {
                // These use the channel but don't consume it
                // chan.send: ( chan value -- bool )
                // chan.receive: ( chan -- value bool )
                state.pop();
                state.pop();
                state.push_unknown();
                state.push_unknown();
            }

            // strand.spawn clones the stack to the child strand
            // Resources on the stack are transferred to child's responsibility
            "strand.spawn" => {
                // Pops quotation, pushes strand-id
                // All resources currently on stack are now shared with child
                // Mark them as consumed since child takes responsibility
                state.pop(); // quotation
                let resources_on_stack: Vec<TrackedResource> = state
                    .stack
                    .iter()
                    .filter_map(|v| match v {
                        StackValue::Resource(r) => Some(r.clone()),
                        StackValue::Unknown => None,
                    })
                    .collect();
                for r in resources_on_stack {
                    state.consume_resource(r);
                }
                state.push_unknown(); // strand-id
            }

            // For any other word, we don't know its stack effect
            // Conservatively, we could assume it consumes/produces unknown values
            // For now, we just leave the stack unchanged (may cause false positives)
            _ => {
                // Unknown word - could be user-defined
                // We'd need type info to know its stack effect
                // For Phase 2a, we'll be conservative and do nothing
            }
        }
    }

    /// Analyze an if/else statement
    fn analyze_if(
        &mut self,
        then_branch: &[Statement],
        else_branch: Option<&Vec<Statement>>,
        state: &mut StackState,
        word: &WordDef,
    ) {
        // Pop the condition
        state.pop();

        // Clone state for each branch
        let mut then_state = state.clone();
        let mut else_state = state.clone();

        // Analyze then branch
        self.analyze_statements(then_branch, &mut then_state, word);

        // Analyze else branch if present
        if let Some(else_stmts) = else_branch {
            self.analyze_statements(else_stmts, &mut else_state, word);
        }

        // Check for inconsistent resource handling between branches
        let merge_result = then_state.merge(&else_state);
        for inconsistent in merge_result.inconsistent {
            self.emit_branch_inconsistency_warning(&inconsistent, word);
        }

        // Compute proper lattice join of both branch states
        // This ensures we track resources from either branch and only
        // consider resources consumed if consumed in BOTH branches
        *state = then_state.join(&else_state);
    }

    /// Analyze a match statement
    fn analyze_match(&mut self, arms: &[MatchArm], state: &mut StackState, word: &WordDef) {
        // Pop the matched value
        state.pop();

        if arms.is_empty() {
            return;
        }

        // Analyze each arm
        let mut arm_states: Vec<StackState> = Vec::new();

        for arm in arms {
            let mut arm_state = state.clone();

            // Match arms may push extracted fields - for now we push unknowns
            // based on the pattern (simplified)
            match &arm.pattern {
                crate::ast::Pattern::Variant(_) => {
                    // Variant match pushes all fields - we don't know how many
                    // so we just continue with current state
                }
                crate::ast::Pattern::VariantWithBindings { bindings, .. } => {
                    // Push unknowns for each binding
                    for _ in bindings {
                        arm_state.push_unknown();
                    }
                }
            }

            self.analyze_statements(&arm.body, &mut arm_state, word);
            arm_states.push(arm_state);
        }

        // Check consistency between all arms
        if arm_states.len() >= 2 {
            let first = &arm_states[0];
            for other in &arm_states[1..] {
                let merge_result = first.merge(other);
                for inconsistent in merge_result.inconsistent {
                    self.emit_branch_inconsistency_warning(&inconsistent, word);
                }
            }
        }

        // Compute proper lattice join of all arm states
        // Resources are only consumed if consumed in ALL arms
        if let Some(first) = arm_states.into_iter().reduce(|acc, s| acc.join(&s)) {
            *state = first;
        }
    }

    /// Emit a warning for a resource dropped without cleanup
    fn emit_drop_warning(
        &mut self,
        resource: &TrackedResource,
        span: Option<&Span>,
        word: &WordDef,
    ) {
        let line = span
            .map(|s| s.line)
            .unwrap_or_else(|| word.source.as_ref().map(|s| s.start_line).unwrap_or(0));
        let column = span.map(|s| s.column);

        self.diagnostics.push(LintDiagnostic {
            id: format!("resource-leak-{}", resource.kind.name().to_lowercase()),
            message: format!(
                "{} created at line {} dropped without cleanup - {}",
                resource.kind.name(),
                resource.created_line + 1,
                resource.kind.cleanup_suggestion()
            ),
            severity: Severity::Warning,
            replacement: String::new(),
            file: self.file.clone(),
            line,
            end_line: None,
            start_column: column,
            end_column: column.map(|c| c + 4), // approximate
            word_name: word.name.clone(),
            start_index: 0,
            end_index: 0,
        });
    }

    /// Emit a warning for inconsistent resource handling between branches
    fn emit_branch_inconsistency_warning(
        &mut self,
        inconsistent: &InconsistentResource,
        word: &WordDef,
    ) {
        let line = word.source.as_ref().map(|s| s.start_line).unwrap_or(0);
        let branch = if inconsistent.consumed_in_else {
            "else"
        } else {
            "then"
        };

        self.diagnostics.push(LintDiagnostic {
            id: "resource-branch-inconsistent".to_string(),
            message: format!(
                "{} created at line {} is consumed in {} branch but not the other - all branches must handle resources consistently",
                inconsistent.resource.kind.name(),
                inconsistent.resource.created_line + 1,
                branch
            ),
            severity: Severity::Warning,
            replacement: String::new(),
            file: self.file.clone(),
            line,
            end_line: None,
            start_column: None,
            end_column: None,
            word_name: word.name.clone(),
            start_index: 0,
            end_index: 0,
        });
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ast::{Statement, WordDef};

    fn make_word_call(name: &str) -> Statement {
        Statement::WordCall {
            name: name.to_string(),
            span: Some(Span::new(0, 0, name.len())),
        }
    }

    #[test]
    fn test_immediate_weave_drop() {
        // : bad ( -- ) [ gen ] strand.weave drop ;
        let word = WordDef {
            name: "bad".to_string(),
            effect: None,
            body: vec![
                Statement::Quotation {
                    span: None,
                    id: 0,
                    body: vec![make_word_call("gen")],
                },
                make_word_call("strand.weave"),
                make_word_call("drop"),
            ],
            source: None,
            allowed_lints: vec![],
        };

        let mut analyzer = ResourceAnalyzer::new(Path::new("test.seq"));
        let diagnostics = analyzer.analyze_word(&word);

        assert_eq!(diagnostics.len(), 1);
        assert!(diagnostics[0].id.contains("weavehandle"));
        assert!(diagnostics[0].message.contains("dropped without cleanup"));
    }

    #[test]
    fn test_weave_properly_cancelled() {
        // : good ( -- ) [ gen ] strand.weave strand.weave-cancel ;
        let word = WordDef {
            name: "good".to_string(),
            effect: None,
            body: vec![
                Statement::Quotation {
                    span: None,
                    id: 0,
                    body: vec![make_word_call("gen")],
                },
                make_word_call("strand.weave"),
                make_word_call("strand.weave-cancel"),
            ],
            source: None,
            allowed_lints: vec![],
        };

        let mut analyzer = ResourceAnalyzer::new(Path::new("test.seq"));
        let diagnostics = analyzer.analyze_word(&word);

        assert!(
            diagnostics.is_empty(),
            "Expected no warnings for properly cancelled weave"
        );
    }

    #[test]
    fn test_branch_inconsistent_handling() {
        // : bad ( -- )
        //   [ gen ] strand.weave
        //   true if strand.weave-cancel else drop then ;
        let word = WordDef {
            name: "bad".to_string(),
            effect: None,
            body: vec![
                Statement::Quotation {
                    span: None,
                    id: 0,
                    body: vec![make_word_call("gen")],
                },
                make_word_call("strand.weave"),
                Statement::BoolLiteral(true),
                Statement::If {
                    then_branch: vec![make_word_call("strand.weave-cancel")],
                    else_branch: Some(vec![make_word_call("drop")]),
                    span: None,
                },
            ],
            source: None,
            allowed_lints: vec![],
        };

        let mut analyzer = ResourceAnalyzer::new(Path::new("test.seq"));
        let diagnostics = analyzer.analyze_word(&word);

        // Should warn about drop in else branch
        assert!(!diagnostics.is_empty());
    }

    #[test]
    fn test_both_branches_cancel() {
        // : good ( -- )
        //   [ gen ] strand.weave
        //   true if strand.weave-cancel else strand.weave-cancel then ;
        let word = WordDef {
            name: "good".to_string(),
            effect: None,
            body: vec![
                Statement::Quotation {
                    span: None,
                    id: 0,
                    body: vec![make_word_call("gen")],
                },
                make_word_call("strand.weave"),
                Statement::BoolLiteral(true),
                Statement::If {
                    then_branch: vec![make_word_call("strand.weave-cancel")],
                    else_branch: Some(vec![make_word_call("strand.weave-cancel")]),
                    span: None,
                },
            ],
            source: None,
            allowed_lints: vec![],
        };

        let mut analyzer = ResourceAnalyzer::new(Path::new("test.seq"));
        let diagnostics = analyzer.analyze_word(&word);

        assert!(
            diagnostics.is_empty(),
            "Expected no warnings when both branches cancel"
        );
    }

    #[test]
    fn test_channel_leak() {
        // : bad ( -- ) chan.make drop ;
        let word = WordDef {
            name: "bad".to_string(),
            effect: None,
            body: vec![make_word_call("chan.make"), make_word_call("drop")],
            source: None,
            allowed_lints: vec![],
        };

        let mut analyzer = ResourceAnalyzer::new(Path::new("test.seq"));
        let diagnostics = analyzer.analyze_word(&word);

        assert_eq!(diagnostics.len(), 1);
        assert!(diagnostics[0].id.contains("channel"));
    }

    #[test]
    fn test_channel_properly_closed() {
        // : good ( -- ) chan.make chan.close ;
        let word = WordDef {
            name: "good".to_string(),
            effect: None,
            body: vec![make_word_call("chan.make"), make_word_call("chan.close")],
            source: None,
            allowed_lints: vec![],
        };

        let mut analyzer = ResourceAnalyzer::new(Path::new("test.seq"));
        let diagnostics = analyzer.analyze_word(&word);

        assert!(
            diagnostics.is_empty(),
            "Expected no warnings for properly closed channel"
        );
    }

    #[test]
    fn test_swap_resource_tracking() {
        // : test ( -- ) chan.make 1 swap drop drop ;
        // After swap: chan is on top, 1 is second
        // First drop removes chan (should warn), second drop removes 1
        let word = WordDef {
            name: "test".to_string(),
            effect: None,
            body: vec![
                make_word_call("chan.make"),
                Statement::IntLiteral(1),
                make_word_call("swap"),
                make_word_call("drop"), // drops chan - should warn
                make_word_call("drop"), // drops 1
            ],
            source: None,
            allowed_lints: vec![],
        };

        let mut analyzer = ResourceAnalyzer::new(Path::new("test.seq"));
        let diagnostics = analyzer.analyze_word(&word);

        assert_eq!(
            diagnostics.len(),
            1,
            "Expected warning for dropped channel: {:?}",
            diagnostics
        );
        assert!(diagnostics[0].id.contains("channel"));
    }

    #[test]
    fn test_over_resource_tracking() {
        // : test ( -- ) chan.make 1 over drop drop drop ;
        // Stack after chan.make: (chan)
        // Stack after 1: (chan 1)
        // Stack after over: (chan 1 chan) - chan copied to top
        // Both chan references are dropped without cleanup - both warn
        let word = WordDef {
            name: "test".to_string(),
            effect: None,
            body: vec![
                make_word_call("chan.make"),
                Statement::IntLiteral(1),
                make_word_call("over"),
                make_word_call("drop"), // drops copied chan - warns
                make_word_call("drop"), // drops 1
                make_word_call("drop"), // drops original chan - also warns
            ],
            source: None,
            allowed_lints: vec![],
        };

        let mut analyzer = ResourceAnalyzer::new(Path::new("test.seq"));
        let diagnostics = analyzer.analyze_word(&word);

        // Both channel drops warn (they share ID but neither was properly consumed)
        assert_eq!(
            diagnostics.len(),
            2,
            "Expected 2 warnings for dropped channels: {:?}",
            diagnostics
        );
    }

    #[test]
    fn test_channel_transferred_via_spawn() {
        // Pattern from shopping-cart: channel transferred to spawned worker
        // : accept-loop ( -- )
        //   chan.make                  # create channel
        //   dup [ worker ] strand.spawn  # transfer to worker
        //   drop drop                  # drop strand-id and dup'd chan
        //   chan.send                  # use remaining chan
        // ;
        let word = WordDef {
            name: "accept-loop".to_string(),
            effect: None,
            body: vec![
                make_word_call("chan.make"),
                make_word_call("dup"),
                Statement::Quotation {
                    span: None,
                    id: 0,
                    body: vec![make_word_call("worker")],
                },
                make_word_call("strand.spawn"),
                make_word_call("drop"),
                make_word_call("drop"),
                make_word_call("chan.send"),
            ],
            source: None,
            allowed_lints: vec![],
        };

        let mut analyzer = ResourceAnalyzer::new(Path::new("test.seq"));
        let diagnostics = analyzer.analyze_word(&word);

        assert!(
            diagnostics.is_empty(),
            "Expected no warnings when channel is transferred via strand.spawn: {:?}",
            diagnostics
        );
    }

    #[test]
    fn test_else_branch_only_leak() {
        // : test ( -- )
        //   chan.make
        //   true if chan.close else drop then ;
        // The else branch drops without cleanup - should warn about inconsistency
        // AND the join should track that the resource might not be consumed
        let word = WordDef {
            name: "test".to_string(),
            effect: None,
            body: vec![
                make_word_call("chan.make"),
                Statement::BoolLiteral(true),
                Statement::If {
                    then_branch: vec![make_word_call("chan.close")],
                    else_branch: Some(vec![make_word_call("drop")]),
                    span: None,
                },
            ],
            source: None,
            allowed_lints: vec![],
        };

        let mut analyzer = ResourceAnalyzer::new(Path::new("test.seq"));
        let diagnostics = analyzer.analyze_word(&word);

        // Should have warnings: branch inconsistency + drop without cleanup
        assert!(
            !diagnostics.is_empty(),
            "Expected warnings for else-branch leak: {:?}",
            diagnostics
        );
    }

    #[test]
    fn test_branch_join_both_consume() {
        // : test ( -- )
        //   chan.make
        //   true if chan.close else chan.close then ;
        // Both branches properly consume - no warnings
        let word = WordDef {
            name: "test".to_string(),
            effect: None,
            body: vec![
                make_word_call("chan.make"),
                Statement::BoolLiteral(true),
                Statement::If {
                    then_branch: vec![make_word_call("chan.close")],
                    else_branch: Some(vec![make_word_call("chan.close")]),
                    span: None,
                },
            ],
            source: None,
            allowed_lints: vec![],
        };

        let mut analyzer = ResourceAnalyzer::new(Path::new("test.seq"));
        let diagnostics = analyzer.analyze_word(&word);

        assert!(
            diagnostics.is_empty(),
            "Expected no warnings when both branches consume: {:?}",
            diagnostics
        );
    }

    #[test]
    fn test_branch_join_neither_consume() {
        // : test ( -- )
        //   chan.make
        //   true if else then drop ;
        // Neither branch consumes, then drop after - should warn
        let word = WordDef {
            name: "test".to_string(),
            effect: None,
            body: vec![
                make_word_call("chan.make"),
                Statement::BoolLiteral(true),
                Statement::If {
                    then_branch: vec![],
                    else_branch: Some(vec![]),
                    span: None,
                },
                make_word_call("drop"), // drops the channel
            ],
            source: None,
            allowed_lints: vec![],
        };

        let mut analyzer = ResourceAnalyzer::new(Path::new("test.seq"));
        let diagnostics = analyzer.analyze_word(&word);

        assert_eq!(
            diagnostics.len(),
            1,
            "Expected warning for dropped channel: {:?}",
            diagnostics
        );
        assert!(diagnostics[0].id.contains("channel"));
    }

    // ========================================================================
    // Cross-word analysis tests (ProgramResourceAnalyzer)
    // ========================================================================

    #[test]
    fn test_cross_word_resource_tracking() {
        // Test that resources returned from user-defined words are tracked
        //
        // : make-chan ( -- chan ) chan.make ;
        // : leak-it ( -- ) make-chan drop ;
        //
        // The drop in leak-it should warn because make-chan returns a channel
        use crate::ast::Program;

        let make_chan = WordDef {
            name: "make-chan".to_string(),
            effect: None,
            body: vec![make_word_call("chan.make")],
            source: None,
            allowed_lints: vec![],
        };

        let leak_it = WordDef {
            name: "leak-it".to_string(),
            effect: None,
            body: vec![make_word_call("make-chan"), make_word_call("drop")],
            source: None,
            allowed_lints: vec![],
        };

        let program = Program {
            words: vec![make_chan, leak_it],
            includes: vec![],
            unions: vec![],
        };

        let mut analyzer = ProgramResourceAnalyzer::new(Path::new("test.seq"));
        let diagnostics = analyzer.analyze_program(&program);

        assert_eq!(
            diagnostics.len(),
            1,
            "Expected warning for dropped channel from make-chan: {:?}",
            diagnostics
        );
        assert!(diagnostics[0].id.contains("channel"));
        assert!(diagnostics[0].message.contains("make-chan"));
    }

    #[test]
    fn test_cross_word_proper_cleanup() {
        // Test that properly cleaned up cross-word resources don't warn
        //
        // : make-chan ( -- chan ) chan.make ;
        // : use-it ( -- ) make-chan chan.close ;
        use crate::ast::Program;

        let make_chan = WordDef {
            name: "make-chan".to_string(),
            effect: None,
            body: vec![make_word_call("chan.make")],
            source: None,
            allowed_lints: vec![],
        };

        let use_it = WordDef {
            name: "use-it".to_string(),
            effect: None,
            body: vec![make_word_call("make-chan"), make_word_call("chan.close")],
            source: None,
            allowed_lints: vec![],
        };

        let program = Program {
            words: vec![make_chan, use_it],
            includes: vec![],
            unions: vec![],
        };

        let mut analyzer = ProgramResourceAnalyzer::new(Path::new("test.seq"));
        let diagnostics = analyzer.analyze_program(&program);

        assert!(
            diagnostics.is_empty(),
            "Expected no warnings for properly closed channel: {:?}",
            diagnostics
        );
    }

    #[test]
    fn test_cross_word_chain() {
        // Test multi-level cross-word tracking
        //
        // : make-chan ( -- chan ) chan.make ;
        // : wrap-chan ( -- chan ) make-chan ;
        // : leak-chain ( -- ) wrap-chan drop ;
        use crate::ast::Program;

        let make_chan = WordDef {
            name: "make-chan".to_string(),
            effect: None,
            body: vec![make_word_call("chan.make")],
            source: None,
            allowed_lints: vec![],
        };

        let wrap_chan = WordDef {
            name: "wrap-chan".to_string(),
            effect: None,
            body: vec![make_word_call("make-chan")],
            source: None,
            allowed_lints: vec![],
        };

        let leak_chain = WordDef {
            name: "leak-chain".to_string(),
            effect: None,
            body: vec![make_word_call("wrap-chan"), make_word_call("drop")],
            source: None,
            allowed_lints: vec![],
        };

        let program = Program {
            words: vec![make_chan, wrap_chan, leak_chain],
            includes: vec![],
            unions: vec![],
        };

        let mut analyzer = ProgramResourceAnalyzer::new(Path::new("test.seq"));
        let diagnostics = analyzer.analyze_program(&program);

        // Should detect the leak through the chain
        assert_eq!(
            diagnostics.len(),
            1,
            "Expected warning for dropped channel through chain: {:?}",
            diagnostics
        );
    }
}