feat(stdlib/E6a): per-decl nominal identity for enum + union decls

Give top-level ENUM and UNION decls per-decl nominal identity so two
same-name flat enums/unions intern DISTINCT nominal TypeIds instead of
collapsing to one global last-wins entry. Establishes the reusable
non-struct register path the later E6 kind-steps (E6b error-set, E6c
protocol, E6d foreign-class) extend.

Registration side (was: stateless `type_bridge.resolveInlineEnum/Union`
`findByName` last-wins short-circuit, no Lowering access):
- Split the type_bridge inline builders into a body-BUILDER
  (`buildEnumInfo` / `buildUnionInfo`) + the existing thin interner
  wrappers (field-type positions keep the legacy single-slot path).
- Add `Lowering.registerEnumDecl` / `registerUnionDecl` mirroring
  `registerStructDecl`: build the TypeInfo, intern via
  `internNamedTypeDecl(decl_key, name_id, info, nominal_id)` under the
  per-decl nominal identity (reserved slot id, else `shadowNominalId`).
- Reroute all six enum/union registration dispatch sites (scanDecls
  const-wrapped + top-level, lowerDecls/comptime, block-local, local
  const) to the new path.

Shared infra generalized ONCE:
- Pass-0b genuine-shadow pre-pass now reserves struct/enum/union shadow
  slots of the MATCHING kind, grouped by (kind, name), via a kind-generic
  `topLevelTypeDecl` / `reserveShadowSlot`. A forward/self/mutual ref to a
  shadow name binds to the reserved nominal TypeId.
- `namedRefTid` consults `type_decl_tids` for `.enum_decl`/`.union_decl`
  before the global `findByName`.

No new per-kind resolution path: selectNominalLeaf / headTypeGate /
flatTypeAuthorCount already gate every kind. Single-author /
phantom-double-spelling names keep nominal_id 0 (byte-identical corpus).

Regressions 0795-0798 (enum + union: ambiguity over every bare-type form,
and own-wins with distinct nominal TypeIds), fail-before/pass-after:
0795/0797 exit 0 -> exit 1 with the loud "type is ambiguous" diagnostic;
0796 silently printed `own=.east` -> correct `own=.north`; 0798 hard
`field 'm' not found` error -> correct `own=5 dep=9`.

Gate: zig build && zig build test (423/423) && run_examples.sh (537/537)
all exit 0; m3te ios-sim build via the main binary exit 0.
This commit is contained in:
agra
2026-06-08 23:18:29 +03:00
parent e5db824477
commit eed2f99f76
24 changed files with 432 additions and 75 deletions

View File

@@ -0,0 +1,37 @@
// E6a — per-decl nominal identity for ENUM decls. A bare ENUM reference is
// non-transitive AND ambiguity-checked at every site, exactly like the struct
// leaf (0755) and the non-leaf struct forms (0767). `main` flat-imports two
// modules that each author a same-name `Dir` enum and authors none itself, so
// EACH of the following bare ENUM forms is a genuine collision the source cannot
// disambiguate — and each must emit the LOUD "type 'Dir' is ambiguous" diagnostic
// and poison the result, NEVER silently pick a global `findByName` last-wins
// author:
//
// - reflection / type-arg slot `size_of(Dir)`
// - typed enum-value annotation `d : Dir = .north`
// - type-as-value `t : Type = Dir`
// - type-category match arm `case Dir:`
//
// Fail-before (pre-E6a): the stateless `type_bridge.resolveInlineEnum`
// `findByName` short-circuit interned ONE global last-wins `Dir`, so every bare
// form silently resolved to it and the program exited 0.
#import "modules/std.sx";
#import "0795-modules-same-name-enum-ambiguous/a.sx";
#import "0795-modules-same-name-enum-ambiguous/b.sx";
describe :: ($T: Type) -> s32 {
r := if T == {
case Dir: 1;
else: 0;
}
r
}
main :: () -> s32 {
sz := size_of(Dir);
d : Dir = .north;
t : Type = Dir;
k := describe(s64);
0
}

View File

@@ -0,0 +1,4 @@
// One of two flat-imported authors of a same-name `Dir` enum. With both modules
// flat-visible from a file that authors none itself, every bare reference to the
// name is genuinely ambiguous.
Dir :: enum { north; south; east; west; }

View File

@@ -0,0 +1,4 @@
// The second flat-imported author of a same-name `Dir` enum. A separate nominal
// identity from a.sx's `Dir`, so each bare reference is a real collision the
// importing source cannot disambiguate.
Dir :: enum { north; south; east; west; }

View File

@@ -0,0 +1,22 @@
// E6a — own-wins-over-flat for ENUM per-decl nominal identity. `main` flat-imports
// `dep.sx` (which authors `Dir { east; west }`) AND authors its OWN `Dir { north;
// south }`. A bare `Dir` reference in `main` resolves to `main`'s OWN author, not
// the flat-imported one (the querying source's author wins outright — no
// ambiguity), so `d : Dir = .north` binds `main`'s enum (whose `.north` variant
// dep's `Dir` lacks) while `dep_dir()` returns dep's DISTINCT `Dir`.
//
// Fail-before (pre-E6a): the stateless `type_bridge.resolveInlineEnum` `findByName`
// short-circuit interned ONE global last-wins `Dir`, so `main`'s `Dir` and dep's
// `Dir` collapsed to a single nominal — `.north` would resolve against whichever
// author won the global slot, silently wrong with no diagnostic.
#import "modules/std.sx";
#import "0796-modules-same-name-enum-own-wins/dep.sx";
Dir :: enum { north; south; }
main :: () -> s32 {
d : Dir = .north;
print("own={} dep={}\n", d, dep_dir());
0
}

View File

@@ -0,0 +1,6 @@
// A flat-imported module authors its OWN `Dir { east; west }`. The importing file
// (`main`) ALSO authors a `Dir` — its own author must win there (own-wins), while
// this module's `Dir` stays a DISTINCT nominal type used by `dep_dir`. The variant
// sets are disjoint, so a cross-binding to the wrong `Dir` is a hard compile error.
Dir :: enum { east; west; }
dep_dir :: () -> Dir { return .west; }

View File

@@ -0,0 +1,36 @@
// E6a — per-decl nominal identity for UNION decls. A bare UNION reference is
// non-transitive AND ambiguity-checked at every site, exactly like the enum
// (0795) and struct (0767) forms. `main` flat-imports two modules that each author
// a same-name `Pair` union and authors none itself, so EACH of the following bare
// UNION forms is a genuine collision the source cannot disambiguate — and each must
// emit the LOUD "type 'Pair' is ambiguous" diagnostic and poison the result, NEVER
// silently pick a global `findByName` last-wins author:
//
// - reflection / type-arg slot `size_of(Pair)`
// - typed annotation `u : Pair = ---`
// - type-as-value `t : Type = Pair`
// - type-category match arm `case Pair:`
//
// Fail-before (pre-E6a): the stateless `type_bridge.resolveInlineUnion`
// `findByName` short-circuit interned ONE global last-wins `Pair`, so every bare
// form silently resolved to it and the program exited 0.
#import "modules/std.sx";
#import "0797-modules-same-name-union-ambiguous/a.sx";
#import "0797-modules-same-name-union-ambiguous/b.sx";
describe :: ($T: Type) -> s32 {
r := if T == {
case Pair: 1;
else: 0;
}
r
}
main :: () -> s32 {
sz := size_of(Pair);
u : Pair = ---;
t : Type = Pair;
k := describe(s64);
0
}

View File

@@ -0,0 +1,4 @@
// One of two flat-imported authors of a same-name `Pair` union. With both modules
// flat-visible from a file that authors none itself, every bare reference to the
// name is genuinely ambiguous.
Pair :: union { f: f32; i: s32; }

View File

@@ -0,0 +1,4 @@
// The second flat-imported author of a same-name `Pair` union. A separate nominal
// identity from a.sx's `Pair`, so each bare reference is a real collision the
// importing source cannot disambiguate.
Pair :: union { f: f32; i: s32; }

View File

@@ -0,0 +1,23 @@
// E6a — own-wins-over-flat for UNION per-decl nominal identity. `main` flat-imports
// `dep.sx` (which authors `Pair { a }`) AND authors its OWN `Pair { m }`. A bare
// `Pair` reference in `main` resolves to `main`'s OWN author, not the flat-imported
// one (the querying source's author wins outright — no ambiguity), so `p : Pair`
// here binds `main`'s union (whose `m` field dep's `Pair` lacks) while `dep_pair()`
// uses dep's DISTINCT `Pair`.
//
// Fail-before (pre-E6a): the stateless `type_bridge.resolveInlineUnion` `findByName`
// short-circuit interned ONE global last-wins `Pair`, so `main`'s `Pair` and dep's
// `Pair` collapsed to a single nominal — `p.m` would resolve against whichever
// author won the global slot, silently wrong with no diagnostic.
#import "modules/std.sx";
#import "0798-modules-same-name-union-own-wins/dep.sx";
Pair :: union { m: s32; }
main :: () -> s32 {
p : Pair = ---;
p.m = 5;
print("own={} dep={}\n", p.m, dep_pair());
0
}

View File

@@ -0,0 +1,10 @@
// A flat-imported module authors its OWN `Pair { a }`. The importing file (`main`)
// ALSO authors a `Pair` — its own author must win there (own-wins), while this
// module's `Pair` stays a DISTINCT nominal type used by `dep_pair`. The field sets
// are disjoint, so a cross-binding to the wrong `Pair` is a hard compile error.
Pair :: union { a: s32; }
dep_pair :: () -> s32 {
p : Pair = ---;
p.a = 9;
return p.a;
}

View File

@@ -0,0 +1 @@
1

View File

@@ -0,0 +1,23 @@
error: type 'Dir' is ambiguous: it is declared in multiple flat-imported modules; qualify the reference or remove the duplicate import
--> examples/0795-modules-same-name-enum-ambiguous.sx:32:19
|
32 | sz := size_of(Dir);
| ^^^
error: type 'Dir' is ambiguous: it is declared in multiple flat-imported modules; qualify the reference or remove the duplicate import
--> examples/0795-modules-same-name-enum-ambiguous.sx:33:9
|
33 | d : Dir = .north;
| ^^^
error: type 'Dir' is ambiguous: it is declared in multiple flat-imported modules; qualify the reference or remove the duplicate import
--> examples/0795-modules-same-name-enum-ambiguous.sx:34:16
|
34 | t : Type = Dir;
| ^^^
error: type 'Dir' is ambiguous: it is declared in multiple flat-imported modules; qualify the reference or remove the duplicate import
--> examples/0795-modules-same-name-enum-ambiguous.sx:25:14
|
25 | case Dir: 1;
| ^^^

View File

@@ -0,0 +1 @@
0

View File

@@ -0,0 +1 @@

View File

@@ -0,0 +1 @@
own=.north dep=.west

View File

@@ -0,0 +1 @@
1

View File

@@ -0,0 +1,23 @@
error: type 'Pair' is ambiguous: it is declared in multiple flat-imported modules; qualify the reference or remove the duplicate import
--> examples/0797-modules-same-name-union-ambiguous.sx:31:19
|
31 | sz := size_of(Pair);
| ^^^^
error: type 'Pair' is ambiguous: it is declared in multiple flat-imported modules; qualify the reference or remove the duplicate import
--> examples/0797-modules-same-name-union-ambiguous.sx:32:9
|
32 | u : Pair = ---;
| ^^^^
error: type 'Pair' is ambiguous: it is declared in multiple flat-imported modules; qualify the reference or remove the duplicate import
--> examples/0797-modules-same-name-union-ambiguous.sx:33:16
|
33 | t : Type = Pair;
| ^^^^
error: type 'Pair' is ambiguous: it is declared in multiple flat-imported modules; qualify the reference or remove the duplicate import
--> examples/0797-modules-same-name-union-ambiguous.sx:24:14
|
24 | case Pair: 1;
| ^^^^

View File

@@ -0,0 +1 @@
0

View File

@@ -0,0 +1 @@
own=5 dep=9