Skip to content

Conversation

ehuss
Copy link
Contributor

@ehuss ehuss commented Sep 29, 2025

This attempts to clarify some details about proc-macro tokens, in particular the differences in handling _ from macro_rules. This includes several changes:

  • Removed the wordy description of a macro_rules TokenTree. This is already defined in the grammar in more detail. This does remove the sentence discussing metavariable substitutions. I would prefer to defer this to a more detailed description in the macro_rules chapter on how metavariable substitutions work.
  • Removed the English description of a proc-macro token tree, and instead write it as a grammar production.

Closes #2020
Closes #1236

This attempts to clarify some details about proc-macro tokens, in
particular the differences in handling `_` from `macro_rules`. This
includes several changes:

- Removed the wordy description of a `macro_rules` TokenTree. This
  is already defined in the grammar in more detail. This does remove
  the sentence discussing metavariable substitutions. I would prefer
  to defer this to a more detailed description in the `macro_rules`
  chapter on how metavariable substitutions work.
- Removed the English description of a proc-macro token tree, and
  instead write it as a grammar production.

Closes rust-lang#2020
Closes rust-lang#1236
@rustbot rustbot added the S-waiting-on-review Status: The marked PR is awaiting review from a maintainer label Sep 29, 2025
@petrochenkov
Copy link
Contributor

r? @petrochenkov

- Delimited groups (`(...)`, `{...}`, etc)
- All operators supported by the language, both single-character and
multi-character ones (`+`, `+=`).
- Note that this set doesn't include the single quote `'`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wrote the English text to highlight the differences between macro_rules and proc macro tokens, like multi-character operators (which are not a thing in proc macros), lifetimes, negative literals.
From the formal grammar the difference is very hard to see.

| ProcMacroTokenPunct
| ProcMacroTokenLiteral
ProcMacroTokenIdent -> IDENTIFIER_OR_KEYWORD | RAW_IDENTIFIER | `_`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't reference consider _ a keyword?
I think it should, it's lexically an identifier, and it's reserved, so it's a keyword (== reserved identifier).

When tokens are passed to a proc-macro:

- All multi-character punctuation is broken into single characters.
- `_` is treated as an identifier.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is misleading, _ is a (reserved) identifier.

- Negative literals are converted into two tokens (the `-` and the literal)
possibly wrapped into a delimited group ([`Group`]) with implicit delimiters
([`Delimiter::None`]) when it's necessary for preserving parsing priorities.
- The `_` identifier is interpreted as the `_` punctuation.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the reference should ever call _ punctuation, it's a (reserved) identifier in all senses except one - ident matcher in macro_rules doesn't accept it for backward compatibility reasons (but it can likely be changed over an edition).

@petrochenkov
Copy link
Contributor

@rustbot author
(I don't have rights to change labels directly here.)

@rustbot rustbot added S-waiting-on-author Status: The marked PR is awaiting some action (such as code changes) from the PR author. and removed S-waiting-on-review Status: The marked PR is awaiting review from a maintainer labels Sep 29, 2025
@rustbot
Copy link
Collaborator

rustbot commented Sep 29, 2025

Reminder, once the PR becomes ready for a review, use @rustbot ready.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: The marked PR is awaiting some action (such as code changes) from the PR author.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Further cases where lone underscore isn't covered Grammar for "identifier or keyword" seems wrong
3 participants