iter ct.3.1: codegen lookup_ctor_by_type direct lookup; delete imports-fallback

This commit is contained in:
2026-05-11 09:31:48 +02:00
parent d4927b2297
commit a8a58cc41e
+35 -60
View File
@@ -376,15 +376,15 @@ fn lower_workspace_inner(ws: &Workspace, alloc: AllocStrategy) -> Result<String>
let key = imp.alias.clone().unwrap_or_else(|| imp.module.clone()); let key = imp.alias.clone().unwrap_or_else(|| imp.module.clone());
import_map.insert(key, imp.module.clone()); import_map.insert(key, imp.module.clone());
} }
// Iter 23.2 (fourth lockstep site, after Pattern::Ctor 15a / // Iter 23.2: mirror the typechecker's implicit-prelude import
// Term::Ctor typecheck / lookup_ctor_by_type codegen): mirror // injection (crates/ailang-check/src/lib.rs:1198 and :1300).
// the typechecker's implicit-prelude import injection // User modules that call prelude-resident fns synthesised by
// (crates/ailang-check/src/lib.rs:1198 and :1300). User // Iter 22b.3 monomorphisation (e.g. `prelude.eq__Int` from a
// modules that call prelude-resident fns synthesised by Iter // user calling `eq x y` on Ints) need the codegen-side
// 22b.3 monomorphisation (e.g. `prelude.eq__Int` from a user // import_map to resolve the `prelude.` prefix at the call
// calling `eq x y` on Ints) need the codegen-side import_map // site (`lower_call`'s prefix lookup). Post-ct.3 ctor lookup
// to resolve the `prelude.` prefix at the call site // is canonical (bare = local, qualified routes via import_map);
// (`lower_call`'s prefix lookup). // this entry serves fn-name resolution only.
if m.name != "prelude" { if m.name != "prelude" {
import_map import_map
.entry("prelude".to_string()) .entry("prelude".to_string())
@@ -1676,25 +1676,13 @@ impl<'a> Emitter<'a> {
} }
} }
/// Iter 15a: resolves a ctor reference by `type_name` (which may be /// Resolves a `Term::Ctor.type_name` (canonical post-ct.1: bare
/// qualified `module.T` or bare `T`) plus a bare ctor name. Returns /// = local TypeDef, qualified `<owner>.<type>` = explicit
/// the same `CtorRef` shape used by the local `ctor_index`. The /// cross-module) to the codegen-side `CtorRef`. Qualified names
/// returned `CtorRef.type_name` is always the bare type name as /// route through `import_map`; bare names hit the current
/// declared in the owning module. The current `module_name` is the /// module's `module_ctor_index` directly. No imports-walk
/// authority for "bare" — that lets a specialised fn body emitted /// fallback — the typechecker (post-ct.2) and the workspace
/// under a swapped `module_name` (see `emit_specialised_fn`) /// validator (post-ct.1) have already pinned canonical form.
/// resolve its bare ctor references against the *owner* module's
/// ctor table, not the consumer's.
///
/// Iter 23.1: bare-type lookup now falls back to imported modules
/// when the ctor is not local — mirroring `lookup_ctor_in_pattern`.
/// The typechecker (Iter 23.1 Task 3) already accepts a bare
/// `Term::Ctor { type_name, ctor }` whose `type_name` is owned by
/// an imported module (the implicit `prelude` import is the
/// motivating case: an `Ordering` ctor `LT` written without an
/// explicit `imports: ["prelude"]`). Codegen must honour the same
/// resolution path so the lockstep `Pattern::Ctor ↔ Term::Ctor ↔
/// codegen` is preserved.
pub(crate) fn lookup_ctor_by_type( pub(crate) fn lookup_ctor_by_type(
&self, &self,
type_name: &str, type_name: &str,
@@ -1725,43 +1713,30 @@ impl<'a> Emitter<'a> {
} }
Ok(cref) Ok(cref)
} else { } else {
// Local-first: current module's ctor table wins on a // ct.3 Task 1: bare type_name is canonical (post-ct.1
// type-matching hit. A local ctor with the same name but // validator) ⇒ local. Hit the current module's ctor
// a DIFFERENT `type_name` is not a conflict — typecheck // table directly; non-match is a hard error. No
// has already pinned which type is meant, so the lookup // imports-walk — the typechecker has already pinned
// falls through to the imports-fallback. Only an // type_name to the owning module.
// exhausted scan yields `Err`. let cref = self
if let Some(cref) = self
.module_ctor_index .module_ctor_index
.get(self.module_name) .get(self.module_name)
.and_then(|m| m.get(ctor_name)) .and_then(|m| m.get(ctor_name))
.cloned() .cloned()
{ .ok_or_else(|| {
if cref.type_name == type_name { CodegenError::Internal(format!(
return Ok(cref); "unknown ctor `{ctor_name}` for type `{type_name}` in module `{}`",
} self.module_name
// type_name mismatch — keep looking in imports. ))
})?;
if cref.type_name != type_name {
return Err(CodegenError::Internal(format!(
"ctor `{ctor_name}` belongs to local type `{}`, not `{type_name}`; \
cross-module ctor refs require qualified type_name",
cref.type_name
)));
} }
// Iter 23.1: fallback — scan other modules for a bare ctor Ok(cref)
// whose declared `type_name` matches. The typechecker has
// already pinned uniqueness (an ambiguous bare type name
// is rejected with `CheckError::AmbiguousType`), so the
// first hit whose owning type matches the requested
// `type_name` is unique. Mirrors `lookup_ctor_in_pattern`.
for (mname, ctors) in self.module_ctor_index.iter() {
if mname == self.module_name {
continue;
}
if let Some(cref) = ctors.get(ctor_name).cloned() {
if cref.type_name == type_name {
return Ok(cref);
}
}
}
Err(CodegenError::Internal(format!(
"unknown ctor `{ctor_name}` for type `{type_name}` in module `{}`",
self.module_name
)))
} }
} }