Skip to content

Commit 915f9b3

Browse files
authored
Merge pull request #330 from ehuss/2021-raw-lifetime
2021: Update for raw lifetimes
2 parents 3c25b2e + c4c1c79 commit 915f9b3

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-5
lines changed

src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
- [Disjoint capture in closures](rust-2021/disjoint-capture-in-closures.md)
3232
- [Panic macro consistency](rust-2021/panic-macro-consistency.md)
3333
- [Reserved syntax](rust-2021/reserved-syntax.md)
34+
- [Raw lifetimes](rust-2021/raw-lifetimes.md)
3435
- [Warnings promoted to errors](rust-2021/warnings-promoted-to-error.md)
3536
- [Or patterns in macro-rules](rust-2021/or-patterns-macro-rules.md)
3637
- [C-string literals](rust-2021/c-string-literals.md)

src/rust-2021/raw-lifetimes.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Raw lifetimes
2+
3+
## Summary
4+
5+
- `'r#ident_or_keyword` is now allowed as a lifetime, which allows using keywords such as `'r#fn`.
6+
7+
## Details
8+
9+
Raw lifetimes are introduced in the 2021 edition to support the ability to migrate to newer editions that introduce new keywords. This is analogous to [raw identifiers] which provide the same functionality for identifiers. For example, the 2024 edition introduced the `gen` keyword. Since lifetimes cannot be keywords, this would cause code that use a lifetime `'gen` to fail to compile. Raw lifetimes allow the migration lint to modify those lifetimes to `'r#gen` which do allow keywords.
10+
11+
In editions prior to 2021, raw lifetimes are parsed as separate tokens. For example `'r#foo` is parsed as three tokens: `'r`, `#`, and `foo`.
12+
13+
[raw identifiers]: ../../reference/identifiers.html#raw-identifiers
14+
15+
## Migration
16+
17+
As a part of the 2021 edition a migration lint, [`rust_2021_prefixes_incompatible_syntax`], has been added in order to aid in automatic migration of Rust 2018 codebases to Rust 2021.
18+
19+
In order to migrate your code to be Rust 2021 Edition compatible, run:
20+
21+
```sh
22+
cargo fix --edition
23+
```
24+
25+
Should you want or need to manually migrate your code, migration is fairly straight-forward.
26+
27+
Let's say you have a macro that is defined like so:
28+
29+
```rust
30+
macro_rules! my_macro {
31+
($a:tt $b:tt $c:tt) => {};
32+
}
33+
```
34+
35+
In Rust 2015 and 2018 it's legal for this macro to be called like so with no space between the tokens:
36+
37+
```rust,ignore
38+
my_macro!('r#foo);
39+
```
40+
41+
In the 2021 edition, this is now parsed as a single token. In order to call this macro, you must add a space before the identifier like so:
42+
43+
```rust,ignore
44+
my_macro!('r# foo);
45+
```
46+
47+
[`rust_2021_prefixes_incompatible_syntax`]: ../../rustc/lints/listing/allowed-by-default.html#rust-2021-prefixes-incompatible-syntax

src/rust-2021/reserved-syntax.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
## Summary
44

5-
- `any_identifier#`, `any_identifier"..."`, and `any_identifier'...'` are now reserved
6-
syntax, and no longer tokenize.
5+
- `any_identifier#`, `any_identifier"..."`, `any_identifier'...'`, and `'any_identifier#` are now reserved syntax, and no longer tokenize.
76
- This is mostly relevant to macros. E.g. `quote!{ #a#b }` is no longer accepted.
87
- It doesn't treat keywords specially, so e.g. `match"..." {}` is no longer accepted.
98
- Insert whitespace between the identifier and the subsequent `#`, `"`, or `'`
@@ -13,8 +12,8 @@
1312
## Details
1413

1514
To make space for new syntax in the future,
16-
we've decided to reserve syntax for prefixed identifiers and literals:
17-
`prefix#identifier`, `prefix"string"`, `prefix'c'`, and `prefix#123`,
15+
we've decided to reserve syntax for prefixed identifiers, literals, and lifetimes:
16+
`prefix#identifier`, `prefix"string"`, `prefix'c'`, `prefix#123`, and `'prefix#`,
1817
where `prefix` can be any identifier.
1918
(Except those prefixes that already have a meaning, such as `b'...'` (byte
2019
chars) and `r"..."` (raw strings).)
@@ -52,7 +51,7 @@ committed to any of them yet):
5251

5352
## Migration
5453

55-
As a part of the 2021 edition a migration lint, `rust_2021_prefixes_incompatible_syntax`, has been added in order to aid in automatic migration of Rust 2018 codebases to Rust 2021.
54+
As a part of the 2021 edition a migration lint, [`rust_2021_prefixes_incompatible_syntax`], has been added in order to aid in automatic migration of Rust 2018 codebases to Rust 2021.
5655

5756
In order to migrate your code to be Rust 2021 Edition compatible, run:
5857

@@ -81,3 +80,5 @@ This `z` prefix is no longer allowed in Rust 2021, so in order to call this macr
8180
```rust,ignore
8281
my_macro!(z "hey");
8382
```
83+
84+
[`rust_2021_prefixes_incompatible_syntax`]: ../../rustc/lints/listing/allowed-by-default.html#rust-2021-prefixes-incompatible-syntax

0 commit comments

Comments
 (0)