From 08e1e67b490253deed9b91761d95fe80a2f48cd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20BRANSTETT?= Date: Wed, 23 Feb 2022 18:22:28 +0100 Subject: [PATCH 1/3] Remove invalid #[cfg(tests)] in index_map --- compiler/rustc_data_structures/src/sorted_map/index_map.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/compiler/rustc_data_structures/src/sorted_map/index_map.rs b/compiler/rustc_data_structures/src/sorted_map/index_map.rs index 593316e2699fa..0ec32dc43070d 100644 --- a/compiler/rustc_data_structures/src/sorted_map/index_map.rs +++ b/compiler/rustc_data_structures/src/sorted_map/index_map.rs @@ -152,6 +152,3 @@ impl std::ops::Index for SortedIndexMultiMap { &self.items[idx].1 } } - -#[cfg(tests)] -mod tests; From a93c7abc690fa9250182c9b637dfe4241d6c6d33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20BRANSTETT?= Date: Sat, 26 Feb 2022 01:31:36 +0100 Subject: [PATCH 2/3] Add #![allow(unexpected_cfgs)] in preparation of global --check-cfg --- library/core/src/mem/maybe_uninit.rs | 1 + library/std/src/os/windows/fs.rs | 3 +++ 2 files changed, 4 insertions(+) diff --git a/library/core/src/mem/maybe_uninit.rs b/library/core/src/mem/maybe_uninit.rs index e38c0412a0afe..9db5a9a288940 100644 --- a/library/core/src/mem/maybe_uninit.rs +++ b/library/core/src/mem/maybe_uninit.rs @@ -817,6 +817,7 @@ impl MaybeUninit { /// ### Correct usage of this method: /// /// ```rust + /// # #![allow(unexpected_cfgs)] /// use std::mem::MaybeUninit; /// /// # unsafe extern "C" fn initialize_buffer(buf: *mut [u8; 1024]) { *buf = [0; 1024] } diff --git a/library/std/src/os/windows/fs.rs b/library/std/src/os/windows/fs.rs index 31d1e3c1e42ee..f15baff59dbfb 100644 --- a/library/std/src/os/windows/fs.rs +++ b/library/std/src/os/windows/fs.rs @@ -158,6 +158,7 @@ pub trait OpenOptionsExt { /// # Examples /// /// ```no_run + /// # #![allow(unexpected_cfgs)] /// # #[cfg(for_demonstration_only)] /// extern crate winapi; /// # mod winapi { pub const FILE_FLAG_DELETE_ON_CLOSE: u32 = 0x04000000; } @@ -195,6 +196,7 @@ pub trait OpenOptionsExt { /// # Examples /// /// ```no_run + /// # #![allow(unexpected_cfgs)] /// # #[cfg(for_demonstration_only)] /// extern crate winapi; /// # mod winapi { pub const FILE_ATTRIBUTE_HIDDEN: u32 = 2; } @@ -236,6 +238,7 @@ pub trait OpenOptionsExt { /// # Examples /// /// ```no_run + /// # #![allow(unexpected_cfgs)] /// # #[cfg(for_demonstration_only)] /// extern crate winapi; /// # mod winapi { pub const SECURITY_IDENTIFICATION: u32 = 0; } From 976fdb18c98864f13448a7d48402c5df7b51d711 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20BRANSTETT?= Date: Wed, 23 Feb 2022 18:27:36 +0100 Subject: [PATCH 3/3] Enable conditional compilation checking on the Rust codebase --- src/bootstrap/builder.rs | 28 ++++++++++++++++++++++++++++ src/bootstrap/lib.rs | 28 ++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/src/bootstrap/builder.rs b/src/bootstrap/builder.rs index 0d387ff1e37c2..00aebc21f581a 100644 --- a/src/bootstrap/builder.rs +++ b/src/bootstrap/builder.rs @@ -26,6 +26,7 @@ use crate::run; use crate::test; use crate::tool::{self, SourceType}; use crate::util::{self, add_dylib_path, add_link_lib_path, exe, libdir}; +use crate::EXTRA_CHECK_CFGS; use crate::{Build, CLang, DocTests, GitRepo, Mode}; pub use crate::Compiler; @@ -1095,6 +1096,33 @@ impl<'a> Builder<'a> { rustflags.arg("-Zunstable-options"); } + // #[cfg(not(bootstrap)] + if stage != 0 { + // Enable cfg checking of cargo features + // FIXME: De-comment this when cargo beta get support for it + // cargo.arg("-Zcheck-cfg-features"); + + // Enable cfg checking of rustc well-known names + rustflags.arg("-Zunstable-options").arg("--check-cfg=names()"); + + // Add extra cfg not defined in rustc + for (restricted_mode, name, values) in EXTRA_CHECK_CFGS { + if *restricted_mode == None || *restricted_mode == Some(mode) { + // Creating a string of the values by concatenating each value: + // ',"tvos","watchos"' or '' (nothing) when there are no values + let values = match values { + Some(values) => values + .iter() + .map(|val| [",", "\"", val, "\""]) + .flatten() + .collect::(), + None => String::new(), + }; + rustflags.arg(&format!("--check-cfg=values({name}{values})")); + } + } + } + // 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 // #71458. diff --git a/src/bootstrap/lib.rs b/src/bootstrap/lib.rs index abfac2a589793..82faffb535177 100644 --- a/src/bootstrap/lib.rs +++ b/src/bootstrap/lib.rs @@ -186,6 +186,34 @@ const LLVM_TOOLS: &[&str] = &[ pub const VERSION: usize = 2; +/// Extra --check-cfg to add when building +/// (Mode restriction, config name, config values (if any)) +const EXTRA_CHECK_CFGS: &[(Option, &'static str, Option<&[&'static str]>)] = &[ + (None, "bootstrap", None), + (Some(Mode::Rustc), "parallel_compiler", None), + (Some(Mode::ToolRustc), "parallel_compiler", None), + (Some(Mode::Std), "miri", None), + (Some(Mode::Std), "stdarch_intel_sde", None), + (Some(Mode::Std), "no_fp_fmt_parse", None), + (Some(Mode::Std), "no_global_oom_handling", None), + (Some(Mode::Std), "freebsd12", None), + (Some(Mode::Std), "backtrace_in_libstd", None), + // FIXME: Used by rustfmt is their test but is invalid (neither cargo nor bootstrap ever set + // this config) should probably by removed or use a allow attribute. + (Some(Mode::ToolRustc), "release", None), + // FIXME: Used by stdarch in their test, should use a allow attribute instead. + (Some(Mode::Std), "dont_compile_me", None), + // FIXME: Used by serde_json, but we should not be triggering on external dependencies. + (Some(Mode::Rustc), "no_btreemap_remove_entry", None), + (Some(Mode::ToolRustc), "no_btreemap_remove_entry", None), + // FIXME: Used by crossbeam-utils, but we should not be triggering on external dependencies. + (Some(Mode::Rustc), "crossbeam_loom", None), + (Some(Mode::ToolRustc), "crossbeam_loom", None), + // FIXME: Used by proc-macro2, but we should not be triggering on external dependencies. + (Some(Mode::Rustc), "span_locations", None), + (Some(Mode::ToolRustc), "span_locations", None), +]; + /// A structure representing a Rust compiler. /// /// Each compiler has a `stage` that it is associated with and a `host` that