feat(debug): Add NYASH_TRACE_VARMAP for variable_map debugging

Add trace_varmap() helper to track variable_map state during JoinIR merge.
Enable with NYASH_TRACE_VARMAP=1 to see variable→ValueId mappings.

Strategic trace points in Pattern 3:
- pattern3_before_merge: State before JoinIR→MIR merge
- pattern3_after_merge: State after merge (detects missing updates)
- pattern3_exit_phi_connected: State after exit PHI connection

This would have caught the 1.5hr debugging session bug instantly:
  [varmap/pattern3_after_merge] sum=ValueId(5)  ← still old value!
  [varmap/pattern3_exit_phi_connected] sum=ValueId(0)  ← fixed!

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
nyash-codex
2025-12-05 19:35:30 +09:00
parent ba9a4fa66d
commit e01d1b8a7c

View File

@ -3,6 +3,17 @@ use super::{Effect, EffectMask, MirInstruction, ValueId};
use crate::ast::ASTNode;
impl super::MirBuilder {
/// Trace variable_map state for debugging
/// Enable with NYASH_TRACE_VARMAP=1
fn trace_varmap(&self, context: &str) {
if std::env::var("NYASH_TRACE_VARMAP").is_ok() {
let vars: Vec<_> = self.variable_map.iter()
.map(|(k, v)| format!("{}={:?}", k, v))
.collect();
eprintln!("[varmap/{}] {{{}}}", context, vars.join(", "));
}
}
/// Control-flow: block
pub(super) fn cf_block(&mut self, statements: Vec<ASTNode>) -> Result<ValueId, String> {
// identical to build_block; kept here for future policy hooks
@ -783,11 +794,13 @@ impl super::MirBuilder {
// Merge JoinIR blocks into current function
// Phase 188-Impl-3: Create and pass JoinInlineBoundary for Pattern 3
// Pattern 3 has TWO carriers: i and sum
self.trace_varmap("pattern3_before_merge");
let boundary = crate::mir::join_ir::lowering::inline_boundary::JoinInlineBoundary::new_inputs_only(
vec![ValueId(0), ValueId(1)], // JoinIR's main() parameters (i, sum init)
vec![loop_var_id, sum_var_id], // Host's loop variables
);
let exit_phi_result = self.merge_joinir_mir_blocks(&mir_module, Some(&boundary), debug)?;
self.trace_varmap("pattern3_after_merge");
// Phase 189-Fix: Update variable_map["sum"] to point to exit PHI result
// The exit PHI contains the final value of sum after the loop completes.
@ -800,6 +813,7 @@ impl super::MirBuilder {
exit_phi
);
}
self.trace_varmap("pattern3_exit_phi_connected");
}
// Phase 188-Impl-3: Return Void (the loop itself doesn't produce a value, but its result