Skip to content

Commit c41cf30

Browse files
committed
Strip unconfigured nodes from decorator-generated AST
1 parent 83d283b commit c41cf30

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

src/libsyntax/ext/base.rs

+10
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,16 @@ impl Annotatable {
9595
_ => panic!("expected Item")
9696
}
9797
}
98+
99+
pub fn fold_with<F: Folder>(self, folder: &mut F) -> SmallVector<Self> {
100+
match self {
101+
Annotatable::Item(item) => folder.fold_item(item).map(Annotatable::Item),
102+
Annotatable::ImplItem(item) =>
103+
folder.fold_impl_item(item.unwrap()).map(|item| Annotatable::ImplItem(P(item))),
104+
Annotatable::TraitItem(item) =>
105+
folder.fold_trait_item(item.unwrap()).map(|item| Annotatable::TraitItem(P(item))),
106+
}
107+
}
98108
}
99109

100110
// A more flexible ItemDecorator.

src/libsyntax/ext/expand.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -839,16 +839,18 @@ fn expand_decorators(a: Annotatable,
839839
}
840840
});
841841

842-
// we'd ideally decorator_items.push_all(expand_annotatable(ann, fld)),
843-
// but that double-mut-borrows fld
844842
let mut items: SmallVector<Annotatable> = SmallVector::zero();
845843
dec.expand(fld.cx,
846844
attr.span,
847845
&attr.node.value,
848846
&a,
849847
&mut |ann| items.push(ann));
850-
decorator_items.extend(items.into_iter()
851-
.flat_map(|ann| expand_annotatable(ann, fld).into_iter()));
848+
849+
for item in items {
850+
for configured_item in item.fold_with(&mut fld.strip_unconfigured()) {
851+
decorator_items.extend(expand_annotatable(configured_item, fld));
852+
}
853+
}
852854

853855
fld.cx.bt_pop();
854856
}

src/libsyntax/util/small_vector.rs

+9
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,15 @@ impl<T> SmallVector<T> {
136136
}
137137

138138
pub fn is_empty(&self) -> bool { self.len() == 0 }
139+
140+
pub fn map<U, F: FnMut(T) -> U>(self, mut f: F) -> SmallVector<U> {
141+
let repr = match self.repr {
142+
Zero => Zero,
143+
One(t) => One(f(t)),
144+
Many(vec) => Many(vec.into_iter().map(f).collect()),
145+
};
146+
SmallVector { repr: repr }
147+
}
139148
}
140149

141150
impl<T> IntoIterator for SmallVector<T> {

0 commit comments

Comments
 (0)