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,6 +1,7 @@
// Tests for types.zig
const std = @import("std");
const types = @import("types.zig");
const ast = @import("../ast.zig");
const TypeId = types.TypeId;
const TypeTable = types.TypeTable;
const TypeInfo = types.TypeInfo;
@@ -287,3 +288,199 @@ test "isUnsignedInt: user-defined arbitrary-width ints" {
const ptr_ty = table.ptrTo(.u32);
try std.testing.expect(!table.isUnsignedInt(ptr_ty));
}
// ── Phase D: nominal identity + key-safe mutation ───────────────────────
test "phase D: forward-decl field fill preserves intern key" {
const alloc = std.testing.allocator;
var table = TypeTable.init(alloc);
defer table.deinit();
const foo = table.internString("Foo");
const no_fields = [_]TypeInfo.StructInfo.Field{};
const stub: TypeInfo = .{ .@"struct" = .{ .name = foo, .fields = &no_fields } };
const id = table.internNominal(stub, 0);
// Full definition arrives later; same name (and nominal id) → same key.
const fields = [_]TypeInfo.StructInfo.Field{
.{ .name = table.internString("x"), .ty = .s64 },
.{ .name = table.internString("y"), .ty = .s64 },
};
table.updatePreservingKey(id, .{ .@"struct" = .{ .name = foo, .fields = &fields } });
// TypeId stable, fields filled, and a fresh structural intern of the same
// name still resolves to it (the field-fill didn't touch the key).
try std.testing.expectEqual(@as(usize, 2), table.get(id).@"struct".fields.len);
try std.testing.expectEqual(id, table.internNominal(stub, 0));
try std.testing.expectEqual(id, table.findByName(foo).?);
}
test "phase D: anon rename re-keys intern_map" {
const alloc = std.testing.allocator;
var table = TypeTable.init(alloc);
defer table.deinit();
const anon = table.internString("__anon");
const fields = [_]TypeInfo.StructInfo.Field{
.{ .name = table.internString("x"), .ty = .s64 },
};
const id = table.internNominal(.{ .@"struct" = .{ .name = anon, .fields = &fields } }, 0);
try std.testing.expectEqual(id, table.findByName(anon).?);
const qualified = table.internString("Parent.field");
table.replaceKeyedInfo(id, .{ .@"struct" = .{ .name = qualified, .fields = &fields } });
// Old name no longer resolves; new name does; same TypeId.
try std.testing.expect(table.findByName(anon) == null);
try std.testing.expectEqual(id, table.findByName(qualified).?);
// Re-keyed: structural intern under the new name dedups to the same id...
try std.testing.expectEqual(id, table.intern(.{ .@"struct" = .{ .name = qualified, .fields = &fields } }));
// ...and the stale old key is gone, so a fresh "__anon" gets a NEW id.
const fresh = table.intern(.{ .@"struct" = .{ .name = anon, .fields = &fields } });
try std.testing.expect(fresh != id);
}
test "phase D: generic struct instantiation interns by distinct names" {
const alloc = std.testing.allocator;
var table = TypeTable.init(alloc);
defer table.deinit();
const f1 = [_]TypeInfo.StructInfo.Field{.{ .name = table.internString("e"), .ty = .f32 }};
const vec3a = table.internNominal(.{ .@"struct" = .{ .name = table.internString("Vec__3"), .fields = &f1 } }, 0);
const vec4 = table.internNominal(.{ .@"struct" = .{ .name = table.internString("Vec__4"), .fields = &f1 } }, 0);
// Distinct instantiations → distinct ids.
try std.testing.expect(vec3a != vec4);
// Re-instantiating the same monomorph → same id (structural dedup by name).
const vec3b = table.internNominal(.{ .@"struct" = .{ .name = table.internString("Vec__3"), .fields = &f1 } }, 0);
try std.testing.expectEqual(vec3a, vec3b);
// A forward-decl fill on the instantiation keeps the id.
const f2 = [_]TypeInfo.StructInfo.Field{
.{ .name = table.internString("e"), .ty = .f32 },
.{ .name = table.internString("f"), .ty = .f32 },
};
table.updatePreservingKey(vec3a, .{ .@"struct" = .{ .name = table.internString("Vec__3"), .fields = &f2 } });
try std.testing.expectEqual(vec3a, table.findByName(table.internString("Vec__3")).?);
}
test "phase D: type-returning function result interns stably" {
const alloc = std.testing.allocator;
var table = TypeTable.init(alloc);
defer table.deinit();
// `Complex(u32)` registers a struct under the mangled alias name; interning
// the same instantiation twice is stable.
const name = table.internString("Complex__u32");
const fields = [_]TypeInfo.StructInfo.Field{
.{ .name = table.internString("re"), .ty = .u32 },
.{ .name = table.internString("im"), .ty = .u32 },
};
const info: TypeInfo = .{ .@"struct" = .{ .name = name, .fields = &fields } };
const a = table.internNominal(info, 0);
const b = table.internNominal(info, 0);
try std.testing.expectEqual(a, b);
try std.testing.expectEqual(a, table.findByName(name).?);
}
test "phase D: parameterized protocol value struct interns stably" {
const alloc = std.testing.allocator;
var table = TypeTable.init(alloc);
defer table.deinit();
// `instantiateParamProtocol` registers a `{ctx, __vtable}` value struct
// under a mangled name (e.g. `VL__s64`). Same instantiation → same id.
const void_ptr = table.ptrTo(.void);
const fields = [_]TypeInfo.StructInfo.Field{
.{ .name = table.internString("ctx"), .ty = void_ptr },
.{ .name = table.internString("__vtable"), .ty = void_ptr },
};
const info: TypeInfo = .{ .@"struct" = .{ .name = table.internString("VL__s64"), .fields = &fields, .is_protocol = true } };
const a = table.intern(info);
const b = table.intern(info);
try std.testing.expectEqual(a, b);
// A different parameterization is a different name → different id.
const other = table.intern(.{ .@"struct" = .{ .name = table.internString("VL__f64"), .fields = &fields, .is_protocol = true } });
try std.testing.expect(other != a);
}
test "phase D: same display-name distinct nominal ids" {
const alloc = std.testing.allocator;
var table = TypeTable.init(alloc);
defer table.deinit();
const foo = table.internString("Foo");
const f = [_]TypeInfo.StructInfo.Field{.{ .name = table.internString("x"), .ty = .s64 }};
const base: TypeInfo = .{ .@"struct" = .{ .name = foo, .fields = &f } };
const a = table.internNominal(base, 1);
const b = table.internNominal(base, 2);
const structural = table.internNominal(base, 0);
// Three authors of "Foo" → three distinct TypeIds.
try std.testing.expect(a != b);
try std.testing.expect(a != structural);
try std.testing.expect(b != structural);
// Re-interning the same nominal id is idempotent.
try std.testing.expectEqual(a, table.internNominal(base, 1));
try std.testing.expectEqual(b, table.internNominal(base, 2));
// The nominal id is recorded on the stored info.
try std.testing.expectEqual(@as(u32, 1), table.get(a).@"struct".nominal_id);
try std.testing.expectEqual(@as(u32, 2), table.get(b).@"struct".nominal_id);
try std.testing.expectEqual(@as(u32, 0), table.get(structural).@"struct".nominal_id);
// Same disambiguation holds for the enum and error_set nominal arms.
const bar = table.internString("Bar");
const variants = [_]types.StringId{ table.internString("a"), table.internString("b") };
const e1 = table.internNominal(.{ .@"enum" = .{ .name = bar, .variants = &variants } }, 1);
const e2 = table.internNominal(.{ .@"enum" = .{ .name = bar, .variants = &variants } }, 2);
try std.testing.expect(e1 != e2);
const baz = table.internString("Baz");
const tags = [_]u32{ 1, 2 };
const es1 = table.internNominal(.{ .error_set = .{ .name = baz, .tags = &tags } }, 1);
const es2 = table.internNominal(.{ .error_set = .{ .name = baz, .tags = &tags } }, 2);
try std.testing.expect(es1 != es2);
}
test "phase D: internNominal(.,0) is byte-identical to legacy intern (old==new)" {
const alloc = std.testing.allocator;
var table = TypeTable.init(alloc);
defer table.deinit();
const f = [_]TypeInfo.StructInfo.Field{.{ .name = table.internString("x"), .ty = .s64 }};
const variants = [_]types.StringId{table.internString("v")};
const tags = [_]u32{7};
const cases = [_]TypeInfo{
.{ .@"struct" = .{ .name = table.internString("S"), .fields = &f } },
.{ .@"enum" = .{ .name = table.internString("E"), .variants = &variants } },
.{ .@"union" = .{ .name = table.internString("U"), .fields = &f } },
.{ .tagged_union = .{ .name = table.internString("T"), .fields = &f, .tag_type = .s64 } },
.{ .error_set = .{ .name = table.internString("Err"), .tags = &tags } },
};
for (cases) |info| {
const old = table.intern(info); // legacy structural path
const new = table.internNominal(info, 0); // new API, structural id
try std.testing.expectEqual(old, new);
}
}
test "phase D: findUniqueByName returns the sole match" {
const alloc = std.testing.allocator;
var table = TypeTable.init(alloc);
defer table.deinit();
const foo = table.internString("Foo");
try std.testing.expect(table.findUniqueByName(foo) == null);
const id = table.internNominal(.{ .@"struct" = .{ .name = foo, .fields = &.{} } }, 0);
try std.testing.expectEqual(id, table.findUniqueByName(foo).?);
}
test "phase D: type_decl_tids maps decl node to TypeId" {
const alloc = std.testing.allocator;
var table = TypeTable.init(alloc);
defer table.deinit();
const id = table.internNominal(.{ .@"struct" = .{ .name = table.internString("Node1"), .fields = &.{} } }, 0);
var node = ast.Node{ .span = .{ .start = 0, .end = 0 }, .data = .{ .int_literal = .{ .value = 0 } } };
try table.type_decl_tids.put(&node, id);
try std.testing.expectEqual(id, table.type_decl_tids.get(&node).?);
}