feat(ext): single callable binding, provider v2 CLI, capability handlers, stdlib aliases
Implements RFC-EXT-0001 PR-00..PR-07 first pass: - CallSiteKey/CallBindingTable + one frontend binding pass; runtime dispatch by identity - prepare composition root shared by check/run/inspect/Analysis; exact plugin reconciliation - provider.json v2 --provider/--allow-provider worker CLI with fail-closed verification - capability handler registry with real fs.read and audit - verified stdlib public aliases replace print_override/AST scan - docs: specs, ADR-0054/0055/0057/0058, README, CI/release gates, evidence PR-04 extension selection/lock v4 and final frozen-binary evidence remain.
This commit is contained in:
1 parent
9aa536c2c9
commit
1f6fd644a0
54 files changed
+8710
-828
No files matched your search
@@ -39,7 +39,30 @@ impl AnalysisResult {
|
||||
/// Analyze one Markdown source as a Marqdo module.
|
||||
///
|
||||
/// `module_id` is the canonical project-relative identity (e.g. `index.mq.md`).
|
||||
///
|
||||
/// This standalone entry point also runs the module-local call check so that a
|
||||
/// single document can be validated without a program graph. The CLI
|
||||
/// composition root uses [`analyze_source_for_program`] instead: it defers all
|
||||
/// call resolution to the single program-wide binding pass (ADR-0054).
|
||||
pub fn analyze_source(module_id: &str, source: &str) -> AnalysisResult {
|
||||
analyze_source_inner(module_id, source, true)
|
||||
}
|
||||
|
||||
/// Analyze one Markdown source for the composition root.
|
||||
///
|
||||
/// Identical to [`analyze_source`] except that it does not run the provisional
|
||||
/// module-local call check; the caller binds calls exactly once over the whole
|
||||
/// program (including extension descriptors) and therefore owns the only
|
||||
/// authoritative call diagnostics.
|
||||
pub fn analyze_source_for_program(module_id: &str, source: &str) -> AnalysisResult {
|
||||
analyze_source_inner(module_id, source, false)
|
||||
}
|
||||
|
||||
fn analyze_source_inner(
|
||||
module_id: &str,
|
||||
source: &str,
|
||||
validate_local_calls: bool,
|
||||
) -> AnalysisResult {
|
||||
let id = ModuleId::from(module_id);
|
||||
let document = marqdo_markdown::parse(source);
|
||||
let units = units::build_units(&document, &id);
|
||||
@@ -161,7 +184,9 @@ pub fn analyze_source(module_id: &str, source: &str) -> AnalysisResult {
|
||||
resources,
|
||||
anchors: units.anchors.clone(),
|
||||
};
|
||||
diagnostics.extend(calls::validate_module(&module));
|
||||
if validate_local_calls {
|
||||
diagnostics.extend(calls::validate_module(&module));
|
||||
}
|
||||
|
||||
// One stable order for every consumer: source position first, then the
|
||||
// stable code, as required by `spec/mapping.md` §8. Diagnostics without a
|
||||
|
||||
+498
-248
@@ -1,33 +1,51 @@
|
||||
//! Static call validation.
|
||||
//! Static call resolution and binding (RFC-EXT-0001 §4, ADR-0054).
|
||||
//!
|
||||
//! Calls are resolved after all functions have signatures (including inferred
|
||||
//! ones), so arity/name mistakes are compile errors with source spans instead
|
||||
//! of surprises at runtime. Type errors stay runtime concerns: v1 has no static
|
||||
//! type inference beyond literal checks.
|
||||
//! This module owns the *single* resolution algorithm used by `check`, `run`,
|
||||
//! `inspect` and Analysis: it walks every statically visible call site exactly
|
||||
//! once, resolves it against [`marqdo_ir::callable::CallableRegistry`] and
|
||||
//! records a [`marqdo_ir::call_binding::BoundTarget`] in one
|
||||
//! [`marqdo_ir::call_binding::CallBindingTable`]. The runtime consumes that
|
||||
//! table and never re-resolves a name.
|
||||
//!
|
||||
//! Resolution priority is explicit and order-independent:
|
||||
//!
|
||||
//! | call form | rule |
|
||||
//! |---|---|
|
||||
//! | `f(...)` | local variable (dynamic) → official alias/plugin-fixed builtin → module function (current namespace first, then canonical, then unique local) |
|
||||
//! | `alias.f(...)` | explicit import alias → builtin dotted prefix → exact plugin export |
|
||||
//! | `some_value.f(...)` | dynamic language value only |
|
||||
//!
|
||||
//! Import aliases that collide with a builtin dotted namespace or a visible
|
||||
//! plugin namespace are rejected with `ext.import_namespace_conflict`; declared
|
||||
//! plugin exports that collide with each other or with builtins are rejected
|
||||
//! when the registry is built. The previous "try builtin, then guess plugin at
|
||||
//! runtime" behaviour is gone.
|
||||
//!
|
||||
//! `validate_module` keeps the standalone frontend API (a single document with
|
||||
//! no program graph) for AST/conformance tests: unresolved imports are
|
||||
//! *deferred* there because only the composition root can bind a whole program.
|
||||
|
||||
use std::collections::{BTreeMap, BTreeSet};
|
||||
|
||||
use marqdo_expression::{Arg, Expr, Ident};
|
||||
use marqdo_ir::{builtins, Module, ModuleId, Param, PlaceTarget, Program, SignatureKind, Stmt};
|
||||
use marqdo_ir::builtins;
|
||||
use marqdo_ir::call_binding::{BoundTarget, CallBindingTable, CallSiteKey};
|
||||
use marqdo_ir::callable::{CallableDescriptor, CallableRegistry};
|
||||
use marqdo_ir::{
|
||||
Function, FunctionId, Module, ModuleId, PlaceTarget, Program, SignatureKind, Stmt,
|
||||
};
|
||||
use marqdo_span::Span;
|
||||
use marqdo_value::{codes, Diagnostic};
|
||||
|
||||
/// Signature information needed to validate a call.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct CallableSignature {
|
||||
/// Parameter list.
|
||||
pub params: Vec<Param>,
|
||||
/// Explicit signatures allow positional arguments; inferred do not.
|
||||
pub kind: SignatureKind,
|
||||
}
|
||||
|
||||
/// Build the callable signature table for a module.
|
||||
pub fn signature_table(module: &Module) -> BTreeMap<String, CallableSignature> {
|
||||
/// Build the callable signature table for one module (diagnostics helper).
|
||||
pub fn signature_table(
|
||||
module: &Module,
|
||||
) -> BTreeMap<String, marqdo_ir::callable::CallableSignature> {
|
||||
let mut table = BTreeMap::new();
|
||||
for function in &module.functions {
|
||||
table.insert(
|
||||
function.id.name.clone(),
|
||||
CallableSignature {
|
||||
marqdo_ir::callable::CallableSignature {
|
||||
params: function.params.clone(),
|
||||
kind: function.signature,
|
||||
},
|
||||
@@ -36,10 +54,107 @@ pub fn signature_table(module: &Module) -> BTreeMap<String, CallableSignature> {
|
||||
table
|
||||
}
|
||||
|
||||
/// Validate every call in every function body of the module.
|
||||
/// Result of binding a whole program.
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct BoundProgram {
|
||||
/// One deterministic binding per static call site.
|
||||
pub bindings: CallBindingTable,
|
||||
/// Call-resolution diagnostics, sorted later by the caller.
|
||||
pub diagnostics: Vec<(ModuleId, Diagnostic)>,
|
||||
}
|
||||
|
||||
/// Validate every call in a single module (standalone frontend entry).
|
||||
///
|
||||
/// Imports and plugin exports cannot be resolved without a program graph, so
|
||||
/// those call sites are deferred to the composition-root binding pass instead
|
||||
/// of producing false errors or a second priority order.
|
||||
pub fn validate_module(module: &Module) -> Vec<Diagnostic> {
|
||||
let table = signature_table(module);
|
||||
let mut program = Program::default();
|
||||
program.modules.insert(module.id.clone(), module.clone());
|
||||
program.main_module = Some(module.id.clone());
|
||||
let registry = CallableRegistry::try_from_program(&program)
|
||||
.expect("a single module cannot contain duplicate external declarations");
|
||||
bind_with(module, &program, ®istry, true)
|
||||
.diagnostics
|
||||
.into_iter()
|
||||
.map(|(_, diagnostic)| diagnostic)
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Validate every call in a loaded program (tests and standalone frontend API).
|
||||
///
|
||||
/// The CLI composition root uses [`bind_program_calls`] and keeps the binding
|
||||
/// table; this wrapper keeps the historical API for frontend tests.
|
||||
pub fn validate_program(program: &Program) -> Vec<(ModuleId, Diagnostic)> {
|
||||
let registry = match CallableRegistry::try_from_program(program) {
|
||||
Ok(registry) => registry,
|
||||
Err(_) => CallableRegistry::from_program(program),
|
||||
};
|
||||
bind_program_calls_with(program, ®istry, false).diagnostics
|
||||
}
|
||||
|
||||
/// Resolve every call site in `program` against `registry` exactly once.
|
||||
///
|
||||
/// This is the only binding entry point used by the composition root. The
|
||||
/// returned table must be installed on [`Program::set_call_bindings`] before
|
||||
/// execution.
|
||||
pub fn bind_program_calls(program: &Program, registry: &CallableRegistry) -> BoundProgram {
|
||||
bind_program_calls_with(program, registry, false)
|
||||
}
|
||||
|
||||
fn bind_program_calls_with(
|
||||
program: &Program,
|
||||
registry: &CallableRegistry,
|
||||
standalone: bool,
|
||||
) -> BoundProgram {
|
||||
let mut result = BoundProgram::default();
|
||||
for module in program.modules.values() {
|
||||
let mut bound = bind_with(module, program, registry, standalone);
|
||||
// Conflict diagnostics are produced once per module import, before
|
||||
// any call site; keep them in front of call diagnostics.
|
||||
result.diagnostics.append(&mut bound.diagnostics);
|
||||
for (key, target) in bound.bindings.iter() {
|
||||
result.bindings.insert(key.clone(), target.clone());
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
fn bind_with(
|
||||
module: &Module,
|
||||
program: &Program,
|
||||
registry: &CallableRegistry,
|
||||
standalone: bool,
|
||||
) -> BoundProgram {
|
||||
let mut conflicted = BTreeSet::new();
|
||||
let mut diagnostics = Vec::new();
|
||||
// Reject import aliases that collide with a fixed builtin dotted namespace
|
||||
// or a visible plugin namespace. `alias.f(...)` must have one target.
|
||||
for import in &module.imports {
|
||||
let builtin_conflict = registry.has_builtin_dotted_prefix(&import.alias);
|
||||
let plugin_conflict = registry.has_external_dotted_prefix(&import.alias);
|
||||
if builtin_conflict || plugin_conflict {
|
||||
conflicted.insert(import.alias.clone());
|
||||
let owner = if builtin_conflict {
|
||||
"a builtin dotted namespace"
|
||||
} else {
|
||||
"a declared plugin export namespace"
|
||||
};
|
||||
diagnostics.push((
|
||||
module.id.clone(),
|
||||
Diagnostic::error(
|
||||
codes::EXT_IMPORT_NAMESPACE_CONFLICT,
|
||||
Some(import.span),
|
||||
format!("import alias `{}` conflicts with {owner}", import.alias),
|
||||
)
|
||||
.with_hint(format!(
|
||||
"rename the import alias, for example `{}_lib`, so `{}.…` has one meaning",
|
||||
import.alias, import.alias
|
||||
)),
|
||||
));
|
||||
}
|
||||
}
|
||||
let mut bound = BoundProgram::default();
|
||||
for function in &module.functions {
|
||||
let mut locals: BTreeSet<String> = function
|
||||
.params
|
||||
@@ -47,73 +162,50 @@ pub fn validate_module(module: &Module) -> Vec<Diagnostic> {
|
||||
.map(|param| param.name.clone())
|
||||
.collect();
|
||||
collect_assigned(&function.body, &mut locals);
|
||||
let mut check = CallChecker {
|
||||
current_namespace: function.namespace.clone(),
|
||||
let mut binder = Binder {
|
||||
module,
|
||||
program,
|
||||
registry,
|
||||
function_id: function.id.clone(),
|
||||
namespace: function.namespace.clone(),
|
||||
locals: &locals,
|
||||
table: &table,
|
||||
imports: module
|
||||
.imports
|
||||
.iter()
|
||||
.map(|import| import.alias.clone())
|
||||
.collect(),
|
||||
module_id: module.id.clone(),
|
||||
program: None,
|
||||
cross_only: false,
|
||||
standalone,
|
||||
conflicted: &conflicted,
|
||||
bindings: CallBindingTable::default(),
|
||||
diagnostics: Vec::new(),
|
||||
};
|
||||
check.stmts(&function.body);
|
||||
diagnostics.extend(check.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)),
|
||||
);
|
||||
for param in &function.params {
|
||||
// Signature defaults are compile-time expressions evaluated by the
|
||||
// runtime in the callee's own frame; bind their calls with the
|
||||
// callee identity so the runtime lookup matches.
|
||||
if let Some(default) = ¶m.default {
|
||||
binder.expr(default);
|
||||
}
|
||||
}
|
||||
binder.stmts(&function.body);
|
||||
for (key, target) in binder.bindings.iter() {
|
||||
bound.bindings.insert(key.clone(), target.clone());
|
||||
}
|
||||
diagnostics.extend(
|
||||
binder
|
||||
.diagnostics
|
||||
.into_iter()
|
||||
.map(|diagnostic| (module.id.clone(), diagnostic)),
|
||||
);
|
||||
}
|
||||
BoundProgram {
|
||||
bindings: bound.bindings,
|
||||
diagnostics,
|
||||
}
|
||||
results
|
||||
}
|
||||
|
||||
fn collect_assigned(stmts: &[Stmt], out: &mut BTreeSet<String>) {
|
||||
for stmt in stmts {
|
||||
match stmt {
|
||||
Stmt::Assign {
|
||||
target: PlaceTarget::Local { name, .. },
|
||||
..
|
||||
} => {
|
||||
out.insert(name.clone());
|
||||
Stmt::Assign { target, .. } => {
|
||||
if let PlaceTarget::Local { name, .. } = target {
|
||||
out.insert(name.clone());
|
||||
}
|
||||
}
|
||||
Stmt::Branch { arms, .. } => {
|
||||
for arm in arms {
|
||||
@@ -121,23 +213,25 @@ fn collect_assigned(stmts: &[Stmt], out: &mut BTreeSet<String>) {
|
||||
}
|
||||
}
|
||||
Stmt::Loop { body, .. } => collect_assigned(body, out),
|
||||
_ => {}
|
||||
Stmt::Eval { .. } | Stmt::Return { .. } => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct CallChecker<'a> {
|
||||
current_namespace: Option<String>,
|
||||
struct Binder<'a> {
|
||||
module: &'a Module,
|
||||
program: &'a Program,
|
||||
registry: &'a CallableRegistry,
|
||||
function_id: FunctionId,
|
||||
namespace: Option<String>,
|
||||
locals: &'a BTreeSet<String>,
|
||||
table: &'a BTreeMap<String, CallableSignature>,
|
||||
imports: BTreeSet<String>,
|
||||
module_id: ModuleId,
|
||||
program: Option<&'a Program>,
|
||||
cross_only: bool,
|
||||
standalone: bool,
|
||||
conflicted: &'a BTreeSet<String>,
|
||||
bindings: CallBindingTable,
|
||||
diagnostics: Vec<Diagnostic>,
|
||||
}
|
||||
|
||||
impl<'a> CallChecker<'a> {
|
||||
impl<'a> Binder<'a> {
|
||||
fn stmts(&mut self, stmts: &[Stmt]) {
|
||||
for stmt in stmts {
|
||||
match stmt {
|
||||
@@ -192,205 +286,309 @@ impl<'a> CallChecker<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
fn bind(&mut self, span: Span, target: BoundTarget) {
|
||||
let key = CallSiteKey::new(self.function_id.clone(), span);
|
||||
self.bindings.insert(key, target);
|
||||
}
|
||||
|
||||
fn diagnostic(&mut self, code: &str, span: Span, message: String, hint: Option<String>) {
|
||||
let mut diagnostic = Diagnostic::error(code, Some(span), message);
|
||||
if let Some(hint) = hint {
|
||||
diagnostic = diagnostic.with_hint(hint);
|
||||
}
|
||||
self.diagnostics.push(diagnostic);
|
||||
}
|
||||
|
||||
fn call(&mut self, callee: &Expr, args: &[Arg], span: Span) {
|
||||
match callee {
|
||||
Expr::Ident(Ident { name, .. }) => {
|
||||
if self.cross_only {
|
||||
Expr::Ident(Ident {
|
||||
name,
|
||||
span: name_span,
|
||||
}) => {
|
||||
if self.locals.contains(name) {
|
||||
self.bind(span, BoundTarget::DynamicValue);
|
||||
return;
|
||||
}
|
||||
if let Some(target) = self.registry.alias_target(name).cloned() {
|
||||
if let Some(descriptor) = self.registry.builtin(name) {
|
||||
self.validate_signature(
|
||||
&descriptor.signature,
|
||||
args,
|
||||
name,
|
||||
span,
|
||||
*name_span,
|
||||
);
|
||||
}
|
||||
self.bind(span, BoundTarget::Marqdo { function: target });
|
||||
return;
|
||||
}
|
||||
if let Some(builtin) = builtins::find(name) {
|
||||
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"),
|
||||
self.diagnostic(
|
||||
codes::CALL_ARITY,
|
||||
span,
|
||||
message,
|
||||
Some("check the builtin parameter list".to_string()),
|
||||
);
|
||||
}
|
||||
self.bind(
|
||||
span,
|
||||
BoundTarget::Builtin {
|
||||
canonical_name: builtin.name.to_string(),
|
||||
},
|
||||
);
|
||||
return;
|
||||
}
|
||||
let name_span = match callee {
|
||||
Expr::Ident(Ident { span, .. }) => *span,
|
||||
_ => span,
|
||||
};
|
||||
if let Some(signature) = self.resolve(name) {
|
||||
self.validate_against(name, signature, args, span, name_span);
|
||||
return;
|
||||
match self.resolve_module_function(name) {
|
||||
Resolution::One(function) => {
|
||||
if let Some(target) = self.program.function(&function) {
|
||||
let signature = marqdo_ir::callable::CallableSignature {
|
||||
params: target.params.clone(),
|
||||
kind: target.signature,
|
||||
};
|
||||
self.validate_signature(&signature, args, name, span, *name_span);
|
||||
}
|
||||
self.bind(span, BoundTarget::Marqdo { function });
|
||||
}
|
||||
Resolution::Ambiguous => {
|
||||
self.diagnostic(
|
||||
codes::SYMBOL_UNKNOWN,
|
||||
*name_span,
|
||||
format!("ambiguous function name `{name}`"),
|
||||
Some(
|
||||
"qualify it with its namespace, for example `namespace.函数(...)`"
|
||||
.to_string(),
|
||||
),
|
||||
);
|
||||
}
|
||||
Resolution::None => {
|
||||
if self.standalone
|
||||
&& self
|
||||
.module
|
||||
.imports
|
||||
.iter()
|
||||
.any(|import| import.alias == *name)
|
||||
{
|
||||
// A local may hold import-related values in the
|
||||
// standalone frontend API; defer.
|
||||
return;
|
||||
}
|
||||
self.diagnostic(
|
||||
codes::SYMBOL_UNKNOWN,
|
||||
*name_span,
|
||||
format!("unknown function `{name}`"),
|
||||
Some(
|
||||
"define it as a level-2 heading, call a builtin, or load the \
|
||||
plugin that declares it"
|
||||
.to_string(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
if self.locals.contains(name) || self.imports.contains(name) {
|
||||
// A local may hold a FunctionRef value; resolved at runtime.
|
||||
return;
|
||||
}
|
||||
self.diagnostics.push(
|
||||
Diagnostic::error(
|
||||
codes::SYMBOL_UNKNOWN,
|
||||
Some(name_span),
|
||||
format!("unknown function `{name}`"),
|
||||
)
|
||||
.with_hint("define it as a level-2 heading or call a builtin"),
|
||||
);
|
||||
}
|
||||
Expr::Member { base, name, .. } => {
|
||||
if let Expr::Ident(Ident { name: alias, .. }) = base.as_ref() {
|
||||
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"),
|
||||
if let Expr::Ident(Ident {
|
||||
name: alias,
|
||||
span: alias_span,
|
||||
}) = base.as_ref()
|
||||
{
|
||||
if self.locals.contains(alias) {
|
||||
self.bind(span, BoundTarget::DynamicValue);
|
||||
return;
|
||||
}
|
||||
if self.conflicted.contains(alias) {
|
||||
// The import conflict diagnostic already tells the user
|
||||
// that `alias.…` has no single meaning; do not cascade.
|
||||
return;
|
||||
}
|
||||
if let Some(target_module) =
|
||||
self.program.resolve_import(&self.module.id, alias).cloned()
|
||||
{
|
||||
if let Some(function) = self.registry.function_id(&target_module, name) {
|
||||
if let Some(target) = self.program.function(&function) {
|
||||
let signature = marqdo_ir::callable::CallableSignature {
|
||||
params: target.params.clone(),
|
||||
kind: target.signature,
|
||||
};
|
||||
self.validate_signature(
|
||||
&signature,
|
||||
args,
|
||||
&format!("{alias}.{name}"),
|
||||
span,
|
||||
span,
|
||||
);
|
||||
}
|
||||
self.bind(span, BoundTarget::Marqdo { function });
|
||||
} else {
|
||||
self.diagnostic(
|
||||
codes::SYMBOL_UNKNOWN,
|
||||
span,
|
||||
format!("module `{alias}` has no function `{name}`"),
|
||||
Some(format!(
|
||||
"check `inspect --signature` on the imported module `{alias}`"
|
||||
)),
|
||||
);
|
||||
}
|
||||
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;
|
||||
let qualified = format!("{alias}.{name}");
|
||||
if let Some(builtin) = builtins::find(&qualified) {
|
||||
if let Err(message) = builtins::validate_args(builtin, args) {
|
||||
self.diagnostic(
|
||||
codes::CALL_ARITY,
|
||||
span,
|
||||
message,
|
||||
Some("check the builtin parameter list".to_string()),
|
||||
);
|
||||
}
|
||||
self.bind(
|
||||
span,
|
||||
BoundTarget::Builtin {
|
||||
canonical_name: builtin.name.to_string(),
|
||||
},
|
||||
);
|
||||
return;
|
||||
}
|
||||
if let Some(descriptor) = self.registry.external(&qualified) {
|
||||
self.validate_signature(
|
||||
&descriptor.signature,
|
||||
args,
|
||||
&qualified,
|
||||
span,
|
||||
span,
|
||||
);
|
||||
let param_names = descriptor
|
||||
.signature
|
||||
.params
|
||||
.iter()
|
||||
.map(|param| param.name.clone())
|
||||
.collect();
|
||||
let target = match &descriptor.origin {
|
||||
marqdo_ir::callable::CallableOrigin::AbiPlugin { package, export } => {
|
||||
BoundTarget::AbiPlugin {
|
||||
package: package.clone(),
|
||||
export: export.clone(),
|
||||
digest: descriptor
|
||||
.implementation_digest
|
||||
.clone()
|
||||
.unwrap_or_default(),
|
||||
param_names,
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => BoundTarget::DynamicValue,
|
||||
};
|
||||
self.bind(span, target);
|
||||
return;
|
||||
}
|
||||
// A declared ABI export is a static descriptor: validate
|
||||
// its signature here, without loading the shared library.
|
||||
if let Some(program) = self.program {
|
||||
if let Some(descriptor) = program.external_callable(&qualified) {
|
||||
if let Err(message) =
|
||||
descriptor.signature.validate_args(args, &qualified)
|
||||
{
|
||||
self.diagnostics.push(
|
||||
Diagnostic::error(codes::CALL_ARITY, Some(span), message)
|
||||
.with_hint("check the plugin manifest export signature"),
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if self.standalone {
|
||||
// Without a program graph an import or plugin export
|
||||
// cannot be distinguished statically; defer instead of
|
||||
// emitting a false error.
|
||||
return;
|
||||
}
|
||||
self.diagnostic(
|
||||
codes::SYMBOL_UNKNOWN,
|
||||
*alias_span,
|
||||
format!("unknown function or plugin export `{qualified}`"),
|
||||
Some(
|
||||
"declare the export in a plugin manifest and pass `--plugin` for \
|
||||
`run`, or fix the module import alias"
|
||||
.to_string(),
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
self.bind(span, BoundTarget::DynamicValue);
|
||||
self.expr(base);
|
||||
}
|
||||
other => self.expr(other),
|
||||
}
|
||||
}
|
||||
|
||||
fn resolve(&self, name: &str) -> Option<&'a CallableSignature> {
|
||||
if let Some(namespace) = &self.current_namespace {
|
||||
if let Some(found) = self.table.get(&format!("{namespace}.{name}")) {
|
||||
return Some(found);
|
||||
other => {
|
||||
self.bind(span, BoundTarget::DynamicValue);
|
||||
self.expr(other);
|
||||
}
|
||||
}
|
||||
if let Some(found) = self.table.get(name) {
|
||||
return Some(found);
|
||||
}
|
||||
// Unique local name across namespaces.
|
||||
let mut matches = self
|
||||
.table
|
||||
.iter()
|
||||
.filter(|(key, _)| key.ends_with(&format!(".{name}")))
|
||||
.map(|(_, signature)| signature);
|
||||
let first = matches.next()?;
|
||||
if matches.next().is_some() {
|
||||
None
|
||||
} else {
|
||||
Some(first)
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_against(
|
||||
fn validate_signature(
|
||||
&mut self,
|
||||
name: &str,
|
||||
signature: &CallableSignature,
|
||||
signature: &marqdo_ir::callable::CallableSignature,
|
||||
args: &[Arg],
|
||||
display: &str,
|
||||
call_span: Span,
|
||||
name_span: Span,
|
||||
) {
|
||||
if signature.kind == SignatureKind::Inferred {
|
||||
if let Some(positional) = args.iter().find(|arg| arg.name.is_none()) {
|
||||
self.diagnostics.push(
|
||||
Diagnostic::error(
|
||||
codes::CALL_ARITY,
|
||||
Some(positional.span),
|
||||
format!(
|
||||
"`{name}` has an inferred signature and only accepts named arguments"
|
||||
),
|
||||
)
|
||||
.with_hint("pass arguments as `name = value`"),
|
||||
);
|
||||
if let Err(message) = signature.validate_args(args, display) {
|
||||
// Positional overflow and missing arguments point at the call or
|
||||
// the function name respectively; keep messages deterministic.
|
||||
let span = if message.contains("missing required") {
|
||||
name_span
|
||||
} else {
|
||||
call_span
|
||||
};
|
||||
self.diagnostic(
|
||||
codes::CALL_ARITY,
|
||||
span,
|
||||
message,
|
||||
Some("check `inspect --signature` or the plugin manifest signature".to_string()),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn resolve_module_function(&self, name: &str) -> Resolution {
|
||||
if let Some(namespace) = &self.namespace {
|
||||
let canonical = format!("{namespace}.{name}");
|
||||
if let Some(function) = self
|
||||
.module
|
||||
.functions
|
||||
.iter()
|
||||
.find(|function| function.id.name == canonical)
|
||||
{
|
||||
return Resolution::One(function.id.clone());
|
||||
}
|
||||
}
|
||||
let mut supplied: BTreeSet<&str> = BTreeSet::new();
|
||||
let mut positional = 0usize;
|
||||
for arg in args {
|
||||
match &arg.name {
|
||||
Some(argument_name) => {
|
||||
if !signature
|
||||
.params
|
||||
.iter()
|
||||
.any(|param| param.name == *argument_name)
|
||||
{
|
||||
self.diagnostics.push(
|
||||
Diagnostic::error(
|
||||
codes::CALL_ARITY,
|
||||
Some(arg.span),
|
||||
format!("`{name}` has no parameter named `{argument_name}`"),
|
||||
)
|
||||
.with_hint("check `inspect --signature`"),
|
||||
);
|
||||
}
|
||||
supplied.insert(argument_name.as_str());
|
||||
}
|
||||
None => {
|
||||
if let Some(param) = signature.params.get(positional) {
|
||||
supplied.insert(param.name.as_str());
|
||||
positional += 1;
|
||||
} else {
|
||||
self.diagnostics.push(Diagnostic::error(
|
||||
codes::CALL_ARITY,
|
||||
Some(call_span),
|
||||
format!(
|
||||
"`{name}` accepts at most {} positional argument(s)",
|
||||
signature.params.len()
|
||||
),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(function) = self
|
||||
.module
|
||||
.functions
|
||||
.iter()
|
||||
.find(|function| function.id.name == name)
|
||||
{
|
||||
return Resolution::One(function.id.clone());
|
||||
}
|
||||
for param in &signature.params {
|
||||
if param.default.is_none() && !supplied.contains(param.name.as_str()) {
|
||||
self.diagnostics.push(
|
||||
Diagnostic::error(
|
||||
codes::CALL_ARITY,
|
||||
Some(name_span),
|
||||
format!("`{name}` is missing required argument `{}`", param.name),
|
||||
)
|
||||
.with_hint("pass it by name, e.g. `x = value`"),
|
||||
);
|
||||
}
|
||||
let mut matches = self
|
||||
.module
|
||||
.functions
|
||||
.iter()
|
||||
.filter(|function| function.local_name == name);
|
||||
match (matches.next(), matches.next()) {
|
||||
(Some(function), None) => Resolution::One(function.id.clone()),
|
||||
(None, _) => Resolution::None,
|
||||
(Some(_), Some(_)) => Resolution::Ambiguous,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum Resolution {
|
||||
One(FunctionId),
|
||||
Ambiguous,
|
||||
None,
|
||||
}
|
||||
|
||||
/// Whether a descriptor is a statically declared ABI plugin export.
|
||||
pub fn is_abi_export(descriptor: &CallableDescriptor) -> bool {
|
||||
matches!(
|
||||
descriptor.origin,
|
||||
marqdo_ir::callable::CallableOrigin::AbiPlugin { .. }
|
||||
)
|
||||
}
|
||||
|
||||
/// Whether a function signature requires explicit named/positional checking.
|
||||
pub fn _signature_kind(signature: &marqdo_ir::callable::CallableSignature) -> SignatureKind {
|
||||
signature.kind
|
||||
}
|
||||
|
||||
/// Helper used by tests and by `inspect` to render a function body trace.
|
||||
#[allow(dead_code)]
|
||||
fn _unused(_: &Function, _: &Program) {}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -494,11 +692,36 @@ mod tests {
|
||||
package: "demo".into(),
|
||||
export: "solver.add".into(),
|
||||
},
|
||||
implementation_digest: None,
|
||||
implementation_digest: Some("a".repeat(64)),
|
||||
source_span: None,
|
||||
});
|
||||
let diagnostics = validate_program(&program);
|
||||
assert!(diagnostics.iter().any(|(_, d)| d.code == codes::CALL_ARITY));
|
||||
let registry = CallableRegistry::try_from_program(&program).unwrap();
|
||||
let bound = bind_program_calls(&program, ®istry);
|
||||
assert!(bound
|
||||
.diagnostics
|
||||
.iter()
|
||||
.any(|(_, d)| d.code == codes::CALL_ARITY));
|
||||
// A valid positional/named mix binds to the exact export identity.
|
||||
let module = make_module(vec![function(
|
||||
"main",
|
||||
&[],
|
||||
SignatureKind::Explicit,
|
||||
vec![return_call("solver.add(1, b=2)")],
|
||||
)]);
|
||||
program.modules.insert(module.id.clone(), module);
|
||||
let registry = CallableRegistry::try_from_program(&program).unwrap();
|
||||
let bound = bind_program_calls(&program, ®istry);
|
||||
assert!(bound.diagnostics.is_empty(), "{:?}", bound.diagnostics);
|
||||
let target = bound.bindings.iter().next().unwrap().1;
|
||||
match target {
|
||||
BoundTarget::AbiPlugin {
|
||||
package, export, ..
|
||||
} => {
|
||||
assert_eq!(package, "demo");
|
||||
assert_eq!(export, "solver.add");
|
||||
}
|
||||
other => panic!("unexpected target {other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -556,4 +779,31 @@ mod tests {
|
||||
.iter()
|
||||
.any(|d| d.code == codes::CALL_ARITY));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn import_alias_conflicting_with_builtin_namespace_is_rejected() {
|
||||
let module = Module {
|
||||
id: ModuleId::from("index.mq.md"),
|
||||
units: vec![],
|
||||
functions: vec![function(
|
||||
"main",
|
||||
&[],
|
||||
SignatureKind::Explicit,
|
||||
vec![return_call("math.eval(x=1)")],
|
||||
)],
|
||||
imports: vec![marqdo_ir::ImportDecl {
|
||||
alias: "math".into(),
|
||||
target: "./math.mq.md".into(),
|
||||
span: Span::new(0, 1),
|
||||
}],
|
||||
resources: vec![],
|
||||
anchors: std::collections::BTreeMap::new(),
|
||||
};
|
||||
let diagnostics = validate_module(&module);
|
||||
assert!(diagnostics
|
||||
.iter()
|
||||
.any(|d| d.code == codes::EXT_IMPORT_NAMESPACE_CONFLICT));
|
||||
// Standalone mode defers unresolved imports, but the composition root
|
||||
// run is covered by the CLI black-box CALL-01 test.
|
||||
}
|
||||
}
|
||||
@@ -14,5 +14,5 @@ pub mod resources;
|
||||
pub mod signature;
|
||||
pub mod units;
|
||||
|
||||
pub use analyze::{analyze_source, AnalysisResult};
|
||||
pub use analyze::{analyze_source, analyze_source_for_program, AnalysisResult};
|
||||
pub use units::{build_units, github_slug, FunctionScope, HeadingInfo, UnitIndex};
|
||||
Reference in new issue
Block a user