Skip to content

Add hygiene annotations for tokens in macro_rules! bodies#153308

Open
aytey wants to merge 1 commit intorust-lang:mainfrom
aytey:macro_meta_hygiene
Open

Add hygiene annotations for tokens in macro_rules! bodies#153308
aytey wants to merge 1 commit intorust-lang:mainfrom
aytey:macro_meta_hygiene

Conversation

@aytey
Copy link
Contributor

@aytey aytey commented Mar 2, 2026

-Zunpretty=expanded,hygiene was not printing syntax context annotations for identifiers and lifetimes inside macro_rules! bodies. These tokens are printed via print_tt()token_to_string_ext(), which converts tokens to strings without calling ann_post(). This meant that macro-generated macro_rules! definitions with hygienic metavar parameters (e.g. multiple $marg distinguished only by hygiene) were printed with no way to tell them apart.

This was fixed by adding a match on token.kind in print_tt() to call ann_post() for Ident, NtIdent, Lifetime, and NtLifetime tokens, matching how print_ident() and print_lifetime() already handle AST-level identifiers and lifetimes.

`-Zunpretty=expanded,hygiene` was not printing syntax context annotations
for identifiers and lifetimes inside `macro_rules!` bodies. These tokens
are printed via `print_tt()` → `token_to_string_ext()`, which converts
tokens to strings without calling `ann_post()`. This meant that
macro-generated `macro_rules!` definitions with hygienic metavar
parameters (e.g. multiple `$marg` distinguished only by hygiene) were
printed with no way to tell them apart.

This was fixed by adding a match on `token.kind` in `print_tt()` to call
`ann_post()` for `Ident`, `NtIdent`, `Lifetime`, and `NtLifetime`
tokens, matching how `print_ident()` and `print_lifetime()` already
handle AST-level identifiers and lifetimes.

Signed-off-by: Andrew V. Teylu <andrew.teylu@vector.com>
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 2, 2026
@rustbot
Copy link
Collaborator

rustbot commented Mar 2, 2026

r? @jackh726

rustbot has assigned @jackh726.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 69 candidates
  • Random selection from 15 candidates

@aytey aytey changed the title Add hygiene annotations for tokens in macro_rules! bodies Add hygiene annotations for tokens in macro_rules! bodies Mar 2, 2026
@aytey
Copy link
Contributor Author

aytey commented Mar 2, 2026

r? @jdonszelmann

(I hope this appropriate; I only selected @jdonszelmann as she helped me with my previous commit in the same area)

@rustbot rustbot assigned jdonszelmann and unassigned jackh726 Mar 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants