skip to content

A Rust 2021 crate exports a macro that writes `let gen = $e;` and a plain `extern "C"` block; why does it compile when a 2024 crate invokes it?

level: seniorimportance: nice to knowfreq 15%

answer

  1. editions are chosen per crate
  2. tokens remember where they came from
  3. the caller's arguments differ
  4. the upgrade lands downstream

basics

~20 s

Edition hygiene: every token remembers the edition of the crate it was written in, and rustc parses it under that edition wherever it is expanded. The macro's own tokens follow 2021 rules, where gen is an identifier and extern blocks need no unsafe.

solid answer

~50 s

rustc applies **edition hygiene**: each token carries the edition of the crate whose source it came from, and the parser interprets it under that edition after expansion. The 2024 edition (stable since Rust 1.85) reserved `gen` as a keyword and requires `unsafe extern` blocks, but the macro's `let gen` and `extern "C" { ... }` tokens were written in a 2021 crate, so they parse under 2021 rules even inside a 2024 crate. Tokens the caller passes in keep the caller's edition, so a 2024 caller writing `gen` in the argument still gets an error. The trap is upgrading the macro's crate: it still builds cleanly, because rustc checks a transcriber as Rust only when it is expanded, but every caller then fails with ``expected identifier, found reserved keyword `gen` `` and `extern blocks must be unsafe`, and `cargo fix --edition` cannot fix a macro it never saw expanded.

code

rust · 17 lines
rust
// old2021/src/lib.rs, a library crate with edition = "2021"
#[macro_export]
macro_rules! double {
    ($e:expr) => {{
        let gen = $e;
        gen * 2
    }};
}

#[macro_export]
macro_rules! c_abs {
    () => {
        extern "C" {
            fn abs(x: i32) -> i32;
        }
    };
}

go deeper

for a junior

Know that editions are chosen per crate, and that crates on different editions can depend on each other and use each other's macros.

for a middle

Explain edition hygiene: tokens keep the edition of the crate they were written in, so a macro's body follows its crate's edition and the caller's arguments follow the caller's.

for a senior

Plan a macro crate's edition upgrade: rewrite bodies that use newly reserved words or now-unsafe forms, and expand every macro arm in the crate's own tests before release.

for a principal

Weigh upgrading a widely used macro crate knowing the crate builds either way and breakage lands on downstream users, so gate it on expansion tests and a clear release note.

## Editions are per crate, and tokens remember theirs An **edition** is a per-crate setting (`edition = "2024"` in the manifest, `--edition 2024` for rustc) that selects a set of language rules: which words are keywords, which forms need `unsafe`, how some fragments match. Crates on different editions link together freely, and a macro from one crate is expanded inside another, so rustc has to decide which edition applies to the expanded code. The answer is **edition hygiene**. In the words of the Edition Guide, "the tokens within a macro are marked with which edition they come from", and "the parser looks at the edition of the tokens to know how to interpret it". The edition is a property of each token, not of the crate doing the expanding. ## Walking through the example The 2021 crate exports two macros: `double!`, which expands to `{ let gen = $e; gen * 2 }`, and `c_abs!`, which expands to `extern "C" { fn abs(x: i32) -> i32; }`. A 2024 binary calls both, and rustc 1.99.1 compiles and runs it, printing 42 for `double!(21)` and 4 for `abs(-4)` called inside an `unsafe` block. - `gen` became a **reserved keyword** in the 2024 edition. The `gen` tokens here were written in the 2021 crate, so they are parsed as an ordinary identifier. - The 2024 edition requires extern blocks to be written `unsafe extern`. The `extern` token is a 2021 token, so the 2021 rule applies and no `unsafe` is needed. - The tokens the **caller** writes inside the invocation carry the **caller's** edition. A 2024 crate calling `double!(gen)` fails with ``expected expression, found reserved keyword `gen` ``, because that `gen` was written in 2024 code. The reverse also holds: a 2021 caller passing `{ let gen = 5; gen + 1 }` to a macro from a 2024 crate compiles and yields 6. The same principle decides how a `macro_rules!` matcher's fragments behave: the edition of the crate that defines the macro governs them, as the Edition Guide notes for the 2021 change to `pat`. In a test beside this example, a 2021 crate's `$e:expr` arm did not match a 2024 caller's input `const { 1 }`, while the same arm defined in a 2024 crate did; the details belong with fragment specifiers. ## The upgrade trap Edition hygiene protects callers from the macro author's edition, and it moves the cost of an upgrade onto them: 1. The macro crate changes `edition = "2021"` to `"2024"`. 2. It still builds. rustc checks a transcriber's tokens as Rust code only when the macro is expanded, and nothing in the crate expands these macros. 3. Every downstream caller now fails: ``expected identifier, found reserved keyword `gen` `` at each `double!` call and `extern blocks must be unsafe` at each `c_abs!` call, all reported at the call sites in the caller's files. 4. `cargo fix --edition` would not have helped. The Edition Guide warns that it may not fix a macro that generates syntax that no longer works, and it never inspects a macro the crate does not expand. The defence is tests that expand **every arm** of every exported macro inside the macro's own repository: unit tests, integration tests under `tests/`, or doctests, so `cargo test` parses each expansion under the new edition before release. The Edition Guide also suggests invoking the macros from crates on several editions. ## Summary table | Token written in | Parsed under | Example outcome | |---|---|---| | The 2021 macro crate's transcriber | 2021 rules | `let gen` and a plain `extern "C"` block accepted | | The 2024 caller's macro arguments | 2024 rules | `gen` rejected as a reserved keyword | | The same transcriber after the crate moves to 2024 | 2024 rules | Every call site fails until the body is rewritten | ## Rewriting for 2024 - Use `r#gen` or another name instead of `gen`. - Write `unsafe extern "C" { ... }`, which every edition has accepted since Rust 1.82. - Wrap `#[no_mangle]` and `#[export_name]` as `#[unsafe(no_mangle)]` in generated items, another 2024 rule. - Run the crate's macro tests before and after the edition change, and call the change out in the release notes, because callers see the failures, not the author. Every rewrite above is also valid on the 2021 edition, so the macro crate can adopt them first, release, and switch editions in a later version with nothing left to break.

  • A 2024 crate passes gen itself as the argument to that 2021 macro; does it compile?
    No. Tokens written inside the invocation carry the caller's edition, so that gen is a 2024 token and therefore the reserved keyword; rustc 1.99.1 reports expected expression, found reserved keyword gen at the argument. Only the tokens written in the macro's own definition keep 2021 rules. The caller can write r#gen instead.
  • How do you catch the upgrade breakage before publishing a macro crate?
    Expand every arm of every exported macro inside the crate's own repository, in unit tests, integration tests or doctests, so cargo test parses each expansion under the new edition. cargo fix --edition rewrites only code it compiles, and a macro the crate never expands is never inspected. Calling the macros from test crates on several editions covers the caller side.

  • A macro's expansion always follows the edition of the crate that invokes it.
  • Bumping a macro crate's edition is safe once that crate itself compiles cleanly.
  • cargo fix --edition rewrites every macro_rules! body in the crate it migrates.
  • Tokens a caller passes into a macro take on the macro crate's edition.
  • A 2024-edition crate cannot use macros from a crate still on the 2021 edition.

Assumes Rust 1.99 on the 2024 edition (stable since 1.85), which reserved gen and requires unsafe extern blocks; tokens from 2021-edition crates keep the older rules.