//! 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" }` 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` 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" })`. 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 ); } } }