After moving a Rust library from 2018 to 2021 with a clean `cargo fix --edition` and green tests, a downstream crate fails inside your exported `macro_rules!` macro; why?
answer
- whose edition do macro tokens carry?
- lints fire on expansions
- nobody expanded it in the crate
- panic!($e) under 2021 rules
basics
~20 sA macro's tokens carry the edition of the crate that defines it, so bumping the library changed the macro's body to 2021 rules for every caller. cargo fix only sees macros expanded inside the crate, so an uninvoked exported macro migrates silently.
solid answer
~40 sMacro tokens carry the edition of the crate they were written in, wherever they expand. Before the bump, the library's `macro_rules! fail { ($e:expr) => { panic!($e) } }` wrote a 2018 `panic!`, which accepted a `String` as its only argument. After the bump the same tokens are 2021, where `panic!`'s first argument must be a format string, so a caller's `fail!(reason)` fails with "format argument must be a string literal", whatever the caller's edition. `cargo fix --edition` never warned because its lints fire on expansions and nothing in the library expanded the macro. Fix the macro so it means the same on every edition, here `panic!("{}", $e)`, and add a test that invokes it, which makes the migration lint visible next time.
code
rust · 17 lines// alerts: edition 2021, macro rewritten so it means the same on every edition
#[macro_export]
macro_rules! fail {
($e:expr) => {
panic!("{}", $e)
};
}
#[cfg(test)]
mod tests {
#[test]
#[should_panic(expected = "disk full")]
fn fail_accepts_a_runtime_string() {
let reason = String::from("disk full");
fail!(reason);
}
}go deeper
Recall that macros are expanded where they are called but keep the rules of the crate that defined them.
Explain edition hygiene: each token remembers its source crate's edition, so a 2021 macro body behaves as 2021 code inside a 2018 caller.
Diagnose the silent break: migration lints fire on expansions, an uninvoked exported macro has none, and in-crate tests plus cross-edition test crates expose it before release.
Treat exported macros as part of a library's public surface during an edition bump, with tests from the oldest supported edition and a reviewed macro expansion before release.
## The failure A utility library exports: ```rust #[macro_export] macro_rules! fail { ($e:expr) => { panic!($e) }; } ``` On the 2018 edition this compiles, and callers write `fail!(reason)` with a `String`. The maintainer runs `cargo fix --edition`, sees `Fixed src/lib.rs (4 fixes)` with no warnings, switches the library to `edition = "2021"`, and the library's tests pass. The next release breaks a downstream crate still on 2018: ```plaintext error: format argument must be a string literal --> src/main.rs:5:23 | 5 | alerts::fail!(reason); | ^^^^^^ ``` A downstream crate on 2024 fails identically. Before the bump both callers compiled. ## Why: macros keep their own crate's edition The compiler uses **edition hygiene** for macros: every token is tagged with the edition of the crate whose source it came from, and the parser interprets it by that tag wherever it expands. The rule exists so that a macro written for one edition works when called from any other. - The `panic!` token inside `fail!` comes from the library, so it is interpreted with the **library's** edition, not the caller's. - Under 2018 rules, `panic!` with one argument took it as the panic payload, so a `String` was accepted, and did not treat it as a format string. - Under 2021 rules, `panic!` always formats like `println!`, so its first argument must be a string literal; a non-string payload needs `std::panic::panic_any`. So the edition bump changed the meaning of the macro **for every caller at once**, and the caller's own edition is irrelevant. The reverse also holds: with the library still on 2018, a 2024 caller's `fail!(reason)` compiles, because the tokens are 2018 tokens. ## Why the rule is designed this way If a macro used its caller's edition, every published macro would have to be valid on all editions at once, and a crate that moved to a new edition could break macros it merely calls. Tying tokens to the defining crate keeps each crate's edition choice its own: a library written for 2015 keeps working for callers on 2024. The flip side is the case here. When the defining crate changes its edition, its macros change with it, and the effect lands in other people's crates. ## Why `cargo fix --edition` stayed silent The migration lints, here `non_fmt_panics`, run on **expanded code**. `cargo fix --edition` checks the crate's own targets, and if nothing in the library invokes `fail!`, no expansion exists and no lint fires. The edition guide warns about this case directly: for a macro defined but never called in the migrating crate, Cargo displays no warnings or errors, yet the macro stops working when called from any crate. When the library *does* invoke the macro, in a unit test for example, `cargo fix --edition` prints "panic message is not a string literal" at that call and leaves it unfixed, and after the bump the library's own `cargo test` fails. A test of the macro turns a silent downstream break into a local one. ## The fix Write the macro so its expansion means the same thing on every edition: - `panic!("{}", $e)` — formats any `Display` value, valid on 2018 and 2021 alike; - `::std::panic::panic_any($e)` — if callers depend on a non-string panic payload. After the change, callers on 2018 and 2024 compile and the library's test passes. ## How to prevent it 1. **Invoke every exported macro inside the crate**, in unit tests, before migrating, so the migration lints see each expansion. 2. **Test macros from crates on other editions**, as the edition guide recommends: a small test crate on the oldest edition you support catches what in-crate tests cannot. 3. **Treat proc macros separately**: `cargo fix` cannot fix them at all, so review the code they generate against the new edition by hand. 4. **Remember doctests**, which `cargo fix` never updates, and which may hold the only call to a macro. 5. **Review before release**: an edition bump in a library with exported macros deserves a careful look at what those macros generate before release. ## Where the border is General macro hygiene — local variable names, `$crate::` paths, textual scoping — belongs to the material on macro hygiene; fragment specifiers such as `pat` versus `pat_param` belong to declarative macros. This question is about the edition tag a macro's tokens carry and the blind spot it creates in an edition migration.
- If the library stays on 2018, can a 2024 crate call `fail!(reason)`?Yes. The macro's `panic!` token was written in a 2018 crate, so it is interpreted with 2018 rules even when it expands inside a 2024 crate; rustc 1.99.1 compiles that call. Edition hygiene is what lets old macros keep working for new callers, and it is the same mechanism that breaks callers when the defining crate moves.
- Can `cargo fix --edition` fix a macro when the crate does invoke it?Sometimes. The edition guide says a `macro_rules!` macro used within the same crate can sometimes be updated automatically, but not always. In this case `cargo fix --edition` printed "panic message is not a string literal" at the call inside the test and left the code unchanged, so the macro still needed a manual fix, but the problem was visible before release.
- A macro expands with the caller's edition rules.
- Green library tests after cargo fix --edition prove exported macros still work.
- cargo fix --edition warns about every exported macro it cannot migrate.
- A library's edition bump cannot change how its exported macros behave for callers.
- cargo fix can migrate proc macros the same way as ordinary code.