Skip to content

Commit 6bc87e2

Browse files
authored
Merge pull request #1128 from mattheww/2022-01_reserved_syntax
Document syntax reserved in Rust 2021
2 parents f8ba2f1 + be704f3 commit 6bc87e2

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

src/tokens.md

+37-2
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@ evaluated (primarily) at compile time.
8888

8989
#### Suffixes
9090

91-
A suffix is a non-raw identifier immediately (without whitespace)
92-
following the primary part of a literal.
91+
A suffix is a sequence of characters following the primary part of a literal (without intervening whitespace), of the same form as a non-raw identifier or keyword.
9392

9493
Any kind of literal (string, integer, etc) with any suffix is valid as a token,
9594
and can be passed to a macro without producing an error.
@@ -659,3 +658,39 @@ them are referred to as "token trees" in [macros]. The three types of brackets
659658
[use declarations]: items/use-declarations.md
660659
[use wildcards]: items/use-declarations.md
661660
[while let]: expressions/loop-expr.md#predicate-pattern-loops
661+
662+
## Reserved prefixes
663+
664+
> **<sup>Lexer 2021+</sup>**\
665+
> RESERVED_TOKEN_DOUBLE_QUOTE : ( IDENTIFIER_OR_KEYWORD <sub>_Except `b` or `r` or `br`_</sub> | `_` ) `"`\
666+
> RESERVED_TOKEN_SINGLE_QUOTE : ( IDENTIFIER_OR_KEYWORD <sub>_Except `b`_</sub> | `_` ) `'`\
667+
> RESERVED_TOKEN_POUND : ( IDENTIFIER_OR_KEYWORD <sub>_Except `r` or `br`_</sub> | `_` ) `#`
668+
669+
Some lexical forms known as _reserved prefixes_ are reserved for future use.
670+
671+
Source input which would otherwise be lexically interpreted as a non-raw identifier (or a keyword or `_`) which is immediately followed by a `#`, `'`, or `"` character (without intervening whitespace) is identified as a reserved prefix.
672+
673+
Note that raw identifiers, raw string literals, and raw byte string literals may contain a `#` character but are not interpreted as containing a reserved prefix.
674+
675+
Similarly the `r`, `b`, and `br` prefixes used in raw string literals, byte literals, byte string literals, and raw byte string literals are not interpreted as reserved prefixes.
676+
677+
> **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).
678+
>
679+
> Before the 2021 edition, a reserved prefixes are accepted by the lexer and interpreted as multiple tokens (for example, one token for the identifier or keyword, followed by a `#` token).
680+
>
681+
> Examples accepted in all editions:
682+
> ```rust
683+
> macro_rules! lexes {($($_:tt)*) => {}}
684+
> lexes!{a #foo}
685+
> lexes!{continue 'foo}
686+
> lexes!{match "..." {}}
687+
> lexes!{r#let#foo} // three tokens: r#let # foo
688+
> ```
689+
>
690+
> Examples accepted before the 2021 edition but rejected later:
691+
> ```rust,edition2018
692+
> macro_rules! lexes {($($_:tt)*) => {}}
693+
> lexes!{a#foo}
694+
> lexes!{continue'foo}
695+
> lexes!{match"..." {}}
696+
> ```

0 commit comments

Comments
 (0)