Skip to content

Commit 78cacb9

Browse files
committed
simplify check_cfg_arg impl
1 parent 9725c4b commit 78cacb9

File tree

5 files changed

+23
-33
lines changed

5 files changed

+23
-33
lines changed

compiler/rustc_session/src/config/cfg.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
//!
1010
//! ## Adding a new cfg
1111
//!
12-
//! Adding a new feature requires two new symbols one for the cfg itself
13-
//! and the second one for the unstable feature gate, those are defined in
12+
//! Adding a new feature requires two new symbols: one for the cfg itself
13+
//! and the second one for the unstable feature gate; those are defined in
1414
//! `rustc_span::symbol`.
1515
//!
1616
//! As well as the following points,
@@ -107,8 +107,8 @@ pub(crate) fn disallow_cfgs(sess: &Session, user_cfgs: &Cfg) {
107107
//
108108
// The tests are in tests/ui/cfg/disallowed-cli-cfgs.rs.
109109

110-
// By-default all builtin cfgs are disallowed, only those are allowed:
111-
// - test: as it makes sense to the have the `test` cfg active without the builtin
110+
// By default all builtin cfgs are disallowed, but these are allowed:
111+
// - test: as it makes sense to have the `test` cfg active without the builtin
112112
// test harness. See Cargo `harness = false` config.
113113
//
114114
// Cargo `--cfg test`: https://github.com/rust-lang/cargo/blob/bc89bffa5987d4af8f71011c7557119b39e44a65/src/cargo/core/compiler/mod.rs#L1124

src/bootstrap/src/core/builder/cargo.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -687,15 +687,15 @@ impl Builder<'_> {
687687

688688
for (restricted_mode, name, values) in EXTRA_CHECK_CFGS {
689689
if restricted_mode.is_none() || *restricted_mode == Some(mode) {
690-
rustflags.arg(&check_cfg_arg(name, *values));
690+
rustflags.arg(&check_cfg_arg(name, values));
691691

692692
if *name == "bootstrap" {
693693
// Cargo doesn't pass RUSTFLAGS to proc_macros:
694694
// https://github.com/rust-lang/cargo/issues/4423
695695
// Thus, if we are on stage 0, we explicitly set `--cfg=bootstrap`.
696696
// We also declare that the flag is expected, which we need to do to not
697697
// get warnings about it being unexpected.
698-
hostflags.arg(check_cfg_arg(name, *values));
698+
hostflags.arg(check_cfg_arg(name, values));
699699
}
700700
}
701701
}

src/bootstrap/src/lib.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,13 @@ const LLD_FILE_NAMES: &[&str] = &["ld.lld", "ld64.lld", "lld-link", "wasm-ld"];
8181

8282
/// Extra `--check-cfg` to add when building the compiler or tools
8383
/// (Mode restriction, config name, config values (if any))
84-
#[expect(clippy::type_complexity)] // It's fine for hard-coded list and type is explained above.
85-
const EXTRA_CHECK_CFGS: &[(Option<Mode>, &str, Option<&[&'static str]>)] = &[
86-
(Some(Mode::Rustc), "bootstrap", None),
87-
(Some(Mode::Codegen), "bootstrap", None),
88-
(Some(Mode::ToolRustcPrivate), "bootstrap", None),
89-
(Some(Mode::ToolStd), "bootstrap", None),
90-
(Some(Mode::ToolRustcPrivate), "rust_analyzer", None),
91-
(Some(Mode::ToolStd), "rust_analyzer", None),
84+
const EXTRA_CHECK_CFGS: &[(Option<Mode>, &str, &[&str])] = &[
85+
(Some(Mode::Rustc), "bootstrap", &[]),
86+
(Some(Mode::Codegen), "bootstrap", &[]),
87+
(Some(Mode::ToolRustcPrivate), "bootstrap", &[]),
88+
(Some(Mode::ToolStd), "bootstrap", &[]),
89+
(Some(Mode::ToolRustcPrivate), "rust_analyzer", &[]),
90+
(Some(Mode::ToolStd), "rust_analyzer", &[]),
9291
// Any library specific cfgs like `target_os`, `target_arch` should be put in
9392
// priority the `[lints.rust.unexpected_cfgs.check-cfg]` table
9493
// in the appropriate `library/{std,alloc,core}/Cargo.toml`

src/bootstrap/src/utils/helpers.rs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -490,22 +490,13 @@ where
490490
})
491491
}
492492

493-
/// Create a `--check-cfg` argument invocation for a given name
494-
/// and it's values.
495-
pub fn check_cfg_arg(name: &str, values: Option<&[&str]>) -> String {
496-
// Creating a string of the values by concatenating each value:
497-
// ',values("tvos","watchos")' or '' (nothing) when there are no values.
498-
let next = match values {
499-
Some(values) => {
500-
let mut tmp = values.iter().flat_map(|val| [",", "\"", val, "\""]).collect::<String>();
501-
502-
tmp.insert_str(1, "values(");
503-
tmp.push(')');
504-
tmp
505-
}
506-
None => "".to_string(),
507-
};
508-
format!("--check-cfg=cfg({name}{next})")
493+
/// Create a `--check-cfg` argument invocation for a given name and values.
494+
pub fn check_cfg_arg(name: &str, values: &[&str]) -> String {
495+
if values.is_empty() {
496+
format!("--check-cfg=cfg({name})")
497+
} else {
498+
format!("--check-cfg=cfg({name},values(\"{}\"))", values.join("\",\""))
499+
}
509500
}
510501

511502
/// Prepares `BootstrapCommand` that runs git inside the source directory if given.

src/bootstrap/src/utils/helpers/tests.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ fn test_string_to_hex_encode() {
4747

4848
#[test]
4949
fn test_check_cfg_arg() {
50-
assert_eq!(check_cfg_arg("bootstrap", None), "--check-cfg=cfg(bootstrap)");
50+
assert_eq!(check_cfg_arg("bootstrap", &[]), "--check-cfg=cfg(bootstrap)");
5151
assert_eq!(
52-
check_cfg_arg("target_arch", Some(&["s360"])),
52+
check_cfg_arg("target_arch", &["s360"]),
5353
"--check-cfg=cfg(target_arch,values(\"s360\"))"
5454
);
5555
assert_eq!(
56-
check_cfg_arg("target_os", Some(&["nixos", "nix2"])),
56+
check_cfg_arg("target_os", &["nixos", "nix2"]),
5757
"--check-cfg=cfg(target_os,values(\"nixos\",\"nix2\"))"
5858
);
5959
}

0 commit comments

Comments
 (0)