0270. Recursive types through vec and map
- Status: Accepted
- Date: 2026-11-22
- Plan: M20.1
- Review: Language semantics; amends 0020, 0022, 0040
Context
E0410 rejects every type cycle, including
struct Node { children: vec<Node> } and
JSON-like
enum Value { Array(vec<Value>), Object(map<str, Value>), … }.
AI apps need those trees. Direct self-containment is still
unsound: a by-value field would make layout size infinite.
vec and map are already fixed-size
handles into arena storage (trt_vec,
const trt_map_hdr *).
Decision
Allow a struct/enum cycle only when every cycle edge goes
through a vec or map type argument
(including option<vec<T>> /
option<map<…>>: stop at the container, do
not treat option/result alone as
indirection). Newtypes stay erased, so they still reject any
self-cycle even through vec/map. Keep
rejecting direct and indirect by-value cycles (record fields, enum
payloads, option<T> / result
without a container break) with E0410; help text suggests
vec/map or an i64 id.
C layouts stay finite: parents store handles, not inline
children. Lowering reserves nominal layout IDs before expanding
fields so container element types can forward-reference. IR verify
requires definition-before-use only for by-value record/union
embedding; container element and map key/value refs may point at
any valid layout. Equality, promote, and relocate helpers may be
mutually recursive; emit prototypes first.
LayoutSizes treats vec/map as fixed; record/union
sizes follow the by-value DAG. Reflect/Json/Show use the same
inductive break so derive can name recursive
companions; a std JSON Value package is a follow-up,
not part of this spike.
Interpreter: no interp* edits in this spike.
Finite trees through existing
Value::{Record,Union,Seq,Map} should run on the
shared IR once lowering accepts them; document any parity gaps and
fix later without regressing current programs.
Alternatives considered
- Keep tables and typed IDs only — blocks JSON Value and trees agents need.
- Allow
option/resultas breaks — payloads are still by-value; layouts stay infinite. Only containers (or option-of-containers) break size. - Add an explicit
box/reftype — new kernel concept; vec/map already heap-allocate.
Consequences
Amends the “no recursive layouts” design-table line: by-value
recursion stays forbidden; inductive recursion through vec/map is
allowed. Teach-core and E0410 copy mention containers. Follow-up:
std JSON Value + codec derive goldens;
interpreter-focused parity if a gap appears.