0080. Generated files are companions of the deriving module
- Status: Accepted
- Date: 2026-11-01
- Plan: M9.5 Step 5.3
- Review: Making Tenon plugin-first
Context
Ordinary generated modules would import their host to use its types, while the host would import them to use their declarations, creating a cycle. Private types cannot be imported, and ADR-0069 restricts newtype representations to the declaring file. Generators also should not ship in runtime programs merely because their modules were loaded.
Decision
Extend ADR-0064
and ADR-0069 as specified by ADR-0078.
For derive X for T, T must be a
non-generic struct, enum or newtype declared in the deriving file;
X resolves through normal file bindings to a
function. The companion path is
<package>/gen/<module-or-root-stem-path>/<T>.<X>.hn.
A companion shares its host's ModuleId but has its own file
index. It sees host bindings, its own imports and declarations;
the host sees its declarations without an import. Identical
provider/name imports from both are deduplicated.
Loaded.file_owners maps a companion to its host for
newtype access only; other file-scoped authority is not widened.
Package ownership and capability ceilings remain those of the
deriving package.
Rewrite records uses: an import edge is generator-only when every selected local name is used only as a derive generator. Modules reachable only through such edges remain loaded for hashing but leave the runtime program. A generator closure depending on its deriving module is E0963; generator packages may have derives when their companions are fresh. Existing program topology stays stable.
Alternatives considered
- Ordinary generated modules — import cycles and file-private types prevent the owner-authorized API.
- Grant all files in a module newtype access — foreign files could bypass the declaring owner's opacity; the companion exception is narrower.
- Keep all generator modules in runtime lowering — unused generators and their dependencies would ship because non-generic functions are roots.
Consequences
The owner explicitly authorizes generation; generated code can use private types and newtype representations without weakening access for unrelated files. The loader must retain separate file identities and distinguish stage-only edges. Missing companions and invalid bindings receive stable diagnostics.