Skip to content

Commit 9d5c149

Browse files
committed
Auto merge of #14567 - Urgau:check-cfg-cleanup-lints, r=epage
Cleanup duplicated check-cfg lint logic ### What does this PR try to resolve? This PR clean-ups some duplication left by #13958, because of Cargo MSRV. Fixes #13975 ### How should we test and review this PR? The tests in `tests/testsuite/check_cfg.rs` show no change in behaviours (except for the better error messages). I suggest maybe reviewing commit by commit.
2 parents f1d3500 + 3058f50 commit 9d5c149

File tree

5 files changed

+43
-63
lines changed

5 files changed

+43
-63
lines changed

src/cargo/core/compiler/build_runner/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ impl<'a, 'gctx> BuildRunner<'a, 'gctx> {
246246
let mut args = compiler::extern_args(&self, unit, &mut unstable_opts)?;
247247
args.extend(compiler::lto_args(&self, unit));
248248
args.extend(compiler::features_args(unit));
249-
args.extend(compiler::check_cfg_args(unit)?);
249+
args.extend(compiler::check_cfg_args(unit));
250250

251251
let script_meta = self.find_build_script_metadata(unit);
252252
if let Some(meta) = script_meta {

src/cargo/core/compiler/fingerprint/mod.rs

-22
Original file line numberDiff line numberDiff line change
@@ -1421,34 +1421,12 @@ fn calculate_normal(
14211421
}
14221422
.to_vec();
14231423

1424-
// Include all the args from `[lints.rust.unexpected_cfgs.check-cfg]`
1425-
//
1426-
// HACK(#13975): duplicating the lookup logic here until `--check-cfg` is supported
1427-
// on Cargo's MSRV and we can centralize the logic in `lints_to_rustflags`
1428-
let mut lint_check_cfg = Vec::new();
1429-
if let Ok(Some(lints)) = unit.pkg.manifest().normalized_toml().normalized_lints() {
1430-
if let Some(rust_lints) = lints.get("rust") {
1431-
if let Some(unexpected_cfgs) = rust_lints.get("unexpected_cfgs") {
1432-
if let Some(config) = unexpected_cfgs.config() {
1433-
if let Some(check_cfg) = config.get("check-cfg") {
1434-
if let Ok(check_cfgs) =
1435-
toml::Value::try_into::<Vec<String>>(check_cfg.clone())
1436-
{
1437-
lint_check_cfg = check_cfgs;
1438-
}
1439-
}
1440-
}
1441-
}
1442-
}
1443-
}
1444-
14451424
let profile_hash = util::hash_u64((
14461425
&unit.profile,
14471426
unit.mode,
14481427
build_runner.bcx.extra_args_for(unit),
14491428
build_runner.lto[unit],
14501429
unit.pkg.manifest().lint_rustflags(),
1451-
lint_check_cfg,
14521430
));
14531431
// Include metadata since it is exposed as environment variables.
14541432
let m = unit.pkg.manifest().metadata();

src/cargo/core/compiler/mod.rs

+6-33
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ use std::io::{BufRead, Write};
6363
use std::path::{Path, PathBuf};
6464
use std::sync::Arc;
6565

66-
use anyhow::{bail, Context as _, Error};
66+
use anyhow::{Context as _, Error};
6767
use lazycell::LazyCell;
6868
use tracing::{debug, trace};
6969

@@ -743,7 +743,7 @@ fn prepare_rustdoc(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> CargoResu
743743
let doc_dir = build_runner.files().out_dir(unit);
744744
rustdoc.arg("-o").arg(&doc_dir);
745745
rustdoc.args(&features_args(unit));
746-
rustdoc.args(&check_cfg_args(unit)?);
746+
rustdoc.args(&check_cfg_args(unit));
747747

748748
add_error_format_and_color(build_runner, &mut rustdoc);
749749
add_allow_features(build_runner, &mut rustdoc);
@@ -1140,7 +1140,7 @@ fn build_base_args(
11401140
}
11411141

11421142
cmd.args(&features_args(unit));
1143-
cmd.args(&check_cfg_args(unit)?);
1143+
cmd.args(&check_cfg_args(unit));
11441144

11451145
let meta = build_runner.files().metadata(unit);
11461146
cmd.arg("-C").arg(&format!("metadata={}", meta));
@@ -1354,7 +1354,7 @@ fn package_remap(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> OsString {
13541354
}
13551355

13561356
/// Generates the `--check-cfg` arguments for the `unit`.
1357-
fn check_cfg_args(unit: &Unit) -> CargoResult<Vec<OsString>> {
1357+
fn check_cfg_args(unit: &Unit) -> Vec<OsString> {
13581358
// The routine below generates the --check-cfg arguments. Our goals here are to
13591359
// enable the checking of conditionals and pass the list of declared features.
13601360
//
@@ -1391,39 +1391,12 @@ fn check_cfg_args(unit: &Unit) -> CargoResult<Vec<OsString>> {
13911391
// Cargo and docs.rs than rustc and docs.rs. In particular, all users of docs.rs use
13921392
// Cargo, but not all users of rustc (like Rust-for-Linux) use docs.rs.
13931393

1394-
let mut args = vec![
1394+
vec![
13951395
OsString::from("--check-cfg"),
13961396
OsString::from("cfg(docsrs)"),
13971397
OsString::from("--check-cfg"),
13981398
arg_feature,
1399-
];
1400-
1401-
// Also include the custom arguments specified in `[lints.rust.unexpected_cfgs.check_cfg]`
1402-
if let Ok(Some(lints)) = unit.pkg.manifest().normalized_toml().normalized_lints() {
1403-
if let Some(rust_lints) = lints.get("rust") {
1404-
if let Some(unexpected_cfgs) = rust_lints.get("unexpected_cfgs") {
1405-
if let Some(config) = unexpected_cfgs.config() {
1406-
if let Some(check_cfg) = config.get("check-cfg") {
1407-
if let Ok(check_cfgs) =
1408-
toml::Value::try_into::<Vec<String>>(check_cfg.clone())
1409-
{
1410-
for check_cfg in check_cfgs {
1411-
args.push(OsString::from("--check-cfg"));
1412-
args.push(OsString::from(check_cfg));
1413-
}
1414-
// error about `check-cfg` not being a list-of-string
1415-
} else {
1416-
bail!(
1417-
"`lints.rust.unexpected_cfgs.check-cfg` must be a list of string"
1418-
);
1419-
}
1420-
}
1421-
}
1422-
}
1423-
}
1424-
}
1425-
1426-
Ok(args)
1399+
]
14271400
}
14281401

14291402
/// Adds LTO related codegen flags.

src/cargo/util/toml/mod.rs

+26-3
Original file line numberDiff line numberDiff line change
@@ -1446,7 +1446,7 @@ pub fn to_real_manifest(
14461446
.normalized_lints()
14471447
.expect("previously normalized")
14481448
.unwrap_or(&default),
1449-
);
1449+
)?;
14501450

14511451
let metadata = ManifestMetadata {
14521452
description: normalized_package
@@ -2545,7 +2545,7 @@ switch to nightly channel you can pass
25452545
warnings.push(message);
25462546
}
25472547

2548-
fn lints_to_rustflags(lints: &manifest::TomlLints) -> Vec<String> {
2548+
fn lints_to_rustflags(lints: &manifest::TomlLints) -> CargoResult<Vec<String>> {
25492549
let mut rustflags = lints
25502550
.iter()
25512551
// We don't want to pass any of the `cargo` lints to `rustc`
@@ -2575,7 +2575,30 @@ fn lints_to_rustflags(lints: &manifest::TomlLints) -> Vec<String> {
25752575
})
25762576
.collect::<Vec<_>>();
25772577
rustflags.sort();
2578-
rustflags.into_iter().map(|(_, _, option)| option).collect()
2578+
2579+
let mut rustflags: Vec<_> = rustflags.into_iter().map(|(_, _, option)| option).collect();
2580+
2581+
// Also include the custom arguments specified in `[lints.rust.unexpected_cfgs.check_cfg]`
2582+
if let Some(rust_lints) = lints.get("rust") {
2583+
if let Some(unexpected_cfgs) = rust_lints.get("unexpected_cfgs") {
2584+
if let Some(config) = unexpected_cfgs.config() {
2585+
if let Some(check_cfg) = config.get("check-cfg") {
2586+
if let Ok(check_cfgs) = toml::Value::try_into::<Vec<String>>(check_cfg.clone())
2587+
{
2588+
for check_cfg in check_cfgs {
2589+
rustflags.push("--check-cfg".to_string());
2590+
rustflags.push(check_cfg);
2591+
}
2592+
// error about `check-cfg` not being a list-of-string
2593+
} else {
2594+
bail!("`lints.rust.unexpected_cfgs.check-cfg` must be a list of string");
2595+
}
2596+
}
2597+
}
2598+
}
2599+
}
2600+
2601+
Ok(rustflags)
25792602
}
25802603

25812604
fn emit_diagnostic(

tests/testsuite/check_cfg.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -707,8 +707,11 @@ fn config_invalid_not_list() {
707707
p.cargo("check")
708708
.with_status(101)
709709
.with_stderr_data(str![[r#"
710-
[ERROR] `lints.rust.unexpected_cfgs.check-cfg` must be a list of string
711-
...
710+
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
711+
712+
Caused by:
713+
`lints.rust.unexpected_cfgs.check-cfg` must be a list of string
714+
712715
"#]])
713716
.run();
714717
}
@@ -734,8 +737,11 @@ fn config_invalid_not_list_string() {
734737
p.cargo("check")
735738
.with_status(101)
736739
.with_stderr_data(str![[r#"
737-
[ERROR] `lints.rust.unexpected_cfgs.check-cfg` must be a list of string
738-
...
740+
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
741+
742+
Caused by:
743+
`lints.rust.unexpected_cfgs.check-cfg` must be a list of string
744+
739745
"#]])
740746
.run();
741747
}

0 commit comments

Comments
 (0)