Files
AILang/crates/ail/tests/mono_unification.rs
T
Brummel 77b28ad64d iter form-a.1 (Tasks 1-5): additive phase + relocation
First half of the form-a-default-authoring milestone-close iter
(Boss-decided strategy C, big-bang). All five tasks DONE; cargo
test --workspace green at every per-task boundary.

T1 — Add three new tests:
- parse_is_deterministic_over_every_ail_fixture (round_trip.rs)
- cli_parse_then_render_then_parse_is_idempotent (roundtrip_cli.rs)
- carve_out_inventory.rs (new file; #[ignore]'d until T8 deletion)

T2 — Bulk-render the 99 missing examples/<stem>.ail files via
`ail render`. Corpus 58 .ail (pre-iter) -> 157 .ail. Eight .ail.json
carve-outs (7 §C4(a) subject-matter + 1 §C4(b) prelude) preserved.
One re-loop triggered: load_workspace prefers .ail siblings since
ext-cli.1, so the newly-rendered imports broke seven Group-A entries
whose JSON entry-paths now resolved imports to fresh .ail. Repair:
pre-emptive forward-pull of five T3 migrations + 4 transient
#[ignore]s on workspace.rs mod tests (cleanly relocated in T5).

T3 — 14 Group-A test files migrated from ailang_core::load_workspace
to ailang_surface::load_workspace + .ail paths. Carve-out lines
preserved verbatim (7 sites in typeclass_22b2.rs / typeclass_22b3.rs).

T4 — 12 Group-B test files: bulk regex flip on build_and_run /
build_and_run_with_alloc / build_and_run_with_rc_stats call sites
(~70 e2e.rs invocations + 11 subprocess sites). Four files
mis-classified Group-A as Group-B in plan recon (mono_hash_stability,
prelude_free_fns, print_mono_body_shape, show_mono_synthesis); two
files mis-classified Group-B as Group-A (mono_recursive_fn,
mono_xmod_qualified_ref). Migrated per actual shape, not plan label.

T5 — Relocated #[cfg(test)] mod tests from production source to
integration test crates with ailang-surface dev-dependency:
- crates/ailang-core/tests/hash_pin.rs (10 tests from hash.rs)
- crates/ailang-core/tests/workspace_pin.rs (10 non-carve-out tests
  from workspace.rs)
- crates/ailang-codegen/tests/eq_primitives_pin.rs (3 tests from
  codegen/src/lib.rs:3717-3799)
- ailang-prose/tests/snapshot.rs migrated (helper + 8 fixtures) to
  .ail + ailang_surface::load_module
Carve-out tests in workspace.rs mod tests preserved in-place
(3× 22b2 + 3× ct1 = 6 tests). Tempdir-based loader-mechanism tests
(3 sites) also preserved — they don't consume examples/.

Tests: 560 passed, 0 failed, 4 ignored (was 558 + 3 T1 new -
1 transit carve_out_inventory #[ignore] = 560 active).

Tasks 6-12 (bench-driver suffix flips, e2e diff-test rewrite + 4
additional raw-JSON-inspect handlers, .ail.json deletion, retiring
obsolete roundtrip tests + schema_coverage corpus flip, §C3
DESIGN.md restatement, §A4 doctrine edits, WhatsNew + roadmap
strike) ship in the next dispatch on this iter ID.

Known debt inherited to T6-12: 4 raw-JSON-inspect tests in e2e.rs
(borrow_own_demo / reuse_as_demo / render_parse_round_trip_canonical
/ ail_run_accepts_ail_source_with_same_stdout_as_ail_json dual-form
smoke) need rewrite or #[ignore] before T8 deletion; recorded in
journal Concerns + Known debt sections.
2026-05-13 11:12:48 +02:00

222 lines
9.2 KiB
Rust

//! Tests for the unified mono pass over polymorphic free fns (iter 23.4).
//!
//! These tests assert workspace-level invariants AFTER
//! `monomorphise_workspace` has run: synthesised mono `Def::Fn`s for
//! both class-method residuals AND polymorphic free-fn calls live in
//! the post-mono workspace.
use ailang_check::mono::{mono_target_key, MonoTarget};
use ailang_core::ast::{Def, Type};
use ailang_surface::load_workspace;
use std::path::PathBuf;
fn fixture(name: &str) -> PathBuf {
let mut d = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
d.pop();
d.pop();
d.join("examples").join(name)
}
#[test]
fn cmp_max_smoke_produces_both_mono_symbols_in_one_fixpoint() {
let ws = load_workspace(&fixture("cmp_max_smoke.ail"))
.expect("workspace loads");
let diags = ailang_check::check_workspace(&ws);
assert!(diags.is_empty(), "typecheck diagnostics: {diags:?}");
let post_mono = ailang_check::monomorphise_workspace(&ws).expect("mono green");
// Free-fn mono symbols are appended to their owner module
// (where the polymorphic Def::Fn lives) — for cmp_max, that is
// `cmp_max_smoke`. Class-method mono symbols (compare__Int) are
// appended to the instance's defining module (`prelude`).
let main_mod = post_mono
.modules
.get("cmp_max_smoke")
.expect("main module present");
let prelude_mod = post_mono
.modules
.get("prelude")
.expect("prelude module present");
let main_names: Vec<&str> = main_mod
.defs
.iter()
.filter_map(|d| if let Def::Fn(f) = d { Some(f.name.as_str()) } else { None })
.collect();
let prelude_names: Vec<&str> = prelude_mod
.defs
.iter()
.filter_map(|d| if let Def::Fn(f) = d { Some(f.name.as_str()) } else { None })
.collect();
assert!(
main_names.contains(&"cmp_max__Int"),
"post-mono workspace must contain free-fn mono symbol cmp_max__Int in module cmp_max_smoke; got {main_names:?}"
);
assert!(
prelude_names.contains(&"compare__Int"),
"post-mono workspace must contain class-method mono symbol compare__Int in prelude (closed transitively from cmp_max__Int body); got {prelude_names:?}"
);
}
#[test]
fn collect_mono_targets_emits_free_fn_target_for_cmp_max_at_int() {
use ailang_check::mono::collect_mono_targets;
let ws = load_workspace(&fixture("cmp_max_smoke.ail")).expect("workspace loads");
let diags = ailang_check::check_workspace(&ws);
assert!(diags.is_empty(), "typecheck diagnostics: {diags:?}");
let env = ailang_check::build_check_env(&ws);
let main_mod = ws.modules.get("cmp_max_smoke").expect("main module");
let main_fn = main_mod.defs.iter().find_map(|d| {
if let Def::Fn(f) = d { (f.name == "main").then_some(f) } else { None }
}).expect("main fn");
let targets = collect_mono_targets(main_fn, "cmp_max_smoke", &env)
.expect("collector runs");
let has_free_fn_target = targets.iter().any(|t| matches!(
t, MonoTarget::FreeFn { name, type_args, .. }
if name == "cmp_max" && type_args.len() == 1
&& matches!(&type_args[0], Type::Con { name, args } if name == "Int" && args.is_empty())
));
assert!(
has_free_fn_target,
"expected MonoTarget::FreeFn {{ name: \"cmp_max\", type_args: [Int] }}; got {targets:?}"
);
}
#[test]
fn cmp_max_smoke_runs_end_to_end() {
// Compile + run the cmp_max_smoke fixture, prove it prints 7
// (cmp_max(3, 7) at Int via Ord Int).
use std::process::Command;
let manifest_dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let workspace = manifest_dir.parent().unwrap().parent().unwrap();
let src = workspace.join("examples").join("cmp_max_smoke.ail");
let tmp = std::env::temp_dir().join(format!(
"ailang_cmp_max_smoke_{}",
std::process::id()
));
std::fs::create_dir_all(&tmp).unwrap();
let out = tmp.join("bin");
let status = Command::new(env!("CARGO_BIN_EXE_ail"))
.args(["build", src.to_str().unwrap(), "-o"])
.arg(&out)
.status()
.expect("ail build failed to run");
assert!(status.success(), "ail build failed for cmp_max_smoke");
let output = Command::new(&out).output().expect("run cmp_max_smoke binary");
assert!(output.status.success(), "binary exited non-zero");
let stdout = String::from_utf8(output.stdout).expect("stdout utf8");
assert_eq!(stdout.trim(), "7", "cmp_max(3, 7) at Int prints 7");
}
#[test]
fn workspace_with_only_poly_free_fn_and_no_classes_still_monomorphises() {
// poly_id has a Type::Forall free fn and NO class/instance defs
// in the user module. The prelude (auto-injected) has classes,
// but for early-out purposes we test the user-module gate.
// Today the workspace-has-typeclasses early-out checks the
// WHOLE workspace including the prelude, so it actually fires
// on every user workspace. The Task-7 generalisation widens
// the predicate to also include `Def::Fn` with `Type::Forall`
// as a specialisable target — which makes the predicate true
// for any workspace with poly free fns (regardless of classes).
let ws = load_workspace(&fixture("poly_id.ail")).expect("workspace loads");
let diags = ailang_check::check_workspace(&ws);
assert!(diags.is_empty(), "typecheck diagnostics: {diags:?}");
let post_mono = ailang_check::monomorphise_workspace(&ws).expect("mono green");
// After the unified pass, the workspace contains synthesised
// mono fn defs for `id` at Int and at Bool — both invoked from
// `main`.
let names: Vec<String> = post_mono.modules.values()
.flat_map(|m| m.defs.iter().filter_map(|d| {
if let Def::Fn(f) = d { Some(f.name.clone()) } else { None }
}))
.collect();
assert!(names.iter().any(|n| n == "id__Int"),
"poly_id must produce id__Int via unified mono; got {names:?}");
assert!(names.iter().any(|n| n == "id__Bool"),
"poly_id must produce id__Bool via unified mono; got {names:?}");
}
#[test]
fn rewrite_mono_calls_rewrites_bare_poly_free_fn_to_mono_symbol() {
let ws = load_workspace(&fixture("cmp_max_smoke.ail")).expect("workspace loads");
let diags = ailang_check::check_workspace(&ws);
assert!(diags.is_empty(), "typecheck diagnostics: {diags:?}");
let post_mono = ailang_check::monomorphise_workspace(&ws).expect("mono green");
let main_mod = post_mono.modules.get("cmp_max_smoke").expect("main module");
let main_fn = main_mod.defs.iter().find_map(|d| {
if let Def::Fn(f) = d { (f.name == "main").then_some(f) } else { None }
}).expect("main fn");
let body_str = format!("{:?}", main_fn.body);
assert!(
body_str.contains("cmp_max__Int"),
"main body must call cmp_max__Int after rewrite; got: {body_str}"
);
// The bare-name `cmp_max` (as a Term::Var.name) must NOT appear
// anywhere in main's body post-rewrite.
assert!(
!body_str.contains("Var { name: \"cmp_max\""),
"main body must NOT still call bare cmp_max after rewrite; got: {body_str}"
);
}
#[test]
fn synthesise_mono_fn_free_fn_arm_substitutes_rigid_vars() {
use ailang_check::mono::synthesise_mono_fn_for_free_fn;
let ws = load_workspace(&fixture("cmp_max_smoke.ail")).expect("workspace loads");
let diags = ailang_check::check_workspace(&ws);
assert!(diags.is_empty(), "typecheck diagnostics: {diags:?}");
let int_ty = Type::Con { name: "Int".into(), args: vec![] };
let target = MonoTarget::FreeFn {
name: "cmp_max".into(),
type_args: vec![int_ty.clone()],
defining_module: "cmp_max_smoke".into(),
};
let synth = synthesise_mono_fn_for_free_fn(&target, &ws)
.expect("synthesis succeeds");
assert_eq!(synth.name, "cmp_max__Int");
// Body must still reference `compare` (pre-rewrite); Task 6
// rewrites it to `compare__Int` during the walker pass.
let body_str = format!("{:?}", synth.body);
assert!(body_str.contains("compare"), "body still references compare bare-name pre-rewrite: {body_str}");
// Param types must be Int after rigid substitution.
assert!(matches!(
&synth.ty,
Type::Fn { params, ret, .. }
if params.len() == 2
&& matches!(&params[0], Type::Con { name, args } if name == "Int" && args.is_empty())
&& matches!(&params[1], Type::Con { name, args } if name == "Int" && args.is_empty())
&& matches!(ret.as_ref(), Type::Con { name, args } if name == "Int" && args.is_empty())
), "synth.ty = {:?}", synth.ty);
}
#[test]
fn mono_target_free_fn_variant_keys_distinctly_from_class_method() {
let int_ty = Type::Con { name: "Int".into(), args: vec![] };
let class_tgt = MonoTarget::ClassMethod {
class: "Eq".into(),
method: "eq".into(),
type_: int_ty.clone(),
defining_module: "prelude".into(),
};
let free_tgt = MonoTarget::FreeFn {
name: "cmp_max".into(),
type_args: vec![int_ty.clone()],
defining_module: "prelude".into(),
};
assert_ne!(
mono_target_key(&class_tgt),
mono_target_key(&free_tgt),
"class-method and free-fn targets must have distinct dedup keys"
);
}