green(reify): type-fn bodies comptime-evaluated; reify fully removed from the compiler

Second slice of the re-architecture — the compiler now has ZERO type-
construction code beyond declare/define.

- instantiateTypeFunction: a type-fn body returning a computed Type (a call
  to a non-generic, bodied, Type-returning fn) is comptime-evaluated with the
  type bindings active, then renamed to the mangled instantiation name for
  identity (renameNominalType). Replaces the old reify-call pattern-matching.
- DELETED: reifyType (lower/nominal.zig), findReturnReifyCall (lower/generic.zig),
  and the stale inline-position reify gate in resolveTypeCallWithBindings.
- evalComptimeType (was evalComptimeTypeNamed): pure eval, no rename; the
  type-fn caller renames explicitly. renameReifiedType → renameNominalType.
- The TYPE NAME now travels in the data: EnumInfo gains `name`, and define()
  names the slot from it (the compiler derives no name from a binding LHS).
  examples/0614/0615 carry `name = "..."`; RecvResult/TryResult set it too.
- field_type stays a reflection #builtin (reads a type); only construction
  moved out. All reify mentions stripped from compiler source.

examples 0614/0615/0617 run on the floor. Full suite green (673).
This commit is contained in:
agra
2026-06-16 21:03:16 +03:00
parent 442a70b8c9
commit 8ae655687a
11 changed files with 112 additions and 194 deletions

View File

@@ -734,100 +734,6 @@ 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 {