Files
AILang/crates/ail/tests/codegen_import_map_fallback_pin.rs
T
Brummel 37ac704bf3 iter revert: back out the Iteration-discipline milestone (it.1 + it.2)
One forward iteration; main never rewound. 1ff7e81 (the pre-9973546
commit) is the per-region byte oracle — every reverted source/test
file is byte-identical to it; crates/ailang-check/src/lib.rs fully so.

Root cause being corrected: the Iteration-discipline milestone was an
over-escalation of fieldtest finding F1 (a [friction] item whose own
minimal recommendation was a DESIGN.md note). Its totality dichotomy
made the maximally-LLM-natural build(d:Int)=Node(1,build(d-1),
build(d-1)) inexpressible (it.3 BLOCKED); the only in-thesis escape
(A1/it.2b) conceded the language's first documented-unenforced
totality precondition — a purity-pillar dilution the user rejected in
favour of a full revert + rebuild.

Removed: Term::Loop/Term::Recur/LoopBinder; the verify_structural_
recursion guardedness pass + term_contains_loop + Diverge-injection +
the transitively-it.2 module_fns plumbing; the five Recur*/
NonStructuralRecursion CheckError variants (+ code() + the 3 dedicated
ctx() arms); the it.1 codegen loop-header/phi/back-edge + parallel
block_terminated setter; all Loop/Recur walker arms; 16 it.1/it.2
fixtures; 2 pin files; bench/it3-oracle/. Restored: 2 RC fixtures to
1ff7e81 content.

Surgically kept (not in 1ff7e81, landed with the milestone but
independently sound): feature-acceptance clause 3 in DESIGN.md and
skills/brainstorm/SKILL.md, with its worked example de-claimed from
"shipped" to hypothetical-illustration form; the F3 P2 todo.
bench/orchestrator-stats/2026-05-15-iter-it.{1,2,3}.json kept as
historical record (like journals/plans).

Sole net addition: an honest F1/F4 documented-idiom note in DESIGN.md
(the tail-recursive accumulator fallback; examples/mut_counter.ail),
guarded by a doc-presence test — "a documentation note is not a
reshape", asserts nothing at the typecheck level.

Roadmap: the Iteration-discipline block + blocking-fork section
removed; the genuine total-Int-recursion ambition preserved as a
deferred P2 milestone sequenced behind a future Nat/refinement-types
milestone (not abandoned — correctly sequenced after the type
machinery it needs). 2026-05-15-iteration-discipline.md carries a
superseded header; it.1/it.2/it.3 journals + plans stay as history.

Correctness gate PRISTINE: 164 surviving 1ff7e81-era fixtures
ail check/ail run byte-identical to pre-milestone behaviour (verified
against a 1ff7e81 worktree reference compiler, zero drift);
cargo test --workspace 600/0; zero residual it.1/it.2 production
surface.

Spec docs/specs/2026-05-16-iteration-discipline-revert.md (b3853bf),
plan docs/plans/2026-05-16-iter-revert.md (abf0013).
2026-05-16 01:28:47 +02:00

135 lines
5.4 KiB
Rust

//! Pin for the iter-24.3 codegen `import_map`-fallback path
//! (DESIGN.md §"Cross-module references in synthesised bodies"
//! invariant 2).
//!
//! Property protected: post-mono synthesised body cross-module
//! references resolve at codegen via the fallback to
//! `module_user_fns` / `module_def_ail_types` when the prefix is
//! NOT in the current module's `import_map`. Specifically, the
//! synthesised `prelude.print__<UserType>` body references
//! `<user_module>.show__<UserType>` even though `prelude` does not
//! import user modules.
//!
//! Failure mode this pin catches: a future codegen refactor
//! tightens `resolve_top_level_fn` or `lower_app`'s cross-module
//! arm or `synth_with_extras`'s Var arm back to `import_map`-only.
//! 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)
}
// Iter mut.1: a `Term::Mut` cannot itself host a
// `show_user_adt.show__<T>` reference (its body is a
// mut-block, not a synthesised polymorphic call), but
// recurse defensively so any nested reference inside a
// var init / body / assign-value still surfaces.
Term::Mut { vars, body } => {
vars.iter().any(|v| contains_xmod_show_var(&v.init))
|| contains_xmod_show_var(body)
}
Term::Assign { value, .. } => contains_xmod_show_var(value),
Term::Lit { .. } => 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
);
}