2e6a4ca200
Installs the dispatch infrastructure for type-driven method resolution without retiring MethodNameCollision yet. The new multi-candidate path is exercised exclusively by 15 unit tests; real workspaces continue producing single-class residuals (candidates: None) and every pre-mq.2 fixture typechecks unchanged. Three new CheckError variants: - AmbiguousMethodResolution (multi-candidate after type-driven filter) - UnknownClass (explicit qualifier names a class not in registry) - NoInstance gains additive candidate_classes field (Vec<String>) Schema/Env additions: - Env.method_to_candidate_classes: BTreeMap<String, BTreeSet<String>> workspace-flat inverse of class_methods, built in build_check_env. - ResidualConstraint.candidates: Option<BTreeSet<String>> — None preserves pre-mq.2 single-class semantics; Some(set) carries the multi-candidate residual that discharge refines. - ResidualConstraint visibility bumped pub(crate) → pub for unit-test crate access. New helpers: - MethodDispatchOutcome enum + pure resolve_method_dispatch implementing the spec's 5-step rule (qualifier → singleton → type-driven filter → constraint-driven filter → Multi for discharge-time refinement). - parse_method_qualifier splits Term::Var.name into (method_name, optional_qualifier_prefix) at the last dot. - RefineOutcome enum + refine_multi_candidate_residual for discharge-time refinement. - resolve_residual_class_for_mono wires the refinement into mono's collect_residuals_ordered residual-to-target mapping. Synth Var-arm class-method branch rewritten via parse_method_qualifier with inner-dot gate (qualifier must be <module>.<Class>; single-dot names fall through to the existing qualified-fn path). Constraint-discharge in check_fn uses expanded (post-superclass- expansion) constraints for the rigid-var path — sounder than raw declared_constraints. Plan-invented format_type_for_display replaced with the existing ailang_core::pretty::type_to_string (one less duplicate). Synth-time declared_constraints: &[] is a deliberate gap documented as known debt — load-bearing only post-mq.3 for the rigid-var fallback (env-plumbing the active fn's constraints into the Var arm is a ~10-line edit slated for mq.3). 9/9 tasks, 539 tests green (was 520 pre-mq.2; +15 mq.2 unit tests + 4 pre-existing). bench/compile_check.py + cross_lang.py clean; bench/check.py 1 regression (latency noise — runtime cannot be touched by a typecheck-side iter).
155 lines
5.1 KiB
Rust
155 lines
5.1 KiB
Rust
//! mq.2.5: pin tests on `resolve_method_dispatch` — the new
|
|
//! dispatch-resolution helper that synth's `Term::Var` arm consults
|
|
//! per the spec's 5-step rule.
|
|
//!
|
|
//! Six cases:
|
|
//! 1. Unique candidate.
|
|
//! 2. Multi + explicit qualifier matching one.
|
|
//! 3. Multi + explicit qualifier matching none → UnknownClass.
|
|
//! 4. Multi + type-driven filter narrows to one.
|
|
//! 5. Multi + constraint-driven filter narrows to one (rigid var case).
|
|
//! 6. Multi + true ambiguity → AmbiguousMethodResolution.
|
|
|
|
use ailang_check::{resolve_method_dispatch, MethodDispatchOutcome};
|
|
use ailang_core::ast::{Constraint, Type};
|
|
use ailang_core::canonical;
|
|
use std::collections::{BTreeMap, BTreeSet};
|
|
|
|
fn registry_with(entries: &[(&str, &str)]) -> BTreeMap<(String, String), ()> {
|
|
// Returns a synthetic registry: keys are (qualified_class, type_hash)
|
|
// values are unit (instance presence flag).
|
|
let mut reg = BTreeMap::new();
|
|
for (cls, ty_name) in entries {
|
|
let t = Type::Con { name: ty_name.to_string(), args: vec![] };
|
|
let h = canonical::type_hash(&t);
|
|
reg.insert((cls.to_string(), h), ());
|
|
}
|
|
reg
|
|
}
|
|
|
|
fn candidates_of(classes: &[&str]) -> BTreeSet<String> {
|
|
classes.iter().map(|s| s.to_string()).collect()
|
|
}
|
|
|
|
/// Case 1: unique candidate → returns that class.
|
|
#[test]
|
|
fn case1_unique_candidate_resolves() {
|
|
let candidates = candidates_of(&["prelude.Eq"]);
|
|
let registry = registry_with(&[("prelude.Eq", "Int")]);
|
|
let arg_ty = Type::Con { name: "Int".to_string(), args: vec![] };
|
|
let result = resolve_method_dispatch(
|
|
/*method*/ "eq",
|
|
/*qualifier_prefix*/ None,
|
|
/*candidates*/ &candidates,
|
|
/*concrete_arg_type*/ Some(&arg_ty),
|
|
/*declared_constraints*/ &[],
|
|
/*registry*/ ®istry,
|
|
);
|
|
assert_eq!(result, MethodDispatchOutcome::Resolved("prelude.Eq".to_string()));
|
|
}
|
|
|
|
/// Case 2: multi + explicit qualifier matching one → returns that class.
|
|
#[test]
|
|
fn case2_qualifier_matches_one() {
|
|
let candidates = candidates_of(&["prelude.Show", "userlib.Show"]);
|
|
let registry = registry_with(&[
|
|
("prelude.Show", "Int"),
|
|
("userlib.Show", "Int"),
|
|
]);
|
|
let arg_ty = Type::Con { name: "Int".to_string(), args: vec![] };
|
|
let result = resolve_method_dispatch(
|
|
"show",
|
|
Some("userlib.Show"),
|
|
&candidates,
|
|
Some(&arg_ty),
|
|
&[],
|
|
®istry,
|
|
);
|
|
assert_eq!(result, MethodDispatchOutcome::Resolved("userlib.Show".to_string()));
|
|
}
|
|
|
|
/// Case 3: multi + explicit qualifier matching none → UnknownClass.
|
|
#[test]
|
|
fn case3_qualifier_matches_none_unknown_class() {
|
|
let candidates = candidates_of(&["prelude.Show", "userlib.Show"]);
|
|
let registry = registry_with(&[("prelude.Show", "Int")]);
|
|
let arg_ty = Type::Con { name: "Int".to_string(), args: vec![] };
|
|
let result = resolve_method_dispatch(
|
|
"show",
|
|
Some("nonexistent.Show"),
|
|
&candidates,
|
|
Some(&arg_ty),
|
|
&[],
|
|
®istry,
|
|
);
|
|
assert_eq!(
|
|
result,
|
|
MethodDispatchOutcome::UnknownClass("nonexistent.Show".to_string()),
|
|
);
|
|
}
|
|
|
|
/// Case 4: multi + type-driven filter narrows to one → returns that class.
|
|
#[test]
|
|
fn case4_type_driven_filter_narrows_to_one() {
|
|
let candidates = candidates_of(&["prelude.Show", "userlib.Show"]);
|
|
let registry = registry_with(&[("prelude.Show", "Int")]); // only prelude.Show has Show Int
|
|
let arg_ty = Type::Con { name: "Int".to_string(), args: vec![] };
|
|
let result = resolve_method_dispatch(
|
|
"show",
|
|
None,
|
|
&candidates,
|
|
Some(&arg_ty),
|
|
&[],
|
|
®istry,
|
|
);
|
|
assert_eq!(result, MethodDispatchOutcome::Resolved("prelude.Show".to_string()));
|
|
}
|
|
|
|
/// Case 5: multi + constraint-driven filter narrows to one (rigid var) → returns that class.
|
|
#[test]
|
|
fn case5_constraint_driven_filter_narrows_to_one() {
|
|
let candidates = candidates_of(&["prelude.Show", "userlib.Show"]);
|
|
let registry = registry_with(&[]);
|
|
let rigid_a = Type::Var { name: "a".to_string() };
|
|
let declared = vec![Constraint {
|
|
class: "prelude.Show".to_string(),
|
|
type_: Type::Var { name: "a".to_string() },
|
|
}];
|
|
let result = resolve_method_dispatch(
|
|
"show",
|
|
None,
|
|
&candidates,
|
|
Some(&rigid_a), // rigid var, can't drive registry lookup
|
|
&declared,
|
|
®istry,
|
|
);
|
|
assert_eq!(result, MethodDispatchOutcome::Resolved("prelude.Show".to_string()));
|
|
}
|
|
|
|
/// Case 6: multi + true ambiguity → AmbiguousMethodResolution.
|
|
#[test]
|
|
fn case6_true_ambiguity() {
|
|
let candidates = candidates_of(&["prelude.Show", "userlib.Show"]);
|
|
let registry = registry_with(&[
|
|
("prelude.Show", "Int"),
|
|
("userlib.Show", "Int"),
|
|
]);
|
|
let arg_ty = Type::Con { name: "Int".to_string(), args: vec![] };
|
|
let result = resolve_method_dispatch(
|
|
"show",
|
|
None,
|
|
&candidates,
|
|
Some(&arg_ty),
|
|
&[],
|
|
®istry,
|
|
);
|
|
assert_eq!(
|
|
result,
|
|
MethodDispatchOutcome::Ambiguous {
|
|
method: "show".to_string(),
|
|
at_type: "Int".to_string(),
|
|
candidates: vec!["prelude.Show".to_string(), "userlib.Show".to_string()],
|
|
},
|
|
);
|
|
}
|