fix(runtime): resolve runtime errors against their logical module (B-08)

RuntimeError and CallFrame now carry a logical ModuleId and an origin
(source/callsite). Errors are attributed at function/body boundaries so the
innermost callee module wins; argument-binding errors and host/plugin/foreign
boundaries are attributed to the caller callsite without fabricating a foreign
stack.

The CLI resolves (module_id, span) through LoadedProject::locate instead of
indexing every span against the entry file. Unknown modules and out-of-range
spans keep the module id and byte span but emit null line/file. Human output and
frames now point at the correct file.

Record the decision in ADR-0017 and supersede the ADR-0006 limitation. Add an
end-to-end imported-module division-by-zero test.
This commit is contained in:
cflmy committed 2026-11-20 12:57:44 +08:00
1 parent 6fed396d20
commit 509bdb1bf6
8 files changed
+390 -38

No files matched your search

+74 -11
View File
@@ -95,6 +95,17 @@ impl<'a> Interpreter<'a> {
name: &str,
args: Vec<EvaluatedArg>,
call_span: Option<Span>,
) -> Result<Value, RuntimeError> {
self.call_by_name_from(module, name, args, call_span, None)
}
fn call_by_name_from(
&mut self,
module: &ModuleId,
name: &str,
args: Vec<EvaluatedArg>,
call_span: Option<Span>,
caller_module: Option<&ModuleId>,
) -> Result<Value, RuntimeError> {
let program = self.program;
let Some(target_module) = program.module(module) else {
@@ -111,7 +122,7 @@ impl<'a> Interpreter<'a> {
format!("unknown function `{name}` in module `{module}`"),
));
};
self.call_function(program, function, args, call_span)
self.call_function(program, function, args, call_span, caller_module)
}
fn call_function(
@@ -120,6 +131,7 @@ impl<'a> Interpreter<'a> {
function: &'a Function,
args: Vec<EvaluatedArg>,
call_span: Option<Span>,
caller_module: Option<&ModuleId>,
) -> Result<Value, RuntimeError> {
let display = function.id.name.clone();
let trace_id = function.id.to_string();
@@ -128,7 +140,15 @@ impl<'a> Interpreter<'a> {
}
self.host
.record_semantic("call.enter", Some(&trace_id), call_span, None);
let locals = self.bind_args(function, args, call_span)?;
let locals = match self.bind_args(function, args, call_span) {
Ok(locals) => locals,
Err(mut error) => {
if let Some(module) = caller_module {
error.attribute_module(module.canonical());
}
return Err(error);
}
};
let mut frame = Frame {
function: function.id.clone(),
namespace: function.namespace.clone(),
@@ -148,7 +168,12 @@ impl<'a> Interpreter<'a> {
Ok(Flow::Next) => Ok(Value::Unit),
Ok(Flow::Return(value)) => Ok(value),
Err(mut error) => {
error.push_frame(display, call_span);
error.attribute_module(function.id.module.canonical());
error.push_frame(
display,
call_span,
caller_module.map(|module| module.canonical()),
);
Err(error)
}
}
@@ -245,6 +270,20 @@ impl<'a> Interpreter<'a> {
program: &'a Program,
stmts: &'a [Stmt],
frame: &mut Frame,
) -> Result<Flow, RuntimeError> {
let module = frame.function.module.clone();
self.exec_stmts_inner(program, stmts, frame)
.map_err(|mut error| {
error.attribute_module(module.canonical());
error
})
}
fn exec_stmts_inner(
&mut self,
program: &'a Program,
stmts: &'a [Stmt],
frame: &mut Frame,
) -> Result<Flow, RuntimeError> {
for stmt in stmts {
self.host.consume_fuel(1, Some(stmt.span()))?;
@@ -357,7 +396,8 @@ impl<'a> Interpreter<'a> {
}
CallTarget::Resolved { module, function } => (module.clone(), function.clone()),
};
self.call_by_name(&module, &name, args, Some(span))
let caller = frame.function.module.clone();
self.call_by_name_from(&module, &name, args, Some(span), Some(&caller))
}
fn eval_args(
@@ -484,11 +524,12 @@ impl<'a> Interpreter<'a> {
span: Span,
frame: &mut Frame,
) -> Result<Value, RuntimeError> {
let caller_module = frame.function.module.clone();
match callee {
Expr::Ident(ident) => {
if let Some(value) = frame.locals.get(&ident.name).cloned() {
let args = self.eval_args(args, frame)?;
return self.call_value(program, value, args, Some(span));
return self.call_value(program, value, args, Some(span), Some(&caller_module));
}
if let Some(builtin) = builtins::find(&ident.name) {
let args = self.eval_args(args, frame)?;
@@ -505,7 +546,7 @@ impl<'a> Interpreter<'a> {
format!("unknown function `{}`", ident.name),
));
};
self.call_function(program, function, args, Some(span))
self.call_function(program, function, args, Some(span), Some(&caller_module))
}
Expr::Member { base, name, .. } => {
if let Expr::Ident(alias) = base.as_ref() {
@@ -521,7 +562,13 @@ impl<'a> Interpreter<'a> {
format!("unknown function `{name}` in module `{target_module}`"),
));
};
return self.call_function(program, function, args, Some(span));
return self.call_function(
program,
function,
args,
Some(span),
Some(&caller_module),
);
}
let qualified = format!("{}.{name}", alias.name);
if let Some(builtin) = builtins::find(&qualified) {
@@ -567,12 +614,24 @@ impl<'a> Interpreter<'a> {
}
let callee_value = self.eval_expr(callee, frame)?;
let args = self.eval_args(args, frame)?;
self.call_value(program, callee_value, args, Some(span))
self.call_value(
program,
callee_value,
args,
Some(span),
Some(&caller_module),
)
}
other => {
let callee_value = self.eval_expr(other, frame)?;
let args = self.eval_args(args, frame)?;
self.call_value(program, callee_value, args, Some(span))
self.call_value(
program,
callee_value,
args,
Some(span),
Some(&caller_module),
)
}
}
}
@@ -583,6 +642,7 @@ impl<'a> Interpreter<'a> {
callee: Value,
args: Vec<EvaluatedArg>,
span: Option<Span>,
caller_module: Option<&ModuleId>,
) -> Result<Value, RuntimeError> {
match callee {
Value::FunctionRef(reference) => {
@@ -597,7 +657,7 @@ impl<'a> Interpreter<'a> {
format!("function reference `{id}` does not exist"),
));
};
self.call_function(program, function, args, span)
self.call_function(program, function, args, span, caller_module)
}
other => Err(RuntimeError::new(
ErrorCategory::NotCallable,
@@ -633,7 +693,10 @@ impl<'a> Interpreter<'a> {
module: &ModuleId,
) -> Result<Value, RuntimeError> {
let context = ExecutionContext::new(module.to_string(), span);
self.host.invoke(op, args, &context)
self.host.invoke(op, args, &context).map_err(|mut error| {
error.mark_callsite();
error
})
}
}