On this page

0078. Generators: signature, trigger, explicit gen, staged edges

Context

ADR-0006 requires a fixed generator trigger and readable output without grammar plugins. ADR-0064 modules cannot load generated imports back into their host without cycles; ADR-0069 protects newtype representations by file. ADR-0075 body hashes are shallow and newtype signatures omit representations, so neither alone is a complete generator key.

Decision

  1. Signature (D1): a generator is an ordinary pub fn X(t: std.reflect.Type) -> result<str, str>. It returns source text, validated by the existing parser and formatter, rather than a second public AST API.
  2. Reflection (D2): embed std.reflect via include_str!, matching that exact import only. Reserve dependency key std (E0932); reject local std/reflect.hn (E0934). Build reflection from the post-Rewrite AST before host type checking. Type { target, refs, named } is a flat indexed table: primitive/container/Named/Unsupported refs; named structs, enums and newtypes carry names, module, visibility, same-generator derived, signature hash and shape. Deterministic post-order includes the target's type closure; render an expression literal for a synthetic entry, avoiding Rust-value marshalling.
  3. Trigger (D3): top-level derive Ident for Ident ends like a newtype. derive is contextual only before Ident for; malformed or public derives are E0117. Resolve the generator through normal file import bindings, by provider ModuleId and item. The target must be a non-generic struct, enum or newtype declared in the same file (E0962); foreign files cannot grant themselves newtype authority.
  4. Companions (D3): write <package>/gen/<module-or-root-stem-path>/<T>.<X>.hn. A companion shares the host ModuleId but has its own file index, sees host bindings and its own imports/declarations, and contributes declarations visible to the host. Identical imports are deduplicated. Extend ADR-0064/0069 with Loaded.file_owners for host newtype access only; package ceilings remain owned by the deriving file's package.
  5. Stages (D4): Rewrite records uses. An edge whose selected names are used only as derive generators is generator-only. Keep those modules loaded for hashing but omit exclusively stage-reachable modules from runtime lowering. A generator closure depending on its deriving module is E0963. Generator packages may derive with fresh checked-in companions.
  6. Sandbox (D5): use a pure interpreter entry returning a value on a worker thread, no capabilities, I/O or clock. Runtime platform/print/exit guards reject hand-built forbidden IR as compiler-bug tool errors (exit 3). Fixed limits are 50,000,000 fuel, 256 MiB fresh heap growth (not views), and 16 MiB output; no manifest override. Heap exhaustion traps out of memory. Generator closures are exempt from capability-ceiling reporting (ADR-0074 is declared intent); security review remains M16.3.
  7. Explicit execution (D6): only harness gen <file.hn> [--check] executes generators or writes companions; check/build/run/interp/emission only verify freshness. Extend ADR-0072's explicit-write rule: no lock/network writes, dependencies verified but regenerated only at their own root. Load without companions, construct synthetic stage roots under package rules, execute in ModuleId/source order, validate/format/header output, and check the whole program in memory before atomic publication and header-proven orphan deletion. Failure leaves the tree untouched; --check writes nothing.
  8. Keys (D7): checked-in gen/ is the cache, with no second store. SHA-256 over domain harness-gen-key-1\n, GEN_ABI, toolchain version, generator identity, sorted canonical signature/body forms of its entire module closure (private/transitive helpers included), target signature hash and reflection literal covers every observed input, including newtype reprs and derived flags. Bump ABI for reflection/validation/format changes. A v1 magic header records generator, target, toolchain/ABI, key and formatted-output hashes. Distinguish stale (E0969), missing (E0968), edited/bad header (E0970), and orphan (E0971).
  9. Determinism/validation (D8–D9): canonical formatting, insertion-ordered maps and absent ambient access give byte-identical output across runs/cwds. Output must parse and contain neither derive nor main (E0967); imports resolve under the deriving package's rules, limited to its own package or std. Duplicate derives are E0972; E0960–E0966 cover binding/signature/target/cycle/trap/limit/returned-error failures; E0973–E0979 stay reserved.
  10. Tools (D10): fmt/rename walkers skip magic-header files; direct fmt and edit replace-fn refuse them. Outline lists derives and marks generated files. Rename refuses generated references with regeneration instructions. Generated declarations are ordinary host items for hashes/compatibility; strip derives before canonical forms and merging.

Owner defaults are adopted: refuse generated-reference renames (park rename --regen); same-file targets only; reject generic targets; JSON integers stay numbers even beyond 2^53 (use a str newtype for interop); option keys are required with null for none; codecs remain path dependencies until M9.8; stale files fail checks; limits are fixed pending M10.5; always commit gen/; reserve exact std.reflect pending M0.9 naming. Schema evolution and file-input DSL generators are deferred.

Alternatives considered

Consequences

One grammar and source-based plugin authoring survive; staged reachability and companion authority require explicit loader support. M9.5 lands parser, sandbox, companions, reflection, execution, provenance, tools and spec in that order; M9.6 adds codecs and the necessary vec.to_bytes primitive. The language's absent runtime reflection/macros statement remains true.

ADR reservations: 0079 sandbox limits (Step 5.2) then 0080 companions (5.3) follow landing order. The reflection record lands before cache/provenance, so this owner-assigned sequence is 0081 reflection (5.4), 0082 cache/provenance (5.6), and 0083 codec wire rules (6.1). ADR-0076 belongs to parallel M9.3 work. At 8a46e18, canon exposes forms/CanonicalItem.sig_form/body_form and the loader exposes Loaded.program/visible_interfaces/capability_ceilings; M9.3 Steps 2–5 and cp/9.2-packages are absent. Recheck those integration points after they land, before closing M9.5/CP-9.3.