P5.7 Step D: re-express metatype define() as sx over register_type

define(handle, info) is now an ordinary sx fn in modules/std/meta.sx: it matches
the TypeInfo union and calls the abi(.compiler) register_type primitive with the
matching kind code, decoding the variant/field/element list into []Member. An
all-void enum variant set registers as kind 2 (actual enum); any payload variant
as kind 3 (tagged_union).

To support matching the TypeInfo VALUE in the comptime VM, added tagged-union
value support: kindOf now treats tagged_union as a by-address aggregate, enum_tag
reads the tag word at offset 0, and a new enum_payload arm reads the active
payload at tag_size (both bail loudly on backing_type unions, whose layout
differs). register_type's duplicate-name diagnostics now include the offending
name. Dropped the define interception in tryLowerReflectionCall; the .enum(...)
arg infers TypeInfo from the sx fn's param type via the ordinary call path.

Regenerated 1179/1180 diagnostic snapshots (same span/line; the message now
names register_type instead of define()). define/type_info builtins still exist
pending dead-code removal.
This commit is contained in:
agra
2026-06-19 21:09:18 +03:00
parent 8850fcce70
commit 7b1d8ceb83
5 changed files with 117 additions and 51 deletions

View File

@@ -902,9 +902,44 @@ pub const Vm = struct {
if (oty.isBuiltin()) return .{ .value = v }; // already an integer tag
const table = try self.requireTable();
if (table.get(oty) == .@"enum") return .{ .value = v }; // payloadless: word IS the tag
self.detail = "comptime VM: enum_tag on a tagged union not yet ported";
if (table.get(oty) == .tagged_union) {
// `{ tag@0, payload@tag_size }` — read the tag word from the
// value's address. A `backing_type` union lays the tag out
// differently (it's a field of the backing struct), so bail
// rather than read the wrong bytes.
const tu = table.get(oty).tagged_union;
if (tu.backing_type != null) {
self.detail = "comptime VM: enum_tag on a backing_type tagged union not yet ported (layout differs)";
return error.Unsupported;
}
return .{ .value = try self.readField(table, v, tu.tag_type) };
}
self.detail = "comptime VM: enum_tag on an unexpected operand type";
return error.Unsupported;
},
// Extract a tagged union's active payload — the bytes at `tag_size`,
// read as the variant's payload type. Mirrors the `enum_init` write
// layout (`{ tag@0, [N x i8] payload@tag_size }`). The match-arm
// capture binding (`case .v: (x)`) uses this.
.enum_payload => |fa| {
const oty = (try self.refTy(ref_types, fa.base));
const base = frame.get(fa.base.index());
const table = try self.requireTable();
if (oty.isBuiltin() or table.get(oty) != .tagged_union) {
self.detail = "comptime VM: enum_payload on a non-tagged-union operand";
return error.Unsupported;
}
const tu = table.get(oty).tagged_union;
if (tu.backing_type != null) {
self.detail = "comptime VM: enum_payload on a backing_type tagged union not yet ported (layout differs)";
return error.Unsupported;
}
if (fa.field_index >= tu.fields.len)
return self.failMsg("comptime VM: enum_payload variant index out of range");
const payload_ty = tu.fields[fa.field_index].ty;
const tag_size: Addr = @intCast(table.typeSizeBytes(tu.tag_type));
return .{ .value = try self.readField(table, base + tag_size, payload_ty) };
},
// `is_comptime()` — always true on the comptime VM (folds to false in
// compiled code). Mirrors the legacy interp's `.is_comptime => true`.
@@ -1786,7 +1821,7 @@ pub const Vm = struct {
const names = self.gpa.alloc(types.StringId, members.items.len) catch return self.failMsg("comptime register_type: out of memory");
for (members.items, 0..) |m, i| {
if (m.ty != .void) return self.failMsg("comptime register_type: payload variant — use kind 3 (tagged_union)");
for (names[0..i]) |prev| if (prev == m.name) return self.failMsg("comptime register_type: duplicate enum variant");
for (names[0..i]) |prev| if (prev == m.name) return self.failFmt("comptime register_type: duplicate variant name '{s}'", .{tbl.getString(m.name)});
names[i] = m.name;
}
tbl.replaceKeyedInfo(handle, .{ .@"enum" = .{ .name = ident.name, .variants = names, .nominal_id = ident.nominal_id } });
@@ -1794,7 +1829,7 @@ pub const Vm = struct {
1, 3 => { // struct / tagged_union — `{ name, ty }` fields (dup names rejected)
const flds = self.gpa.alloc(types.TypeInfo.StructInfo.Field, members.items.len) catch return self.failMsg("comptime register_type: out of memory");
for (members.items, 0..) |m, i| {
for (flds[0..i]) |prev| if (prev.name == m.name) return self.failMsg("comptime register_type: duplicate member name");
for (flds[0..i]) |prev| if (prev.name == m.name) return self.failFmt("comptime register_type: duplicate member name '{s}'", .{tbl.getString(m.name)});
flds[i] = .{ .name = m.name, .ty = m.ty };
}
const full: types.TypeInfo = if (kind == 1)
@@ -2212,7 +2247,9 @@ pub const Vm = struct {
.pointer, .many_pointer, .function => .word,
.@"enum" => .word, // payloadless enum: i64 (or its backing) — a word
.error_set => .word, // the error channel is a u32 tag id — a word
.@"struct", .array, .tuple, .slice => .aggregate,
// A tagged union is a `{ tag@0, [N x i8] payload@tag_size }` value held
// by-address (like a struct) — same as the `enum_init` write path.
.@"struct", .array, .tuple, .slice, .tagged_union => .aggregate,
// `?T`: a pointer child is null-as-0 (word); else `{T, i1}` by-address.
.optional => |o| if (optChildIsPtr(table, o.child)) .word else .aggregate,
else => .unsupported,