Cleanup duplicated check-cfg lints logic

This commit is contained in:
Urgau 2024-09-19 15:52:49 +02:00
parent f1d350055a
commit 4c38aeed80
4 changed files with 39 additions and 59 deletions

View File

@ -1421,34 +1421,12 @@ fn calculate_normal(
}
.to_vec();
// Include all the args from `[lints.rust.unexpected_cfgs.check-cfg]`
//
// HACK(#13975): duplicating the lookup logic here until `--check-cfg` is supported
// on Cargo's MSRV and we can centralize the logic in `lints_to_rustflags`
let mut lint_check_cfg = Vec::new();
if let Ok(Some(lints)) = unit.pkg.manifest().normalized_toml().normalized_lints() {
if let Some(rust_lints) = lints.get("rust") {
if let Some(unexpected_cfgs) = rust_lints.get("unexpected_cfgs") {
if let Some(config) = unexpected_cfgs.config() {
if let Some(check_cfg) = config.get("check-cfg") {
if let Ok(check_cfgs) =
toml::Value::try_into::<Vec<String>>(check_cfg.clone())
{
lint_check_cfg = check_cfgs;
}
}
}
}
}
}
let profile_hash = util::hash_u64((
&unit.profile,
unit.mode,
build_runner.bcx.extra_args_for(unit),
build_runner.lto[unit],
unit.pkg.manifest().lint_rustflags(),
lint_check_cfg,
));
// Include metadata since it is exposed as environment variables.
let m = unit.pkg.manifest().metadata();

View File

@ -63,7 +63,7 @@ use std::io::{BufRead, Write};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use anyhow::{bail, Context as _, Error};
use anyhow::{Context as _, Error};
use lazycell::LazyCell;
use tracing::{debug, trace};
@ -1391,39 +1391,12 @@ fn check_cfg_args(unit: &Unit) -> CargoResult<Vec<OsString>> {
// Cargo and docs.rs than rustc and docs.rs. In particular, all users of docs.rs use
// Cargo, but not all users of rustc (like Rust-for-Linux) use docs.rs.
let mut args = vec![
Ok(vec![
OsString::from("--check-cfg"),
OsString::from("cfg(docsrs)"),
OsString::from("--check-cfg"),
arg_feature,
];
// Also include the custom arguments specified in `[lints.rust.unexpected_cfgs.check_cfg]`
if let Ok(Some(lints)) = unit.pkg.manifest().normalized_toml().normalized_lints() {
if let Some(rust_lints) = lints.get("rust") {
if let Some(unexpected_cfgs) = rust_lints.get("unexpected_cfgs") {
if let Some(config) = unexpected_cfgs.config() {
if let Some(check_cfg) = config.get("check-cfg") {
if let Ok(check_cfgs) =
toml::Value::try_into::<Vec<String>>(check_cfg.clone())
{
for check_cfg in check_cfgs {
args.push(OsString::from("--check-cfg"));
args.push(OsString::from(check_cfg));
}
// error about `check-cfg` not being a list-of-string
} else {
bail!(
"`lints.rust.unexpected_cfgs.check-cfg` must be a list of string"
);
}
}
}
}
}
}
Ok(args)
])
}
/// Adds LTO related codegen flags.

View File

@ -1446,7 +1446,7 @@ pub fn to_real_manifest(
.normalized_lints()
.expect("previously normalized")
.unwrap_or(&default),
);
)?;
let metadata = ManifestMetadata {
description: normalized_package
@ -2545,7 +2545,7 @@ switch to nightly channel you can pass
warnings.push(message);
}
fn lints_to_rustflags(lints: &manifest::TomlLints) -> Vec<String> {
fn lints_to_rustflags(lints: &manifest::TomlLints) -> CargoResult<Vec<String>> {
let mut rustflags = lints
.iter()
// 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> {
})
.collect::<Vec<_>>();
rustflags.sort();
rustflags.into_iter().map(|(_, _, option)| option).collect()
let mut rustflags: Vec<_> = rustflags.into_iter().map(|(_, _, option)| option).collect();
// Also include the custom arguments specified in `[lints.rust.unexpected_cfgs.check_cfg]`
if let Some(rust_lints) = lints.get("rust") {
if let Some(unexpected_cfgs) = rust_lints.get("unexpected_cfgs") {
if let Some(config) = unexpected_cfgs.config() {
if let Some(check_cfg) = config.get("check-cfg") {
if let Ok(check_cfgs) = toml::Value::try_into::<Vec<String>>(check_cfg.clone())
{
for check_cfg in check_cfgs {
rustflags.push("--check-cfg".to_string());
rustflags.push(check_cfg);
}
// error about `check-cfg` not being a list-of-string
} else {
bail!("`lints.rust.unexpected_cfgs.check-cfg` must be a list of string");
}
}
}
}
}
Ok(rustflags)
}
fn emit_diagnostic(

View File

@ -707,8 +707,11 @@ fn config_invalid_not_list() {
p.cargo("check")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] `lints.rust.unexpected_cfgs.check-cfg` must be a list of string
...
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
Caused by:
`lints.rust.unexpected_cfgs.check-cfg` must be a list of string
"#]])
.run();
}
@ -734,8 +737,11 @@ fn config_invalid_not_list_string() {
p.cargo("check")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] `lints.rust.unexpected_cfgs.check-cfg` must be a list of string
...
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
Caused by:
`lints.rust.unexpected_cfgs.check-cfg` must be a list of string
"#]])
.run();
}