refactor(normalization): Phase 135 P0 - Extend plan to zero post-loop assigns

Generalize NormalizationPlan suffix detection to accept zero post-loop assignments:

Goal: Improve entry point consistency by allowing `loop + assign* + return` (N >= 0)

Implementation:
- Modified plan_box.rs detection logic (only file changed)
- Removed `post_assign_count >= 1` requirement
- Unified Phase 131 (loop + return) and Phase 132-133 (loop + assign+ + return) paths

Changes:
- src/mir/builder/control_flow/normalization/plan_box.rs:
  - Removed assignment count constraint
  - Unified pattern detection: `loop + assign* + return` (N >= 0)
- apps/tests/phase135_loop_true_break_once_post_empty_return_min.hako (new fixture)
- tools/smokes/v2/profiles/integration/apps/phase135_*.sh (new smoke tests)

Pattern support:
- Phase 131/135: loop + return only (consumed: 2, post_assign_count: 0) 
- Phase 132: loop + 1 assign + return (consumed: 3, post_assign_count: 1) 
- Phase 133: loop + N assigns + return (consumed: 2+N, post_assign_count: N) 

Design principles maintained:
- **Minimal change**: Only plan_box.rs modified (execute_box unchanged)
- **SSOT**: Detection logic centralized in plan_box.rs
- **Box-First**: Responsibility separation preserved (Plan/Execute)

Test results:
- Unit tests (plan_box): 9/9 PASS (2 new tests added)
- Phase 135 VM/LLVM EXE: PASS (exit code 1)
- Phase 131 regression: 2/2 PASS (path now unified)
- Phase 133 regression: 2/2 PASS
- cargo test --lib: PASS

Benefits:
- Unified entry point for all loop + post patterns
- Easier maintenance (single detection logic)
- Future extensibility (easy to add new patterns)
- Clear separation of Phase 131 and Phase 132-135 paths

Default behavior unchanged: Dev-only guard maintained

Related: Phase 135 normalization pattern consistency improvement

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

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
nyash-codex
2025-12-18 22:46:32 +09:00
parent f4ab5ca5f4
commit 91c7dfbf0b
6 changed files with 217 additions and 44 deletions

View File

@ -87,13 +87,13 @@ pub enum PlanKind {
---
## Pattern Detection (Phase 131-133)
## Pattern Detection (Phase 131-135)
### Phase 131: Loop-Only
- Pattern: `loop(true) { ... break }`
- Pattern: `loop(true) { ... break }` (single statement, no return)
- Consumed: 1 statement
- Kind: `PlanKind::LoopOnly`
- Example: `loop(true) { x = 1; break }; return x`
- Example: `loop(true) { x = 1; break }`
### Phase 132: Loop + Single Post
- Pattern: `loop(true) { ... break }; <assign>; return <expr>`
@ -107,6 +107,14 @@ pub enum PlanKind {
- Kind: `PlanKind::LoopWithPost { post_assign_count: N }`
- Example: `loop(true) { x = 1; break }; x = x + 2; x = x + 3; return x`
### Phase 135: Loop + Return (Zero Post Assigns) **NEW**
- Pattern: `loop(true) { ... break }; return <expr>` (0 post-loop assignments)
- Consumed: 2 statements (loop, return)
- Kind: `PlanKind::LoopWithPost { post_assign_count: 0 }`
- Example: `loop(true) { x = 1; break }; return x`
- **Improvement**: Unifies Phase 131 and Phase 132-133 patterns under `LoopWithPost` enum
- **Compatibility**: Phase 131 (loop-only, no return) remains as `PlanKind::LoopOnly`
---
## Contract
@ -127,7 +135,7 @@ pub enum PlanKind {
**Invariants**:
- `consumed <= remaining.len()` (never consume more than available)
- If `requires_return` is true, `remaining[consumed-1]` must be Return
- Post-assign patterns require `consumed >= 3` (loop + assign + return minimum)
- Post-assign patterns require `consumed >= 2` (loop + return minimum, Phase 135+)
### NormalizationExecuteBox::execute()
@ -172,10 +180,13 @@ pub enum PlanKind {
- Phase 131 pattern detection (loop-only)
- Phase 132 pattern detection (loop + single post)
- Phase 133 pattern detection (loop + multiple post)
- **Phase 135 pattern detection (loop + zero post)** **NEW**
- Return boundary detection (consumed stops at return)
- **Return boundary with trailing statements** **NEW**
- Non-matching patterns (returns None)
### Regression Smokes
- **Phase 135**: `phase135_loop_true_break_once_post_empty_return_{vm,llvm_exe}.sh` **NEW**
- Phase 133: `phase133_loop_true_break_once_post_multi_add_{vm,llvm_exe}.sh`
- Phase 132: `phase132_loop_true_break_once_post_add_llvm_exe.sh`
- Phase 131: `phase131_loop_true_break_once_{vm,llvm_exe}.sh`