//! Bug 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` (every Def::Type ctor //! across every module, keyed by bare ctor name → bare type name). //! //! In contrast, `check_in_workspace` (lib.rs:1247-1258) explicitly clears //! that flat index after `build_check_env` and rebuilds it per-module so //! that `Pattern::Ctor`'s local-first / imports-fallback resolution //! (lib.rs:2486-2521) can keep the qualified-type-name comparison at //! lib.rs:2526 intact: the imports-fallback branch produces a qualified //! `resolved_type_name` (`Mod.Type`), the local-hit branch produces a bare //! one (`Type`). //! //! The mono pass never does the per-module overlay. So when a body in //! module B pattern-matches a constructor `C` whose Def::Type lives in //! imported module A, the workspace-flat ctor_index resolves `C` locally //! (in fact: anywhere in the workspace) and yields the bare type name //! `"Type"`. The scrutinee, however, was typed by the main pass against //! the qualified name `"A.Type"` (per the imports-fallback path), so the //! comparison //! //! ```ignore //! Type::Con { name, args } if name == &resolved_type_name => args.clone(), //! _ => return Err(CheckError::PatternTypeMismatch { ... }), //! ``` //! //! at lib.rs:2526 fails with `cannot match constructor pattern C against //! type A.Type<...>`. //! //! Sibling regressions in the same family: //! * `mono_xmod_qualified_ref.rs` — `env.imports` not seeded. //! * commit 13b36cc — `env.globals` not seeded for self-recursive fns. //! * commit 5c5180f — `env.types` / `env.ctor_index` not seeded for //! user ADTs (the original "flat ctor_index" decision that this //! bug now exposes as wrong-by-construction for cross-module //! pattern-match resolution). //! //! Surfaced by iter 23.2 Task 3, which adds `class Eq a` + Eq Int/Bool/Str //! instances to `examples/prelude.ail.json`. Before Task 3 the prelude has //! only `Def::Type Ordering`, so `workspace_has_typeclasses(ws) == false` //! and the mono pass early-outs at `mono.rs:73`. Task 3 flips the gate; //! every workspace now traverses bodies, which brings the latent bug to //! the surface. //! //! Without the prelude change, two pre-existing E2E tests already //! exercise the bug shape (`nested_ctor_pattern_first_two_sum`, //! `std_either_list_demo`) and silently pass because the mono pass is a //! no-op on a class-free workspace. This test pins the inner cause //! against a minimal 2-module fixture so it stays RED regardless of the //! prelude's typeclass content. use std::path::PathBuf; fn examples_dir() -> PathBuf { let mut d = PathBuf::from(env!("CARGO_MANIFEST_DIR")); d.pop(); d.pop(); d.join("examples") } /// 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-fix repro: `monomorphise_workspace` returns /// `Err(CheckError::PatternTypeMismatch { ctor: "Cons", ty: /// "test_mono_ctor_listmod.List" })`. The same workspace /// typechecks cleanly because `check_in_workspace` clears the flat /// `ctor_index` before running `Pattern::Ctor` resolution. /// /// Post-fix expectation: `monomorphise_workspace` returns `Ok` and the /// `head_or_zero` fn body's match arm types correctly. #[test] fn mono_pass_handles_xmod_ctor_pattern() { let entry = examples_dir().join("test_mono_ctor_main.ail.json"); let ws = ailang_core::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 ); } } }