Skip to content

Commit 623af22

Browse files
committed
Auto merge of #33821 - sanxiyn:cfg-test, r=nikomatsakis
Do not inject test harness for --cfg test Fix #33670.
2 parents f1776fe + 3c4eb01 commit 623af22

File tree

3 files changed

+25
-12
lines changed

3 files changed

+25
-12
lines changed

src/librustc_driver/driver.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,10 @@ pub fn phase_2_configure_and_expand(sess: &Session,
734734
})?;
735735

736736
krate = time(time_passes, "maybe building test harness", || {
737-
syntax::test::modify_for_testing(&sess.parse_sess, &sess.opts.cfg, krate, sess.diagnostic())
737+
syntax::test::modify_for_testing(&sess.parse_sess,
738+
sess.opts.test,
739+
krate,
740+
sess.diagnostic())
738741
});
739742

740743
krate = time(time_passes,

src/libsyntax/test.rs

+3-11
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ struct TestCtxt<'a> {
5959
testfns: Vec<Test>,
6060
reexport_test_harness_main: Option<InternedString>,
6161
is_test_crate: bool,
62-
config: ast::CrateConfig,
6362

6463
// top-level re-export submodule, filled out after folding is finished
6564
toplevel_reexport: Option<ast::Ident>,
@@ -68,14 +67,9 @@ struct TestCtxt<'a> {
6867
// Traverse the crate, collecting all the test functions, eliding any
6968
// existing main functions, and synthesizing a main test harness
7069
pub fn modify_for_testing(sess: &ParseSess,
71-
cfg: &ast::CrateConfig,
70+
should_test: bool,
7271
krate: ast::Crate,
7372
span_diagnostic: &errors::Handler) -> ast::Crate {
74-
// We generate the test harness when building in the 'test'
75-
// configuration, either with the '--test' or '--cfg test'
76-
// command line options.
77-
let should_test = attr::contains_name(&krate.config, "test");
78-
7973
// Check for #[reexport_test_harness_main = "some_name"] which
8074
// creates a `use some_name = __test::main;`. This needs to be
8175
// unconditional, so that the attribute is still marked as used in
@@ -85,7 +79,7 @@ pub fn modify_for_testing(sess: &ParseSess,
8579
"reexport_test_harness_main");
8680

8781
if should_test {
88-
generate_test_harness(sess, reexport_test_harness_main, krate, cfg, span_diagnostic)
82+
generate_test_harness(sess, reexport_test_harness_main, krate, span_diagnostic)
8983
} else {
9084
strip_test_functions(krate)
9185
}
@@ -271,7 +265,6 @@ fn mk_reexport_mod(cx: &mut TestCtxt, tests: Vec<ast::Ident>,
271265
fn generate_test_harness(sess: &ParseSess,
272266
reexport_test_harness_main: Option<InternedString>,
273267
krate: ast::Crate,
274-
cfg: &ast::CrateConfig,
275268
sd: &errors::Handler) -> ast::Crate {
276269
// Remove the entry points
277270
let mut cleaner = EntryPointCleaner { depth: 0 };
@@ -281,14 +274,13 @@ fn generate_test_harness(sess: &ParseSess,
281274
let mut cx: TestCtxt = TestCtxt {
282275
sess: sess,
283276
span_diagnostic: sd,
284-
ext_cx: ExtCtxt::new(sess, cfg.clone(),
277+
ext_cx: ExtCtxt::new(sess, vec![],
285278
ExpansionConfig::default("test".to_string()),
286279
&mut feature_gated_cfgs),
287280
path: Vec::new(),
288281
testfns: Vec::new(),
289282
reexport_test_harness_main: reexport_test_harness_main,
290283
is_test_crate: is_test_crate(&krate),
291-
config: krate.config.clone(),
292284
toplevel_reexport: None,
293285
};
294286
cx.ext_cx.crate_root = Some("std");

src/test/run-pass/test-vs-cfg-test.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags: --cfg test
12+
13+
// Make sure `--cfg test` does not inject test harness
14+
15+
#[test]
16+
fn test() { panic!(); }
17+
18+
fn main() {}

0 commit comments

Comments
 (0)