Skip to content

Commit 2a83574

Browse files
committed
Refactor out expand_item (with better semantics than before).
1 parent 8be8cf8 commit 2a83574

File tree

1 file changed

+39
-36
lines changed

1 file changed

+39
-36
lines changed

src/libsyntax/ext/expand.rs

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -409,42 +409,7 @@ fn expand_pat(p: P<ast::Pat>, fld: &mut MacroExpander) -> P<ast::Pat> {
409409

410410
fn expand_multi_modified(a: Annotatable, fld: &mut MacroExpander) -> Expansion {
411411
match a {
412-
Annotatable::Item(it) => match it.node {
413-
ast::ItemKind::Mac(..) => {
414-
if match it.node {
415-
ItemKind::Mac(ref mac) => mac.node.path.segments.is_empty(),
416-
_ => unreachable!(),
417-
} {
418-
return Expansion::Items(SmallVector::one(it));
419-
}
420-
it.and_then(|it| match it.node {
421-
ItemKind::Mac(mac) => {
422-
let invoc =
423-
fld.new_invoc(ExpansionKind::Items, InvocationKind::Bang {
424-
mac: mac, attrs: it.attrs, ident: Some(it.ident), span: it.span,
425-
});
426-
expand_invoc(invoc, fld)
427-
}
428-
_ => unreachable!(),
429-
})
430-
}
431-
ast::ItemKind::Mod(_) | ast::ItemKind::ForeignMod(_) => {
432-
let valid_ident =
433-
it.ident.name != keywords::Invalid.name();
434-
435-
if valid_ident {
436-
fld.cx.mod_push(it.ident);
437-
}
438-
let macro_use = contains_macro_use(fld, &it.attrs);
439-
let result = fld.with_exts_frame(macro_use, |fld| noop_fold_item(it, fld));
440-
if valid_ident {
441-
fld.cx.mod_pop();
442-
}
443-
Expansion::Items(result)
444-
},
445-
_ => Expansion::Items(noop_fold_item(it, fld)),
446-
},
447-
412+
Annotatable::Item(it) => Expansion::Items(expand_item(it, fld)),
448413
Annotatable::TraitItem(it) => Expansion::TraitItems(expand_trait_item(it.unwrap(), fld)),
449414
Annotatable::ImplItem(ii) => Expansion::ImplItems(expand_impl_item(ii.unwrap(), fld)),
450415
}
@@ -480,6 +445,44 @@ fn expand_annotatable(mut item: Annotatable, fld: &mut MacroExpander) -> Expansi
480445
}
481446
}
482447

448+
fn expand_item(item: P<ast::Item>, fld: &mut MacroExpander) -> SmallVector<P<ast::Item>> {
449+
match item.node {
450+
ast::ItemKind::Mac(..) => {
451+
if match item.node {
452+
ItemKind::Mac(ref mac) => mac.node.path.segments.is_empty(),
453+
_ => unreachable!(),
454+
} {
455+
return SmallVector::one(item);
456+
}
457+
item.and_then(|item| match item.node {
458+
ItemKind::Mac(mac) => {
459+
let invoc =
460+
fld.new_invoc(ExpansionKind::Items, InvocationKind::Bang {
461+
mac: mac, attrs: item.attrs, ident: Some(item.ident), span: item.span,
462+
});
463+
expand_invoc(invoc, fld).make_items()
464+
}
465+
_ => unreachable!(),
466+
})
467+
}
468+
ast::ItemKind::Mod(_) | ast::ItemKind::ForeignMod(_) => {
469+
let valid_ident =
470+
item.ident.name != keywords::Invalid.name();
471+
472+
if valid_ident {
473+
fld.cx.mod_push(item.ident);
474+
}
475+
let macro_use = contains_macro_use(fld, &item.attrs);
476+
let result = fld.with_exts_frame(macro_use, |fld| noop_fold_item(item, fld));
477+
if valid_ident {
478+
fld.cx.mod_pop();
479+
}
480+
result
481+
},
482+
_ => noop_fold_item(item, fld),
483+
}
484+
}
485+
483486
fn expand_impl_item(ii: ast::ImplItem, fld: &mut MacroExpander)
484487
-> SmallVector<ast::ImplItem> {
485488
match ii.node {

0 commit comments

Comments
 (0)