8
8
// option. This file may not be copied, modified, or distributed
9
9
// except according to those terms.
10
10
11
- use attr:: AttrMetaMethods ;
11
+ use attr:: { AttrMetaMethods , HasAttrs } ;
12
12
use errors:: Handler ;
13
13
use feature_gate:: GatedCfgAttr ;
14
14
use fold:: Folder ;
@@ -20,7 +20,7 @@ use ptr::P;
20
20
use util:: small_vector:: SmallVector ;
21
21
22
22
pub trait CfgFolder : fold:: Folder {
23
- fn in_cfg ( & mut self , attrs : & [ ast :: Attribute ] ) -> bool ;
23
+ fn configure < T : HasAttrs > ( & mut self , node : T ) -> Option < T > ;
24
24
fn visit_unconfigurable_expr ( & mut self , _expr : & ast:: Expr ) { }
25
25
}
26
26
@@ -32,8 +32,12 @@ struct Context<'a, F> where F: FnMut(&[ast::Attribute]) -> bool {
32
32
}
33
33
34
34
impl < ' a , F : FnMut ( & [ ast:: Attribute ] ) -> bool > CfgFolder for Context < ' a , F > {
35
- fn in_cfg ( & mut self , attrs : & [ ast:: Attribute ] ) -> bool {
36
- ( self . in_cfg ) ( attrs)
35
+ fn configure < T : HasAttrs > ( & mut self , node : T ) -> Option < T > {
36
+ if ( self . in_cfg ) ( node. attrs ( ) ) {
37
+ Some ( node)
38
+ } else {
39
+ None
40
+ }
37
41
}
38
42
39
43
fn visit_unconfigurable_expr ( & mut self , expr : & ast:: Expr ) {
@@ -70,58 +74,48 @@ impl<T> fold::Folder for T where T: CfgFolder {
70
74
fn fold_foreign_mod ( & mut self , foreign_mod : ast:: ForeignMod ) -> ast:: ForeignMod {
71
75
ast:: ForeignMod {
72
76
abi : foreign_mod. abi ,
73
- items : foreign_mod. items . into_iter ( ) . filter ( |item| {
74
- self . in_cfg ( & item. attrs )
75
- } ) . collect ( ) ,
77
+ items : foreign_mod. items . into_iter ( ) . filter_map ( |item| self . configure ( item) ) . collect ( ) ,
76
78
}
77
79
}
78
80
79
81
fn fold_item_kind ( & mut self , item : ast:: ItemKind ) -> ast:: ItemKind {
80
82
let fold_struct = |this : & mut Self , vdata| match vdata {
81
83
ast:: VariantData :: Struct ( fields, id) => {
82
- ast:: VariantData :: Struct ( fields. into_iter ( ) . filter ( |m| {
83
- this. in_cfg ( & m. attrs )
84
- } ) . collect ( ) , id)
84
+ let fields = fields. into_iter ( ) . filter_map ( |field| this. configure ( field) ) ;
85
+ ast:: VariantData :: Struct ( fields. collect ( ) , id)
85
86
}
86
87
ast:: VariantData :: Tuple ( fields, id) => {
87
- ast:: VariantData :: Tuple ( fields. into_iter ( ) . filter ( |m| {
88
- this. in_cfg ( & m. attrs )
89
- } ) . collect ( ) , id)
88
+ let fields = fields. into_iter ( ) . filter_map ( |field| this. configure ( field) ) ;
89
+ ast:: VariantData :: Tuple ( fields. collect ( ) , id)
90
90
}
91
91
ast:: VariantData :: Unit ( id) => ast:: VariantData :: Unit ( id)
92
92
} ;
93
93
94
94
let item = match item {
95
- ast:: ItemKind :: Impl ( u, o, a, b, c, impl_items) => {
96
- let impl_items = impl_items. into_iter ( )
97
- . filter ( |ii| self . in_cfg ( & ii. attrs ) )
98
- . collect ( ) ;
99
- ast:: ItemKind :: Impl ( u, o, a, b, c, impl_items)
95
+ ast:: ItemKind :: Impl ( u, o, a, b, c, items) => {
96
+ let items = items. into_iter ( ) . filter_map ( |item| self . configure ( item) ) . collect ( ) ;
97
+ ast:: ItemKind :: Impl ( u, o, a, b, c, items)
100
98
}
101
- ast:: ItemKind :: Trait ( u, a, b, methods) => {
102
- let methods = methods. into_iter ( )
103
- . filter ( |ti| self . in_cfg ( & ti. attrs ) )
104
- . collect ( ) ;
105
- ast:: ItemKind :: Trait ( u, a, b, methods)
99
+ ast:: ItemKind :: Trait ( u, a, b, items) => {
100
+ let items = items. into_iter ( ) . filter_map ( |item| self . configure ( item) ) . collect ( ) ;
101
+ ast:: ItemKind :: Trait ( u, a, b, items)
106
102
}
107
103
ast:: ItemKind :: Struct ( def, generics) => {
108
104
ast:: ItemKind :: Struct ( fold_struct ( self , def) , generics)
109
105
}
110
106
ast:: ItemKind :: Enum ( def, generics) => {
111
107
let variants = def. variants . into_iter ( ) . filter_map ( |v| {
112
- if !self . in_cfg ( & v. node . attrs ) {
113
- None
114
- } else {
115
- Some ( Spanned {
108
+ self . configure ( v) . map ( |v| {
109
+ Spanned {
116
110
node : ast:: Variant_ {
117
111
name : v. node . name ,
118
112
attrs : v. node . attrs ,
119
113
data : fold_struct ( self , v. node . data ) ,
120
114
disr_expr : v. node . disr_expr ,
121
115
} ,
122
116
span : v. span
123
- } )
124
- }
117
+ }
118
+ } )
125
119
} ) ;
126
120
ast:: ItemKind :: Enum ( ast:: EnumDef {
127
121
variants : variants. collect ( ) ,
@@ -146,31 +140,21 @@ impl<T> fold::Folder for T where T: CfgFolder {
146
140
}
147
141
148
142
fn fold_opt_expr ( & mut self , expr : P < ast:: Expr > ) -> Option < P < ast:: Expr > > {
149
- if self . in_cfg ( expr. attrs ( ) ) {
150
- Some ( fold_expr ( self , expr) )
151
- } else {
152
- None
153
- }
143
+ self . configure ( expr) . map ( |expr| fold_expr ( self , expr) )
154
144
}
155
145
156
146
fn fold_stmt ( & mut self , stmt : ast:: Stmt ) -> SmallVector < ast:: Stmt > {
157
- if self . in_cfg ( stmt. node . attrs ( ) ) {
158
- fold:: noop_fold_stmt ( stmt, self )
159
- } else {
160
- SmallVector :: zero ( )
161
- }
147
+ self . configure ( stmt) . map ( |stmt| fold:: noop_fold_stmt ( stmt, self ) )
148
+ . unwrap_or ( SmallVector :: zero ( ) )
162
149
}
163
150
164
151
fn fold_mac ( & mut self , mac : ast:: Mac ) -> ast:: Mac {
165
152
fold:: noop_fold_mac ( mac, self )
166
153
}
167
154
168
155
fn fold_item ( & mut self , item : P < ast:: Item > ) -> SmallVector < P < ast:: Item > > {
169
- if self . in_cfg ( & item. attrs ) {
170
- SmallVector :: one ( item. map ( |i| self . fold_item_simple ( i) ) )
171
- } else {
172
- SmallVector :: zero ( )
173
- }
156
+ self . configure ( item) . map ( |item| SmallVector :: one ( item. map ( |i| self . fold_item_simple ( i) ) ) )
157
+ . unwrap_or ( SmallVector :: zero ( ) )
174
158
}
175
159
}
176
160
@@ -192,7 +176,7 @@ fn fold_expr<F: CfgFolder>(folder: &mut F, expr: P<ast::Expr>) -> P<ast::Expr> {
192
176
node : match node {
193
177
ast:: ExprKind :: Match ( m, arms) => {
194
178
ast:: ExprKind :: Match ( m, arms. into_iter ( )
195
- . filter ( |a| folder. in_cfg ( & a . attrs ) )
179
+ . filter_map ( |a| folder. configure ( a ) )
196
180
. collect ( ) )
197
181
}
198
182
_ => node
0 commit comments