docs(metatype): make_enum done; note deferred free-form-construction gaps

This commit is contained in:
agra
2026-06-17 04:56:56 +03:00
parent 2250652ba5
commit 4e8075491d

View File

@@ -4,6 +4,19 @@ Companion to [PLAN-METATYPE.md](PLAN-METATYPE.md). Update after every step (one
step at a time, per the cadence rule).
## Last completed step
**`make_enum(name, variants: []EnumVariant) -> Type`** — the general enum
constructor over `declare`/`define`, minting a nominal enum from a variant list
passed as a VALUE (not a hardcoded literal). Pure sx in `meta.sx`, no compiler
machinery — the open-ended form the channel-result constructors are special cases
of. Because `variants` is an ordinary comptime value, a non-generic builder
assembles it in a local before minting; `examples/0620` (`build_level` fills a
local array → `make_enum` mints `Level`) exercises `define` decoding a value-arg
SLICE (`decodeVariantElements`' slice branch) vs. the inline `.[ … ]` array the
06140618 examples pass directly. No compiler change (locks existing capability).
Suite green (678). See "make_enum follow-ups" under Next step for the deferred
free-form-construction gaps (subslice/List at comptime, generic-type-fn locals).
## Prior 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.
@@ -28,7 +41,7 @@ mints a byte-identical copy with NO literal variant list.
`examples/0619` locks it (source enum `circle:f64 / rect:i64 / empty` reflected →
reconstructed → constructs + matches). Full suite green (676 examples + units).
## Prior step
## Earlier step
**Self-reference — recursive enums via `declare("Name")` + `*Name`.** The
`declare`/`define` floor now supports self-referential types.
@@ -59,7 +72,8 @@ traversal)**; `field_type` reflection `0616`. Full suite green (674 examples).
## Current state
- `modules/std/meta.sx`: `EnumVariant` / `EnumInfo{ name, variants }` / `TypeInfo`
data types; `declare` / `define` / `type_info` / `field_type` `#builtin`s;
`RecvResult($T)` / `TryResult($T)` sx constructors over `define(declare(), …)`.
`RecvResult($T)` / `TryResult($T)` + the general `make_enum(name, variants)` sx
constructors over `define(declare(), …)`.
- Compiler primitives only: `declare`/`define` (construction), `field_type`
(reflection). No constructor-name knowledge anywhere in the compiler — every
named constructor is sx. `declare(name)` carries the type name (compile-time
@@ -75,16 +89,28 @@ shifts every `.ir` snapshot. On-demand import keeps the prelude clean.
## Next step
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`.
**UNBLOCKED** (issue 0140 fixed — decode failures now surface a clean diagnostic
at the construction site instead of a panic/cascade). Remaining work: a computed
(pointer-backed) `[]EnumVariant` slice from a local var / `List` still does not
DECODE in `defineEnum`/`decodeVariantElements` — that decoder needs to follow a
`slot_ptr`/`heap_ptr` data field (today it only reads an inline `.aggregate`).
Probes: `.sx-tmp/probe_me2.sx` (sliced local array → bail "TypeError"),
`.sx-tmp/probe_makeenum.sx` (List → bail "struct_get: base has no fields"); the
inline-literal-param form already works (`.sx-tmp/probe_me3.sx`).
- **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 at end of comptime), use-before-define.
### make_enum follow-ups (deferred capability gaps — NOT crashes; clean diagnostics)
`make_enum` itself is DONE (see Last completed step). Two adjacent capabilities
would let the variant list be built more freely; both currently error cleanly
(post-0140) rather than crash, so they're enhancements, not blockers:
- **Comptime slice over a non-string aggregate.** `arr[..]` over a `[]EnumVariant`
array bails — the interp's `.subslice` op (`interp.zig:~1235`) only supports
STRING bases. Needed for `vs[..]`-style construction. Probe `.sx-tmp/probe_me2.sx`.
- **Comptime `List` growth.** Building variants via `List(EnumVariant).append` at
comptime bails ("struct_get: base has no fields") — the allocator/List protocol
path isn't fully interp-evaluable. Probe `.sx-tmp/probe_makeenum.sx`.
- **Generic type-fn body locals.** A *generic* `($T) -> Type` comptime-evals only
its return EXPRESSION (`generic.zig:1760`, `findReturnTypeExpr`), so a local
before the return is unresolved. Workaround: build the list inline in the return,
or use a non-generic `() -> Type` builder (whose whole body is evaluated — this
is what `examples/0620` uses). Probe `.sx-tmp/probe_me5.sx`.
- **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
@@ -105,6 +131,13 @@ Pick any (independent):
`examples/1178` locks it).
## Log
- **`make_enum` done.** General enum constructor `make_enum(name, variants:
[]EnumVariant) -> Type` in `meta.sx` (pure sx over declare/define). A non-generic
builder assembles the variant list in a local, then mints from it —
`examples/0620` exercises `define`'s value-arg SLICE decode. No compiler change.
Suite green (678). Deferred free-form gaps (subslice/List at comptime,
generic-type-fn locals) noted under Next step — all clean diagnostics now, not
crashes (post-0140), so enhancements rather than blockers.
- **issue 0140 fixed.** A comptime type-construction bail (`declare`/`define`/
reflection) used to panic at LLVM emission ("unresolved type reached LLVM
emission") or hide behind a cascade — `evalComptimeType` swallowed the interp's