Expand description
Typed macros: the half of §2.4 that wants the checker’s answers.
An ordinary macro runs before anything has been inferred, which is why derive_json is
handed a declaration rather than an expression — a model’s fields are in its syntax, so no
type information is needed and none can be had. A typed macro is the other case: it is called
with expressions, and what it wants to know is what those expressions are.
So a typed macro is expanded by the checker, at the call site, once the arguments have been
inferred. The body is the same language an untyped macro body is (crate::interp), with one
name added: node_ty(e) answers with the type the checker gave e, as a value the body can
ask questions of.
§What a body sees
TyRepr is the value, and it is reached through the ordinary record notation rather than
through a family of builtins:
| Written | Answers |
|---|---|
t.name | "Int", "list", "Todo" — the head, with no arguments |
t.kind | "builtin", "model", "union", "newtype", "fn", "param", "unknown" |
t.args | list[Int] answers [Int]; a function answers its parameter types |
t.result | a function’s result type |
t.fields | a model’s fields, as {name, ty} records, with the type’s own arguments substituted in |
t.variants | a union’s variants, as {name, fields} records |
t.inner | what a newtype wraps |
fields, variants and inner are read on access rather than carried in the value, and
that is not an optimisation: model Tree: left: Tree is a type whose fields mention itself, so
a value holding its own fields eagerly would not be a finite value. A type expression is
always finite; only looking into a declaration recurses, and a body that recurses without a base
case is stopped by the same nesting bound every other compile-time call is.
Structs§
- Decl
Info - What one
model,unionornewtypedeclaration holds. - TypeEnv
- What the checker inferred, for the one call being expanded.
- Typed
Expander - The typed macros a module has, and everything their bodies may reach.
Enums§
- TyKind
- What a declaration is, for a macro asking what it may look into.
- TyRepr
- The type of an expression, as a macro body sees it.