Skip to content

Improve description of ident fragment matcher, and $crate metavariable#2214

Open
steffahn wants to merge 1 commit intorust-lang:masterfrom
steffahn:ident_matcher
Open

Improve description of ident fragment matcher, and $crate metavariable#2214
steffahn wants to merge 1 commit intorust-lang:masterfrom
steffahn:ident_matcher

Conversation

@steffahn
Copy link
Copy Markdown
Member

I am initially coming to work on this due to the line

an IDENTIFIER_OR_KEYWORD except _, RAW_IDENTIFIER, or $crate

being unfortunately very ambiguous, since this can be read as “_, RAW_IDENTIFIER, or $crate” all belonging
to the “except” listing.

My best fix for that is to add parentheses around the “except _”.

an IDENTIFIER_OR_KEYWORD (except _), a RAW_IDENTIFIER, or $crate

This seems to read more nicely than alternatives I have tried, for instance

either an IDENTIFIER_OR_KEYWORD except _, or a RAW_IDENTIFIER, or "$crate"

(which still seems ambiguous)

And the exception for “_” seems minor enough (previously it had been missing completely for a while) that parentheses also seem sensible semantically, IMHO.

The meaning of accepting $crate was confusing to me though; and apparently a known issue, so this closes #588. (I think. Unless that issue wants even more information; or information in some particular other place?)


To achieve this, I have written a new section to the reference description of $crate, and worded the concept as $crate being “initially replaced”.

  • The verb “replace” is for consistency with the wording I could find around the handling of other metavariables, which are being “replaced” during expansion;
  • the qualifier “initially” is added to emphasize the important difference of this process from macro expansion. And also where I’m linking to it, the phrasing “replaced $crate” can easily be misunderstood for people to think that “$crate” has already been replaced by the name of the crate it’s referring to, even though that’s not at all how this process works!

I am initially coming to work on this due to the line
> an IDENTIFIER_OR_KEYWORD except _, RAW_IDENTIFIER, or $crate

being unfortunately very ambiguous, since this can be read
as “_, RAW_IDENTIFIER, or $crate” all belonging
to the “except” listing.

My best fix for that is to add parentheses around the “except _”.
This seems to read more nicely than alternatives I have tried, for instance

> either an IDENTIFIER_OR_KEYWORD except _, or a RAW_IDENTIFIER, or "$crate"

(which still seems ambiguous)

And the exception for “_” seems minor enough (previously it had been missing completely
for a while) that parentheses also seem sensible semantically, IMHO.

The meaning of accepting `$crate` was confusing to me though; and apparently
a known issue, so this closes rust-lang#588.

To achieve this, I have written a new section to the reference description of `$crate`,
and worded the concept as `$crate` being "initially replaced". The verb "replace"
is for consistency with the wording of other metavariables, which are being "replaced"
during expansion; the qualifier "initially" is added to emphasize the important
difference of this process from *macro expansion*. And also where I'm linking to it,
the phrasing "replaced $crate" can easily be misunderstood for people to think
that "$crate" has already been replaced by the name of the crate it's referring to,
even though that's not at all how this process works!
@rustbot rustbot added the S-waiting-on-review Status: The marked PR is awaiting review from a maintainer label Mar 21, 2026
```

r[macro.decl.hygiene.crate.replaced]
The raw syntax of `$crate` consists of two tokens (`$` followed by `crate`). When used as a metavariable within a macro definition, this syntax is initially replaced with a single token (also called "`$crate`"), which can be used an identifier.
Copy link
Copy Markdown
Contributor

@ehuss ehuss Apr 14, 2026

Choose a reason for hiding this comment

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

Can you say why this has an  ?

View changes since the review

fn foo() {}
```

However, the crate being referred to does *not* itself need to be visible from the invocation site as a directly declared dependency. A main purpose of `$crate` is to offer a way of reliably naming crates (and their public items) in macro-generated code, even if the only exists as a transitive dependency (i.e. "dependency of a dependency") from the invocation site.
Copy link
Copy Markdown
Contributor

@ehuss ehuss Apr 14, 2026

Choose a reason for hiding this comment

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

I think this is what was intended?

Suggested change
However, the crate being referred to does *not* itself need to be visible from the invocation site as a directly declared dependency. A main purpose of `$crate` is to offer a way of reliably naming crates (and their public items) in macro-generated code, even if the only exists as a transitive dependency (i.e. "dependency of a dependency") from the invocation site.
However, the crate being referred to does *not* itself need to be visible from the invocation site as a directly declared dependency. A main purpose of `$crate` is to offer a way of reliably naming crates (and their public items) in macro-generated code, even if it only exists as a transitive dependency (i.e. "dependency of a dependency") from the invocation site.

View changes since the review

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Or maybe "the crate"?

@ehuss
Copy link
Copy Markdown
Contributor

ehuss commented Apr 14, 2026

Just for clarity, I also wanted to link to rust-lang/rust#155121 as there are active changes happening in this area. I don't think that affects this PR. I'm not sure if you're already aware of those changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: The marked PR is awaiting review from a maintainer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Document $crate in macro matcher

3 participants