fix(mir/builder): improve LocalSSA error handling and add Copy/Call traces

Changes:
- src/mir/builder/ssa/local.rs:
  - Fix LocalSSA::ensure() to check emit_instruction() errors
  - Return original value instead of undefined ValueId on failure
  - Add NYASH_LOCAL_SSA_TRACE logging for failures

- src/mir/builder.rs:
  - Add Copy instruction trace with NYASH_LOCAL_SSA_TRACE=1
  - Add Call instruction trace for Method calls
  - Helps debug receiver materialization issues

- src/mir/builder/builder_calls.rs:
  - Remove duplicate receiver materialization (moved to builder.rs)
  - Rely on emit_instruction for final receiver handling

Related to Stage-B ValueId(21) undefined error investigation.
Next: Fix Nyash MirBuilder receiver handling in basic_lower_box.hako

🤖 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-17 08:41:50 +09:00
parent 06159da58b
commit f7d218190e
3 changed files with 73 additions and 12 deletions

View File

@ -42,7 +42,14 @@ pub fn ensure(builder: &mut MirBuilder, v: ValueId, kind: LocalKind) -> ValueId
// Stage-B 経路などでは current_block が割り当て済みでも、ブロック自体が
// function にまだ追加されていない場合があり、そのまま emit_instruction すると
// Copy が黙って落ちてしまう。ここで best-effort で作成しておく。
let _ = builder.ensure_block_exists(bb);
// CRITICAL: Check for errors - if block creation fails, return original value.
if let Err(e) = builder.ensure_block_exists(bb) {
if std::env::var("NYASH_LOCAL_SSA_TRACE").ok().as_deref() == Some("1") {
eprintln!("[local-ssa] ensure_block_exists FAILED bb={:?} kind={:?} v=%{} err={}",
bb, kind, v.0, e);
}
return v;
}
// CRITICAL FIX: If `v` is from a pinned slot, check if there's a PHI value for that slot
// in the current block's variable_map. If so, use the PHI value directly instead of
@ -69,11 +76,20 @@ pub fn ensure(builder: &mut MirBuilder, v: ValueId, kind: LocalKind) -> ValueId
}
let loc = builder.next_value_id();
// Best-effort: errors are propagated by caller; we ignore here to keep helper infallible
let _ = builder.emit_instruction(crate::mir::MirInstruction::Copy { dst: loc, src: v });
// CRITICAL: Check emit_instruction result - if Copy fails, return original value
// to avoid returning undefined ValueId.
if let Err(e) = builder.emit_instruction(crate::mir::MirInstruction::Copy { dst: loc, src: v }) {
if std::env::var("NYASH_LOCAL_SSA_TRACE").ok().as_deref() == Some("1") {
eprintln!("[local-ssa] emit_instruction Copy FAILED bb={:?} kind={:?} v=%{} dst=%{} err={}",
bb, kind, v.0, loc.0, e);
}
// Failed to emit Copy - return original value instead of undefined dst
return v;
}
if std::env::var("NYASH_LOCAL_SSA_TRACE").ok().as_deref() == Some("1") {
eprintln!("[local-ssa] copy bb={:?} kind={:?} %{} -> %{}", bb, kind, v.0, loc.0);
}
// Success: register metadata and cache
if let Some(t) = builder.value_types.get(&v).cloned() {
builder.value_types.insert(loc, t);
}