From 843b691dc8fb4824d71c3c3d10a9f835f3e99d9f Mon Sep 17 00:00:00 2001 From: Urgau Date: Fri, 20 Oct 2023 17:47:22 +0200 Subject: [PATCH 1/2] Prepare bootstrap tool for new check-cfg syntax --- src/bootstrap/src/bin/rustdoc.rs | 2 ++ src/bootstrap/src/core/builder.rs | 34 +++++++++++++++++++++++-------- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/bootstrap/src/bin/rustdoc.rs b/src/bootstrap/src/bin/rustdoc.rs index f5f80ba2a0b8b..dbbce6fe22047 100644 --- a/src/bootstrap/src/bin/rustdoc.rs +++ b/src/bootstrap/src/bin/rustdoc.rs @@ -70,7 +70,9 @@ fn main() { cmd.arg("--cfg=bootstrap"); } cmd.arg("-Zunstable-options"); + // #[cfg(bootstrap)] cmd.arg("--check-cfg=values(bootstrap)"); + // cmd.arg("--check-cfg=cfg(bootstrap)"); if verbose > 1 { eprintln!( diff --git a/src/bootstrap/src/core/builder.rs b/src/bootstrap/src/core/builder.rs index 039a87e760d70..b474cf7bf0fdb 100644 --- a/src/bootstrap/src/core/builder.rs +++ b/src/bootstrap/src/core/builder.rs @@ -1401,19 +1401,28 @@ impl<'a> Builder<'a> { rustflags.arg("-Zunstable-options"); } - // Enable cfg checking of cargo features for everything but std and also enable cfg - // checking of names and values. + // #[cfg(bootstrap)] should remove every code path where it's false + const USE_NEW_CHECK_CFG_SYNTAX: bool = false; + + // Enable compile-time checking of `cfg` names, values and Cargo `features`. // // Note: `std`, `alloc` and `core` imports some dependencies by #[path] (like // backtrace, core_simd, std_float, ...), those dependencies have their own // features but cargo isn't involved in the #[path] process and so cannot pass the // complete list of features, so for that reason we don't enable checking of // features for std crates. - cargo.arg(if mode != Mode::Std { - "-Zcheck-cfg=names,values,output,features" + if USE_NEW_CHECK_CFG_SYNTAX { + cargo.arg("-Zcheck-cfg"); + if mode == Mode::Std { + rustflags.arg("--check-cfg=cfg(feature,values(any()))"); + } } else { - "-Zcheck-cfg=names,values,output" - }); + cargo.arg(if mode != Mode::Std { + "-Zcheck-cfg=names,values,output,features" + } else { + "-Zcheck-cfg=names,values,output" + }); + } // Add extra cfg not defined in/by rustc // @@ -1433,7 +1442,12 @@ impl<'a> Builder<'a> { .collect::(), None => String::new(), }; - rustflags.arg(&format!("--check-cfg=values({name}{values})")); + if USE_NEW_CHECK_CFG_SYNTAX { + let values = values.strip_prefix(",").unwrap_or(&values); // remove the first `,` + rustflags.arg(&format!("--check-cfg=cfg({name},values({values}))")); + } else { + rustflags.arg(&format!("--check-cfg=values({name}{values})")); + } } } @@ -1449,7 +1463,11 @@ impl<'a> Builder<'a> { // We also declare that the flag is expected, which we need to do to not // get warnings about it being unexpected. hostflags.arg("-Zunstable-options"); - hostflags.arg("--check-cfg=values(bootstrap)"); + if USE_NEW_CHECK_CFG_SYNTAX { + hostflags.arg("--check-cfg=cfg(bootstrap)"); + } else { + hostflags.arg("--check-cfg=values(bootstrap)"); + } // FIXME: It might be better to use the same value for both `RUSTFLAGS` and `RUSTDOCFLAGS`, // but this breaks CI. At the very least, stage0 `rustdoc` needs `--cfg bootstrap`. See From 046bd076e20c65d5431e87a576b941387ff4df65 Mon Sep 17 00:00:00 2001 From: Urgau Date: Mon, 23 Oct 2023 14:57:20 +0200 Subject: [PATCH 2/2] Use local_rebuild property to support new check-cfg in case of rebuild --- src/bootstrap/src/core/builder.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/bootstrap/src/core/builder.rs b/src/bootstrap/src/core/builder.rs index b474cf7bf0fdb..251f94988ed87 100644 --- a/src/bootstrap/src/core/builder.rs +++ b/src/bootstrap/src/core/builder.rs @@ -1401,8 +1401,8 @@ impl<'a> Builder<'a> { rustflags.arg("-Zunstable-options"); } - // #[cfg(bootstrap)] should remove every code path where it's false - const USE_NEW_CHECK_CFG_SYNTAX: bool = false; + // #[cfg(bootstrap)] + let use_new_check_cfg_syntax = self.local_rebuild; // Enable compile-time checking of `cfg` names, values and Cargo `features`. // @@ -1411,7 +1411,7 @@ impl<'a> Builder<'a> { // features but cargo isn't involved in the #[path] process and so cannot pass the // complete list of features, so for that reason we don't enable checking of // features for std crates. - if USE_NEW_CHECK_CFG_SYNTAX { + if use_new_check_cfg_syntax { cargo.arg("-Zcheck-cfg"); if mode == Mode::Std { rustflags.arg("--check-cfg=cfg(feature,values(any()))"); @@ -1442,7 +1442,7 @@ impl<'a> Builder<'a> { .collect::(), None => String::new(), }; - if USE_NEW_CHECK_CFG_SYNTAX { + if use_new_check_cfg_syntax { let values = values.strip_prefix(",").unwrap_or(&values); // remove the first `,` rustflags.arg(&format!("--check-cfg=cfg({name},values({values}))")); } else { @@ -1463,7 +1463,7 @@ impl<'a> Builder<'a> { // We also declare that the flag is expected, which we need to do to not // get warnings about it being unexpected. hostflags.arg("-Zunstable-options"); - if USE_NEW_CHECK_CFG_SYNTAX { + if use_new_check_cfg_syntax { hostflags.arg("--check-cfg=cfg(bootstrap)"); } else { hostflags.arg("--check-cfg=values(bootstrap)");