Files
AILang/crates/ail/tests/codegen_import_map_fallback_pin.rs
T
Brummel a378dad0aa docs(design): ratify the check→codegen boundary (mir.5, typed-MIR close)
mir.5 is the typed-MIR milestone's closing iteration. Its CODE half had
already converged before the iteration began, so mir.5 ships no code —
it ratifies into the design/ ledger the boundary the code already holds.

Verified at iteration entry (empirically, not from the spec sketch):
  - all four named re-derivers grep-clean in codegen: synth_with_extras,
    synth_arg_type, type_home_module, the second infer_module_with_cross;
  - lower_workspace takes &MirWorkspace (codegen consumes MIR);
  - MTerm::New is unreachable!() — raw-buf.4 desugars Term::New to
    (app T.new …) before codegen, so there is no element-type
    re-derivation left to relocate;
  - #51 / #53 (the element-type / new-T codegen crashes) are closed;
    their residue was fixed by ee4107c / 420f75f plus the New-desugar,
    not by a separate raw-buf patch track.

Ledger work (the mir.5 deliverable):
  - NEW design/contracts/0018-check-codegen-boundary.md: the invariant
    "codegen re-derives nothing; MIR is total over what check proved;
    a codegen arm that recomputes a fact instead of reading MIR is
    drift." Ratifier: lower_to_mir_ty.rs::callee_classification_builtin_and_static.
  - 0013-typeclasses invariant 2 retracted: codegen no longer re-resolves
    cross-module names via an import_map fallback; lower_to_mir::classify_callee
    resolves the reference once into Callee::Static and codegen consumes it.
  - 0003-pipeline.md: the "lower to MIR" line names the real stage
    (elaborate_workspace → MirWorkspace → lower_workspace) and a new
    paragraph states the boundary, cross-referencing 0018.
  - INDEX.md: boundary contract row added; qualified-xref re-pointed at
    lower_to_mir + 0018.
  - codegen_import_map_fallback_pin.rs doc-comment made honest — it pins
    the post-mono AST precondition classify_callee relies on, not a
    codegen-side resolution that no longer exists. Assertions unchanged;
    the test stays green.
  - spec 0060 gains a mir.5 refinement note recording the early code
    convergence.

Acceptance criteria 1-7 of docs/specs/0060-typed-mir.md are all met.
Full workspace suite green (exit 0, 0 failed, 2 ignored); 708 passed
carried from mir.4 (no test added or removed).

Not done here (deliberately): the milestone #7 (raw-buf) subsumption
note is an external Gitea tracker write; the /boss auto-mode classifier
declined it as an unauthorised external write and it is surfaced to the
user rather than worked around. The end-to-end milestone fieldtest
remains the deliberate manual close-gate before the tracker milestone
is marked done.

refs #51 #53
2026-06-01 01:34:37 +02:00

143 lines
6.1 KiB
Rust

//! Pin for the post-mono cross-module reference precondition
//! (design/contracts/0013-typeclasses.md, "Cross-module references in
//! synthesised bodies" invariant 2; resolved per the
//! design/contracts/0018-check-codegen-boundary.md boundary).
//!
//! Property protected: mono synthesises a `prelude.print__<UserType>`
//! body that references `<user_module>.show__<UserType>` even though
//! `prelude` does not import user modules — the cross-module reference
//! exists in the post-mono AST and is import_map-independent. This is
//! the precondition `lower_to_mir::classify_callee` relies on when it
//! resolves the reference once into `Callee::Static`; codegen then
//! consumes that identity and re-derives nothing (the typed-MIR
//! boundary). The pin asserts the AST-level precondition directly, so
//! it stays green and cheap to bisect.
//!
//! Failure mode this pin catches: a future mono refactor stops
//! emitting the cross-module `show_user_adt.show__<UserType>` Var (or
//! routes it through `prelude`'s import_map), which would break the
//! resolution `classify_callee` performs downstream. Without this pin,
//! the regression surfaces only at the `show_user_adt` E2E (which
//! builds + runs a binary, slow to bisect).
use ailang_check::{check_workspace, monomorphise_workspace};
use ailang_core::ast::{Def, Term};
use ailang_surface::load_workspace;
use std::path::PathBuf;
fn fixture_path() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("../../examples")
.join("show_user_adt.ail")
}
#[test]
fn synthesised_print_uses_user_module_show_via_fallback() {
// Step 1: workspace loads + typechecks clean.
let ws = load_workspace(&fixture_path()).expect("workspace loads");
let diags = check_workspace(&ws);
assert!(
diags.is_empty(),
"typecheck diagnostics in show_user_adt fixture: {diags:?}"
);
// Step 2: mono synthesis produces `prelude.print__<IntBox>` whose
// body references `show_user_adt.show__<IntBox>` (cross-module).
let post_mono = monomorphise_workspace(&ws).expect("mono green");
let prelude_mod = post_mono
.modules
.get("prelude")
.expect("prelude post-mono module present");
let print_def = prelude_mod
.defs
.iter()
.find_map(|d| match d {
Def::Fn(f) if f.name.starts_with("print__") => Some(f),
_ => None,
})
.expect("synthesised print__<UserType> not found in prelude post-mono module");
// Step 3: recursively walk `print_def.body` looking for a Var
// whose name carries the `show_user_adt.` prefix (the cross-
// module reference invariant 2 protects).
fn contains_xmod_show_var(t: &Term) -> bool {
match t {
Term::Var { name } => {
name.starts_with("show_user_adt.") && name.contains("show__")
}
Term::Let { value, body, .. } => {
contains_xmod_show_var(value) || contains_xmod_show_var(body)
}
Term::LetRec { body, in_term, .. } => {
contains_xmod_show_var(body) || contains_xmod_show_var(in_term)
}
Term::App { callee, args, .. } => {
contains_xmod_show_var(callee) || args.iter().any(contains_xmod_show_var)
}
Term::Do { args, .. } => args.iter().any(contains_xmod_show_var),
Term::Lam { body, .. } => contains_xmod_show_var(body),
Term::If { cond, then, else_ } => {
contains_xmod_show_var(cond)
|| contains_xmod_show_var(then)
|| contains_xmod_show_var(else_)
}
Term::Match { scrutinee, arms } => {
contains_xmod_show_var(scrutinee)
|| arms.iter().any(|a| contains_xmod_show_var(&a.body))
}
Term::Ctor { args, .. } => args.iter().any(contains_xmod_show_var),
Term::Seq { lhs, rhs } => {
contains_xmod_show_var(lhs) || contains_xmod_show_var(rhs)
}
Term::Clone { value } => contains_xmod_show_var(value),
Term::ReuseAs { source, body } => {
contains_xmod_show_var(source) || contains_xmod_show_var(body)
}
// loop-recur iter 1: a `Term::Loop` cannot itself host a
// synthesised cross-module reference, but recurse
// defensively through binder inits / body / recur args.
Term::Loop { binders, body } => {
binders.iter().any(|b| contains_xmod_show_var(&b.init))
|| contains_xmod_show_var(body)
}
Term::Recur { args } => args.iter().any(contains_xmod_show_var),
// prep.2 (kernel-extension-mechanics): recurse through
// NewArg::Value subterms; type-args do not carry a Var.
Term::New { args, .. } => args.iter().any(|arg| match arg {
ailang_core::ast::NewArg::Value(v) => contains_xmod_show_var(v),
ailang_core::ast::NewArg::Type(_) => false,
}),
Term::Lit { .. } => false,
Term::Intrinsic => false,
}
}
assert!(
contains_xmod_show_var(&print_def.body),
"synthesised print body should contain a `show_user_adt.<suffix>` Var \
referencing the user-module's show__<IntBox> mono symbol — \
this is the cross-module reference codegen resolves via the \
import_map-fallback path. Body: {:?}",
print_def.body
);
// Step 4: confirm prelude module's `imports` does NOT contain
// `show_user_adt` — the resolution at codegen time genuinely
// bypasses the source template's import_map.
let prelude_src = ws
.modules
.get("prelude")
.expect("prelude source module present");
assert!(
prelude_src
.imports
.iter()
.all(|imp| imp.module != "show_user_adt"),
"prelude must not import show_user_adt (the invariant is that \
codegen resolves the cross-module ref WITHOUT going through \
import_map). Got imports: {:?}",
prelude_src.imports
);
}