Skip to content

Commit 15d5074

Browse files
committed
Process cfg_attr attributes on non-optional expressions
1 parent d3a0e17 commit 15d5074

File tree

2 files changed

+37
-38
lines changed

2 files changed

+37
-38
lines changed

src/libsyntax/config.rs

+35-31
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
@@ -32,30 +38,6 @@ pub struct StripUnconfigured<'a> {
3238
}
3339

3440
impl<'a> StripUnconfigured<'a> {
35-
// Determine if an item should be translated in the current crate
36-
// configuration based on the item's attributes
37-
fn in_cfg(&mut self, attrs: &[ast::Attribute]) -> bool {
38-
attrs.iter().all(|attr| {
39-
let mis = match attr.node.value.node {
40-
ast::MetaItemKind::List(_, ref mis) if is_cfg(&attr) => mis,
41-
_ => return true
42-
};
43-
44-
if mis.len() != 1 {
45-
self.diag.emit_error(|diagnostic| {
46-
diagnostic.span_err(attr.span, "expected 1 cfg-pattern");
47-
});
48-
return true;
49-
}
50-
51-
attr::cfg_matches(self.config, &mis[0], &mut self.diag)
52-
})
53-
}
54-
55-
fn process_cfg_attrs(&mut self, attrs: Vec<ast::Attribute>) -> Vec<ast::Attribute> {
56-
attrs.into_iter().filter_map(|attr| self.process_cfg_attr(attr)).collect()
57-
}
58-
5941
fn process_cfg_attr(&mut self, attr: ast::Attribute) -> Option<ast::Attribute> {
6042
if !attr.check_name("cfg_attr") {
6143
return Some(attr);
@@ -92,9 +74,30 @@ impl<'a> StripUnconfigured<'a> {
9274
}
9375

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

100103
fn visit_stmt_or_expr_attrs(&mut self, attrs: &[ast::Attribute]) {
@@ -104,7 +107,7 @@ impl<'a> CfgFolder for StripUnconfigured<'a> {
104107
}
105108
}
106109

107-
fn visit_unconfigurable_expr(&mut self, expr: &ast::Expr) {
110+
fn visit_unremovable_expr(&mut self, expr: &ast::Expr) {
108111
if let Some(attr) = expr.attrs().iter().find(|a| is_cfg(a)) {
109112
let msg = "removing an expression is not supported in this position";
110113
self.diag.diag.span_err(attr.span, msg);
@@ -195,7 +198,8 @@ impl<T: CfgFolder> fold::Folder for T {
195198
//
196199
// NB: This is intentionally not part of the fold_expr() function
197200
// in order for fold_opt_expr() to be able to avoid this check
198-
self.visit_unconfigurable_expr(&expr);
201+
self.visit_unremovable_expr(&expr);
202+
let expr = self.process_attrs(expr);
199203
fold_expr(self, expr)
200204
}
201205

src/libsyntax/test.rs

+2-7
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)