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

@@ -4,15 +4,25 @@ Companion to [PLAN-REIFY.md](PLAN-REIFY.md). Update after every step (one step a
time, per the cadence rule).
## Last completed step
**Phase 0.1 (xfail).** Added `examples/0614-comptime-reify-enum.sx` — reify a
flat enum (`value: i64`, `closed`) from a `TypeInfo` literal, construct
`E.value(3)` / `E.closed`, match both. Seeded an EMPTY
`expected/0614-…​.exit` marker → corpus is RED ("unparseable expected exit",
reify still bails). This is the deliberate xfail; **Phase 0.2 turns it green**
(per the no-commit-both-adds-and-passes cadence). `zig build test` is RED on
this commit by design (1 example fails); the very next commit makes it pass.
**Phase 0.2 (green) — Phase 0 COMPLETE.** Implemented `reify(.enum(...))`:
`Lowering.reifyType` (in `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 → the minted type is
byte-identical to a hand-written `enum { value: i64; closed; }` and flows
through enum codegen unmodified (**Contract 2 confirmed** — a source enum
exhibits the exact same construct/match behavior, verified by probe). Wired at
the `E :: reify(...)` const-decl hook in `lower/decl.zig`. `examples/0614`
green (`value 3` / `closed`, exit 0); snapshots captured; full suite green
(670 examples, 447 unit). Unsupported reify shapes bail loudly via
`reifyBail` (never a silent default).
### (prior) Phase 0.0 (lock). Added the comptime type-metaprogramming surface as the
Also landed (user-directed, separate commit `feat(parser):`): reserved keyword
as a member name after `.` (`.enum`, `case .enum:`, `x.enum`) — so the reify
example reads `reify(.enum(...))` without a backtick. readme updated.
### Two earlier Phase-0 commits
- **0.1 (xfail).** Added `examples/0614` + empty `.exit` marker → RED.
- **0.0 (lock).** Added the comptime type-metaprogramming surface as the
on-demand module `library/modules/std/meta.sx` (NOT the prelude — see decision
below): `EnumVariant`/`EnumInfo`/`TypeInfo` data types + bodyless `#builtin`
decls `reify` / `type_info` / `field_type`. Each builtin bails LOUDLY when
@@ -42,16 +52,32 @@ on-demand import keeps the prelude clean; reify users `#import
"modules/std/meta.sx"`. (User-directed.)
## Next step
**Phase 0.1 (xfail):** add `examples/06xx-comptime-reify-enum.sx` — `reify(.`enum(
.{ variants = .[ .{name="value",payload=i64}, .{name="closed",payload=void} ] }))`,
construct `.value(3)`, match it. RED (reify unimplemented → the loud bail above).
Seed `examples/expected/<name>.exit`. Then 0.2 (green: implement `reify(.`enum)`
in the interpreter / `decl.zig` reify hook).
**Phase 1 (type-fn → reify identity).** `R :: ($T) -> Type { reify(...) }`; assert
`R(i64)` from two sites is ONE type (assignable / matchable across sites). xfail →
green by registering a reify-returning type-fn's result under the instantiation
mangled name (mirror `generic.zig:1663-1689`). NOTE: Phase 0's `reifyType` is hooked
only at the `E :: reify(...)` const-decl site (`decl.zig`) and reads a LITERAL
`TypeInfo` off the AST; Phase 1 must route a reify call returned from a type-fn body
(and likely generalize the literal-AST reader, or evaluate via the interpreter, for
non-literal `TypeInfo`).
OPEN DESIGN Q (raised by user, = Phase 4): self-referencing enum via `*Self`/`[]Self`
payloads. Current `reifyType` resolves each payload eagerly through `buildEnumInfo`,
so a `*Self` payload has no type to point at yet — Phase 4 needs the
reserve-placeholder→complete path (`nominal.zig` reserve + `types.zig:442`): intern
the enum's nominal slot FIRST, expose it as `Self`, then resolve payloads. By-value
recursion stays rejected (infinite size).
## Known issues
None yet.
## Log
- **0.2 (green) — Phase 0 done.** `reifyType` mints a flat enum from a literal
`TypeInfo` via the shared `buildEnumInfo` path; `0614` green; Contract 2 confirmed
(reify'd enum == source enum, same construct/match). Payload-less variant idiom in
the example is `c : E = .closed;` (enum-literal target), same as a source enum.
- **parser (user-directed).** Keyword as member name after `.` — see `feat(parser)`.
- **0.1 (xfail).** `0614` + empty `.exit` marker → RED.
- **0.0 (lock).** Meta surface in `modules/std/meta.sx` (data types + 3 bodyless
`#builtin` decls), loud bails at all three reach points, `src/parser.test.zig`
parse-lock. Two user-directed refinements folded in: variant uses `` `enum ``

View File

@@ -106,7 +106,8 @@ Examples: `06xx` (comptime, deterministic), `11xx` (diagnostics for loud failure
## Status
- [ ] Phase 0 — `reify` flat enum
- [x] Phase 0 — `reify` flat enum (`reify(.enum(...))` mints a flat enum via the
shared `buildEnumInfo` path; `examples/0614` green; Contract 2 confirmed)
- [ ] Phase 1 — type-fn identity
- [ ] Phase 2 — `type_info` + `field_type`
- [ ] Phase 3 — `make_enum` + `RecvResult`/`TryResult`