fix(lower): reserve genuine same-name struct shadows before fields — close F1 [stdlib E2 attempt-2]

A self / forward / mutual reference inside a same-name struct shadow bound to
the FIRST same-name author (another module's struct) instead of its own nominal
TypeId: registerStructDecl resolved a shadow's field types BEFORE registering its
decl key in type_decl_tids, so namedRefTid fell through to the name-only
findByName first-author fallback (F1).

Fix: a genuine same-name struct shadow (≥2 DISTINCT struct decls author the name
in the scanned decl set) reserves ALL its authors' distinct nominal slots up-front
in scanDecls — the first at id 0, the rest at fresh nonzero ids — BEFORE any field
resolves. Every self / forward / mutual ref to the shadow name then resolves via
type_decl_tids to its OWN nominal TypeId.

Gating on the scanned decls, not nameHasMultipleTypeAuthors (the raw import facts
over-count a single file reached via two un-normalized import spellings, e.g.
math/matrix44), keeps single-real-decl names on the legacy id-0 post-field path —
byte-identical (494 prior markers unchanged, single-author old==new).

internNamedTypeDecl now takes the precomputed nominal_id; no-drift + single
graph-walk invariants untouched; generics / enum / union / error-set stay legacy.

Regressions: 0757 (self-ref *Box → reads B's own field), 0758 (forward + mutual
*Node/*Box between two shadows). Fail-before on d98ad5c
("field 'y'/'m' not found"), pass-after.
This commit is contained in:
agra
2026-06-07 23:51:46 +03:00
parent d98ad5c14f
commit 66d10c00bb
13 changed files with 175 additions and 22 deletions

View File

@@ -863,6 +863,42 @@ pub const Lowering = struct {
else => {},
}
}
// Pass 0b: reserve every GENUINE same-name STRUCT shadow's DISTINCT nominal
// slot BEFORE the registration loop resolves any fields (E2/F1). A field
// type referencing a shadow name — self (`next: *Box`), or a forward /
// mutual ref to a shadow declared LATER in the same module (`peer: *Node`)
// — then binds to its OWN nominal TypeId via `type_decl_tids`, never the
// global findByName first-author fallback (issue 0105).
//
// "Genuine" = ≥2 DISTINCT struct decls in THIS scan author the name (so it
// needs ≥2 distinct nominal TypeIds). Gating on the scanned decls — NOT
// `nameHasMultipleTypeAuthors` (the raw import facts, which over-count one
// file reached via two un-normalized import spellings, e.g. `math/matrix44`
// pulled in twice) — keeps a single-real-decl name on the legacy id-0 path,
// byte-identical. ALL authors of a genuine shadow reserve, in declaration
// order: the FIRST at id 0, the rest at fresh nonzero ids, matching the
// per-decl registration order so the first-author-keeps-0 assignment holds.
var shadow_first = std.AutoHashMap(types.StringId, *const anyopaque).init(self.alloc);
defer shadow_first.deinit();
var genuine_shadows = std.AutoHashMap(types.StringId, void).init(self.alloc);
defer genuine_shadows.deinit();
for (decls) |decl| {
const sd = topLevelStructDecl(decl) orelse continue;
if (sd.type_params.len > 0) continue;
const nm = self.module.types.internString(sd.name);
const key: *const anyopaque = @ptrCast(sd);
const gop = shadow_first.getOrPut(nm) catch continue;
if (gop.found_existing) {
if (gop.value_ptr.* != key) genuine_shadows.put(nm, {}) catch {};
} else gop.value_ptr.* = key;
}
for (decls) |decl| {
const sd = topLevelStructDecl(decl) orelse continue;
const nm = self.module.types.internString(sd.name);
if (!genuine_shadows.contains(nm)) continue;
self.setCurrentSourceFile(decl.source_file);
self.reserveShadowStructSlot(sd);
}
for (decls) |decl| {
self.setCurrentSourceFile(decl.source_file);
const is_imported = if (self.main_file) |mf|
@@ -14122,31 +14158,66 @@ pub const Lowering = struct {
return out;
}
/// The top-level STRUCT decl a top-level node authors (a bare `struct_decl`, or
/// a `Name :: struct {...}` const wrapper), or null. Used by the genuine-shadow
/// scan in `scanDecls` to enumerate same-name struct authors uniformly.
fn topLevelStructDecl(decl: *const Node) ?*const ast.StructDecl {
return switch (decl.data) {
.struct_decl => &decl.data.struct_decl,
.const_decl => |cd| if (cd.value.data == .struct_decl) &cd.value.data.struct_decl else null,
else => null,
};
}
/// Reserve a GENUINE same-name STRUCT shadow author's DISTINCT nominal slot
/// BEFORE any field resolves, so a self / forward / mutual reference to a shadow
/// name (`next: *Box`; `peer: *Node` where Node is a shadow declared later)
/// binds to ITS nominal TypeId via `type_decl_tids` instead of the global
/// findByName first-author fallback (issue 0105 / F1). Called only from the
/// `scanDecls` genuine-shadow pass, which has already established that ≥2
/// distinct struct decls author this name; ALL of them reserve — the FIRST at
/// id 0, the rest at fresh nonzero ids — so none falls through to the name-only
/// `findByName` (which, once a shadow is interned, no longer uniquely identifies
/// the first author). Idempotent per decl key: an already-reserved decl returns
/// before re-invoking `shadowNominalId`, so the shadow id is computed once.
/// Generic templates resolve lazily on instantiation and are skipped.
fn reserveShadowStructSlot(self: *Lowering, sd: *const ast.StructDecl) void {
if (sd.type_params.len > 0) return;
const table = &self.module.types;
const decl_key: *const anyopaque = @ptrCast(sd);
if (table.type_decl_tids.contains(decl_key)) return;
const name_id = table.internString(sd.name);
const nominal_id = self.shadowNominalId(name_id); // 0 for the first author, nonzero for the rest
const reserved = table.internNominal(.{ .@"struct" = .{ .name = name_id, .fields = &.{} } }, nominal_id);
table.type_decl_tids.put(decl_key, reserved) catch {};
}
/// Register (or re-register) a top-level NAMED type decl under a per-source
/// nominal identity (E2), returning its TypeId. `decl_key` is the decl's
/// stable pointer (the import raw-facts identity); `info` carries the full
/// body with `nominal_id` left 0 — this stamps the right id and records the
/// `decl_key → TypeId` map (`type_decl_tids`, the `fn_decl_fids` analogue).
/// body; `nominal_id` is the slot's identity (0 for a single / first author,
/// nonzero for a later same-name shadow) — computed once by the caller
/// (`registerStructDecl`), which reuses the id reserved up-front in `scanDecls`
/// for a genuine shadow (so its fields' self / forward / mutual refs already
/// resolved against it). This stamps the id and records the `decl_key → TypeId`
/// map (`type_decl_tids`, the `fn_decl_fids` analogue).
///
/// Shadow detection is gated on the IMPORT FACTS, not registration-time
/// source: only a name authored AS A NAMED TYPE by ≥2 distinct modules
/// (`nameHasMultipleTypeAuthors`) can produce a same-name shadow. A
/// single-author name keeps `nominal_id = 0` and adopts any forward-reference
/// stub (`findByName` orelse intern) — BYTE-IDENTICAL to pre-E2 registration,
/// and immune to the compiler re-registering one logical type from several
/// contexts (default-context emission, comptime eval) under a shifting
/// `current_source_file`. For a genuinely multi-authored name, the FIRST
/// source keeps id 0 and later sources get fresh ids → DISTINCT TypeIds, so
/// the authors no longer collapse last-wins (issue 0105). Idempotent per
/// `decl_key`: a re-registration reuses the recorded slot, refreshing its body.
fn internNamedTypeDecl(self: *Lowering, decl_key: *const anyopaque, name_id: types.StringId, info: types.TypeInfo) TypeId {
/// A `nominal_id == 0` author adopts any forward-reference stub (`findByName`
/// orelse intern) — BYTE-IDENTICAL to pre-E2 registration. For a genuinely
/// multi-authored name, the FIRST source keeps id 0 and later sources get
/// fresh ids → DISTINCT TypeIds, so the authors no longer collapse last-wins
/// (issue 0105). Idempotent per `decl_key`: a re-registration — OR an up-front
/// shadow reservation — reuses the recorded slot, refreshing its body via
/// `updatePreservingKey` (key-stable because a struct's intern key is its
/// name + nominal id, not its fields).
fn internNamedTypeDecl(self: *Lowering, decl_key: *const anyopaque, name_id: types.StringId, info: types.TypeInfo, nominal_id: u32) TypeId {
const table = &self.module.types;
// Same decl seen again → reuse its slot + nominal id, refresh the body.
// Slot already recorded (re-registration, or a reserve-before-fields shadow
// reservation) → reuse its slot + nominal id, refresh the body.
if (table.type_decl_tids.get(decl_key)) |existing_id| {
table.updatePreservingKey(existing_id, stampNominalId(info, nominalIdOf(table.get(existing_id))));
return existing_id;
}
const nominal_id: u32 = self.shadowNominalId(name_id);
const id = if (nominal_id == 0)
(table.findByName(name_id) orelse table.internNominal(info, 0))
else
@@ -14282,6 +14353,20 @@ pub const Lowering = struct {
return;
}
// Per-decl nominal identity (E2). EACH author of a GENUINE same-name STRUCT
// shadow already reserved its distinct slot up-front in `scanDecls` (the
// first at id 0, the rest at nonzero ids), so a self / forward / mutual
// reference to the shadow name bound to ITS nominal TypeId via
// `type_decl_tids`, not the global findByName first-author fallback (issue
// 0105 / F1): reuse that reserved id. A single-author name (or a phantom
// over-counted by the raw import facts) was NOT reserved — it keeps id 0 and
// the legacy post-field registration, byte-identical to pre-F1.
// `shadowNominalId` here only fires for the non-scanDecls registration paths
// (comptime `lowerDecls`, block-local), where module facts are unwired so it
// returns 0.
const decl_key: *const anyopaque = @ptrCast(sd);
const nominal_id: u32 = if (table.type_decl_tids.get(decl_key)) |id| nominalIdOf(table.get(id)) else self.shadowNominalId(name_id);
// Build field list, expanding #using entries
var fields = std.ArrayList(types.TypeInfo.StructInfo.Field).empty;
var field_idx: usize = 0;
@@ -14336,13 +14421,13 @@ pub const Lowering = struct {
}
}
// Register under a per-decl nominal identity (E2). A forward-reference
// placeholder (empty-field stub) is adopted in place; a same-name struct
// authored in a DIFFERENT source gets its own distinct TypeId instead of
// last-wins clobbering the first (issue 0105). `&decl.data.struct_decl`
// (the stable import-raw-facts pointer) is the identity key.
// Register under the per-decl nominal identity computed above. A non-first
// shadow author's slot was already reserved before fields resolved, so this
// fills it (key-stable updatePreservingKey); a first / single author adopts
// any forward-reference stub. Same-name structs in DIFFERENT sources get
// distinct TypeIds instead of last-wins clobbering the first (issue 0105).
const info: types.TypeInfo = .{ .@"struct" = .{ .name = name_id, .fields = fields.items } };
_ = self.internNamedTypeDecl(@ptrCast(sd), name_id, info);
_ = self.internNamedTypeDecl(decl_key, name_id, info, nominal_id);
// Store field defaults for struct literal lowering
if (sd.field_defaults.len > 0) {