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:
@@ -1677,10 +1677,10 @@ pub fn tryLowerReflectionCall(self: *Lowering, name: []const u8, c: *const ast.C
|
||||
if (self.reflectionTypeArgGuard(name, c)) |sentinel| return sentinel;
|
||||
|
||||
if (std.mem.eql(u8, name, "declare")) {
|
||||
// Comptime type-construction primitive (REIFY floor): mint an empty
|
||||
// nominal slot. Comptime-only — emitted as a builtin_call the interp
|
||||
// executes against its `mint` table; never reaches codegen (reify and
|
||||
// friends, which call it, are only ever comptime-evaluated).
|
||||
// Comptime type-construction primitive: mint an empty nominal slot.
|
||||
// Comptime-only — emitted as a builtin_call the interp executes against
|
||||
// its `mint` table; never reaches codegen (its sx callers are only ever
|
||||
// comptime-evaluated).
|
||||
if (c.args.len != 0) {
|
||||
if (self.diagnostics) |d| d.addFmt(.err, c.callee.span, "declare() takes no arguments", .{});
|
||||
return Ref.none;
|
||||
@@ -1688,8 +1688,8 @@ pub fn tryLowerReflectionCall(self: *Lowering, name: []const u8, c: *const ast.C
|
||||
return self.builder.callBuiltin(.declare, &.{}, .any);
|
||||
}
|
||||
if (std.mem.eql(u8, name, "define")) {
|
||||
// Comptime type-construction primitive (REIFY floor): complete a
|
||||
// declare()'d slot from a TypeInfo value. `define(handle, info)`.
|
||||
// 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;
|
||||
@@ -1700,13 +1700,13 @@ pub fn tryLowerReflectionCall(self: *Lowering, name: []const u8, c: *const ast.C
|
||||
return self.builder.callBuiltin(.define, args_owned, .void);
|
||||
}
|
||||
if (std.mem.eql(u8, name, "type_info")) {
|
||||
// Comptime reflection-into-data (REIFY). Until the interpreter-side
|
||||
// reflection lands (Phase 2), bail loudly rather than fall through to
|
||||
// the no-body `#builtin` const_decl path (which would mis-lower as a
|
||||
// zero-arg call). A silent fall-through would hand the caller a
|
||||
// garbage TypeInfo value.
|
||||
// Comptime reflection-into-data (reflect a type INTO a `TypeInfo`
|
||||
// value). Until the interpreter-side reflection lands, bail loudly
|
||||
// rather than fall through to the no-body `#builtin` const_decl path
|
||||
// (which would mis-lower as a zero-arg call). A silent fall-through
|
||||
// would hand the caller a garbage TypeInfo value.
|
||||
if (self.diagnostics) |d|
|
||||
d.addFmt(.err, c.callee.span, "type_info is not yet implemented (REIFY Phase 2)", .{});
|
||||
d.addFmt(.err, c.callee.span, "type_info is not yet implemented", .{});
|
||||
return Ref.none;
|
||||
}
|
||||
if (std.mem.eql(u8, name, "size_of")) {
|
||||
|
||||
@@ -384,15 +384,16 @@ pub fn lowerInsertExprValue(self: *Lowering, expr: *const Node) Ref {
|
||||
return last_val;
|
||||
}
|
||||
|
||||
/// Evaluate a Type-returning expression at compile time → its `TypeId`.
|
||||
/// The driver of the REIFY floor: `expr` (e.g. `reify(.enum(...))`, a type-fn
|
||||
/// call) is wrapped in a throwaway comptime fn and run through the interpreter
|
||||
/// with the type-MINT table enabled, so `declare`/`define` builtins reached
|
||||
/// inside it mutate the real type table. The result value is a `.type_tag`.
|
||||
/// When `name` is given, the minted (anonymous) type is renamed to it so
|
||||
/// `type_name` / diagnostics read the binding's name. Returns null (caller
|
||||
/// poisons) if evaluation didn't yield a Type.
|
||||
pub fn evalComptimeTypeNamed(self: *Lowering, expr: *const Node, name: ?[]const u8) ?TypeId {
|
||||
/// Evaluate a `Type`-returning expression at compile time → its `TypeId`.
|
||||
/// `expr` (a call to any bodied `-> Type` fn) is wrapped in a throwaway comptime
|
||||
/// fn and run through the interpreter with the type-mint table enabled, so the
|
||||
/// `declare`/`define` builtins reached inside it mutate the real type table. The
|
||||
/// result value is a `.type_tag`. A type minted via `define` is already named
|
||||
/// (the name travels in its `TypeInfo`); a caller needing a different identity
|
||||
/// name (the type-fn mangled-name path) renames afterwards via
|
||||
/// `renameNominalType`. Returns null (caller poisons) if evaluation didn't yield
|
||||
/// a Type.
|
||||
pub fn evalComptimeType(self: *Lowering, expr: *const Node) ?TypeId {
|
||||
const func_id = self.createComptimeFunction("__ctype", expr, .any);
|
||||
|
||||
var interp = interp_mod.Interpreter.init(self.module, self.alloc);
|
||||
@@ -401,15 +402,14 @@ pub fn evalComptimeTypeNamed(self: *Lowering, expr: *const Node, name: ?[]const
|
||||
interp.setMintTable(&self.module.types);
|
||||
|
||||
const result = interp.call(func_id, &.{}) catch return null;
|
||||
const tid = result.asTypeId() orelse return null;
|
||||
if (name) |nm| self.renameReifiedType(tid, nm);
|
||||
return tid;
|
||||
return result.asTypeId();
|
||||
}
|
||||
|
||||
/// Rename a freshly-minted (anonymous `__reified_N`) nominal type to its
|
||||
/// binding's name, re-keying `intern_map` so `findByName(name)` resolves it.
|
||||
/// A no-op for a non-nominal / already-named-as-requested type.
|
||||
pub fn renameReifiedType(self: *Lowering, tid: TypeId, name: []const u8) void {
|
||||
/// Rename a nominal type to a new name, re-keying `intern_map` so
|
||||
/// `findByName(name)` resolves it. Used by the type-fn instantiation path to
|
||||
/// give a comptime-minted type its mangled instantiation name (identity /
|
||||
/// Contract 1). A no-op for a non-nominal / already-named-as-requested type.
|
||||
pub fn renameNominalType(self: *Lowering, tid: TypeId, name: []const u8) void {
|
||||
const tbl = &self.module.types;
|
||||
const new_name_id = tbl.internString(name);
|
||||
var info = tbl.get(tid);
|
||||
|
||||
@@ -44,8 +44,8 @@ const isPackFn = Lowering.isPackFn;
|
||||
/// Anything starting with `Java_` is a JNI native method that Android's
|
||||
/// runtime resolves by name mangling — same rule.
|
||||
/// True when `fd` declares a `-> Type` return — the signal that a non-generic
|
||||
/// call to it (`E :: f(...)`) should be comptime-evaluated to mint a type (the
|
||||
/// REIFY floor). Matches a bare `Type` type-expr return only.
|
||||
/// call to it (`E :: f(...)`) should be comptime-evaluated to mint a type.
|
||||
/// Matches a bare `Type` type-expr return only.
|
||||
fn fnReturnsTypeValue(fd: *const ast.FnDecl) bool {
|
||||
const rt = fd.return_type orelse return false;
|
||||
return rt.data == .type_expr and std.mem.eql(u8, rt.data.type_expr.name, "Type");
|
||||
@@ -660,16 +660,18 @@ pub fn scanDecls(self: *Lowering, decls: []const *const Node) void {
|
||||
else => "",
|
||||
};
|
||||
// `E :: f(...)` where `f` is a NON-generic fn returning
|
||||
// `Type` (e.g. the sx `reify` / `make_enum`): comptime-
|
||||
// evaluate the call — `declare`/`define` reached inside it
|
||||
// mint the type — and bind `E` as an alias to the result.
|
||||
// The compiler has ZERO `reify` knowledge: any Type-returning
|
||||
// value-fn flows here. Generic type-fns (`$T`) are minted by
|
||||
// `Type` (a comptime type constructor): comptime-evaluate the
|
||||
// call — `declare`/`define` reached inside it mint the type —
|
||||
// and bind `E` as an alias to the result. No hardcoded
|
||||
// constructor names: any Type-returning value-fn flows here.
|
||||
// Generic type-fns (`$T`) are minted by
|
||||
// `instantiateTypeFunction` below. Poison on failure so
|
||||
// `E.x` gets a clean follow-on, never a silent default.
|
||||
if (self.program_index.fn_ast_map.get(callee_name)) |fd| {
|
||||
if (fd.type_params.len == 0 and fnReturnsTypeValue(fd)) {
|
||||
const tid = self.evalComptimeTypeNamed(cd.value, cd.name) orelse TypeId.unresolved;
|
||||
// The minted type's NAME comes from its `TypeInfo`
|
||||
// (via `define`), not the binding LHS — no rename.
|
||||
const tid = self.evalComptimeType(cd.value) orelse TypeId.unresolved;
|
||||
self.putTypeAlias(self.current_source_file, cd.name, tid);
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -1252,20 +1252,11 @@ pub fn resolveTypeCallWithBindings(self: *Lowering, cl: *const ast.Call) TypeId
|
||||
.field_access => |fa| fa.field,
|
||||
else => return .unresolved,
|
||||
};
|
||||
// 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 only supported in a `::` type binding (e.g. `E :: reify(...)`) in Phase 0", .{});
|
||||
return .unresolved;
|
||||
}
|
||||
// field_type($T, i) -> Type — comptime reflection (read a type's i-th
|
||||
// field / variant-payload / element type). A genuine type-table op, kept as
|
||||
// a compiler builtin (like type_name); folds at lower time so it composes
|
||||
// inside type_eq / type_name / any type-arg slot.
|
||||
if (std.mem.eql(u8, callee_name, "field_type")) {
|
||||
// field_type($T, i) -> Type — the i-th field / variant-payload /
|
||||
// element type of `T`. Folds at lower time (it's a `$T: Type` builtin),
|
||||
// so it composes inside `type_eq` / `type_name` / any type-arg slot.
|
||||
if (cl.args.len != 2) {
|
||||
if (self.diagnostics) |d|
|
||||
d.addFmt(.err, cl.callee.span, "field_type takes a type and an index: field_type($T, i)", .{});
|
||||
@@ -1758,17 +1749,22 @@ pub fn instantiateTypeFunction(self: *Lowering, alias_name: []const u8, template
|
||||
return self.instantiateTypeUnion(if (has_alias) alias_name else mangled_name, mangled_name, &enum_decl);
|
||||
}
|
||||
|
||||
// A type-fn body that RETURNS `reify(...)` — mint the enum under THIS
|
||||
// instantiation's name (mangled for inline use, the alias name for
|
||||
// `Foo :: Box(i64)`). The type-arg bindings are active here, so the reify
|
||||
// payloads resolve against the instantiation's args (`payload = T` → the
|
||||
// bound type). Registering under the mangled name lets the cache check at
|
||||
// the top of this fn return the SAME TypeId on a second instantiation —
|
||||
// so `Box(i64)` at two sites is ONE type (Contract 1). Must precede the
|
||||
// general case below, whose `resolveTypeWithBindings` would route the
|
||||
// reify call to the inline-position loud bail.
|
||||
if (findReturnReifyCall(fd.body)) |reify_call| {
|
||||
return self.reifyType(if (has_alias) alias_name else mangled_name, reify_call);
|
||||
// A type-fn body that returns a COMPUTED Type — a call to a non-generic,
|
||||
// bodied, Type-returning fn (a comptime type constructor). Comptime-evaluate
|
||||
// the return expression with the type bindings active (so a payload `= T`
|
||||
// resolves to the bound arg) and mint under THIS instantiation's name. The
|
||||
// rename to the mangled name lets the cache check at the top return the
|
||||
// SAME TypeId on a second instantiation — `Foo(i64)` at two sites is ONE
|
||||
// type (nominal identity). Must precede the general static case below, whose
|
||||
// `resolveTypeWithBindings` can't evaluate a Type-returning call.
|
||||
if (findReturnTypeExpr(fd.body)) |ret_node| {
|
||||
if (self.returnExprMintsType(ret_node)) {
|
||||
const tid = self.evalComptimeType(ret_node) orelse return .unresolved;
|
||||
// Re-key to the instantiation's mangled (or alias) name so the
|
||||
// cache check at the top dedups a second instantiation — Contract 1.
|
||||
self.renameNominalType(tid, if (has_alias) alias_name else mangled_name);
|
||||
return tid;
|
||||
}
|
||||
}
|
||||
|
||||
// General case: the body returns a TYPE EXPRESSION that is not an inline
|
||||
@@ -1801,17 +1797,21 @@ pub fn findReturnTypeExpr(body: *const Node) ?*const Node {
|
||||
return body;
|
||||
}
|
||||
|
||||
/// The `reify(...)` call a type-fn body returns (block `return reify(...)` or
|
||||
/// arrow `=> reify(...)`), or null if the body's return is not a bare `reify`
|
||||
/// call. Used to route a reify-returning type-fn through `reifyType` under the
|
||||
/// instantiation name (Phase 1 nominal identity).
|
||||
pub fn findReturnReifyCall(body: *const Node) ?*const ast.Call {
|
||||
const ret = findReturnTypeExpr(body) orelse return null;
|
||||
if (ret.data != .call) return null;
|
||||
/// True when a type-fn's return expression mints a type at comptime — a call to
|
||||
/// a NON-generic, bodied, `Type`-returning fn (a comptime type constructor).
|
||||
/// Such a body is comptime-evaluated (its `declare`/`define` mint the type)
|
||||
/// rather than statically resolved. Excludes generic / `#builtin` type
|
||||
/// constructors (`Vector(N,T)`, `Make($T)`), which the static path handles. No
|
||||
/// hardcoded constructor names — any qualifying Type-returning fn flows here.
|
||||
pub fn returnExprMintsType(self: *Lowering, ret: *const Node) bool {
|
||||
if (ret.data != .call) return false;
|
||||
const callee = ret.data.call.callee;
|
||||
if (callee.data != .identifier) return null;
|
||||
if (!std.mem.eql(u8, callee.data.identifier.name, "reify")) return null;
|
||||
return &ret.data.call;
|
||||
if (callee.data != .identifier) return false;
|
||||
const fd = self.program_index.fn_ast_map.get(callee.data.identifier.name) orelse return false;
|
||||
if (fd.type_params.len != 0) return false; // generic constructors stay static
|
||||
if (fd.body.data == .block and fd.body.data.block.stmts.len == 0) return false; // bodyless #builtin
|
||||
const rt = fd.return_type orelse return false;
|
||||
return rt.data == .type_expr and std.mem.eql(u8, rt.data.type_expr.name, "Type");
|
||||
}
|
||||
|
||||
/// Instantiate a tagged enum from a type function body.
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user