Skip to content

Commit

Permalink
Make --force-warn support auto-detect.
Browse files Browse the repository at this point in the history
  • Loading branch information
ehuss committed Sep 1, 2021
1 parent e6a783a commit 7a63770
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
9 changes: 9 additions & 0 deletions src/cargo/core/compiler/build_context/target_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ pub struct TargetInfo {
pub rustdocflags: Vec<String>,
/// 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`.
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -253,6 +261,7 @@ impl TargetInfo {
)?,
cfg,
supports_split_debuginfo,
supports_force_warn,
})
}

Expand Down
1 change: 1 addition & 0 deletions src/cargo/core/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
29 changes: 20 additions & 9 deletions src/cargo/ops/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -362,7 +374,7 @@ pub fn fix_maybe_exec_rustc(config: &Config) -> CargoResult<bool> {
// 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")?;
Expand Down Expand Up @@ -403,7 +415,7 @@ pub fn fix_maybe_exec_rustc(config: &Config) -> CargoResult<bool> {
// - 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.
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -539,15 +551,14 @@ 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.
let only = HashSet::new();

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
Expand Down Expand Up @@ -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
Expand All @@ -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");
Expand Down
11 changes: 5 additions & 6 deletions tests/testsuite/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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", &[])
Expand Down Expand Up @@ -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 ...
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 7a63770

Please sign in to comment.