Skip to content

Commit 0ba15c9

Browse files
committed
hygiene for item fn args
also, introduce fn_decl_arg_bindings and expand_and_rename abstractions
1 parent 9270832 commit 0ba15c9

File tree

1 file changed

+49
-7
lines changed

1 file changed

+49
-7
lines changed

src/libsyntax/ext/expand.rs

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,19 @@ fn expand_item_modifiers(mut it: Gc<ast::Item>, fld: &mut MacroExpander)
386386
expand_item_modifiers(it, fld)
387387
}
388388

389+
/// Expand item_underscore
390+
fn expand_item_underscore(item: &ast::Item_, fld: &mut MacroExpander) -> ast::Item_ {
391+
match *item {
392+
ast::ItemFn(decl, fn_style, abi, ref generics, body) => {
393+
let (rewritten_fn_decl, rewritten_body)
394+
= expand_and_rename_fn_decl_and_block(decl,body,fld);
395+
let expanded_generics = fold::fold_generics(generics,fld);
396+
ast::ItemFn(rewritten_fn_decl, fn_style, abi, expanded_generics, rewritten_body)
397+
}
398+
_ => noop_fold_item_underscore(&*item, fld)
399+
}
400+
}
401+
389402
// does this attribute list contain "macro_escape" ?
390403
fn contains_macro_escape(attrs: &[ast::Attribute]) -> bool {
391404
attr::contains_name(attrs, "macro_escape")
@@ -656,6 +669,7 @@ fn expand_non_macro_stmt(s: &Stmt, fld: &mut MacroExpander)
656669
}
657670
}
658671

672+
// expand the arm of a 'match', renaming for macro hygiene
659673
fn expand_arm(arm: &ast::Arm, fld: &mut MacroExpander) -> ast::Arm {
660674
// expand pats... they might contain macro uses:
661675
let expanded_pats : Vec<Gc<ast::Pat>> = arm.pats.iter().map(|pat| fld.fold_pat(*pat)).collect();
@@ -665,17 +679,15 @@ fn expand_arm(arm: &ast::Arm, fld: &mut MacroExpander) -> ast::Arm {
665679
// all of the pats must have the same set of bindings, so use the
666680
// first one to extract them and generate new names:
667681
let first_pat = expanded_pats.get(0);
668-
// code duplicated from 'let', above. Perhaps this can be lifted
669-
// into a separate function:
670682
let idents = pattern_bindings(*first_pat);
671-
let new_pending_renames =
683+
let new_renames =
672684
idents.iter().map(|id| (*id,fresh_name(id))).collect();
673685
// apply the renaming, but only to the PatIdents:
674-
let mut rename_pats_fld = PatIdentRenamer{renames:&new_pending_renames};
686+
let mut rename_pats_fld = PatIdentRenamer{renames:&new_renames};
675687
let rewritten_pats =
676688
expanded_pats.iter().map(|pat| rename_pats_fld.fold_pat(*pat)).collect();
677689
// apply renaming and then expansion to the guard and the body:
678-
let mut rename_fld = IdentRenamer{renames:&new_pending_renames};
690+
let mut rename_fld = IdentRenamer{renames:&new_renames};
679691
let rewritten_guard =
680692
arm.guard.map(|g| fld.fold_expr(rename_fld.fold_expr(g)));
681693
let rewritten_body = fld.fold_expr(rename_fld.fold_expr(arm.body));
@@ -687,8 +699,6 @@ fn expand_arm(arm: &ast::Arm, fld: &mut MacroExpander) -> ast::Arm {
687699
}
688700
}
689701

690-
691-
692702
/// A visitor that extracts the PatIdent (binding) paths
693703
/// from a given thingy and puts them in a mutable
694704
/// array
@@ -721,6 +731,15 @@ fn pattern_bindings(pat : &ast::Pat) -> Vec<ast::Ident> {
721731
name_finder.ident_accumulator
722732
}
723733

734+
/// find the PatIdent paths in a
735+
fn fn_decl_arg_bindings(fn_decl: &ast::FnDecl) -> Vec<ast::Ident> {
736+
let mut pat_idents = PatIdentFinder{ident_accumulator:Vec::new()};
737+
for arg in fn_decl.inputs.iter() {
738+
pat_idents.visit_pat(arg.pat,());
739+
}
740+
pat_idents.ident_accumulator
741+
}
742+
724743
// expand a block. pushes a new exts_frame, then calls expand_block_elts
725744
fn expand_block(blk: &Block, fld: &mut MacroExpander) -> P<Block> {
726745
// see note below about treatment of exts table
@@ -882,6 +901,25 @@ impl<'a> Folder for PatIdentRenamer<'a> {
882901
}
883902
}
884903

904+
/// Given a fn_decl and a block and a MacroExpander, expand the fn_decl, then use the
905+
/// PatIdents in its arguments to perform renaming in the FnDecl and
906+
/// the block, returning both the new FnDecl and the new Block.
907+
fn expand_and_rename_fn_decl_and_block(fn_decl: &ast::FnDecl, block: Gc<ast::Block>,
908+
fld: &mut MacroExpander)
909+
-> (Gc<ast::FnDecl>, Gc<ast::Block>) {
910+
let expanded_decl = fld.fold_fn_decl(fn_decl);
911+
let idents = fn_decl_arg_bindings(expanded_decl);
912+
let renames =
913+
idents.iter().map(|id : &ast::Ident| (*id,fresh_name(id))).collect();
914+
// first, a renamer for the PatIdents, for the fn_decl:
915+
let mut rename_pat_fld = PatIdentRenamer{renames: &renames};
916+
let rewritten_fn_decl = rename_pat_fld.fold_fn_decl(expanded_decl);
917+
// now, a renamer for *all* idents, for the body:
918+
let mut rename_fld = IdentRenamer{renames: &renames};
919+
let rewritten_body = fld.fold_block(rename_fld.fold_block(block));
920+
(rewritten_fn_decl,rewritten_body)
921+
}
922+
885923
/// A tree-folder that performs macro expansion
886924
pub struct MacroExpander<'a, 'b> {
887925
pub extsbox: SyntaxEnv,
@@ -901,6 +939,10 @@ impl<'a, 'b> Folder for MacroExpander<'a, 'b> {
901939
expand_item(item, self)
902940
}
903941

942+
fn fold_item_underscore(&mut self, item: &ast::Item_) -> ast::Item_ {
943+
expand_item_underscore(item, self)
944+
}
945+
904946
fn fold_stmt(&mut self, stmt: &ast::Stmt) -> SmallVector<Gc<ast::Stmt>> {
905947
expand_stmt(stmt, self)
906948
}

0 commit comments

Comments
 (0)