Skip to content

Commit 8691b69

Browse files
committed
Refactor code out of the folder implementation for StripUnconfigured.
1 parent 2c88b4b commit 8691b69

File tree

1 file changed

+79
-48
lines changed

1 file changed

+79
-48
lines changed

src/libsyntax/config.rs

Lines changed: 79 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -156,37 +156,35 @@ impl<'a> StripUnconfigured<'a> {
156156
}
157157
}
158158
}
159-
}
160159

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 {
163161
ast::ForeignMod {
164162
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(),
168164
}
169165
}
170166

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 {
173169
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));
175171
ast::VariantData::Struct(fields.collect(), id)
176172
}
177173
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));
179175
ast::VariantData::Tuple(fields.collect(), id)
180176
}
181177
ast::VariantData::Unit(id) => ast::VariantData::Unit(id)
182-
};
178+
}
179+
}
183180

184-
let item = match item {
181+
fn configure_item_kind(&mut self, item: ast::ItemKind) -> ast::ItemKind {
182+
match item {
185183
ast::ItemKind::Struct(def, generics) => {
186-
ast::ItemKind::Struct(fold_struct(self, def), generics)
184+
ast::ItemKind::Struct(self.configure_variant_data(def), generics)
187185
}
188186
ast::ItemKind::Union(def, generics) => {
189-
ast::ItemKind::Union(fold_struct(self, def), generics)
187+
ast::ItemKind::Union(self.configure_variant_data(def), generics)
190188
}
191189
ast::ItemKind::Enum(def, generics) => {
192190
let variants = def.variants.into_iter().filter_map(|v| {
@@ -195,7 +193,7 @@ impl<'a> fold::Folder for StripUnconfigured<'a> {
195193
node: ast::Variant_ {
196194
name: v.node.name,
197195
attrs: v.node.attrs,
198-
data: fold_struct(self, v.node.data),
196+
data: self.configure_variant_data(v.node.data),
199197
disr_expr: v.node.disr_expr,
200198
},
201199
span: v.span
@@ -207,12 +205,19 @@ impl<'a> fold::Folder for StripUnconfigured<'a> {
207205
}, generics)
208206
}
209207
item => item,
210-
};
208+
}
209+
}
211210

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+
}
213218
}
214219

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> {
216221
self.visit_stmt_or_expr_attrs(expr.attrs());
217222

218223
// 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> {
227232
self.sess.span_diagnostic.span_err(attr.span, msg);
228233
}
229234

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))
232260
}
233261

234262
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)))
236270
}
237271

238272
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)
242279
}
243280

244281
fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac {
245282
fold::noop_fold_mac(mac, self)
246283
}
247284

248285
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)
251292
}
252293

253294
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)
256301
}
257302

258303
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)
261310
}
262311

263312
fn fold_interpolated(&mut self, nt: token::Nonterminal) -> token::Nonterminal {
@@ -267,24 +316,6 @@ impl<'a> fold::Folder for StripUnconfigured<'a> {
267316
}
268317
}
269318

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-
288319
fn is_cfg(attr: &ast::Attribute) -> bool {
289320
attr.check_name("cfg")
290321
}

0 commit comments

Comments
 (0)