iter 22b.3.2: mono_symbol — primitive surface forms + hash for compound

This commit is contained in:
2026-05-09 20:21:24 +02:00
parent 108115f7dd
commit 028227587b
3 changed files with 87 additions and 2 deletions
+42
View File
@@ -5,6 +5,7 @@
//! `examples_dir()` pattern — `cargo test`-cwd is the crate dir,
//! the fixtures live in `<repo>/examples/`.
use ailang_core::ast::Type;
use std::path::PathBuf;
fn examples_dir() -> PathBuf {
@@ -93,3 +94,44 @@ fn monomorphise_workspace_is_identity_when_no_concrete_call_sites() {
);
}
}
#[test]
fn mono_symbol_primitive_types_use_surface_form() {
// Primitive-type forms must be human-readable in diagnostics
// and ABI symbols. Per the spec's mono-symbol naming guidance:
// <method>#<surface-name> for primitives.
let int_ty = Type::int();
let bool_ty = Type::bool_();
let str_ty = Type::str_();
let unit_ty = Type::unit();
assert_eq!(ailang_check::mono::mono_symbol("show", &int_ty), "show#Int");
assert_eq!(ailang_check::mono::mono_symbol("eq", &bool_ty), "eq#Bool");
assert_eq!(ailang_check::mono::mono_symbol("show", &str_ty), "show#Str");
assert_eq!(ailang_check::mono::mono_symbol("foo", &unit_ty), "foo#Unit");
}
#[test]
fn mono_symbol_compound_type_uses_hash_suffix() {
// Compound types fall through to canonical type_hash with an
// 8-hex-char prefix. Stability matters more than legibility
// here — same input must produce the same name across runs.
let pair_ty = Type::Con {
name: "Pair".into(),
args: vec![Type::int(), Type::bool_()],
};
let name = ailang_check::mono::mono_symbol("show", &pair_ty);
// Format: `show#<8-hex-chars>`.
assert!(name.starts_with("show#"), "name = {name}");
let suffix = &name["show#".len()..];
assert_eq!(suffix.len(), 8, "compound suffix is 8 hex chars: {suffix}");
assert!(
suffix.chars().all(|c| c.is_ascii_hexdigit()),
"compound suffix must be hex: {suffix}"
);
// Stability: a second call with the same type produces the same name.
let again = ailang_check::mono::mono_symbol("show", &pair_ty);
assert_eq!(name, again, "mono_symbol must be deterministic");
}