iter mq.1: class-ref canonical-form extension + workspace-internal class-name qualification
Three schema fields move bare → canonical (bare for same-module,
<module>.<Class> for cross-module): InstanceDef.class,
Constraint.class, SuperclassRef.class. Symmetric to ct.1's
Type::Con.name rule. ClassDef.name stays bare.
validate_canonical_type_names gains three field-walks via new
check_class_ref helper + two sibling diagnostics
BareCrossModuleClassRef / BadCrossModuleClassRef.
check_class_name_fields narrowed to ClassDef.name-only.
Workspace-internal class-name keys all qualified:
class_def_module, class_by_name, registry entries.0,
ClassMethodEntry.class_name, class_superclasses, mono's
class_index, Origin::Class.class_name. New qualify_class_ref
helper at workspace.rs scope plus sibling
qualify_class_ref_in_check in ailang-check.
Two new positive on-disk fixtures (mq1_xmod_constraint_class{,_dep})
exercise the qualified Constraint.class shape.
Recon claim that all existing fixtures' class-refs are intra-module
was empirically wrong — 10 fixtures required minimal canonical-form
migration. Duplicate-instance test architecturally restructured:
post-mq.1 orphan-freedom makes two-modules-on-same-(class,type)
structurally impossible; new test_22b1_dup_same_module fixture
lands both instances in the class's module.
Plan defects fixed inline: Origin::Class.class_name single
construction site (plan recon off-by-one); build_class_index in
mono.rs needed qualifying; build_module_globals needed Def::Instance
carve-out; check_fn declared-constraint matching needed inline
canonical-form lifting; NoInstance Float-aware message arm needed
prelude.Eq/prelude.Ord match; coherence type-leg needed qualified-
head split-and-lookup. Tasks 3-6 ran as one coherent fix.
7/7 tasks, 520 tests green. bench/compile_check.py + cross_lang.py
exit 0; bench/check.py exit 1 (4 improvements-beyond-tolerance,
audit-ratifiable, not a regression).
MethodNameCollision + dispatch path unchanged; iters mq.2 + mq.3
land them.
This commit is contained in:
@@ -1280,6 +1280,38 @@ fn workspace_error_to_diagnostic(
|
||||
"name": name,
|
||||
})),
|
||||
),
|
||||
// mq.1 (canonical-class-names): bare class-ref that does not
|
||||
// resolve to a local class of the owning module. Sibling of
|
||||
// `BareCrossModuleTypeRef` for class-reference fields.
|
||||
W::BareCrossModuleClassRef { module, name, candidates } => Some(
|
||||
ailang_check::Diagnostic::error(
|
||||
"bare-cross-module-class-ref",
|
||||
format!(
|
||||
"module `{module}` contains bare class name `{name}` that does not resolve to a local class; \
|
||||
candidates from imports: {candidates:?}"
|
||||
),
|
||||
)
|
||||
.with_ctx(serde_json::json!({
|
||||
"module": module,
|
||||
"name": name,
|
||||
"candidates": candidates,
|
||||
})),
|
||||
),
|
||||
// mq.1: qualified `<owner>.<class>` where `<owner>` is not a
|
||||
// known module or `<owner>` has no class `<class>`.
|
||||
W::BadCrossModuleClassRef { module, name } => Some(
|
||||
ailang_check::Diagnostic::error(
|
||||
"bad-cross-module-class-ref",
|
||||
format!(
|
||||
"module `{module}` references qualified class `{name}` but the owner module is not known \
|
||||
or does not declare a class by that name"
|
||||
),
|
||||
)
|
||||
.with_ctx(serde_json::json!({
|
||||
"module": module,
|
||||
"name": name,
|
||||
})),
|
||||
),
|
||||
// ct.1: a class-reference field contains a `.` — class names
|
||||
// are not module-qualified in this milestone.
|
||||
W::QualifiedClassName { module, name, field } => Some(
|
||||
|
||||
@@ -86,14 +86,18 @@ fn check_json_emits_bad_cross_module_type_ref() {
|
||||
);
|
||||
}
|
||||
|
||||
/// Property: a qualified class name in an `InstanceDef.class` field
|
||||
/// (here `prelude.Eq`) is rejected by `ail check --json` with
|
||||
/// diagnostic code `qualified-class-name` and non-zero exit. ct.1
|
||||
/// reserves module qualification for type names; classes stay bare in
|
||||
/// this milestone. Guards against `workspace_error_to_diagnostic`
|
||||
/// losing the `QualifiedClassName` arm.
|
||||
/// Property: mq.1 — a qualified class name in an `InstanceDef.class`
|
||||
/// field is the canonical form, not a rejection. The
|
||||
/// `test_ct1_qualified_class_rejected` fixture (declares
|
||||
/// `instance prelude.Eq Int` outside prelude and outside Int's
|
||||
/// defining module) is now rejected by the downstream coherence
|
||||
/// check with `orphan-instance` instead of the pre-mq.1
|
||||
/// `qualified-class-name`. Guards against
|
||||
/// `workspace_error_to_diagnostic` losing the OrphanInstance arm
|
||||
/// AND against any regression that would reintroduce
|
||||
/// `qualified-class-name` on a referencing field.
|
||||
#[test]
|
||||
fn check_json_emits_qualified_class_name() {
|
||||
fn check_json_emits_orphan_instance_on_xmod_class_without_coherence_post_mq1() {
|
||||
let fixture = examples_dir().join("test_ct1_qualified_class_rejected.ail.json");
|
||||
let output = Command::new(ail_bin())
|
||||
.args(["check", "--json", fixture.to_str().unwrap()])
|
||||
@@ -101,7 +105,7 @@ fn check_json_emits_qualified_class_name() {
|
||||
.expect("ail binary must launch");
|
||||
assert!(
|
||||
!output.status.success(),
|
||||
"ail check must fail on qualified class name; stdout={} stderr={}",
|
||||
"ail check must fail; stdout={} stderr={}",
|
||||
String::from_utf8_lossy(&output.stdout),
|
||||
String::from_utf8_lossy(&output.stderr),
|
||||
);
|
||||
@@ -110,8 +114,12 @@ fn check_json_emits_qualified_class_name() {
|
||||
serde_json::from_str(&stdout).expect("--json mode emits a JSON array on stdout");
|
||||
let arr = diags.as_array().expect("diagnostics is a JSON array");
|
||||
assert!(
|
||||
arr.iter().any(|d| d["code"] == "qualified-class-name"),
|
||||
"expected `qualified-class-name` in diagnostics array; got {stdout}"
|
||||
arr.iter().any(|d| d["code"] == "orphan-instance"),
|
||||
"expected `orphan-instance` in diagnostics array; got {stdout}"
|
||||
);
|
||||
assert!(
|
||||
!arr.iter().any(|d| d["code"] == "qualified-class-name"),
|
||||
"must NOT fire `qualified-class-name` on a referencing field post-mq.1; got {stdout}"
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -37,10 +37,12 @@ fn class_method_is_in_module_globals() {
|
||||
mod_globals.has_class_method("show"),
|
||||
"class method `show` must appear in module globals"
|
||||
);
|
||||
// mq.1: class_method_class returns the qualified form
|
||||
// `<defining_module>.<Class>`.
|
||||
assert_eq!(
|
||||
mod_globals.class_method_class("show"),
|
||||
Some("Show"),
|
||||
"class method `show` must remember its class is `Show`"
|
||||
Some("test_22b2_class_method_lookup.Show"),
|
||||
"class method `show` must remember its class is `Show` (qualified post-mq.1)"
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -148,7 +148,8 @@ fn collect_mono_targets_single_concrete_call_site() {
|
||||
let t = &targets[0];
|
||||
match t {
|
||||
ailang_check::mono::MonoTarget::ClassMethod { class, method, type_, defining_module } => {
|
||||
assert_eq!(class, "Show");
|
||||
// mq.1: MonoTarget.class carries the qualified workspace-key shape.
|
||||
assert_eq!(class, "test_22b2_instance_present.Show");
|
||||
assert_eq!(method, "show");
|
||||
assert!(
|
||||
matches!(type_, ailang_core::ast::Type::Con { name, args } if name == "Int" && args.is_empty()),
|
||||
|
||||
@@ -716,7 +716,9 @@ impl CheckError {
|
||||
// (`ne` / `lt` / `le` / `gt` / `ge` / direct `eq` / `compare`)
|
||||
// are the natural surface where the LLM hits this.
|
||||
if let CheckError::NoInstance { class, at_type, .. } = self.inner() {
|
||||
if (class == "Eq" || class == "Ord") && at_type == "Float" {
|
||||
// mq.1: `class` carries the qualified workspace-key shape
|
||||
// (`prelude.Eq` / `prelude.Ord`), not the bare name.
|
||||
if (class == "prelude.Eq" || class == "prelude.Ord") && at_type == "Float" {
|
||||
d.message = format!(
|
||||
"{} — Float has no Eq/Ord instance by design (partial \
|
||||
orderability per IEEE-754); see DESIGN.md §\"Float semantics\".",
|
||||
@@ -1107,6 +1109,11 @@ impl ModuleGlobals {
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ClassMethodEntry {
|
||||
/// The class that declared the method (e.g. `Show` for `show`).
|
||||
///
|
||||
/// mq.1: carries the qualified form `<defining_module>.<Class>`;
|
||||
/// flows downstream into `ResidualConstraint.class` and
|
||||
/// `MonoTarget::ClassMethod.class` unchanged. Match the registry
|
||||
/// key shape so discharge / mono lookups land on the right entry.
|
||||
pub class_name: String,
|
||||
/// The class's single type parameter name (e.g. `a` in
|
||||
/// `class Show a`). The class param appears at this name inside
|
||||
@@ -1141,7 +1148,13 @@ pub fn build_module_globals(
|
||||
let mut globals = ModuleGlobals::default();
|
||||
for def in &m.defs {
|
||||
let def_name = def.name();
|
||||
if def_name.contains('.') {
|
||||
// mq.1: for `Def::Instance`, `def.name()` returns
|
||||
// `inst.class` which carries the canonical-form value
|
||||
// (qualified for cross-module). The dot-in-def-name check
|
||||
// is meant for `Fn`/`Const`/`Type` whose names are author-
|
||||
// chosen identifiers — instances have no author-chosen
|
||||
// name and are excluded.
|
||||
if !matches!(def, Def::Instance(_)) && def_name.contains('.') {
|
||||
return Err(CheckError::Def(
|
||||
def_name.to_string(),
|
||||
Box::new(CheckError::InvalidDefName {
|
||||
@@ -1198,11 +1211,17 @@ pub fn build_module_globals(
|
||||
// with enough metadata for the typecheck arms in
|
||||
// Tasks 9 / 10 to build a residual constraint at
|
||||
// the call site.
|
||||
//
|
||||
// mq.1: store the qualified class name
|
||||
// (`<defining_module>.<Class>`) so the residual
|
||||
// emitted at the call site matches the workspace
|
||||
// registry's qualified key.
|
||||
let qualified_class = format!("{mname}.{}", cd.name);
|
||||
for meth in &cd.methods {
|
||||
globals.class_methods.insert(
|
||||
meth.name.clone(),
|
||||
ClassMethodEntry {
|
||||
class_name: cd.name.clone(),
|
||||
class_name: qualified_class.clone(),
|
||||
class_param: cd.param.clone(),
|
||||
method_ty: meth.ty.clone(),
|
||||
defining_module: mname.clone(),
|
||||
@@ -1301,11 +1320,25 @@ pub fn build_check_env(ws: &Workspace) -> Env {
|
||||
}
|
||||
|
||||
// env.class_superclasses — walk every module's Def::Class.
|
||||
for m in ws.modules.values() {
|
||||
//
|
||||
// mq.1: key + value both carry the qualified class name. The map
|
||||
// is queried by `expand_declared_constraints` with `c.class`
|
||||
// (a `Constraint.class` value, which is canonical-form on the
|
||||
// residual side post-mq.1), so both halves of the map must match
|
||||
// the same workspace-key shape. A bare `SuperclassRef.class`
|
||||
// (same-module superclass) is lifted via `qualify_class_ref` —
|
||||
// mirrored inline below to avoid pulling in the workspace helper.
|
||||
for (mod_name, m) in &ws.modules {
|
||||
for d in &m.defs {
|
||||
if let Def::Class(cd) = d {
|
||||
if let Some(sc) = &cd.superclass {
|
||||
env.class_superclasses.insert(cd.name.clone(), sc.class.clone());
|
||||
let class_qualified = format!("{mod_name}.{}", cd.name);
|
||||
let sc_qualified = if sc.class.contains('.') {
|
||||
sc.class.clone()
|
||||
} else {
|
||||
format!("{mod_name}.{}", sc.class)
|
||||
};
|
||||
env.class_superclasses.insert(class_qualified, sc_qualified);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1604,8 +1637,21 @@ fn check_fn(f: &FnDef, env: &Env) -> Result<()> {
|
||||
// Decision 11) and compare against residuals. Var-shaped residuals
|
||||
// not covered by the expanded set fire `MissingConstraint`. Concrete
|
||||
// residuals are deferred to Task 10's `no-instance` arm.
|
||||
//
|
||||
// mq.1: declared constraints carry the canonical-form `class`
|
||||
// value (bare for same-module, qualified for cross-module). Lift
|
||||
// to the workspace-key shape (always qualified) before comparing
|
||||
// against residuals — residuals always carry qualified `class`
|
||||
// because they come from `ClassMethodEntry.class_name` which is
|
||||
// qualified post-mq.1.
|
||||
let declared_constraints: Vec<Constraint> = match &f.ty {
|
||||
Type::Forall { constraints, .. } => constraints.clone(),
|
||||
Type::Forall { constraints, .. } => constraints
|
||||
.iter()
|
||||
.map(|c| Constraint {
|
||||
class: qualify_class_ref_in_check(&c.class, &env.current_module),
|
||||
type_: c.type_.clone(),
|
||||
})
|
||||
.collect(),
|
||||
_ => Vec::new(),
|
||||
};
|
||||
let expanded = expand_declared_constraints(&declared_constraints, &env.class_superclasses);
|
||||
@@ -1733,6 +1779,20 @@ pub struct FreeFnCall {
|
||||
pub metas: Vec<Type>,
|
||||
}
|
||||
|
||||
/// mq.1: lift a canonical-form class-ref value to the qualified
|
||||
/// workspace-key shape. Bare ⇒ prepend `caller_module` (the bare
|
||||
/// form names the caller-module-local class under the canonical-form
|
||||
/// rule); qualified ⇒ as-is. Sibling of `workspace::qualify_class_ref`
|
||||
/// for the check-side; duplicated to keep `ailang-check` from
|
||||
/// reaching across crates for one private helper.
|
||||
pub(crate) fn qualify_class_ref_in_check(class_ref: &str, caller_module: &str) -> String {
|
||||
if class_ref.contains('.') {
|
||||
class_ref.to_string()
|
||||
} else {
|
||||
format!("{caller_module}.{class_ref}")
|
||||
}
|
||||
}
|
||||
|
||||
/// Iter 22b.2 (Task 9): expand a list of declared class constraints
|
||||
/// with their one-step superclass closure (Decision 11). For every
|
||||
/// declared `(C, t)`, append `(S, t)` if class `C` has a superclass
|
||||
|
||||
@@ -341,12 +341,18 @@ fn poly_free_fn_names_for_module(
|
||||
/// Iter 22b.3: workspace-wide `class-name -> ClassDef` index.
|
||||
/// Used by the fixpoint to look up the matching class definition
|
||||
/// when synthesising a fn.
|
||||
///
|
||||
/// mq.1: keys carry the qualified class name
|
||||
/// (`<defining_module>.<Class>`) to match the post-mq.1 residual /
|
||||
/// `MonoTarget::ClassMethod.class` field and the registry's
|
||||
/// qualified entries key.
|
||||
fn build_class_index(ws: &Workspace) -> BTreeMap<String, ClassDef> {
|
||||
let mut idx = BTreeMap::new();
|
||||
for m in ws.modules.values() {
|
||||
for (mod_name, m) in &ws.modules {
|
||||
for d in &m.defs {
|
||||
if let Def::Class(c) = d {
|
||||
idx.insert(c.name.clone(), c.clone());
|
||||
let qualified = format!("{mod_name}.{}", c.name);
|
||||
idx.insert(qualified, c.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,11 +97,21 @@ fn build_env_inline_pre_refactor(ws: &ailang_core::workspace::Workspace) -> Env
|
||||
}
|
||||
|
||||
// env.class_superclasses — walk every module's Def::Class.
|
||||
for m in ws.modules.values() {
|
||||
//
|
||||
// mq.1: both key and value carry the qualified workspace-key shape;
|
||||
// bare `SuperclassRef.class` is lifted using the parent class's
|
||||
// defining module.
|
||||
for (mod_name, m) in &ws.modules {
|
||||
for d in &m.defs {
|
||||
if let Def::Class(cd) = d {
|
||||
if let Some(sc) = &cd.superclass {
|
||||
env.class_superclasses.insert(cd.name.clone(), sc.class.clone());
|
||||
let class_qualified = format!("{mod_name}.{}", cd.name);
|
||||
let sc_qualified = if sc.class.contains('.') {
|
||||
sc.class.clone()
|
||||
} else {
|
||||
format!("{mod_name}.{}", sc.class)
|
||||
};
|
||||
env.class_superclasses.insert(class_qualified, sc_qualified);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -258,7 +258,13 @@ pub struct Suppress {
|
||||
/// pre-22b hashes.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ClassDef {
|
||||
/// Class name (capitalised by convention).
|
||||
/// Class name (capitalised by convention). Bare — symmetric to
|
||||
/// `TypeDef.name`, the field is the defining-site context, not
|
||||
/// a reference. Cross-module class references live in
|
||||
/// `InstanceDef.class`, `Constraint.class`, and
|
||||
/// `SuperclassRef.class`; those fields carry the canonical form
|
||||
/// (bare for same-module, `<module>.<Class>` for cross-module)
|
||||
/// per mq.1.
|
||||
pub name: String,
|
||||
/// Single type-parameter name.
|
||||
pub param: String,
|
||||
@@ -275,7 +281,9 @@ pub struct ClassDef {
|
||||
/// Iter 22b.1: reference to a superclass relation in [`ClassDef`].
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct SuperclassRef {
|
||||
/// Superclass name.
|
||||
/// Superclass name in canonical form (mq.1): bare for a
|
||||
/// same-module class, `<module>.<Class>` for a cross-module
|
||||
/// class. Symmetric to ct.1's `Type::Con.name` rule.
|
||||
pub class: String,
|
||||
/// Type the superclass is applied to. MUST equal the parent
|
||||
/// `ClassDef.param` (validated in 22b.2 — schema does not enforce).
|
||||
@@ -310,7 +318,9 @@ pub struct ClassMethod {
|
||||
/// overrides of default-bearing methods.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct InstanceDef {
|
||||
/// Class being instantiated.
|
||||
/// Class being instantiated, in canonical form (mq.1): bare for
|
||||
/// a same-module class, `<module>.<Class>` for a cross-module
|
||||
/// class. Symmetric to ct.1's `Type::Con.name` rule.
|
||||
pub class: String,
|
||||
/// Concrete type the class is applied to.
|
||||
#[serde(rename = "type")]
|
||||
@@ -341,7 +351,9 @@ pub struct InstanceMethod {
|
||||
/// matching registry entry exists.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Constraint {
|
||||
/// Class name.
|
||||
/// Class name in canonical form (mq.1): bare for a same-module
|
||||
/// class, `<module>.<Class>` for a cross-module class.
|
||||
/// Symmetric to ct.1's `Type::Con.name` rule.
|
||||
pub class: String,
|
||||
/// Type the class is applied to.
|
||||
#[serde(rename = "type")]
|
||||
|
||||
@@ -293,20 +293,34 @@ mod tests {
|
||||
let dup_a_src = std::fs::read(examples.join("test_22b1_dup_a.ail.json"))
|
||||
.expect("examples/test_22b1_dup_a.ail.json present");
|
||||
let dup_a_mod: crate::ast::Module = serde_json::from_slice(&dup_a_src).unwrap();
|
||||
// test_22b1_dup_a has two defs: the Show class and an instance
|
||||
// of Show for test_22b1_dup_b.MyInt. Def::name() returns the
|
||||
// class name for both, so pin each by index. The instance
|
||||
// carries the migrated cross-module qualifier in its `type`.
|
||||
assert_eq!(dup_a_mod.defs.len(), 2, "test_22b1_dup_a expected to have exactly 2 defs (class + instance)");
|
||||
// mq.1: test_22b1_dup_a has one def post-migration — the instance
|
||||
// of `test_22b1_dup_classmod.Show` for `test_22b1_dup_b.MyInt`.
|
||||
// The `Show` class itself moved to `test_22b1_dup_classmod` to
|
||||
// break the dup_a ↔ dup_b cycle that the pre-mq.1 bare-class
|
||||
// shape required for cross-module duplicate-instance coverage.
|
||||
assert_eq!(dup_a_mod.defs.len(), 1, "test_22b1_dup_a expected to have exactly 1 def (the instance)");
|
||||
// Hash captured post-mq.1 migration; the instance carries the
|
||||
// canonical qualified `class` field. Computed by running this
|
||||
// test once with a length-only assertion and recording the
|
||||
// observed value. A future schema-touching change that shifts
|
||||
// this hash without intent triggers this pin.
|
||||
assert_eq!(
|
||||
def_hash(&dup_a_mod.defs[0]),
|
||||
"1c2573661ffd3da3",
|
||||
"test_22b1_dup_a class Show canonical hash must match captured post-migration value"
|
||||
"bdefb4ec75e046e4",
|
||||
"test_22b1_dup_a instance hash drifted; expected post-mq.1 captured value"
|
||||
);
|
||||
|
||||
// Also pin the new test_22b1_dup_classmod fixture's `Show` class
|
||||
// hash: the class moved here from dup_a verbatim (same method
|
||||
// signature), so the hash is independent of the migration.
|
||||
let dup_classmod_src = std::fs::read(examples.join("test_22b1_dup_classmod.ail.json"))
|
||||
.expect("examples/test_22b1_dup_classmod.ail.json present");
|
||||
let dup_classmod_mod: crate::ast::Module = serde_json::from_slice(&dup_classmod_src).unwrap();
|
||||
assert_eq!(dup_classmod_mod.defs.len(), 1, "test_22b1_dup_classmod expected to have exactly 1 def (class Show)");
|
||||
assert_eq!(
|
||||
def_hash(&dup_a_mod.defs[1]),
|
||||
"cc8685f92f246e40",
|
||||
"test_22b1_dup_a instance Show for test_22b1_dup_b.MyInt canonical hash must match captured post-migration value"
|
||||
def_hash(&dup_classmod_mod.defs[0]),
|
||||
"1c2573661ffd3da3",
|
||||
"test_22b1_dup_classmod class Show canonical hash must equal the pre-mq.1 dup_a class Show hash (same bytes)"
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
+632
-159
File diff suppressed because it is too large
Load Diff
@@ -41,8 +41,9 @@ fn two_modules_with_same_bare_foo_both_register() {
|
||||
args: vec![],
|
||||
});
|
||||
|
||||
let main_key = ("MyC".to_string(), main_foo_hash);
|
||||
let lib_key = ("MyC".to_string(), lib_foo_hash);
|
||||
// mq.1: registry-entries key is keyed by the qualified class name.
|
||||
let main_key = ("ctt2_collision_cls.MyC".to_string(), main_foo_hash);
|
||||
let lib_key = ("ctt2_collision_cls.MyC".to_string(), lib_foo_hash);
|
||||
|
||||
assert!(
|
||||
ws.registry.entries.contains_key(&main_key),
|
||||
|
||||
Reference in New Issue
Block a user