refactor: Deduplicate word simulation logic in resource_lint.rs #207

Closed
opened 2026-01-09 04:42:42 +00:00 by navicore · 1 comment
navicore commented 2026-01-09 04:42:42 +00:00 (Migrated from github.com)

Problem

crates/compiler/src/resource_lint.rs contains nearly identical word simulation logic duplicated in two places:

  1. First pass (lines 434-536): simulate_word_call - 102 lines, no diagnostics
  2. Second pass (lines 634-855): analyze_word_call_with_context - 221 lines, with diagnostics

Both handle:

  • Resource creation tracking
  • Resource consumption tracking
  • Stack operations simulation
  • User-defined word effects

Impact

  • Bug fixes must be applied in two places
  • Inconsistencies between passes cause subtle bugs
  • 323 lines of duplicated logic

Proposed Solution

Extract common simulation logic into a shared function with configurable diagnostic emission:

struct SimulationConfig {
    emit_diagnostics: bool,
    diagnostic_sink: Option<&mut Vec<Diagnostic>>,
}

fn simulate_word_call_common(
    word: &str,
    state: &mut ResourceState,
    config: SimulationConfig,
) -> SimulationResult {
    // Single implementation used by both passes
}

Or use a visitor pattern:

trait SimulationVisitor {
    fn on_resource_created(&mut self, resource: &TrackedResource);
    fn on_resource_consumed(&mut self, resource: &TrackedResource);
    fn on_error(&mut self, error: ResourceError);
}

// First pass uses NoOpVisitor
// Second pass uses DiagnosticVisitor

Files Affected

  • crates/compiler/src/resource_lint.rs

Acceptance Criteria

  • Single source of truth for word simulation logic
  • Both passes produce identical simulation results
  • All resource_lint tests pass
  • Code reduction of at least 150 lines

Labels

refactor, technical-debt, critical

## Problem `crates/compiler/src/resource_lint.rs` contains nearly identical word simulation logic duplicated in two places: 1. **First pass** (lines 434-536): `simulate_word_call` - 102 lines, no diagnostics 2. **Second pass** (lines 634-855): `analyze_word_call_with_context` - 221 lines, with diagnostics Both handle: - Resource creation tracking - Resource consumption tracking - Stack operations simulation - User-defined word effects ## Impact - Bug fixes must be applied in two places - Inconsistencies between passes cause subtle bugs - 323 lines of duplicated logic ## Proposed Solution Extract common simulation logic into a shared function with configurable diagnostic emission: ```rust struct SimulationConfig { emit_diagnostics: bool, diagnostic_sink: Option<&mut Vec<Diagnostic>>, } fn simulate_word_call_common( word: &str, state: &mut ResourceState, config: SimulationConfig, ) -> SimulationResult { // Single implementation used by both passes } ``` Or use a visitor pattern: ```rust trait SimulationVisitor { fn on_resource_created(&mut self, resource: &TrackedResource); fn on_resource_consumed(&mut self, resource: &TrackedResource); fn on_error(&mut self, error: ResourceError); } // First pass uses NoOpVisitor // Second pass uses DiagnosticVisitor ``` ## Files Affected - `crates/compiler/src/resource_lint.rs` ## Acceptance Criteria - [ ] Single source of truth for word simulation logic - [ ] Both passes produce identical simulation results - [ ] All resource_lint tests pass - [ ] Code reduction of at least 150 lines ## Labels refactor, technical-debt, critical
navicore commented 2026-01-09 05:06:17 +00:00 (Migrated from github.com)
https://github.com/navicore/patch-seq/pull/216
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
navicore/patch-seq#207
No description provided.