feat(lower): source-aware forward-alias fixpoint [stdlib E1.5]

resolveForwardIdentifierAliases now resolves a forward alias A :: B against
B AS SEEN FROM A's own source via selectNominalLeaf (E1's source-keyed
nominal leaf over type_aliases_by_source / moduleTypeAuthor), never the
global type_alias_map / global findByName. The already-resolved guard is
per-source (aliasResolvedInSource). .pending routes back into the fixpoint;
.undeclared / .not_visible leave A unwritten (no global last-wins leak).

This is the sequencing pin before E2: a global fixpoint binds A to a
same-name B authored by a different module (e.g. a namespaced import that
pollutes the global alias map last-wins), re-opening 0105 one layer down
once shadows register. Writes stay on the unified putTypeAlias helper (E1
no-drift invariant); the single graph-walk in resolver.zig is untouched.

Regression: examples/0750-modules-forward-alias-source-aware — a forward
alias A :: B with main's own B :: u64 and a namespaced same-name B :: u8;
A must bind main's u64 (300), not the global last-wins u8 (44).
This commit is contained in:
agra
2026-06-07 20:43:01 +03:00
parent 4a10c9a291
commit 2d34993586
6 changed files with 88 additions and 16 deletions

View File

@@ -0,0 +1,31 @@
// Source-aware forward-alias fixpoint (R5 §4, E1.5). A forward identifier alias
// `A :: B` must resolve its target `B` AS SEEN FROM ITS OWN SOURCE, not via the
// global `type_alias_map` (which is last-wins across every module).
//
// `main` authors a forward alias `A :: B` and its own `B :: u64`. The namespaced
// import `ns :: #import ".../dep.sx"` ALSO authors a top-level `B :: u8`; being
// scanned after main's `B`, dep's alias is what the GLOBAL `type_alias_map["B"]`
// ends up holding (last-wins). A global forward-alias fixpoint therefore bound
// `A` to dep's `u8` — re-opening 0105 one layer down. The source-aware fixpoint
// resolves `A`'s target against MAIN's source, binding the local `B :: u64`.
//
// Observable: a runtime 300 coerced into an `A`-typed slot round-trips as 300
// when `A` is `u64` (correct) and truncates to 44 when `A` is wrongly `u8`.
// The direct reference `b : B` already resolves source-aware via E1's nominal
// leaf, so it pins the same `u64` for contrast. Regression (stdlib E1.5).
#import "modules/std.sx";
A :: B;
B :: u64;
ns :: #import "0750-modules-forward-alias-source-aware/dep.sx";
main :: () -> s32 {
n : s64 = 300;
a : A = xx n;
b : B = xx n;
print("forward A (u64=300): {}\n", cast(s64) a);
print("direct B (u64=300): {}\n", cast(s64) b);
print("ns.width(): {}\n", ns.width());
return 0;
}

View File

@@ -0,0 +1,10 @@
// Namespaced helper module. It authors a top-level type alias `B` whose
// spelling collides with the importer's own `B`. Because the import is
// NAMESPACED (`ns :: #import`), `dep.B` is NOT flat-visible to the importer —
// but its alias write still lands in the global `type_alias_map` (last-wins),
// which is exactly what the source-aware forward-alias fixpoint must ignore.
B :: u8;
width :: () -> s32 {
return 8;
}

View File

@@ -0,0 +1 @@
0

View File

@@ -0,0 +1,3 @@
forward A (u64=300): 300
direct B (u64=300): 300
ns.width(): 8

View File

@@ -1360,14 +1360,30 @@ pub const Lowering = struct {
/// Resolve identifier-RHS type aliases whose target is declared LATER in the
/// file. The forward scan above only registers an alias (`A :: B`) when `B`
/// is already in `type_alias_map` / the `TypeTable`; a forward target isn't
/// yet present, so `A` is left unregistered and its uses get falsely flagged
/// as an unknown type (issue 0069). Re-resolve to a fixpoint now that every
/// top-level name has been seen, so `A :: B; B :: s32;` converges the same as
/// the ordered `B :: s32; A :: B;`. A value const is never an `.identifier`
/// node (`NotAType :: 123` is an int literal), and an alias whose target is a
/// value const still misses both lookups, so neither this pass nor issue 0068
/// can register a non-type name.
/// is already resolved as a type author; a forward target isn't yet present,
/// so `A` is left unregistered and its uses get falsely flagged as an unknown
/// type (issue 0069). Re-resolve to a fixpoint now that every top-level name
/// has been seen, so `A :: B; B :: s32;` converges the same as the ordered
/// `B :: s32; A :: B;`. A value const is never an `.identifier` node
/// (`NotAType :: 123` is an int literal), and an alias whose target is a value
/// const stays unresolved, so neither this pass nor issue 0068 can register a
/// non-type name.
///
/// SOURCE-AWARE (R5 §4, E1.5). The target `B` is resolved AS SEEN FROM `A`'s
/// OWN source via the source-aware nominal leaf (`selectNominalLeaf` over
/// `type_aliases_by_source` / `moduleTypeAuthor` — E1), NEVER the global
/// `type_alias_map` / global `findByName`. The "already resolved" guard is
/// likewise per-source. When a same-name `B` is authored by a *different*
/// source (e.g. a namespaced import polluting the global alias map last-wins),
/// a global fixpoint would bind `A` to the wrong `B` and re-open 0105 one
/// layer down once E2 registers shadows; resolving against `A`'s source binds
/// the local `B`. The `.pending` outcome (B is itself a not-yet-resolved
/// forward alias) routes BACK into this fixpoint — `A` is skipped this round
/// and converges on a later iteration. `.undeclared` (no type author) and
/// `.not_visible` (a namespaced-only type, not bare-aliasable) leave `A`
/// unwritten; its uses surface the stub / diagnostic, never a silent global
/// leak. The write stays on the unified `putTypeAlias` helper (E1 no-drift
/// invariant — only the helper touches the maps).
fn resolveForwardIdentifierAliases(self: *Lowering, decls: []const *const Node) void {
var progressed = true;
while (progressed) {
@@ -1378,22 +1394,32 @@ pub const Lowering = struct {
else => continue,
};
if (cd.value.data != .identifier) continue;
if (self.program_index.type_alias_map.contains(cd.name)) continue;
const src = decl.source_file orelse self.main_file orelse continue;
if (self.aliasResolvedInSource(src, cd.name)) continue;
const rhs_name = cd.value.data.identifier.name;
if (self.program_index.type_alias_map.get(rhs_name)) |chained| {
self.putTypeAlias(decl.source_file, cd.name, chained);
progressed = true;
} else {
const name_id = self.module.types.internString(rhs_name);
if (self.module.types.findByName(name_id)) |tid| {
switch (self.selectNominalLeaf(rhs_name, src, false)) {
.resolved => |tid| {
self.putTypeAlias(decl.source_file, cd.name, tid);
progressed = true;
}
},
// B not yet a resolved type author from this source: a forward
// alias still pending (re-tried next round), an undeclared
// name, or a namespaced-only type that is not bare-aliasable.
// Leave A unwritten — no global last-wins leak.
.pending, .undeclared, .not_visible => {},
}
}
}
}
/// TRUE iff `name` is already recorded as a type alias FROM `src` — the
/// per-source analogue of `type_alias_map.contains`, so the forward-alias
/// fixpoint resolves a same-name alias in each source independently (E1.5).
fn aliasResolvedInSource(self: *Lowering, src: []const u8, name: []const u8) bool {
if (self.program_index.type_aliases_by_source.get(src)) |inner| return inner.contains(name);
return false;
}
/// Try to convert an array literal's elements into a compile-time
/// ConstantValue.aggregate. `array_ty` is the array's resolved TypeId; its
/// element type drives type-aware serialization of struct-literal and