feat(loop-phi): Add body-local variable PHI generation for Rust AST loops

Phase 25.1c/k: Fix ValueId undefined errors in loops with body-local variables

**Problem:**
- FuncScannerBox.scan_all_boxes/1 and BreakFinderBox._find_loops/2 had ValueId
  undefined errors for variables declared inside loop bodies
- LoopFormBuilder only generated PHIs for preheader variables, missing body-locals
- Example: `local ch = s.substring(i, i+1)` inside loop → undefined on next iteration

**Solution:**
1. **Rust AST path** (src/mir/loop_builder.rs):
   - Detect body-local variables by comparing body_end_vars vs current_vars
   - Generate empty PHI nodes at loop header for body-local variables
   - Seal PHIs with latch + continue snapshot inputs after seal_phis()
   - Added HAKO_LOOP_PHI_TRACE=1 logging for debugging

2. **JSON v0 path** (already fixed in previous session):
   - src/runner/json_v0_bridge/lowering/loop_.rs handles body-locals
   - Uses same strategy but for JSON v0 bridge lowering

**Results:**
-  FuncScannerBox.scan_all_boxes: 41 body-local PHIs generated
-  Main.main (demo harness): 23 body-local PHIs generated
- ⚠️ Still some ValueId undefined errors remaining (exit PHI issue)

**Files changed:**
- src/mir/loop_builder.rs: body-local PHI generation logic
- lang/src/compiler/entry/func_scanner.hako: debug logging
- /tmp/stageb_funcscan_demo.hako: test harness

🤖 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-19 23:12:01 +09:00
parent fb256670a1
commit 525e59bc8d
35 changed files with 1795 additions and 607 deletions

View File

@ -55,16 +55,17 @@ pub(super) fn invoke_plugin_box(
&format!("{:?}", e)
)),
}
} else if let Some(string_box) = recv_box
.as_any()
.downcast_ref::<crate::boxes::string_box::StringBox>()
{
// Handle builtin StringBox methods
} else if recv_box.type_name() == "StringBox" {
// Handle builtin StringBox methods via to_string_box() so it works
// for both basic and plugin-backed StringBox implementations.
let s_box = recv_box.to_string_box();
let s = s_box.value;
match method {
"lastIndexOf" => {
if let Some(arg_id) = args.get(0) {
let needle = this.reg_load(*arg_id)?.to_string();
let result_box = string_box.lastIndexOf(&needle);
let helper = crate::boxes::string_box::StringBox::new(s);
let result_box = helper.lastIndexOf(&needle);
this.write_from_box(dst, result_box);
Ok(())
} else {
@ -74,13 +75,40 @@ pub(super) fn invoke_plugin_box(
"indexOf" | "find" => {
if let Some(arg_id) = args.get(0) {
let needle = this.reg_load(*arg_id)?.to_string();
let result_box = string_box.find(&needle);
let helper = crate::boxes::string_box::StringBox::new(s);
let result_box = helper.find(&needle);
this.write_from_box(dst, result_box);
Ok(())
} else {
Err(this.err_invalid("indexOf/find requires 1 argument"))
}
}
// Phase 25.1m: minimal builtin support for StringBox.is_space(ch)
"is_space" => {
if let Some(arg_id) = args.get(0) {
let ch = this.reg_load(*arg_id)?.to_string();
let is_ws = ch == " " || ch == "\t" || ch == "\n" || ch == "\r";
this.write_result(dst, VMValue::Bool(is_ws));
Ok(())
} else {
Err(this.err_invalid("is_space requires 1 argument"))
}
}
// Phase 25.1m: minimal builtin support for StringBox.is_alpha(ch)
"is_alpha" => {
if let Some(arg_id) = args.get(0) {
let ch = this.reg_load(*arg_id)?.to_string();
let c = ch.chars().next().unwrap_or('\0');
let is_alpha =
('A'..='Z').contains(&c) ||
('a'..='z').contains(&c) ||
c == '_';
this.write_result(dst, VMValue::Bool(is_alpha));
Ok(())
} else {
Err(this.err_invalid("is_alpha requires 1 argument"))
}
}
_ => Err(this.err_method_not_found("StringBox", method)),
}
} else {