Skip to content

Commit ac1a1d3

Browse files
committed
Allow MultiItemModifiers to expand into zero or many items
1 parent 34191ed commit ac1a1d3

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

src/libsyntax/ext/base.rs

+13-11
Original file line numberDiff line numberDiff line change
@@ -123,32 +123,34 @@ impl<F> MultiItemDecorator for F
123123
}
124124
}
125125

126-
// A more flexible ItemKind::Modifier (ItemKind::Modifier should go away, eventually, FIXME).
127-
// meta_item is the annotation, item is the item being modified, parent_item
128-
// is the impl or trait item is declared in if item is part of such a thing.
126+
// `meta_item` is the annotation, and `item` is the item being modified.
129127
// FIXME Decorators should follow the same pattern too.
130128
pub trait MultiItemModifier {
131129
fn expand(&self,
132130
ecx: &mut ExtCtxt,
133131
span: Span,
134132
meta_item: &ast::MetaItem,
135133
item: Annotatable)
136-
-> Annotatable;
134+
-> Vec<Annotatable>;
137135
}
138136

139-
impl<F> MultiItemModifier for F
140-
where F: Fn(&mut ExtCtxt,
141-
Span,
142-
&ast::MetaItem,
143-
Annotatable) -> Annotatable
137+
impl<F, T> MultiItemModifier for F
138+
where F: Fn(&mut ExtCtxt, Span, &ast::MetaItem, Annotatable) -> T,
139+
T: Into<Vec<Annotatable>>,
144140
{
145141
fn expand(&self,
146142
ecx: &mut ExtCtxt,
147143
span: Span,
148144
meta_item: &ast::MetaItem,
149145
item: Annotatable)
150-
-> Annotatable {
151-
(*self)(ecx, span, meta_item, item)
146+
-> Vec<Annotatable> {
147+
(*self)(ecx, span, meta_item, item).into()
148+
}
149+
}
150+
151+
impl Into<Vec<Annotatable>> for Annotatable {
152+
fn into(self) -> Vec<Annotatable> {
153+
vec![self]
152154
}
153155
}
154156

src/libsyntax/ext/expand.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -865,7 +865,7 @@ fn expand_annotatable(mut item: Annotatable, fld: &mut MacroExpander) -> SmallVe
865865
});
866866
let modified = mac.expand(fld.cx, attr.span, &attr.node.value, item);
867867
fld.cx.bt_pop();
868-
expand_annotatable(modified, fld)
868+
modified.into_iter().flat_map(|it| expand_annotatable(it, fld)).collect()
869869
}
870870
_ => unreachable!(),
871871
}

0 commit comments

Comments
 (0)