-
Notifications
You must be signed in to change notification settings - Fork 553
Change underscore to be a keyword #2049
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
This changes underscore from being a punctuation character to a keyword. This is intended to help better align with proc-macros, which treat it as an [`Ident`](https://doc.rust-lang.org/proc_macro/struct.Ident.html). Note one unusual rule is inline assembly `ParamName` which is `IDENTIFIER_OR_KEYWORD`. From what I can tell, it does accept `_`, but the fmt template does not. Templates are not specified in great detail in the std docs, and don't touch on this fact. Closes rust-lang#1236 Closes rust-lang#2020
RAW_IDENTIFIER -> `r#` IDENTIFIER_OR_KEYWORD _except `crate`, `self`, `super`, `Self`, `_`_ | ||
NON_KEYWORD_IDENTIFIER -> IDENTIFIER_OR_KEYWORD _except a [strict][lex.keywords.strict] or [reserved][lex.keywords.reserved] keyword_ | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RESERVED_RAW_IDENTIFIER
a few lines below may be redundant now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, I spent some time thinking about this and how to correctly express that these keywords are rejected. The current except clause didn't express that in the way that I was intending. I pushed up a commit that instead of removing the reserved rule, it moves the except part into the reserved rule.
Or, to put it in another way, r#crate
is a token, it's just rejected as an error. The previous lexical grammar wasn't really conveying that.
* `expr`: an [Expression] | ||
* `expr_2021`: an [Expression] except [UnderscoreExpression] and [ConstBlockExpression] (see [macro.decl.meta.edition2024]) | ||
* `ident`: an [IDENTIFIER_OR_KEYWORD], [RAW_IDENTIFIER], or [`$crate`] | ||
* `ident`: an [IDENTIFIER_OR_KEYWORD] except `_`, [RAW_IDENTIFIER], or [`$crate`] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pre-existing, but this doesn't seem correct, ident
accepts raw identifiers and expanded $crate
(and unexpanded $crate
is two tokens and not IDENTIFIER_OR_KEYWORD
).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This reworks the reserved raw identifier and lifetimes to hopefully more clearly express what they mean. The "except" clauses in the raw identifier were intended to mean a set subtraction, not an explicit "and it is an error if it is specified". Using set subtraction isn't correct because that would mean `r#crate` would be interpreted as 3 tokens (since RAW_IDENTIFIER did not match it, but IDENTIFIER_OR_KEYWORD PUNCTUATION IDENTIFIER_OR_KEYWORD would). I also reordered Token, since the intent is that the first production in an alternation that matches wins. The idea here is to make the reserved tokens high priority, so that they clearly match first and cause an error. (I did not exhaustively analyze the rest of the rules to see if they follow that behavior, that is for another day.) It could be said it would be nice to document the rationale for the restrictions (rust-lang#2042), but that is a bigger ask.
fe4d139
to
46088a7
Compare
This changes underscore from being a punctuation character to a keyword. This is intended to help better align with proc-macros, which treat it as an
Ident
.Note one unusual rule is inline assembly
ParamName
which isIDENTIFIER_OR_KEYWORD
. From what I can tell, it does accept_
, but the fmt template does not. Templates are not specified in great detail in the std docs, and don't touch on this fact.Closes #1236
Closes #2020
cc @petrochenkov @mattheww