skip to content

In Rust 2024, why can a macro_rules! macro send a const { … } or _ argument to a different arm, and what does expr_2021 keep?

level: middleimportance: should knowfreq 30%

answer

  1. an edition widened one fragment
  2. const blocks and underscore
  3. the first matching arm wins
  4. the defining crate's edition counts

basics

~20 s

In the 2024 edition the expr fragment also matches const blocks and _ expressions, so an earlier $e:expr arm can now win over a later const or _ arm. expr_2021 keeps the old matching and exists for migration.

solid answer

~40 s

The 2024 edition, stable since Rust 1.85, widened the `expr` fragment specifier: it now also matches top-level const block expressions (`const { 1 + 1 }`, stable since 1.79) and underscore expressions (`_`). Because arms are tried in order, a macro with `($e:expr)` before `(const $b:block)` sends `const { 1 + 1 }` to the second arm on 2021 and to the first on 2024; rustc 1.99.1 shows exactly that. `expr_2021`, accepted in every edition since 1.83, matches what `expr` matched before. `cargo fix --edition` runs the `edition_2024_expr_fragment_specifier` lint, which rewrites `expr` to `expr_2021` so behaviour is preserved; the edition guide advises reverting that where no conflicting arm exists. The edition that applies is that of the crate defining the macro, not the caller's.

code

rust · 17 lines
rust
macro_rules! classify {
    ($e:expr) => { "expr arm" };
    (const $b:block) => { "const arm" };
    (_) => { "underscore arm" };
}

macro_rules! classify_2021 {
    ($e:expr_2021) => { "expr arm" };
    (const $b:block) => { "const arm" };
    (_) => { "underscore arm" };
}

fn main() {
    println!("{} | {}", classify!(const { 1 + 1 }), classify_2021!(const { 1 + 1 }));
    println!("{} | {}", classify!(_), classify_2021!(_));
    println!("{}", classify!(2 + 2));
}

go deeper

for a junior

Know that editions can change small language rules and that the 2024 edition changed what a macro's expr fragment accepts.

for a middle

Explain which expressions expr gained in 2024, why that can move a call to a different arm, and what expr_2021 and the migration lint do.

for a senior

Audit macros during an edition migration: find expr arms ahead of const or _ arms, keep expr_2021 only where it matters, and know that the defining crate's edition governs.

for a principal

Plan edition migrations of shared macro crates as API changes, reorder arms so specific ones come first, and avoid leaving expr_2021 everywhere as unexplained migration residue.

## What changed An **edition** lets Rust change a rule without breaking existing crates: each crate declares its edition, and the old rule keeps applying to old crates. The **2024 edition**, stable since **Rust 1.85**, used that to widen the `expr` **fragment specifier** of `macro_rules!`. The edition guide puts it in two lines: the `expr` fragment specifier now also supports `const` and `_` expressions, and the `expr_2021` specifier has been added for backwards compatibility. The two expressions involved are newer than the fragment's definition: - **Underscore expressions**, `_` on the left of a destructuring assignment such as `(a, _) = pair;`, which the edition guide dates to 1.59. - **Const block expressions**, `const { ... }`, stabilised in **1.79** ("Stabilize inline `const {}` expressions" in the release notes). The Reference records the rule: before the 2024 edition, `expr` does not match an underscore expression or a const block at the **top level**, though both are allowed inside larger expressions. ## Why it can change which arm runs A macro's arms are tried in order, and the first arm that matches is expanded. Take a macro written when `expr` did not match const blocks: ```rust macro_rules! classify { ($e:expr) => { "expr arm" }; (const $b:block) => { "const arm" }; (_) => { "underscore arm" }; } ``` | Call | Defined in a 2021 crate | Defined in a 2024 crate | |---|---|---| | `classify!(const { 1 + 1 })` | const arm | expr arm | | `classify!(_)` | underscore arm | expr arm | | `classify!(2 + 2)` | expr arm | expr arm | These results come from rustc 1.99.1. On 2021, the first arm fails because `expr` refuses a top-level const block, so rustc tries the second arm. On 2024, the first arm accepts it and the later arms are never reached. Silent arm changes like this are why the widening waited for an edition instead of shipping in a regular release. ## `expr_2021` and the migration lint - **`expr_2021`** matches an expression except an underscore expression or a const block, the pre-2024 behaviour. The release notes for **1.83** record it as stabilised in all editions, so a macro can use it on 2021 or 2024 alike. - **`edition_2024_expr_fragment_specifier`** is a lint, allow-by-default and part of the `rust-2024-compatibility` group, so `cargo fix --edition` runs it. It rewrites every `$x:expr` to `$x:expr_2021`, with rustc's help text: "to keep the existing behavior, use the `expr_2021` fragment specifier". - The edition guide's advice is to review afterwards: in most macros there is no competing `const` or `_` arm, and plain `expr` is what you want, so revert the lint's change there. ## Which crate's edition counts The fragment's meaning follows the edition of the crate that **defines** the macro. A macro defined in a 2021 library keeps the old `expr` even when a 2024 crate invokes it: compiled with rustc 1.99.1, the call `lib21::classify!(const { 1 + 1 })` from a 2024 binary still reaches the const arm. Migrating a consumer crate therefore never changes a dependency's macros; migrating the library does. How editions apply inside expansions in general is covered in the lesson on macro hygiene and scope. ## A review checklist when moving a crate to 2024 1. Search the crate for `macro_rules!` arms that use `$x:expr` before an arm starting with `const` or `_`. 2. For each, decide whether `const { ... }` and `_` should reach the specific arm; if yes, keep `expr_2021` on the earlier arm or reorder the arms so the specific ones come first. 3. Elsewhere, revert `expr_2021` to `expr`, so callers can pass const blocks to your macros. 4. Note the earlier precedent: the 2021 edition did the same thing for `pat`, which began matching top-level or-patterns, with `pat_param` keeping the old meaning. The lesson for macro authors is that arm order encodes assumptions about what each fragment accepts. Putting the most specific arms first makes a macro robust to fragments that grow in a future edition.

  • If my binary moves to 2024, do the macros of a 2021 dependency change behaviour?
    No. The meaning of `expr` follows the edition of the crate that defines the macro, so a 2021 library's macros keep the old matching when a 2024 crate calls them. Only migrating the library itself changes them, and its own `cargo fix --edition` run will offer `expr_2021` where needed.
  • Should I keep the expr_2021 that cargo fix inserted?
    Only where a later arm handles `const` or `_` specially, or the macro would otherwise misbehave on them. Elsewhere, the edition guide recommends reverting to plain `expr`, so callers can pass `const { ... }` and `_` like any other expression.

  • The 2024 edition removed the plain expr fragment specifier
  • A 2024 caller changes how a 2021 library's macros match
  • expr_2021 only compiles in crates on the 2021 edition
  • cargo fix leaves macro fragment specifiers untouched
  • expr in 2021 rejected const blocks even inside larger expressions

Assumes Rust 1.99 on the 2024 edition (stable since 1.85); expr_2021 is accepted in every edition since 1.83.