diff --git a/src/bin/cargo/commands/fix.rs b/src/bin/cargo/commands/fix.rs index 8850c8b018f..f04800e8eba 100644 --- a/src/bin/cargo/commands/fix.rs +++ b/src/bin/cargo/commands/fix.rs @@ -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(()) } diff --git a/src/cargo/core/features.rs b/src/cargo/core/features.rs index cdf5c94eb0d..320a0b7cec7 100644 --- a/src/cargo/core/features.rs +++ b/src/cargo/core/features.rs @@ -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::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, @@ -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>, + fix_edition: Option = ("Permanently unstable edition migration helper"), gc: bool = ("Track cache usage and \"garbage collect\" unused files"), #[serde(deserialize_with = "deserialize_git_features")] git: Option = ("Enable support for shallow git fetch operations"), @@ -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 = diff --git a/src/cargo/core/workspace.rs b/src/cargo/core/workspace.rs index 50b1de570e8..4f28a4efb14 100644 --- a/src/cargo/core/workspace.rs +++ b/src/cargo/core/workspace.rs @@ -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> { + 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. diff --git a/src/cargo/ops/fix/fix_edition.rs b/src/cargo/ops/fix/fix_edition.rs new file mode 100644 index 00000000000..966ddf9488d --- /dev/null +++ b/src/cargo/ops/fix/fix_edition.rs @@ -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 { + 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(()) +} diff --git a/src/cargo/ops/fix.rs b/src/cargo/ops/fix/mod.rs similarity index 95% rename from src/cargo/ops/fix.rs rename to src/cargo/ops/fix/mod.rs index bd810defccd..b2723e6015a 100644 --- a/src/cargo/ops/fix.rs +++ b/src/cargo/ops/fix/mod.rs @@ -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}; @@ -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. @@ -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, pub idioms: bool, pub compile_opts: CompileOptions, pub allow_dirty: bool, @@ -100,30 +103,66 @@ pub struct FixOptions { pub requested_lockfile_path: Option, } +/// 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()?; @@ -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"); @@ -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(_)) { @@ -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) { @@ -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) { @@ -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. diff --git a/src/cargo/ops/mod.rs b/src/cargo/ops/mod.rs index 9960c12649d..2431b73d037 100644 --- a/src/cargo/ops/mod.rs +++ b/src/cargo/ops/mod.rs @@ -26,7 +26,9 @@ pub use self::cargo_update::upgrade_manifests; pub use self::cargo_update::write_manifest_upgrades; pub use self::cargo_update::UpdateOptions; pub use self::common_for_install_and_uninstall::{resolve_root, InstallTracker}; -pub use self::fix::{fix, fix_exec_rustc, fix_get_proxy_lock_addr, FixOptions}; +pub use self::fix::{ + fix, fix_edition, fix_exec_rustc, fix_get_proxy_lock_addr, EditionFixMode, FixOptions, +}; pub use self::lockfile::{load_pkg_lockfile, resolve_to_string, write_pkg_lockfile}; pub use self::registry::info; pub use self::registry::modify_owners; diff --git a/src/doc/src/reference/unstable.md b/src/doc/src/reference/unstable.md index f16ecf25a33..76ecb860f49 100644 --- a/src/doc/src/reference/unstable.md +++ b/src/doc/src/reference/unstable.md @@ -126,6 +126,7 @@ Each new feature described below should explain how to use it. * [native-completions](#native-completions) --- Move cargo shell completions to native completions. * [warnings](#warnings) --- controls warning behavior; options for allowing or denying warnings. * [Package message format](#package-message-format) --- Message format for `cargo package`. + * [`fix-edition`](#fix-edition) --- A permanently unstable edition migration helper. ## allow-features @@ -1915,6 +1916,19 @@ When new editions are introduced, the `unstable-editions` feature is required un The special "future" edition is a home for new features that are under development, and is permanently unstable. The "future" edition also has no new behavior by itself. Each change in the future edition requires an opt-in such as a `#![feature(...)]` attribute. +## `fix-edition` + +`-Zfix-edition` is a permanently unstable flag to assist with testing edition migrations, particularly with the use of crater. It only works with the `cargo fix` subcommand. It takes two different forms: + +- `-Zfix-edition=start=$INITIAL` --- This form checks if the current edition is equal to the given number. If not, it exits with success (because we want to ignore older editions). If it is, then it runs the equivalent of `cargo check`. This is intended to be used with crater's "start" toolchain to set a baseline for the "before" toolchain. +- `-Zfix-edition=end=$INITIAL,$NEXT` --- This form checks if the current edition is equal to the given `$INITIAL` value. If not, it exits with success. If it is, then it performs an edition migration to the edition specified in `$NEXT`. Afterwards, it will modify `Cargo.toml` to add the appropriate `cargo-features = ["unstable-edition"]`, update the `edition` field, and run the equivalent of `cargo check` to verify that the migration works on the new edition. + +For example: + +```console +cargo +nightly fix -Zfix-edition=end=2024,future +``` + # Stabilized and removed features ## Compile progress diff --git a/tests/testsuite/cargo/z_help/stdout.term.svg b/tests/testsuite/cargo/z_help/stdout.term.svg index 0468eb61bc2..b10ddce370d 100644 --- a/tests/testsuite/cargo/z_help/stdout.term.svg +++ b/tests/testsuite/cargo/z_help/stdout.term.svg @@ -1,4 +1,4 @@ - +