feat(codegen): pre-resolve the App callee in MIR; delete is_static_callee + type_home_module (mir.2)
Second re-deriver removal of the typed-MIR milestone (spec
docs/specs/0060-typed-mir.md, plan docs/plans/0116-mir.2-callee-static.md).
Static-callee resolution moves out of codegen and into lower_to_mir:
every Term::App callee is classified once, mirroring check's own synth
Term::Var ladder (lib.rs:3409-3582), and emitted as a resolved Callee.
Codegen reads the callee identity off MIR and routes each kind to the
right lowering. The codegen re-derivers is_static_callee and
type_home_module are deleted, and the lower_app <-> is_static_callee
lockstep pair is struck from CLAUDE.md.
Callee reshape (beyond the spec's original {module, fn_name} sketch —
spec 0060's Concrete-code-shapes block is updated in this iteration):
pub enum Callee {
Static { module, fn_name, sig: Type }, // user/prelude fn -> emit_call, module pre-resolved
Builtin { name, sig: Type }, // operator/intrinsic -> inline opcode lowering
Indirect(Box<MTerm>), // dynamic: fn-pointer / closure / shadowed name
}
Two forced refinements, both settled in planning:
- sig:Type on each resolved variant. drop::synth_callee_ret_mode reads
the callee ret_mode, today off Callee::Indirect(inner).ty(). A
resolved callee has no sub-term, so the sig (= synth_pure(callee),
mode-preserving) is the lossless replacement. This is NOT a mir.3
pull-forward: the MArg param-mode / consume_count fields stay at their
mir.1 defaults. Without it, every statically-resolved call would lose
its drop signal and the RawBuf / int_to_str RC-stats pins mir.1b fixed
would regress.
- Builtin variant. Operators and the str-num builtins (+, not,
str_concat, ...) are lowered inline by name and have no module/fn_name;
a separate variant is cleaner than a sentinel module. The classifier
decides Builtin vs Static from CHECK'S OWN resolution (a name in
env.globals with no owning module is a builtin), never a copy of
codegen's old is_static_callee allowlist. The recon's divergence audit
confirmed check's builtin set and codegen's inline-arm set match
exactly, so the single-engine rule is mechanically satisfiable with no
env threading gap.
type_home_module is fully deletable: a workspace-wide grep confirmed no
program references a type-scoped T.fn as a value, so its only other
consumer (resolve_top_level_fn) collapses to the module-name fallback
with no behaviour change. (The spec's "x3 mirrors" wording over-counted;
the live surface was the definition plus two call sites.)
Alternatives rejected during planning: sentinel module in Static
(ugly); builtins left as Indirect (breaks — no fn-pointer for an
operator); name-rewrite Static{name,sig} keeping lower_app's by-name
dispatch (blurs builtin/user and still needs sig). The mir.1b
re-synth-strictness trap (post-mono synth_pure re-unify surfacing
mode-strip / bare-vs-qualified / metavar leaks) did NOT fire under the
new producer path — qualify_local_types mode-preservation and the $u
wildcard normalisation from mir.1b held.
Verification (orchestrator, post-implement inspect): diff matches the
plan; grep-clean for is_static_callee + type_home_module across
ailang-codegen and ail; cargo build --workspace green; cargo test
--workspace 700 passed / 0 failed / 3 ignored (no #[ignore] added this
iteration — the 3 are the pre-existing #49 Str-leg pin + two doctests).
Acceptance witnesses green: #53 (mono_new_over_user_adt_builds_and_runs),
#51 (rawbuf_new_int_size_only_builds_and_runs still builds), show_print
cross-module (print_user_adt_runs_end_to_end), the new mir.2 pins
(mir2_resolved_callee_kinds_run_end_to_end, callee_classification_*,
strengthened new_over_user_adt_carries_node_types asserting Callee::Static).
#49 heap-Str loop binder stays #[ignore] (lifts at mir.4).
Two new examples/ fixtures back the new pins (classify_pin.ail for the
producer classification pin, mir2_callee_kinds.ail for the E2E),
ail-parse / round-trip clean.
Forward note (cycle-close architect): crates/ail/tests/codegen_import_map_fallback_pin.rs:14-15
doc prose names lower_app's cross-module arm and synth_with_extras as
failure-mode targets — both now deleted (mir.2 / mir.1b). The pin's
protected invariant is still valid and green; the prose is stale and
wants a doc-honesty reword, left out of this iteration's footprint.
This commit is contained in:
@@ -106,6 +106,75 @@ fn arg(term: MTerm) -> MArg {
|
||||
MArg { term, mode: Mode::Owned, consume_count: 1 }
|
||||
}
|
||||
|
||||
/// The resolved identity of an App callee, mirroring synth's
|
||||
/// `Term::Var` resolution ladder (`lib.rs:3409-3582`). `lower_term`
|
||||
/// turns this into the matching `Callee` variant. Resolution uses
|
||||
/// check's own env (the single engine) — never a copy of codegen's
|
||||
/// `is_static_callee` allowlist.
|
||||
enum CalleeClass {
|
||||
Builtin { name: String },
|
||||
Static { module: String, fn_name: String },
|
||||
Indirect,
|
||||
}
|
||||
|
||||
fn classify_callee(ctx: &Ctx, callee: &Term) -> CalleeClass {
|
||||
// A non-`Var` callee (lambda, applied expression, …) is dynamic.
|
||||
let name = match callee {
|
||||
Term::Var { name } => name.clone(),
|
||||
_ => return CalleeClass::Indirect,
|
||||
};
|
||||
// rung 1: shadowed by a local binder → dynamic (synth `lib.rs:3409`).
|
||||
if ctx.locals.contains_key(&name) {
|
||||
return CalleeClass::Indirect;
|
||||
}
|
||||
// rung 5: dotted `T.fn` / `Mod.fn` — the TypeDef-first ladder
|
||||
// (synth `lib.rs:3557-3582`). `T.fn` resolves to T's home module;
|
||||
// `Mod.fn` to the imported module. This replaces codegen's
|
||||
// `type_home_module`.
|
||||
if name.matches('.').count() == 1 {
|
||||
let (prefix, suffix) = name.split_once('.').expect("checked");
|
||||
let type_home = ctx
|
||||
.env
|
||||
.module_types
|
||||
.iter()
|
||||
.find_map(|(m, types)| types.contains_key(prefix).then(|| m.clone()));
|
||||
let target = type_home.or_else(|| ctx.env.imports.get(prefix).cloned());
|
||||
return match target {
|
||||
Some(module) => CalleeClass::Static { module, fn_name: suffix.to_string() },
|
||||
// Unreachable for typechecked input (check would have
|
||||
// raised TypeScopedReceiverNotAType); defensive dynamic.
|
||||
None => CalleeClass::Indirect,
|
||||
};
|
||||
}
|
||||
// rung 2: same-module global. It is a user fn IFF this module's
|
||||
// declared globals carry the name; otherwise it is a builtin —
|
||||
// both live in `env.globals` (synth `lib.rs:3416-3425`).
|
||||
if ctx.env.globals.contains_key(&name) {
|
||||
let is_user_fn = ctx
|
||||
.env
|
||||
.module_globals
|
||||
.get(&ctx.env.current_module)
|
||||
.is_some_and(|m| m.contains_key(&name));
|
||||
return if is_user_fn {
|
||||
CalleeClass::Static { module: ctx.env.current_module.clone(), fn_name: name }
|
||||
} else {
|
||||
CalleeClass::Builtin { name }
|
||||
};
|
||||
}
|
||||
// rung 3: implicit-import (prelude-fallback) fn (synth `lib.rs:3428-3435`).
|
||||
if let Some(module) = ctx.env.imports.values().find_map(|m| {
|
||||
ctx.env
|
||||
.module_globals
|
||||
.get(m)
|
||||
.filter(|g| g.contains_key(&name))
|
||||
.map(|_| m.clone())
|
||||
}) {
|
||||
return CalleeClass::Static { module, fn_name: name };
|
||||
}
|
||||
// Typechecked input always resolves above; defensive dynamic.
|
||||
CalleeClass::Indirect
|
||||
}
|
||||
|
||||
/// Lower one term to `MTerm`, filling `ty` from `synth_pure`.
|
||||
fn lower_term(ctx: &mut Ctx, t: &Term) -> Result<MTerm> {
|
||||
let ty = ctx.synth_pure(t)?;
|
||||
@@ -118,9 +187,22 @@ fn lower_term(ctx: &mut Ctx, t: &Term) -> Result<MTerm> {
|
||||
|
||||
Term::Var { name } => MTerm::Var { name: name.clone(), ty },
|
||||
|
||||
// callee is Indirect at mir.1 (mir.2 resolves Static).
|
||||
// mir.2: classify the callee against check's own resolution
|
||||
// ladder and emit the resolved `Callee`. `sig` is the callee's
|
||||
// fn-type (mode-preserving via `synth_pure`), carried so
|
||||
// codegen's drop path reads the callee `ret_mode`.
|
||||
Term::App { callee, args, tail } => {
|
||||
let m_callee = Callee::Indirect(Box::new(lower_term(ctx, callee)?));
|
||||
let class = classify_callee(ctx, callee);
|
||||
let sig = ctx.synth_pure(callee)?;
|
||||
let m_callee = match class {
|
||||
CalleeClass::Builtin { name } => Callee::Builtin { name, sig },
|
||||
CalleeClass::Static { module, fn_name } => {
|
||||
Callee::Static { module, fn_name, sig }
|
||||
}
|
||||
CalleeClass::Indirect => {
|
||||
Callee::Indirect(Box::new(lower_term(ctx, callee)?))
|
||||
}
|
||||
};
|
||||
let m_args = args
|
||||
.iter()
|
||||
.map(|a| Ok(arg(lower_term(ctx, a)?)))
|
||||
|
||||
Reference in New Issue
Block a user