Skip to content

Commit 34191ed

Browse files
committed
Refactor MultiModifier expansion
1 parent 6ba7b7c commit 34191ed

File tree

1 file changed

+34
-65
lines changed

1 file changed

+34
-65
lines changed

src/libsyntax/ext/expand.rs

+34-65
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use ast::{Local, Ident, Mac_, Name, SpannedIdent};
1313
use ast::{MacStmtStyle, Mrk, Stmt, StmtKind, ItemKind};
1414
use ast::TokenTree;
1515
use ast;
16+
use attr::HasAttrs;
1617
use ext::mtwt;
1718
use ext::build::AstBuilder;
1819
use attr;
@@ -712,11 +713,7 @@ impl<'a> Folder for PatIdentRenamer<'a> {
712713
}
713714
}
714715

715-
fn expand_annotatable(a: Annotatable,
716-
fld: &mut MacroExpander)
717-
-> SmallVector<Annotatable> {
718-
let a = expand_item_multi_modifier(a, fld);
719-
716+
fn expand_multi_modified(a: Annotatable, fld: &mut MacroExpander) -> SmallVector<Annotatable> {
720717
let new_items: SmallVector<Annotatable> = match a {
721718
Annotatable::Item(it) => match it.node {
722719
ast::ItemKind::Mac(..) => {
@@ -795,29 +792,6 @@ fn decorate(a: Annotatable, fld: &mut MacroExpander) -> SmallVector<Annotatable>
795792
new_items
796793
}
797794

798-
// Partition a set of attributes into one kind of attribute, and other kinds.
799-
macro_rules! partition {
800-
($fn_name: ident, $variant: ident) => {
801-
#[allow(deprecated)] // The `allow` is needed because the `Modifier` variant might be used.
802-
fn $fn_name(attrs: &[ast::Attribute],
803-
fld: &MacroExpander)
804-
-> (Vec<ast::Attribute>, Vec<ast::Attribute>) {
805-
attrs.iter().cloned().partition(|attr| {
806-
match fld.cx.syntax_env.find(intern(&attr.name())) {
807-
Some(rc) => match *rc {
808-
$variant(..) => true,
809-
_ => false
810-
},
811-
_ => false
812-
}
813-
})
814-
}
815-
}
816-
}
817-
818-
partition!(multi_modifiers, MultiModifier);
819-
820-
821795
fn expand_decorators(a: Annotatable,
822796
fld: &mut MacroExpander,
823797
decorator_items: &mut SmallVector<Annotatable>,
@@ -861,46 +835,41 @@ fn expand_decorators(a: Annotatable,
861835
}
862836
}
863837

864-
fn expand_item_multi_modifier(mut it: Annotatable,
865-
fld: &mut MacroExpander)
866-
-> Annotatable {
867-
let (modifiers, other_attrs) = multi_modifiers(it.attrs(), fld);
868-
869-
// Update the attrs, leave everything else alone. Is this mutation really a good idea?
870-
it = it.fold_attrs(other_attrs);
871-
872-
if modifiers.is_empty() {
873-
return it
874-
}
875-
876-
for attr in &modifiers {
877-
let mname = intern(&attr.name());
878-
879-
match fld.cx.syntax_env.find(mname) {
880-
Some(rc) => match *rc {
881-
MultiModifier(ref mac) => {
882-
attr::mark_used(attr);
883-
fld.cx.bt_push(ExpnInfo {
884-
call_site: attr.span,
885-
callee: NameAndSpan {
886-
format: MacroAttribute(mname),
887-
span: Some(attr.span),
888-
// attributes can do whatever they like,
889-
// for now
890-
allow_internal_unstable: true,
891-
}
892-
});
893-
it = mac.expand(fld.cx, attr.span, &attr.node.value, it);
894-
fld.cx.bt_pop();
838+
fn expand_annotatable(mut item: Annotatable, fld: &mut MacroExpander) -> SmallVector<Annotatable> {
839+
let mut multi_modifier = None;
840+
item = item.map_attrs(|mut attrs| {
841+
for i in 0..attrs.len() {
842+
if let Some(extension) = fld.cx.syntax_env.find(intern(&attrs[i].name())) {
843+
if let MultiModifier(..) = *extension {
844+
multi_modifier = Some((attrs.remove(i), extension));
845+
break;
895846
}
896-
_ => unreachable!()
897-
},
898-
_ => unreachable!()
847+
}
899848
}
900-
}
849+
attrs
850+
});
901851

902-
// Expansion may have added new ItemKind::Modifiers.
903-
expand_item_multi_modifier(it, fld)
852+
match multi_modifier {
853+
None => expand_multi_modified(item, fld),
854+
Some((attr, extension)) => match *extension {
855+
MultiModifier(ref mac) => {
856+
attr::mark_used(&attr);
857+
fld.cx.bt_push(ExpnInfo {
858+
call_site: attr.span,
859+
callee: NameAndSpan {
860+
format: MacroAttribute(intern(&attr.name())),
861+
span: Some(attr.span),
862+
// attributes can do whatever they like, for now
863+
allow_internal_unstable: true,
864+
}
865+
});
866+
let modified = mac.expand(fld.cx, attr.span, &attr.node.value, item);
867+
fld.cx.bt_pop();
868+
expand_annotatable(modified, fld)
869+
}
870+
_ => unreachable!(),
871+
}
872+
}
904873
}
905874

906875
fn expand_impl_item(ii: ast::ImplItem, fld: &mut MacroExpander)

0 commit comments

Comments
 (0)