From 471fa05fef0b09e0430a80e3d7f205ef19fc4a86 Mon Sep 17 00:00:00 2001 From: est31 Date: Wed, 15 Jun 2022 21:31:28 +0200 Subject: [PATCH] Make #[cfg(bootstrap)] not error in proc macros on later stages MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit As was discovered in https://github.com/rust-lang/rust/pull/93628#issuecomment-1154697627 , adding #[cfg(bootstrap)] to a rust-internal proc macro crate would yield an unexpected cfg name error, at least on later stages wher the bootstrap cfg arg wasn't set. rustc already passes arguments to mark bootstrap as expected, however the means of delivery through the RUSTFLAGS env var is unable to reach proc macro crates, as described in the issue linked in the code this commit touches. This wouldn't be an issue for cfg args that get passed through RUSTFLAGS, as they would never become *active* either, so any usage of one of these flags in a proc macro's code would legitimately yield a lint warning. But since dc302587e2cf5105a3a864319d7e7bcb434bba20, rust takes extra measures to pass --cfg=bootstrap even in proc macros, by passing it via the wrapper. Thus, we need to send the flags to mark bootstrap as expected also from the wrapper, so that #[cfg(bootstrap)] also works from proc macros. I want to thank Urgau and jplatte for helping me find the cause of this. ❤️ --- src/bootstrap/bin/rustc.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/bootstrap/bin/rustc.rs b/src/bootstrap/bin/rustc.rs index d9467e8fd6bc1..40a3cc6d12cac 100644 --- a/src/bootstrap/bin/rustc.rs +++ b/src/bootstrap/bin/rustc.rs @@ -127,12 +127,18 @@ fn main() { } } + // Cargo doesn't pass RUSTFLAGS to proc_macros: + // https://github.com/rust-lang/cargo/issues/4423 + // Thus, if we are on stage 0, we explicitly set `--cfg=bootstrap`. + // We also declare that the flag is expected, which is mainly needed for + // later stages so that they don't warn about #[cfg(bootstrap)], + // but enabling it for stage 0 too lets any warnings, if they occur, + // occur more early on, e.g. about #[cfg(bootstrap = "foo")]. if stage == "0" { - // Cargo doesn't pass RUSTFLAGS to proc_macros: - // https://github.com/rust-lang/cargo/issues/4423 - // Set `--cfg=bootstrap` explicitly instead. cmd.arg("--cfg=bootstrap"); } + cmd.arg("-Zunstable-options"); + cmd.arg("--check-cfg=values(bootstrap)"); } if let Ok(map) = env::var("RUSTC_DEBUGINFO_MAP") {