feat(stdlib): per-decl nominal identity + same-name shadows — close 0105 [stdlib E2]

Make same-name top-level types in different sources DISTINCT nominal types
instead of collapsing last-wins in the type table (issue 0105).

Registration:
- internNamedTypeDecl assigns a per-decl nominal_id and populates
  type_decl_tids. The first author of a name keeps nominal_id 0 (byte-identical
  to pre-E2); a genuine cross-module shadow (>=2 distinct normalized-path
  authors per the import facts) gets a fresh id -> a distinct TypeId.
- mergeFlat/addOwnDecl stop first-wins-dropping per-source decls (named types +
  non-fn const_decls) so every same-name author reaches registration; functions
  and var_decls (incl. #foreign extern globals) keep first-wins.

Resolution (selectNominalLeaf):
- own-author wins; else flatTypeAuthorCount over the transitive flat closure:
  >=2 distinct -> .ambiguous (loud diagnostic + poison); exactly one -> resolved;
  a flat author not yet findByName-registered -> .undeclared stub (not a leak).
- struct-literal type names route through the same source-aware leaf.
- lazyLowerFunction pins the function's own source before resolving its return
  type, so a shadowed signature type resolves in its module, not the caller's.

Codegen:
- mangleTypeName appends __n<id> for nonzero nominal_id so same-name shadows get
  distinct monomorph symbols (struct_to_string__Box vs __Box__n1).

Library hygiene:
- rename trace.sx's compiler-contracted Frame -> TraceFrame (+ the two compiler
  findByName sites) so it never collides with a UI/geometry Frame; the layout is
  structural (getFrameStructType / SxFrame), name-independent.

Examples: 0752-0756 pin the five 0105 cases (distinct fields / same fields /
own-wins / ambiguous / alias per-source); 0170 pins the folded anon-struct-field
regression.
This commit is contained in:
agra
2026-06-07 22:57:28 +03:00
parent 4b2a067991
commit d98ad5c14f
38 changed files with 233 additions and 26 deletions

View File

@@ -0,0 +1,20 @@
// Two top-level structs each carry an inline anonymous-struct field named
// `inner`, but of DIFFERENT shapes. Each `inner` must resolve to its OWN
// anonymous type (`A.inner` has `x`; `B.inner` has `y, z`) — they must not
// cross-bind on the shared field spelling.
//
// Regression (folded from the Phase-D `replaceKeyedInfo` re-key, which made the
// per-parent anon rename key-safe): on master 7ffc0c1 the two anon types
// cross-bound and `b.inner.y` failed with "field 'y' not found on type
// 'B.inner'". Pins fail-before / pass-after.
#import "modules/std.sx";
A :: struct { inner: struct { x: s64; }; }
B :: struct { inner: struct { y: s64; z: s64; }; }
main :: () -> s32 {
a := A.{ inner = .{ x = 1 } };
b := B.{ inner = .{ y = 2, z = 3 } };
print("{} {} {}\n", a.inner.x, b.inner.y, b.inner.z);
0
}

View File

@@ -0,0 +1,14 @@
// issue 0105 case 1 — same-name struct, DIFFERENT fields. Two flat-imported
// modules each declare a top-level `Box` with a different field set. Each
// module's function builds and returns ITS OWN `Box`; `main` (which authors no
// `Box`) prints both. Each value resolves against its declaring module's type,
// so the formatter shows A's `{x}` and B's `{p, q}` — proving the two `Box`
// types are distinct nominal identities, not a single last-wins collapse.
#import "modules/std.sx";
#import "0752-modules-same-name-struct-distinct-fields/a.sx";
#import "0752-modules-same-name-struct-distinct-fields/b.sx";
main :: () -> s32 {
print("a={} b={}\n", a_box(), b_box());
0
}

View File

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

View File

@@ -0,0 +1,5 @@
// Module B authors a DIFFERENT `Box` (two fields `p`, `q`) — a same-name shadow
// of A's `Box`. Pre-0105 the two collapsed last-wins in the type table, so one
// module's field set vanished; now each `Box` is a distinct nominal type.
Box :: struct { p: s64; q: s64; }
b_box :: () -> Box { return Box.{ p = 3, q = 4 }; }

View File

@@ -0,0 +1,15 @@
// issue 0105 case 2 — same-name struct, SAME fields. Two flat-imported modules
// each declare `Pair { x, y }` with identical shape. They are STILL distinct
// nominal identities (each holds its own per-source TypeId / nominal id), not
// folded into one — both register, both monomorphize their own formatter, and
// each module's value prints correctly. (The nominal-distinctness mechanism is
// pinned at the unit level in `types.test.zig`; this example pins that two
// identically-shaped same-name structs coexist without collapse or crash.)
#import "modules/std.sx";
#import "0753-modules-same-name-struct-same-fields/a.sx";
#import "0753-modules-same-name-struct-same-fields/b.sx";
main :: () -> s32 {
print("a={} b={}\n", a_pair(), b_pair());
0
}

View File

@@ -0,0 +1,3 @@
// Module A authors `Pair { x, y }`.
Pair :: struct { x: s64; y: s64; }
a_pair :: () -> Pair { return Pair.{ x = 1, y = 2 }; }

View File

@@ -0,0 +1,5 @@
// Module B authors `Pair { x, y }` with the SAME field shape as A's. The two
// still get distinct nominal identities (not collapsed): each keeps its own
// TypeId / per-source author, so both register and format independently.
Pair :: struct { x: s64; y: s64; }
b_pair :: () -> Pair { return Pair.{ x = 5, y = 6 }; }

View File

@@ -0,0 +1,15 @@
// issue 0105 case 3 — own-wins-over-flat. `main` flat-imports `dep.sx` (which
// authors `Widget { a }`) AND authors its OWN `Widget { m }`. A bare `Widget`
// reference in `main` resolves to `main`'s OWN author, not the flat-imported one
// (the querying source's author wins outright — no ambiguity), so `Widget.{ m }`
// builds `main`'s type while `dep_widget()` returns `dep`'s distinct `Widget`.
#import "modules/std.sx";
#import "0754-modules-same-name-struct-own-wins/dep.sx";
Widget :: struct { m: s64; }
main :: () -> s32 {
w := Widget.{ m = 5 };
print("own={} dep={}\n", w, dep_widget());
0
}

View File

@@ -0,0 +1,5 @@
// A flat-imported module authors its OWN `Widget { a }`. The importing file
// (`main`) ALSO authors a `Widget` — its own author must win there (0105 case 3),
// while this module's `Widget` stays a distinct type used by `dep_widget`.
Widget :: struct { a: s64; }
dep_widget :: () -> Widget { return Widget.{ a = 9 }; }

View File

@@ -0,0 +1,13 @@
// issue 0105 case 4 — two-flat-visible → AMBIGUOUS. `main` flat-imports two
// modules that each author a same-name `Thing`, and authors none itself. A bare
// `Thing` reference can't be disambiguated, so the compiler emits a LOUD
// diagnostic ("ambiguous … qualify the reference or remove the duplicate
// import") and poisons the result — never a silent first-/last-wins pick.
#import "modules/std.sx";
#import "0755-modules-same-name-struct-ambiguous/a.sx";
#import "0755-modules-same-name-struct-ambiguous/b.sx";
main :: () -> s32 {
t : Thing = .{ a = 1 };
0
}

View File

@@ -0,0 +1,2 @@
// One of two flat-imported authors of a same-name `Thing`.
Thing :: struct { a: s64; }

View File

@@ -0,0 +1,3 @@
// The second flat-imported author of a same-name `Thing`. With both visible and
// no own author in `main`, a bare `Thing` reference is genuinely ambiguous.
Thing :: struct { b: s64; }

View File

@@ -0,0 +1,13 @@
// issue 0105 case 5 — same-name type ALIAS, per-source visibility. Two
// flat-imported modules each alias `Id` to a DIFFERENT type (A: `s32`, B:
// `f64`). Each module's bare `Id` resolves against its OWN source alias, so A's
// `x : Id` is a 32-bit integer (prints 100) and B's `x : Id` is a float (prints
// 2.5) — proving aliases are source-keyed, never folded last-wins.
#import "modules/std.sx";
#import "0756-modules-same-name-alias-per-source/a.sx";
#import "0756-modules-same-name-alias-per-source/b.sx";
main :: () -> s32 {
print("a={} b={}\n", a_val(), b_val());
0
}

View File

@@ -0,0 +1,4 @@
// Module A aliases `Id` to `s32`. A bare `Id` in this module resolves to A's
// alias regardless of B's same-name alias (per-source alias visibility).
Id :: s32;
a_val :: () -> s64 { x : Id = 100; y : s64 = xx x; return y; }

View File

@@ -0,0 +1,5 @@
// Module B aliases the SAME name `Id` to a DIFFERENT type `f64`. A bare `Id` in
// this module resolves to B's `f64` alias, not A's `s32` — each module's alias
// is keyed to its own source, so the two never collide last-wins.
Id :: f64;
b_val :: () -> f64 { x : Id = 2; return x + 0.5; }

View File

@@ -0,0 +1 @@
0

View File

@@ -0,0 +1 @@
1 2 3

View File

@@ -0,0 +1 @@
a=Box{x: 7} b=Box{p: 3, q: 4}

View File

@@ -0,0 +1 @@
0

View File

@@ -0,0 +1 @@
a=Pair{x: 1, y: 2} b=Pair{x: 5, y: 6}

View File

@@ -0,0 +1 @@
0

View File

@@ -0,0 +1 @@
own=Widget{m: 5} dep=Widget{a: 9}

View File

@@ -0,0 +1 @@
1

View File

@@ -0,0 +1,5 @@
error: type 'Thing' is ambiguous: it is declared in multiple flat-imported modules; qualify the reference or remove the duplicate import
--> examples/0755-modules-same-name-struct-ambiguous.sx:11:9
|
11 | t : Thing = .{ a = 1 };
| ^^^^^

View File

@@ -0,0 +1 @@
0

View File

@@ -0,0 +1 @@
a=100 b=2.500000

View File

@@ -12,7 +12,7 @@
// still sees the chain) or the (future) failable-`main` wrapper. // still sees the chain) or the (future) failable-`main` wrapper.
// //
// Frame resolution (ERR E3.0 slice 3a): in compiled code a frame is a pointer // Frame resolution (ERR E3.0 slice 3a): in compiled code a frame is a pointer
// to an interned `Frame` the compiler stamped in at the push site, so the // to an interned `TraceFrame` the compiler stamped in at the push site, so the
// location resolves in-process with no DWARF and no symbolizer. (The comptime // location resolves in-process with no DWARF and no symbolizer. (The comptime
// path — a packed `(func_id, ir_offset)` resolved via the interpreter's IR // path — a packed `(func_id, ir_offset)` resolved via the interpreter's IR
// tables — lands with slice 3b.) // tables — lands with slice 3b.)
@@ -20,9 +20,13 @@
libc :: #library "c"; libc :: #library "c";
// The compiled return-trace frame. Layout MUST match `getFrameStructType` in // The compiled return-trace frame. Named `TraceFrame` (not `Frame`) so it never
// src/ir/emit_llvm.zig and `SxFrame` in library/vendors/sx_trace_runtime/sx_trace.c. // collides with a UI / geometry `Frame` a consumer flat-imports — same-name
Frame :: struct { // types are now distinct nominal identities (issue 0105), so a bare `Frame` must
// resolve unambiguously to the consumer's own. Layout MUST match
// `getFrameStructType` in src/ir/emit_llvm.zig and `SxFrame` in
// library/vendors/sx_trace_runtime/sx_trace.c.
TraceFrame :: struct {
file: string; file: string;
line: s32; line: s32;
col: s32; col: s32;
@@ -44,7 +48,7 @@ spaces :: (n: s32) -> string {
// The error-trace buffer C API (library/vendors/sx_trace_runtime/sx_trace.c), // The error-trace buffer C API (library/vendors/sx_trace_runtime/sx_trace.c),
// linked in for the JIT and auto-injected for AOT when traces are used. // linked in for the JIT and auto-injected for AOT when traces are used.
// `frame_at` returns the raw stored `u64`; `__trace_resolve_frame` turns it // `frame_at` returns the raw stored `u64`; `__trace_resolve_frame` turns it
// into a `Frame` — by reinterpreting the stamped `*Frame` in compiled code, or // into a `TraceFrame` — by reinterpreting the stamped `*TraceFrame` in compiled code, or
// by resolving the packed `(func_id, span.start)` in the comptime interpreter. // by resolving the packed `(func_id, span.start)` in the comptime interpreter.
sx_trace_len :: () -> u32 #foreign; sx_trace_len :: () -> u32 #foreign;
sx_trace_truncated :: () -> u32 #foreign; sx_trace_truncated :: () -> u32 #foreign;

View File

@@ -313,7 +313,11 @@ pub const ResolvedModule = struct {
if (self.scope.contains(name)) return false; if (self.scope.contains(name)) return false;
try self.scope.put(name, {}); try self.scope.put(name, {});
if (seen_list.contains(name)) { if (seen_list.contains(name)) {
append_to_global = false; // A cross-module name collision: drop from the global list
// (first-wins) UNLESS this is a per-source decl (type / alias /
// const), which must reach registration as a distinct author of
// its own module (issues 0104/0105).
append_to_global = isPerSourceDecl(decl);
} else { } else {
try seen_list.put(name, {}); try seen_list.put(name, {});
} }
@@ -346,14 +350,42 @@ pub const ResolvedModule = struct {
for (other.decls) |decl| { for (other.decls) |decl| {
if (seen_nodes.contains(decl)) continue; if (seen_nodes.contains(decl)) continue;
if (decl.data.declName()) |name| { if (decl.data.declName()) |name| {
if (seen_list.contains(name)) continue; if (seen_list.contains(name)) {
try seen_list.put(name, {}); // First-wins on a cross-module name collision — EXCEPT a
// per-source decl (type / alias / const), each of which must
// reach registration as a distinct same-name author of its own
// module (issues 0104/0105). Only FUNCTIONS keep first-wins
// (issue 0102 — the shadowed author stays reachable via its
// qualified name / SelectedFunc). Node identity (above) still
// de-dups a diamond import of the SAME decl.
if (!isPerSourceDecl(decl)) continue;
} else {
try seen_list.put(name, {});
}
} }
try seen_nodes.put(decl, {}); try seen_nodes.put(decl, {});
try list.append(allocator, decl); try list.append(allocator, decl);
} }
} }
/// A decl that must register PER-SOURCE: each same-name author across modules
/// registers against its OWN module rather than collapsing to a single
/// first-wins winner. NAMED types and non-function `const_decl`s (type
/// aliases + value consts, source-keyed via the alias/const caches) are
/// per-source — that is what closes issues 0104/0105. Everything else keeps
/// the first-wins name-merge: FUNCTIONS (issue 0102 — the shadowed author
/// stays reachable via its qualified name / SelectedFunc), and crucially
/// `var_decl`s, including a `#foreign` extern global declared in two files
/// (e.g. `__stdinp : *void #foreign;`) that MUST resolve to the ONE libSystem
/// symbol, not split into a duplicate `__stdinp.1`.
fn isPerSourceDecl(decl: *const Node) bool {
return switch (decl.data) {
.struct_decl, .enum_decl, .union_decl, .error_set_decl, .protocol_decl, .foreign_class_decl => true,
.const_decl => |cd| cd.value.data != .fn_decl,
else => false,
};
}
/// Add another module as a namespaced import. The alias `name` becomes /// Add another module as a namespaced import. The alias `name` becomes
/// part of this module's own decls (so a flat-importer of this module /// part of this module's own decls (so a flat-importer of this module
/// sees the alias one hop out — matching authored names). /// sees the alias one hop out — matching authored names).

View File

@@ -151,7 +151,7 @@ pub const CallResolver = struct {
if (std.mem.eql(u8, bare_name, "is_comptime")) return refl(bare_name, .bool); if (std.mem.eql(u8, bare_name, "is_comptime")) return refl(bare_name, .bool);
if (std.mem.eql(u8, bare_name, "__interp_print_frames")) return refl(bare_name, .void); if (std.mem.eql(u8, bare_name, "__interp_print_frames")) return refl(bare_name, .void);
if (std.mem.eql(u8, bare_name, "__trace_resolve_frame")) if (std.mem.eql(u8, bare_name, "__trace_resolve_frame"))
return refl(bare_name, self.l.module.types.findByName(self.l.module.types.internString("Frame")) orelse .unresolved); return refl(bare_name, self.l.module.types.findByName(self.l.module.types.internString("TraceFrame")) orelse .unresolved);
if (std.mem.eql(u8, bare_name, "is_flags")) return refl(bare_name, .bool); if (std.mem.eql(u8, bare_name, "is_flags")) return refl(bare_name, .bool);
if (std.mem.eql(u8, bare_name, "type_is_unsigned")) return refl(bare_name, .bool); if (std.mem.eql(u8, bare_name, "type_is_unsigned")) return refl(bare_name, .bool);
if (std.mem.eql(u8, bare_name, "type_of")) return refl(bare_name, .any); if (std.mem.eql(u8, bare_name, "type_of")) return refl(bare_name, .any);

View File

@@ -51,10 +51,16 @@ pub const GenericResolver = struct {
const info = self.l.module.types.get(ty); const info = self.l.module.types.get(ty);
return switch (info) { return switch (info) {
.@"struct" => |s| self.l.module.types.getString(s.name), // A nominal type's mangle includes its `nominal_id` when nonzero so two
.@"union" => |u| self.l.module.types.getString(u.name), // same-DISPLAY-name authors in different sources (issue 0105) produce
.tagged_union => |u| self.l.module.types.getString(u.name), // DISTINCT monomorph symbols (`struct_to_string__Box` vs
.@"enum" => |e| self.l.module.types.getString(e.name), // `struct_to_string__Box__n1`) instead of one symbol with conflicting
// signatures. `nominal_id == 0` (the single-author / structural case)
// appends nothing — byte-identical to the pre-E2 mangle.
.@"struct" => |s| self.mangleNominalName(self.l.module.types.getString(s.name), s.nominal_id),
.@"union" => |u| self.mangleNominalName(self.l.module.types.getString(u.name), u.nominal_id),
.tagged_union => |u| self.mangleNominalName(self.l.module.types.getString(u.name), u.nominal_id),
.@"enum" => |e| self.mangleNominalName(self.l.module.types.getString(e.name), e.nominal_id),
.pointer => |p| blk: { .pointer => |p| blk: {
const inner = self.mangleTypeName(p.pointee); const inner = self.mangleTypeName(p.pointee);
break :blk std.fmt.allocPrint(self.l.alloc, "ptr_{s}", .{inner}) catch "pointer"; break :blk std.fmt.allocPrint(self.l.alloc, "ptr_{s}", .{inner}) catch "pointer";
@@ -96,6 +102,14 @@ pub const GenericResolver = struct {
}; };
} }
/// Append a `__n<id>` disambiguator to a nominal type's display name when its
/// `nominal_id` is nonzero (a same-name shadow, issue 0105); id 0 returns the
/// name unchanged so single-author mangling is byte-identical.
fn mangleNominalName(self: GenericResolver, name: []const u8, nominal_id: u32) []const u8 {
if (nominal_id == 0) return name;
return std.fmt.allocPrint(self.l.alloc, "{s}__n{d}", .{ name, nominal_id }) catch name;
}
fn mangleParamList(self: GenericResolver, prefix: []const u8, params: []const TypeId, ret: TypeId) []const u8 { fn mangleParamList(self: GenericResolver, prefix: []const u8, params: []const TypeId, ret: TypeId) []const u8 {
var buf = std.ArrayList(u8).empty; var buf = std.ArrayList(u8).empty;
buf.appendSlice(self.l.alloc, prefix) catch return prefix; buf.appendSlice(self.l.alloc, prefix) catch return prefix;

View File

@@ -2523,12 +2523,18 @@ pub const Lowering = struct {
var reentry = FnBodyReentry.enter(self); var reentry = FnBodyReentry.enter(self);
defer reentry.restore(); defer reentry.restore();
const ret_ty = self.resolveReturnType(fd); // Re-use the existing function slot — switch builder to it. Pin the
// function's OWN source BEFORE resolving the return type, so a same-name
// Re-use the existing function slot — switch builder to it. // shadowed type in the signature (issue 0105) resolves against THIS
// function's module rather than the caller's (which, importing two
// same-name authors, would be ambiguous). Param types below already
// resolve after this point.
self.builder.func = fid; self.builder.func = fid;
const func = &self.module.functions.items[@intFromEnum(fid)]; const func = &self.module.functions.items[@intFromEnum(fid)];
self.setCurrentSourceFile(func.source_file); self.setCurrentSourceFile(func.source_file);
const ret_ty = self.resolveReturnType(fd);
if (!func.is_extern) { if (!func.is_extern) {
// Already promoted (e.g., via lowerComptimeDeps) — skip. // Already promoted (e.g., via lowerComptimeDeps) — skip.
return; return;
@@ -5610,11 +5616,15 @@ pub const Lowering = struct {
} }
} }
const ty: TypeId = if (sl.struct_name) |name| blk: { const ty: TypeId = if (sl.struct_name) |name|
const name_id = self.module.types.internString(name); // Source-aware (E2): a bare struct-literal type name resolves to the
break :blk self.module.types.findByName(name_id) orelse // querying source's OWN same-name author, not the global `findByName`
self.module.types.intern(.{ .@"struct" = .{ .name = name_id, .fields = &.{} } }); // first-match — so `Box.{...}` in module B builds B's `Box`, never a
} else if (sl.type_expr) |te| // flat-imported A's. `.undeclared`/`.pending` keep the empty-struct
// stub (byte-identical to the legacy `findByName orelse intern`);
// `.ambiguous`/`.not_visible` surface their loud diagnostic + poison.
self.resolveNominalLeaf(name, false, span)
else if (sl.type_expr) |te|
// Generic struct literal: Pair(s32).{ ... } — resolve type from type_expr // Generic struct literal: Pair(s32).{ ... } — resolve type from type_expr
self.resolveTypeWithBindings(te) self.resolveTypeWithBindings(te)
else self.target_type orelse .unresolved; else self.target_type orelse .unresolved;
@@ -11876,12 +11886,12 @@ pub const Lowering = struct {
return self.builder.emit(.{ .interp_print_frames = {} }, .void); return self.builder.emit(.{ .interp_print_frames = {} }, .void);
} }
if (std.mem.eql(u8, name, "__trace_resolve_frame")) { if (std.mem.eql(u8, name, "__trace_resolve_frame")) {
// Backs `trace.sx`'s formatter: a raw trace-buffer u64 → a `Frame`. // Backs `trace.sx`'s formatter: a raw trace-buffer u64 → a `TraceFrame`.
// Compiled code reinterprets the operand as `*Frame` and loads it; // Compiled code reinterprets the operand as `*TraceFrame` and loads it;
// the interp unpacks (func_id, span.start) and resolves (ERR E3.0 // the interp unpacks (func_id, span.start) and resolves (ERR E3.0
// slice 3b). Result type is the `Frame` struct from trace.sx. // slice 3b). Result type is the `TraceFrame` struct from trace.sx.
const frame_ty = self.module.types.findByName(self.module.types.internString("Frame")) orelse { const frame_ty = self.module.types.findByName(self.module.types.internString("TraceFrame")) orelse {
if (self.diagnostics) |d| d.addFmt(.err, null, "`__trace_resolve_frame` needs `Frame` (from trace.sx) in scope", .{}); if (self.diagnostics) |d| d.addFmt(.err, null, "`__trace_resolve_frame` needs `TraceFrame` (from trace.sx) in scope", .{});
return self.builder.constInt(0, .void); return self.builder.constInt(0, .void);
}; };
const arg = self.lowerExpr(c.args[0]); const arg = self.lowerExpr(c.args[0]);