|
| 1 | +# Reserved syntax |
| 2 | + |
| 3 | +🚧 The 2024 Edition has not yet been released and hence this section is still "under construction". |
| 4 | +More information may be found in the tracking issue at <https://github.com/rust-lang/rust/issues/123735>. |
| 5 | + |
| 6 | +## Summary |
| 7 | + |
| 8 | +- Unprefixed guarded strings of the form `#"foo"#` are reserved for future use. |
| 9 | +- Two or more `#` characters are reserved for future use. |
| 10 | + |
| 11 | +## Details |
| 12 | + |
| 13 | +[RFC 3593] reserved syntax in the 2024 Edition for guarded string literals that do not have a prefix to make room for possible future language changes. The 2021 Edition [reserved syntax][2021] for guarded strings with a prefix, such as `ident##"foo"##`. The 2024 Edition extends that to also reserve strings without the `ident` prefix. |
| 14 | + |
| 15 | +There are two reserved syntaxes: |
| 16 | + |
| 17 | +- One or more `#` characters immediately followed by a [string literal]. |
| 18 | +- Two or more `#` characters in a row (not separated by whitespace). |
| 19 | + |
| 20 | +This reservation is done across an edition boundary because of interactions with tokenization and macros. For example, consider this macro: |
| 21 | + |
| 22 | +```rust |
| 23 | +macro_rules! demo { |
| 24 | + ( $a:tt ) => { println!("one token") }; |
| 25 | + ( $a:tt $b:tt $c:tt ) => { println!("three tokens") }; |
| 26 | +} |
| 27 | + |
| 28 | +demo!("foo"); |
| 29 | +demo!(r#"foo"#); |
| 30 | +demo!(#"foo"#); |
| 31 | +demo!(###) |
| 32 | +``` |
| 33 | + |
| 34 | +Prior to the 2024 Edition, this produces: |
| 35 | + |
| 36 | +```text |
| 37 | +one token |
| 38 | +one token |
| 39 | +three tokens |
| 40 | +three tokens |
| 41 | +``` |
| 42 | + |
| 43 | +Starting in the 2024 Edition, the `#"foo"#` line and the `###` line now generates a compile error because those forms are now reserved. |
| 44 | + |
| 45 | +[2021]: ../rust-2021/reserved-syntax.md |
| 46 | +[string literal]: ../../reference/tokens.html#string-literals |
| 47 | +[RFC 3593]: https://rust-lang.github.io/rfcs/3593-unprefixed-guarded-strings.html |
| 48 | + |
| 49 | +## Migration |
| 50 | + |
| 51 | +The [`rust_2024_guarded_string_incompatible_syntax`] lint will identify any tokens that match the reserved syntax, and will suggest a modification to insert spaces where necessary to ensure the tokens continue to be parsed separately. |
| 52 | + |
| 53 | +The lint is part of the `rust-2024-compatibility` lint group which is included in the automatic edition migration. In order to migrate your code to be Rust 2024 Edition compatible, run: |
| 54 | + |
| 55 | +```sh |
| 56 | +cargo fix --edition |
| 57 | +``` |
| 58 | + |
| 59 | +Alternatively, you can manually enable the lint to find macro calls where you may need to update the tokens: |
| 60 | + |
| 61 | +```rust |
| 62 | +// Add this to the root of your crate to do a manual migration. |
| 63 | +#![warn(rust_2024_guarded_string_incompatible_syntax)] |
| 64 | +``` |
| 65 | + |
| 66 | +[`rust_2024_guarded_string_incompatible_syntax`]: ../../rustc/lints/listing/allowed-by-default.html#rust-2024-guarded-string-incompatible-syntax |
0 commit comments