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

@@ -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, "__interp_print_frames")) return refl(bare_name, .void);
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, "type_is_unsigned")) return refl(bare_name, .bool);
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);
return switch (info) {
.@"struct" => |s| self.l.module.types.getString(s.name),
.@"union" => |u| self.l.module.types.getString(u.name),
.tagged_union => |u| self.l.module.types.getString(u.name),
.@"enum" => |e| self.l.module.types.getString(e.name),
// A nominal type's mangle includes its `nominal_id` when nonzero so two
// same-DISPLAY-name authors in different sources (issue 0105) produce
// DISTINCT monomorph symbols (`struct_to_string__Box` vs
// `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: {
const inner = self.mangleTypeName(p.pointee);
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 {
var buf = std.ArrayList(u8).empty;
buf.appendSlice(self.l.alloc, prefix) catch return prefix;

View File

@@ -2523,12 +2523,18 @@ pub const Lowering = struct {
var reentry = FnBodyReentry.enter(self);
defer reentry.restore();
const ret_ty = self.resolveReturnType(fd);
// Re-use the existing function slot — switch builder to it.
// 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
// 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;
const func = &self.module.functions.items[@intFromEnum(fid)];
self.setCurrentSourceFile(func.source_file);
const ret_ty = self.resolveReturnType(fd);
if (!func.is_extern) {
// Already promoted (e.g., via lowerComptimeDeps) — skip.
return;
@@ -5610,11 +5616,15 @@ pub const Lowering = struct {
}
}
const ty: TypeId = if (sl.struct_name) |name| blk: {
const name_id = self.module.types.internString(name);
break :blk self.module.types.findByName(name_id) orelse
self.module.types.intern(.{ .@"struct" = .{ .name = name_id, .fields = &.{} } });
} else if (sl.type_expr) |te|
const ty: TypeId = if (sl.struct_name) |name|
// Source-aware (E2): a bare struct-literal type name resolves to the
// querying source's OWN same-name author, not the global `findByName`
// first-match — so `Box.{...}` in module B builds B's `Box`, never a
// 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
self.resolveTypeWithBindings(te)
else self.target_type orelse .unresolved;
@@ -11876,12 +11886,12 @@ pub const Lowering = struct {
return self.builder.emit(.{ .interp_print_frames = {} }, .void);
}
if (std.mem.eql(u8, name, "__trace_resolve_frame")) {
// Backs `trace.sx`'s formatter: a raw trace-buffer u64 → a `Frame`.
// Compiled code reinterprets the operand as `*Frame` and loads it;
// Backs `trace.sx`'s formatter: a raw trace-buffer u64 → a `TraceFrame`.
// Compiled code reinterprets the operand as `*TraceFrame` and loads it;
// the interp unpacks (func_id, span.start) and resolves (ERR E3.0
// slice 3b). Result type is the `Frame` struct from trace.sx.
const frame_ty = self.module.types.findByName(self.module.types.internString("Frame")) orelse {
if (self.diagnostics) |d| d.addFmt(.err, null, "`__trace_resolve_frame` needs `Frame` (from trace.sx) in scope", .{});
// slice 3b). Result type is the `TraceFrame` struct from trace.sx.
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 `TraceFrame` (from trace.sx) in scope", .{});
return self.builder.constInt(0, .void);
};
const arg = self.lowerExpr(c.args[0]);