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

@@ -1677,33 +1677,14 @@ pub fn tryLowerReflectionCall(self: *Lowering, name: []const u8, c: *const ast.C
// classification covers all 7; it runs before dispatch.
if (self.reflectionTypeArgGuard(name, c)) |sentinel| return sentinel;
// `declare(name)` is now an ordinary sx function (`modules/std/meta.sx`)
// written over the `abi(.compiler)` primitive `declare_type` — no longer
// intercepted here. (`preregisterForwardTypes` still scans for the literal
// `declare("Name")` spelling so a `*Name` self-reference forward-registers
// before the body lowers; the sx `declare` calls `declare_type`, which
// returns that same forward slot.)
if (std.mem.eql(u8, name, "define")) {
// Comptime type-construction primitive: complete a declare()'d slot
// from a TypeInfo value. `define(handle, info)`.
if (c.args.len != 2) {
if (self.diagnostics) |d| d.addFmt(.err, c.callee.span, "define(handle, info) takes exactly two arguments", .{});
return Ref.none;
}
const handle_ref = self.lowerExpr(c.args[0]);
// Lower the info arg with `TypeInfo` as the target type so its `.enum(…)`
// enum-literal infers correctly (we intercept the call, bypassing the
// normal param-type-threading the regular call path does).
const saved_tt = self.target_type;
if (self.module.types.findByName(self.module.types.internString("TypeInfo"))) |ti|
self.target_type = ti;
const info_ref = self.lowerExpr(c.args[1]);
self.target_type = saved_tt;
const args_owned = self.alloc.dupe(Ref, &.{ handle_ref, info_ref }) catch return Ref.none;
// define returns the (now-completed) handle as a `Type` value, so the
// one-shot constructor form chains: `T :: define(declare(), info)`.
return self.builder.callBuiltin(.define, args_owned, .type_value);
}
// `declare(name)` and `define(handle, info)` are now ordinary sx functions
// (`modules/std/meta.sx`) written over the `abi(.compiler)` primitives
// (`declare_type` / `register_type`) — no longer intercepted here.
// (`preregisterForwardTypes` still scans for the literal `declare("Name")`
// spelling so a `*Name` self-reference forward-registers before the body
// lowers; the sx `declare` calls `declare_type`, which returns that slot.
// The `.enum(…)` arg to `define` now infers `TypeInfo` from the sx fn's
// declared param type via the ordinary call path's target-type threading.)
if (std.mem.eql(u8, name, "type_info")) {
// Comptime reflection-into-data: reflect a type INTO a `TypeInfo`
// value (the inverse of `define`'s decode). Resolve `$T` at lower