@@ -156,37 +156,35 @@ impl<'a> StripUnconfigured<'a> {
156
156
}
157
157
}
158
158
}
159
- }
160
159
161
- impl < ' a > fold:: Folder for StripUnconfigured < ' a > {
162
- fn fold_foreign_mod ( & mut self , foreign_mod : ast:: ForeignMod ) -> ast:: ForeignMod {
160
+ fn configure_foreign_mod ( & mut self , foreign_mod : ast:: ForeignMod ) -> ast:: ForeignMod {
163
161
ast:: ForeignMod {
164
162
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 ( ) ,
163
+ items : foreign_mod. items . into_iter ( ) . filter_map ( |item| self . configure ( item) ) . collect ( ) ,
168
164
}
169
165
}
170
166
171
- fn fold_item_kind ( & mut self , item : ast:: ItemKind ) -> ast:: ItemKind {
172
- let fold_struct = | this : & mut Self , vdata| match vdata {
167
+ fn configure_variant_data ( & mut self , vdata : ast:: VariantData ) -> ast:: VariantData {
168
+ match vdata {
173
169
ast:: VariantData :: Struct ( fields, id) => {
174
- let fields = fields. into_iter ( ) . filter_map ( |field| this . configure ( field) ) ;
170
+ let fields = fields. into_iter ( ) . filter_map ( |field| self . configure ( field) ) ;
175
171
ast:: VariantData :: Struct ( fields. collect ( ) , id)
176
172
}
177
173
ast:: VariantData :: Tuple ( fields, id) => {
178
- let fields = fields. into_iter ( ) . filter_map ( |field| this . configure ( field) ) ;
174
+ let fields = fields. into_iter ( ) . filter_map ( |field| self . configure ( field) ) ;
179
175
ast:: VariantData :: Tuple ( fields. collect ( ) , id)
180
176
}
181
177
ast:: VariantData :: Unit ( id) => ast:: VariantData :: Unit ( id)
182
- } ;
178
+ }
179
+ }
183
180
184
- let item = match item {
181
+ fn configure_item_kind ( & mut self , item : ast:: ItemKind ) -> ast:: ItemKind {
182
+ match item {
185
183
ast:: ItemKind :: Struct ( def, generics) => {
186
- ast:: ItemKind :: Struct ( fold_struct ( self , def) , generics)
184
+ ast:: ItemKind :: Struct ( self . configure_variant_data ( def) , generics)
187
185
}
188
186
ast:: ItemKind :: Union ( def, generics) => {
189
- ast:: ItemKind :: Union ( fold_struct ( self , def) , generics)
187
+ ast:: ItemKind :: Union ( self . configure_variant_data ( def) , generics)
190
188
}
191
189
ast:: ItemKind :: Enum ( def, generics) => {
192
190
let variants = def. variants . into_iter ( ) . filter_map ( |v| {
@@ -195,7 +193,7 @@ impl<'a> fold::Folder for StripUnconfigured<'a> {
195
193
node : ast:: Variant_ {
196
194
name : v. node . name ,
197
195
attrs : v. node . attrs ,
198
- data : fold_struct ( self , v. node . data ) ,
196
+ data : self . configure_variant_data ( v. node . data ) ,
199
197
disr_expr : v. node . disr_expr ,
200
198
} ,
201
199
span : v. span
@@ -207,12 +205,19 @@ impl<'a> fold::Folder for StripUnconfigured<'a> {
207
205
} , generics)
208
206
}
209
207
item => item,
210
- } ;
208
+ }
209
+ }
211
210
212
- fold:: noop_fold_item_kind ( item, self )
211
+ fn configure_expr_kind ( & mut self , expr_kind : ast:: ExprKind ) -> ast:: ExprKind {
212
+ if let ast:: ExprKind :: Match ( m, arms) = expr_kind {
213
+ let arms = arms. into_iter ( ) . filter_map ( |a| self . configure ( a) ) . collect ( ) ;
214
+ ast:: ExprKind :: Match ( m, arms)
215
+ } else {
216
+ expr_kind
217
+ }
213
218
}
214
219
215
- fn fold_expr ( & mut self , expr : P < ast:: Expr > ) -> P < ast:: Expr > {
220
+ fn configure_expr ( & mut self , expr : P < ast:: Expr > ) -> P < ast:: Expr > {
216
221
self . visit_stmt_or_expr_attrs ( expr. attrs ( ) ) ;
217
222
218
223
// If an expr is valid to cfg away it will have been removed by the
@@ -227,37 +232,81 @@ impl<'a> fold::Folder for StripUnconfigured<'a> {
227
232
self . sess . span_diagnostic . span_err ( attr. span , msg) ;
228
233
}
229
234
230
- let expr = self . process_cfg_attrs ( expr) ;
231
- fold_expr ( self , expr)
235
+ self . process_cfg_attrs ( expr)
236
+ }
237
+
238
+ fn configure_stmt ( & mut self , stmt : ast:: Stmt ) -> Option < ast:: Stmt > {
239
+ self . visit_stmt_or_expr_attrs ( stmt. attrs ( ) ) ;
240
+ self . configure ( stmt)
241
+ }
242
+ }
243
+
244
+ impl < ' a > fold:: Folder for StripUnconfigured < ' a > {
245
+ fn fold_foreign_mod ( & mut self , foreign_mod : ast:: ForeignMod ) -> ast:: ForeignMod {
246
+ let foreign_mod = self . configure_foreign_mod ( foreign_mod) ;
247
+ fold:: noop_fold_foreign_mod ( foreign_mod, self )
248
+ }
249
+
250
+ fn fold_item_kind ( & mut self , item : ast:: ItemKind ) -> ast:: ItemKind {
251
+ let item = self . configure_item_kind ( item) ;
252
+ fold:: noop_fold_item_kind ( item, self )
253
+ }
254
+
255
+ fn fold_expr ( & mut self , expr : P < ast:: Expr > ) -> P < ast:: Expr > {
256
+ let mut expr = self . configure_expr ( expr) . unwrap ( ) ;
257
+ expr. node = self . configure_expr_kind ( expr. node ) ;
258
+
259
+ P ( fold:: noop_fold_expr ( expr, self ) )
232
260
}
233
261
234
262
fn fold_opt_expr ( & mut self , expr : P < ast:: Expr > ) -> Option < P < ast:: Expr > > {
235
- self . configure ( expr) . map ( |expr| fold_expr ( self , expr) )
263
+ let mut expr = match self . configure ( expr) {
264
+ Some ( expr) => expr. unwrap ( ) ,
265
+ None => return None ,
266
+ } ;
267
+ expr. node = self . configure_expr_kind ( expr. node ) ;
268
+
269
+ Some ( P ( fold:: noop_fold_expr ( expr, self ) ) )
236
270
}
237
271
238
272
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 ( ) )
273
+ let stmt = match self . configure_stmt ( stmt) {
274
+ Some ( stmt) => stmt,
275
+ None => return SmallVector :: zero ( ) ,
276
+ } ;
277
+
278
+ fold:: noop_fold_stmt ( stmt, self )
242
279
}
243
280
244
281
fn fold_mac ( & mut self , mac : ast:: Mac ) -> ast:: Mac {
245
282
fold:: noop_fold_mac ( mac, self )
246
283
}
247
284
248
285
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 ( ) )
286
+ let item = match self . configure ( item) {
287
+ Some ( item) => item,
288
+ None => return SmallVector :: zero ( ) ,
289
+ } ;
290
+
291
+ fold:: noop_fold_item ( item, self )
251
292
}
252
293
253
294
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 ( ) )
295
+ let item = match self . configure ( item) {
296
+ Some ( item) => item,
297
+ None => return SmallVector :: zero ( ) ,
298
+ } ;
299
+
300
+ fold:: noop_fold_impl_item ( item, self )
256
301
}
257
302
258
303
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 ( ) )
304
+ let item = match self . configure ( item) {
305
+ Some ( item) => item,
306
+ None => return SmallVector :: zero ( ) ,
307
+ } ;
308
+
309
+ fold:: noop_fold_trait_item ( item, self )
261
310
}
262
311
263
312
fn fold_interpolated ( & mut self , nt : token:: Nonterminal ) -> token:: Nonterminal {
@@ -267,24 +316,6 @@ impl<'a> fold::Folder for StripUnconfigured<'a> {
267
316
}
268
317
}
269
318
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
319
fn is_cfg ( attr : & ast:: Attribute ) -> bool {
289
320
attr. check_name ( "cfg" )
290
321
}
0 commit comments