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

@ -162,6 +162,19 @@ impl super::MirBuilder {
} else if is_static {
// Generic static box: lower all static methods into standalone MIR functions (BoxName.method/N)
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() {
if let ASTNode::FunctionDeclaration { params, body, .. } = method_ast {
let func_name = format!(