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,16 @@
// issue 0105 / F1 regression — a SELF-REFERENCE inside a same-name struct shadow.
// Two flat-imported modules each declare a top-level `Box`; module B's `Box` has
// a field `next: *Box` referencing its own name. The shadow must resolve that
// self-ref to ITS OWN nominal identity, not the first same-name author (A's
// `Box`), so `head.next.*.y` reads B's `y` (= 42). Proves the reserve-before-
// fields ordering: a shadow author's decl key is recorded before its fields are
// resolved, so a self / forward ref binds via `type_decl_tids`, never the global
// findByName first-author fallback.
#import "modules/std.sx";
#import "0757-modules-same-name-struct-self-ref/a.sx";
#import "0757-modules-same-name-struct-self-ref/b.sx";
main :: () -> s32 {
print("a={} b={}\n", a_box(), b_chain());
0
}

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;
}

View File

@@ -0,0 +1,15 @@
// issue 0105 / F1 regression — FORWARD + MUTUAL refs between same-name struct
// shadows. Two flat-imported modules each declare `Box` and `Node`; module B's
// `Box` forward-refs B's `Node` (declared later) and B's `Node` back-refs B's
// `Box`. Every cross-reference must bind to B's OWN nominal identities, proving
// the up-front genuine-shadow reservation: ALL of a genuine shadow's authors are
// reserved in `type_decl_tids` before any field resolves, so a forward / mutual
// ref never falls back to the global findByName first-author (A's `Node`).
#import "modules/std.sx";
#import "0758-modules-same-name-struct-mutual-ref/a.sx";
#import "0758-modules-same-name-struct-mutual-ref/b.sx";
main :: () -> s32 {
print("b={}\n", b_test());
0
}

View File

@@ -0,0 +1,3 @@
// Module A authors its OWN `Box` and `Node` (the FIRST same-name authors).
Box :: struct { x: s64; }
Node :: struct { n: s64; }

View File

@@ -0,0 +1,13 @@
// Module B authors same-name `Box` and `Node` shadows that reference EACH OTHER.
// B's `Box` has a FORWARD ref to B's `Node` (declared after it), and B's `Node`
// back-refs B's `Box`. Both forward and mutual refs must resolve to B's OWN
// nominal TypeIds, not the first same-name authors in A.
Box :: struct { y: s64; peer: *Node; }
Node :: struct { m: s64; owner: *Box; }
b_test :: () -> s64 {
nd := Node.{ m = 99, owner = null };
bx := Box.{ y = 7, peer = @nd };
// Reads B's Node.m (99); pre-fix the forward ref bound to A's Node (which has
// field `n`, not `m`) → "field 'm' not found on type 'Node'".
return bx.peer.*.m;
}

View File

@@ -0,0 +1 @@
0

View File

@@ -0,0 +1 @@
a=Box{x: 7} b=42

View File

@@ -0,0 +1 @@
0

View File

@@ -0,0 +1 @@
b=99