docs(metatype): type_info($T) enum reflection done; update plan/checkpoint

This commit is contained in:
agra
2026-06-16 22:53:52 +03:00
parent 1ffda415c2
commit 52b0dc2a9a
2 changed files with 47 additions and 10 deletions

View File

@@ -4,6 +4,31 @@ Companion to [PLAN-METATYPE.md](PLAN-METATYPE.md). Update after every step (one
step at a time, per the cadence rule).
## Last completed step
**`type_info($T)` reflection — enum round-trip.** Reflect a type INTO a `TypeInfo`
value (the inverse of `define`'s decode), so `define(declare(n), type_info(T))`
mints a byte-identical copy with NO literal variant list.
- `inst.zig`: new `BuiltinId.type_info` (comptime-only, alongside `declare`/`define`).
- `lower/call.zig:tryLowerReflectionCall`: the old "not yet implemented" bail is
gone. Resolve `$T` at lower time, reject a non-`enum`/non-`tagged_union` arg
loudly (good span: `"type_info: 'X' is not an enum …"`), else emit
`callBuiltin(.type_info, [const_type], TypeInfo)`.
- `interp.zig:reflectTypeInfo`: builds the exact nested-aggregate Value
`defineEnum` decodes — variant `{name, payload}`, slice `{data, len}`, EnumInfo
`{variants}`, TypeInfo `{tag0, EnumInfo}`. A `tagged_union` reflects each
`field.ty` (tagless variants already carry `void`); a payloadless `` `enum ``
reflects `void` per variant. Round-trips both source enums AND constructed
(declare/define) enums.
- emit unchanged — `type_info` is always comptime-evaluated; the existing
comptime-only `else` arm in `emitCallBuiltin` (shared with declare/define)
never fires.
- Scope: **enum-only** (the symmetric inverse of `define`'s current capability).
Struct/tuple `TypeInfo` widening is a separate later step.
`examples/0619` locks it (source enum `circle:f64 / rect:i64 / empty` reflected →
reconstructed → constructs + matches). Full suite green (676 examples + units).
## Prior step
**Self-reference — recursive enums via `declare("Name")` + `*Name`.** The
`declare`/`define` floor now supports self-referential types.
@@ -39,7 +64,9 @@ traversal)**; `field_type` reflection `0616`. Full suite green (674 examples).
(reflection). No constructor-name knowledge anywhere in the compiler — every
named constructor is sx. `declare(name)` carries the type name (compile-time
string) for forward-type registration.
- `type_info` still bails loudly (`call.zig:tryLowerReflectionCall`) — pending.
- `type_info($T)` reflects an `enum`/`tagged_union` INTO a `TypeInfo` value
(`call.zig` emits `callBuiltin(.type_info)`; `interp.zig:reflectTypeInfo` builds
the Value). Enum-only; struct/tuple widening pending. `examples/0619` round-trip.
## Decision (kept)
**Meta lives in `modules/std/meta.sx`, not the prelude.** Declaring its data types
@@ -51,10 +78,10 @@ Pick any (independent):
- **`make_enum(variants: []EnumVariant)`** sx helper over a COMPUTED (non-literal)
variant list — exercises the interpreter decoding a value-arg slice in `define`
(vs. the literal `.[ … ]` the current examples use).
- **`type_info($T) -> TypeInfo`** — reflect a type INTO a value (inverse of
`define`'s decode); widen `TypeInfo` past `` `enum `` + construct a
`[]EnumVariant` value in the interp. Bails loudly today in
`call.zig:tryLowerReflectionCall`. Its own focused effort.
- **Widen `type_info` / `TypeInfo` past `` `enum ``** — struct/tuple variants:
add `` .`struct ``/`` .`tuple `` to the `TypeInfo` enum in `meta.sx`, teach
`reflectTypeInfo` to build them, and teach `defineEnum` (→ a `defineType`) to
decode them. Round-trips a struct through `define` once it lands.
- **Validation + loud diagnostics** (remaining) — duplicate variant names, a
`declare()` never `define()`d (hard error), use-before-define. (By-value
self-reference already rejected — issue 0139.)
@@ -65,6 +92,13 @@ Pass 1g emits a loud "infinitely sized" diagnostic + breaks the cycle; covers
source + comptime types; `examples/1178` locks it.)
## Log
- **`type_info($T)` reflection done (enum round-trip).** New `BuiltinId.type_info`;
`lower/call.zig` resolves `$T`, rejects non-enum loudly, emits the builtin;
`interp.zig:reflectTypeInfo` constructs the exact nested-aggregate Value
`defineEnum` decodes (variant `{name,payload}` / slice `{data,len}` / EnumInfo /
TypeInfo `.enum`). `tagged_union` reflects `field.ty`; payloadless `` `enum ``
reflects `void`. Round-trips source AND constructed enums. Enum-only;
struct/tuple widening deferred. `examples/0619` locks it. Suite green (676).
- **By-value self-reference rejected (issue 0139, F5 partial).** New
`checkInfiniteSize` pass (Pass 1g) detects by-VALUE containment cycles (source +
comptime types, direct + mutual), emits a loud "infinitely sized" diagnostic,

View File

@@ -10,7 +10,8 @@ Comptime type metaprogramming with the smallest possible compiler surface:
- **`define(handle, info) -> Type`** — fill a declared handle's body from a
`TypeInfo` *value*, and return the handle (so the one-shot form chains).
- **`type_info($T) -> TypeInfo`** — reflect a type INTO data (the inverse of
`define`'s decode). *Pending.*
`define`'s decode). *Done for enums* (`interp.zig:reflectTypeInfo`,
`examples/0619`); struct/tuple widening pending.
- **`field_type($T, i) -> Type`** — the i-th field / variant-payload / element
type of `$T`. *Done.*
@@ -109,10 +110,12 @@ while red. Examples: `06xx` (comptime), `11xx` (diagnostics).
- [ ] **`make_enum(variants: []EnumVariant)`** sx helper over a COMPUTED
(non-literal) variant list — exercises the interpreter decoding a value-arg
slice in `define`.
- [ ] **`type_info($T) -> TypeInfo`** — reflect a type INTO a value (inverse of
`define`'s decode): widen `TypeInfo` past `` `enum `` (struct/tuple) AND
construct a `[]EnumVariant`-style value in the interpreter. Bails loudly today
in `call.zig:tryLowerReflectionCall`. Round-trips through `define` once it lands.
- [x] **`type_info($T) -> TypeInfo`** (enum-only) — reflect an `enum`/`tagged_union`
INTO a value (inverse of `define`'s decode): `interp.zig:reflectTypeInfo`
constructs the `.enum(EnumInfo{ variants })` Value `defineEnum` decodes, so
`define(declare(n), type_info(T))` round-trips. `examples/0619`. Non-enum args
rejected loudly at lower time. **Still pending:** widen `TypeInfo` past
`` `enum `` (struct/tuple variants) + the matching `define` decode arms.
- [~] **Validation + loud diagnostics** — by-VALUE self-reference DONE
(`checkInfiniteSize` Pass 1g: loud "infinitely sized" diagnostic + cycle
break, covers source + comptime types; `examples/1178`, issue 0139 resolved).