0024. Inference-only monomorphized generics
- Status: Accepted
- Date: 2026-10-30
- Plan: M4.4
- Review: Language semantics; Compiler and backend
Context
Generic code must have predictable diagnostics, concrete Core IR and measurable native code size. The agreed M4 plan reserves this number for generics.
Decision
Declare fn id<T>(x: T) -> T,
struct Pair<A, B> and
enum Choice<T>. Type applications such as
Pair<i64, str> occur only in type positions.
Call sites infer arguments locally from the expected result, then
arguments in written order. Explicit call-site arguments receive
E0603 with a removal fix. Definitions are checked once with
opaque, rigid parameters. Equality is universal; arithmetic and
printing require their existing concrete types. Interface bounds
remain M4.7. Contextual inference holes cannot escape an
expression.
mono.rs runs last in the front end, without the
AST. All non-generic functions are source-ordered roots retaining
their IDs; generic instances follow in FIFO discovery order and
deduplicate exact (name, type arguments) keys.
Unreached generics are checked but emit nothing. Lowering
substitutes types and interns record/union layouts post-order;
both backends consume the same concrete IR. Instances use
hi<function-id>_ C names, disjoint from user
hn_ names. Locations and fuel charges retain the
generic definition's positions.
Reject discovery beyond depth 64 or type trees beyond 256 nodes
with E0605, and more than 10,000 generic instances with E0606.
Count substitution size before allocating expanded trees. The same
size guard also applies during inference and front-end validation
of concrete field/payload expansion: a mono-call-only guard cannot
bound straight-line inference or expanding generic layout
declarations. This necessary extension prevents allocation
failures before mono/lowering. Branching generic fields can also
create exponentially many distinct layouts without large type
trees; concrete generic layouts have a separate 10,000-entry E0606
budget. Plain nongeneric layouts do not consume this budget.
Nongeneric programs bypass specialization discovery and lower
source functions directly with the same root IDs. Generic
signature metadata is sparse so scalar functions do not pay for
vectors/spans they cannot use (ADR-0029). Track C bytes,
functions, layouts and build medians with
bench.py --sizes.
Alternatives considered
[T]or turbofish/explicit calls — adds syntax and expression ambiguity; local inference and annotations suffice for M4.- Instantiation-time body checking — defers invalid definitions and repeats checking; opaque definition parameters give stable, local diagnostics.
- Boxed or dictionary-passed values — adds runtime representation and dispatch before the memory model; concrete specialization fits the C backend.
- Depth alone — exponential type growth exhausts memory before depth 64.
Consequences
No generic parameters reach IR or either backend.
Specialization may increase C size, so the generics fixture
measures eight shared instances across callers. The limits are
ordinary shared-front-end diagnostics, including under
check.