Skip to content

Commit 70f9701

Browse files
committed
---
yaml --- r: 153115 b: refs/heads/try2 c: 0ba15c9 h: refs/heads/master i: 153113: b80f899 153111: 5afaa5f v: v3
1 parent 7211272 commit 70f9701

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: 92708322fd19747100ff5eda2e4584bc9d1f2b38
8+
refs/heads/try2: 0ba15c99f31f769bd015871202000e48c77066ca
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/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)