Skip to content

Commit 90d5e6f

Browse files
committed
Process cfg_attr attributes on non-optional expressions
1 parent c71e5aa commit 90d5e6f

File tree

2 files changed

+37
-38
lines changed

2 files changed

+37
-38
lines changed

src/libsyntax/config.rs

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,15 @@ use ptr::P;
1919
use util::small_vector::SmallVector;
2020

2121
pub trait CfgFolder: fold::Folder {
22-
fn configure<T: HasAttrs>(&mut self, node: T) -> Option<T>;
22+
fn in_cfg(&mut self, attrs: &[ast::Attribute]) -> bool;
23+
fn process_attrs<T: HasAttrs>(&mut self, node: T) -> T { node }
2324
fn visit_stmt_or_expr_attrs(&mut self, _attrs: &[ast::Attribute]) {}
24-
fn visit_unconfigurable_expr(&mut self, _expr: &ast::Expr) {}
25+
fn visit_unremovable_expr(&mut self, _expr: &ast::Expr) {}
26+
27+
fn configure<T: HasAttrs>(&mut self, node: T) -> Option<T> {
28+
let node = self.process_attrs(node);
29+
if self.in_cfg(node.attrs()) { Some(node) } else { None }
30+
}
2531
}
2632

2733
/// A folder that strips out items that do not belong in the current
@@ -42,30 +48,6 @@ impl<'a> StripUnconfigured<'a> {
4248
}
4349
}
4450

45-
// Determine if an item should be translated in the current crate
46-
// configuration based on the item's attributes
47-
fn in_cfg(&mut self, attrs: &[ast::Attribute]) -> bool {
48-
attrs.iter().all(|attr| {
49-
let mis = match attr.node.value.node {
50-
ast::MetaItemKind::List(_, ref mis) if is_cfg(&attr) => mis,
51-
_ => return true
52-
};
53-
54-
if mis.len() != 1 {
55-
self.diag.emit_error(|diagnostic| {
56-
diagnostic.span_err(attr.span, "expected 1 cfg-pattern");
57-
});
58-
return true;
59-
}
60-
61-
attr::cfg_matches(self.config, &mis[0], &mut self.diag)
62-
})
63-
}
64-
65-
fn process_cfg_attrs(&mut self, attrs: Vec<ast::Attribute>) -> Vec<ast::Attribute> {
66-
attrs.into_iter().filter_map(|attr| self.process_cfg_attr(attr)).collect()
67-
}
68-
6951
fn process_cfg_attr(&mut self, attr: ast::Attribute) -> Option<ast::Attribute> {
7052
if !attr.check_name("cfg_attr") {
7153
return Some(attr);
@@ -102,9 +84,30 @@ impl<'a> StripUnconfigured<'a> {
10284
}
10385

10486
impl<'a> CfgFolder for StripUnconfigured<'a> {
105-
fn configure<T: HasAttrs>(&mut self, node: T) -> Option<T> {
106-
let node = node.map_attrs(|attrs| self.process_cfg_attrs(attrs));
107-
if self.in_cfg(node.attrs()) { Some(node) } else { None }
87+
// Determine if an item should be translated in the current crate
88+
// configuration based on the item's attributes
89+
fn in_cfg(&mut self, attrs: &[ast::Attribute]) -> bool {
90+
attrs.iter().all(|attr| {
91+
let mis = match attr.node.value.node {
92+
ast::MetaItemKind::List(_, ref mis) if is_cfg(&attr) => mis,
93+
_ => return true
94+
};
95+
96+
if mis.len() != 1 {
97+
self.diag.emit_error(|diagnostic| {
98+
diagnostic.span_err(attr.span, "expected 1 cfg-pattern");
99+
});
100+
return true;
101+
}
102+
103+
attr::cfg_matches(self.config, &mis[0], &mut self.diag)
104+
})
105+
}
106+
107+
fn process_attrs<T: HasAttrs>(&mut self, node: T) -> T {
108+
node.map_attrs(|attrs| {
109+
attrs.into_iter().filter_map(|attr| self.process_cfg_attr(attr)).collect()
110+
})
108111
}
109112

110113
fn visit_stmt_or_expr_attrs(&mut self, attrs: &[ast::Attribute]) {
@@ -114,7 +117,7 @@ impl<'a> CfgFolder for StripUnconfigured<'a> {
114117
}
115118
}
116119

117-
fn visit_unconfigurable_expr(&mut self, expr: &ast::Expr) {
120+
fn visit_unremovable_expr(&mut self, expr: &ast::Expr) {
118121
if let Some(attr) = expr.attrs().iter().find(|a| is_cfg(a)) {
119122
let msg = "removing an expression is not supported in this position";
120123
self.diag.diag.span_err(attr.span, msg);
@@ -200,7 +203,8 @@ impl<T: CfgFolder> fold::Folder for T {
200203
//
201204
// NB: This is intentionally not part of the fold_expr() function
202205
// in order for fold_opt_expr() to be able to avoid this check
203-
self.visit_unconfigurable_expr(&expr);
206+
self.visit_unremovable_expr(&expr);
207+
let expr = self.process_attrs(expr);
204208
fold_expr(self, expr)
205209
}
206210

src/libsyntax/test.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -317,13 +317,8 @@ fn strip_test_functions(krate: ast::Crate) -> ast::Crate {
317317
// #[test] functions
318318
struct StripTests;
319319
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) }
320+
fn in_cfg(&mut self, attrs: &[ast::Attribute]) -> bool {
321+
!attr::contains_name(attrs, "test") && !attr::contains_name(attrs, "bench")
327322
}
328323
}
329324

0 commit comments

Comments
 (0)