0081. Reflected types and std.reflect
- Status: Accepted
- Date: 2026-11-01
- Plan: M9.5, Step 5.4
- Review: Making Tenon plugin-first, Data, schemas and wire formats
- Builds on: ADR-0078
Context
Generators need a stable, inspectable description of a derive target before generated code exists and before host type checking. Rust-side values would require a separate marshalling API; recursive values would also make schema traversal awkward. Reflection must expose exactly the information a generator can observe, deterministically, so generator keys can include the rendered reflection literal.
Decision
std.reflect is an embedded module whose source is
included with include_str! and identified as
std@<version>/reflect.hn. It is selected only
for the exact import path std.reflect. A dependency
key named std is forbidden (E0932), and a local
std/reflect.hn is forbidden even when empty (E0934),
preventing local shadowing of the embedded module.
The public schema is a flat set of i64-indexed
tables. Type.target and Ref.Named index
Type.named. Field types, enum payload entries,
newtype representations, and container arguments index
Type.refs, never nested Type values.
References represent scalar types, Vec,
Option, Map, Named, or
Unsupported(type_spelling). Named rows carry the
local name and rewritten module identity, visibility,
same-generator derived flag, signature hash, and
struct/enum/newtype shape.
The builder consumes the post-Rewrite AST, after rewriting the
target's own module but before host type checking. It needs no
checked Types, since generated declarations do not
yet exist. The named table contains the target's named-type
closure in deterministic declaration-order postorder, with the
target last. Generic user fields and capability types are
represented as Unsupported with their type spelling
rather than causing builder failure.
For each named type, sig_hash is derived from its
canonical signature. The generator cache key also includes the
expression literal rendered from the complete reflection value,
including representations and derived flags; thus
every reflected observation participates in freshness. The caller
supplies the set of qualified type names for which this same
generator is derived anywhere in the loaded graph. No other
generator's derives affect that set.
The Rust builder renders the reflected value as a Harness expression literal in the synthetic entry source. This keeps interpreter execution on the normal language path and avoids Rust-to-interpreter-value marshalling.
Alternatives considered
- Recursive reflected values: complicate traversal and expose recursive value structure; indexed flat tables make references explicit and stable.
- Build from checked types: impossible before generated output exists and host checking has run.
- Marshal directly into interpreter values: adds a second construction path and bypasses ordinary source parsing.
- Allow local or dependency-provided
std.reflect: makes the reserved built-in import ambiguous or shadowable.
Consequences
Reflection is deterministic and bounded by the loaded type
closure. Generators must follow indices to inspect nested
structure and explicitly handle Unsupported. The
exact embedded source identity makes traps machine-independent.
Any schema or literal-rendering change that affects
generator-visible data must change the generator ABI/key inputs as
specified by ADR-0078.