feat(frontend): cross-module call validation and builtin namespace inference
- calls: validate_program runs after a whole project is loaded and checks qualified calls through explicit import aliases against the imported module's signatures (missing function, missing/unknown arguments), plus qualified builtin calls such as math.eval; local calls stay checked by validate_module so diagnostics are not duplicated. - infer: `math.eval(...)` / `foreign.call(...)` callee namespaces are builtin namespaces, not free variable reads, so they no longer become inferred parameters or unknown-variable errors. Explicit signatures behave likewise.
This commit is contained in:
1 parent
3f8dce013b
commit
d54fda9e78
2 files changed
+109
-6
No files matched your search
@@ -8,7 +8,7 @@
|
|||||||
use std::collections::{BTreeMap, BTreeSet};
|
use std::collections::{BTreeMap, BTreeSet};
|
||||||
|
|
||||||
use marqdo_expression::{Arg, Expr, Ident};
|
use marqdo_expression::{Arg, Expr, Ident};
|
||||||
use marqdo_ir::{builtins, Module, Param, SignatureKind, Stmt};
|
use marqdo_ir::{builtins, Module, ModuleId, Param, Program, SignatureKind, Stmt};
|
||||||
use marqdo_span::Span;
|
use marqdo_span::Span;
|
||||||
use marqdo_value::{codes, Diagnostic};
|
use marqdo_value::{codes, Diagnostic};
|
||||||
|
|
||||||
@@ -56,6 +56,9 @@ pub fn validate_module(module: &Module) -> Vec<Diagnostic> {
|
|||||||
.iter()
|
.iter()
|
||||||
.map(|import| import.alias.clone())
|
.map(|import| import.alias.clone())
|
||||||
.collect(),
|
.collect(),
|
||||||
|
module_id: module.id.clone(),
|
||||||
|
program: None,
|
||||||
|
cross_only: false,
|
||||||
diagnostics: Vec::new(),
|
diagnostics: Vec::new(),
|
||||||
};
|
};
|
||||||
check.stmts(&function.body);
|
check.stmts(&function.body);
|
||||||
@@ -64,6 +67,45 @@ pub fn validate_module(module: &Module) -> Vec<Diagnostic> {
|
|||||||
diagnostics
|
diagnostics
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Validate cross-module and qualified-builtin calls once a whole program is
|
||||||
|
/// loaded. Local calls are already validated by [`validate_module`].
|
||||||
|
pub fn validate_program(program: &Program) -> Vec<(ModuleId, Diagnostic)> {
|
||||||
|
let mut results = Vec::new();
|
||||||
|
for module in program.modules.values() {
|
||||||
|
let table = signature_table(module);
|
||||||
|
for function in &module.functions {
|
||||||
|
let mut locals: BTreeSet<String> = function
|
||||||
|
.params
|
||||||
|
.iter()
|
||||||
|
.map(|param| param.name.clone())
|
||||||
|
.collect();
|
||||||
|
collect_assigned(&function.body, &mut locals);
|
||||||
|
let mut check = CallChecker {
|
||||||
|
current_namespace: function.namespace.clone(),
|
||||||
|
locals: &locals,
|
||||||
|
table: &table,
|
||||||
|
imports: module
|
||||||
|
.imports
|
||||||
|
.iter()
|
||||||
|
.map(|import| import.alias.clone())
|
||||||
|
.collect(),
|
||||||
|
module_id: module.id.clone(),
|
||||||
|
program: Some(program),
|
||||||
|
cross_only: true,
|
||||||
|
diagnostics: Vec::new(),
|
||||||
|
};
|
||||||
|
check.stmts(&function.body);
|
||||||
|
results.extend(
|
||||||
|
check
|
||||||
|
.diagnostics
|
||||||
|
.into_iter()
|
||||||
|
.map(|diagnostic| (module.id.clone(), diagnostic)),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
results
|
||||||
|
}
|
||||||
|
|
||||||
fn collect_assigned(stmts: &[Stmt], out: &mut BTreeSet<String>) {
|
fn collect_assigned(stmts: &[Stmt], out: &mut BTreeSet<String>) {
|
||||||
for stmt in stmts {
|
for stmt in stmts {
|
||||||
match stmt {
|
match stmt {
|
||||||
@@ -86,6 +128,9 @@ struct CallChecker<'a> {
|
|||||||
locals: &'a BTreeSet<String>,
|
locals: &'a BTreeSet<String>,
|
||||||
table: &'a BTreeMap<String, CallableSignature>,
|
table: &'a BTreeMap<String, CallableSignature>,
|
||||||
imports: BTreeSet<String>,
|
imports: BTreeSet<String>,
|
||||||
|
module_id: ModuleId,
|
||||||
|
program: Option<&'a Program>,
|
||||||
|
cross_only: bool,
|
||||||
diagnostics: Vec<Diagnostic>,
|
diagnostics: Vec<Diagnostic>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -147,6 +192,9 @@ impl<'a> CallChecker<'a> {
|
|||||||
fn call(&mut self, callee: &Expr, args: &[Arg], span: Span) {
|
fn call(&mut self, callee: &Expr, args: &[Arg], span: Span) {
|
||||||
match callee {
|
match callee {
|
||||||
Expr::Ident(Ident { name, .. }) => {
|
Expr::Ident(Ident { name, .. }) => {
|
||||||
|
if self.cross_only {
|
||||||
|
return;
|
||||||
|
}
|
||||||
if let Some(builtin) = builtins::find(name) {
|
if let Some(builtin) = builtins::find(name) {
|
||||||
if let Err(message) = builtins::validate_args(builtin, args) {
|
if let Err(message) = builtins::validate_args(builtin, args) {
|
||||||
self.diagnostics.push(
|
self.diagnostics.push(
|
||||||
@@ -177,9 +225,52 @@ impl<'a> CallChecker<'a> {
|
|||||||
.with_hint("define it as a level-2 heading or call a builtin"),
|
.with_hint("define it as a level-2 heading or call a builtin"),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
// Qualified calls are validated once the imported module has been
|
Expr::Member { base, name, .. } => {
|
||||||
// analysed by the loader; argument expressions are still checked.
|
if let Expr::Ident(Ident { name: alias, .. }) = base.as_ref() {
|
||||||
Expr::Member { base, .. } => self.expr(base),
|
let qualified = format!("{}.{name}", alias);
|
||||||
|
if let Some(builtin) = builtins::find(&qualified) {
|
||||||
|
if let Err(message) = builtins::validate_args(builtin, args) {
|
||||||
|
self.diagnostics.push(
|
||||||
|
Diagnostic::error(codes::CALL_ARITY, Some(span), message)
|
||||||
|
.with_hint("check the builtin parameter list"),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if self.imports.contains(alias) {
|
||||||
|
if let Some(program) = self.program {
|
||||||
|
if let Some(target_module) =
|
||||||
|
program.resolve_import(&self.module_id, alias)
|
||||||
|
{
|
||||||
|
if let Some(target) = program.module(target_module) {
|
||||||
|
if let Some(function) = target.function(name) {
|
||||||
|
let signature = CallableSignature {
|
||||||
|
params: function.params.clone(),
|
||||||
|
kind: function.signature,
|
||||||
|
};
|
||||||
|
self.validate_against(
|
||||||
|
&qualified, &signature, args, span, span,
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
self.diagnostics.push(
|
||||||
|
Diagnostic::error(
|
||||||
|
codes::SYMBOL_UNKNOWN,
|
||||||
|
Some(span),
|
||||||
|
format!("module `{alias}` has no function `{name}`"),
|
||||||
|
)
|
||||||
|
.with_hint(
|
||||||
|
"check `inspect --signature` on the imported module",
|
||||||
|
),
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self.expr(base);
|
||||||
|
}
|
||||||
other => self.expr(other),
|
other => self.expr(other),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -139,9 +139,21 @@ impl<'a> Analyzer<'a> {
|
|||||||
self.read_expr(right, state);
|
self.read_expr(right, state);
|
||||||
}
|
}
|
||||||
Expr::Call { callee, args, .. } => {
|
Expr::Call { callee, args, .. } => {
|
||||||
// A bare identifier callee names a function, not a variable.
|
// A callee identifier names a function; a `ns.name` callee is a
|
||||||
|
// builtin namespace (math.eval) or an imported module alias.
|
||||||
|
// Neither is a variable read.
|
||||||
match callee.as_ref() {
|
match callee.as_ref() {
|
||||||
Expr::Ident(_) => {}
|
Expr::Ident(_) => {}
|
||||||
|
Expr::Member { base, name, .. } => {
|
||||||
|
let is_builtin_namespace = matches!(
|
||||||
|
base.as_ref(),
|
||||||
|
Expr::Ident(ident)
|
||||||
|
if marqdo_ir::builtins::find(&format!("{}.{name}", ident.name)).is_some()
|
||||||
|
);
|
||||||
|
if !is_builtin_namespace {
|
||||||
|
self.read_expr(base, state);
|
||||||
|
}
|
||||||
|
}
|
||||||
other => self.read_expr(other, state),
|
other => self.read_expr(other, state),
|
||||||
}
|
}
|
||||||
for Arg { value, .. } in args {
|
for Arg { value, .. } in args {
|
||||||
@@ -295,7 +307,7 @@ pub fn infer(
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use marqdo_expression::{parse_expression, parse_statement};
|
use marqdo_expression::parse_expression;
|
||||||
|
|
||||||
fn assign(name: &str, value: &str) -> Stmt {
|
fn assign(name: &str, value: &str) -> Stmt {
|
||||||
Stmt::Assign {
|
Stmt::Assign {
|
||||||
|
|||||||
Reference in new issue
Block a user