Skip to content

Commit 3af0c65

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

File tree

2 files changed

+75
-55
lines changed

2 files changed

+75
-55
lines changed

src/libsyntax/config.rs

Lines changed: 69 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use feature_gate::{emit_feature_err, EXPLAIN_STMT_ATTR_SYNTAX, Features, get_fea
1313
use {fold, attr};
1414
use ast;
1515
use codemap::{Spanned, respan};
16-
use parse::{ParseSess, token};
16+
use parse::ParseSess;
1717
use ptr::P;
1818

1919
use util::small_vector::SmallVector;
@@ -60,6 +60,15 @@ pub fn features(mut krate: ast::Crate, sess: &ParseSess, should_test: bool)
6060
(krate, features)
6161
}
6262

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+
6372
impl<'a> StripUnconfigured<'a> {
6473
fn configure<T: HasAttrs>(&mut self, node: T) -> Option<T> {
6574
let node = self.process_cfg_attrs(node);
@@ -156,37 +165,35 @@ impl<'a> StripUnconfigured<'a> {
156165
}
157166
}
158167
}
159-
}
160168

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 {
163170
ast::ForeignMod {
164171
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(),
168173
}
169174
}
170175

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 {
173178
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));
175180
ast::VariantData::Struct(fields.collect(), id)
176181
}
177182
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));
179184
ast::VariantData::Tuple(fields.collect(), id)
180185
}
181186
ast::VariantData::Unit(id) => ast::VariantData::Unit(id)
182-
};
187+
}
188+
}
183189

184-
let item = match item {
190+
fn configure_item_kind(&mut self, item: ast::ItemKind) -> ast::ItemKind {
191+
match item {
185192
ast::ItemKind::Struct(def, generics) => {
186-
ast::ItemKind::Struct(fold_struct(self, def), generics)
193+
ast::ItemKind::Struct(self.configure_variant_data(def), generics)
187194
}
188195
ast::ItemKind::Union(def, generics) => {
189-
ast::ItemKind::Union(fold_struct(self, def), generics)
196+
ast::ItemKind::Union(self.configure_variant_data(def), generics)
190197
}
191198
ast::ItemKind::Enum(def, generics) => {
192199
let variants = def.variants.into_iter().filter_map(|v| {
@@ -195,7 +202,7 @@ impl<'a> fold::Folder for StripUnconfigured<'a> {
195202
node: ast::Variant_ {
196203
name: v.node.name,
197204
attrs: v.node.attrs,
198-
data: fold_struct(self, v.node.data),
205+
data: self.configure_variant_data(v.node.data),
199206
disr_expr: v.node.disr_expr,
200207
},
201208
span: v.span
@@ -207,12 +214,19 @@ impl<'a> fold::Folder for StripUnconfigured<'a> {
207214
}, generics)
208215
}
209216
item => item,
210-
};
217+
}
218+
}
211219

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+
}
213227
}
214228

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

218232
// 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> {
227241
self.sess.span_diagnostic.span_err(attr.span, msg);
228242
}
229243

230-
let expr = self.process_cfg_attrs(expr);
231-
fold_expr(self, expr)
244+
self.process_cfg_attrs(expr)
232245
}
233246

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)
236257
}
237258

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)
242262
}
243263

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+
}
246281
}
247282

248283
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)
251285
}
252286

253287
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)
256289
}
257290

258291
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)
261293
}
262294

263-
fn fold_interpolated(&mut self, nt: token::Nonterminal) -> token::Nonterminal {
295+
fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac {
264296
// Don't configure interpolated AST (c.f. #34171).
265297
// Interpolated AST will get configured once the surrounding tokens are parsed.
266-
nt
298+
mac
267299
}
268300
}
269301

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

src/libsyntax/util/small_vector.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ enum SmallVectorRepr<T> {
2929
Many(Vec<T>),
3030
}
3131

32+
impl<T> Default for SmallVector<T> {
33+
fn default() -> Self {
34+
SmallVector { repr: Zero }
35+
}
36+
}
37+
3238
impl<T> Into<Vec<T>> for SmallVector<T> {
3339
fn into(self) -> Vec<T> {
3440
match self.repr {

0 commit comments

Comments
 (0)