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:
20
examples/0170-types-anon-struct-field-distinct.sx
Normal file
20
examples/0170-types-anon-struct-field-distinct.sx
Normal 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
|
||||
}
|
||||
14
examples/0752-modules-same-name-struct-distinct-fields.sx
Normal file
14
examples/0752-modules-same-name-struct-distinct-fields.sx
Normal 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
|
||||
}
|
||||
@@ -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 }; }
|
||||
@@ -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 }; }
|
||||
15
examples/0753-modules-same-name-struct-same-fields.sx
Normal file
15
examples/0753-modules-same-name-struct-same-fields.sx
Normal 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
|
||||
}
|
||||
3
examples/0753-modules-same-name-struct-same-fields/a.sx
Normal file
3
examples/0753-modules-same-name-struct-same-fields/a.sx
Normal 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 }; }
|
||||
5
examples/0753-modules-same-name-struct-same-fields/b.sx
Normal file
5
examples/0753-modules-same-name-struct-same-fields/b.sx
Normal 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 }; }
|
||||
15
examples/0754-modules-same-name-struct-own-wins.sx
Normal file
15
examples/0754-modules-same-name-struct-own-wins.sx
Normal 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
|
||||
}
|
||||
5
examples/0754-modules-same-name-struct-own-wins/dep.sx
Normal file
5
examples/0754-modules-same-name-struct-own-wins/dep.sx
Normal 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 }; }
|
||||
13
examples/0755-modules-same-name-struct-ambiguous.sx
Normal file
13
examples/0755-modules-same-name-struct-ambiguous.sx
Normal 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
|
||||
}
|
||||
2
examples/0755-modules-same-name-struct-ambiguous/a.sx
Normal file
2
examples/0755-modules-same-name-struct-ambiguous/a.sx
Normal file
@@ -0,0 +1,2 @@
|
||||
// One of two flat-imported authors of a same-name `Thing`.
|
||||
Thing :: struct { a: s64; }
|
||||
3
examples/0755-modules-same-name-struct-ambiguous/b.sx
Normal file
3
examples/0755-modules-same-name-struct-ambiguous/b.sx
Normal 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; }
|
||||
13
examples/0756-modules-same-name-alias-per-source.sx
Normal file
13
examples/0756-modules-same-name-alias-per-source.sx
Normal 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
|
||||
}
|
||||
4
examples/0756-modules-same-name-alias-per-source/a.sx
Normal file
4
examples/0756-modules-same-name-alias-per-source/a.sx
Normal 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; }
|
||||
5
examples/0756-modules-same-name-alias-per-source/b.sx
Normal file
5
examples/0756-modules-same-name-alias-per-source/b.sx
Normal 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; }
|
||||
@@ -0,0 +1 @@
|
||||
0
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
1 2 3
|
||||
@@ -0,0 +1 @@
|
||||
0
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
a=Box{x: 7} b=Box{p: 3, q: 4}
|
||||
@@ -0,0 +1 @@
|
||||
0
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
a=Pair{x: 1, y: 2} b=Pair{x: 5, y: 6}
|
||||
@@ -0,0 +1 @@
|
||||
0
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
own=Widget{m: 5} dep=Widget{a: 9}
|
||||
@@ -0,0 +1 @@
|
||||
1
|
||||
@@ -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 };
|
||||
| ^^^^^
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
0
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
a=100 b=2.500000
|
||||
Reference in New Issue
Block a user