From 7a6377092cb7b9aff5da63c81079f77695dd8eda Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Tue, 17 Aug 2021 11:09:08 -0700 Subject: [PATCH] Make --force-warn support auto-detect. --- .../compiler/build_context/target_info.rs | 9 ++++++ src/cargo/core/features.rs | 1 + src/cargo/ops/fix.rs | 29 +++++++++++++------ tests/testsuite/fix.rs | 11 ++++--- 4 files changed, 35 insertions(+), 15 deletions(-) diff --git a/src/cargo/core/compiler/build_context/target_info.rs b/src/cargo/core/compiler/build_context/target_info.rs index e8ed0a07381..3c032b6e1a5 100644 --- a/src/cargo/core/compiler/build_context/target_info.rs +++ b/src/cargo/core/compiler/build_context/target_info.rs @@ -47,6 +47,8 @@ pub struct TargetInfo { pub rustdocflags: Vec, /// Whether or not rustc supports the `-Csplit-debuginfo` flag. pub supports_split_debuginfo: bool, + /// Whether or not rustc supports the `--force-warn` flag. Remove after 1.56 is stable. + pub supports_force_warn: bool, } /// Kind of each file generated by a Unit, part of `FileType`. @@ -178,6 +180,12 @@ impl TargetInfo { extra_fingerprint, ) .is_ok(); + let supports_force_warn = rustc + .cached_output( + process.clone().arg("--force-warn=rust-2021-compatibility"), + extra_fingerprint, + ) + .is_ok(); process.arg("--print=sysroot"); process.arg("--print=cfg"); @@ -253,6 +261,7 @@ impl TargetInfo { )?, cfg, supports_split_debuginfo, + supports_force_warn, }) } diff --git a/src/cargo/core/features.rs b/src/cargo/core/features.rs index fc1d6f78312..524985dcaa3 100644 --- a/src/cargo/core/features.rs +++ b/src/cargo/core/features.rs @@ -139,6 +139,7 @@ pub enum Edition { // - Set LATEST_STABLE to the new version. // - Update `is_stable` to `true`. // - Set the editionNNNN feature to stable in the features macro below. +// - Update any tests that are affected. // - Update the man page for the --edition flag. // - Update unstable.md to move the edition section to the bottom. // - Update the documentation: diff --git a/src/cargo/ops/fix.rs b/src/cargo/ops/fix.rs index 86f5b2c4390..1d479176c3c 100644 --- a/src/cargo/ops/fix.rs +++ b/src/cargo/ops/fix.rs @@ -51,7 +51,7 @@ use log::{debug, trace, warn}; use rustfix::diagnostics::Diagnostic; use rustfix::{self, CodeFix}; -use crate::core::compiler::RustcTargetData; +use crate::core::compiler::{CompileKind, RustcTargetData, TargetInfo}; use crate::core::resolver::features::{DiffMap, FeatureOpts, FeatureResolver}; use crate::core::resolver::{HasDevUnits, Resolve, ResolveBehavior}; use crate::core::{Edition, MaybePackage, Workspace}; @@ -67,6 +67,7 @@ const FIX_ENV: &str = "__CARGO_FIX_PLZ"; const BROKEN_CODE_ENV: &str = "__CARGO_FIX_BROKEN_CODE"; const EDITION_ENV: &str = "__CARGO_FIX_EDITION"; const IDIOMS_ENV: &str = "__CARGO_FIX_IDIOMS"; +const SUPPORTS_FORCE_WARN: &str = "__CARGO_SUPPORTS_FORCE_WARN"; pub struct FixOptions { pub edition: bool, @@ -122,6 +123,17 @@ pub fn fix(ws: &Workspace<'_>, opts: &mut FixOptions) -> CargoResult<()> { let rustc = ws.config().load_global_rustc(Some(ws))?; wrapper.arg(&rustc.path); + // Remove this once 1.56 is stabilized. + let target_info = TargetInfo::new( + ws.config(), + &opts.compile_opts.build_config.requested_kinds, + &rustc, + CompileKind::Host, + )?; + if target_info.supports_force_warn { + wrapper.env(SUPPORTS_FORCE_WARN, "1"); + } + // primary crates are compiled using a cargo subprocess to do extra work of applying fixes and // repeating build until there are no more changes to be applied opts.compile_opts.build_config.primary_unit_rustc = Some(wrapper); @@ -362,7 +374,7 @@ pub fn fix_maybe_exec_rustc(config: &Config) -> CargoResult { // that we have to back it all out. if !fixes.files.is_empty() { let mut cmd = rustc.build_command(); - args.apply(&mut cmd, config); + args.apply(&mut cmd); cmd.arg("--error-format=json"); debug!("calling rustc for final verification: {:?}", cmd); let output = cmd.output().context("failed to spawn rustc")?; @@ -403,7 +415,7 @@ pub fn fix_maybe_exec_rustc(config: &Config) -> CargoResult { // - If `--broken-code`, show the error messages. // - If the fix succeeded, show any remaining warnings. let mut cmd = rustc.build_command(); - args.apply(&mut cmd, config); + args.apply(&mut cmd); for arg in args.format_args { // Add any json/error format arguments that Cargo wants. This allows // things like colored output to work correctly. @@ -497,7 +509,7 @@ fn rustfix_crate( // We'll generate new errors below. file.errors_applying_fixes.clear(); } - rustfix_and_fix(&mut fixes, rustc, filename, args, config)?; + rustfix_and_fix(&mut fixes, rustc, filename, args)?; let mut progress_yet_to_be_made = false; for (path, file) in fixes.files.iter_mut() { if file.errors_applying_fixes.is_empty() { @@ -539,7 +551,6 @@ fn rustfix_and_fix( rustc: &ProcessBuilder, filename: &Path, args: &FixArgs, - config: &Config, ) -> Result<(), Error> { // If not empty, filter by these lints. // TODO: implement a way to specify this. @@ -547,7 +558,7 @@ fn rustfix_and_fix( let mut cmd = rustc.build_command(); cmd.arg("--error-format=json"); - args.apply(&mut cmd, config); + args.apply(&mut cmd); debug!( "calling rustc to collect suggestions and validate previous fixes: {:?}", cmd @@ -822,10 +833,10 @@ impl FixArgs { }) } - fn apply(&self, cmd: &mut Command, config: &Config) { + fn apply(&self, cmd: &mut Command) { cmd.arg(&self.file); cmd.args(&self.other); - if self.prepare_for_edition.is_some() && config.nightly_features_allowed { + if self.prepare_for_edition.is_some() && env::var_os(SUPPORTS_FORCE_WARN).is_some() { // When migrating an edition, we don't want to fix other lints as // they can sometimes add suggestions that fail to apply, causing // the entire migration to fail. But those lints aren't needed to @@ -844,7 +855,7 @@ impl FixArgs { if let Some(edition) = self.prepare_for_edition { if edition.supports_compat_lint() { - if config.nightly_features_allowed { + if env::var_os(SUPPORTS_FORCE_WARN).is_some() { cmd.arg("--force-warn") .arg(format!("rust-{}-compatibility", edition)) .arg("-Zunstable-options"); diff --git a/tests/testsuite/fix.rs b/tests/testsuite/fix.rs index c761b3720e2..6ec21d18bd5 100644 --- a/tests/testsuite/fix.rs +++ b/tests/testsuite/fix.rs @@ -950,6 +950,10 @@ fn prepare_for_already_on_latest_unstable() { #[cargo_test] fn prepare_for_already_on_latest_stable() { // Stable counterpart of prepare_for_already_on_latest_unstable. + if !is_nightly() { + // Remove once 1.56 is stabilized. + return; + } if Edition::LATEST_UNSTABLE.is_some() { eprintln!("This test cannot run while the latest edition is unstable, skipping."); return; @@ -1434,10 +1438,6 @@ fn fix_color_message() { #[cargo_test] fn edition_v2_resolver_report() { // Show a report if the V2 resolver shows differences. - if !is_nightly() { - // 2021 is unstable - return; - } Package::new("common", "1.0.0") .feature("f1", &[]) .feature("dev-feat", &[]) @@ -1477,7 +1477,6 @@ fn edition_v2_resolver_report() { .build(); p.cargo("fix --edition --allow-no-vcs") - .masquerade_as_nightly_cargo() .with_stderr_unordered("\ [UPDATING] [..] [DOWNLOADING] crates ... @@ -1530,7 +1529,7 @@ fn rustfix_handles_multi_spans() { fn fix_edition_2021() { // Can migrate 2021, even when lints are allowed. if !is_nightly() { - // 2021 is unstable + // Remove once 1.56 is stabilized. return; } let p = project()