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

@@ -0,0 +1,3 @@
// Module A authors its OWN `Box` (one field `x`) — the FIRST same-name author.
Box :: struct { x: s64; }
a_box :: () -> Box { return Box.{ x = 7 }; }

View File

@@ -0,0 +1,12 @@
// Module B authors a same-name `Box` shadow whose field SELF-REFERENCES its own
// name (`next: *Box`). Pre-fix the self-ref resolved to A's `Box` (registered
// first under the bare name), so `next.*.y` failed with "field 'y' not found on
// type 'Box'". The shadow's slot is now reserved BEFORE its fields resolve, so
// `*Box` binds to B's OWN nominal TypeId and the deref sees B's `y`.
Box :: struct { y: s64; next: *Box; }
b_chain :: () -> s64 {
tail := Box.{ y = 42, next = null };
head := Box.{ y = 1, next = @tail };
// Walk the self-referential link; reads B's own `y`, not A's `x`.
return head.next.*.y;
}