seq-compiler 5.6.1

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
//! Program-wide resource analyzer — does a two-pass walk that first collects
//! each word's resource-return shape, then simulates each body with that
//! information to catch cross-word leaks.

use std::collections::HashMap;
use std::path::Path;

use crate::ast::{Program, Span, Statement, WordDef};
use crate::lint::{LintDiagnostic, Severity};

use super::state::{
    InconsistentResource, ResourceKind, StackState, StackValue, TrackedResource, WordResourceInfo,
};

/// 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);
                }
            }
            ">aux" => {
                // Move top of main stack to aux stack (Issue #350)
                if let Some(val) = state.pop() {
                    state.aux_stack.push(val);
                }
            }
            "aux>" => {
                // Move top of aux stack back to main stack (Issue #350)
                if let Some(val) = state.aux_stack.pop() {
                    state.stack.push(val);
                }
            }
            "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,
        });
    }
}