diff --git a/src/libsyntax/config.rs b/src/libsyntax/config.rs index a22e08d4275..1948c3afdbd 100644 --- a/src/libsyntax/config.rs +++ b/src/libsyntax/config.rs @@ -26,14 +26,14 @@ pub trait CfgFolder: fold::Folder { /// A folder that strips out items that do not belong in the current /// configuration. -struct Context<'a, F> where F: FnMut(&[ast::Attribute]) -> bool { - in_cfg: F, - diagnostic: &'a Handler, +pub struct StripUnconfigured<'a> { + diag: CfgDiagReal<'a, 'a>, + config: &'a ast::CrateConfig, } -impl<'a, F: FnMut(&[ast::Attribute]) -> bool> CfgFolder for Context<'a, F> { +impl<'a> CfgFolder for StripUnconfigured<'a> { fn configure(&mut self, node: T) -> Option { - if (self.in_cfg)(node.attrs()) { + if in_cfg(self.config, node.attrs(), &mut self.diag) { Some(node) } else { None @@ -43,7 +43,7 @@ impl<'a, F: FnMut(&[ast::Attribute]) -> bool> CfgFolder for Context<'a, F> { fn visit_unconfigurable_expr(&mut self, expr: &ast::Expr) { if let Some(attr) = expr.attrs().iter().find(|a| is_cfg(a)) { let msg = "removing an expression is not supported in this position"; - self.diagnostic.span_err(attr.span, msg); + self.diag.diag.span_err(attr.span, msg); } } } @@ -58,16 +58,14 @@ pub fn strip_unconfigured_items(diagnostic: &Handler, krate: ast::Crate, check_for_gated_stmt_expr_attributes(&krate, feature_gated_cfgs); let krate = process_cfg_attr(diagnostic, krate, feature_gated_cfgs); - let config = krate.config.clone(); - strip_items(diagnostic, - krate, - |attrs| { - let mut diag = CfgDiagReal { - diag: diagnostic, - feature_gated_cfgs: feature_gated_cfgs, - }; - in_cfg(&config, attrs, &mut diag) - }) + + StripUnconfigured { + config: &krate.config.clone(), + diag: CfgDiagReal { + diag: diagnostic, + feature_gated_cfgs: feature_gated_cfgs, + }, + }.fold_crate(krate) } impl fold::Folder for T { @@ -158,17 +156,6 @@ impl fold::Folder for T { } } -pub fn strip_items<'a, F>(diagnostic: &'a Handler, - krate: ast::Crate, in_cfg: F) -> ast::Crate where - F: FnMut(&[ast::Attribute]) -> bool, -{ - let mut ctxt = Context { - in_cfg: in_cfg, - diagnostic: diagnostic, - }; - ctxt.fold_crate(krate) -} - fn fold_expr(folder: &mut F, expr: P) -> P { expr.map(|ast::Expr {id, span, node, attrs}| { fold::noop_fold_expr(ast::Expr { diff --git a/src/libsyntax/test.rs b/src/libsyntax/test.rs index 8eeb61e0de4..84c7250fac6 100644 --- a/src/libsyntax/test.rs +++ b/src/libsyntax/test.rs @@ -87,7 +87,7 @@ pub fn modify_for_testing(sess: &ParseSess, if should_test { generate_test_harness(sess, reexport_test_harness_main, krate, cfg, span_diagnostic) } else { - strip_test_functions(span_diagnostic, krate) + strip_test_functions(krate) } } @@ -312,14 +312,22 @@ fn generate_test_harness(sess: &ParseSess, return res; } -fn strip_test_functions(diagnostic: &errors::Handler, krate: ast::Crate) - -> ast::Crate { +fn strip_test_functions(krate: ast::Crate) -> ast::Crate { // When not compiling with --test we should not compile the // #[test] functions - config::strip_items(diagnostic, krate, |attrs| { - !attr::contains_name(&attrs[..], "test") && - !attr::contains_name(&attrs[..], "bench") - }) + struct StripTests; + impl config::CfgFolder for StripTests { + fn configure(&mut self, node: T) -> Option { + let strip_node = { + let attrs = node.attrs(); + attr::contains_name(attrs, "test") || attr::contains_name(attrs, "bench") + }; + + if strip_node { None } else { Some(node) } + } + } + + StripTests.fold_crate(krate) } /// Craft a span that will be ignored by the stability lint's