diff options
| author | Jeffrey Seyfried <jeffrey.seyfried@gmail.com> | 2016-06-01 01:27:12 +0000 |
|---|---|---|
| committer | Jeffrey Seyfried <jeffrey.seyfried@gmail.com> | 2016-06-11 03:13:44 +0000 |
| commit | 66b9ade341360a201c5ba37a9a2d6b6c1597a44b (patch) | |
| tree | 5d6bde398410e183c477459b0be1222da8890f9b | |
| parent | 0554abac637800415bb1b30d8656898552a55ea0 (diff) | |
| download | rust-66b9ade341360a201c5ba37a9a2d6b6c1597a44b.tar.gz rust-66b9ade341360a201c5ba37a9a2d6b6c1597a44b.zip | |
Strip `#[test]` nodes during `cfg` processing on non-test builds.
| -rw-r--r-- | src/librustc_driver/driver.rs | 2 | ||||
| -rw-r--r-- | src/libsyntax/config.rs | 16 | ||||
| -rw-r--r-- | src/libsyntax/ext/expand.rs | 3 | ||||
| -rw-r--r-- | src/libsyntax/test.rs | 15 |
4 files changed, 20 insertions, 16 deletions
diff --git a/src/librustc_driver/driver.rs b/src/librustc_driver/driver.rs index 06d8a6c9ac8..af68b7632dd 100644 --- a/src/librustc_driver/driver.rs +++ b/src/librustc_driver/driver.rs @@ -582,6 +582,7 @@ pub fn phase_2_configure_and_expand<'a>(sess: &Session, sess.track_errors(|| { syntax::config::strip_unconfigured_items(sess.diagnostic(), krate, + sess.opts.test, &mut feature_gated_cfgs) }) })?; @@ -692,6 +693,7 @@ pub fn phase_2_configure_and_expand<'a>(sess: &Session, features: Some(&features), recursion_limit: sess.recursion_limit.get(), trace_mac: sess.opts.debugging_opts.trace_macros, + should_test: sess.opts.test, }; let mut loader = macro_import::MacroLoader::new(sess, &cstore, crate_name); let mut ecx = syntax::ext::base::ExtCtxt::new(&sess.parse_sess, diff --git a/src/libsyntax/config.rs b/src/libsyntax/config.rs index 41ad1611e3e..5ede9badd6b 100644 --- a/src/libsyntax/config.rs +++ b/src/libsyntax/config.rs @@ -42,16 +42,19 @@ pub trait CfgFolder: fold::Folder { /// configuration. pub struct StripUnconfigured<'a> { diag: CfgDiagReal<'a, 'a>, + should_test: bool, config: &'a ast::CrateConfig, } impl<'a> StripUnconfigured<'a> { pub fn new(config: &'a ast::CrateConfig, + should_test: bool, diagnostic: &'a Handler, feature_gated_cfgs: &'a mut Vec<GatedCfgAttr>) -> Self { StripUnconfigured { config: config, + should_test: should_test, diag: CfgDiagReal { diag: diagnostic, feature_gated_cfgs: feature_gated_cfgs }, } } @@ -96,6 +99,11 @@ impl<'a> CfgFolder for StripUnconfigured<'a> { // configuration based on the item's attributes fn in_cfg(&mut self, attrs: &[ast::Attribute]) -> bool { attrs.iter().all(|attr| { + // When not compiling with --test we should not compile the #[test] functions + if !self.should_test && is_test_or_bench(attr) { + return false; + } + let mis = match attr.node.value.node { ast::MetaItemKind::List(_, ref mis) if is_cfg(&attr) => mis, _ => return true @@ -135,12 +143,12 @@ impl<'a> CfgFolder for StripUnconfigured<'a> { // Support conditional compilation by transforming the AST, stripping out // any items that do not belong in the current configuration -pub fn strip_unconfigured_items(diagnostic: &Handler, krate: ast::Crate, +pub fn strip_unconfigured_items(diagnostic: &Handler, krate: ast::Crate, should_test: bool, feature_gated_cfgs: &mut Vec<GatedCfgAttr>) -> ast::Crate { let config = &krate.config.clone(); - StripUnconfigured::new(config, diagnostic, feature_gated_cfgs).fold_crate(krate) + StripUnconfigured::new(config, should_test, diagnostic, feature_gated_cfgs).fold_crate(krate) } impl<T: CfgFolder> fold::Folder for T { @@ -278,6 +286,10 @@ fn is_cfg(attr: &ast::Attribute) -> bool { attr.check_name("cfg") } +fn is_test_or_bench(attr: &ast::Attribute) -> bool { + attr.check_name("test") || attr.check_name("bench") +} + pub trait CfgDiag { fn emit_error<F>(&mut self, f: F) where F: FnMut(&Handler); fn flag_gated<F>(&mut self, f: F) where F: FnMut(&mut Vec<GatedCfgAttr>); diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index 15d192b59b8..31971842d63 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -1001,6 +1001,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> { fn strip_unconfigured(&mut self) -> StripUnconfigured { StripUnconfigured::new(&self.cx.cfg, + self.cx.ecfg.should_test, &self.cx.parse_sess.span_diagnostic, self.cx.feature_gated_cfgs) } @@ -1106,6 +1107,7 @@ pub struct ExpansionConfig<'feat> { pub features: Option<&'feat Features>, pub recursion_limit: usize, pub trace_mac: bool, + pub should_test: bool, // If false, strip `#[test]` nodes } macro_rules! feature_tests { @@ -1128,6 +1130,7 @@ impl<'feat> ExpansionConfig<'feat> { features: None, recursion_limit: 64, trace_mac: false, + should_test: false, } } diff --git a/src/libsyntax/test.rs b/src/libsyntax/test.rs index 2ac4aac65de..6e29505f00a 100644 --- a/src/libsyntax/test.rs +++ b/src/libsyntax/test.rs @@ -81,7 +81,7 @@ pub fn modify_for_testing(sess: &ParseSess, if should_test { generate_test_harness(sess, reexport_test_harness_main, krate, span_diagnostic) } else { - strip_test_functions(krate) + krate } } @@ -306,19 +306,6 @@ fn generate_test_harness(sess: &ParseSess, return res; } -fn strip_test_functions(krate: ast::Crate) -> ast::Crate { - // When not compiling with --test we should not compile the - // #[test] functions - struct StripTests; - impl config::CfgFolder for StripTests { - fn in_cfg(&mut self, attrs: &[ast::Attribute]) -> bool { - !attr::contains_name(attrs, "test") && !attr::contains_name(attrs, "bench") - } - } - - StripTests.fold_crate(krate) -} - /// Craft a span that will be ignored by the stability lint's /// call to codemap's is_internal check. /// The expanded code calls some unstable functions in the test crate. |
