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:
@@ -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]);
|
||||
|
||||
Reference in New Issue
Block a user