b586999e81
First iteration of the kernel-extension-mechanics milestone. Ships
the type-scoped `<TypeName>.<member>` resolution path as the
canonical form for type-associated operations, narrows the
`BareCrossModuleTypeRef` / `BadCrossModuleTypeRef` diagnostics from
"bare = strictly local" to "bare = in-scope by any path", migrates
12 std-library example fixtures, and introduces a workspace-wide
normalisation pre-pass `prepare_workspace_for_check` shared between
`check_workspace` and `monomorphise_workspace`.
Architectural discovery during implementation: the plan covered the
`Term::Var` dot-qualified resolver layer plus the workspace
validator's bare-name acceptance, but the migration of bare-form
fixtures exposed five sites where bare vs. qualified type-names
needed symmetric treatment — `Term::Ctor` resolution, `Type::Con`
well-formedness, mono's poly-free-fn name/constraint-count
enumeration, codegen's `lookup_ctor_by_type` bare-name path, and
the upstream desugar-then-qualify composition. Rather than
scattering TypeDef-first ladders across each site, the implementer
centralised the work into one pre-pass that walks every consumer
module's `Type::Con.name` and `Term::Ctor.type_name`, rewriting
bare cross-module references to their qualified `<home>.<Type>`
form. This is symmetric to the pre-existing `qualify_local_types`
(owner-side); the new pre-pass is the consumer-side mirror.
Downstream passes see qualified Types regardless of authoring form.
The TypeDef-first ladder still lives in `synth`'s `Term::Var` arm
because `<TypeName>.<member>` is term-position-only — `Maybe.from_maybe`
is a Var, not a Type expression, and the pre-pass does not rewrite
Var names.
Alternatives considered:
(a) Add TypeDef-first ladder at every resolution site separately
(the plan's implicit assumption). Rejected: O(N) extension
sites, each carrying the same workspace-walking logic; the
pre-pass version is O(1) — one pass, every downstream consumer
benefits.
(b) BLOCKED + spec re-brainstorm. Rejected: the architecture
extension is consistent with prep.1's thesis (bare type-name
resolves to the workspace-wide TypeDef) and forward-compatible
with prep.2 (Term::New.type_name falls under the same rewrite)
and prep.3 (kernel-tier TypeDefs enter the workspace map
automatically). No design regression to bounce back over.
Spec updated to document the realisation mechanism honestly: the
"Realisation mechanism — workspace pre-pass" subsection clarifies
that the resolver-level semantics described in "Implementation
shape" are the user-facing contract, and the actual code path is
the pre-pass.
Verification:
- `cargo test --workspace`: ALL GREEN. 87 e2e + every crate's unit
+ integration tests pass with no regressions.
- Three NEW in-source tests pin Task 1's resolver paths:
`type_scoped_member_resolves`, `type_scoped_member_not_found`,
`type_scoped_receiver_not_a_type`.
- One NEW workspace test pins the narrowed validator:
`ct1_validator_accepts_bare_with_explicit_import`.
- One renamed-and-flipped existing test:
`ct1_validator_rejects_bare_xmod_with_import_candidate` →
`ct1_validator_accepts_bare_xmod_with_import_candidate` (the
bare-with-import path is now ACCEPTED).
- One NEW companion test for the workspace-wide ctor lookup:
`ct2_term_ctor_bare_cross_module_via_workspace_resolves`.
- Two pre-existing tests' assertions updated for the new error
wording: `ct1_check_cli::check_human_mode_emits_actionable_message_to_stderr`
and `crates/ailang-check/tests/workspace.rs::unknown_module_prefix_is_reported`.
- 12 migrated `.ail` fixtures verified via the existing e2e
suite (each fixture is the test runner's target for an existing
`build_and_run` assertion).
- Negative fixture `ct_2_bare_cross_module.ail` semantically
preserved: dropped its `(import std_maybe)` so bare `Maybe` is
out-of-scope under the narrowed rule and still fires
`BareCrossModuleTypeRef`.
Concerns:
- The pre-pass introduces a new architectural layer (consumer-side
qualification) that the spec did not originally anticipate. Spec
amendment in this commit documents the layer. Future iterations
reference `prepare_workspace_for_check` as established
infrastructure.
- `examples/test_ct1_bare_xmod_rejected.ail.json` switched its
offending name from bare `Ordering` (which under the prep.1
semantics may now resolve via implicit prelude) to a still-
unresolvable `Mystery_Type`. The CLI test's intent (assert that
a human-mode `ail check` exits non-zero on a still-RED case) is
preserved.
Milestone status: kernel-extension-mechanics (Gitea #6) advances
1/3 iters. Next: prep.2 (`Term::New` construct) issue #32.
711 lines
23 KiB
Rust
711 lines
23 KiB
Rust
//! Integration tests for `check_workspace` (Iter 5b).
|
|
//!
|
|
//! These tests drive the canonical `examples/ws_*.ail.json` files;
|
|
//! the loader and the checker together form the pipeline that `ail check`
|
|
//! runs in JSON mode against a workspace.
|
|
|
|
use ailang_check::{check_workspace, Severity};
|
|
use ailang_surface::load_workspace;
|
|
use std::path::Path;
|
|
|
|
/// assert that the linearity check's `suggested_rewrites`
|
|
/// payload is non-empty AND each `replacement` parses as a form-A AILang
|
|
/// term. This is the contract the check promises to consumers of
|
|
/// `ail check --json`: a machine can take the replacement string and
|
|
/// substitute it back into the source without re-tokenising the term.
|
|
fn assert_suggested_rewrites_well_formed(d: &ailang_check::Diagnostic) {
|
|
assert!(
|
|
!d.suggested_rewrites.is_empty(),
|
|
"diagnostic {} (def={:?}) has no suggested_rewrites",
|
|
d.code,
|
|
d.def
|
|
);
|
|
for r in &d.suggested_rewrites {
|
|
ailang_surface::parse_term(&r.replacement).unwrap_or_else(|e| {
|
|
panic!(
|
|
"suggested rewrite for `{}` does not parse as form-A AILang: {:?}\nreplacement: {}",
|
|
d.code, e, r.replacement
|
|
)
|
|
});
|
|
}
|
|
}
|
|
|
|
fn examples_dir() -> std::path::PathBuf {
|
|
let manifest = env!("CARGO_MANIFEST_DIR");
|
|
Path::new(manifest).parent().unwrap().parent().unwrap().join("examples")
|
|
}
|
|
|
|
#[test]
|
|
fn happy_path_resolves_qualified_import() {
|
|
// ws_main imports ws_lib and calls `ws_lib.add` — fully typed.
|
|
// Expected: no diagnostics.
|
|
let entry = examples_dir().join("ws_main.ail");
|
|
let ws = load_workspace(&entry).expect("load ws_main");
|
|
let diags = check_workspace(&ws);
|
|
assert!(
|
|
diags.is_empty(),
|
|
"expected no diagnostics; got: {}",
|
|
serde_json::to_string_pretty(&diags).unwrap()
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn unknown_import_is_reported() {
|
|
// ws_broken references `ws_lib.bogus` — module is there, def isn't.
|
|
let entry = examples_dir().join("ws_broken.ail");
|
|
let ws = load_workspace(&entry).expect("load ws_broken");
|
|
let diags = check_workspace(&ws);
|
|
assert_eq!(diags.len(), 1, "got: {:?}", diags);
|
|
assert!(matches!(diags[0].severity, Severity::Error));
|
|
assert_eq!(diags[0].code, "unknown-import");
|
|
// Context must point structurally to module + def name.
|
|
assert_eq!(
|
|
diags[0].ctx.get("module").and_then(|v| v.as_str()),
|
|
Some("ws_lib")
|
|
);
|
|
assert_eq!(
|
|
diags[0].ctx.get("name").and_then(|v| v.as_str()),
|
|
Some("bogus")
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn unknown_module_prefix_is_reported() {
|
|
// ws_unknown_module has no imports but references `nope.x`. Under
|
|
// prep.1's type-scoped resolution, `nope` is neither a known
|
|
// TypeDef nor an imported module, so the diagnostic narrowed from
|
|
// the legacy `unknown-module` to `type-scoped-receiver-not-a-type`
|
|
// — a more precise wording for the same failure mode.
|
|
let entry = examples_dir().join("ws_unknown_module.ail");
|
|
let ws = load_workspace(&entry).expect("load ws_unknown_module");
|
|
let diags = check_workspace(&ws);
|
|
assert_eq!(diags.len(), 1, "got: {:?}", diags);
|
|
assert!(matches!(diags[0].severity, Severity::Error));
|
|
assert_eq!(diags[0].code, "type-scoped-receiver-not-a-type");
|
|
assert_eq!(
|
|
diags[0].ctx.get("name").and_then(|v| v.as_str()),
|
|
Some("nope")
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn invalid_def_name_with_dot_is_reported() {
|
|
// Synthetic: a module with a def whose name contains a dot.
|
|
// We construct this as a Module directly and feed it into a
|
|
// trivial workspace, because the canonical convention should not
|
|
// let this through to disk in the first place.
|
|
use ailang_core::ast::*;
|
|
use std::collections::BTreeMap;
|
|
|
|
let m = Module {
|
|
schema: ailang_core::SCHEMA.into(),
|
|
name: "t".into(),
|
|
imports: vec![],
|
|
defs: vec![Def::Const(ConstDef {
|
|
name: "weird.name".into(),
|
|
ty: Type::int(),
|
|
value: Term::Lit {
|
|
lit: Literal::Int { value: 0 },
|
|
},
|
|
doc: None,
|
|
})],
|
|
};
|
|
let mut modules = BTreeMap::new();
|
|
modules.insert(m.name.clone(), m.clone());
|
|
let ws = ailang_core::Workspace {
|
|
entry: m.name.clone(),
|
|
modules,
|
|
root_dir: std::path::PathBuf::from("."),
|
|
registry: ailang_core::workspace::Registry::default(),
|
|
};
|
|
let diags = check_workspace(&ws);
|
|
assert_eq!(diags.len(), 1, "got: {:?}", diags);
|
|
assert_eq!(diags[0].code, "invalid-def-name");
|
|
assert_eq!(
|
|
diags[0].ctx.get("reason").and_then(|v| v.as_str()),
|
|
Some("contains-dot")
|
|
);
|
|
}
|
|
|
|
/// body-check is multi-diagnose. A module with two independent
|
|
/// errors in different defs must produce two diagnostics — the first
|
|
/// error must not short-circuit the second.
|
|
#[test]
|
|
fn body_errors_accumulate_across_defs() {
|
|
use ailang_core::ast::*;
|
|
use std::collections::BTreeMap;
|
|
|
|
// Two fns, each with a different body error:
|
|
// bad_a: arity mismatch — calls `+` with three arguments.
|
|
// bad_b: references a name that does not exist anywhere.
|
|
let bad_a = Def::Fn(FnDef {
|
|
name: "bad_a".into(),
|
|
ty: Type::Fn {
|
|
params: vec![],
|
|
ret: Box::new(Type::int()),
|
|
effects: vec![],
|
|
param_modes: vec![],
|
|
ret_mode: ParamMode::Implicit,
|
|
},
|
|
params: vec![],
|
|
body: Term::App {
|
|
callee: Box::new(Term::Var { name: "+".into() }),
|
|
args: vec![
|
|
Term::Lit { lit: Literal::Int { value: 1 } },
|
|
Term::Lit { lit: Literal::Int { value: 2 } },
|
|
Term::Lit { lit: Literal::Int { value: 3 } },
|
|
],
|
|
tail: false,
|
|
},
|
|
suppress: vec![],
|
|
doc: None,
|
|
export: None,
|
|
});
|
|
let bad_b = Def::Fn(FnDef {
|
|
name: "bad_b".into(),
|
|
ty: Type::Fn {
|
|
params: vec![],
|
|
ret: Box::new(Type::int()),
|
|
effects: vec![],
|
|
param_modes: vec![],
|
|
ret_mode: ParamMode::Implicit,
|
|
},
|
|
params: vec![],
|
|
body: Term::Var {
|
|
name: "this_does_not_exist".into(),
|
|
},
|
|
suppress: vec![],
|
|
doc: None,
|
|
export: None,
|
|
});
|
|
|
|
let m = Module {
|
|
schema: ailang_core::SCHEMA.into(),
|
|
name: "two_errors".into(),
|
|
imports: vec![],
|
|
defs: vec![bad_a, bad_b],
|
|
};
|
|
let mut modules = BTreeMap::new();
|
|
modules.insert(m.name.clone(), m.clone());
|
|
let ws = ailang_core::Workspace {
|
|
entry: m.name.clone(),
|
|
modules,
|
|
root_dir: std::path::PathBuf::from("."),
|
|
registry: ailang_core::workspace::Registry::default(),
|
|
};
|
|
let diags = check_workspace(&ws);
|
|
|
|
assert_eq!(diags.len(), 2, "want both errors, got: {:#?}", diags);
|
|
|
|
// Each diagnostic carries the offending def name as `def`.
|
|
let defs: Vec<&str> = diags
|
|
.iter()
|
|
.filter_map(|d| d.def.as_deref())
|
|
.collect();
|
|
assert!(defs.contains(&"bad_a"), "missing bad_a; got {defs:?}");
|
|
assert!(defs.contains(&"bad_b"), "missing bad_b; got {defs:?}");
|
|
}
|
|
|
|
/// a fn with all-explicit modes that consumes its
|
|
/// `(own (con List))` parameter twice should trigger
|
|
/// `use-after-consume` on the second occurrence and ship a
|
|
/// non-empty, well-formed `suggested_rewrites`.
|
|
///
|
|
/// Body:
|
|
/// `(seq (app sum_list xs) (app sum_list xs))`
|
|
///
|
|
/// Both `xs` are in Own arg position; the second use is after consume.
|
|
#[test]
|
|
fn use_after_consume_on_own_param_is_reported() {
|
|
use ailang_core::ast::*;
|
|
use std::collections::BTreeMap;
|
|
|
|
// Helper: a fn that consumes a (con List) and returns Int.
|
|
let sum_list = Def::Fn(FnDef {
|
|
name: "sum_list".into(),
|
|
ty: Type::Fn {
|
|
params: vec![Type::Con { name: "List".into(), args: vec![] }],
|
|
param_modes: vec![ParamMode::Own],
|
|
ret: Box::new(Type::int()),
|
|
ret_mode: ParamMode::Implicit,
|
|
effects: vec![],
|
|
},
|
|
params: vec!["ys".into()],
|
|
body: Term::Lit { lit: Literal::Int { value: 0 } },
|
|
suppress: vec![],
|
|
doc: None,
|
|
export: None,
|
|
});
|
|
|
|
// The offending fn. Body is `(+ (sum_list xs) (sum_list xs))` — both
|
|
// arg slots of `+` are Implicit/Consume, so `xs` is consumed twice
|
|
// without intervening clone. The second occurrence triggers
|
|
// `use-after-consume`. (Using `+` rather than `Seq` keeps the
|
|
// function's return-type Int, which matches its declared signature.)
|
|
let bad = Def::Fn(FnDef {
|
|
name: "bad".into(),
|
|
ty: Type::Fn {
|
|
params: vec![Type::Con { name: "List".into(), args: vec![] }],
|
|
param_modes: vec![ParamMode::Own],
|
|
ret: Box::new(Type::int()),
|
|
ret_mode: ParamMode::Implicit,
|
|
effects: vec![],
|
|
},
|
|
params: vec!["xs".into()],
|
|
body: Term::App {
|
|
callee: Box::new(Term::Var { name: "+".into() }),
|
|
args: vec![
|
|
Term::App {
|
|
callee: Box::new(Term::Var { name: "sum_list".into() }),
|
|
args: vec![Term::Var { name: "xs".into() }],
|
|
tail: false,
|
|
},
|
|
Term::App {
|
|
callee: Box::new(Term::Var { name: "sum_list".into() }),
|
|
args: vec![Term::Var { name: "xs".into() }],
|
|
tail: false,
|
|
},
|
|
],
|
|
tail: false,
|
|
},
|
|
suppress: vec![],
|
|
doc: None,
|
|
export: None,
|
|
});
|
|
|
|
// List ADT (referenced by both fn types via `Type::Con`); a real
|
|
// module needs the type to be in-scope for `check_type_well_formed`.
|
|
let list_adt = Def::Type(TypeDef {
|
|
name: "List".into(),
|
|
vars: vec![],
|
|
ctors: vec![Ctor {
|
|
name: "Nil".into(),
|
|
fields: vec![],
|
|
}],
|
|
doc: None,
|
|
drop_iterative: false,
|
|
});
|
|
|
|
let m = Module {
|
|
schema: ailang_core::SCHEMA.into(),
|
|
name: "lin_uac".into(),
|
|
imports: vec![],
|
|
defs: vec![list_adt, sum_list, bad],
|
|
};
|
|
let mut modules = BTreeMap::new();
|
|
modules.insert(m.name.clone(), m.clone());
|
|
let ws = ailang_core::Workspace {
|
|
entry: m.name.clone(),
|
|
modules,
|
|
root_dir: std::path::PathBuf::from("."),
|
|
registry: ailang_core::workspace::Registry::default(),
|
|
};
|
|
let diags = check_workspace(&ws);
|
|
|
|
let lin: Vec<&ailang_check::Diagnostic> = diags
|
|
.iter()
|
|
.filter(|d| d.code == "use-after-consume")
|
|
.collect();
|
|
assert_eq!(
|
|
lin.len(),
|
|
1,
|
|
"want exactly one use-after-consume; got: {:#?}",
|
|
diags
|
|
);
|
|
let d = lin[0];
|
|
assert!(matches!(d.severity, Severity::Error));
|
|
assert_eq!(d.def.as_deref(), Some("bad"));
|
|
assert_eq!(
|
|
d.ctx.get("binder").and_then(|v| v.as_str()),
|
|
Some("xs"),
|
|
"ctx should name the offending binder; got {:?}",
|
|
d.ctx
|
|
);
|
|
assert_suggested_rewrites_well_formed(d);
|
|
}
|
|
|
|
/// a fn whose body passes `xs` to a `Borrow` arg and then,
|
|
/// in a SIBLING arg slot of the same call, consumes it via an `Own`
|
|
/// arg, should trigger `consume-while-borrowed`.
|
|
///
|
|
/// Body:
|
|
/// `(app dual_fn xs xs)`
|
|
/// where `dual_fn` has param_modes = [Borrow, Own].
|
|
#[test]
|
|
fn consume_while_borrowed_in_sibling_arg_is_reported() {
|
|
use ailang_core::ast::*;
|
|
use std::collections::BTreeMap;
|
|
|
|
let list_adt = Def::Type(TypeDef {
|
|
name: "List".into(),
|
|
vars: vec![],
|
|
ctors: vec![Ctor {
|
|
name: "Nil".into(),
|
|
fields: vec![],
|
|
}],
|
|
doc: None,
|
|
drop_iterative: false,
|
|
});
|
|
|
|
// dual_fn: (borrow List) → (own List) → Int.
|
|
let dual_fn = Def::Fn(FnDef {
|
|
name: "dual_fn".into(),
|
|
ty: Type::Fn {
|
|
params: vec![
|
|
Type::Con { name: "List".into(), args: vec![] },
|
|
Type::Con { name: "List".into(), args: vec![] },
|
|
],
|
|
param_modes: vec![ParamMode::Borrow, ParamMode::Own],
|
|
ret: Box::new(Type::int()),
|
|
ret_mode: ParamMode::Implicit,
|
|
effects: vec![],
|
|
},
|
|
params: vec!["a".into(), "b".into()],
|
|
body: Term::Lit { lit: Literal::Int { value: 0 } },
|
|
suppress: vec![],
|
|
doc: None,
|
|
export: None,
|
|
});
|
|
|
|
let bad = Def::Fn(FnDef {
|
|
name: "bad".into(),
|
|
ty: Type::Fn {
|
|
params: vec![Type::Con { name: "List".into(), args: vec![] }],
|
|
param_modes: vec![ParamMode::Own],
|
|
ret: Box::new(Type::int()),
|
|
ret_mode: ParamMode::Implicit,
|
|
effects: vec![],
|
|
},
|
|
params: vec!["xs".into()],
|
|
body: Term::App {
|
|
callee: Box::new(Term::Var { name: "dual_fn".into() }),
|
|
args: vec![
|
|
Term::Var { name: "xs".into() },
|
|
Term::Var { name: "xs".into() },
|
|
],
|
|
tail: false,
|
|
},
|
|
suppress: vec![],
|
|
doc: None,
|
|
export: None,
|
|
});
|
|
|
|
let m = Module {
|
|
schema: ailang_core::SCHEMA.into(),
|
|
name: "lin_cwb".into(),
|
|
imports: vec![],
|
|
defs: vec![list_adt, dual_fn, bad],
|
|
};
|
|
let mut modules = BTreeMap::new();
|
|
modules.insert(m.name.clone(), m.clone());
|
|
let ws = ailang_core::Workspace {
|
|
entry: m.name.clone(),
|
|
modules,
|
|
root_dir: std::path::PathBuf::from("."),
|
|
registry: ailang_core::workspace::Registry::default(),
|
|
};
|
|
let diags = check_workspace(&ws);
|
|
|
|
let lin: Vec<&ailang_check::Diagnostic> = diags
|
|
.iter()
|
|
.filter(|d| d.code == "consume-while-borrowed")
|
|
.collect();
|
|
assert_eq!(
|
|
lin.len(),
|
|
1,
|
|
"want exactly one consume-while-borrowed; got: {:#?}",
|
|
diags
|
|
);
|
|
let d = lin[0];
|
|
assert!(matches!(d.severity, Severity::Error));
|
|
assert_eq!(d.def.as_deref(), Some("bad"));
|
|
assert_eq!(
|
|
d.ctx.get("binder").and_then(|v| v.as_str()),
|
|
Some("xs"),
|
|
"ctx should name the offending binder; got {:?}",
|
|
d.ctx
|
|
);
|
|
assert_suggested_rewrites_well_formed(d);
|
|
}
|
|
|
|
/// happy-path `(reuse-as xs (term-ctor List Cons ...))`
|
|
/// inside an all-explicit-mode `map_inc`-style fn must produce NO
|
|
/// diagnostics — neither `reuse-as-non-allocating-body` (body is a
|
|
/// Ctor) nor `reuse-as-source-not-bare-var` (source is `xs`) nor
|
|
/// `use-after-consume` (xs is matched then consumed once via
|
|
/// reuse-as in the Cons arm; the merge across arms is consistent).
|
|
///
|
|
/// Body:
|
|
/// `(match xs
|
|
/// (case Nil (term-ctor List Nil))
|
|
/// (case (Cons h t)
|
|
/// (reuse-as xs (term-ctor List Cons (+ h 1) (app map_inc t)))))`
|
|
#[test]
|
|
fn reuse_as_happy_path_in_map_inc_is_linearity_clean() {
|
|
use ailang_core::ast::*;
|
|
use std::collections::BTreeMap;
|
|
|
|
let list_adt = Def::Type(TypeDef {
|
|
name: "List".into(),
|
|
vars: vec![],
|
|
ctors: vec![
|
|
Ctor { name: "Nil".into(), fields: vec![] },
|
|
Ctor {
|
|
name: "Cons".into(),
|
|
fields: vec![
|
|
Type::Con { name: "Int".into(), args: vec![] },
|
|
Type::Con { name: "List".into(), args: vec![] },
|
|
],
|
|
},
|
|
],
|
|
doc: None,
|
|
drop_iterative: false,
|
|
});
|
|
|
|
let map_inc_ty = Type::Fn {
|
|
params: vec![Type::Con { name: "List".into(), args: vec![] }],
|
|
param_modes: vec![ParamMode::Own],
|
|
ret: Box::new(Type::Con { name: "List".into(), args: vec![] }),
|
|
ret_mode: ParamMode::Own,
|
|
effects: vec![],
|
|
};
|
|
|
|
// Cons arm body:
|
|
// (reuse-as xs
|
|
// (term-ctor List Cons (app + h 1) (app map_inc t)))
|
|
let cons_arm_body = Term::ReuseAs {
|
|
source: Box::new(Term::Var { name: "xs".into() }),
|
|
body: Box::new(Term::Ctor {
|
|
type_name: "List".into(),
|
|
ctor: "Cons".into(),
|
|
args: vec![
|
|
Term::App {
|
|
callee: Box::new(Term::Var { name: "+".into() }),
|
|
args: vec![
|
|
Term::Var { name: "h".into() },
|
|
Term::Lit { lit: Literal::Int { value: 1 } },
|
|
],
|
|
tail: false,
|
|
},
|
|
Term::App {
|
|
callee: Box::new(Term::Var { name: "map_inc".into() }),
|
|
args: vec![Term::Var { name: "t".into() }],
|
|
tail: false,
|
|
},
|
|
],
|
|
}),
|
|
};
|
|
|
|
let map_inc_body = Term::Match {
|
|
scrutinee: Box::new(Term::Var { name: "xs".into() }),
|
|
arms: vec![
|
|
Arm {
|
|
pat: Pattern::Ctor { ctor: "Nil".into(), fields: vec![] },
|
|
body: Term::Ctor {
|
|
type_name: "List".into(),
|
|
ctor: "Nil".into(),
|
|
args: vec![],
|
|
},
|
|
},
|
|
Arm {
|
|
pat: Pattern::Ctor {
|
|
ctor: "Cons".into(),
|
|
fields: vec![
|
|
Pattern::Var { name: "h".into() },
|
|
Pattern::Var { name: "t".into() },
|
|
],
|
|
},
|
|
body: cons_arm_body,
|
|
},
|
|
],
|
|
};
|
|
|
|
let map_inc = Def::Fn(FnDef {
|
|
name: "map_inc".into(),
|
|
ty: map_inc_ty,
|
|
params: vec!["xs".into()],
|
|
body: map_inc_body,
|
|
suppress: vec![],
|
|
doc: None,
|
|
export: None,
|
|
});
|
|
|
|
let m = Module {
|
|
schema: ailang_core::SCHEMA.into(),
|
|
name: "reuse_as_happy".into(),
|
|
imports: vec![],
|
|
defs: vec![list_adt, map_inc],
|
|
};
|
|
let mut modules = BTreeMap::new();
|
|
modules.insert(m.name.clone(), m.clone());
|
|
let ws = ailang_core::Workspace {
|
|
entry: m.name.clone(),
|
|
modules,
|
|
root_dir: std::path::PathBuf::from("."),
|
|
registry: ailang_core::workspace::Registry::default(),
|
|
};
|
|
let diags = check_workspace(&ws);
|
|
assert!(
|
|
diags.is_empty(),
|
|
"happy-path reuse-as must check clean; got: {:#?}",
|
|
diags
|
|
);
|
|
}
|
|
|
|
/// a deliberately mismatched reuse-as — `(reuse-as xs
|
|
/// (Nil))` inside the Cons arm of a match on `xs` — must surface as
|
|
/// a `reuse-as-shape-mismatch` diagnostic. The source's path-ctor
|
|
/// (Cons, 2 fields) and the body ctor (Nil, 0 fields) have different
|
|
/// shapes; the in-place rewrite is unsafe; the build must fail.
|
|
///
|
|
/// The diagnostic must:
|
|
/// - carry code `reuse-as-shape-mismatch`
|
|
/// - identify the offending def (`f`)
|
|
/// - record `ctx.reason` as a stable kebab-case sub-code (here:
|
|
/// `field-count-mismatch`) so JSON consumers can branch on the
|
|
/// specific failure mode without prose-parsing
|
|
/// - ship a non-empty `suggested_rewrites` whose replacement parses
|
|
/// back as form-A AILang (the rewrite drops the wrapper and keeps
|
|
/// the body alone)
|
|
#[test]
|
|
fn reuse_as_shape_mismatch_is_reported_on_cons_to_nil() {
|
|
use ailang_core::ast::*;
|
|
use std::collections::BTreeMap;
|
|
|
|
let list_adt = Def::Type(TypeDef {
|
|
name: "List".into(),
|
|
vars: vec![],
|
|
ctors: vec![
|
|
Ctor { name: "Nil".into(), fields: vec![] },
|
|
Ctor {
|
|
name: "Cons".into(),
|
|
fields: vec![
|
|
Type::Con { name: "Int".into(), args: vec![] },
|
|
Type::Con { name: "List".into(), args: vec![] },
|
|
],
|
|
},
|
|
],
|
|
doc: None,
|
|
drop_iterative: false,
|
|
});
|
|
|
|
let f_ty = Type::Fn {
|
|
params: vec![Type::Con { name: "List".into(), args: vec![] }],
|
|
param_modes: vec![ParamMode::Own],
|
|
ret: Box::new(Type::Con { name: "List".into(), args: vec![] }),
|
|
ret_mode: ParamMode::Own,
|
|
effects: vec![],
|
|
};
|
|
|
|
// Body:
|
|
// (match xs
|
|
// (Nil → Nil)
|
|
// (Cons h t → (reuse-as xs Nil))) ; BAD: 2-field Cons → 0-field Nil.
|
|
let cons_arm_body = Term::ReuseAs {
|
|
source: Box::new(Term::Var { name: "xs".into() }),
|
|
body: Box::new(Term::Ctor {
|
|
type_name: "List".into(),
|
|
ctor: "Nil".into(),
|
|
args: vec![],
|
|
}),
|
|
};
|
|
let body = Term::Match {
|
|
scrutinee: Box::new(Term::Var { name: "xs".into() }),
|
|
arms: vec![
|
|
Arm {
|
|
pat: Pattern::Ctor { ctor: "Nil".into(), fields: vec![] },
|
|
body: Term::Ctor {
|
|
type_name: "List".into(),
|
|
ctor: "Nil".into(),
|
|
args: vec![],
|
|
},
|
|
},
|
|
Arm {
|
|
pat: Pattern::Ctor {
|
|
ctor: "Cons".into(),
|
|
fields: vec![
|
|
Pattern::Var { name: "h".into() },
|
|
Pattern::Var { name: "t".into() },
|
|
],
|
|
},
|
|
body: cons_arm_body,
|
|
},
|
|
],
|
|
};
|
|
|
|
let f = Def::Fn(FnDef {
|
|
name: "f".into(),
|
|
ty: f_ty,
|
|
params: vec!["xs".into()],
|
|
body,
|
|
suppress: vec![],
|
|
doc: None,
|
|
export: None,
|
|
});
|
|
|
|
let m = Module {
|
|
schema: ailang_core::SCHEMA.into(),
|
|
name: "reuse_as_mismatch".into(),
|
|
imports: vec![],
|
|
defs: vec![list_adt, f],
|
|
};
|
|
let mut modules = BTreeMap::new();
|
|
modules.insert(m.name.clone(), m.clone());
|
|
let ws = ailang_core::Workspace {
|
|
entry: m.name.clone(),
|
|
modules,
|
|
root_dir: std::path::PathBuf::from("."),
|
|
registry: ailang_core::workspace::Registry::default(),
|
|
};
|
|
let diags = check_workspace(&ws);
|
|
let shape_diags: Vec<&ailang_check::Diagnostic> = diags
|
|
.iter()
|
|
.filter(|d| d.code == "reuse-as-shape-mismatch")
|
|
.collect();
|
|
assert_eq!(
|
|
shape_diags.len(),
|
|
1,
|
|
"expected exactly one reuse-as-shape-mismatch diagnostic; got: {diags:#?}"
|
|
);
|
|
let d = shape_diags[0];
|
|
assert!(matches!(d.severity, Severity::Error));
|
|
assert_eq!(d.def.as_deref(), Some("f"));
|
|
assert_eq!(
|
|
d.ctx.get("reason").and_then(|v| v.as_str()),
|
|
Some("field-count-mismatch"),
|
|
"ctx.reason must be the stable sub-code; ctx was: {}",
|
|
d.ctx
|
|
);
|
|
assert_suggested_rewrites_well_formed(d);
|
|
}
|
|
|
|
/// positive control. The ON-DISK `borrow_own_demo` fixture
|
|
/// (the only currently-shipping all-explicit-mode program) must remain
|
|
/// linearity-clean. If this regresses, the check has become incorrect:
|
|
/// `borrow_own_demo`'s `list_length` (borrow) and `sum_list` (own)
|
|
/// are exactly the canonical accept shape.
|
|
#[test]
|
|
fn borrow_own_demo_is_linearity_clean() {
|
|
let entry = examples_dir().join("borrow_own_demo.ail");
|
|
let ws = load_workspace(&entry).expect("load borrow_own_demo");
|
|
let diags = check_workspace(&ws);
|
|
let lin: Vec<&ailang_check::Diagnostic> = diags
|
|
.iter()
|
|
.filter(|d| {
|
|
d.code == "use-after-consume" || d.code == "consume-while-borrowed"
|
|
})
|
|
.collect();
|
|
assert!(
|
|
lin.is_empty(),
|
|
"borrow_own_demo must stay linearity-clean; got: {:#?}",
|
|
lin
|
|
);
|
|
// Belt-and-braces: the *whole* check should be clean too — modes
|
|
// are still metadata-only at the typechecker level (Iter 18a).
|
|
assert!(
|
|
diags.is_empty(),
|
|
"borrow_own_demo must check clean; got: {:#?}",
|
|
diags
|
|
);
|
|
}
|