#[non_exhaustive]pub struct Origin {
pub parent: Context,
pub macro_name: Symbol,
pub call_site: Span,
pub call_context: Context,
}Expand description
Where a non-root Context came from: the expansion that minted it.
Returned by Expander::origin. The fields answer
the two questions a later phase asks about a macro-introduced token: how
should its name resolve, and how did it get here.
The struct is #[non_exhaustive]: only the expander builds one, and new
fields may be added in a minor release. Read the fields directly, or
destructure with a trailing ...
§Examples
use macro_lang::{Context, Expander, Fragment, Macro, Pattern, Rule, Template, Tree};
use intern_lang::Interner;
use token_lang::Span;
let mut names = Interner::new();
let id = names.intern("id");
// A macro whose template introduces a single token: `0`.
let mac: Macro<char> = Macro::new(id, vec![Rule {
pattern: vec![],
template: vec![Template::token('0', Span::new(20, 21))],
}])?;
let mut expander = Expander::new();
let out = expander.expand(&mac, &[], Span::new(0, 4), Context::ROOT)?;
let ctx = match out.first() {
Some(Tree::Token { ctx, .. }) => *ctx,
_ => return Err("expected one token".into()),
};
let origin = expander.origin(ctx).ok_or("context not minted by this expander")?;
assert_eq!(origin.macro_name, id);
assert_eq!(origin.call_site, Span::new(0, 4));
assert_eq!(origin.parent, Context::ROOT);
assert_eq!(origin.call_context, Context::ROOT);Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.parent: ContextThe context the token had in the macro’s definition, before this expansion marked it.
For a macro defined directly in source this is Context::ROOT. A name
resolver falls back to this context to resolve a macro-introduced name
that the expansion itself does not bind — a call to a global function from
inside a template, for example — so it resolves where the macro was
defined rather than where it was called.
macro_name: SymbolThe name of the macro whose expansion introduced the token.
call_site: SpanThe span of the invocation that triggered the expansion.
call_context: ContextThe context of the invocation itself. Following call_context outward
walks the chain of nested expansions, which is what an “in this macro
invocation” diagnostic backtrace prints.