diff --git a/examples/0170-types-anon-struct-field-distinct.sx b/examples/0170-types-anon-struct-field-distinct.sx new file mode 100644 index 0000000..f6425bd --- /dev/null +++ b/examples/0170-types-anon-struct-field-distinct.sx @@ -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 +} diff --git a/examples/0752-modules-same-name-struct-distinct-fields.sx b/examples/0752-modules-same-name-struct-distinct-fields.sx new file mode 100644 index 0000000..0cf5951 --- /dev/null +++ b/examples/0752-modules-same-name-struct-distinct-fields.sx @@ -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 +} diff --git a/examples/0752-modules-same-name-struct-distinct-fields/a.sx b/examples/0752-modules-same-name-struct-distinct-fields/a.sx new file mode 100644 index 0000000..85a97f3 --- /dev/null +++ b/examples/0752-modules-same-name-struct-distinct-fields/a.sx @@ -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 }; } diff --git a/examples/0752-modules-same-name-struct-distinct-fields/b.sx b/examples/0752-modules-same-name-struct-distinct-fields/b.sx new file mode 100644 index 0000000..2663433 --- /dev/null +++ b/examples/0752-modules-same-name-struct-distinct-fields/b.sx @@ -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 }; } diff --git a/examples/0753-modules-same-name-struct-same-fields.sx b/examples/0753-modules-same-name-struct-same-fields.sx new file mode 100644 index 0000000..6b547d7 --- /dev/null +++ b/examples/0753-modules-same-name-struct-same-fields.sx @@ -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 +} diff --git a/examples/0753-modules-same-name-struct-same-fields/a.sx b/examples/0753-modules-same-name-struct-same-fields/a.sx new file mode 100644 index 0000000..9642a1b --- /dev/null +++ b/examples/0753-modules-same-name-struct-same-fields/a.sx @@ -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 }; } diff --git a/examples/0753-modules-same-name-struct-same-fields/b.sx b/examples/0753-modules-same-name-struct-same-fields/b.sx new file mode 100644 index 0000000..2b8c35c --- /dev/null +++ b/examples/0753-modules-same-name-struct-same-fields/b.sx @@ -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 }; } diff --git a/examples/0754-modules-same-name-struct-own-wins.sx b/examples/0754-modules-same-name-struct-own-wins.sx new file mode 100644 index 0000000..a410d22 --- /dev/null +++ b/examples/0754-modules-same-name-struct-own-wins.sx @@ -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 +} diff --git a/examples/0754-modules-same-name-struct-own-wins/dep.sx b/examples/0754-modules-same-name-struct-own-wins/dep.sx new file mode 100644 index 0000000..aead1df --- /dev/null +++ b/examples/0754-modules-same-name-struct-own-wins/dep.sx @@ -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 }; } diff --git a/examples/0755-modules-same-name-struct-ambiguous.sx b/examples/0755-modules-same-name-struct-ambiguous.sx new file mode 100644 index 0000000..e70c414 --- /dev/null +++ b/examples/0755-modules-same-name-struct-ambiguous.sx @@ -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 +} diff --git a/examples/0755-modules-same-name-struct-ambiguous/a.sx b/examples/0755-modules-same-name-struct-ambiguous/a.sx new file mode 100644 index 0000000..98bf0f6 --- /dev/null +++ b/examples/0755-modules-same-name-struct-ambiguous/a.sx @@ -0,0 +1,2 @@ +// One of two flat-imported authors of a same-name `Thing`. +Thing :: struct { a: s64; } diff --git a/examples/0755-modules-same-name-struct-ambiguous/b.sx b/examples/0755-modules-same-name-struct-ambiguous/b.sx new file mode 100644 index 0000000..57a4e04 --- /dev/null +++ b/examples/0755-modules-same-name-struct-ambiguous/b.sx @@ -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; } diff --git a/examples/0756-modules-same-name-alias-per-source.sx b/examples/0756-modules-same-name-alias-per-source.sx new file mode 100644 index 0000000..55bfbb9 --- /dev/null +++ b/examples/0756-modules-same-name-alias-per-source.sx @@ -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 +} diff --git a/examples/0756-modules-same-name-alias-per-source/a.sx b/examples/0756-modules-same-name-alias-per-source/a.sx new file mode 100644 index 0000000..6ae65d3 --- /dev/null +++ b/examples/0756-modules-same-name-alias-per-source/a.sx @@ -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; } diff --git a/examples/0756-modules-same-name-alias-per-source/b.sx b/examples/0756-modules-same-name-alias-per-source/b.sx new file mode 100644 index 0000000..c8309ab --- /dev/null +++ b/examples/0756-modules-same-name-alias-per-source/b.sx @@ -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; } diff --git a/examples/expected/0170-types-anon-struct-field-distinct.exit b/examples/expected/0170-types-anon-struct-field-distinct.exit new file mode 100644 index 0000000..573541a --- /dev/null +++ b/examples/expected/0170-types-anon-struct-field-distinct.exit @@ -0,0 +1 @@ +0 diff --git a/examples/expected/0170-types-anon-struct-field-distinct.stderr b/examples/expected/0170-types-anon-struct-field-distinct.stderr new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/examples/expected/0170-types-anon-struct-field-distinct.stderr @@ -0,0 +1 @@ + diff --git a/examples/expected/0170-types-anon-struct-field-distinct.stdout b/examples/expected/0170-types-anon-struct-field-distinct.stdout new file mode 100644 index 0000000..b85905e --- /dev/null +++ b/examples/expected/0170-types-anon-struct-field-distinct.stdout @@ -0,0 +1 @@ +1 2 3 diff --git a/examples/expected/0752-modules-same-name-struct-distinct-fields.exit b/examples/expected/0752-modules-same-name-struct-distinct-fields.exit new file mode 100644 index 0000000..573541a --- /dev/null +++ b/examples/expected/0752-modules-same-name-struct-distinct-fields.exit @@ -0,0 +1 @@ +0 diff --git a/examples/expected/0752-modules-same-name-struct-distinct-fields.stderr b/examples/expected/0752-modules-same-name-struct-distinct-fields.stderr new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/examples/expected/0752-modules-same-name-struct-distinct-fields.stderr @@ -0,0 +1 @@ + diff --git a/examples/expected/0752-modules-same-name-struct-distinct-fields.stdout b/examples/expected/0752-modules-same-name-struct-distinct-fields.stdout new file mode 100644 index 0000000..116ad52 --- /dev/null +++ b/examples/expected/0752-modules-same-name-struct-distinct-fields.stdout @@ -0,0 +1 @@ +a=Box{x: 7} b=Box{p: 3, q: 4} diff --git a/examples/expected/0753-modules-same-name-struct-same-fields.exit b/examples/expected/0753-modules-same-name-struct-same-fields.exit new file mode 100644 index 0000000..573541a --- /dev/null +++ b/examples/expected/0753-modules-same-name-struct-same-fields.exit @@ -0,0 +1 @@ +0 diff --git a/examples/expected/0753-modules-same-name-struct-same-fields.stderr b/examples/expected/0753-modules-same-name-struct-same-fields.stderr new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/examples/expected/0753-modules-same-name-struct-same-fields.stderr @@ -0,0 +1 @@ + diff --git a/examples/expected/0753-modules-same-name-struct-same-fields.stdout b/examples/expected/0753-modules-same-name-struct-same-fields.stdout new file mode 100644 index 0000000..e3b306c --- /dev/null +++ b/examples/expected/0753-modules-same-name-struct-same-fields.stdout @@ -0,0 +1 @@ +a=Pair{x: 1, y: 2} b=Pair{x: 5, y: 6} diff --git a/examples/expected/0754-modules-same-name-struct-own-wins.exit b/examples/expected/0754-modules-same-name-struct-own-wins.exit new file mode 100644 index 0000000..573541a --- /dev/null +++ b/examples/expected/0754-modules-same-name-struct-own-wins.exit @@ -0,0 +1 @@ +0 diff --git a/examples/expected/0754-modules-same-name-struct-own-wins.stderr b/examples/expected/0754-modules-same-name-struct-own-wins.stderr new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/examples/expected/0754-modules-same-name-struct-own-wins.stderr @@ -0,0 +1 @@ + diff --git a/examples/expected/0754-modules-same-name-struct-own-wins.stdout b/examples/expected/0754-modules-same-name-struct-own-wins.stdout new file mode 100644 index 0000000..e290ee4 --- /dev/null +++ b/examples/expected/0754-modules-same-name-struct-own-wins.stdout @@ -0,0 +1 @@ +own=Widget{m: 5} dep=Widget{a: 9} diff --git a/examples/expected/0755-modules-same-name-struct-ambiguous.exit b/examples/expected/0755-modules-same-name-struct-ambiguous.exit new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/examples/expected/0755-modules-same-name-struct-ambiguous.exit @@ -0,0 +1 @@ +1 diff --git a/examples/expected/0755-modules-same-name-struct-ambiguous.stderr b/examples/expected/0755-modules-same-name-struct-ambiguous.stderr new file mode 100644 index 0000000..d62ec55 --- /dev/null +++ b/examples/expected/0755-modules-same-name-struct-ambiguous.stderr @@ -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 }; + | ^^^^^ diff --git a/examples/expected/0755-modules-same-name-struct-ambiguous.stdout b/examples/expected/0755-modules-same-name-struct-ambiguous.stdout new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/examples/expected/0755-modules-same-name-struct-ambiguous.stdout @@ -0,0 +1 @@ + diff --git a/examples/expected/0756-modules-same-name-alias-per-source.exit b/examples/expected/0756-modules-same-name-alias-per-source.exit new file mode 100644 index 0000000..573541a --- /dev/null +++ b/examples/expected/0756-modules-same-name-alias-per-source.exit @@ -0,0 +1 @@ +0 diff --git a/examples/expected/0756-modules-same-name-alias-per-source.stderr b/examples/expected/0756-modules-same-name-alias-per-source.stderr new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/examples/expected/0756-modules-same-name-alias-per-source.stderr @@ -0,0 +1 @@ + diff --git a/examples/expected/0756-modules-same-name-alias-per-source.stdout b/examples/expected/0756-modules-same-name-alias-per-source.stdout new file mode 100644 index 0000000..8c61644 --- /dev/null +++ b/examples/expected/0756-modules-same-name-alias-per-source.stdout @@ -0,0 +1 @@ +a=100 b=2.500000 diff --git a/library/modules/trace.sx b/library/modules/trace.sx index 2005042..8323ee5 100644 --- a/library/modules/trace.sx +++ b/library/modules/trace.sx @@ -12,7 +12,7 @@ // 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 -// 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 // path — a packed `(func_id, ir_offset)` resolved via the interpreter's IR // tables — lands with slice 3b.) @@ -20,9 +20,13 @@ libc :: #library "c"; -// The compiled return-trace frame. Layout MUST match `getFrameStructType` in -// src/ir/emit_llvm.zig and `SxFrame` in library/vendors/sx_trace_runtime/sx_trace.c. -Frame :: struct { +// The compiled return-trace frame. Named `TraceFrame` (not `Frame`) so it never +// collides with a UI / geometry `Frame` a consumer flat-imports — same-name +// 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; line: 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), // 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 -// 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. sx_trace_len :: () -> u32 #foreign; sx_trace_truncated :: () -> u32 #foreign; diff --git a/src/imports.zig b/src/imports.zig index 5ebdc3d..47747d6 100644 --- a/src/imports.zig +++ b/src/imports.zig @@ -313,7 +313,11 @@ pub const ResolvedModule = struct { if (self.scope.contains(name)) return false; try self.scope.put(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 { try seen_list.put(name, {}); } @@ -346,14 +350,42 @@ pub const ResolvedModule = struct { for (other.decls) |decl| { if (seen_nodes.contains(decl)) continue; if (decl.data.declName()) |name| { - if (seen_list.contains(name)) continue; - try seen_list.put(name, {}); + if (seen_list.contains(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 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 /// part of this module's own decls (so a flat-importer of this module /// sees the alias one hop out — matching authored names). diff --git a/src/ir/calls.zig b/src/ir/calls.zig index 29c6e89..bdf6c63 100644 --- a/src/ir/calls.zig +++ b/src/ir/calls.zig @@ -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); diff --git a/src/ir/generics.zig b/src/ir/generics.zig index e8a3d4e..ab7fce3 100644 --- a/src/ir/generics.zig +++ b/src/ir/generics.zig @@ -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` 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; diff --git a/src/ir/lower.zig b/src/ir/lower.zig index 4e2a583..7e7f054 100644 --- a/src/ir/lower.zig +++ b/src/ir/lower.zig @@ -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]);