fix(stdlib/E4): bare generic head selects visible author; qualified missing-member diagnoses

E4 non-transitive type rule had two generic-head author-selection holes:

#1 A BARE generic struct head / alias with a single bare-VISIBLE author still
   instantiated a NON-visible 2-flat-hop same-name template, because the
   `.unregistered` gate arm fell through to the global last-wins
   `struct_template_map` winner. Add `bareVisibleStructTemplate`: after the
   visibility gate passes, select the source-keyed template authored by the
   single bare-visible author (own-wins, else the one 1-hop flat author) and
   instantiate THAT instead of the global map's last-wins entry. Null (→ the
   global map, byte-identical) when the visible author IS the canonical one
   (the common single-author case) or the picture isn't a clean single author.
   Applied at every bare generic-struct head/alias site (annotation `.call` /
   `.parameterized_type_expr`, alias-registration `.call` /
   `.parameterized_type_expr`, array-literal head).

#2 A QUALIFIED head `a.Box(..)` whose namespace `a` authors no member `Box`
   silently fell back to the bare global template, instantiating an unrelated
   module's `Box`. Add `qualifiedMemberMissing`: a qualified head whose known
   namespace lacks the member now emits "namespace 'a' has no member 'Box'" and
   poisons with `.unresolved`; a qualified head NEVER reaches the bare global map.

Regressions: 0774 (bare head + bare alias, 2-hop same-name → size=8 alias=8,
fail-before 16 16); 0775 (qualified missing member → diagnostic + exit 1,
fail-before size=16 exit 0).
This commit is contained in:
agra
2026-06-08 18:39:53 +03:00
parent 8c59acbd25
commit 246883073c
13 changed files with 238 additions and 15 deletions

View File

@@ -0,0 +1,28 @@
// A BARE generic struct head (`Box(s64)`) and a BARE generic alias
// (`ABox :: Box(s64)`) must instantiate the template authored by the single
// bare-VISIBLE author — this file's own author or a DIRECT (1-hop) flat import —
// NOT the global last-wins `struct_template_map`, which a NON-visible 2-flat-hop
// same-name template can win.
//
// `b.sx` declares a one-field `Box($T)` (size 8) and itself flat-imports `c.sx`,
// which declares a two-field `Box($T)` (size 16). This file flat-imports ONLY
// `b.sx`, so `b.Box` is one flat hop away (visible) and `c.Box` is two hops away
// (NOT bare-visible, mirrors 0764/0706). The bare head `Box(s64)` and the bare
// alias `ABox :: Box(s64)` must both select `b.Box` (size 8).
//
// Regression (Phase E4 finding #1): before the bare head/alias consulted the
// source-keyed visible author, both fell through the `.unregistered` gate arm to
// the global last-wins template and instantiated the non-visible `c.Box`
// (size 16). Fail-before printed `size=16 alias=16`.
#import "modules/std.sx";
#import "0774-modules-bare-generic-head-visible-author/b.sx";
ABox :: Box(s64);
main :: () -> s32 {
x : Box(s64) = .{ x = 1 };
a : ABox = .{ x = 2 };
print("size={} alias={} x={} a={}\n", size_of(Box(s64)), size_of(ABox), x.x, a.x);
0
}

View File

@@ -0,0 +1,11 @@
// The bare-VISIBLE author: a one-field generic `Box` (size 8). `b.sx` itself
// flat-imports `c.sx`, so `b_make`'s `Box(s64)` resolves here (the head is one
// flat hop away in this module) — but a file that imports b.sx reaches `c.Box`
// only at two hops, so it must NOT win the bare head in the importer.
Box :: struct($T: Type) { x: T; }
#import "c.sx";
b_make :: () -> Box(s64) {
.{ x = 99 }
}

View File

@@ -0,0 +1,5 @@
// The NON-visible 2-flat-hop author: a two-field generic `Box` (size 16). Same
// template NAME as b's, different layout. It wins the global last-wins
// `struct_template_map`, so the bare head in a file importing only b.sx must NOT
// pick it.
Box :: struct($T: Type) { x: T; y: T; }

View File

@@ -0,0 +1,23 @@
// A QUALIFIED generic head `a.Box(s64)` where namespace `a` exists but authors
// NO member named `Box` must DIAGNOSE the missing member — never silently fall
// back to the bare last-wins `struct_template_map` and instantiate an unrelated
// module's same-name `Box`.
//
// `a.sx` authors only `Other` (no `Box`); `b.sx` authors a generic `Box($T)`.
// The qualified reference `a.Box(s64)` must report that `a` has no member `Box`,
// NOT resolve to `b.Box`.
//
// Regression (Phase E4 finding #2): before the qualified head path diagnosed the
// missing member, `qualifiedStructTemplate` returned null and the code fell
// through to the bare global template, silently instantiating `b.Box`
// (`size=16 x=1 y=2`, exit 0).
#import "modules/std.sx";
a :: #import "0775-modules-qualified-generic-missing-member/a.sx";
b :: #import "0775-modules-qualified-generic-missing-member/b.sx";
main :: () -> s32 {
x : a.Box(s64) = .{ x = 1, y = 2 };
print("{}\n", x.x);
0
}

View File

@@ -0,0 +1,3 @@
// Namespace `a` authors ONLY `Other` — no `Box`. A qualified `a.Box(..)` head
// must diagnose the missing member, not resolve to another module's `Box`.
Other :: struct { z: s64; }

View File

@@ -0,0 +1,4 @@
// An unrelated module's generic `Box($T)` (two fields, size 16). The qualified
// head `a.Box(..)` must NOT silently resolve to this template via the bare
// global last-wins map.
Box :: struct($T: Type) { x: T; y: T; }

View File

@@ -0,0 +1 @@
size=8 alias=8 x=1 a=2

View File

@@ -0,0 +1,5 @@
error: namespace 'a' has no member 'Box'
--> examples/0775-modules-qualified-generic-missing-member.sx:20:9
|
20 | x : a.Box(s64) = .{ x = 1, y = 2 };
| ^^^^^^^^^^