From 246883073ca712219f80fc07526d6e85e075c50f Mon Sep 17 00:00:00 2001 From: agra Date: Mon, 8 Jun 2026 18:39:53 +0300 Subject: [PATCH] fix(stdlib/E4): bare generic head selects visible author; qualified missing-member diagnoses MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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). --- ...odules-bare-generic-head-visible-author.sx | 28 +++ .../b.sx | 11 ++ .../c.sx | 5 + ...odules-qualified-generic-missing-member.sx | 23 +++ .../a.sx | 3 + .../b.sx | 4 + ...ules-bare-generic-head-visible-author.exit | 1 + ...es-bare-generic-head-visible-author.stderr | 0 ...es-bare-generic-head-visible-author.stdout | 1 + ...ules-qualified-generic-missing-member.exit | 1 + ...es-qualified-generic-missing-member.stderr | 5 + ...es-qualified-generic-missing-member.stdout | 0 src/ir/lower.zig | 171 ++++++++++++++++-- 13 files changed, 238 insertions(+), 15 deletions(-) create mode 100644 examples/0774-modules-bare-generic-head-visible-author.sx create mode 100644 examples/0774-modules-bare-generic-head-visible-author/b.sx create mode 100644 examples/0774-modules-bare-generic-head-visible-author/c.sx create mode 100644 examples/0775-modules-qualified-generic-missing-member.sx create mode 100644 examples/0775-modules-qualified-generic-missing-member/a.sx create mode 100644 examples/0775-modules-qualified-generic-missing-member/b.sx create mode 100644 examples/expected/0774-modules-bare-generic-head-visible-author.exit create mode 100644 examples/expected/0774-modules-bare-generic-head-visible-author.stderr create mode 100644 examples/expected/0774-modules-bare-generic-head-visible-author.stdout create mode 100644 examples/expected/0775-modules-qualified-generic-missing-member.exit create mode 100644 examples/expected/0775-modules-qualified-generic-missing-member.stderr create mode 100644 examples/expected/0775-modules-qualified-generic-missing-member.stdout diff --git a/examples/0774-modules-bare-generic-head-visible-author.sx b/examples/0774-modules-bare-generic-head-visible-author.sx new file mode 100644 index 0000000..91a789e --- /dev/null +++ b/examples/0774-modules-bare-generic-head-visible-author.sx @@ -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 +} diff --git a/examples/0774-modules-bare-generic-head-visible-author/b.sx b/examples/0774-modules-bare-generic-head-visible-author/b.sx new file mode 100644 index 0000000..4f460a1 --- /dev/null +++ b/examples/0774-modules-bare-generic-head-visible-author/b.sx @@ -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 } +} diff --git a/examples/0774-modules-bare-generic-head-visible-author/c.sx b/examples/0774-modules-bare-generic-head-visible-author/c.sx new file mode 100644 index 0000000..cf7b1fe --- /dev/null +++ b/examples/0774-modules-bare-generic-head-visible-author/c.sx @@ -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; } diff --git a/examples/0775-modules-qualified-generic-missing-member.sx b/examples/0775-modules-qualified-generic-missing-member.sx new file mode 100644 index 0000000..1cee944 --- /dev/null +++ b/examples/0775-modules-qualified-generic-missing-member.sx @@ -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 +} diff --git a/examples/0775-modules-qualified-generic-missing-member/a.sx b/examples/0775-modules-qualified-generic-missing-member/a.sx new file mode 100644 index 0000000..29be4b5 --- /dev/null +++ b/examples/0775-modules-qualified-generic-missing-member/a.sx @@ -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; } diff --git a/examples/0775-modules-qualified-generic-missing-member/b.sx b/examples/0775-modules-qualified-generic-missing-member/b.sx new file mode 100644 index 0000000..9e8575d --- /dev/null +++ b/examples/0775-modules-qualified-generic-missing-member/b.sx @@ -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; } diff --git a/examples/expected/0774-modules-bare-generic-head-visible-author.exit b/examples/expected/0774-modules-bare-generic-head-visible-author.exit new file mode 100644 index 0000000..573541a --- /dev/null +++ b/examples/expected/0774-modules-bare-generic-head-visible-author.exit @@ -0,0 +1 @@ +0 diff --git a/examples/expected/0774-modules-bare-generic-head-visible-author.stderr b/examples/expected/0774-modules-bare-generic-head-visible-author.stderr new file mode 100644 index 0000000..e69de29 diff --git a/examples/expected/0774-modules-bare-generic-head-visible-author.stdout b/examples/expected/0774-modules-bare-generic-head-visible-author.stdout new file mode 100644 index 0000000..e929fdd --- /dev/null +++ b/examples/expected/0774-modules-bare-generic-head-visible-author.stdout @@ -0,0 +1 @@ +size=8 alias=8 x=1 a=2 diff --git a/examples/expected/0775-modules-qualified-generic-missing-member.exit b/examples/expected/0775-modules-qualified-generic-missing-member.exit new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/examples/expected/0775-modules-qualified-generic-missing-member.exit @@ -0,0 +1 @@ +1 diff --git a/examples/expected/0775-modules-qualified-generic-missing-member.stderr b/examples/expected/0775-modules-qualified-generic-missing-member.stderr new file mode 100644 index 0000000..5789cc4 --- /dev/null +++ b/examples/expected/0775-modules-qualified-generic-missing-member.stderr @@ -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 }; + | ^^^^^^^^^^ diff --git a/examples/expected/0775-modules-qualified-generic-missing-member.stdout b/examples/expected/0775-modules-qualified-generic-missing-member.stdout new file mode 100644 index 0000000..e69de29 diff --git a/src/ir/lower.zig b/src/ir/lower.zig index ec751a8..418263d 100644 --- a/src/ir/lower.zig +++ b/src/ir/lower.zig @@ -1026,13 +1026,20 @@ pub const Lowering = struct { // template via the namespace edge (mirrors the annotation // head site `resolveTypeCallWithBindings`), not the bare // last-wins `struct_template_map`. - const qual_call_tmpl: ?StructTemplate = if (head_qualified and call_data.callee.data.field_access.object.data == .identifier) - self.qualifiedStructTemplate(call_data.callee.data.field_access.object.data.identifier.name, callee_name) + const qual_alias: ?[]const u8 = if (head_qualified and call_data.callee.data.field_access.object.data == .identifier) + call_data.callee.data.field_access.object.data.identifier.name else null; + const qual_call_tmpl: ?StructTemplate = if (qual_alias) |a| self.qualifiedStructTemplate(a, callee_name) else null; if (callee_name.len > 0) { if (qual_call_tmpl) |qt| { self.registerGenericStructAlias(cd.name, &qt, call_data.args); + } else if (qual_alias != null and self.qualifiedMemberMissing(qual_alias.?, callee_name)) { + // Qualified alias head whose namespace lacks the member: + // diagnose, never fall to the bare global template (E4 #2). + if (self.diagnostics) |d| + d.addFmt(.err, call_data.callee.span, "namespace '{s}' has no member '{s}'", .{ qual_alias.?, callee_name }); + self.putTypeAlias(self.current_source_file, cd.name, .unresolved); } else if (self.program_index.struct_template_map.getPtr(callee_name)) |tmpl| reg: { // 2-hop generic-struct head leak: poison the alias with // `.unresolved` (suppressed downstream) so the use site @@ -1042,6 +1049,14 @@ pub const Lowering = struct { self.putTypeAlias(self.current_source_file, cd.name, .unresolved); break :reg; } + // A bare alias head selects the single bare-VISIBLE + // author's template, not the global last-wins map (E4 #1). + if (!head_qualified) { + if (self.bareVisibleStructTemplate(callee_name)) |vt| { + self.registerGenericStructAlias(cd.name, &vt, call_data.args); + break :reg; + } + } self.registerGenericStructAlias(cd.name, tmpl, call_data.args); } else if (std.mem.eql(u8, callee_name, "Vector")) { // Builtin type constructor — checked BEFORE @@ -1075,17 +1090,29 @@ pub const Lowering = struct { // template via the namespace edge (mirrors the annotation // head site `resolveParameterizedWithBindings`), not the // bare last-wins `struct_template_map`. - const qual_pt_tmpl: ?StructTemplate = if (pt_qualified) blk: { - const dot = std.mem.indexOfScalar(u8, pt.name, '.').?; - break :blk self.qualifiedStructTemplate(pt.name[0..dot], base_name); - } else null; + const pt_alias: ?[]const u8 = if (pt_qualified) pt.name[0 .. std.mem.indexOfScalar(u8, pt.name, '.').?] else null; + const qual_pt_tmpl: ?StructTemplate = if (pt_alias) |a| self.qualifiedStructTemplate(a, base_name) else null; if (qual_pt_tmpl) |qt| { self.registerGenericStructAlias(cd.name, &qt, pt.args); + } else if (pt_alias != null and self.qualifiedMemberMissing(pt_alias.?, base_name)) { + // Qualified alias base whose namespace lacks the member: + // diagnose, never fall to the bare global template (E4 #2). + if (self.diagnostics) |d| + d.addFmt(.err, cd.value.span, "namespace '{s}' has no member '{s}'", .{ pt_alias.?, base_name }); + self.putTypeAlias(self.current_source_file, cd.name, .unresolved); } else if (self.program_index.struct_template_map.getPtr(base_name)) |tmpl| reg: { if (!pt_qualified and self.headTypeLeak(base_name, cd.value.span)) { self.putTypeAlias(self.current_source_file, cd.name, .unresolved); break :reg; } + // A bare alias base selects the single bare-VISIBLE author's + // template, not the global last-wins map (E4 #1). + if (!pt_qualified) { + if (self.bareVisibleStructTemplate(base_name)) |vt| { + self.registerGenericStructAlias(cd.name, &vt, pt.args); + break :reg; + } + } self.registerGenericStructAlias(cd.name, tmpl, pt.args); } else { // Builtin parameterised type (Vector(N, T) etc) — @@ -2317,6 +2344,18 @@ pub const Lowering = struct { }; } + /// The `*StructDecl` a raw author wraps, or null when the author is not a + /// struct — a top-level `Box :: struct(...)` is recorded either as a bare + /// `struct_decl` RawDeclRef or a `const_decl` whose value is one, so both + /// unwrap to the same decl (mirrors `qualifiedStructTemplate`'s own-decl walk). + fn structDeclOfRaw(ref: resolver_mod.RawDeclRef) ?*const ast.StructDecl { + return switch (ref) { + .struct_decl => |sd| sd, + .const_decl => |cd| if (cd.value.data == .struct_decl) &cd.value.data.struct_decl else null, + else => null, + }; + } + /// TRUE iff `ref` is a TYPE-FUNCTION head author — a `fn_decl` (or const- /// wrapped fn) declaring at least one `$`-parameter, i.e. instantiable as a /// bare type head (`Make(s64)` where `Make :: ($T) -> Type`). Mirrors the @@ -6909,7 +6948,14 @@ pub const Lowering = struct { } // Try as generic struct if (self.program_index.struct_template_map.getPtr(callee_name)) |tmpl| { - if (cl.callee.data != .field_access and self.headTypeLeak(callee_name, cl.callee.span)) return .unresolved; + const bare = cl.callee.data != .field_access; + if (bare and self.headTypeLeak(callee_name, cl.callee.span)) return .unresolved; + // A bare head selects the single bare-VISIBLE author, not the + // global last-wins map (E4 #1). + if (bare) { + if (self.bareVisibleStructTemplate(callee_name)) |vt| + return self.instantiateGenericStruct(&vt, cl.args); + } return self.instantiateGenericStruct(tmpl, cl.args); } return .unresolved; @@ -14144,11 +14190,26 @@ pub const Lowering = struct { if (self.qualifiedStructTemplate(alias, callee_name)) |tmpl| { return self.instantiateGenericStruct(&tmpl, cl.args); } + // The namespace exists but authors no member `callee_name` — diagnose + // the missing member; never fall back to the bare global template (E4 #2). + if (self.qualifiedMemberMissing(alias, callee_name)) { + if (self.diagnostics) |d| + d.addFmt(.err, cl.callee.span, "namespace '{s}' has no member '{s}'", .{ alias, callee_name }); + return .unresolved; + } } - // User-defined generic struct - if (self.program_index.struct_template_map.getPtr(callee_name)) |tmpl| { - if (!is_qualified and self.headTypeLeak(callee_name, cl.callee.span)) return .unresolved; - return self.instantiateGenericStruct(tmpl, cl.args); + // User-defined generic struct. A BARE head selects the single bare-VISIBLE + // author's template (own or 1-hop flat), source-keyed — NOT the global + // last-wins map, which a non-visible 2-flat-hop same-name template can win + // (E4 #1). A qualified head NEVER reaches the bare map (it resolved or + // diagnosed above). + if (!is_qualified) { + if (self.program_index.struct_template_map.getPtr(callee_name)) |tmpl| { + if (self.headTypeLeak(callee_name, cl.callee.span)) return .unresolved; + if (self.bareVisibleStructTemplate(callee_name)) |vt| + return self.instantiateGenericStruct(&vt, cl.args); + return self.instantiateGenericStruct(tmpl, cl.args); + } } // User-defined type-returning function: Complex(u32), Sx(f32) // Also resolve via scope fn_names (local functions get mangled names) @@ -14197,13 +14258,26 @@ pub const Lowering = struct { if (self.qualifiedStructTemplate(alias, base_name)) |tmpl| { return self.instantiateGenericStruct(&tmpl, pt.args); } + // Namespace exists but authors no member `base_name` — diagnose the + // missing member; never fall back to the bare global template (E4 #2). + if (self.qualifiedMemberMissing(alias, base_name)) { + if (self.diagnostics) |d| + d.addFmt(.err, span, "namespace '{s}' has no member '{s}'", .{ alias, base_name }); + return .unresolved; + } } } - // User-defined generic struct: look up template and instantiate - if (self.program_index.struct_template_map.getPtr(base_name)) |tmpl| { - if (!is_qualified and self.headTypeLeak(base_name, span)) return .unresolved; - return self.instantiateGenericStruct(tmpl, pt.args); + // User-defined generic struct: a BARE head selects the single bare-VISIBLE + // author's template (own or 1-hop flat), source-keyed — NOT the global + // last-wins map (E4 #1). A qualified head NEVER reaches the bare map. + if (!is_qualified) { + if (self.program_index.struct_template_map.getPtr(base_name)) |tmpl| { + if (self.headTypeLeak(base_name, span)) return .unresolved; + if (self.bareVisibleStructTemplate(base_name)) |vt| + return self.instantiateGenericStruct(&vt, pt.args); + return self.instantiateGenericStruct(tmpl, pt.args); + } } // Parameterized protocol used as a value type (`VL(s64)`): materialize a @@ -14886,6 +14960,73 @@ pub const Lowering = struct { return null; } + /// TRUE iff `alias` is a KNOWN namespace in the current source but its target + /// module authors NO member named `member` at all. A qualified generic head + /// `a.Box(..)` whose namespace lacks `Box` must diagnose the missing member — + /// never silently fall back to the bare last-wins `struct_template_map` (which + /// would instantiate an unrelated module's same-name `Box`, E4 finding #2). + /// FALSE when `alias` is not a namespace at all (leave the caller's existing + /// non-namespace handling), or when the namespace DOES author `member` (a + /// generic struct → `qualifiedStructTemplate` already selected it; any other + /// kind → the type-fn / named-type arms handle it). + fn qualifiedMemberMissing(self: *Lowering, alias: []const u8, member: []const u8) bool { + const edges = self.program_index.namespace_edges orelse return false; + const from = self.current_source_file orelse return false; + const alias_map = edges.getPtr(from) orelse return false; + const target = alias_map.get(alias) orelse return false; + for (target.own_decls) |decl| { + const dn = decl.data.declName() orelse continue; + if (std.mem.eql(u8, dn, member)) return false; + } + return true; + } + + /// The generic struct template authored by the single bare-VISIBLE author of + /// `name` when that author is NOT the one the global last-wins + /// `struct_template_map` already holds — the E4 non-transitive fix for a bare + /// generic head / alias whose visible author (own or a single 1-hop flat + /// import) is shadowed in the global map by a NON-visible (≥2-flat-hop) + /// same-name template (finding #1). Returns the rebuilt, source-pinned template + /// to instantiate INSTEAD of the global one. Null — caller uses the global map + /// unchanged (byte-identical) — when: no source context; the single visible + /// author IS the canonical map author (the common single-author case, matched + /// by source file); or the visible picture is not a clean single generic-struct + /// author (own non-generic shadow, or ≥2 flat authors whose ambiguity + /// `headTypeLeak` has already diagnosed + poisoned before this is consulted). + fn bareVisibleStructTemplate(self: *Lowering, name: []const u8) ?StructTemplate { + if (self.emitting_default_context) return null; + const from = self.current_source_file orelse return null; + const canon = self.program_index.struct_template_map.get(name) orelse return null; + const canon_src = canon.source_file orelse ""; + // own-wins: the querying source's own generic-struct author shadows imports. + if (self.moduleTypeAuthor(from, name)) |author| { + const sd = switch (author) { + .named => |ref| structDeclOfRaw(ref) orelse return null, + .alias => return null, + }; + if (sd.type_params.len == 0) return null; + if (std.mem.eql(u8, from, canon_src)) return null; + return self.buildGenericStructTemplate(sd, from); + } + // Otherwise: the single 1-hop flat-import generic-struct author. + var res = self.resolver(); + const set = res.collectVisibleAuthors(name, from, .user_bare_flat); + defer if (set.flat.len > 0) self.alloc.free(set.flat); + if (set.own != null) return null; // own non-type shadow → leave to existing paths + var picked: ?*const ast.StructDecl = null; + var picked_src: []const u8 = ""; + for (set.flat) |fa| { + const sd = structDeclOfRaw(fa.raw) orelse continue; + if (sd.type_params.len == 0) continue; + if (picked != null) return null; // ≥2 visible authors → gate diagnoses ambiguity + picked = sd; + picked_src = fa.source; + } + const sd = picked orelse return null; + if (std.mem.eql(u8, picked_src, canon_src)) return null; + return self.buildGenericStructTemplate(sd, picked_src); + } + /// Instantiate a generic struct template and register the result under an /// alias name (`Vec3 :: Vec(3, f32)` / `ABox :: a.Box(s64)`). Shared by the /// `.call` and `.parameterized_type_expr` const-decl alias branches and the