Skip to content

Add -Zfix-edition #15596

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletions src/bin/cargo/commands/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,24 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {

let allow_dirty = args.flag("allow-dirty");

ops::fix(
gctx,
&ws,
&root_manifest,
&mut ops::FixOptions {
edition: args.flag("edition"),
idioms: args.flag("edition-idioms"),
compile_opts: opts,
allow_dirty,
allow_staged: allow_dirty || args.flag("allow-staged"),
allow_no_vcs: args.flag("allow-no-vcs"),
broken_code: args.flag("broken-code"),
requested_lockfile_path: lockfile_path,
},
)?;
let mut opts = ops::FixOptions {
edition: args
.flag("edition")
.then_some(ops::EditionFixMode::NextRelative),
idioms: args.flag("edition-idioms"),
compile_opts: opts,
allow_dirty,
allow_staged: allow_dirty || args.flag("allow-staged"),
allow_no_vcs: args.flag("allow-no-vcs"),
broken_code: args.flag("broken-code"),
requested_lockfile_path: lockfile_path,
};

if let Some(fe) = &gctx.cli_unstable().fix_edition {
ops::fix_edition(gctx, &ws, &mut opts, fe)?;
} else {
ops::fix(gctx, &ws, &mut opts)?;
}

Ok(())
}
46 changes: 46 additions & 0 deletions src/cargo/core/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,45 @@ impl FromStr for Edition {
}
}

/// The value for `-Zfix-edition`.
#[derive(Debug, Deserialize)]
pub enum FixEdition {
/// `-Zfix-edition=start=$INITIAL`
///
/// This mode for `cargo fix` will just run `cargo check` if the current
/// edition is equal to this edition. If it is a different edition, then
/// it just exits with success. This is used for crater integration which
/// needs to set a baseline for the "before" toolchain.
Start(Edition),
/// `-Zfix-edition=end=$INITIAL,$NEXT`
///
/// This mode for `cargo fix` will migrate to the `next` edition if the
/// current edition is `initial`. After migration, it will update
/// `Cargo.toml` and verify that that it works on the new edition. If the
/// current edition is not `initial`, then it immediately exits with
/// success since we just want to ignore those packages.
End { initial: Edition, next: Edition },
}

impl FromStr for FixEdition {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, <Self as FromStr>::Err> {
if let Some(start) = s.strip_prefix("start=") {
Ok(FixEdition::Start(start.parse()?))
} else if let Some(end) = s.strip_prefix("end=") {
let (initial, next) = end
.split_once(',')
.ok_or_else(|| anyhow::format_err!("expected `initial,next`"))?;
Ok(FixEdition::End {
initial: initial.parse()?,
next: next.parse()?,
})
} else {
bail!("invalid `-Zfix-edition, expected start= or end=, got `{s}`");
}
}
}

#[derive(Debug, PartialEq)]
enum Status {
Stable,
Expand Down Expand Up @@ -791,6 +830,7 @@ unstable_cli_options!(
dual_proc_macros: bool = ("Build proc-macros for both the host and the target"),
feature_unification: bool = ("Enable new feature unification modes in workspaces"),
features: Option<Vec<String>>,
fix_edition: Option<FixEdition> = ("Permanently unstable edition migration helper"),
gc: bool = ("Track cache usage and \"garbage collect\" unused files"),
#[serde(deserialize_with = "deserialize_git_features")]
git: Option<GitFeatures> = ("Enable support for shallow git fetch operations"),
Expand Down Expand Up @@ -1298,6 +1338,12 @@ impl CliUnstable {
"doctest-xcompile" => stabilized_warn(k, "1.89", STABILIZED_DOCTEST_XCOMPILE),
"dual-proc-macros" => self.dual_proc_macros = parse_empty(k, v)?,
"feature-unification" => self.feature_unification = parse_empty(k, v)?,
"fix-edition" => {
let fe = v
.ok_or_else(|| anyhow::anyhow!("-Zfix-edition expected a value"))?
.parse()?;
self.fix_edition = Some(fe);
}
"gc" => self.gc = parse_empty(k, v)?,
"git" => {
self.git =
Expand Down
12 changes: 12 additions & 0 deletions src/cargo/core/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,18 @@ impl<'gctx> Workspace<'gctx> {
Ok(ws)
}

/// Reloads the workspace.
///
/// This is useful if the workspace has been updated, such as with `cargo
/// fix` modifying the `Cargo.toml` file.
pub fn reload(&self, gctx: &'gctx GlobalContext) -> CargoResult<Workspace<'gctx>> {
let mut ws = Workspace::new(self.root_manifest(), gctx)?;
ws.set_resolve_honors_rust_version(Some(self.resolve_honors_rust_version));
ws.set_resolve_feature_unification(self.resolve_feature_unification);
ws.set_requested_lockfile_path(self.requested_lockfile_path.clone());
Ok(ws)
}

fn set_resolve_behavior(&mut self) -> CargoResult<()> {
// - If resolver is specified in the workspace definition, use that.
// - If the root package specifies the resolver, use that.
Expand Down
108 changes: 108 additions & 0 deletions src/cargo/ops/fix/fix_edition.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
//! Support for the permanently unstable `-Zfix-edition` flag.

use super::{EditionFixMode, FixOptions};
use crate::core::features::{Edition, FixEdition};
use crate::core::{Package, Workspace};
use crate::ops;
use crate::util::toml_mut::manifest::LocalManifest;
use crate::{CargoResult, GlobalContext};
use toml_edit::{Formatted, Item, Value};

/// Performs the actions for the `-Zfix-edition` flag.
pub fn fix_edition(
gctx: &GlobalContext,
original_ws: &Workspace<'_>,
opts: &mut FixOptions,
fix_edition: &FixEdition,
) -> CargoResult<()> {
let packages = opts.compile_opts.spec.get_packages(original_ws)?;
let skip_if_not_edition = |edition| -> CargoResult<bool> {
if !packages.iter().all(|p| p.manifest().edition() == edition) {
gctx.shell().status(
"Skipping",
&format!("not all packages are at edition {edition}"),
)?;
Ok(true)
} else {
Ok(false)
}
};

match fix_edition {
FixEdition::Start(edition) => {
// The start point just runs `cargo check` if the edition is the
// starting edition. This is so that crater can set a baseline of
// whether or not the package builds at all. For other editions,
// we skip entirely since they are not of interest since we can't
// migrate them.
if skip_if_not_edition(*edition)? {
return Ok(());
}
ops::compile(&original_ws, &opts.compile_opts)?;
}
FixEdition::End { initial, next } => {
// Skip packages that are not the starting edition, since we can
// only migrate from one edition to the next.
if skip_if_not_edition(*initial)? {
return Ok(());
}
// Do the edition fix.
opts.edition = Some(EditionFixMode::OverrideSpecific(*next));
opts.allow_dirty = true;
opts.allow_no_vcs = true;
opts.allow_staged = true;
ops::fix(gctx, original_ws, opts)?;
// Do `cargo check` with the new edition so that we can verify
// that it also works on the next edition.
replace_edition(&packages, *next)?;
gctx.shell()
.status("Updated", &format!("edition to {next}"))?;
let ws = original_ws.reload(gctx)?;
// Unset these since we just want to do a normal `cargo check`.
*opts
.compile_opts
.build_config
.rustfix_diagnostic_server
.borrow_mut() = None;
opts.compile_opts.build_config.primary_unit_rustc = None;

ops::compile(&ws, &opts.compile_opts)?;
}
}
Ok(())
}

/// Modifies the `edition` value of the given packages to the new edition.
fn replace_edition(packages: &[&Package], to_edition: Edition) -> CargoResult<()> {
for package in packages {
let mut manifest_mut = LocalManifest::try_new(package.manifest_path())?;
let document = &mut manifest_mut.data;
let root = document.as_table_mut();
// Update edition to the new value.
if let Some(package) = root.get_mut("package").and_then(|t| t.as_table_like_mut()) {
package.insert(
"edition",
Item::Value(Value::String(Formatted::new(to_edition.to_string()))),
);
}
// If the edition is unstable, add it to cargo-features.
if !to_edition.is_stable() {
let feature = "unstable-editions";

if let Some(features) = root
.entry("cargo-features")
.or_insert_with(|| Item::Value(Value::Array(toml_edit::Array::new())))
.as_array_mut()
{
if !features
.iter()
.any(|f| f.as_str().map_or(false, |f| f == feature))
{
features.push(feature);
}
}
}
manifest_mut.write()?;
}
Ok(())
}
77 changes: 60 additions & 17 deletions src/cargo/ops/fix.rs → src/cargo/ops/fix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ use rustfix::CodeFix;
use semver::Version;
use tracing::{debug, trace, warn};

pub use self::fix_edition::fix_edition;
use crate::core::compiler::CompileKind;
use crate::core::compiler::RustcTargetData;
use crate::core::resolver::features::{DiffMap, FeatureOpts, FeatureResolver, FeaturesFor};
Expand All @@ -66,6 +67,8 @@ use crate::util::GlobalContext;
use crate::util::{existing_vcs_repo, LockServer, LockServerClient};
use crate::{drop_eprint, drop_eprintln};

mod fix_edition;

/// **Internal only.**
/// Indicates Cargo is in fix-proxy-mode if presents.
/// The value of it is the socket address of the [`LockServer`] being used.
Expand All @@ -90,7 +93,7 @@ const IDIOMS_ENV_INTERNAL: &str = "__CARGO_FIX_IDIOMS";
const SYSROOT_INTERNAL: &str = "__CARGO_FIX_RUST_SRC";

pub struct FixOptions {
pub edition: bool,
pub edition: Option<EditionFixMode>,
pub idioms: bool,
pub compile_opts: CompileOptions,
pub allow_dirty: bool,
Expand All @@ -100,30 +103,66 @@ pub struct FixOptions {
pub requested_lockfile_path: Option<PathBuf>,
}

/// The behavior of `--edition` migration.
#[derive(Clone, Copy)]
pub enum EditionFixMode {
/// Migrates the package from the current edition to the next.
///
/// This is the normal (stable) behavior of `--edition`.
NextRelative,
/// Migrates to a specific edition.
///
/// This is used by `-Zfix-edition` to force a specific edition like
/// `future`, which does not have a relative value.
OverrideSpecific(Edition),
}

impl EditionFixMode {
/// Returns the edition to use for the given current edition.
pub fn next_edition(&self, current_edition: Edition) -> Edition {
match self {
EditionFixMode::NextRelative => current_edition.saturating_next(),
EditionFixMode::OverrideSpecific(edition) => *edition,
}
}

/// Serializes to a string.
fn to_string(&self) -> String {
match self {
EditionFixMode::NextRelative => "1".to_string(),
EditionFixMode::OverrideSpecific(edition) => edition.to_string(),
}
}

/// Deserializes from the given string.
fn from_str(s: &str) -> EditionFixMode {
match s {
"1" => EditionFixMode::NextRelative,
edition => EditionFixMode::OverrideSpecific(edition.parse().unwrap()),
}
}
}

pub fn fix(
gctx: &GlobalContext,
original_ws: &Workspace<'_>,
root_manifest: &Path,
opts: &mut FixOptions,
) -> CargoResult<()> {
check_version_control(gctx, opts)?;

let mut target_data =
RustcTargetData::new(original_ws, &opts.compile_opts.build_config.requested_kinds)?;
if opts.edition {
if let Some(edition_mode) = opts.edition {
let specs = opts.compile_opts.spec.to_package_id_specs(&original_ws)?;
let members: Vec<&Package> = original_ws
.members()
.filter(|m| specs.iter().any(|spec| spec.matches(m.package_id())))
.collect();
migrate_manifests(original_ws, &members)?;
migrate_manifests(original_ws, &members, edition_mode)?;

check_resolver_change(&original_ws, &mut target_data, opts)?;
}
let mut ws = Workspace::new(&root_manifest, gctx)?;
ws.set_resolve_honors_rust_version(Some(original_ws.resolve_honors_rust_version()));
ws.set_resolve_feature_unification(original_ws.resolve_feature_unification());
ws.set_requested_lockfile_path(opts.requested_lockfile_path.clone());
let ws = original_ws.reload(gctx)?;

// Spin up our lock server, which our subprocesses will use to synchronize fixes.
let lock_server = LockServer::new()?;
Expand All @@ -137,8 +176,8 @@ pub fn fix(
wrapper.env(BROKEN_CODE_ENV_INTERNAL, "1");
}

if opts.edition {
wrapper.env(EDITION_ENV_INTERNAL, "1");
if let Some(mode) = &opts.edition {
wrapper.env(EDITION_ENV_INTERNAL, mode.to_string());
}
if opts.idioms {
wrapper.env(IDIOMS_ENV_INTERNAL, "1");
Expand Down Expand Up @@ -252,7 +291,11 @@ fn check_version_control(gctx: &GlobalContext, opts: &FixOptions) -> CargoResult
);
}

fn migrate_manifests(ws: &Workspace<'_>, pkgs: &[&Package]) -> CargoResult<()> {
fn migrate_manifests(
ws: &Workspace<'_>,
pkgs: &[&Package],
edition_mode: EditionFixMode,
) -> CargoResult<()> {
// HACK: Duplicate workspace migration logic between virtual manifests and real manifests to
// reduce multiple Migrating messages being reported for the same file to the user
if matches!(ws.root_maybe(), MaybePackage::Virtual(_)) {
Expand All @@ -263,7 +306,7 @@ fn migrate_manifests(ws: &Workspace<'_>, pkgs: &[&Package]) -> CargoResult<()> {
.map(|p| p.manifest().edition())
.max()
.unwrap_or_default();
let prepare_for_edition = highest_edition.saturating_next();
let prepare_for_edition = edition_mode.next_edition(highest_edition);
if highest_edition == prepare_for_edition
|| (!prepare_for_edition.is_stable() && !ws.gctx().nightly_features_allowed)
{
Expand Down Expand Up @@ -308,7 +351,7 @@ fn migrate_manifests(ws: &Workspace<'_>, pkgs: &[&Package]) -> CargoResult<()> {

for pkg in pkgs {
let existing_edition = pkg.manifest().edition();
let prepare_for_edition = existing_edition.saturating_next();
let prepare_for_edition = edition_mode.next_edition(existing_edition);
if existing_edition == prepare_for_edition
|| (!prepare_for_edition.is_stable() && !ws.gctx().nightly_features_allowed)
{
Expand Down Expand Up @@ -1195,10 +1238,10 @@ impl FixArgs {
// ALLOWED: For the internal mechanism of `cargo fix` only.
// Shouldn't be set directly by anyone.
#[allow(clippy::disallowed_methods)]
let prepare_for_edition = env::var(EDITION_ENV_INTERNAL).ok().map(|_| {
enabled_edition
.unwrap_or(Edition::Edition2015)
.saturating_next()
let prepare_for_edition = env::var(EDITION_ENV_INTERNAL).ok().map(|v| {
let enabled_edition = enabled_edition.unwrap_or(Edition::Edition2015);
let mode = EditionFixMode::from_str(&v);
mode.next_edition(enabled_edition)
});

// ALLOWED: For the internal mechanism of `cargo fix` only.
Expand Down
Loading