Skip to content

Commit 66b9ade

Browse files
committed
Strip #[test] nodes during cfg processing on non-test builds.
1 parent 0554aba commit 66b9ade

File tree

4 files changed

+20
-16
lines changed

4 files changed

+20
-16
lines changed

src/librustc_driver/driver.rs

+2
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,7 @@ pub fn phase_2_configure_and_expand<'a>(sess: &Session,
582582
sess.track_errors(|| {
583583
syntax::config::strip_unconfigured_items(sess.diagnostic(),
584584
krate,
585+
sess.opts.test,
585586
&mut feature_gated_cfgs)
586587
})
587588
})?;
@@ -692,6 +693,7 @@ pub fn phase_2_configure_and_expand<'a>(sess: &Session,
692693
features: Some(&features),
693694
recursion_limit: sess.recursion_limit.get(),
694695
trace_mac: sess.opts.debugging_opts.trace_macros,
696+
should_test: sess.opts.test,
695697
};
696698
let mut loader = macro_import::MacroLoader::new(sess, &cstore, crate_name);
697699
let mut ecx = syntax::ext::base::ExtCtxt::new(&sess.parse_sess,

src/libsyntax/config.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,19 @@ pub trait CfgFolder: fold::Folder {
4242
/// configuration.
4343
pub struct StripUnconfigured<'a> {
4444
diag: CfgDiagReal<'a, 'a>,
45+
should_test: bool,
4546
config: &'a ast::CrateConfig,
4647
}
4748

4849
impl<'a> StripUnconfigured<'a> {
4950
pub fn new(config: &'a ast::CrateConfig,
51+
should_test: bool,
5052
diagnostic: &'a Handler,
5153
feature_gated_cfgs: &'a mut Vec<GatedCfgAttr>)
5254
-> Self {
5355
StripUnconfigured {
5456
config: config,
57+
should_test: should_test,
5558
diag: CfgDiagReal { diag: diagnostic, feature_gated_cfgs: feature_gated_cfgs },
5659
}
5760
}
@@ -96,6 +99,11 @@ impl<'a> CfgFolder for StripUnconfigured<'a> {
9699
// configuration based on the item's attributes
97100
fn in_cfg(&mut self, attrs: &[ast::Attribute]) -> bool {
98101
attrs.iter().all(|attr| {
102+
// When not compiling with --test we should not compile the #[test] functions
103+
if !self.should_test && is_test_or_bench(attr) {
104+
return false;
105+
}
106+
99107
let mis = match attr.node.value.node {
100108
ast::MetaItemKind::List(_, ref mis) if is_cfg(&attr) => mis,
101109
_ => return true
@@ -135,12 +143,12 @@ impl<'a> CfgFolder for StripUnconfigured<'a> {
135143

136144
// Support conditional compilation by transforming the AST, stripping out
137145
// any items that do not belong in the current configuration
138-
pub fn strip_unconfigured_items(diagnostic: &Handler, krate: ast::Crate,
146+
pub fn strip_unconfigured_items(diagnostic: &Handler, krate: ast::Crate, should_test: bool,
139147
feature_gated_cfgs: &mut Vec<GatedCfgAttr>)
140148
-> ast::Crate
141149
{
142150
let config = &krate.config.clone();
143-
StripUnconfigured::new(config, diagnostic, feature_gated_cfgs).fold_crate(krate)
151+
StripUnconfigured::new(config, should_test, diagnostic, feature_gated_cfgs).fold_crate(krate)
144152
}
145153

146154
impl<T: CfgFolder> fold::Folder for T {
@@ -278,6 +286,10 @@ fn is_cfg(attr: &ast::Attribute) -> bool {
278286
attr.check_name("cfg")
279287
}
280288

289+
fn is_test_or_bench(attr: &ast::Attribute) -> bool {
290+
attr.check_name("test") || attr.check_name("bench")
291+
}
292+
281293
pub trait CfgDiag {
282294
fn emit_error<F>(&mut self, f: F) where F: FnMut(&Handler);
283295
fn flag_gated<F>(&mut self, f: F) where F: FnMut(&mut Vec<GatedCfgAttr>);

src/libsyntax/ext/expand.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1001,6 +1001,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
10011001

10021002
fn strip_unconfigured(&mut self) -> StripUnconfigured {
10031003
StripUnconfigured::new(&self.cx.cfg,
1004+
self.cx.ecfg.should_test,
10041005
&self.cx.parse_sess.span_diagnostic,
10051006
self.cx.feature_gated_cfgs)
10061007
}
@@ -1106,6 +1107,7 @@ pub struct ExpansionConfig<'feat> {
11061107
pub features: Option<&'feat Features>,
11071108
pub recursion_limit: usize,
11081109
pub trace_mac: bool,
1110+
pub should_test: bool, // If false, strip `#[test]` nodes
11091111
}
11101112

11111113
macro_rules! feature_tests {
@@ -1128,6 +1130,7 @@ impl<'feat> ExpansionConfig<'feat> {
11281130
features: None,
11291131
recursion_limit: 64,
11301132
trace_mac: false,
1133+
should_test: false,
11311134
}
11321135
}
11331136

src/libsyntax/test.rs

+1-14
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub fn modify_for_testing(sess: &ParseSess,
8181
if should_test {
8282
generate_test_harness(sess, reexport_test_harness_main, krate, span_diagnostic)
8383
} else {
84-
strip_test_functions(krate)
84+
krate
8585
}
8686
}
8787

@@ -306,19 +306,6 @@ fn generate_test_harness(sess: &ParseSess,
306306
return res;
307307
}
308308

309-
fn strip_test_functions(krate: ast::Crate) -> ast::Crate {
310-
// When not compiling with --test we should not compile the
311-
// #[test] functions
312-
struct StripTests;
313-
impl config::CfgFolder for StripTests {
314-
fn in_cfg(&mut self, attrs: &[ast::Attribute]) -> bool {
315-
!attr::contains_name(attrs, "test") && !attr::contains_name(attrs, "bench")
316-
}
317-
}
318-
319-
StripTests.fold_crate(krate)
320-
}
321-
322309
/// Craft a span that will be ignored by the stability lint's
323310
/// call to codemap's is_internal check.
324311
/// The expanded code calls some unstable functions in the test crate.

0 commit comments

Comments
 (0)