Skip to content

Commit e0a0414

Browse files
authored
Merge pull request #1603 from compiler-errors/patch-1
Raw lifetimes
2 parents 5b2b828 + baa3d73 commit e0a0414

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

src/identifiers.md

+6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ r[ident.syntax]
1414
>
1515
> IDENTIFIER :\
1616
> NON_KEYWORD_IDENTIFIER | RAW_IDENTIFIER
17+
>
18+
> RESERVED_RAW_IDENTIFIER : `r#_`
1719
1820
<!-- When updating the version, update the UAX links, too. -->
1921
r[ident.unicode]
@@ -71,6 +73,10 @@ r[ident.raw.allowed]
7173
Unlike a normal identifier, a raw identifier may be any strict or reserved
7274
keyword except the ones listed above for `RAW_IDENTIFIER`.
7375

76+
r[ident.raw.reserved]
77+
It is an error to use the RESERVED_RAW_IDENTIFIER token `r#_` in order to avoid confusion with the [_WildcardPattern_].
78+
79+
[_WildcardPattern_]: patterns.md#wildcard-pattern
7480
[`extern crate`]: items/extern-crates.md
7581
[`no_mangle`]: abi.md#the-no_mangle-attribute
7682
[`path` attribute]: items/modules.md#the-path-attribute

src/tokens.md

+31-2
Original file line numberDiff line numberDiff line change
@@ -758,17 +758,39 @@ r[lex.token.life.syntax]
758758
> &nbsp;&nbsp; &nbsp;&nbsp; `'` [IDENTIFIER_OR_KEYWORD][identifier]
759759
> _(not immediately followed by `'`)_\
760760
> &nbsp;&nbsp; | `'_`
761-
> _(not immediately followed by `'`)_
761+
> _(not immediately followed by `'`)_\
762+
> &nbsp;&nbsp; | RAW_LIFETIME
762763
>
763764
> LIFETIME_OR_LABEL :\
764765
> &nbsp;&nbsp; &nbsp;&nbsp; `'` [NON_KEYWORD_IDENTIFIER][identifier]
766+
> _(not immediately followed by `'`)_\
767+
> &nbsp;&nbsp; | RAW_LIFETIME
768+
>
769+
> RAW_LIFETIME :\
770+
> &nbsp;&nbsp; `'r#` [IDENTIFIER_OR_KEYWORD][identifier] <sub>*Except `crate`, `self`, `super`, `Self`*</sub>
771+
> _(not immediately followed by `'`)_
772+
>
773+
> RESERVED_RAW_LIFETIME : `'r#_`
765774
> _(not immediately followed by `'`)_
766775
767776
r[lex.token.life.intro]
768777
Lifetime parameters and [loop labels] use LIFETIME_OR_LABEL tokens. Any
769778
LIFETIME_TOKEN will be accepted by the lexer, and for example, can be used in
770779
macros.
771780

781+
r[lex.token.life.raw.intro]
782+
A raw lifetime is like a normal lifetime, but its identifier is prefixed by `r#`. (Note that the `r#` prefix is not included as part of the actual lifetime.)
783+
784+
r[lex.token.life.raw.allowed]
785+
Unlike a normal lifetime, a raw lifetime may be any strict or reserved keyword except the ones listed above for `RAW_LIFETIME`.
786+
787+
r[lex.token.life.raw.reserved]
788+
It is an error to use the RESERVED_RAW_LIFETIME token `'r#_` in order to avoid confusion with the [placeholder lifetime].
789+
790+
r[lex.token.life.raw.edition2021]
791+
> **Edition differences**: Raw lifetimes are accepted in the 2021
792+
> edition or later. In earlier additions the token `'r#lt` is lexed as `'r # lt`.
793+
772794
## Punctuation
773795

774796
r[lex.token.punct]
@@ -849,7 +871,8 @@ r[lex.token.reserved-prefix.syntax]
849871
> **<sup>Lexer 2021+</sup>**\
850872
> RESERVED_TOKEN_DOUBLE_QUOTE : ( IDENTIFIER_OR_KEYWORD <sub>_Except `b` or `c` or `r` or `br` or `cr`_</sub> | `_` ) `"`\
851873
> RESERVED_TOKEN_SINGLE_QUOTE : ( IDENTIFIER_OR_KEYWORD <sub>_Except `b`_</sub> | `_` ) `'`\
852-
> RESERVED_TOKEN_POUND : ( IDENTIFIER_OR_KEYWORD <sub>_Except `r` or `br` or `cr`_</sub> | `_` ) `#`
874+
> RESERVED_TOKEN_POUND : ( IDENTIFIER_OR_KEYWORD <sub>_Except `r` or `br` or `cr`_</sub> | `_` ) `#`\
875+
> RESERVED_TOKEN_LIFETIME : `'` (IDENTIFIER_OR_KEYWORD <sub>_Except `r`_</sub> | _) `#`
853876
854877
r[lex.token.reserved-prefix.intro]
855878
Some lexical forms known as _reserved prefixes_ are reserved for future use.
@@ -863,6 +886,9 @@ Note that raw identifiers, raw string literals, and raw byte string literals may
863886
r[lex.token.reserved-prefix.strings]
864887
Similarly the `r`, `b`, `br`, `c`, and `cr` prefixes used in raw string literals, byte literals, byte string literals, raw byte string literals, C string literals, and raw C string literals are not interpreted as reserved prefixes.
865888

889+
r[lex.token.reserved-prefix.life]
890+
Source input which would otherwise be lexically interpreted as a non-raw lifetime (or a keyword or `_`) which is immediately followed by a `#` character (without intervening whitespace) is identified as a reserved lifetime prefix.
891+
866892
r[lex.token.reserved-prefix.edition2021]
867893
> **Edition differences**: Starting with the 2021 edition, reserved prefixes are reported as an error by the lexer (in particular, they cannot be passed to macros).
868894
>
@@ -875,6 +901,7 @@ r[lex.token.reserved-prefix.edition2021]
875901
> lexes!{continue 'foo}
876902
> lexes!{match "..." {}}
877903
> lexes!{r#let#foo} // three tokens: r#let # foo
904+
> lexes!{'prefix #lt}
878905
> ```
879906
>
880907
> Examples accepted before the 2021 edition but rejected later:
@@ -883,6 +910,7 @@ r[lex.token.reserved-prefix.edition2021]
883910
> lexes!{a#foo}
884911
> lexes!{continue'foo}
885912
> lexes!{match"..." {}}
913+
> lexes!{'prefix#lt}
886914
> ```
887915
888916
[Inferred types]: types/inferred.md
@@ -924,6 +952,7 @@ r[lex.token.reserved-prefix.edition2021]
924952
[numeric types]: types/numeric.md
925953
[paths]: paths.md
926954
[patterns]: patterns.md
955+
[placeholder lifetime]: lifetime-elision.md
927956
[question]: expressions/operator-expr.md#the-question-mark-operator
928957
[range]: expressions/range-expr.md
929958
[rangepat]: patterns.md#range-patterns

0 commit comments

Comments
 (0)