Skip to main content

Origin

Struct Origin 

Source
#[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
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§parent: Context

The 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: Symbol

The name of the macro whose expansion introduced the token.

§call_site: Span

The span of the invocation that triggered the expansion.

§call_context: Context

The 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.

Trait Implementations§

Source§

impl Clone for Origin

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Origin

Source§

impl Debug for Origin

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Origin

Source§

impl Hash for Origin

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Origin

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Origin

Auto Trait Implementations§

§

impl Forget for Origin

§

impl Freeze for Origin

§

impl Move for Origin

§

impl RefUnwindSafe for Origin

§

impl Send for Origin

§

impl Sync for Origin

§

impl Unpin for Origin

§

impl UnsafeUnpin for Origin

§

impl UnwindSafe for Origin

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.