macros

Opaque, category-indexed syntax values for procedural macros.

Syntax keeps token boundaries, source trivia, provenance, and hygiene contexts. Quotation marks written identifiers with definition-site plus fresh expansion scopes. Antiquotation ($name) embeds an existing syntax value without changing its use-site context. Parsing validates categories from canonical tokens; it never re-lexes rendered text.

API Reference

Structs

struct SyntaxContext

A set of lexical scopes. Its representation is private so identifiers can acquire context only through explicit syntax operations, never from text.

struct QuoteContext

The contexts applied to identifiers introduced by one quotation.

struct Syntax<Category>

Opaque, category-indexed macro syntax with token provenance and hygiene.

struct SyntaxSplice<Category>

A named antiquotation value awaiting insertion into a quote.

struct SyntaxFailure

A stable procedural-syntax failure suitable for crossing the macro-service ABI.

struct SyntaxResult<Category>

A category-checked syntax value or its construction failure.

struct SyntaxIdentifier

An identifier's spelling, source provenance, and hygienic resolution context.

struct MaterializedIdentifier

One identifier in the virtual source parsed for expansion. span and lexeme index that virtual source; source_* point back to the token from which the syntax object was captured or quoted.

struct MaterializedSyntax<Category>

The virtual source, canonical tokens, AST, and provenance produced from syntax.

struct MaterializedResult<Category>

A successful materialization or the parse failure that rejected it.

struct SyntaxScopeRecord

ABI-facing hygiene records keep generic Range fields flattened. Macro services return this envelope beside their category-specific parse result.

struct SyntaxMetadataOutput

Flattened identifier metadata returned across the procedural-macro service boundary.

struct ExprMacroOutput

The service boundary for expression procedural macros. A successful value carries the already-parsed canonical outcome; Rust adapts it directly and does not parse or lex the materialized source again.

struct DeclWrapperOutput

The service boundary for declaration wrapper macros. A replacement carries the already-parsed canonical outcome, exactly like ExprMacroOutput; removed reports the intentional no-declaration result distinctly from failure.

struct DeclView

A token-preserving lens over a declaration's name and optional body.

Enums

enum SyntaxOrigin

Determines which hygiene context an identifier receives during expansion.

enum SyntaxScopeKind

Scope identities are allocated by the macro driver. The pair keeps scope ordinals local to one deterministic compilation namespace.

enum DeclWrapperResult

A wrapper macro's single explicit result: replace the target with one declaration, intentionally emit no declaration, or report a diagnostic.

enum DeclShape

The grammatical shape a viewed declaration parsed as. Functions, methods, and initializers report a body; every shape reports a name when the grammar declares one.

Functions

func splice<Category>(name: String, consume syntax: Syntax<Category>) -> SyntaxSplice<Category>

Binds category-checked syntax to an antiquotation name for one quote operation.

func pattern_expr_fragment(consume syntax: Syntax<Pattern>) -> Syntax<Expr>

These fragments are valid only inside an expression quotation at the corresponding grammar position. The completed quotation is parsed before it can escape, so a pattern or type can never materialize as a standalone expression result.

func expr_decl_fragment(consume syntax: Syntax<Expr>) -> Syntax<Decl>

These fragments are valid only inside a declaration quotation at the corresponding grammar position; the completed quotation is still parsed as one declaration before it can escape the wrapper service boundary.

func string_literal_expr(value: String, source_id: Int, source_span: Range<Int>, context: &QuoteContext) -> Syntax<Expr>

Construct a category-checked string literal without routing generated text through the lexer. This is intentionally a literal-only constructor: macro authors still cannot manufacture identifiers or their syntax contexts from strings. The generated token points back to the source syntax that caused the literal so diagnostics retain useful provenance.

func no_macro_input() -> MacroInput?

Optional constructors for the generated wrapper ABI entry points, which deliberately avoid naming MacroInput so their generated text can never collide with a macro unit's own imports.

func capture_wrapper_target(source_id: Int, source: String, encoded: String, context_tag: Int, context: &SyntaxContext) -> SyntaxResult<Decl>

Capture a wrapper's target declaration. The first application in a chain supplies the parser-recorded canonical tokens; a chained application supplies the previous replacement's rendered source instead, which scans deterministically because it was materialized from canonical tokens.

func view_decl(target: &Syntax<Decl>, declaration: &DeclContext) -> DeclView?

Build a structural view of a declaration in the context it occupies. .none reports syntax the declaration grammar rejects, which a wrapper target can only be when a previous rebuild produced it.

func view_body(view: &DeclView) -> Syntax<Expr>?

The body block as expression syntax (braces included), preserving each token's provenance and hygiene context, so it can be spliced into an expression quotation.

func view_with_name(view: &DeclView, name: String, context: &QuoteContext) -> SyntaxResult<Decl>

Rebuild the declaration with a fresh, hygienically introduced name: the binder carries the definition site plus the expansion scope, so it neither captures caller names nor leaks into caller scope.

func view_with_name_token(view: &DeclView, source_id: Int, source: String, token: Token, use_site: &SyntaxContext) -> SyntaxResult<Decl>

Rebuild the declaration with a caller-provided identifier as the binder (ADR 0026's intentional exposure path). The token must come from use-site syntax — typically the wrapper's argument tree — and keeps its use-site context, so callers can see the rebuilt name.

func syntax_text<Category>(syntax: &Syntax<Category>) -> String

Materializes the syntax's tokens and trivia as source text without reparsing.