Files
hakorune/docs/development/current
nyash-codex e6e306c020 fix(joinir): Phase 219 Phantom Carrier Bug fix complete
Fixes phantom carrier detection that blocked AST-based if-sum lowerer.

## Problem

Name-based heuristic in loop_update_summary.rs created phantom "count"
carrier even when variable didn't exist, causing:
1. counter_count() = 2 (i + phantom "count") instead of 1
2. is_simple_if_sum_pattern() = false
3. AST-based lowerer never activates (falls back to legacy)
4. Result: RC=0 instead of expected values

## Solution

Assignment-based carrier detection:

### New API (recommended)
```rust
pub fn analyze_loop_updates_from_ast(
    loop_body_ast: &[ASTNode]
) -> LoopUpdateSummary
```

- Extracts only variables with actual assignments in loop body
- Classifies by RHS structure (not name)
- Eliminates phantom carriers completely

### Changes

1. **Added**: `extract_assigned_variables()` - AST walker for assignments
2. **Added**: `find_assignment_rhs()` - RHS expression extraction
3. **Added**: `classify_update_kind_from_rhs()` - Structure-based classification
4. **Added**: `is_likely_loop_index()` - Name heuristic for disambiguation
5. **Deprecated**: `analyze_loop_updates()` - Legacy name-based API (3 call sites remain)

### Verification

Before:
```
variable_map = {i, sum, defs, len}
→ Phantom "count" detected
→ counter_count() = 2
→ is_simple_if_sum_pattern() = false
```

After:
```
assigned_vars = {i, sum}  // Only assigned!
→ No phantom carriers
→ counter_count() = 1
→ accumulation_count() = 1
→ is_simple_if_sum_pattern() = true 
```

## Files Modified

**Core Fix**:
- src/mir/join_ir/lowering/loop_update_summary.rs (+116 lines)
  - New assignment-based API
  - Phantom carrier elimination

**Integration**:
- src/mir/builder/control_flow/joinir/patterns/pattern_pipeline.rs (+3 lines)
  - Updated is_if_sum_pattern() to use new API

## Test Results

-  Phantom carrier bug fixed
-  AST lowerer activates correctly
- ⚠️ 3 deprecation warnings (expected, legacy call sites)
-  phase212/218 still RC=0 (blocked by condition variable support)

## Design Principles

1. **No Phantom Carriers**: Only variables with actual assignments
2. **Assignment-Based Detection**: LHS from AST assignments only
3. **Structure-Based Classification**: RHS patterns + name disambiguation

## Next Steps

Phase 220: Condition variable extraction and wiring to enable
phase212/218 tests to pass with correct RC values.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-10 02:30:14 +09:00
..