Files
cflmy a67f344b6b ext(00-08): RFC-EXT-0003 core/extension boundary, math detach and release stdlib
Implements issue #3 (RFC-EXT-0003) slices on the post-3.0.0-beta.1 tree.

PR-00/01/H-06: reproduce and fix sibling heading mode inheritance.
build_units now pops the current and same-or-higher-level headings from the
ancestor stack before deriving the inherited mode, so an unmarked heading can never
inherit the previous sibling. Covered by the units matrix, conformance fixtures
22/23, and black-box heading_inheritance through check --emit/run/call.

PR-02/H-08: the deterministic release archive now ships the complete verified
stdlib/ tree. package.py and smoke.py recompute the runtime tree/alias digests,
record them in RELEASE_INFO.json.stdlib, and fail on mismatch; smoke removes the
bundle and requires a fail-closed run. Release discovery no longer falls back to the
compile-time source tree (debug builds keep that development convenience).

PR-03/H-04: one marqdo_host::CORE_RESOURCE_KINDS registry is used by both the CLI
provider-manifest validator and every trusted host registration path; all six core
kinds are rejected as provider name or declared kind before registration.
H-03: docs/architecture/resource-operations.md records every reviewed
resource_request operation and architecture_boundaries pins the exact allowlist; new
domain operations are forbidden in core (ADR-0062).

PR-04/H-01: remove math.parse/eval/substitute/simplify/differentiate from
BUILTINS, HostOp, the runtime, the host kernel and the workspace
(crates/marqdo-math deleted). Formula stays inert; Int/Float arithmetic stays.
Math algorithms are a pure examples/math-library package plus an optional
examples/math-backend ABI plugin that requests the math.compute capability
(ADR-0061). Removed calls are static symbol.unknown; the math import namespace is
released again. Tests/examples/specs migrated; new math_library black-box test.

PR-05/06 Step A: ADR-0060 classifies @exec/@knowledge as the closed Core Modes
and specifies the Proposed metadata-only Heading Annotations v1 (target 3.1,
sem.invalid_mode_marker unchanged, Analysis v2 unchanged). ADR-0063 records the
foreign F-A retention and deferred F-B removal.

Docs/status/compatibility/changelog and the RFC-EXT-0003 evidence file are updated.
Full local gate: cargo fmt PASS, clippy -D warnings PASS, workspace tests 586
passed/0 failed/2 ignored, python tools/ci 28 passed.
2026-11-22 18:57:33 +08:00

440 lines
15 KiB
Rust
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! Static parameter inference via definite-assignment data-flow analysis.
//!
//! Parameters come only from free reads of executable expressions. Prose,
//! headings, inert code spans and natural language never introduce parameters.
//!
//! The analysis tracks, for every program point:
//! * `must` – variables definitely assigned on every path from entry;
//! * `may` – variables assigned on at least one path;
//! * `ambiguous` – variables that may have been assigned before the read on
//! some path but not on all paths, which makes the variable's identity
//! ambiguous (blueprint S-04) rather than silently inventing a parameter.
//!
//! Loop bodies get special treatment: their first iteration starts from the
//! pre-loop state, so a read of an entry variable inside a loop is a parameter
//! candidate even though the body also assigns it (the classic counter case).
use std::collections::{BTreeMap, BTreeSet};
use marqdo_expression::{Arg, Expr, Ident, RecordEntry};
use marqdo_ir::{BranchArm, PlaceTarget, Stmt};
use marqdo_span::Span;
use marqdo_value::{codes, Diagnostic};
use crate::signature::Signature;
/// Result of the inference analysis for one function.
#[derive(Debug, Clone, Default)]
pub struct Inference {
/// Candidate parameters sorted by canonical name.
pub params: Vec<ParamCandidate>,
/// Diagnostics (ambiguity errors, explicit-signature unknown reads).
pub diagnostics: Vec<Diagnostic>,
}
/// One inferred parameter candidate.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParamCandidate {
/// Canonical parameter name.
pub name: String,
/// Span of the first free read.
pub span: Span,
}
#[derive(Debug, Clone, Default)]
struct State {
must: BTreeSet<String>,
may: BTreeSet<String>,
ambiguous: BTreeSet<String>,
}
impl State {
fn join(&self, other: &State) -> State {
State {
must: self.must.intersection(&other.must).cloned().collect(),
may: self.may.union(&other.may).cloned().collect(),
ambiguous: self.ambiguous.union(&other.ambiguous).cloned().collect(),
}
}
}
struct Analyzer<'a> {
/// Names that are parameters/locals/builtins/imports and therefore never
/// reported as unknown; used for explicit signatures.
known: Option<&'a BTreeSet<String>>,
reads: BTreeMap<String, Span>,
diagnostics: Vec<Diagnostic>,
ambiguous_reported: BTreeSet<String>,
unknown_reported: BTreeSet<String>,
}
impl<'a> Analyzer<'a> {
fn new(known: Option<&'a BTreeSet<String>>) -> Self {
Self {
known,
reads: BTreeMap::new(),
diagnostics: Vec::new(),
ambiguous_reported: BTreeSet::new(),
unknown_reported: BTreeSet::new(),
}
}
fn read_name(&mut self, name: &str, span: Span, state: &State) {
if state.must.contains(name) {
return;
}
if state.ambiguous.contains(name) {
if !self.ambiguous_reported.contains(name) {
self.ambiguous_reported.insert(name.to_string());
self.diagnostics.push(
Diagnostic::error(
codes::SYMBOL_UNINITIALIZED,
Some(span),
format!("`{name}` may be read before it is assigned"),
)
.with_hint(
"assign it on every path before this read, or pass it in as a parameter",
),
);
}
return;
}
if let Some(known) = self.known {
if known.contains(name) {
return;
}
if !self.unknown_reported.contains(name) {
self.unknown_reported.insert(name.to_string());
self.diagnostics.push(
Diagnostic::error(
codes::SYMBOL_UNKNOWN,
Some(span),
format!("unknown variable `{name}`"),
)
.with_hint("declare it in the explicit signature or assign it first"),
);
}
return;
}
self.reads.entry(name.to_string()).or_insert(span);
}
fn read_expr(&mut self, expr: &Expr, state: &State) {
match expr {
Expr::Int(_) | Expr::Float(_) | Expr::Text(_) | Expr::Bool(_) | Expr::None => {}
Expr::Ident(Ident { name, span }) => self.read_name(name, *span, state),
Expr::List(items) => {
for item in items {
self.read_expr(item, state);
}
}
Expr::Record(entries) => {
for RecordEntry { value, .. } in entries {
self.read_expr(value, state);
}
}
Expr::Unary { operand, .. } => self.read_expr(operand, state),
Expr::Binary { left, right, .. } => {
self.read_expr(left, state);
self.read_expr(right, state);
}
Expr::Call { callee, args, .. } => {
// A callee identifier names a function; a `ns.name` callee is a
// builtin namespace (resource.request) or an imported module alias.
// Neither is a variable read.
match callee.as_ref() {
Expr::Ident(_) => {}
Expr::Member { base, name, .. } => {
// `ns.f(...)` bases are namespaces: builtin namespaces,
// imported module aliases or explicitly trusted plugin
// namespaces bound at run time. They are never inferred
// parameters. Non-identifier bases are values and are
// still read normally.
let _ = name;
if !matches!(base.as_ref(), Expr::Ident(_)) {
self.read_expr(base, state);
}
}
other => self.read_expr(other, state),
}
for Arg { value, .. } in args {
self.read_expr(value, state);
}
}
Expr::Member { base, .. } => self.read_expr(base, state),
Expr::Index { base, index, .. } => {
self.read_expr(base, state);
self.read_expr(index, state);
}
}
}
fn assign(&self, name: &str, state: &mut State) {
state.must.insert(name.to_string());
state.may.insert(name.to_string());
state.ambiguous.insert(name.to_string());
}
fn analyze_stmts(&mut self, stmts: &[Stmt], mut state: State) -> State {
for stmt in stmts {
state = match stmt {
Stmt::Assign { target, value, .. } => {
match target {
PlaceTarget::Local { name, .. } => {
// Preserve v1 order: the RHS is read before the
// local rebinding makes the name definitely
// assigned.
self.read_expr(value, &state);
self.assign(name, &mut state);
}
PlaceTarget::Index { base, index, .. } => {
// Runtime order for an index place is base, index,
// RHS; reads in the place are free reads like any
// other.
self.read_expr(base, &state);
self.read_expr(index, &state);
self.read_expr(value, &state);
}
}
state
}
Stmt::Eval { expr, .. } => {
self.read_expr(expr, &state);
state
}
Stmt::Return { value, .. } => {
self.read_expr(value, &state);
state
}
Stmt::Branch { arms, .. } => self.analyze_branch(arms, state),
Stmt::Loop {
condition, body, ..
} => {
self.read_expr(condition, &state);
// First iteration starts from the pre-loop state: body
// assignments must not flag pre-loop reads as ambiguous.
let body_out = self.analyze_stmts(
body,
State {
must: state.must.clone(),
may: state.may.clone(),
ambiguous: state.ambiguous.clone(),
},
);
State {
must: state.must.clone(),
may: body_out
.may
.union(&state.may)
.cloned()
.collect::<BTreeSet<_>>(),
ambiguous: body_out
.ambiguous
.union(&state.ambiguous)
.cloned()
.collect::<BTreeSet<_>>(),
}
}
};
}
state
}
fn analyze_branch(&mut self, arms: &[BranchArm], state: State) -> State {
// Conditions are evaluated in order against the incoming state.
for arm in arms {
self.read_expr(&arm.condition, &state);
}
// The branch may match no arm, so the incoming state joins every arm's
// state; variables assigned only inside an arm stay "may assigned".
let mut joined: Option<State> = Some(State {
must: state.must.clone(),
may: state.may.clone(),
ambiguous: state.ambiguous.clone(),
});
for arm in arms {
let arm_out = self.analyze_stmts(
&arm.body,
State {
must: state.must.clone(),
may: state.may.clone(),
ambiguous: state.ambiguous.clone(),
},
);
joined = Some(match joined {
None => arm_out,
Some(previous) => previous.join(&arm_out),
});
}
match joined {
Some(joined) => joined,
None => state,
}
}
}
/// Infer parameters for a function with no explicit signature.
pub fn infer(
stmts: &[Stmt],
signature: Option<&Signature>,
imports: &BTreeSet<String>,
) -> Inference {
let known = signature.map(|signature| {
let mut names: BTreeSet<String> = signature
.params
.iter()
.map(|param| param.name.clone())
.collect();
names.extend(imports.iter().cloned());
names.extend(
marqdo_ir::builtins::BUILTINS
.iter()
.map(|builtin| builtin.name.to_string()),
);
names
});
let mut analyzer = Analyzer::new(known.as_ref());
analyzer.analyze_stmts(stmts, State::default());
let mut params: Vec<ParamCandidate> = analyzer
.reads
.iter()
.filter(|(name, _)| {
// Builtins and imported aliases are never parameters.
marqdo_ir::builtins::find(name).is_none() && !imports.contains(*name)
})
.map(|(name, span)| ParamCandidate {
name: name.clone(),
span: *span,
})
.collect();
params.sort_by(|a, b| a.name.cmp(&b.name));
Inference {
params,
diagnostics: analyzer.diagnostics,
}
}
#[cfg(test)]
mod tests {
use super::*;
use marqdo_expression::parse_expression;
fn assign(name: &str, value: &str) -> Stmt {
Stmt::Assign {
target: PlaceTarget::Local {
name: name.to_string(),
name_span: Span::new(0, 0),
},
value: parse_expression(value).unwrap(),
span: Span::new(0, 0),
}
}
fn ret(expr: &str) -> Stmt {
Stmt::Return {
value: parse_expression(expr).unwrap(),
span: Span::new(0, 0),
}
}
fn params(stmts: &[Stmt]) -> Vec<String> {
infer(stmts, None, &BTreeSet::new())
.params
.into_iter()
.map(|candidate| candidate.name)
.collect()
}
#[test]
fn index_assignment_reads_its_base_and_index_without_binding_a_local() {
let stmts = vec![
Stmt::Assign {
target: PlaceTarget::Index {
base: parse_expression("m").unwrap(),
index: parse_expression("i").unwrap(),
span: Span::new(0, 0),
},
value: parse_expression("9").unwrap(),
span: Span::new(0, 0),
},
ret("m[0]"),
];
// `m` and `i` are free reads; the place never rebinds `m` as a local.
assert_eq!(params(&stmts), vec!["i".to_string(), "m".to_string()]);
}
#[test]
fn infers_free_reads() {
// n * 阶乘(n=n-1)
let call: Expr = parse_expression("n * 阶乘(n=n-1)").unwrap();
let stmts = vec![Stmt::Return {
value: call,
span: Span::new(0, 0),
}];
assert_eq!(params(&stmts), vec!["n".to_string()]);
}
#[test]
fn unconditional_entry_assignment_is_local() {
let stmts = vec![assign("n", "3"), ret("n")];
assert!(params(&stmts).is_empty());
}
#[test]
fn loop_counter_is_parameter() {
let stmts = vec![
Stmt::Loop {
condition: parse_expression("n > 0").unwrap(),
condition_span: Span::new(0, 0),
body: vec![assign("n", "n - 1")],
span: Span::new(0, 0),
},
ret("n"),
];
assert_eq!(params(&stmts), vec!["n".to_string()]);
}
#[test]
fn partial_branch_assignment_is_ambiguous() {
let stmts = vec![
Stmt::Branch {
arms: vec![BranchArm {
condition: parse_expression("true").unwrap(),
condition_span: Span::new(0, 0),
body: vec![assign("x", "1")],
span: Span::new(0, 0),
}],
span: Span::new(0, 0),
},
ret("x"),
];
let inference = infer(&stmts, None, &BTreeSet::new());
assert!(inference.params.is_empty());
assert!(inference
.diagnostics
.iter()
.any(|d| d.code == codes::SYMBOL_UNINITIALIZED));
}
#[test]
fn explicit_signature_reports_unknown_reads() {
let signature = Signature {
params: vec![marqdo_ir::Param {
name: "r".into(),
span: Span::new(0, 0),
type_annotation: None,
default: None,
}],
span: Span::new(0, 0),
diagnostics: Vec::new(),
};
let stmts = vec![ret("r * pi * typo")];
let inference = infer(&stmts, Some(&signature), &BTreeSet::new());
assert!(inference.params.is_empty());
assert!(inference
.diagnostics
.iter()
.any(|d| d.code == codes::SYMBOL_UNKNOWN && d.message.contains("typo")));
}
}