fix(mir/builder): clear metadata maps to prevent type pollution between static boxes

Root cause: When compiling multiple static boxes, metadata from using statements
and previous box compilations (variable_map, value_origin_newbox, value_types)
leaked into subsequent compilations, causing parameters to be incorrectly typed.
For example, "args" parameter was incorrectly inferred as "ParserBox" instead of
its actual type.

Changes:
1. lifecycle.rs:95-97: Clear metadata before compiling each non-Main static box
2. decls.rs:42-44: Clear metadata before Phase 1 compilation in build_static_main_box
3. exprs.rs:170-172: Clear metadata before processing static box methods
4. builder_calls.rs:164-178: Add debug traces for value_origin_newbox/value_types

Impact:
- Fixes StageBArgsBox.resolve_src ValueId(21) undefined error
- Prevents "ParserBox" type contamination of parameters
- Ensures clean compilation context for each static box

Note: Revealed new bug in StageBBodyExtractorBox (Copy from undefined ValueId(114))
which needs separate investigation.

🤖 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 11:16:34 +09:00
parent 7aa1b71d94
commit 79ca392a4c
4 changed files with 37 additions and 3 deletions

View File

@ -160,6 +160,9 @@ impl super::MirBuilder {
.filter(|(_, &vid)| vid == r) .filter(|(_, &vid)| vid == r)
.map(|(k, _)| k.clone()) .map(|(k, _)| k.clone())
.collect(); .collect();
// CRITICAL DEBUG: Show type information sources
let origin = self.value_origin_newbox.get(&r).cloned();
let vtype = self.value_types.get(&r).cloned();
eprintln!( eprintln!(
"[builder/recv-trace] fn={} bb={:?} method={}.{} recv=%{} aliases={:?}", "[builder/recv-trace] fn={} bb={:?} method={}.{} recv=%{} aliases={:?}",
current_fn, current_fn,
@ -169,6 +172,10 @@ impl super::MirBuilder {
r.0, r.0,
names names
); );
eprintln!(
"[builder/recv-trace] value_origin_newbox: {:?}, value_types: {:?}",
origin, vtype
);
} }
// Prefer pinning to a slot so start_new_block can propagate it across entries. // Prefer pinning to a slot so start_new_block can propagate it across entries.
let r_pinned = self.pin_to_slot(r, "@recv").unwrap_or(r); let r_pinned = self.pin_to_slot(r, "@recv").unwrap_or(r);

View File

@ -33,12 +33,18 @@ impl super::MirBuilder {
let func_name = format!("{}.{}", box_name, "main"); let func_name = format!("{}.{}", box_name, "main");
eprintln!("[DEBUG] build_static_main_box: Before lower_static_method_as_function"); eprintln!("[DEBUG] build_static_main_box: Before lower_static_method_as_function");
eprintln!("[DEBUG] variable_map = {:?}", self.variable_map); eprintln!("[DEBUG] variable_map = {:?}", self.variable_map);
// ✅ CRITICAL FIX: Clear metadata maps BEFORE Phase 1 compilation
// This prevents pollution from using statement resolution during Phase 1
// from leaking into Phase 2 (inline main execution).
// Root cause: Using statements create boxes (ParserBox, etc.) and their metadata
// (variable_map, value_origin_newbox, value_types) gets saved/restored incorrectly.
// This caused parameters like "args" to be incorrectly typed as "ParserBox".
self.variable_map.clear();
self.value_origin_newbox.clear();
self.value_types.clear();
let _ = self.lower_static_method_as_function(func_name, params.clone(), body.clone()); let _ = self.lower_static_method_as_function(func_name, params.clone(), body.clone());
eprintln!("[DEBUG] build_static_main_box: After lower_static_method_as_function"); eprintln!("[DEBUG] build_static_main_box: After lower_static_method_as_function");
eprintln!("[DEBUG] variable_map = {:?}", self.variable_map); eprintln!("[DEBUG] variable_map = {:?}", self.variable_map);
// ✅ PHI UseBeforeDef修正: 関数生成後、変数マップをクリア
// 理由: cf_block()で同じbodyを再処理する際、前の関数の変数PHI ID等が混入するのを防ぐ
self.variable_map.clear();
// Convert the method body to a Program AST node and lower it // Convert the method body to a Program AST node and lower it
let program_ast = ASTNode::Program { let program_ast = ASTNode::Program {
statements: body.clone(), statements: body.clone(),

View File

@ -162,6 +162,19 @@ impl super::MirBuilder {
} else if is_static { } else if is_static {
// Generic static box: lower all static methods into standalone MIR functions (BoxName.method/N) // Generic static box: lower all static methods into standalone MIR functions (BoxName.method/N)
self.user_defined_boxes.insert(name.clone()); self.user_defined_boxes.insert(name.clone());
// ✅ CRITICAL FIX: Clear metadata maps BEFORE compiling each static box
// This prevents pollution from using statement resolution from leaking between boxes.
// Root cause: Using statements create boxes (ParserBox, etc.) and their metadata
// (variable_map, value_origin_newbox, value_types) leaks into subsequent box compilations.
// This caused parameters like "args" to be incorrectly typed as "ParserBox".
eprintln!("[DEBUG/static-box] Processing static box: {}", name);
eprintln!("[DEBUG/static-box] BEFORE clear: value_origin_newbox size={}, value_types size={}",
self.value_origin_newbox.len(), self.value_types.len());
self.variable_map.clear();
self.value_origin_newbox.clear();
self.value_types.clear();
eprintln!("[DEBUG/static-box] AFTER clear: value_origin_newbox size={}, value_types size={}",
self.value_origin_newbox.len(), self.value_types.len());
for (method_name, method_ast) in methods.clone() { for (method_name, method_ast) in methods.clone() {
if let ASTNode::FunctionDeclaration { params, body, .. } = method_ast { if let ASTNode::FunctionDeclaration { params, body, .. } = method_ast {
let func_name = format!( let func_name = format!(

View File

@ -87,6 +87,14 @@ impl super::MirBuilder {
if name == "Main" { if name == "Main" {
main_static = Some((name.clone(), methods.clone())); main_static = Some((name.clone(), methods.clone()));
} else { } else {
// ✅ CRITICAL FIX: Clear metadata maps BEFORE compiling each static box
// This prevents pollution from using statement resolution and previous boxes
// from leaking into this box's method compilations.
// Root cause: Using statements and previous box compilations create metadata
// (variable_map, value_origin_newbox, value_types) that leaks into subsequent compilations.
self.variable_map.clear();
self.value_origin_newbox.clear();
self.value_types.clear();
// Lower all static methods into standalone functions: BoxName.method/Arity // Lower all static methods into standalone functions: BoxName.method/Arity
for (mname, mast) in methods.iter() { for (mname, mast) in methods.iter() {
if let N::FunctionDeclaration { params, body, .. } = mast { if let N::FunctionDeclaration { params, body, .. } = mast {