Skip to content

Commit 46088a7

Browse files
committed
Rework RESERVED_RAW_IDENTIFIER and RESERVED_RAW_LIFETIME
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 (#2042), but that is a bigger ask.
1 parent 3749e6d commit 46088a7

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

src/identifiers.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ XID_Start -> <`XID_Start` defined by Unicode>
99
1010
XID_Continue -> <`XID_Continue` defined by Unicode>
1111
12-
RAW_IDENTIFIER -> `r#` IDENTIFIER_OR_KEYWORD _except `crate`, `self`, `super`, `Self`, `_`_
12+
RAW_IDENTIFIER -> `r#` IDENTIFIER_OR_KEYWORD
1313
1414
NON_KEYWORD_IDENTIFIER -> IDENTIFIER_OR_KEYWORD _except a [strict][lex.keywords.strict] or [reserved][lex.keywords.reserved] keyword_
1515
1616
IDENTIFIER -> NON_KEYWORD_IDENTIFIER | RAW_IDENTIFIER
1717
18-
RESERVED_RAW_IDENTIFIER -> `r#_`
18+
RESERVED_RAW_IDENTIFIER -> `r#` (`_` | `crate` | `self` | `Self` | `super`)
1919
```
2020

2121
<!-- When updating the version, update the UAX links, too. -->
@@ -72,7 +72,7 @@ Unlike a normal identifier, a raw identifier may be any strict or reserved
7272
keyword except the ones listed above for `RAW_IDENTIFIER`.
7373

7474
r[ident.raw.reserved]
75-
It is an error to use the [RESERVED_RAW_IDENTIFIER] token `r#_` in order to avoid confusion with the [WildcardPattern].
75+
It is an error to use the [RESERVED_RAW_IDENTIFIER] token.
7676

7777
[`extern crate`]: items/extern-crates.md
7878
[`no_mangle`]: abi.md#the-no_mangle-attribute

src/tokens.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ r[lex.token]
44
r[lex.token.syntax]
55
```grammar,lexer
66
Token ->
7-
IDENTIFIER_OR_KEYWORD
7+
RESERVED_TOKEN
88
| RAW_IDENTIFIER
99
| CHAR_LITERAL
1010
| STRING_LITERAL
@@ -18,7 +18,7 @@ Token ->
1818
| FLOAT_LITERAL
1919
| LIFETIME_TOKEN
2020
| PUNCTUATION
21-
| RESERVED_TOKEN
21+
| IDENTIFIER_OR_KEYWORD
2222
```
2323

2424
r[lex.token.intro]
@@ -769,9 +769,9 @@ LIFETIME_OR_LABEL ->
769769
| RAW_LIFETIME
770770
771771
RAW_LIFETIME ->
772-
`'r#` IDENTIFIER_OR_KEYWORD _except `crate`, `self`, `super`, `Self`, `_` and not immediately followed by `'`_
772+
`'r#` IDENTIFIER_OR_KEYWORD _not immediately followed by `'`_
773773
774-
RESERVED_RAW_LIFETIME -> `'r#_` _not immediately followed by `'`_
774+
RESERVED_RAW_LIFETIME -> `'r#` (`_` | `crate` | `self` | `Self` | `super`) _not immediately followed by `'`_
775775
```
776776

777777
r[lex.token.life.intro]
@@ -786,7 +786,7 @@ r[lex.token.life.raw.allowed]
786786
Unlike a normal lifetime, a raw lifetime may be any strict or reserved keyword except the ones listed above for `RAW_LIFETIME`.
787787

788788
r[lex.token.life.raw.reserved]
789-
It is an error to use the RESERVED_RAW_LIFETIME token `'r#_` in order to avoid confusion with the [placeholder lifetime].
789+
It is an error to use the [RESERVED_RAW_LIFETIME] token.
790790

791791
r[lex.token.life.raw.edition2021]
792792
> [!EDITION-2021]
@@ -922,7 +922,7 @@ r[lex.token.reserved]
922922
## Reserved tokens
923923

924924
r[lex.token.reserved.intro]
925-
Several token forms are reserved for future use. It is an error for the source input to match one of these forms.
925+
Several token forms are reserved for future use or to avoid confusion. It is an error for the source input to match one of these forms.
926926

927927
r[lex.token.reserved.syntax]
928928
```grammar,lexer
@@ -1058,7 +1058,6 @@ r[lex.token.reserved-guards.edition2024]
10581058
[numeric types]: types/numeric.md
10591059
[paths]: paths.md
10601060
[patterns]: patterns.md
1061-
[placeholder lifetime]: lifetime-elision.md
10621061
[question]: expressions/operator-expr.md#the-try-propagation-expression
10631062
[range]: expressions/range-expr.md
10641063
[rangepat]: patterns.md#range-patterns

0 commit comments

Comments
 (0)