Skip to content

Commit a306f85

Browse files
committed
Implement CfgFolder directly instead of passing a closure to strip_items
1 parent f3e8076 commit a306f85

File tree

2 files changed

+29
-34
lines changed

2 files changed

+29
-34
lines changed

src/libsyntax/config.rs

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ pub trait CfgFolder: fold::Folder {
2626

2727
/// A folder that strips out items that do not belong in the current
2828
/// configuration.
29-
struct Context<'a, F> where F: FnMut(&[ast::Attribute]) -> bool {
30-
in_cfg: F,
31-
diagnostic: &'a Handler,
29+
pub struct StripUnconfigured<'a> {
30+
diag: CfgDiagReal<'a, 'a>,
31+
config: &'a ast::CrateConfig,
3232
}
3333

34-
impl<'a, F: FnMut(&[ast::Attribute]) -> bool> CfgFolder for Context<'a, F> {
34+
impl<'a> CfgFolder for StripUnconfigured<'a> {
3535
fn configure<T: HasAttrs>(&mut self, node: T) -> Option<T> {
36-
if (self.in_cfg)(node.attrs()) {
36+
if in_cfg(self.config, node.attrs(), &mut self.diag) {
3737
Some(node)
3838
} else {
3939
None
@@ -43,7 +43,7 @@ impl<'a, F: FnMut(&[ast::Attribute]) -> bool> CfgFolder for Context<'a, F> {
4343
fn visit_unconfigurable_expr(&mut self, expr: &ast::Expr) {
4444
if let Some(attr) = expr.attrs().iter().find(|a| is_cfg(a)) {
4545
let msg = "removing an expression is not supported in this position";
46-
self.diagnostic.span_err(attr.span, msg);
46+
self.diag.diag.span_err(attr.span, msg);
4747
}
4848
}
4949
}
@@ -58,16 +58,14 @@ pub fn strip_unconfigured_items(diagnostic: &Handler, krate: ast::Crate,
5858
check_for_gated_stmt_expr_attributes(&krate, feature_gated_cfgs);
5959

6060
let krate = process_cfg_attr(diagnostic, krate, feature_gated_cfgs);
61-
let config = krate.config.clone();
62-
strip_items(diagnostic,
63-
krate,
64-
|attrs| {
65-
let mut diag = CfgDiagReal {
66-
diag: diagnostic,
67-
feature_gated_cfgs: feature_gated_cfgs,
68-
};
69-
in_cfg(&config, attrs, &mut diag)
70-
})
61+
62+
StripUnconfigured {
63+
config: &krate.config.clone(),
64+
diag: CfgDiagReal {
65+
diag: diagnostic,
66+
feature_gated_cfgs: feature_gated_cfgs,
67+
},
68+
}.fold_crate(krate)
7169
}
7270

7371
impl<T: CfgFolder> fold::Folder for T {
@@ -158,17 +156,6 @@ impl<T: CfgFolder> fold::Folder for T {
158156
}
159157
}
160158

161-
pub fn strip_items<'a, F>(diagnostic: &'a Handler,
162-
krate: ast::Crate, in_cfg: F) -> ast::Crate where
163-
F: FnMut(&[ast::Attribute]) -> bool,
164-
{
165-
let mut ctxt = Context {
166-
in_cfg: in_cfg,
167-
diagnostic: diagnostic,
168-
};
169-
ctxt.fold_crate(krate)
170-
}
171-
172159
fn fold_expr<F: CfgFolder>(folder: &mut F, expr: P<ast::Expr>) -> P<ast::Expr> {
173160
expr.map(|ast::Expr {id, span, node, attrs}| {
174161
fold::noop_fold_expr(ast::Expr {

src/libsyntax/test.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ pub fn modify_for_testing(sess: &ParseSess,
8787
if should_test {
8888
generate_test_harness(sess, reexport_test_harness_main, krate, cfg, span_diagnostic)
8989
} else {
90-
strip_test_functions(span_diagnostic, krate)
90+
strip_test_functions(krate)
9191
}
9292
}
9393

@@ -312,14 +312,22 @@ fn generate_test_harness(sess: &ParseSess,
312312
return res;
313313
}
314314

315-
fn strip_test_functions(diagnostic: &errors::Handler, krate: ast::Crate)
316-
-> ast::Crate {
315+
fn strip_test_functions(krate: ast::Crate) -> ast::Crate {
317316
// When not compiling with --test we should not compile the
318317
// #[test] functions
319-
config::strip_items(diagnostic, krate, |attrs| {
320-
!attr::contains_name(&attrs[..], "test") &&
321-
!attr::contains_name(&attrs[..], "bench")
322-
})
318+
struct StripTests;
319+
impl config::CfgFolder for StripTests {
320+
fn configure<T: attr::HasAttrs>(&mut self, node: T) -> Option<T> {
321+
let strip_node = {
322+
let attrs = node.attrs();
323+
attr::contains_name(attrs, "test") || attr::contains_name(attrs, "bench")
324+
};
325+
326+
if strip_node { None } else { Some(node) }
327+
}
328+
}
329+
330+
StripTests.fold_crate(krate)
323331
}
324332

325333
/// Craft a span that will be ignored by the stability lint's

0 commit comments

Comments
 (0)