Files
hakorune/src/mir/builder/exprs_lambda.rs
nyash-codex eadde8d1dd 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>
2025-11-17 00:48:18 +09:00

178 lines
6.3 KiB
Rust

use super::ValueId;
use crate::ast::ASTNode;
impl super::MirBuilder {
// Lambda lowering to NewClosure
pub(super) fn build_lambda_expression(
&mut self,
params: Vec<String>,
body: Vec<ASTNode>,
) -> Result<ValueId, String> {
use std::collections::HashSet;
let mut used: HashSet<String> = HashSet::new();
let mut locals: HashSet<String> = HashSet::new();
for p in &params {
locals.insert(p.clone());
}
fn collect_vars(
ast: &ASTNode,
used: &mut std::collections::HashSet<String>,
locals: &mut std::collections::HashSet<String>,
) {
match ast {
ASTNode::Variable { name, .. } => {
if !locals.contains(name) {
used.insert(name.clone());
}
}
ASTNode::Assignment { target, value, .. } => {
collect_vars(target, used, locals);
collect_vars(value, used, locals);
}
ASTNode::BinaryOp { left, right, .. } => {
collect_vars(left, used, locals);
collect_vars(right, used, locals);
}
ASTNode::UnaryOp { operand, .. } => {
collect_vars(operand, used, locals);
}
ASTNode::MethodCall {
object, arguments, ..
} => {
collect_vars(object, used, locals);
for a in arguments {
collect_vars(a, used, locals);
}
}
ASTNode::FunctionCall { arguments, .. } => {
for a in arguments {
collect_vars(a, used, locals);
}
}
ASTNode::Call {
callee, arguments, ..
} => {
collect_vars(callee, used, locals);
for a in arguments {
collect_vars(a, used, locals);
}
}
ASTNode::FieldAccess { object, .. } => {
collect_vars(object, used, locals);
}
ASTNode::New { arguments, .. } => {
for a in arguments {
collect_vars(a, used, locals);
}
}
ASTNode::If {
condition,
then_body,
else_body,
..
} => {
collect_vars(condition, used, locals);
for st in then_body {
collect_vars(st, used, locals);
}
if let Some(eb) = else_body {
for st in eb {
collect_vars(st, used, locals);
}
}
}
ASTNode::Loop {
condition, body, ..
} => {
collect_vars(condition, used, locals);
for st in body {
collect_vars(st, used, locals);
}
}
ASTNode::TryCatch {
try_body,
catch_clauses,
finally_body,
..
} => {
for st in try_body {
collect_vars(st, used, locals);
}
for c in catch_clauses {
for st in &c.body {
collect_vars(st, used, locals);
}
}
if let Some(fb) = finally_body {
for st in fb {
collect_vars(st, used, locals);
}
}
}
ASTNode::Throw { expression, .. } => {
collect_vars(expression, used, locals);
}
ASTNode::Print { expression, .. } => {
collect_vars(expression, used, locals);
}
ASTNode::Return { value, .. } => {
if let Some(v) = value {
collect_vars(v, used, locals);
}
}
ASTNode::AwaitExpression { expression, .. } => {
collect_vars(expression, used, locals);
}
ASTNode::MatchExpr {
scrutinee,
arms,
else_expr,
..
} => {
collect_vars(scrutinee, used, locals);
for (_, e) in arms {
collect_vars(e, used, locals);
}
collect_vars(else_expr, used, locals);
}
ASTNode::Program { statements, .. } => {
for st in statements {
collect_vars(st, used, locals);
}
}
ASTNode::FunctionDeclaration { params, body, .. } => {
let mut inner = locals.clone();
for p in params {
inner.insert(p.clone());
}
for st in body {
collect_vars(st, used, &mut inner);
}
}
_ => {}
}
}
for st in body.iter() {
collect_vars(st, &mut used, &mut locals);
}
let mut captures: Vec<(String, ValueId)> = Vec::new();
for name in used.into_iter() {
if let Some(&vid) = self.variable_map.get(&name) {
captures.push((name, vid));
}
}
let me = self.variable_map.get("me").copied();
let dst = self.next_value_id();
self.emit_instruction(super::MirInstruction::NewClosure {
dst,
params: params.clone(),
body: body.clone(),
captures,
me,
})?;
self.value_types
.insert(dst, crate::mir::MirType::Box("FunctionBox".to_string()));
Ok(dst)
}
}