Skip to content

Commit 3dfda2c

Browse files
authored
Rollup merge of rust-lang#109985 - blyxyas:is_test_crate, r=compiler-errors
Add little `is_test_crate` function Ok, this is quite a story. I'm mainly a Clippy contributor, so I was fixing [this Clippy issue](rust-lang/rust-clippy#10584) about a lint having to ignore test modules but that wasn't ignoring test files (integration test, `test/` dirs and such). As test **files** don't tend to have an inner `#[cfg(test)]` module inside them, I tried everything, looking for filenames, looking for item's parents in the HIR Map, doing black magic... I even asked [on Zulip](https://rust-lang.zulipchat.com/#narrow/stream/122651-general/topic/.E2.9C.94.20Checking.20if.20file.20is.20integration.20test), and jyn answered something about `--cfg test`. Aha! That's something that I might be looking for, so I started looking at `rustc_driver_impl` flag parsing and configuration and all that. Then, I stumbled on [this function right here](https://github.com/rust-lang/rust/blob/2e486be8d29d198d48bc26bfce5712a4822814f5/compiler/rustc_driver_impl/src/lib.rs#L174-L181), and noticed the argument `config: Config`. That's a hint. So [Config](https://doc.rust-lang.org/beta/nightly-rustc/rustc_interface/interface/struct.Config.html) has the field `opts: Options`, and [`Options`](https://doc.rust-lang.org/beta/nightly-rustc/rustc_session/options/struct.Options.html) has the field `test`. This journey has been ~7 or 8 hours in 3 days, it's a very hard thing to find, so this PR adds a mini-function to check if the current crate is a testing one. So that no one has to travel through the same as me, and can just search for `is_test_crate` in the documentation.
2 parents 4e165c1 + 2c97676 commit 3dfda2c

File tree

7 files changed

+12
-7
lines changed

7 files changed

+12
-7
lines changed

compiler/rustc_builtin_macros/src/test_harness.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub fn inject(krate: &mut ast::Crate, sess: &Session, resolver: &mut dyn Resolve
5353
// even in non-test builds
5454
let test_runner = get_test_runner(span_diagnostic, &krate);
5555

56-
if sess.opts.test {
56+
if sess.is_test_crate() {
5757
let panic_strategy = match (panic_strategy, sess.opts.unstable_opts.panic_abort_tests) {
5858
(PanicStrategy::Abort, true) => PanicStrategy::Abort,
5959
(PanicStrategy::Abort, false) => {

compiler/rustc_codegen_ssa/src/back/link.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2301,7 +2301,7 @@ fn add_native_libs_from_crate(
23012301
|| (whole_archive == None
23022302
&& bundle
23032303
&& cnum == LOCAL_CRATE
2304-
&& sess.opts.test);
2304+
&& sess.is_test_crate());
23052305

23062306
if bundle && cnum != LOCAL_CRATE {
23072307
if let Some(filename) = lib.filename {

compiler/rustc_interface/src/passes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ fn configure_and_expand(
230230
features: Some(features),
231231
recursion_limit,
232232
trace_mac: sess.opts.unstable_opts.trace_macros,
233-
should_test: sess.opts.test,
233+
should_test: sess.is_test_crate(),
234234
span_debug: sess.opts.unstable_opts.span_debug,
235235
proc_macro_backtrace: sess.opts.unstable_opts.proc_macro_backtrace,
236236
..rustc_expand::expand::ExpansionConfig::default(crate_name.to_string())
@@ -292,7 +292,7 @@ fn configure_and_expand(
292292
}
293293

294294
sess.time("maybe_create_a_macro_crate", || {
295-
let is_test_crate = sess.opts.test;
295+
let is_test_crate = sess.is_test_crate();
296296
rustc_builtin_macros::proc_macro_harness::inject(
297297
&mut krate,
298298
sess,

compiler/rustc_passes/src/stability.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ struct MissingStabilityAnnotations<'tcx> {
530530
impl<'tcx> MissingStabilityAnnotations<'tcx> {
531531
fn check_missing_stability(&self, def_id: LocalDefId, span: Span) {
532532
let stab = self.tcx.stability().local_stability(def_id);
533-
if !self.tcx.sess.opts.test
533+
if !self.tcx.sess.is_test_crate()
534534
&& stab.is_none()
535535
&& self.effective_visibilities.is_reachable(def_id)
536536
{

compiler/rustc_resolve/src/check_unused.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ impl Resolver<'_, '_> {
393393
// If we are in the `--test` mode, suppress a help that adds the `#[cfg(test)]`
394394
// attribute; however, if not, suggest adding the attribute. There is no way to
395395
// retrieve attributes here because we do not have a `TyCtxt` yet.
396-
let test_module_span = if tcx.sess.opts.test {
396+
let test_module_span = if tcx.sess.is_test_crate() {
397397
None
398398
} else {
399399
let parent_module = visitor.r.get_nearest_non_block_module(

compiler/rustc_session/src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1258,7 +1258,7 @@ pub fn build_configuration(sess: &Session, mut user_cfg: CrateConfig) -> CrateCo
12581258
// some default and generated configuration items.
12591259
let default_cfg = default_configuration(sess);
12601260
// If the user wants a test runner, then add the test cfg.
1261-
if sess.opts.test {
1261+
if sess.is_test_crate() {
12621262
user_cfg.insert((sym::test, None));
12631263
}
12641264
user_cfg.extend(default_cfg.iter().cloned());

compiler/rustc_session/src/session.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,11 @@ impl Session {
294294
self.crate_types.get().unwrap().as_slice()
295295
}
296296

297+
/// Returns true if the crate is a testing one.
298+
pub fn is_test_crate(&self) -> bool {
299+
self.opts.test
300+
}
301+
297302
pub fn needs_crate_hash(&self) -> bool {
298303
// Why is the crate hash needed for these configurations?
299304
// - debug_assertions: for the "fingerprint the result" check in

0 commit comments

Comments
 (0)