Files
AILang/crates/ail/tests/mono_xmod_ctor_pattern.rs
T
Brummel 7ae92d3e60 refactor(test): hoist duplicated test helpers into ailang-test-support
closes #60

The fixture-corpus filter (NON_PARSEABLE_FIXTURES + list_ail_fixtures)
was copy-pasted across three test crates and drifted out of sync as
new reject fixtures landed; the examples_dir / workspace_root path
walk was reimplemented ~30 more times across the suite, under two
spellings (workspace_root / ws_root).

Introduce crates/ailang-test-support — a zero-dependency, dev-only
leaf crate — as the single home for these helpers:
- NON_PARSEABLE_FIXTURES, list_ail_fixtures
- workspace_root, examples_dir
- canonical_workspace_root (symlink-resolved variant, for the tsan/
  race tests that feed the root into native build steps)

All integration-test copies across ailang-core, ailang-surface,
ailang-prose, ailang-check, and ail now import from the shared crate;
the local definitions and their now-orphaned path imports are removed.
Path helpers resolve via the support crate's own CARGO_MANIFEST_DIR,
so they return the correct absolute paths from any caller.

ail_bin helpers are intentionally left in place: they depend on
env!("CARGO_BIN_EXE_ail"), which Cargo only defines when compiling the
ail crate's own integration tests, so they cannot move to a shared
crate.

Behaviour-identical: same paths, same fixture lists; full workspace
test suite green. Net -177 LOC.
2026-06-02 01:54:37 +02:00

98 lines
4.5 KiB
Rust

//! Regression: the workspace-monomorphisation pass must not mis-resolve
//! a cross-module constructor pattern. The mono pass re-runs `synth` on
//! every fn body to recover residual class constraints; that env is built
//! by `mono::build_workspace_env`, which delegates to `crate::build_check_env`
//! and produces a workspace-flat `ctor_index` and `types` map.
//!
//! After the canonical-form / type-driven-ctor-lookup refactor,
//! `Pattern::Ctor` lookup is type-driven — it consults the
//! scrutinee's canonical `Type::Con.name` to find the TypeDef directly
//! in `env.module_types`, then validates the ctor name within it. The
//! mono pass's flat `ctor_index` is no longer consulted by this path;
//! the per-module overlay (lib.rs:1247-1258) is now decorative for the
//! pattern path and remains only for duplicate-type / duplicate-ctor
//! detection at the workspace-build prologue.
//!
//! This test pins the cross-module pattern shape against a minimal
//! 2-module fixture (`test_mono_ctor_main` + `test_mono_ctor_listmod`).
//! Before the refactor the bug surfaced as
//! `PatternTypeMismatch { ctor: "Cons",
//! ty: "test_mono_ctor_listmod.List<Int>" }` because the mono env
//! resolved `Cons` to bare `List` via the flat index. After the
//! refactor the lookup is type-driven and
//! `expected.name == "test_mono_ctor_listmod.List"` directly indexes
//! the right TypeDef.
//!
//! Surfaced when `class Eq a` + Eq Int/Bool/Str instances were added
//! to `examples/prelude.ail.json`, flipping the
//! `workspace_has_typeclasses` gate so every workspace exercises the
//! mono pass.
use ailang_test_support::examples_dir;
/// Property: `monomorphise_workspace` must succeed on a workspace that
/// (a) flips the typeclass gate (any `Def::Class` + `Def::Instance` is
/// enough), and (b) contains a fn whose body pattern-matches a
/// constructor whose `Def::Type` lives in an imported module.
///
/// The minimal fixture is two modules:
///
/// * `test_mono_ctor_listmod` — declares `data List a = Nil | Cons a (List a)`.
/// * `test_mono_ctor_main` — imports the listmod, declares
/// `class Trivial a` + `instance Trivial Int` (to flip the
/// `workspace_has_typeclasses` gate), and defines
/// `head_or_zero : test_mono_ctor_listmod.List<Int> -> Int` that
/// matches on `Cons h _ | Nil`.
///
/// Pre-canonical-type-form repro: `monomorphise_workspace` returned
/// `Err(CheckError::PatternTypeMismatch { ctor: "Cons", ty:
/// "test_mono_ctor_listmod.List<Int>" })`. The same workspace
/// typechecked cleanly because the typecheck pass overlaid a
/// per-module `ctor_index` whose imports-fallback produced the
/// qualified `resolved_type_name`, while the mono pass kept the
/// workspace-flat index whose local hit produced the bare one.
///
/// Post-canonical-type-form expectation: `monomorphise_workspace` returns `Ok`
/// because the Pattern::Ctor lookup no longer consults
/// `ctor_index` at all — it walks directly from the scrutinee's
/// canonical `Type::Con.name` to its TypeDef in
/// `env.module_types` and validates the ctor by name there.
#[test]
fn mono_pass_handles_xmod_ctor_pattern() {
let entry = examples_dir().join("test_mono_ctor_main.ail");
let ws = ailang_surface::load_workspace(&entry).expect("load");
// Pre-condition: the workspace typechecks cleanly. The bug is
// localised to the mono pass; the typecheck path does not have it.
let diags = ailang_check::check_workspace(&ws);
assert!(
diags.is_empty(),
"fixture must typecheck before mono runs: {:?}",
diags
);
// Pin the symptom to the inner cause. Pre-fix this returns Err
// with the qualified scrutinee type and the bare `Cons` ctor.
let result = ailang_check::monomorphise_workspace(&ws);
match result {
Ok(_) => { /* post-fix: pass */ }
Err(ailang_check::CheckError::PatternTypeMismatch { ctor, ty }) => {
panic!(
"mono pass mis-resolves cross-module ctor pattern: \
ctor=`{}` ty=`{}` (expected mono to succeed; the bare \
ctor_index in build_workspace_env resolves `Cons` to \
the local bare `List` instead of qualified \
`test_mono_ctor_listmod.List`)",
ctor, ty
);
}
Err(other) => {
panic!(
"mono pass failed with unexpected error variant: {:?} \
(expected PatternTypeMismatch on cross-module Cons)",
other
);
}
}
}