As the lead migrating a forty-crate Rust service workspace to the 2024 edition, how would you sequence it, and which `cargo fix` changes need human review?
answer
- each package carries its own edition
- fix, commit, flip, test, format
- every feature set and every target
- preserving rewrites versus unfixable lints
basics
~20 sMigrate one package at a time: run cargo fix --edition for every feature set and target, commit, set edition = "2024", test including doctests, then reformat separately. Review what fix rewrote to preserve old meaning and fix by hand what it cannot rewrite.
solid answer
~40 sEach package in a Cargo workspace declares its own edition, and crates of different editions link into one binary, so forty crates can move one at a time with no flag day. For each crate: `cargo update`, then `cargo fix --edition -p <crate> --all-features`, repeated with `--target` for platform-gated code; commit; set `edition = "2024"`; run `cargo test`, doctests included; reformat in its own commit. Order crates by risk, not by the dependency graph. The review list splits in two. Preserving rewrites to revisit: `if let` turned into `match`, `+ use<>` bounds, `::<()>` never-type annotations, `.iter()` on boxed slices, `expr_2021` fragments. Changes fix cannot make safely: unsafe extern signatures and unsafe attributes (keywords added, nothing verified), `set_var` audits, `static mut` references, tail-expression drop-order warnings, doctests, macros and generated code.
go deeper
Recall the per-crate steps: cargo fix --edition, set the edition in Cargo.toml, run the tests, and that each crate in a workspace has its own edition.
Explain why fixes are committed before the edition flip, why cargo fix needs every feature set and target, and which places it never reaches, such as doctests and macros.
Show the review: which preserving rewrites to revert, which unsafe additions need an audit, and how to triage static_mut_refs and tail_expr_drop_order findings.
Own the plan: order crates by risk and ownership, time-box the mixed-edition period, separate the toolchain upgrade, and decide when a crate may stay on 2021.
## Why crate by crate works The **edition** is a per-package setting in `Cargo.toml`, and rustc compiles each crate with its own edition. Crates on 2018, 2021 and 2024 link into one program, which is the material on per-crate editions; for a workspace it means there is no need for a flag day. A forty-crate service can sit half-migrated for weeks while feature work continues. Two constraints still apply: every toolchain that builds a 2024 crate must be Rust 1.85 or later, and the edition switch also changes Cargo's default resolver for the root package, a manifest question covered with Cargo's resolver. ## A sequence per crate 1. **Update dependencies** with `cargo update` and run the tests, committing that separately. Proc macros and code generators sometimes need a newer release to cope with a new edition. 2. **Run the fixer for every configuration.** `cargo fix --edition -p <crate> --all-features` only rewrites code compiled in that run, so code behind a Cargo feature is skipped without `--all-features`, and code behind `#[cfg(target_os = ...)]` needs a run per `--target`. 3. **Commit the fixes on the old edition.** After `cargo fix --edition` the code compiles on both editions, so the diff is reviewable and bisectable on its own. 4. **Flip the edition** to `edition = "2024"` by hand. `cargo fix --edition` never edits the edition key. 5. **Build and test**, including doctests, and fix what surfaces. 6. **Reformat in a separate commit**, because the 2024 style edition changes `cargo fmt` output and mixing it in buries the semantic diff. ## What the fixer rewrote that you should revisit Most automatic edits **preserve the 2021 meaning**, which is not always what you want on 2024: | Rewrite | Why it was made | Review question | |---|---|---| | `if let` turned into `match` | keeps the old, longer temporary scope | Was holding the temporary through `else` a bug? Then revert to `if let`. | | `+ use<>` on `-> impl Trait` | keeps 2021 lifetime capture | On public APIs, keep the promise or reserve the right to borrow later? | | `::<()>` inserted at generic calls | keeps the `()` never-type fallback | Name the real type instead? | | `boxed.into_iter()` turned into `.iter()` | keeps by-reference iteration | Would owned items be better? | | `$e:expr` turned into `$e:expr_2021` | keeps old macro matching | The edition guide expects most macros to go back to `expr`. | ## What the fixer cannot do safely These need an owner who understands the code: - **Unsafe extern blocks and unsafe attributes.** The fixer adds `unsafe` and `#[unsafe(...)]` but checks no signature and no symbol name; the people who own the FFI code should audit them and decide which items may be declared `safe`. - **`std::env::set_var` and `remove_var`**, now unsafe functions. The fixer wraps calls in `unsafe { }` under a `// FIXME: Audit that the environment access only happens in single-threaded code.` comment. In a multi-threaded service that audit is real work, and the answer is often to stop mutating the environment. - **References to `static mut`.** The `static_mut_refs` lint is deny on 2024 and has no automatic fix; the static needs a redesign with atomics, a lock, a `LazyLock` or raw pointers. - **Tail-expression drop order.** `tail_expr_drop_order` only warns; check each warning where guards or span objects interact. - **Unconverted argument-position `impl Trait` returns.** The capture lint warns but cannot add `use<..>`. ## What the fixer never sees - **Doctests.** `cargo fix` does not update them; a doctest using `gen` as an identifier fails only after the flip. On 2024 rustdoc also merges doctests into one binary where it can, so tests that check `std::panic::Location` or `std::any::type_name` may need the `standalone_crate` tag. - **Macros.** `macro_rules!` macros are fixed automatically only in some cases, typically when used inside their own crate, and proc macros never are. Test them from crates on several editions. - **Generated code** from build scripts, which must be changed in the generator. ## The judgment calls - **Order:** start with small, low-risk crates to calibrate the process, then schedule the crates with the most unsafe, FFI or macro code when their owners can review. - **Pace:** time-box the mixed state, but do not force a crate through during a release freeze; 2021 remains fully supported. - **Toolchain first:** upgrading rustc can itself break builds, because since Rust 1.92 the never-type fallback lints are deny-by-default on every edition. Land that separately. - **Library consumers:** moving a published library to 2024 raises the minimum toolchain its users need to 1.85, a policy decision covered with MSRV.
- Why commit `cargo fix --edition`'s changes before setting `edition = "2024"`?After the fixer runs, the code compiles on both the old and the new edition, so the commit stands alone: reviewers see only the mechanical rewrites, CI proves nothing broke on the old edition, and a later regression can be bisected to either the rewrites or the edition switch. Mixing both into one commit hides which change caused a failure.
- When would you leave a crate on the 2021 edition for now?When the cost outweighs the benefit: heavy exported `macro_rules!` or proc-macro code that is hard to test across editions, generated code whose generator you do not control, or a published library whose users still need toolchains older than 1.85. Editions are not deprecated, so a 2021 crate keeps working and links with 2024 crates; the risk is only drift from current idioms.
- What should the owners of FFI crates check after the migration?Every `unsafe extern` declaration against the C headers, since the fixer only added the keyword; which foreign functions truly have no preconditions and can be declared `safe`; a `// SAFETY:` justification for each `#[unsafe(no_mangle)]` or `#[unsafe(export_name)]`; and any `static mut` the deny lint flagged, which needs a raw-pointer or interior-mutability redesign.
- All forty crates must switch editions in one commit
- cargo fix --edition also updates the edition key in Cargo.toml
- A clean cargo fix run means no manual work remains
- cargo fix migrates doctests and code behind disabled features
- Every rewrite cargo fix makes should be kept as it is
Assumes Rust 1.99 and Cargo 1.99.1 migrating to the 2024 edition (stable since 1.85); the lint names and cargo fix behaviour are those of the 1.99 toolchain.