Skip to content

Commit d76bf3e

Browse files
committed
Strip unconfigured nodes in the InvocationCollector fold.
1 parent 3af0c65 commit d76bf3e

File tree

3 files changed

+67
-19
lines changed

3 files changed

+67
-19
lines changed

src/libsyntax/config.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ macro_rules! configure {
7070
}
7171

7272
impl<'a> StripUnconfigured<'a> {
73-
fn configure<T: HasAttrs>(&mut self, node: T) -> Option<T> {
73+
pub fn configure<T: HasAttrs>(&mut self, node: T) -> Option<T> {
7474
let node = self.process_cfg_attrs(node);
7575
if self.in_cfg(node.attrs()) { Some(node) } else { None }
7676
}
@@ -166,7 +166,7 @@ impl<'a> StripUnconfigured<'a> {
166166
}
167167
}
168168

169-
fn configure_foreign_mod(&mut self, foreign_mod: ast::ForeignMod) -> ast::ForeignMod {
169+
pub fn configure_foreign_mod(&mut self, foreign_mod: ast::ForeignMod) -> ast::ForeignMod {
170170
ast::ForeignMod {
171171
abi: foreign_mod.abi,
172172
items: foreign_mod.items.into_iter().filter_map(|item| self.configure(item)).collect(),
@@ -187,7 +187,7 @@ impl<'a> StripUnconfigured<'a> {
187187
}
188188
}
189189

190-
fn configure_item_kind(&mut self, item: ast::ItemKind) -> ast::ItemKind {
190+
pub fn configure_item_kind(&mut self, item: ast::ItemKind) -> ast::ItemKind {
191191
match item {
192192
ast::ItemKind::Struct(def, generics) => {
193193
ast::ItemKind::Struct(self.configure_variant_data(def), generics)
@@ -217,7 +217,7 @@ impl<'a> StripUnconfigured<'a> {
217217
}
218218
}
219219

220-
fn configure_expr_kind(&mut self, expr_kind: ast::ExprKind) -> ast::ExprKind {
220+
pub fn configure_expr_kind(&mut self, expr_kind: ast::ExprKind) -> ast::ExprKind {
221221
if let ast::ExprKind::Match(m, arms) = expr_kind {
222222
let arms = arms.into_iter().filter_map(|a| self.configure(a)).collect();
223223
ast::ExprKind::Match(m, arms)
@@ -226,7 +226,7 @@ impl<'a> StripUnconfigured<'a> {
226226
}
227227
}
228228

229-
fn configure_expr(&mut self, expr: P<ast::Expr>) -> P<ast::Expr> {
229+
pub fn configure_expr(&mut self, expr: P<ast::Expr>) -> P<ast::Expr> {
230230
self.visit_stmt_or_expr_attrs(expr.attrs());
231231

232232
// If an expr is valid to cfg away it will have been removed by the
@@ -244,7 +244,7 @@ impl<'a> StripUnconfigured<'a> {
244244
self.process_cfg_attrs(expr)
245245
}
246246

247-
fn configure_stmt(&mut self, stmt: ast::Stmt) -> Option<ast::Stmt> {
247+
pub fn configure_stmt(&mut self, stmt: ast::Stmt) -> Option<ast::Stmt> {
248248
self.visit_stmt_or_expr_attrs(stmt.attrs());
249249
self.configure(stmt)
250250
}

src/libsyntax/ext/expand.rs

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,23 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
208208
}
209209

210210
fn collect_invocations(&mut self, expansion: Expansion) -> (Expansion, Vec<Invocation>) {
211-
let expansion = expansion.fold_with(&mut StripUnconfigured {
212-
config: &self.cx.cfg,
213-
should_test: self.cx.ecfg.should_test,
214-
sess: self.cx.parse_sess,
215-
features: self.cx.ecfg.features,
216-
});
217-
let mut collector = InvocationCollector { cx: self.cx, invocations: Vec::new() };
218-
(expansion.fold_with(&mut collector), collector.invocations)
211+
let crate_config = mem::replace(&mut self.cx.cfg, Vec::new());
212+
let result = {
213+
let mut collector = InvocationCollector {
214+
cfg: StripUnconfigured {
215+
config: &crate_config,
216+
should_test: self.cx.ecfg.should_test,
217+
sess: self.cx.parse_sess,
218+
features: self.cx.ecfg.features,
219+
},
220+
cx: self.cx,
221+
invocations: Vec::new(),
222+
};
223+
(expansion.fold_with(&mut collector), collector.invocations)
224+
};
225+
226+
self.cx.cfg = crate_config;
227+
result
219228
}
220229

221230
fn expand_invoc(&mut self, invoc: Invocation) -> Expansion {
@@ -403,9 +412,19 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
403412

404413
struct InvocationCollector<'a, 'b: 'a> {
405414
cx: &'a mut ExtCtxt<'b>,
415+
cfg: StripUnconfigured<'a>,
406416
invocations: Vec<Invocation>,
407417
}
408418

419+
macro_rules! fully_configure {
420+
($this:ident, $node:ident, $noop_fold:ident) => {
421+
match $noop_fold($node, &mut $this.cfg).pop() {
422+
Some(node) => node,
423+
None => return SmallVector::zero(),
424+
}
425+
}
426+
}
427+
409428
impl<'a, 'b> InvocationCollector<'a, 'b> {
410429
fn collect(&mut self, expansion_kind: ExpansionKind, kind: InvocationKind) -> Expansion {
411430
let mark = Mark::fresh();
@@ -475,11 +494,17 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
475494
}
476495
false
477496
}
497+
498+
fn configure<T: HasAttrs>(&mut self, node: T) -> Option<T> {
499+
self.cfg.configure(node)
500+
}
478501
}
479502

480503
impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
481504
fn fold_expr(&mut self, expr: P<ast::Expr>) -> P<ast::Expr> {
482-
let expr = expr.unwrap();
505+
let mut expr = self.cfg.configure_expr(expr).unwrap();
506+
expr.node = self.cfg.configure_expr_kind(expr.node);
507+
483508
if let ast::ExprKind::Mac(mac) = expr.node {
484509
self.collect_bang(mac, expr.attrs.into(), expr.span, ExpansionKind::Expr).make_expr()
485510
} else {
@@ -488,7 +513,9 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
488513
}
489514

490515
fn fold_opt_expr(&mut self, expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
491-
let expr = expr.unwrap();
516+
let mut expr = configure!(self, expr).unwrap();
517+
expr.node = self.cfg.configure_expr_kind(expr.node);
518+
492519
if let ast::ExprKind::Mac(mac) = expr.node {
493520
self.collect_bang(mac, expr.attrs.into(), expr.span, ExpansionKind::OptExpr)
494521
.make_opt_expr()
@@ -511,6 +538,11 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
511538
}
512539

513540
fn fold_stmt(&mut self, stmt: ast::Stmt) -> SmallVector<ast::Stmt> {
541+
let stmt = match self.cfg.configure_stmt(stmt) {
542+
Some(stmt) => stmt,
543+
None => return SmallVector::zero(),
544+
};
545+
514546
let (mac, style, attrs) = match stmt.node {
515547
StmtKind::Mac(mac) => mac.unwrap(),
516548
_ => return noop_fold_stmt(stmt, self),
@@ -540,9 +572,11 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
540572
}
541573

542574
fn fold_item(&mut self, item: P<ast::Item>) -> SmallVector<P<ast::Item>> {
575+
let item = configure!(self, item);
576+
543577
let (item, attr) = self.classify_item(item);
544578
if let Some(attr) = attr {
545-
let item = Annotatable::Item(item);
579+
let item = Annotatable::Item(fully_configure!(self, item, noop_fold_item));
546580
return self.collect_attr(attr, item, ExpansionKind::Items).make_items();
547581
}
548582

@@ -610,9 +644,12 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
610644
}
611645

612646
fn fold_trait_item(&mut self, item: ast::TraitItem) -> SmallVector<ast::TraitItem> {
647+
let item = configure!(self, item);
648+
613649
let (item, attr) = self.classify_item(item);
614650
if let Some(attr) = attr {
615-
let item = Annotatable::TraitItem(P(item));
651+
let item =
652+
Annotatable::TraitItem(P(fully_configure!(self, item, noop_fold_trait_item)));
616653
return self.collect_attr(attr, item, ExpansionKind::TraitItems).make_trait_items()
617654
}
618655

@@ -626,9 +663,11 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
626663
}
627664

628665
fn fold_impl_item(&mut self, item: ast::ImplItem) -> SmallVector<ast::ImplItem> {
666+
let item = configure!(self, item);
667+
629668
let (item, attr) = self.classify_item(item);
630669
if let Some(attr) = attr {
631-
let item = Annotatable::ImplItem(P(item));
670+
let item = Annotatable::ImplItem(P(fully_configure!(self, item, noop_fold_impl_item)));
632671
return self.collect_attr(attr, item, ExpansionKind::ImplItems).make_impl_items();
633672
}
634673

@@ -653,6 +692,14 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
653692
_ => unreachable!(),
654693
}
655694
}
695+
696+
fn fold_foreign_mod(&mut self, foreign_mod: ast::ForeignMod) -> ast::ForeignMod {
697+
noop_fold_foreign_mod(self.cfg.configure_foreign_mod(foreign_mod), self)
698+
}
699+
700+
fn fold_item_kind(&mut self, item: ast::ItemKind) -> ast::ItemKind {
701+
noop_fold_item_kind(self.cfg.configure_item_kind(item), self)
702+
}
656703
}
657704

658705
pub struct ExpansionConfig<'feat> {

src/libsyntax/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ pub mod abi;
104104
pub mod ast;
105105
pub mod attr;
106106
pub mod codemap;
107+
#[macro_use]
107108
pub mod config;
108109
pub mod entry;
109110
pub mod feature_gate;

0 commit comments

Comments
 (0)