fix(mir): fix else block scope bug - PHI materialization order

Root Cause:
- Else blocks were not propagating variable assignments to outer scope
- Bug 1 (if_form.rs): PHI materialization happened before variable_map reset,
  causing PHI nodes to be lost
- Bug 2 (phi.rs): Variable merge didn't check if else branch modified variables

Changes:
- src/mir/builder/if_form.rs:93-127
  - Reordered: reset variable_map BEFORE materializing PHI nodes
  - Now matches then-branch pattern (reset → materialize → execute)
  - Applied to both "else" and "no else" branches for consistency
- src/mir/builder/phi.rs:137-154
  - Added else_modified_var check to detect variable modifications
  - Use modified value from else_var_map_end_opt when available
  - Fall back to pre-if value only when truly not modified

Test Results:
 Simple block: { x=42 } → 42
 If block: if 1 { x=42 } → 42
 Else block: if 0 { x=99 } else { x=42 } → 42 (FIXED!)
 Stage-B body extraction: "return 42" correctly extracted (was null)

Impact:
- Else block variable assignments now work correctly
- Stage-B compiler body extraction restored
- Selfhost builder path can now function
- Foundation for Phase 21.x progress

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
nyash-codex
2025-11-13 20:16:20 +09:00
parent 801833df8d
commit 8b44c5009f
19 changed files with 309 additions and 205 deletions

View File

@ -66,3 +66,31 @@ SSOT for Using/Resolver (summary)
- Verify routing: HAKO_VERIFY_PRIMARY=hakovm (default); hv1_inline perf path parity (env toggles only).
- Build: `cargo build --release` (default features); LLVM paths are optin.
- Docs: keep RESTORE steps for any archived parts; small diffs, easy rollback.
## Convergence Plan — Line Consolidation (A→D)
Goal: reduce parallel lines (Rust/Hako builders, VM variants, LLVM backends) to a clear SSOT while keeping reversibility.
Phase A — Stabilize (now)
- SSOT: semantics/normalization/optimization live in Hako (AotPrep/Normalize).
- Rust: limit to structure/safety/emit (SSA/PHI/guards/executor). No new rules.
- Gates: quick/integration canaries green; VM↔LLVM parity for representatives; no default flips.
Phase B — Defaultization (small flips)
- StageB/selfhost builder: default ON in dev/quick; provider as fallback. Document toggles and rollback.
- AotPrep passes: enable normalize/collections_hot behind canaries; promote gradually.
- Docs: ENV_VARS + CURRENT_TASK に昇格条件/戻し手順を明記。
Phase C — Line Thinning
- LLVM: prefer crate (ny-llvmc) as default; llvmlite becomes optional job (deprecation window).
- VM: Hakorune VM = primary; PyVM = reference/comparison only.
- Remove duplicated heavy paths from default profiles; keep explicit toggles for restore.
Phase D — Toggle Cleanup & Sunsets
- Once stable in defaults for ≥2 weeks: remove legacy toggles and code paths (e.g., Rust normalize.rs).
- Record sunset plan (reason/range/restore) in CURRENT_TASK and changelog.
Acceptance (each phase)
- quick/integration green, parity holds (exit codes/log shape where applicable).
- Defaults unchanged until promotion; any flip is guarded and reversible.
- Small diffs; explicit RESTORE steps; minimal blast radius.