Files
AILang/crates/ail/tests/codegen_import_map_fallback_pin.rs
T
Brummel 07f080256c iter remove-mut-var-assign.1: atomic removal of mut/var/assign
mut/var/assign removed from AILang entirely and atomically. Deleted:
Term::Mut/Term::Assign/struct MutVar; the three Form-A keywords +
parse_mut/parse_assign + grammar EBNF; the 4 mut CheckError variants;
the mut_scope_stack synth threading (param dropped from synth + every
internal/external/test caller); the two lower_term arms; and every
exhaustive no-_ Term::Mut/Term::Assign match arm across 17 source
files — cut in lockstep with DESIGN.md, fixtures, the drift trio,
carve-out and roadmap so the schema is honest at every commit. No
catch-all wildcard introduced (verified). loop/recur + let/if are
the surviving forms.

The shared codegen alloca machinery survives (loop reuses it):
mut_var_allocas renamed binder_allocas (representation-only, loop
codegen byte-identical) and the shared Term::Lam escape guard
simplified to !loop_stack.is_empty() with the loop half
(LoopBinderCapturedByLambda) byte-equivalent. Feature-acceptance
applied inverted: the removed feature fails clause 2 (redundant)
and clause 3 (IS the iterated-mutable-state bug class).

Behaviour preservation is executable: mut_counter/mut_sum_floats
still print 55 after the faithful let/if rewrite. The removal is
made executable by the new mut_removed_pin.rs (4 must-fail pins).
Independent verification: cargo test --workspace 605/0, zero
residual mut symbols in any crate source, loop/recur non-regression
all green (55 / 500000500000 / infinite-compiles / the
lambda_capturing_loop_binder pin), roundtrip_cli PASS.

One DONE_WITH_CONCERNS: a 4th recurrence of the recon-undercount
class (in-source mod tests + a drift-pin fn + 5 orphaned mut
.ail.json carve-outs + a non-enumerated E0599); all resolved within
implementer remit, no behaviour change. Milestone-close audit then
fieldtest remain.

spec docs/specs/2026-05-18-remove-mut-var-assign.md (grounding PASS)
plan docs/plans/remove-mut-var-assign.1.md
2026-05-18 11:06:17 +02:00

133 lines
5.3 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)
}
// 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),
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
);
}