fix(mir/builder): use function-local ValueId throughout MIR builder

Phase 25.1b: Complete SSA fix - eliminate all global ValueId usage in function contexts.

Root cause: ~75 locations throughout MIR builder were using global value
generator (self.value_gen.next()) instead of function-local allocator
(f.next_value_id()), causing SSA verification failures and runtime
"use of undefined value" errors.

Solution:
- Added next_value_id() helper that automatically chooses correct allocator
- Fixed 19 files with ~75 occurrences of ValueId allocation
- All function-context allocations now use function-local IDs

Files modified:
- src/mir/builder/utils.rs: Added next_value_id() helper, fixed 8 locations
- src/mir/builder/builder_calls.rs: 17 fixes
- src/mir/builder/ops.rs: 8 fixes
- src/mir/builder/stmts.rs: 7 fixes
- src/mir/builder/emission/constant.rs: 6 fixes
- src/mir/builder/rewrite/*.rs: 10 fixes
- + 13 other files

Verification:
- cargo build --release: SUCCESS
- Simple tests with NYASH_VM_VERIFY_MIR=1: Zero undefined errors
- Multi-parameter static methods: All working

Known remaining: ValueId(22) in Stage-B (separate issue to investigate)

🤖 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 00:48:18 +09:00
parent 35842503e6
commit eadde8d1dd
59 changed files with 1603 additions and 370 deletions

View File

@ -15,7 +15,7 @@ impl super::MirBuilder {
super::utils::builder_debug_log(&format!("extract_string_literal OK: {}", type_name));
let val = self.build_expression(call.arguments[0].clone())?;
let ty = super::MirBuilder::parse_type_name_to_mir(&type_name);
let dst = self.value_gen.next();
let dst = self.next_value_id();
let op = if call.name == "isType" { TypeOpKind::Check } else { TypeOpKind::Cast };
super::utils::builder_debug_log(&format!("emit TypeOp {:?} value={} dst= {}", op, val, dst));
self.emit_instruction(MirInstruction::TypeOp { dst, op, value: val, ty })?;
@ -46,7 +46,7 @@ impl super::MirBuilder {
));
let val = self.build_expression(arguments[0].clone())?;
let ty = super::MirBuilder::parse_type_name_to_mir(&type_name);
let dst = self.value_gen.next();
let dst = self.next_value_id();
let op = if name == "isType" {
TypeOpKind::Check
} else {
@ -89,7 +89,7 @@ impl super::MirBuilder {
));
let obj_val = self.build_expression(*object.clone())?;
let ty = super::MirBuilder::parse_type_name_to_mir(&type_name);
let dst = self.value_gen.next();
let dst = self.next_value_id();
let op = if method == "is" {
TypeOpKind::Check
} else {
@ -204,7 +204,7 @@ impl super::MirBuilder {
self.variable_map.insert(var_name.clone(), var_id);
last_value = Some(var_id);
}
Ok(last_value.unwrap_or_else(|| self.value_gen.next()))
Ok(last_value.unwrap_or_else(|| self.next_value_id()))
}
// Return statement
@ -266,7 +266,7 @@ impl super::MirBuilder {
for a in arguments.into_iter() {
arg_vals.push(self.build_expression(a)?);
}
let future_id = self.value_gen.next();
let future_id = self.next_value_id();
self.emit_instruction(MirInstruction::ExternCall {
dst: Some(future_id),
iface_name: "env.future".to_string(),
@ -278,7 +278,7 @@ impl super::MirBuilder {
return Ok(future_id);
}
let expression_value = self.build_expression(expression)?;
let future_id = self.value_gen.next();
let future_id = self.next_value_id();
self.emit_instruction(MirInstruction::FutureNew {
dst: future_id,
value: expression_value,
@ -294,7 +294,7 @@ impl super::MirBuilder {
) -> Result<ValueId, String> {
let future_value = self.build_expression(expression)?;
self.emit_instruction(MirInstruction::Safepoint)?;
let result_id = self.value_gen.next();
let result_id = self.next_value_id();
self.emit_instruction(MirInstruction::Await {
dst: result_id,
future: future_value,