fix(stdlib/E4): bare generic static-method head selects the visible author (type + method)
The static-method-call head `Box(s64).make(7)` was the last uncovered bare- generic-head instantiation site: it gated visibility with `headTypeLeak` but then instantiated the global last-wins `struct_template_map` entry and ran the name-keyed `Box.make` from `fn_ast_map`, so a NON-visible 2-flat-hop same-name template (and its method) won. `size_of(Box(s64))` picked the visible `b.Box` (8) while `Box(s64).make(7)` returned a `c.Box`-shaped (16) value. Route the static-method head through the single bare-VISIBLE author for BOTH the instantiated type layout AND the method body: split the existing visible- author selection into `bareVisibleStructDecl` (returns the StructDecl + source; single selection point, `bareVisibleStructTemplate` now delegates to it — no drift) and source-pin the method body via the author's own `sd.methods` (`structMethodFn`) instead of the last-wins `fn_ast_map`. Ambiguity (>1 visible author) is already diagnosed by the pre-existing `headTypeLeak` gate. Exhaustive bare-head instantiation-site audit (all callers reaching `instantiateGenericStruct` / `struct_template_map` for a bare head): .call alias, .parameterized_type_expr alias, resolveType .call, resolveTypeCallWithBindings, resolveParameterizedWithBindings — all already route through the visible-author selection; the static-method head was the only remaining one and is now covered. Regression 0776: bare generic static-method head with a 2-hop same-name template asserts the visible author's layout (xtype=8, x reachable); fail-before xtype=16.
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
// A BARE generic struct head used as a STATIC-METHOD-CALL target
|
||||
// (`Box(s64).make(7)`) must instantiate — and call the method of — the template
|
||||
// authored by the single bare-VISIBLE author, NOT the global last-wins
|
||||
// `struct_template_map` (and its name-keyed `Box.make`), which a NON-visible
|
||||
// 2-flat-hop same-name template can win.
|
||||
//
|
||||
// `b.sx` declares a one-field `Box($T)` (size 8) with its own `make`, and itself
|
||||
// flat-imports `c.sx`, which declares a two-field `Box($T)` (size 16) with its
|
||||
// own `make`. 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/0774/0706). The static-method head `Box(s64).make(7)` must select `b.Box`
|
||||
// (size 8) for BOTH the instantiated type layout and the method body.
|
||||
//
|
||||
// Regression (Phase E4 finding #1, static-method site): before the static-method
|
||||
// head consulted the source-keyed visible author, `size_of(Box(s64))` correctly
|
||||
// picked the visible `b.Box` (8) but `Box(s64).make(7)` instantiated the global
|
||||
// last-wins `c.Box` and ran `c.Box.make`, returning a 16-byte value. Fail-before
|
||||
// printed `size=8 xtype=16 x=7`.
|
||||
|
||||
#import "modules/std.sx";
|
||||
#import "0776-modules-bare-generic-static-method-visible-author/b.sx";
|
||||
|
||||
main :: () -> s32 {
|
||||
x := Box(s64).make(7);
|
||||
print("size={} xtype={} x={}\n", size_of(Box(s64)), size_of(type_of(x)), x.x);
|
||||
0
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// The bare-VISIBLE author: a one-field generic `Box` (size 8) with its own
|
||||
// `make`. `b.sx` itself flat-imports `c.sx`, so a file that imports only b.sx
|
||||
// reaches `c.Box` at two hops and must NOT pick it (nor `c.Box.make`).
|
||||
Box :: struct($T: Type) {
|
||||
x: T;
|
||||
|
||||
make :: (value: T) -> Box(T) {
|
||||
.{ x = value }
|
||||
}
|
||||
}
|
||||
|
||||
#import "c.sx";
|
||||
@@ -0,0 +1,13 @@
|
||||
// The NON-visible 2-flat-hop author: a two-field generic `Box` (size 16) with its
|
||||
// own `make` (sets a second field). Same template NAME as b's, different layout.
|
||||
// It wins the global last-wins `struct_template_map` (and the name-keyed
|
||||
// `Box.make`), so the static-method head in a file importing only b.sx must NOT
|
||||
// pick it.
|
||||
Box :: struct($T: Type) {
|
||||
x: T;
|
||||
y: T;
|
||||
|
||||
make :: (value: T) -> Box(T) {
|
||||
.{ x = value, y = value + 100 }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
0
|
||||
@@ -0,0 +1 @@
|
||||
size=8 xtype=8 x=7
|
||||
@@ -2356,6 +2356,26 @@ pub const Lowering = struct {
|
||||
};
|
||||
}
|
||||
|
||||
/// The single bare-VISIBLE generic-struct author selected by
|
||||
/// `bareVisibleStructDecl`: the `StructDecl` plus the source that DECLARED it.
|
||||
const VisibleStructAuthor = struct {
|
||||
sd: *const ast.StructDecl,
|
||||
source: []const u8,
|
||||
};
|
||||
|
||||
/// The `fn_decl` of struct `sd`'s method named `method`, or null when `sd`
|
||||
/// declares no such method. Used to source-pin a static-method head's body to
|
||||
/// the bare-visible author's own method (`b.Box.make`), bypassing the name-keyed
|
||||
/// last-wins `fn_ast_map` ("Box.make") that a 2-flat-hop same-name template's
|
||||
/// method would otherwise win (E4 #1, static-method site).
|
||||
fn structMethodFn(sd: *const ast.StructDecl, method: []const u8) ?*const ast.FnDecl {
|
||||
for (sd.methods) |mn| {
|
||||
if (mn.data == .fn_decl and std.mem.eql(u8, mn.data.fn_decl.name, method))
|
||||
return &mn.data.fn_decl;
|
||||
}
|
||||
return 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
|
||||
@@ -8713,12 +8733,29 @@ pub const Lowering = struct {
|
||||
// the head is unqualified and subject to the bare-head gate (E4).
|
||||
if (self.program_index.struct_template_map.getPtr(resolved)) |tmpl| {
|
||||
if (self.headTypeLeak(inner_name, inner_call.callee.span)) return Ref.none;
|
||||
const inst_ty = self.instantiateGenericStruct(tmpl, inner_call.args);
|
||||
// A bare generic static-method head (`Box(s64).make(..)`)
|
||||
// selects the single bare-VISIBLE author for BOTH the
|
||||
// instantiated type layout AND the method body — never the
|
||||
// global last-wins map, which a non-visible 2-flat-hop
|
||||
// same-name template (and its name-keyed `Box.make`) can win
|
||||
// (E4 #1, static-method site).
|
||||
const vis = self.bareVisibleStructDecl(inner_name);
|
||||
var vt: StructTemplate = undefined;
|
||||
const use_tmpl: *const StructTemplate = if (vis) |v| blk: {
|
||||
vt = self.buildGenericStructTemplate(v.sd, v.source) orelse break :blk tmpl;
|
||||
break :blk &vt;
|
||||
} else tmpl;
|
||||
const inst_ty = self.instantiateGenericStruct(use_tmpl, inner_call.args);
|
||||
const inst_name = self.formatTypeName(inst_ty);
|
||||
// Look up template method, monomorphize, and call
|
||||
if (self.struct_instance_template.get(inst_name)) |tmpl_name| {
|
||||
const tmpl_qualified = std.fmt.allocPrint(self.alloc, "{s}.{s}", .{ tmpl_name, fa.field }) catch fa.field;
|
||||
if (self.program_index.fn_ast_map.get(tmpl_qualified)) |fd| {
|
||||
const method_fd: ?*const ast.FnDecl = if (vis) |v|
|
||||
structMethodFn(v.sd, fa.field)
|
||||
else fm: {
|
||||
const tmpl_qualified = std.fmt.allocPrint(self.alloc, "{s}.{s}", .{ tmpl_name, fa.field }) catch fa.field;
|
||||
break :fm self.program_index.fn_ast_map.get(tmpl_qualified);
|
||||
};
|
||||
if (method_fd) |fd| {
|
||||
if (self.struct_instance_bindings.getPtr(inst_name)) |bindings| {
|
||||
const mangled = std.fmt.allocPrint(self.alloc, "{s}.{s}", .{ inst_name, fa.field }) catch fa.field;
|
||||
if (!self.lowered_functions.contains(mangled)) {
|
||||
@@ -14981,19 +15018,20 @@ pub const Lowering = struct {
|
||||
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 {
|
||||
/// The bare-VISIBLE single generic-struct author of `name` (its `StructDecl` +
|
||||
/// defining source) when that author is NOT the one the global last-wins
|
||||
/// `struct_template_map` already holds — the E4 non-transitive selection for a
|
||||
/// bare generic head / alias / static-method head 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). Exposing the decl (not just a
|
||||
/// rebuilt template) lets a static-method head source-pin the METHOD body too,
|
||||
/// not only the type layout. 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 bareVisibleStructDecl(self: *Lowering, name: []const u8) ?VisibleStructAuthor {
|
||||
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;
|
||||
@@ -15006,7 +15044,7 @@ pub const Lowering = struct {
|
||||
};
|
||||
if (sd.type_params.len == 0) return null;
|
||||
if (std.mem.eql(u8, from, canon_src)) return null;
|
||||
return self.buildGenericStructTemplate(sd, from);
|
||||
return .{ .sd = sd, .source = from };
|
||||
}
|
||||
// Otherwise: the single 1-hop flat-import generic-struct author.
|
||||
var res = self.resolver();
|
||||
@@ -15024,7 +15062,16 @@ pub const Lowering = struct {
|
||||
}
|
||||
const sd = picked orelse return null;
|
||||
if (std.mem.eql(u8, picked_src, canon_src)) return null;
|
||||
return self.buildGenericStructTemplate(sd, picked_src);
|
||||
return .{ .sd = sd, .source = picked_src };
|
||||
}
|
||||
|
||||
/// The rebuilt, source-pinned generic struct TEMPLATE of the single bare-VISIBLE
|
||||
/// author (`bareVisibleStructDecl`) — instantiate this INSTEAD of the global
|
||||
/// last-wins map entry. Null under the same conditions `bareVisibleStructDecl`
|
||||
/// returns null (caller keeps the global map, byte-identical).
|
||||
fn bareVisibleStructTemplate(self: *Lowering, name: []const u8) ?StructTemplate {
|
||||
const v = self.bareVisibleStructDecl(name) orelse return null;
|
||||
return self.buildGenericStructTemplate(v.sd, v.source);
|
||||
}
|
||||
|
||||
/// Instantiate a generic struct template and register the result under an
|
||||
|
||||
Reference in New Issue
Block a user