Skip to content

Commit 7a63770

Browse files
committed
Make --force-warn support auto-detect.
1 parent e6a783a commit 7a63770

File tree

4 files changed

+35
-15
lines changed

4 files changed

+35
-15
lines changed

src/cargo/core/compiler/build_context/target_info.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ pub struct TargetInfo {
4747
pub rustdocflags: Vec<String>,
4848
/// Whether or not rustc supports the `-Csplit-debuginfo` flag.
4949
pub supports_split_debuginfo: bool,
50+
/// Whether or not rustc supports the `--force-warn` flag. Remove after 1.56 is stable.
51+
pub supports_force_warn: bool,
5052
}
5153

5254
/// Kind of each file generated by a Unit, part of `FileType`.
@@ -178,6 +180,12 @@ impl TargetInfo {
178180
extra_fingerprint,
179181
)
180182
.is_ok();
183+
let supports_force_warn = rustc
184+
.cached_output(
185+
process.clone().arg("--force-warn=rust-2021-compatibility"),
186+
extra_fingerprint,
187+
)
188+
.is_ok();
181189

182190
process.arg("--print=sysroot");
183191
process.arg("--print=cfg");
@@ -253,6 +261,7 @@ impl TargetInfo {
253261
)?,
254262
cfg,
255263
supports_split_debuginfo,
264+
supports_force_warn,
256265
})
257266
}
258267

src/cargo/core/features.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ pub enum Edition {
139139
// - Set LATEST_STABLE to the new version.
140140
// - Update `is_stable` to `true`.
141141
// - Set the editionNNNN feature to stable in the features macro below.
142+
// - Update any tests that are affected.
142143
// - Update the man page for the --edition flag.
143144
// - Update unstable.md to move the edition section to the bottom.
144145
// - Update the documentation:

src/cargo/ops/fix.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ use log::{debug, trace, warn};
5151
use rustfix::diagnostics::Diagnostic;
5252
use rustfix::{self, CodeFix};
5353

54-
use crate::core::compiler::RustcTargetData;
54+
use crate::core::compiler::{CompileKind, RustcTargetData, TargetInfo};
5555
use crate::core::resolver::features::{DiffMap, FeatureOpts, FeatureResolver};
5656
use crate::core::resolver::{HasDevUnits, Resolve, ResolveBehavior};
5757
use crate::core::{Edition, MaybePackage, Workspace};
@@ -67,6 +67,7 @@ const FIX_ENV: &str = "__CARGO_FIX_PLZ";
6767
const BROKEN_CODE_ENV: &str = "__CARGO_FIX_BROKEN_CODE";
6868
const EDITION_ENV: &str = "__CARGO_FIX_EDITION";
6969
const IDIOMS_ENV: &str = "__CARGO_FIX_IDIOMS";
70+
const SUPPORTS_FORCE_WARN: &str = "__CARGO_SUPPORTS_FORCE_WARN";
7071

7172
pub struct FixOptions {
7273
pub edition: bool,
@@ -122,6 +123,17 @@ pub fn fix(ws: &Workspace<'_>, opts: &mut FixOptions) -> CargoResult<()> {
122123
let rustc = ws.config().load_global_rustc(Some(ws))?;
123124
wrapper.arg(&rustc.path);
124125

126+
// Remove this once 1.56 is stabilized.
127+
let target_info = TargetInfo::new(
128+
ws.config(),
129+
&opts.compile_opts.build_config.requested_kinds,
130+
&rustc,
131+
CompileKind::Host,
132+
)?;
133+
if target_info.supports_force_warn {
134+
wrapper.env(SUPPORTS_FORCE_WARN, "1");
135+
}
136+
125137
// primary crates are compiled using a cargo subprocess to do extra work of applying fixes and
126138
// repeating build until there are no more changes to be applied
127139
opts.compile_opts.build_config.primary_unit_rustc = Some(wrapper);
@@ -362,7 +374,7 @@ pub fn fix_maybe_exec_rustc(config: &Config) -> CargoResult<bool> {
362374
// that we have to back it all out.
363375
if !fixes.files.is_empty() {
364376
let mut cmd = rustc.build_command();
365-
args.apply(&mut cmd, config);
377+
args.apply(&mut cmd);
366378
cmd.arg("--error-format=json");
367379
debug!("calling rustc for final verification: {:?}", cmd);
368380
let output = cmd.output().context("failed to spawn rustc")?;
@@ -403,7 +415,7 @@ pub fn fix_maybe_exec_rustc(config: &Config) -> CargoResult<bool> {
403415
// - If `--broken-code`, show the error messages.
404416
// - If the fix succeeded, show any remaining warnings.
405417
let mut cmd = rustc.build_command();
406-
args.apply(&mut cmd, config);
418+
args.apply(&mut cmd);
407419
for arg in args.format_args {
408420
// Add any json/error format arguments that Cargo wants. This allows
409421
// things like colored output to work correctly.
@@ -497,7 +509,7 @@ fn rustfix_crate(
497509
// We'll generate new errors below.
498510
file.errors_applying_fixes.clear();
499511
}
500-
rustfix_and_fix(&mut fixes, rustc, filename, args, config)?;
512+
rustfix_and_fix(&mut fixes, rustc, filename, args)?;
501513
let mut progress_yet_to_be_made = false;
502514
for (path, file) in fixes.files.iter_mut() {
503515
if file.errors_applying_fixes.is_empty() {
@@ -539,15 +551,14 @@ fn rustfix_and_fix(
539551
rustc: &ProcessBuilder,
540552
filename: &Path,
541553
args: &FixArgs,
542-
config: &Config,
543554
) -> Result<(), Error> {
544555
// If not empty, filter by these lints.
545556
// TODO: implement a way to specify this.
546557
let only = HashSet::new();
547558

548559
let mut cmd = rustc.build_command();
549560
cmd.arg("--error-format=json");
550-
args.apply(&mut cmd, config);
561+
args.apply(&mut cmd);
551562
debug!(
552563
"calling rustc to collect suggestions and validate previous fixes: {:?}",
553564
cmd
@@ -822,10 +833,10 @@ impl FixArgs {
822833
})
823834
}
824835

825-
fn apply(&self, cmd: &mut Command, config: &Config) {
836+
fn apply(&self, cmd: &mut Command) {
826837
cmd.arg(&self.file);
827838
cmd.args(&self.other);
828-
if self.prepare_for_edition.is_some() && config.nightly_features_allowed {
839+
if self.prepare_for_edition.is_some() && env::var_os(SUPPORTS_FORCE_WARN).is_some() {
829840
// When migrating an edition, we don't want to fix other lints as
830841
// they can sometimes add suggestions that fail to apply, causing
831842
// the entire migration to fail. But those lints aren't needed to
@@ -844,7 +855,7 @@ impl FixArgs {
844855

845856
if let Some(edition) = self.prepare_for_edition {
846857
if edition.supports_compat_lint() {
847-
if config.nightly_features_allowed {
858+
if env::var_os(SUPPORTS_FORCE_WARN).is_some() {
848859
cmd.arg("--force-warn")
849860
.arg(format!("rust-{}-compatibility", edition))
850861
.arg("-Zunstable-options");

tests/testsuite/fix.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,10 @@ fn prepare_for_already_on_latest_unstable() {
950950
#[cargo_test]
951951
fn prepare_for_already_on_latest_stable() {
952952
// Stable counterpart of prepare_for_already_on_latest_unstable.
953+
if !is_nightly() {
954+
// Remove once 1.56 is stabilized.
955+
return;
956+
}
953957
if Edition::LATEST_UNSTABLE.is_some() {
954958
eprintln!("This test cannot run while the latest edition is unstable, skipping.");
955959
return;
@@ -1434,10 +1438,6 @@ fn fix_color_message() {
14341438
#[cargo_test]
14351439
fn edition_v2_resolver_report() {
14361440
// Show a report if the V2 resolver shows differences.
1437-
if !is_nightly() {
1438-
// 2021 is unstable
1439-
return;
1440-
}
14411441
Package::new("common", "1.0.0")
14421442
.feature("f1", &[])
14431443
.feature("dev-feat", &[])
@@ -1477,7 +1477,6 @@ fn edition_v2_resolver_report() {
14771477
.build();
14781478

14791479
p.cargo("fix --edition --allow-no-vcs")
1480-
.masquerade_as_nightly_cargo()
14811480
.with_stderr_unordered("\
14821481
[UPDATING] [..]
14831482
[DOWNLOADING] crates ...
@@ -1530,7 +1529,7 @@ fn rustfix_handles_multi_spans() {
15301529
fn fix_edition_2021() {
15311530
// Can migrate 2021, even when lints are allowed.
15321531
if !is_nightly() {
1533-
// 2021 is unstable
1532+
// Remove once 1.56 is stabilized.
15341533
return;
15351534
}
15361535
let p = project()

0 commit comments

Comments
 (0)