Implements role-based separation of LoopBodyLocal variables to prevent
inappropriate Trim promotion for body-only local variables.
## Changes
### Task 183-1: Design Documentation
- Created `phase183-loopbodylocal-role-separation.md` with role taxonomy:
- Condition LoopBodyLocal: Used in loop conditions → Trim promotion target
- Body-only LoopBodyLocal: Only in body → No promotion needed
- Documented architectural approach and implementation strategy
### Task 183-2: Implementation
- Added `TrimLoopLowerer::is_var_used_in_condition()` helper
- Recursively checks if variable appears in condition AST
- Handles BinaryOp, UnaryOp, MethodCall node types
- Updated `try_lower_trim_like_loop()` to filter condition LoopBodyLocal
- Only processes LoopBodyLocal that appear in break conditions
- Skips body-only LoopBodyLocal (returns Ok(None) early)
- Added 5 unit tests for variable detection logic
### Task 183-3: Test Files
- Created `phase183_body_only_loopbodylocal.hako`
- Demonstrates body-only LoopBodyLocal (`temp`) not triggering Trim
- Verified trace output: "No LoopBodyLocal detected, skipping Trim lowering"
- Created additional test files (phase183_p1_match_literal, phase183_p2_atoi, phase183_p2_parse_number)
### Task 183-4: Documentation Updates
- Updated `joinir-architecture-overview.md` with Phase 183 results
- Updated `CURRENT_TASK.md` with Phase 183 completion status
## Results
✅ LoopBodyLocal role separation complete
✅ Body-only LoopBodyLocal skips Trim promotion
✅ 5 unit tests passing
✅ Trace verification successful
## Next Steps (Phase 184+)
- Body-local variable MIR lowering support
- String concatenation filter relaxation
- Full _parse_number/_atoi implementation
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Clarifies that LoopScopeShape has two complementary construction paths
for different contexts (AST-based vs LoopForm-based).
## Analysis
After investigating, discovered these builders serve **different purposes**:
1. **AST-based** (`patterns/loop_scope_shape_builder.rs`):
- Builds from AST during MIR generation
- Extracts body_locals from ASTNode::Local declarations
- Used in Pattern 1-4 lowerers
2. **LoopForm-based** (`loop_scope_shape/builder.rs`):
- Builds from LoopForm during JoinIR lowering
- Analyzes LoopFormIntake snapshots
- Used in generic_case_a and pattern routing
These are NOT duplicates - they're complementary paths!
## Changes
1. **Cross-Reference Documentation**:
- `patterns/loop_scope_shape_builder.rs`: Added Phase 183-3 section
- `loop_scope_shape/builder.rs`: Added Phase 183-3 section
- Both now reference each other for clarity
2. **LoopScopeShape Struct Documentation**:
- Added "Phase 183-3: Construction Paths" section
- Documents two construction paths and their contexts
- Explains when to use each builder
3. **Clarified Responsibilities**:
- AST-based: For MIR building phase
- LoopForm-based: For JoinIR lowering phase
- Both maintain consistent field initialization
## Benefits
- **Clear separation**: Documented different contexts for each builder
- **Maintainability**: Future developers understand which builder to use
- **No code changes**: Pure documentation improvement
- **Cross-references**: Easy navigation between related modules
## Testing
✅ All loop_scope_shape tests pass (24 tests)
✅ No behavioral changes
✅ Documentation-only refactoring
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Makes CarrierInfo::from_variable_map() the primary initialization method.
Common pattern initializer now delegates to this centralized logic.
## Changes
1. **Primary Method: CarrierInfo::from_variable_map()**:
- Now the single source of truth for CarrierInfo construction
- Used by both MIR and JoinIR contexts
- Documented as primary initialization method (Phase 183-2)
2. **CommonPatternInitializer Refactoring**:
- Converted to thin wrapper around `CarrierInfo::from_variable_map()`
- Delegates carrier collection to primary method
- Only adds pattern-specific exclusion filtering
- Reduced code duplication (~30 lines removed)
3. **Documentation Updates**:
- `carrier_info.rs`: Added Phase 183-2 section explaining primary role
- `common_init.rs`: Documented delegation strategy
- Clear separation of concerns between modules
4. **Removed Duplicate Logic**:
- Eliminated manual carrier collection in `common_init.rs`
- Removed `CarrierVar` import (no longer directly constructed)
- Unified sorting and validation in one place
## Benefits
- **Single source of truth**: CarrierInfo construction logic in one module
- **Consistency**: Same initialization algorithm across MIR/JoinIR
- **Maintainability**: Changes to carrier logic only needed once
- **Testability**: Primary logic tested in carrier_info module
## Testing
✅ All carrier_info tests pass (7 tests)
✅ All pattern tests pass (124 tests)
✅ No behavioral changes
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Consolidates duplicate pattern detection logic across two routing layers.
## Changes
1. **Unified Detection Documentation**:
- Added Phase 183 comments to `loop_pattern_detection::classify()`
- Documented that this is the single source of truth for pattern classification
- Both routers now reference this centralized function
2. **Router Documentation Updates**:
- `patterns/router.rs`: Added Phase 183 comments explaining structure-based routing
- `loop_pattern_router.rs`: Added unified detection section
- Both routers now explicitly reference shared detection logic
3. **Improved Debug Output**:
- Added `pattern_kind` to debug message in `route_loop_pattern()`
- Helps diagnose pattern matching failures
## Benefits
- **Single source of truth**: Pattern classification logic in one place
- **Consistency**: Both routers use same detection algorithm
- **Maintainability**: Changes to classification rules only needed once
- **Documentation**: Clear references between routers and detection module
## Testing
✅ All loop_pattern_detection tests pass
✅ Pattern 2 tests pass
✅ No behavioral changes, pure documentation/organization refactoring
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Phase 179-B Task 6: Refactor Pattern 4 to use PatternPipelineContext
for unified preprocessing.
Changes:
- Use build_pattern_context() for initial loop variable extraction and scope construction
- Extract loop_var_name, loop_var_id, carrier_info_prelim, and scope from context
- Keep Pattern4CarrierAnalyzer logic inline (Select-based continue semantics)
- Reduce line count: 422 → 414 lines (1.9% reduction)
Note:
Pattern 4 has complex carrier analysis (Select-based continue, carrier filtering,
normalization) that requires specialized Pattern4CarrierAnalyzer. The minimal
refactoring maintains this complexity while establishing the pipeline pattern.
Benefits:
- Consistent entry point with Patterns 1-3
- Unified preprocessing flow
- Maintains all existing functionality and test compatibility
🤖 Generated with Claude Code (https://claude.com/claude-code)
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Phase 179-B Task 5: Refactor Pattern 2 to use PatternPipelineContext
for unified preprocessing.
Changes:
- Use build_pattern_context() for initial loop variable extraction and scope construction
- Extract loop_var_name, loop_var_id, carrier_info, and scope from context
- Keep Trim pattern logic inline (complex, needs dedicated module in future Phase 180+)
- Reduce line count: 517 → 509 lines (1.5% reduction)
Note:
Pattern 2 has significant complexity (Trim pattern, carrier filtering, break
condition processing) that cannot be easily unified without breaking the
"analyzer-only" design constraint of PatternPipelineContext. The minimal
refactoring maintains compatibility while establishing the pipeline pattern.
Benefits:
- Consistent entry point with Pattern 1/3
- Establishes pattern for future Trim module extraction
- Maintains all existing functionality and test compatibility
🤖 Generated with Claude Code (https://claude.com/claude-code)
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Phase 179-B Task 4: Refactor Pattern 3 to use PatternPipelineContext
for unified preprocessing.
Changes:
- Use build_pattern_context() for loop variable extraction and scope construction
- Extract sum_var_id from ctx.carrier_info instead of direct initialization
- Reduce line count: 168 → 149 lines (11% reduction, lower than target due to already optimized code)
- Maintain exact same behavior and test compatibility
Benefits:
- Consistent preprocessing logic with Pattern 1
- Single source of truth for carrier analysis
- Cleaner separation of concerns (analysis vs lowering)
🤖 Generated with Claude Code (https://claude.com/claude-code)
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Phase 179-A Step 4: Update TODO comment to be more specific about
the future improvement path (using MirBuilder's next_value_id()).
Changed vague "TODO: This should be delegated to a proper ValueId allocator"
to clear "Future improvement: Delegate to MirBuilder's next_value_id()".
Note: Other TODOs in loop_patterns/mod.rs are placeholder tests waiting
for future implementation and are intentionally kept as-is.
Phase 179-A Step 3: Improve code maintainability by replacing hardcoded
magic values with descriptive named constants.
Changes:
- instruction_rewriter.rs: K_EXIT_FUNC_NAME constant for "join_func_2"
- pattern3_with_if_phi.rs: PATTERN3_K_EXIT_SUM_FINAL_ID for ValueId(18)
Benefits:
- Self-documenting code (names explain the meaning)
- Easier to maintain (change in one place)
- Prevents typos and inconsistencies
Task 176-1: Pattern2 limitation investigation
- Identified 10 limitation points where only position carrier was handled
- Added TODO markers for Phase 176-2/3 implementation
- Created phase176-pattern2-limitations.md documentation
Task 176-2: CarrierUpdateLowerer helper implementation
- Implemented emit_carrier_update() helper function
- Supports CounterLike and AccumulationLike UpdateExpr patterns
- Added 6 unit tests (all passing)
- Fail-Fast error handling for carrier/variable not found
Task 176-3: Pattern2 lowerer multi-carrier extension
- Extended header PHI generation for all carriers
- Implemented loop update for all carriers using emit_carrier_update()
- Extended ExitLine/ExitMeta construction for all carriers
- Updated function call/jump args to include all carriers
- 9/10 tests passing (1 pre-existing test issue)
Task 176-4: E2E testing and bug fixes
- Fixed Trim pattern loop_var_name overwrite bug (pattern2_with_break.rs)
- Fixed InstructionRewriter latch_incoming mapping bug
- All E2E tests passing (RC=0): pos + result dual-carrier loops work
- test_jsonparser_parse_string_min2.hako verified
Task 176-5: Documentation updates
- Created phase176-completion-report.md
- Updated phase175-multicarrier-design.md with completion status
- Updated joinir-architecture-overview.md roadmap
- Updated CURRENT_TASK.md with Phase 176 completion + Phase 177 TODO
- Updated loop_pattern_space.md F-axis (multi-carrier support complete)
Technical achievements:
- Pattern2 now handles single/multiple carriers uniformly
- CarrierInfo architecture proven to work end-to-end
- Two critical bugs fixed (loop_var overwrite, latch_incoming mapping)
- No regressions in existing tests
Next: Phase 177 - Apply to JsonParser _parse_string full implementation
Debug-only verification module to catch JoinIR contract violations early:
- verify_loop_header_phis: Checks loop_var_name → PHI exists in header block
- verify_exit_line: Checks exit_bindings → values exist, exit block in range
- verify_joinir_contracts: Main entry point, runs all checks
Implementation:
- Added verification functions to merge/mod.rs (private module has type access)
- Called from merge_joinir_mir_blocks after exit block setup
- Only active in debug builds (#[cfg(debug_assertions)])
Benefits:
- Catches "动くけど header PHI 無い" bugs immediately
- Validates exit_bindings before variable_map reconnection
- Prevents silent contract violations during development
Builder pattern for JoinInlineBoundary construction, reduces field manipulation scattering.
# Changes
- NEW: src/mir/join_ir/lowering/inline_boundary_builder.rs (165 lines)
- JoinInlineBoundaryBuilder with 7 fluent methods
- Complete unit test coverage (4 tests)
- MODIFIED: src/mir/join_ir/lowering/mod.rs (+2 lines)
- Export inline_boundary_builder module
- Public re-export of JoinInlineBoundaryBuilder
- MODIFIED: src/mir/builder/control_flow/joinir/patterns/pattern2_with_break.rs
- Replace direct boundary field manipulation with builder pattern
- 9 lines of field assignments → fluent builder chain
# Benefits
- **Centralized**: All boundary construction logic in builder
- **Readable**: Fluent API shows construction intent clearly
- **Maintainable**: Changes to boundary structure isolated to builder
- **Type Safe**: Builder validates field consistency
# Tests
✅ All builder unit tests pass (4/4)
✅ All pattern module tests pass (30+)
✅ Library build succeeds with no errors
🤖 Generated with Claude Code (https://claude.com/claude-code)
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Expand Pattern 2 (loop with break) to handle else-break patterns:
- If condition is in else clause: `if (cond) { ... } else { break }`
- Extract and negate condition for proper break detection
- Added has_break_in_else_clause() helper in ast_feature_extractor
- Pattern2 now handles both then-break and else-break structures
Implementation:
- ast_feature_extractor: Added else-break pattern detection
- pattern2_with_break: Detect else-break case and wrap condition in UnaryOp Not
- Enables support for patterns like trim() with inverted break logic
Known limitation:
- Pattern 2 requires break conditions to only depend on:
* Loop parameter (e.g., 'start' in loop(start < end))
* Condition-only variables from outer scope (e.g., 'end')
- Does NOT support break conditions using loop-body variables (e.g., 'ch')
- Future Pattern 5+ will handle more complex break conditions
🤖 Generated with Claude Code
Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
## Problem
Pattern 4 (loop with continue) was producing SSA-undef errors due to function_params
key mismatch. function_params uses 'join_func_0'/'join_func_1' format (from join_func_name()),
but code was looking for 'main'/'loop_step'.
## Solution
- Fix mod.rs: Use correct function keys 'join_func_0' and 'join_func_1'
- Add warning logs to detect future key mismatches immediately
- Update pattern4_with_continue.rs: Mark as fully implemented (not stub)
- Remove unused imports: MergeResult, TailCallKind, classify_tail_call, etc.
## Testing
- Pattern 3 (If PHI): sum=9 ✅
- Pattern 4 (Continue): 25 ✅ (1+3+5+7+9)
- No SSA-undef errors
- No new warnings on successful execution
## Debug Improvements
Added pre-flight checks in merge/mod.rs Phase 33-21:
```
[cf_loop/joinir] WARNING: function_params.get('join_func_0') returned None.
Available keys: [...]
```
This will save significant debugging time for future parameter mapping issues.
🤖 Generated with Claude Code
Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
Wire LoopUpdateSummary box into real code paths:
- Add `update_summary: Option<LoopUpdateSummary>` field to LoopFeatures
- Add `detect_with_updates()` method to CaseALoweringShape
- Uses UpdateKind (CounterLike/AccumulationLike) for classification
- Single CounterLike carrier → StringExamination
- AccumulationLike present → ArrayAccumulation or IterationWithAccumulation
- Update loop_to_join.rs to use detect_with_updates()
- Update deprecated detect() to use detect_with_updates() internally
- Set update_summary: None in AST-based feature extraction
Carrier name heuristics now encapsulated in LoopUpdateSummary box.
No regression: 15/16 loop smoke tests pass (1 failure is pre-existing).
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
Add NYASH_JOINIR_STRUCTURE_ONLY=1 environment variable to bypass
function name whitelist and route purely based on loop structure.
When enabled:
- Skips hardcoded function name whitelist (13 entries)
- Routes directly to pattern detection (LoopPatternContext)
- Falls back to legacy LoopBuilder if no pattern matches
This is Phase 1 of structure-based routing migration:
- Phase 1: Opt-in via env flag (this commit) ✅
- Phase 2: Make structure-based default (future)
- Phase 3: Remove whitelist entirely (future)
Verified:
- test_loop_return.hako → RC: 2 ✅
- test_trim_loop.hako → RC: 3 ✅🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
Box theory refactoring: Extract exit_bindings construction logic from
pattern lowerers into focused, reusable ExitMetaCollector Box.
**Changes**:
1. Created meta_collector.rs (+102 lines):
- ExitMetaCollector::collect() builds exit_bindings from ExitMeta
- Pure function (no side effects)
- Reusable by all pattern lowerers
2. Updated pattern2_with_break.rs (-20 lines):
- Use ExitMetaCollector::collect() instead of inline filter_map
- Removed manual binding construction loop
- Cleaner caller code
3. Made exit_line module public:
- Allows pattern lowerers to use ExitMetaCollector
- Clear module visibility boundaries
**Box Design**:
- Single responsibility: Convert ExitMeta + variable_map → exit_bindings
- Pure function: No side effects, testable independently
- Reusable: Can be used by Pattern 3, Pattern 4, etc.
**Testing**:
- Build: ✅ Success (1m 04s)
- Execution: ✅ RC: 0 (Pattern 2 verified)
- Regression: ✅ No issues
**Metrics**:
- New lines: +102 (meta_collector.rs)
- Removed lines: -20 (pattern2_with_break.rs)
- Net change: +82 lines
- Code clarity: Significantly improved
🤖 Generated with Claude Code
Co-Authored-By: Claude <noreply@anthropic.com>