Skip to content

Commit 5a7e4c6

Browse files
committed
Auto merge of #94298 - Urgau:rustbuild-check-cfg, r=Mark-Simulacrum
Enable conditional compilation checking on the Rust codebase This pull-request enable conditional compilation checking on every rust project build by the `bootstrap` tool. To be more specific, this PR only enable well known names checking + extra names (bootstrap, parallel_compiler, ...). r? `@Mark-Simulacrum`
2 parents 9fcbc32 + 976fdb1 commit 5a7e4c6

File tree

5 files changed

+60
-3
lines changed

5 files changed

+60
-3
lines changed

compiler/rustc_data_structures/src/sorted_map/index_map.rs

-3
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,3 @@ impl<I: Idx, K, V> std::ops::Index<I> for SortedIndexMultiMap<I, K, V> {
152152
&self.items[idx].1
153153
}
154154
}
155-
156-
#[cfg(tests)]
157-
mod tests;

library/core/src/mem/maybe_uninit.rs

+1
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,7 @@ impl<T> MaybeUninit<T> {
817817
/// ### Correct usage of this method:
818818
///
819819
/// ```rust
820+
/// # #![allow(unexpected_cfgs)]
820821
/// use std::mem::MaybeUninit;
821822
///
822823
/// # unsafe extern "C" fn initialize_buffer(buf: *mut [u8; 1024]) { *buf = [0; 1024] }

library/std/src/os/windows/fs.rs

+3
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ pub trait OpenOptionsExt {
158158
/// # Examples
159159
///
160160
/// ```no_run
161+
/// # #![allow(unexpected_cfgs)]
161162
/// # #[cfg(for_demonstration_only)]
162163
/// extern crate winapi;
163164
/// # mod winapi { pub const FILE_FLAG_DELETE_ON_CLOSE: u32 = 0x04000000; }
@@ -195,6 +196,7 @@ pub trait OpenOptionsExt {
195196
/// # Examples
196197
///
197198
/// ```no_run
199+
/// # #![allow(unexpected_cfgs)]
198200
/// # #[cfg(for_demonstration_only)]
199201
/// extern crate winapi;
200202
/// # mod winapi { pub const FILE_ATTRIBUTE_HIDDEN: u32 = 2; }
@@ -236,6 +238,7 @@ pub trait OpenOptionsExt {
236238
/// # Examples
237239
///
238240
/// ```no_run
241+
/// # #![allow(unexpected_cfgs)]
239242
/// # #[cfg(for_demonstration_only)]
240243
/// extern crate winapi;
241244
/// # mod winapi { pub const SECURITY_IDENTIFICATION: u32 = 0; }

src/bootstrap/builder.rs

+28
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use crate::run;
2626
use crate::test;
2727
use crate::tool::{self, SourceType};
2828
use crate::util::{self, add_dylib_path, add_link_lib_path, exe, libdir};
29+
use crate::EXTRA_CHECK_CFGS;
2930
use crate::{Build, CLang, DocTests, GitRepo, Mode};
3031

3132
pub use crate::Compiler;
@@ -1095,6 +1096,33 @@ impl<'a> Builder<'a> {
10951096
rustflags.arg("-Zunstable-options");
10961097
}
10971098

1099+
// #[cfg(not(bootstrap)]
1100+
if stage != 0 {
1101+
// Enable cfg checking of cargo features
1102+
// FIXME: De-comment this when cargo beta get support for it
1103+
// cargo.arg("-Zcheck-cfg-features");
1104+
1105+
// Enable cfg checking of rustc well-known names
1106+
rustflags.arg("-Zunstable-options").arg("--check-cfg=names()");
1107+
1108+
// Add extra cfg not defined in rustc
1109+
for (restricted_mode, name, values) in EXTRA_CHECK_CFGS {
1110+
if *restricted_mode == None || *restricted_mode == Some(mode) {
1111+
// Creating a string of the values by concatenating each value:
1112+
// ',"tvos","watchos"' or '' (nothing) when there are no values
1113+
let values = match values {
1114+
Some(values) => values
1115+
.iter()
1116+
.map(|val| [",", "\"", val, "\""])
1117+
.flatten()
1118+
.collect::<String>(),
1119+
None => String::new(),
1120+
};
1121+
rustflags.arg(&format!("--check-cfg=values({name}{values})"));
1122+
}
1123+
}
1124+
}
1125+
10981126
// FIXME: It might be better to use the same value for both `RUSTFLAGS` and `RUSTDOCFLAGS`,
10991127
// but this breaks CI. At the very least, stage0 `rustdoc` needs `--cfg bootstrap`. See
11001128
// #71458.

src/bootstrap/lib.rs

+28
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,34 @@ const LLVM_TOOLS: &[&str] = &[
186186

187187
pub const VERSION: usize = 2;
188188

189+
/// Extra --check-cfg to add when building
190+
/// (Mode restriction, config name, config values (if any))
191+
const EXTRA_CHECK_CFGS: &[(Option<Mode>, &'static str, Option<&[&'static str]>)] = &[
192+
(None, "bootstrap", None),
193+
(Some(Mode::Rustc), "parallel_compiler", None),
194+
(Some(Mode::ToolRustc), "parallel_compiler", None),
195+
(Some(Mode::Std), "miri", None),
196+
(Some(Mode::Std), "stdarch_intel_sde", None),
197+
(Some(Mode::Std), "no_fp_fmt_parse", None),
198+
(Some(Mode::Std), "no_global_oom_handling", None),
199+
(Some(Mode::Std), "freebsd12", None),
200+
(Some(Mode::Std), "backtrace_in_libstd", None),
201+
// FIXME: Used by rustfmt is their test but is invalid (neither cargo nor bootstrap ever set
202+
// this config) should probably by removed or use a allow attribute.
203+
(Some(Mode::ToolRustc), "release", None),
204+
// FIXME: Used by stdarch in their test, should use a allow attribute instead.
205+
(Some(Mode::Std), "dont_compile_me", None),
206+
// FIXME: Used by serde_json, but we should not be triggering on external dependencies.
207+
(Some(Mode::Rustc), "no_btreemap_remove_entry", None),
208+
(Some(Mode::ToolRustc), "no_btreemap_remove_entry", None),
209+
// FIXME: Used by crossbeam-utils, but we should not be triggering on external dependencies.
210+
(Some(Mode::Rustc), "crossbeam_loom", None),
211+
(Some(Mode::ToolRustc), "crossbeam_loom", None),
212+
// FIXME: Used by proc-macro2, but we should not be triggering on external dependencies.
213+
(Some(Mode::Rustc), "span_locations", None),
214+
(Some(Mode::ToolRustc), "span_locations", None),
215+
];
216+
189217
/// A structure representing a Rust compiler.
190218
///
191219
/// Each compiler has a `stage` that it is associated with and a `host` that

0 commit comments

Comments
 (0)