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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user