Skip to content

Commit 67f72cc

Browse files
committed
2024: Add reserved syntax
1 parent c7ebae2 commit 67f72cc

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,4 @@
5555
- [Unsafe `extern` blocks](rust-2024/unsafe-extern.md)
5656
- [Unsafe attributes](rust-2024/unsafe-attributes.md)
5757
- [Rustdoc combined tests](rust-2024/rustdoc-doctests.md)
58+
- [Reserving syntax](rust-2024/reserving-syntax.md)

src/rust-2024/reserving-syntax.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Reserved unprefixed guarded strings
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 future-proof against 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: One or more `#` immediately followed by a [string literal], and two or more `#` characters in a row.
16+
17+
This reservation is done across an edition boundary because of interactions with tokenization and macros. For example, with the following macro:
18+
19+
```rust
20+
macro_rules! demo {
21+
( $a:tt ) => { println!("one token") };
22+
( $a:tt $b:tt $c:tt ) => { println!("three tokens") };
23+
}
24+
25+
demo!("foo");
26+
demo!(r#"foo"#);
27+
demo!(#"foo"#);
28+
demo!(###)
29+
```
30+
31+
Prior to the 2024 Edition, this produces:
32+
33+
```
34+
one token
35+
one token
36+
three tokens
37+
three tokens
38+
```
39+
40+
Starting in the 2024 Edition, the `#"foo"#` line and the `###` line now generates a compile error because those forms are now reserved.
41+
42+
[2021]: ../rust-2021/reserving-syntax.md
43+
[string literal]: ../../reference/tokens.html#string-literals
44+
[RFC 3593]: https://rust-lang.github.io/rfcs/3593-unprefixed-guarded-strings.html
45+
46+
## Migration
47+
48+
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 it continues to be parsed as separate tokens.
49+
50+
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:
51+
52+
```sh
53+
cargo fix --edition
54+
```
55+
56+
Alternatively, you can manually enable the lint to find macro calls where you may need to update the tokens:
57+
58+
```rust
59+
// Add this to the root of your crate to do a manual migration.
60+
#![warn(rust_2024_guarded_string_incompatible_syntax)]
61+
```
62+
63+
[`rust_2024_guarded_string_incompatible_syntax`]: ../../rustc/lints/listing/allowed-by-default.html#rust-2024-guarded-string-incompatible-syntax

0 commit comments

Comments
 (0)