Skip to content

Permit crate/self/Self/super as raw idents - #165748

Open
jhpratt wants to merge 2 commits into
rust-lang:mainfrom
jhpratt:raw-path-idents
Open

jhpratt wants to merge 2 commits into
rust-lang:mainfrom
jhpratt:raw-path-idents

Conversation

@jhpratt

@jhpratt jhpratt commented Nov 10, 2026

Copy link
Copy Markdown
Member

Currently, r#crate, r#self, r#Self, and r#super are prohibited regardless of circumstance. This is due to the fact that all four idents are allowed in paths and the fact that an identifier is raw is stripped away very early on in the compilation process.

A trivial attempt at bypassing that would have resulted in quite a bit of work, so I took a different approach: consider the four raw identifiers an identifier of their own. In other words, r#self is not the same as self, even though r#foo is the same as foo. This was surprisingly easy to implement…I only had to add four symbols and intercept them in the parser. Proc macros weren't much harder.

Other than tests specifically targeting the lack of support for these raw identifiers, no UI tests were altered.

As a language change, this will require FCP from T-lang. There's probably some other documentation that needs updating, in particular the reference, which is believed to explicitly document the current behavior. Technically it involves changing the API of proc_macro in that more raw idents are supported, but that feels so trivial that T-libs doesn't need to be involved in my opinion.

cc @rust-lang/lang and @rust-lang/lang-advisors

Initial discussion on Zulip.


Disclosure: An LLM was used to find the appropriate parts of code to edit, but all code was written by hand (minus blessing tests, of course). This is well within the policy.

@jhpratt jhpratt added T-lang Relevant to the language team needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. labels Nov 10, 2026
@rustbot

rustbot commented Nov 10, 2026

Copy link
Copy Markdown
Collaborator

The parser was modified, potentially altering the grammar of (stable) Rust
which would be a breaking change.

cc @fmease

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 10, 2026
@rustbot

rustbot commented Nov 10, 2026

Copy link
Copy Markdown
Collaborator

r? @mu001999

rustbot has assigned @mu001999.
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 79 candidates
  • Random selection from 23 candidates

@petrochenkov petrochenkov self-assigned this Nov 10, 2026
@theemathas

Copy link
Copy Markdown
Contributor

What does $ r#crate do?

@jhpratt

jhpratt commented Nov 10, 2026

Copy link
Copy Markdown
Member Author

Uhh good question. It should be rejected, but I'll add a test and make sure that is the case.

@theemathas

Copy link
Copy Markdown
Contributor

I would expect it to be a normal metavariable, like $x

@mu001999 mu001999 removed their assignment Nov 10, 2026
@petrochenkov

petrochenkov commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

The implementation is clearly a hack, but if the hack is applied immediately after the token leaves rustc_lexer (or proc macro bridge), then maybe it won't cause issues? I'm not entirely sure.

One problem that I see is that the token stream representation (token::Ident) has the hack already applied, so things like token::Ident(kw::Crate, IdentKind::Raw) do not actually mean the expected thing.
On the other hand, applying the hack when going from tokens streams to AST (or Attribute IR, or whatever) would require doing it in many places.

Is there specific motivation to support these, or is it just "why not"?

@petrochenkov

Copy link
Copy Markdown
Contributor

This needs testing if we want to go forward with this.

Name resolution:

  • struct r#Self defines a structure names Self, then path r#Self refers to that struct, and path Self does not
    • and the same for self, super and crate
  • Type parameter called r#Self doesn't cause issues

Parsing:

  • fn foo(r#self) is not treated as a method receiver
  • pub(r#self), impl(r#self) and mut(r#self) are not parsed as visibilities and impl restrictions
    • and the same for super and crate
  • extern crate r#self is not parsed as extern crate self
  • r#super let is not parsed as super let
  • $r#crate is not treated as $crate

@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 10, 2026
@petrochenkov

Copy link
Copy Markdown
Contributor

Any other uses of kw::{SelfUpper,SelfInner,Super,Crate} with IdentKind in vicinity also need to be audited.

@jhpratt

jhpratt commented Nov 10, 2026

Copy link
Copy Markdown
Member Author

Is there specific motivation to support these, or is it just "why not"?

I tried using r#super as a variable name (throwaway code; didn't care enough to get a better name) and was surprised that it didn't work. From the thread on Zulip, it seems that I'm not the only one surprised by that.

I can add a slew of test cases, assuming this is something the team wants in principle?

@Jules-Bertholet

Copy link
Copy Markdown
Contributor

What about 'r#static (also disallowed currently)?

@Jules-Bertholet

Copy link
Copy Markdown
Contributor

It would also be good to have a test that e.g. #[unsafe(no_mangle)] fn r#crate() {} doesn't include the r# in the resulting symbol.

@jhpratt

jhpratt commented Nov 12, 2026

Copy link
Copy Markdown
Member Author

I would expect it to be a normal metavariable, like $x

TIL raw identifiers can be used as metavariable names as well. I assumed they wouldn't be supported because $self. I've confirmed that $r#crate works, just as $r#struct already does.

How do you feel about making the Ident variant mut-restricted (i.e. Ident(mut(self) Symbol, mut(self) IdentKind)), forcing everyone outside the module to go through a constructor function that can handle the conversion? Pattern matching and field access would remain as-is.

For 'r#static, that's reasonable to add as well. I'll take a look at it after I get raw path keyword idents working as desired.

@ytmimi

ytmimi commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

@jhpratt Could you add rustfmt tests? I'd want to make sure rustfmt isn't accidentally removing r#

Maybe just add the following to src/tools/rustfmt/tests/target/raw_identifiers.rs

struct r#crate {}
struct r#self {}
struct r#Self {}
struct r#super {}

or something like this:

use some::r#crate;
use some::r#self;
use some::r#Self;
use some::r#super;
use r#crate::r#super::r#self::r#Self;

@rust-bors

rust-bors Bot commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

☔ The latest upstream changes (presumably #166371) made this pull request unmergeable. Please resolve the merge conflicts by rebasing.

This branch has not been deployed

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

Labels

needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-lang Relevant to the language team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants