use util::small_vector::SmallVector;
+pub trait CfgFolder: fold::Folder {
+ fn in_cfg(&mut self, attrs: &[ast::Attribute]) -> bool;
+ fn visit_unconfigurable_expr(&mut self, _expr: &ast::Expr) {}
+}
+
/// A folder that strips out items that do not belong in the current
/// configuration.
struct Context<'a, F> where F: FnMut(&[ast::Attribute]) -> bool {
diagnostic: &'a Handler,
}
+impl<'a, F: FnMut(&[ast::Attribute]) -> bool> CfgFolder for Context<'a, F> {
+ fn in_cfg(&mut self, attrs: &[ast::Attribute]) -> bool {
+ (self.in_cfg)(attrs)
+ }
+
+ fn visit_unconfigurable_expr(&mut self, expr: &ast::Expr) {
+ if let Some(attr) = expr.attrs().iter().find(|a| is_cfg(a)) {
+ let msg = "removing an expression is not supported in this position";
+ self.diagnostic.span_err(attr.span, msg);
+ }
+ }
+}
+
// Support conditional compilation by transforming the AST, stripping out
// any items that do not belong in the current configuration
pub fn strip_unconfigured_items(diagnostic: &Handler, krate: ast::Crate,
})
}
-impl<'a, F> fold::Folder for Context<'a, F> where F: FnMut(&[ast::Attribute]) -> bool {
+impl<T: CfgFolder> fold::Folder for T {
fn fold_foreign_mod(&mut self, foreign_mod: ast::ForeignMod) -> ast::ForeignMod {
ast::ForeignMod {
abi: foreign_mod.abi,
items: foreign_mod.items.into_iter().filter(|item| {
- (self.in_cfg)(&item.attrs)
+ self.in_cfg(&item.attrs)
}).collect(),
}
}
let fold_struct = |this: &mut Self, vdata| match vdata {
ast::VariantData::Struct(fields, id) => {
ast::VariantData::Struct(fields.into_iter().filter(|m| {
- (this.in_cfg)(&m.attrs)
+ this.in_cfg(&m.attrs)
}).collect(), id)
}
ast::VariantData::Tuple(fields, id) => {
ast::VariantData::Tuple(fields.into_iter().filter(|m| {
- (this.in_cfg)(&m.attrs)
+ this.in_cfg(&m.attrs)
}).collect(), id)
}
ast::VariantData::Unit(id) => ast::VariantData::Unit(id)
let item = match item {
ast::ItemKind::Impl(u, o, a, b, c, impl_items) => {
let impl_items = impl_items.into_iter()
- .filter(|ii| (self.in_cfg)(&ii.attrs))
+ .filter(|ii| self.in_cfg(&ii.attrs))
.collect();
ast::ItemKind::Impl(u, o, a, b, c, impl_items)
}
ast::ItemKind::Trait(u, a, b, methods) => {
let methods = methods.into_iter()
- .filter(|ti| (self.in_cfg)(&ti.attrs))
+ .filter(|ti| self.in_cfg(&ti.attrs))
.collect();
ast::ItemKind::Trait(u, a, b, methods)
}
}
ast::ItemKind::Enum(def, generics) => {
let variants = def.variants.into_iter().filter_map(|v| {
- if !(self.in_cfg)(&v.node.attrs) {
+ if !self.in_cfg(&v.node.attrs) {
None
} else {
Some(Spanned {
//
// NB: This is intentionally not part of the fold_expr() function
// in order for fold_opt_expr() to be able to avoid this check
- if let Some(attr) = expr.attrs().iter().find(|a| is_cfg(a)) {
- self.diagnostic.span_err(attr.span,
- "removing an expression is not supported in this position");
- }
+ self.visit_unconfigurable_expr(&expr);
fold_expr(self, expr)
}
fn fold_opt_expr(&mut self, expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
- if (self.in_cfg)(expr.attrs()) {
+ if self.in_cfg(expr.attrs()) {
Some(fold_expr(self, expr))
} else {
None
}
fn fold_stmt(&mut self, stmt: ast::Stmt) -> SmallVector<ast::Stmt> {
- if (self.in_cfg)(stmt.node.attrs()) {
+ if self.in_cfg(stmt.node.attrs()) {
fold::noop_fold_stmt(stmt, self)
} else {
SmallVector::zero()
}
fn fold_item(&mut self, item: P<ast::Item>) -> SmallVector<P<ast::Item>> {
- if (self.in_cfg)(&item.attrs) {
+ if self.in_cfg(&item.attrs) {
SmallVector::one(item.map(|i| self.fold_item_simple(i)))
} else {
SmallVector::zero()
ctxt.fold_crate(krate)
}
-fn fold_expr<F>(cx: &mut Context<F>, expr: P<ast::Expr>) -> P<ast::Expr> where
- F: FnMut(&[ast::Attribute]) -> bool
-{
+fn fold_expr<F: CfgFolder>(folder: &mut F, expr: P<ast::Expr>) -> P<ast::Expr> {
expr.map(|ast::Expr {id, span, node, attrs}| {
fold::noop_fold_expr(ast::Expr {
id: id,
node: match node {
ast::ExprKind::Match(m, arms) => {
ast::ExprKind::Match(m, arms.into_iter()
- .filter(|a| (cx.in_cfg)(&a.attrs))
+ .filter(|a| folder.in_cfg(&a.attrs))
.collect())
}
_ => node
},
span: span,
attrs: attrs,
- }, cx)
+ }, folder)
})
}