From f27fbf9a4a9b0191338b59d34dda0ddcfe833754 Mon Sep 17 00:00:00 2001 From: Seo Sanghyeon Date: Tue, 24 May 2016 00:44:44 +0900 Subject: [PATCH 1/2] Do not inject test harness for --cfg test --- src/librustc_driver/driver.rs | 6 +++++- src/libsyntax/test.rs | 6 +----- src/test/run-pass/test-vs-cfg-test.rs | 18 ++++++++++++++++++ 3 files changed, 24 insertions(+), 6 deletions(-) create mode 100644 src/test/run-pass/test-vs-cfg-test.rs diff --git a/src/librustc_driver/driver.rs b/src/librustc_driver/driver.rs index 1f3df1ff6f2..2965c763ccc 100644 --- a/src/librustc_driver/driver.rs +++ b/src/librustc_driver/driver.rs @@ -743,7 +743,11 @@ pub fn phase_2_configure_and_expand(sess: &Session, })?; krate = time(time_passes, "maybe building test harness", || { - syntax::test::modify_for_testing(&sess.parse_sess, &sess.opts.cfg, krate, sess.diagnostic()) + syntax::test::modify_for_testing(&sess.parse_sess, + sess.opts.test, + &sess.opts.cfg, + krate, + sess.diagnostic()) }); krate = time(time_passes, diff --git a/src/libsyntax/test.rs b/src/libsyntax/test.rs index 8eeb61e0de4..56485433101 100644 --- a/src/libsyntax/test.rs +++ b/src/libsyntax/test.rs @@ -68,14 +68,10 @@ struct TestCtxt<'a> { // Traverse the crate, collecting all the test functions, eliding any // existing main functions, and synthesizing a main test harness pub fn modify_for_testing(sess: &ParseSess, + should_test: bool, cfg: &ast::CrateConfig, krate: ast::Crate, span_diagnostic: &errors::Handler) -> ast::Crate { - // We generate the test harness when building in the 'test' - // configuration, either with the '--test' or '--cfg test' - // command line options. - let should_test = attr::contains_name(&krate.config, "test"); - // Check for #[reexport_test_harness_main = "some_name"] which // creates a `use some_name = __test::main;`. This needs to be // unconditional, so that the attribute is still marked as used in diff --git a/src/test/run-pass/test-vs-cfg-test.rs b/src/test/run-pass/test-vs-cfg-test.rs new file mode 100644 index 00000000000..708fde59888 --- /dev/null +++ b/src/test/run-pass/test-vs-cfg-test.rs @@ -0,0 +1,18 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// compile-flags: --cfg test + +// Make sure `--cfg test` does not inject test harness + +#[test] +fn test() { panic!(); } + +fn main() {} From 3c4eb014829110a98b4d9d65336fe9fb529ba897 Mon Sep 17 00:00:00 2001 From: Seo Sanghyeon Date: Tue, 24 May 2016 02:00:39 +0900 Subject: [PATCH 2/2] Remove unused field and argument --- src/librustc_driver/driver.rs | 1 - src/libsyntax/test.rs | 8 ++------ 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/src/librustc_driver/driver.rs b/src/librustc_driver/driver.rs index 2965c763ccc..34527d1689c 100644 --- a/src/librustc_driver/driver.rs +++ b/src/librustc_driver/driver.rs @@ -745,7 +745,6 @@ pub fn phase_2_configure_and_expand(sess: &Session, krate = time(time_passes, "maybe building test harness", || { syntax::test::modify_for_testing(&sess.parse_sess, sess.opts.test, - &sess.opts.cfg, krate, sess.diagnostic()) }); diff --git a/src/libsyntax/test.rs b/src/libsyntax/test.rs index 56485433101..6785a8690d4 100644 --- a/src/libsyntax/test.rs +++ b/src/libsyntax/test.rs @@ -59,7 +59,6 @@ struct TestCtxt<'a> { testfns: Vec, reexport_test_harness_main: Option, is_test_crate: bool, - config: ast::CrateConfig, // top-level re-export submodule, filled out after folding is finished toplevel_reexport: Option, @@ -69,7 +68,6 @@ struct TestCtxt<'a> { // existing main functions, and synthesizing a main test harness pub fn modify_for_testing(sess: &ParseSess, should_test: bool, - cfg: &ast::CrateConfig, krate: ast::Crate, span_diagnostic: &errors::Handler) -> ast::Crate { // Check for #[reexport_test_harness_main = "some_name"] which @@ -81,7 +79,7 @@ pub fn modify_for_testing(sess: &ParseSess, "reexport_test_harness_main"); if should_test { - generate_test_harness(sess, reexport_test_harness_main, krate, cfg, span_diagnostic) + generate_test_harness(sess, reexport_test_harness_main, krate, span_diagnostic) } else { strip_test_functions(span_diagnostic, krate) } @@ -267,7 +265,6 @@ fn mk_reexport_mod(cx: &mut TestCtxt, tests: Vec, fn generate_test_harness(sess: &ParseSess, reexport_test_harness_main: Option, krate: ast::Crate, - cfg: &ast::CrateConfig, sd: &errors::Handler) -> ast::Crate { // Remove the entry points let mut cleaner = EntryPointCleaner { depth: 0 }; @@ -277,14 +274,13 @@ fn generate_test_harness(sess: &ParseSess, let mut cx: TestCtxt = TestCtxt { sess: sess, span_diagnostic: sd, - ext_cx: ExtCtxt::new(sess, cfg.clone(), + ext_cx: ExtCtxt::new(sess, vec![], ExpansionConfig::default("test".to_string()), &mut feature_gated_cfgs), path: Vec::new(), testfns: Vec::new(), reexport_test_harness_main: reexport_test_harness_main, is_test_crate: is_test_crate(&krate), - config: krate.config.clone(), toplevel_reexport: None, }; cx.ext_cx.crate_root = Some("std");