feat(types): nominal identity + key-safe TypeTable mutation, ban raw update [stdlib D]

Phase D of the unified resolver: make the TypeTable safe to key by nominal
identity before same-name type shadows land (Phase E). Behavior-preserving —
nominal_id=0 means structural (today's keying, byte-identical); single-author
names intern to the same TypeId as before.

types.zig:
- StructInfo/EnumInfo/UnionInfo/TaggedUnionInfo/ErrorSetInfo gain
  `nominal_id: u32 = 0`. hash/eql fold it into the nominal arms ONLY, and only
  when nonzero, so legacy (structural) interning hashes/compares byte-identically.
- internNominal(info, nominal_id): stamps the id into the nominal arm then
  interns; nonzero id on a non-nominal info trips an assert.
- updatePreservingKey(id, info): field-fill that asserts the intern key is
  unchanged (replaces the forward-decl stub→full pattern).
- replaceKeyedInfo(id, info): the one legitimate re-key (anon rename
  __anon → Parent.field); removes the stale key and installs the new one.
- findUniqueByName: quarantined findByName that asserts ≤1 match.
- type_decl_tids: decl-node → TypeId identity map (the fn_decl_fids analogue),
  consumed by the resolver in Phase E.

Ban raw TypeTable.update outside types.zig (the acceptance bar): every caller
in lower.zig / type_bridge.zig / protocols.zig is reclassified — forward-decl
field fills route through updatePreservingKey, qualifyAnonType's rename through
replaceKeyedInfo. The raw `update` method is removed. Inline named type-decl
registration ("current winners") routes through internNominal(info, 0).

Tests (types.test.zig): forward-decl field fill (stable key), anon rename
(re-key), generic struct instantiation, type-returning function, parameterized
protocol value struct, same display-name → distinct nominal ids, plus an
old==new assertion (internNominal(.,0) byte-identical to legacy intern),
findUniqueByName, and the type_decl_tids identity map.

Gate: zig build (0), zig build test (421/421), run_examples (477, byte-identical),
m3te ios-sim build via worktree binary (0). No shadows registered; stubs intact.
This commit is contained in:
agra
2026-06-07 13:27:10 +03:00
parent e4d58b2abb
commit 09666cb90e
5 changed files with 333 additions and 42 deletions

View File

@@ -1,5 +1,6 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
const ast = @import("../ast.zig");
// ── TypeId ──────────────────────────────────────────────────────────────
// Opaque handle into the TypeTable. First 16 slots are reserved for builtins.
@@ -95,6 +96,10 @@ pub const TypeInfo = union(enum) {
// null ctx == none) rather than the standard `{T, i1}` discriminated
// layout — matching how `?Closure` works.
is_protocol: bool = false,
// Stable nominal identity, assigned once per decl pointer. Folds into
// the intern key so two same-display-name authors get distinct TypeIds.
// `0` == structural: the type is keyed by display name alone (legacy).
nominal_id: u32 = 0,
pub const Field = struct {
name: StringId,
@@ -108,11 +113,13 @@ pub const TypeInfo = union(enum) {
is_flags: bool = false,
explicit_values: ?[]const i64 = null, // for flags (power-of-2) or custom values
backing_type: ?TypeId = null, // e.g. u32 for `enum u32 { ... }`
nominal_id: u32 = 0, // stable nominal identity; 0 == structural (legacy)
};
pub const UnionInfo = struct {
name: StringId,
fields: []const StructInfo.Field,
nominal_id: u32 = 0, // stable nominal identity; 0 == structural (legacy)
};
pub const TaggedUnionInfo = struct {
@@ -121,6 +128,7 @@ pub const TypeInfo = union(enum) {
tag_type: TypeId, // tag integer type (e.g. .u32, .s64)
backing_type: ?TypeId = null, // enum struct backing (e.g. { tag: u32; _: u32; payload: [30]u32; })
explicit_tag_values: ?[]const i64 = null, // explicit variant values (e.g., quit :: 0x100)
nominal_id: u32 = 0, // stable nominal identity; 0 == structural (legacy)
};
pub const ArrayInfo = struct {
@@ -200,6 +208,7 @@ pub const TypeInfo = union(enum) {
pub const ErrorSetInfo = struct {
name: StringId,
tags: []const u32, // sorted global tag ids
nominal_id: u32 = 0, // stable nominal identity; 0 == structural (legacy)
};
};
@@ -323,6 +332,11 @@ pub const TypeTable = struct {
tags: TagRegistry,
/// Maps TypeInfo → TypeId for dedup of structural types
intern_map: std.HashMap(TypeKey, TypeId, TypeKeyContext, 80),
/// Stable nominal identity: type-decl AST node → its TypeId. The
/// `fn_decl_fids` analogue — one entry per declaring node, so two
/// same-display-name declarations resolve to distinct TypeIds via their
/// own node pointer. Populated by the resolver when it assigns nominal ids.
type_decl_tids: std.AutoHashMap(*const ast.Node, TypeId),
alloc: Allocator,
/// Owns the element/param slices duped by the type constructors
/// (`functionType*`, `closureType*`, `packType`). Freed wholesale in
@@ -339,6 +353,7 @@ pub const TypeTable = struct {
.strings = StringPool.init(alloc),
.tags = TagRegistry.init(alloc),
.intern_map = std.HashMap(TypeKey, TypeId, TypeKeyContext, 80).init(alloc),
.type_decl_tids = std.AutoHashMap(*const ast.Node, TypeId).init(alloc),
.alloc = alloc,
.slice_arena = std.heap.ArenaAllocator.init(alloc),
};
@@ -376,6 +391,7 @@ pub const TypeTable = struct {
self.strings.deinit(self.alloc);
self.tags.deinit(self.alloc);
self.intern_map.deinit();
self.type_decl_tids.deinit();
self.slice_arena.deinit();
}
@@ -396,13 +412,48 @@ pub const TypeTable = struct {
return id;
}
/// Update the TypeInfo for an existing TypeId. Used when a forward-declared
/// type (e.g., struct with empty fields) gets its full definition later.
pub fn update(self: *TypeTable, id: TypeId, info: TypeInfo) void {
const idx = id.index();
if (idx < self.infos.items.len) {
self.infos.items[idx] = info;
/// Intern a nominal type (struct/enum/union/tagged_union/error_set) under a
/// stable nominal identity. `nominal_id` folds into the intern key so two
/// authors that share a display name still get distinct TypeIds. With
/// `nominal_id == 0` this is byte-identical to `intern` (structural keying),
/// which is the only id used until same-name shadows land. Passing a nonzero
/// id for a non-nominal info is a caller bug (the id would be dropped).
pub fn internNominal(self: *TypeTable, info: TypeInfo, nominal_id: u32) TypeId {
var stamped = info;
switch (stamped) {
.@"struct" => |*s| s.nominal_id = nominal_id,
.@"enum" => |*e| e.nominal_id = nominal_id,
.@"union" => |*u| u.nominal_id = nominal_id,
.tagged_union => |*u| u.nominal_id = nominal_id,
.error_set => |*e| e.nominal_id = nominal_id,
else => std.debug.assert(nominal_id == 0),
}
return self.intern(stamped);
}
/// Replace the TypeInfo for an existing TypeId WITHOUT changing its intern
/// key. Used when a forward-declared type (struct with empty fields) gets
/// its full definition later: the key is the display name + nominal id, and
/// a field-fill touches neither. Asserts the key is unchanged so a real
/// re-key can't sneak through this path (use `replaceKeyedInfo` for that).
pub fn updatePreservingKey(self: *TypeTable, id: TypeId, info: TypeInfo) void {
const idx = id.index();
const old = self.infos.items[idx];
std.debug.assert((TypeKeyContext{}).eql(.{ .info = old }, .{ .info = info }));
self.infos.items[idx] = info;
}
/// Replace the TypeInfo for an existing TypeId AND re-key `intern_map` to
/// match the new info. The one legitimate re-key is the anonymous-type
/// rename (`__anon` → `Parent.field`), which mutates the display name and
/// therefore the key. Removes the stale key and installs the new one so the
/// renamed type interns and looks up under its new name only.
pub fn replaceKeyedInfo(self: *TypeTable, id: TypeId, info: TypeInfo) void {
const idx = id.index();
const old = self.infos.items[idx];
_ = self.intern_map.remove(.{ .info = old });
self.infos.items[idx] = info;
self.intern_map.put(.{ .info = info }, id) catch unreachable;
}
/// Find a named type (struct/union/enum) by its StringId name.
@@ -422,6 +473,30 @@ pub const TypeTable = struct {
return null;
}
/// Source-sensitive variant of `findByName`: asserts at most one named type
/// matches, then returns it (or null). Quarantines the global first-match
/// scan — new resolver code that must not silently pick a first-of-many
/// author uses this so a same-name collision trips the assert instead of
/// resolving arbitrarily.
pub fn findUniqueByName(self: *const TypeTable, name: StringId) ?TypeId {
var found: ?TypeId = null;
for (self.infos.items, 0..) |info, i| {
const n: ?StringId = switch (info) {
.@"struct" => |s| s.name,
.@"union" => |u| u.name,
.tagged_union => |u| u.name,
.@"enum" => |e| e.name,
.error_set => |e| e.name,
else => null,
};
if (n != null and n.? == name) {
std.debug.assert(found == null);
found = TypeId.fromIndex(@intCast(i));
}
}
return found;
}
// ── Convenience constructors ────────────────────────────────────────
pub fn ptrTo(self: *TypeTable, pointee: TypeId) TypeId {
@@ -949,12 +1024,29 @@ fn hashTypeInfo(h: *std.hash.Wyhash, info: TypeInfo) void {
h.update(&.{pack_present});
if (c.pack_start) |ps| h.update(std.mem.asBytes(&ps));
},
.@"struct" => |s| h.update(std.mem.asBytes(&s.name)),
.@"enum" => |e| h.update(std.mem.asBytes(&e.name)),
.@"union" => |u| h.update(std.mem.asBytes(&u.name)),
.tagged_union => |u| h.update(std.mem.asBytes(&u.name)),
// Nominal arms key by display name; `nominal_id` joins the key only when
// nonzero, so structural (legacy) interning hashes byte-identically.
.@"struct" => |s| {
h.update(std.mem.asBytes(&s.name));
if (s.nominal_id != 0) h.update(std.mem.asBytes(&s.nominal_id));
},
.@"enum" => |e| {
h.update(std.mem.asBytes(&e.name));
if (e.nominal_id != 0) h.update(std.mem.asBytes(&e.nominal_id));
},
.@"union" => |u| {
h.update(std.mem.asBytes(&u.name));
if (u.nominal_id != 0) h.update(std.mem.asBytes(&u.nominal_id));
},
.tagged_union => |u| {
h.update(std.mem.asBytes(&u.name));
if (u.nominal_id != 0) h.update(std.mem.asBytes(&u.nominal_id));
},
.protocol => |p| h.update(std.mem.asBytes(&p.name)),
.error_set => |e| h.update(std.mem.asBytes(&e.name)),
.error_set => |e| {
h.update(std.mem.asBytes(&e.name));
if (e.nominal_id != 0) h.update(std.mem.asBytes(&e.nominal_id));
},
.tuple => |t| {
for (t.fields) |f| h.update(std.mem.asBytes(&f));
if (t.names) |ns| for (ns) |n| h.update(std.mem.asBytes(&n));
@@ -1002,12 +1094,14 @@ fn typeInfoEql(a: TypeInfo, b: TypeInfo) bool {
if (c.pack_start) |cp| if (cp != d.pack_start.?) return false;
return c.ret == d.ret;
},
.@"struct" => |s| s.name == b.@"struct".name,
.@"enum" => |e| e.name == b.@"enum".name,
.@"union" => |u| u.name == b.@"union".name,
.tagged_union => |u| u.name == b.tagged_union.name,
// Nominal arms compare display name + nominal id. With both ids 0 this is
// name-only equality (legacy); a nonzero id distinguishes same-name authors.
.@"struct" => |s| s.name == b.@"struct".name and s.nominal_id == b.@"struct".nominal_id,
.@"enum" => |e| e.name == b.@"enum".name and e.nominal_id == b.@"enum".nominal_id,
.@"union" => |u| u.name == b.@"union".name and u.nominal_id == b.@"union".nominal_id,
.tagged_union => |u| u.name == b.tagged_union.name and u.nominal_id == b.tagged_union.nominal_id,
.protocol => |p| p.name == b.protocol.name,
.error_set => |e| e.name == b.error_set.name,
.error_set => |e| e.name == b.error_set.name and e.nominal_id == b.error_set.nominal_id,
.tuple => |t| {
const u = b.tuple;
if (t.fields.len != u.fields.len) return false;