Skip to content

Clarify the call-site hierarchy and add a non-trivial example - #3069

Closed
isHeSatoshi wants to merge 1 commit into
rust-lang:mainfrom
isHeSatoshi:clarify-call-site-hierarchy
Closed

isHeSatoshi wants to merge 1 commit into
rust-lang:mainfrom
isHeSatoshi:clarify-call-site-hierarchy

Conversation

@isHeSatoshi

@isHeSatoshi isHeSatoshi commented Oct 19, 2026 •

Copy link
Copy Markdown

Fixes #1874.

The call-site hierarchy was described only with an example where every invocation is written in the source:

macro bar($i: ident) { $i }
macro foo($i: ident) { $i }

foo!(bar!(baz));

That gives ROOT -> baz, with no intermediate expansion, which makes the hierarchy hard to read: it is not obvious what is being tracked, and it is not obvious why a plain identifier like baz has a hierarchy at all when the section is about macro invocations.

This change:

  • states what the hierarchy records: where code was written rather than how it was produced, with the invocation's SyntaxContext as the call_site link;
  • notes that every token has one, because every token carries a syntax context, so naming baz in the notation just identifies whose hierarchy is being described;
  • explains why the existing example is trivial: bar!(baz) is written as the argument to foo!, so it is not produced by foo!, and baz keeps the call site's context;
  • adds the case that produces a non-trivial chain, an invocation written inside another macro's definition:
macro bar($i: ident) { $i }
macro foo() { bar!(baz) }

foo!();

I checked the second example against the compiler rather than reasoning about it. With -Zunpretty=expanded,hygiene on nightly, bar reports call_site_ctxt: #4 where #4 is the context created by foo's expansion (the first example reports call_site_ctxt: #0 for both invocations), and the baz in the output carries that same #4 context. Extending the chain to three macros gives call_site_ctxt #0, #4, #5 for a, b and c, with baz at #6, i.e. one fresh context per nesting level.

No new links are added; the section reuses the existing [callsite] and [sc] definitions.

The section explained the call-site hierarchy only with an example in
which every invocation is written in the source, so the hierarchy came
out as a single `ROOT -> baz` chain with no intermediate expansion. It
was hard to tell what the hierarchy tracks, and why a plain identifier
such as `baz` has one at all.

Say what the hierarchy records: where code was written, rather than how
it was produced, with the invocation's syntax context as the `call_site`
link. Note that it is a property of every token, not just of macro
invocations, which is why a token like `baz` is named in the notation.

Then add the case that actually produces a non-trivial chain: an
invocation written inside another macro's definition. When the
invocation is merely passed in as an argument it is written at the call
site, which is why the first example is trivial.
@isHeSatoshi

This comment was marked as low quality.

@rustbot

rustbot commented Oct 19, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the PR. If you have write access, feel free to merge this PR if it does not need reviews. You can request a review using r? rustc-dev-guide or r? <username>.

@rustbot rustbot added the S-waiting-on-review Status: this PR is waiting for a reviewer to verify its content label Oct 19, 2026
@BoxyUwU BoxyUwU closed this Oct 19, 2026
@rustbot rustbot removed the S-waiting-on-review Status: this PR is waiting for a reviewer to verify its content label Oct 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Clarify "call site hierarchy" in macro

4 participants