Skip to content

Commit 1c65098

Browse files
committed
Merge PR #312: Add macro-fragment-specifiers
2 parents cb41a09 + dbc3ebe commit 1c65098

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed
+52-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,54 @@
11
# Macro Fragment Specifiers
22

3-
This is a stub. Help us to improve it by creating a pull request!
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/123742>.
5+
6+
## Summary
7+
8+
- The `expr` [fragment specifier] now also supports `const` and `_` expressions.
9+
- The `expr_2021` fragment specifier has been added for backwards compatibility.
10+
11+
[fragment specifier]: ../../reference/macros-by-example.html#metavariables
12+
13+
## Details
14+
15+
As new syntax is added to Rust, existing `macro_rules` fragment specifiers are sometimes not allowed to match on the new syntax in order to retain backwards compatibility. Supporting the new syntax in the old fragment specifiers is sometimes deferred until the next edition, which provides an opportunity to update them.
16+
17+
Indeed this happened with [`const` expressions] added in 1.79 and [`_` expressions] added in 1.59. In the 2021 Edition and earlier, the `expr` fragment specifier does *not* match those expressions. This is because you may have a scenario like:
18+
19+
```rust,edition2021
20+
macro_rules! example {
21+
($e:expr) => { println!("first rule"); };
22+
(const $e:expr) => { println!("second rule"); };
23+
}
24+
25+
fn main() {
26+
example!(const { 1 + 1 });
27+
}
28+
```
29+
30+
Here, in the 2021 Edition, the macro will match the *second* rule. If earlier editions had changed `expr` to match the newly introduced `const` expressions, then it would match the *first* rule, which would be a breaking change.
31+
32+
In the 2024 Edition, `expr` specifiers now also match `const` and `_` expressions. To support the old behavior, the `expr_2021` fragment specifier has been added which does *not* match the new expressions.
33+
34+
[`const` expressions]: ../../reference/expressions/block-expr.html#const-blocks
35+
[`_` expressions]: ../../reference/expressions/underscore-expr.html
36+
37+
## Migration
38+
39+
The [`edition_2024_expr_fragment_specifier`] lint will change all uses of the `expr` specifier to `expr_2021` to ensure that the behavior of existing macros does not change. 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:
40+
41+
```sh
42+
cargo fix --edition
43+
```
44+
45+
In *most* cases, you will likely want to keep the `expr` specifier instead, in order to support the new expressions. You will need to review your macro to determine if there are other rules that would otherwise match with `const` or `_` and determine if there is a conflict. If you want the new behavior, just revert any changes made by the lint.
46+
47+
Alternatively, you can manually enable the lint to find macros where you may need to update the `expr` specifier.
48+
49+
```rust
50+
// Add this to the root of your crate to do a manual migration.
51+
#![warn(edition_2024_expr_fragment_specifier)]
52+
```
53+
54+
[`edition_2024_expr_fragment_specifier`]: ../../rustc/lints/listing/allowed-by-default.html#edition-2024-expr-fragment-specifier

0 commit comments

Comments
 (0)