liveBook · Manning

Call_site

As explained in Write Powerful Rust Macros

call_site is a Span-associated function that creates a span resolved at the location where a procedural macro is invoked.

call_site() creates a Span that resolves an identifier as though it were written where the procedural macro is invoked. This makes it suitable for generated methods intended for application code. section 5.1.3

Choosing the span for generated identifiers

Construct a generated identifier with Ident::new("...", Span::call_site()). By contrast, mixed_site() follows declarative-macro hygiene rules: a generated local variable using mixed_site cannot be referenced from the surrounding application scope, producing a not-found error. section 5.1.3

pub fn push_ident(tokens: &mut TokenStream, s: &str) {
    let span = Span::call_site();
    push_ident_spanned(tokens, span, s);
}

The helper centralizes Span::call_site() before passing the span into identifier-generation code. section 5.1.3

How Write Powerful Rust Macros covers it

How deep it goes
Explains identifier construction and contrasts call-site resolution with mixed-site hygiene.
Examples
Uses generated method names and a mixed_site local variable that fails when referenced from the invoking function.
What it assumes
Assumes familiarity with Rust procedural macros, spans, identifiers, and macro-generated code.
What you can do after
Apply Span::call_site() when generated identifiers should resolve in application code.
How it connects
Connects Span, procedural macros, identifiers, macro hygiene, mixed_site, and declarative macros.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Write Powerful Rust Macros. The section links point to the parts of the book it draws on.