@@ -13,7 +13,7 @@ use feature_gate::{emit_feature_err, EXPLAIN_STMT_ATTR_SYNTAX, Features, get_fea
13
13
use { fold, attr} ;
14
14
use ast;
15
15
use codemap:: { Spanned , respan} ;
16
- use parse:: { ParseSess , token } ;
16
+ use parse:: ParseSess ;
17
17
use ptr:: P ;
18
18
19
19
use util:: small_vector:: SmallVector ;
@@ -60,6 +60,15 @@ pub fn features(mut krate: ast::Crate, sess: &ParseSess, should_test: bool)
60
60
( krate, features)
61
61
}
62
62
63
+ macro_rules! configure {
64
+ ( $this: ident, $node: ident) => {
65
+ match $this. configure( $node) {
66
+ Some ( node) => node,
67
+ None => return Default :: default ( ) ,
68
+ }
69
+ }
70
+ }
71
+
63
72
impl < ' a > StripUnconfigured < ' a > {
64
73
fn configure < T : HasAttrs > ( & mut self , node : T ) -> Option < T > {
65
74
let node = self . process_cfg_attrs ( node) ;
@@ -156,37 +165,35 @@ impl<'a> StripUnconfigured<'a> {
156
165
}
157
166
}
158
167
}
159
- }
160
168
161
- impl < ' a > fold:: Folder for StripUnconfigured < ' a > {
162
- fn fold_foreign_mod ( & mut self , foreign_mod : ast:: ForeignMod ) -> ast:: ForeignMod {
169
+ fn configure_foreign_mod ( & mut self , foreign_mod : ast:: ForeignMod ) -> ast:: ForeignMod {
163
170
ast:: ForeignMod {
164
171
abi : foreign_mod. abi ,
165
- items : foreign_mod. items . into_iter ( ) . filter_map ( |item| {
166
- self . configure ( item) . map ( |item| fold:: noop_fold_foreign_item ( item, self ) )
167
- } ) . collect ( ) ,
172
+ items : foreign_mod. items . into_iter ( ) . filter_map ( |item| self . configure ( item) ) . collect ( ) ,
168
173
}
169
174
}
170
175
171
- fn fold_item_kind ( & mut self , item : ast:: ItemKind ) -> ast:: ItemKind {
172
- let fold_struct = | this : & mut Self , vdata| match vdata {
176
+ fn configure_variant_data ( & mut self , vdata : ast:: VariantData ) -> ast:: VariantData {
177
+ match vdata {
173
178
ast:: VariantData :: Struct ( fields, id) => {
174
- let fields = fields. into_iter ( ) . filter_map ( |field| this . configure ( field) ) ;
179
+ let fields = fields. into_iter ( ) . filter_map ( |field| self . configure ( field) ) ;
175
180
ast:: VariantData :: Struct ( fields. collect ( ) , id)
176
181
}
177
182
ast:: VariantData :: Tuple ( fields, id) => {
178
- let fields = fields. into_iter ( ) . filter_map ( |field| this . configure ( field) ) ;
183
+ let fields = fields. into_iter ( ) . filter_map ( |field| self . configure ( field) ) ;
179
184
ast:: VariantData :: Tuple ( fields. collect ( ) , id)
180
185
}
181
186
ast:: VariantData :: Unit ( id) => ast:: VariantData :: Unit ( id)
182
- } ;
187
+ }
188
+ }
183
189
184
- let item = match item {
190
+ fn configure_item_kind ( & mut self , item : ast:: ItemKind ) -> ast:: ItemKind {
191
+ match item {
185
192
ast:: ItemKind :: Struct ( def, generics) => {
186
- ast:: ItemKind :: Struct ( fold_struct ( self , def) , generics)
193
+ ast:: ItemKind :: Struct ( self . configure_variant_data ( def) , generics)
187
194
}
188
195
ast:: ItemKind :: Union ( def, generics) => {
189
- ast:: ItemKind :: Union ( fold_struct ( self , def) , generics)
196
+ ast:: ItemKind :: Union ( self . configure_variant_data ( def) , generics)
190
197
}
191
198
ast:: ItemKind :: Enum ( def, generics) => {
192
199
let variants = def. variants . into_iter ( ) . filter_map ( |v| {
@@ -195,7 +202,7 @@ impl<'a> fold::Folder for StripUnconfigured<'a> {
195
202
node : ast:: Variant_ {
196
203
name : v. node . name ,
197
204
attrs : v. node . attrs ,
198
- data : fold_struct ( self , v. node . data ) ,
205
+ data : self . configure_variant_data ( v. node . data ) ,
199
206
disr_expr : v. node . disr_expr ,
200
207
} ,
201
208
span : v. span
@@ -207,12 +214,19 @@ impl<'a> fold::Folder for StripUnconfigured<'a> {
207
214
} , generics)
208
215
}
209
216
item => item,
210
- } ;
217
+ }
218
+ }
211
219
212
- fold:: noop_fold_item_kind ( item, self )
220
+ fn configure_expr_kind ( & mut self , expr_kind : ast:: ExprKind ) -> ast:: ExprKind {
221
+ if let ast:: ExprKind :: Match ( m, arms) = expr_kind {
222
+ let arms = arms. into_iter ( ) . filter_map ( |a| self . configure ( a) ) . collect ( ) ;
223
+ ast:: ExprKind :: Match ( m, arms)
224
+ } else {
225
+ expr_kind
226
+ }
213
227
}
214
228
215
- fn fold_expr ( & mut self , expr : P < ast:: Expr > ) -> P < ast:: Expr > {
229
+ fn configure_expr ( & mut self , expr : P < ast:: Expr > ) -> P < ast:: Expr > {
216
230
self . visit_stmt_or_expr_attrs ( expr. attrs ( ) ) ;
217
231
218
232
// If an expr is valid to cfg away it will have been removed by the
@@ -227,64 +241,64 @@ impl<'a> fold::Folder for StripUnconfigured<'a> {
227
241
self . sess . span_diagnostic . span_err ( attr. span , msg) ;
228
242
}
229
243
230
- let expr = self . process_cfg_attrs ( expr) ;
231
- fold_expr ( self , expr)
244
+ self . process_cfg_attrs ( expr)
232
245
}
233
246
234
- fn fold_opt_expr ( & mut self , expr : P < ast:: Expr > ) -> Option < P < ast:: Expr > > {
235
- self . configure ( expr) . map ( |expr| fold_expr ( self , expr) )
247
+ fn configure_stmt ( & mut self , stmt : ast:: Stmt ) -> Option < ast:: Stmt > {
248
+ self . visit_stmt_or_expr_attrs ( stmt. attrs ( ) ) ;
249
+ self . configure ( stmt)
250
+ }
251
+ }
252
+
253
+ impl < ' a > fold:: Folder for StripUnconfigured < ' a > {
254
+ fn fold_foreign_mod ( & mut self , foreign_mod : ast:: ForeignMod ) -> ast:: ForeignMod {
255
+ let foreign_mod = self . configure_foreign_mod ( foreign_mod) ;
256
+ fold:: noop_fold_foreign_mod ( foreign_mod, self )
236
257
}
237
258
238
- fn fold_stmt ( & mut self , stmt : ast:: Stmt ) -> SmallVector < ast:: Stmt > {
239
- self . visit_stmt_or_expr_attrs ( stmt. attrs ( ) ) ;
240
- self . configure ( stmt) . map ( |stmt| fold:: noop_fold_stmt ( stmt, self ) )
241
- . unwrap_or ( SmallVector :: zero ( ) )
259
+ fn fold_item_kind ( & mut self , item : ast:: ItemKind ) -> ast:: ItemKind {
260
+ let item = self . configure_item_kind ( item) ;
261
+ fold:: noop_fold_item_kind ( item, self )
242
262
}
243
263
244
- fn fold_mac ( & mut self , mac : ast:: Mac ) -> ast:: Mac {
245
- fold:: noop_fold_mac ( mac, self )
264
+ fn fold_expr ( & mut self , expr : P < ast:: Expr > ) -> P < ast:: Expr > {
265
+ let mut expr = self . configure_expr ( expr) . unwrap ( ) ;
266
+ expr. node = self . configure_expr_kind ( expr. node ) ;
267
+ P ( fold:: noop_fold_expr ( expr, self ) )
268
+ }
269
+
270
+ fn fold_opt_expr ( & mut self , expr : P < ast:: Expr > ) -> Option < P < ast:: Expr > > {
271
+ let mut expr = configure ! ( self , expr) . unwrap ( ) ;
272
+ expr. node = self . configure_expr_kind ( expr. node ) ;
273
+ Some ( P ( fold:: noop_fold_expr ( expr, self ) ) )
274
+ }
275
+
276
+ fn fold_stmt ( & mut self , stmt : ast:: Stmt ) -> SmallVector < ast:: Stmt > {
277
+ match self . configure_stmt ( stmt) {
278
+ Some ( stmt) => fold:: noop_fold_stmt ( stmt, self ) ,
279
+ None => return SmallVector :: zero ( ) ,
280
+ }
246
281
}
247
282
248
283
fn fold_item ( & mut self , item : P < ast:: Item > ) -> SmallVector < P < ast:: Item > > {
249
- self . configure ( item) . map ( |item| fold:: noop_fold_item ( item, self ) )
250
- . unwrap_or ( SmallVector :: zero ( ) )
284
+ fold:: noop_fold_item ( configure ! ( self , item) , self )
251
285
}
252
286
253
287
fn fold_impl_item ( & mut self , item : ast:: ImplItem ) -> SmallVector < ast:: ImplItem > {
254
- self . configure ( item) . map ( |item| fold:: noop_fold_impl_item ( item, self ) )
255
- . unwrap_or ( SmallVector :: zero ( ) )
288
+ fold:: noop_fold_impl_item ( configure ! ( self , item) , self )
256
289
}
257
290
258
291
fn fold_trait_item ( & mut self , item : ast:: TraitItem ) -> SmallVector < ast:: TraitItem > {
259
- self . configure ( item) . map ( |item| fold:: noop_fold_trait_item ( item, self ) )
260
- . unwrap_or ( SmallVector :: zero ( ) )
292
+ fold:: noop_fold_trait_item ( configure ! ( self , item) , self )
261
293
}
262
294
263
- fn fold_interpolated ( & mut self , nt : token :: Nonterminal ) -> token :: Nonterminal {
295
+ fn fold_mac ( & mut self , mac : ast :: Mac ) -> ast :: Mac {
264
296
// Don't configure interpolated AST (c.f. #34171).
265
297
// Interpolated AST will get configured once the surrounding tokens are parsed.
266
- nt
298
+ mac
267
299
}
268
300
}
269
301
270
- fn fold_expr ( folder : & mut StripUnconfigured , expr : P < ast:: Expr > ) -> P < ast:: Expr > {
271
- expr. map ( |ast:: Expr { id, span, node, attrs} | {
272
- fold:: noop_fold_expr ( ast:: Expr {
273
- id : id,
274
- node : match node {
275
- ast:: ExprKind :: Match ( m, arms) => {
276
- ast:: ExprKind :: Match ( m, arms. into_iter ( )
277
- . filter_map ( |a| folder. configure ( a) )
278
- . collect ( ) )
279
- }
280
- _ => node
281
- } ,
282
- span : span,
283
- attrs : attrs,
284
- } , folder)
285
- } )
286
- }
287
-
288
302
fn is_cfg ( attr : & ast:: Attribute ) -> bool {
289
303
attr. check_name ( "cfg" )
290
304
}
0 commit comments