green(reify): implement reify(.enum) — mint a flat enum from TypeInfo

REIFY Phase 0.2 (Phase 0 complete). Lowering.reifyType (lower/nominal.zig)
reads the flat-enum TypeInfo literal off the AST, synthesizes an
ast.EnumDecl, and feeds it through the SAME type_bridge.buildEnumInfo
path source enums use — so the minted type is byte-identical to a
hand-written `enum { value: i64; closed; }` and flows through enum
codegen (layout / construct / match) UNMODIFIED (Contract 2).

Wired at the `E :: reify(...)` const-decl hook in lower/decl.zig
(replacing the Phase-0.0 loud bail). Unsupported argument shapes bail
loudly via reifyBail — never a silent default. The generic.zig inline
reify path now reports it's only supported in a `::` binding (Phase 0).

examples/0614 green: reify a {value: i64, closed} enum, construct
.value(3) and .closed, match both -> "value 3" / "closed". Full suite
green (670 examples, 447 unit).
This commit is contained in:
agra
2026-06-16 18:32:05 +03:00
parent b25a2f60d6
commit 353109206b
10 changed files with 155 additions and 30 deletions

View File

@@ -1703,6 +1703,7 @@ pub const Lowering = struct {
pub const registerErrorSetDecl = lower_nominal.registerErrorSetDecl;
pub const registerStructDecl = lower_nominal.registerStructDecl;
pub const registerEnumDecl = lower_nominal.registerEnumDecl;
pub const reifyType = lower_nominal.reifyType;
pub const registerUnionDecl = lower_nominal.registerUnionDecl;
pub const qualifyAnonType = lower_nominal.qualifyAnonType;
pub const nominalIdOf = lower_nominal.nominalIdOf;

View File

@@ -651,15 +651,15 @@ pub fn scanDecls(self: *Lowering, decls: []const *const Node) void {
.field_access => |fa| fa.field,
else => "",
};
// `E :: reify(...)` — mint a nominal type from a `TypeInfo`
// and register `E` as an alias to it. The interpreter-side
// construction lands in Phase 0.2; until then bail LOUDLY
// and poison `E` to `.unresolved` (so downstream `E.value`
// gets a clean follow-on, not a silent default type).
// `E :: reify(...)` — mint a NEW nominal type from a
// `TypeInfo` literal and register `E` as an alias to it.
// `reifyType` builds the type (or diagnoses + returns null);
// either way `E` is bound (to the minted type, or poisoned
// to `.unresolved` so downstream `E.value` gets a clean
// follow-on rather than a silent default type).
if (std.mem.eql(u8, callee_name, "reify")) {
if (self.diagnostics) |d|
d.addFmt(.err, cd.value.span, "reify is not yet implemented (REIFY Phase 0.2)", .{});
self.putTypeAlias(self.current_source_file, cd.name, .unresolved);
const tid = self.reifyType(cd.name, call_data) orelse TypeId.unresolved;
self.putTypeAlias(self.current_source_file, cd.name, tid);
continue;
}
// A namespaced callee (`ns.Box(..)`) is an explicit qualified

View File

@@ -1219,15 +1219,14 @@ pub fn resolveTypeCallWithBindings(self: *Lowering, cl: *const ast.Call) TypeId
.field_access => |fa| fa.field,
else => return .unresolved,
};
// Comptime type-construction builtins (REIFY). `reify`/`field_type`
// appear in type position (`E :: reify(...)`, `field_type(T, i)` as a
// type arg). Until the interpreter-side construction lands (Phase 0.2 /
// Phase 2), bail LOUDLY rather than fall through to the misleading
// "unknown type 'reify'" diagnostic below — a silent default here would
// poison every downstream use of the type.
// Comptime type-construction builtins (REIFY). `reify` is minted in a
// `::` type-binding position by `decl.zig` (`E :: reify(...)`); reaching it
// HERE means an inline type position (`x : reify(...)`, a nested type arg),
// which Phase 0 does not support — bail LOUDLY rather than fall through to
// the misleading "unknown type 'reify'" diagnostic below.
if (std.mem.eql(u8, callee_name, "reify")) {
if (self.diagnostics) |d|
d.addFmt(.err, cl.callee.span, "reify is not yet implemented (REIFY Phase 0.2)", .{});
d.addFmt(.err, cl.callee.span, "reify is only supported in a `::` type binding (e.g. `E :: reify(...)`) in Phase 0", .{});
return .unresolved;
}
if (std.mem.eql(u8, callee_name, "field_type")) {

View File

@@ -734,6 +734,100 @@ pub fn registerEnumDecl(self: *Lowering, ed: *const ast.EnumDecl) void {
_ = self.internNamedTypeDecl(decl_key, name_id, info, nominal_id);
}
/// REIFY Phase 0: mint a NEW nominal enum type from a `TypeInfo` literal passed
/// to `reify(...)`, registered under `type_name`. The argument shape this phase
/// supports is exactly the flat-enum literal:
///
/// reify(.enum(.{ variants = .[ EnumVariant.{ name = "value", payload = i64 },
/// EnumVariant.{ name = "closed", payload = void } ] }))
///
/// The variant data is read DIRECTLY off the literal AST (Phase 0 reify takes a
/// comptime-known literal; the general interp-evaluated path is a later phase),
/// then handed to the SAME `buildEnumInfo` path source enums use — so the
/// minted type is byte-identical to an equivalent hand-written `enum { value:
/// i64; closed; }` and flows through enum codegen (layout / construct / match)
/// unmodified (Contract 2). Returns the minted `TypeId`, or null after emitting
/// a diagnostic if the argument is not a shape this phase can build (never a
/// silent default — REJECTED PATTERNS).
pub fn reifyType(self: *Lowering, type_name: []const u8, reify_call: *const ast.Call) ?TypeId {
const span = reify_call.callee.span;
if (reify_call.args.len != 1) return reifyBail(self, span, "reify expects exactly one TypeInfo argument");
// arg = `.enum(EnumInfo)` — an enum-literal applied as a call.
const arg = reify_call.args[0];
if (arg.data != .call or arg.data.call.callee.data != .enum_literal)
return reifyBail(self, span, "reify Phase 0 supports only `.enum(...)` TypeInfo");
const variant_kind = arg.data.call.callee.data.enum_literal.name;
if (!std.mem.eql(u8, variant_kind, "enum"))
return reifyBail(self, span, "reify Phase 0 supports only the `.enum` TypeInfo variant");
if (arg.data.call.args.len != 1)
return reifyBail(self, span, "reify `.enum(...)` takes one EnumInfo payload");
// EnumInfo payload = `.{ variants = .[ ... ] }`.
const einfo = arg.data.call.args[0];
if (einfo.data != .struct_literal)
return reifyBail(self, span, "reify `.enum(...)` payload must be an EnumInfo struct literal");
const variants_node = fieldInitValue(&einfo.data.struct_literal, "variants") orelse
return reifyBail(self, span, "reify EnumInfo is missing the `variants` field");
if (variants_node.data != .array_literal)
return reifyBail(self, span, "reify `variants` must be an array literal of EnumVariant");
// Each element = `EnumVariant.{ name = "...", payload = T }`.
var names = std.ArrayList([]const u8).empty;
var payloads = std.ArrayList(?*Node).empty;
for (variants_node.data.array_literal.elements) |elem| {
if (elem.data != .struct_literal)
return reifyBail(self, span, "reify variant must be an EnumVariant struct literal");
const name_node = fieldInitValue(&elem.data.struct_literal, "name") orelse
return reifyBail(self, span, "reify EnumVariant is missing `name`");
if (name_node.data != .string_literal)
return reifyBail(self, span, "reify EnumVariant `name` must be a string literal");
const payload_node = fieldInitValue(&elem.data.struct_literal, "payload") orelse
return reifyBail(self, span, "reify EnumVariant is missing `payload`");
names.append(self.alloc, name_node.data.string_literal.raw) catch return null;
payloads.append(self.alloc, payload_node) catch return null;
}
if (names.items.len == 0)
return reifyBail(self, span, "reify enum has no variants");
// Hand the synthesized decl to the shared enum body-builder (`self` is the
// visibility-aware payload-type resolver, as in registerEnumDecl). A
// payload that resolves to `.void` becomes a tagless variant (`closed`),
// exactly as a source `enum { … ; closed; }` would.
const ed = ast.EnumDecl{
.name = type_name,
.variant_names = names.items,
.variant_types = payloads.items,
.is_flags = false,
.variant_values = &.{},
.backing_type = null,
.is_raw = false,
};
const table = &self.module.types;
const info = type_bridge.buildEnumInfo(&ed, table, self);
const name_id = table.internString(type_name);
const tid = table.findByName(name_id) orelse table.internNominal(info, 0);
table.updatePreservingKey(tid, info);
return tid;
}
/// Emit a reify diagnostic and return null — the single loud-failure exit for
/// `reifyType` (no silent default ever reaches the type table).
fn reifyBail(self: *Lowering, span: ?ast.Span, comptime msg: []const u8) ?TypeId {
if (self.diagnostics) |d| d.addFmt(.err, span, msg, .{});
return null;
}
/// The value node of a named field init in a struct literal, or null if absent.
fn fieldInitValue(lit: *const ast.StructLiteral, name: []const u8) ?*Node {
for (lit.field_inits) |fi| {
if (fi.name) |n| {
if (std.mem.eql(u8, n, name)) return fi.value;
}
}
return null;
}
/// Register a top-level UNION decl under a per-decl nominal identity (E6a) —
/// the union twin of `registerEnumDecl` / `registerStructDecl`.
pub fn registerUnionDecl(self: *Lowering, ud: *const ast.UnionDecl) void {