@@ -31,6 +31,7 @@ use util::small_vector::SmallVector;
31
31
32
32
use std:: gc:: { Gc , GC } ;
33
33
34
+
34
35
pub fn expand_expr ( e : Gc < ast:: Expr > , fld : & mut MacroExpander ) -> Gc < ast:: Expr > {
35
36
match e. node {
36
37
// expr_mac should really be expr_ext or something; it's the
@@ -130,8 +131,6 @@ pub fn expand_expr(e: Gc<ast::Expr>, fld: &mut MacroExpander) -> Gc<ast::Expr> {
130
131
// From: `['<ident>:] for <src_pat> in <src_expr> <src_loop_block>`
131
132
// FIXME #6993: change type of opt_ident to Option<Name>
132
133
ast:: ExprForLoop ( src_pat, src_expr, src_loop_block, opt_ident) => {
133
- // Expand any interior macros etc.
134
- // NB: we don't fold pats yet. Curious.
135
134
136
135
let span = e. span ;
137
136
@@ -281,7 +280,7 @@ macro_rules! with_exts_frame (
281
280
)
282
281
283
282
// When we enter a module, record it, for the sake of `module!`
284
- pub fn expand_item ( it : Gc < ast:: Item > , fld : & mut MacroExpander )
283
+ fn expand_item ( it : Gc < ast:: Item > , fld : & mut MacroExpander )
285
284
-> SmallVector < Gc < ast:: Item > > {
286
285
let it = expand_item_modifiers ( it, fld) ;
287
286
@@ -386,13 +385,13 @@ fn expand_item_modifiers(mut it: Gc<ast::Item>, fld: &mut MacroExpander)
386
385
}
387
386
388
387
// does this attribute list contain "macro_escape" ?
389
- pub fn contains_macro_escape ( attrs : & [ ast:: Attribute ] ) -> bool {
388
+ fn contains_macro_escape ( attrs : & [ ast:: Attribute ] ) -> bool {
390
389
attr:: contains_name ( attrs, "macro_escape" )
391
390
}
392
391
393
392
// Support for item-position macro invocations, exactly the same
394
393
// logic as for expression-position macro invocations.
395
- pub fn expand_item_mac ( it : Gc < ast:: Item > , fld : & mut MacroExpander )
394
+ fn expand_item_mac ( it : Gc < ast:: Item > , fld : & mut MacroExpander )
396
395
-> SmallVector < Gc < ast:: Item > > {
397
396
let ( pth, tts) = match it. node {
398
397
ItemMac ( codemap:: Spanned {
@@ -498,7 +497,7 @@ pub fn expand_item_mac(it: Gc<ast::Item>, fld: &mut MacroExpander)
498
497
}
499
498
500
499
// expand a stmt
501
- pub fn expand_stmt ( s : & Stmt , fld : & mut MacroExpander ) -> SmallVector < Gc < Stmt > > {
500
+ fn expand_stmt ( s : & Stmt , fld : & mut MacroExpander ) -> SmallVector < Gc < Stmt > > {
502
501
// why the copying here and not in expand_expr?
503
502
// looks like classic changed-in-only-one-place
504
503
let ( pth, tts, semi) = match s. node {
@@ -659,6 +658,42 @@ fn expand_non_macro_stmt(s: &Stmt, fld: &mut MacroExpander)
659
658
}
660
659
}
661
660
661
+ fn expand_arm ( arm : & ast:: Arm , fld : & mut MacroExpander ) -> ast:: Arm {
662
+ if a. pats . len ( ) == 0 {
663
+ fail ! ( "encountered match arm with 0 patterns" ) ;
664
+ }
665
+ let first_pat = match a. pats . get ( 0 ) {
666
+
667
+ }
668
+ // code duplicated from 'let', above. Perhaps this can be lifted
669
+ // into a separate function:
670
+ let expanded_pat = fld. fold_pat ( pat) ;
671
+ let mut name_finder = new_name_finder ( Vec :: new ( ) ) ;
672
+ name_finder. visit_pat ( & * expanded_pat, ( ) ) ;
673
+ let mut new_pending_renames = Vec :: new ( ) ;
674
+ for ident in name_finder. ident_accumulator . iter ( ) {
675
+ let new_name = fresh_name ( ident) ;
676
+ new_pending_renames. push ( ( * ident, new_name) ) ;
677
+ }
678
+ let rewritten_pat = {
679
+ let mut rename_fld =
680
+ renames_to_fold ( & mut new_pending_renames) ;
681
+ // rewrite the pattern using the new names (the old
682
+ // ones have already been applied):
683
+ rename_fld. fold_pat ( expanded_pat)
684
+ } ;
685
+
686
+ let bound_names
687
+ ast:: Arm {
688
+ attrs : a. attrs . iter ( ) . map ( |x| self . fold_attribute ( * x) ) . collect ( ) ,
689
+ pats : a. pats . iter ( ) . map ( |x| self . fold_pat ( * x) ) . collect ( ) ,
690
+ guard : a. guard . map ( |x| self . fold_expr ( x) ) ,
691
+ body : self . fold_expr ( a. body ) ,
692
+ }
693
+ }
694
+
695
+
696
+
662
697
// a visitor that extracts the pat_ident (binding) paths
663
698
// from a given thingy and puts them in a mutable
664
699
// array (passed in to the traversal).
@@ -711,14 +746,14 @@ fn new_name_finder(idents: Vec<ast::Ident> ) -> NameFinderContext {
711
746
}
712
747
713
748
// expand a block. pushes a new exts_frame, then calls expand_block_elts
714
- pub fn expand_block ( blk : & Block , fld : & mut MacroExpander ) -> P < Block > {
749
+ fn expand_block ( blk : & Block , fld : & mut MacroExpander ) -> P < Block > {
715
750
// see note below about treatment of exts table
716
751
with_exts_frame ! ( fld. extsbox, false ,
717
752
expand_block_elts( blk, fld) )
718
753
}
719
754
720
755
// expand the elements of a block.
721
- pub fn expand_block_elts ( b : & Block , fld : & mut MacroExpander ) -> P < Block > {
756
+ fn expand_block_elts ( b : & Block , fld : & mut MacroExpander ) -> P < Block > {
722
757
let new_view_items = b. view_items . iter ( ) . map ( |x| fld. fold_view_item ( x) ) . collect ( ) ;
723
758
let new_stmts =
724
759
b. stmts . iter ( ) . flat_map ( |x| {
@@ -747,7 +782,7 @@ pub fn expand_block_elts(b: &Block, fld: &mut MacroExpander) -> P<Block> {
747
782
} )
748
783
}
749
784
750
- pub fn expand_pat ( p : Gc < ast:: Pat > , fld : & mut MacroExpander ) -> Gc < ast:: Pat > {
785
+ fn expand_pat ( p : Gc < ast:: Pat > , fld : & mut MacroExpander ) -> Gc < ast:: Pat > {
751
786
let ( pth, tts) = match p. node {
752
787
PatMac ( ref mac) => {
753
788
match mac. node {
@@ -842,13 +877,13 @@ impl<'a> Folder for IdentRenamer<'a> {
842
877
843
878
// given a mutable list of renames, return a tree-folder that applies those
844
879
// renames.
845
- pub fn renames_to_fold < ' a > ( renames : & ' a mut RenameList ) -> IdentRenamer < ' a > {
880
+ fn renames_to_fold < ' a > ( renames : & ' a mut RenameList ) -> IdentRenamer < ' a > {
846
881
IdentRenamer {
847
882
renames : renames,
848
883
}
849
884
}
850
885
851
- pub fn new_span ( cx : & ExtCtxt , sp : Span ) -> Span {
886
+ fn new_span ( cx : & ExtCtxt , sp : Span ) -> Span {
852
887
/* this discards information in the case of macro-defining macros */
853
888
Span {
854
889
lo : sp. lo ,
@@ -883,6 +918,10 @@ impl<'a, 'b> Folder for MacroExpander<'a, 'b> {
883
918
expand_block ( & * block, self )
884
919
}
885
920
921
+ fn fold_arm ( & mut self , arm : & ast:: Arm ) -> ast:: Arm {
922
+ expand_arm ( arm, self )
923
+ }
924
+
886
925
fn new_span ( & mut self , span : Span ) -> Span {
887
926
new_span ( self . cx , span)
888
927
}
@@ -1248,7 +1287,6 @@ mod test {
1248
1287
1249
1288
// FIXME #9384, match variable hygiene. Should expand into
1250
1289
// fn z() {match 8 {x_1 => {match 9 {x_2 | x_2 => x_2 + x_1}}}}
1251
- #[ ignore]
1252
1290
#[ test] fn issue_9384 ( ) {
1253
1291
run_renaming_test (
1254
1292
& ( "macro_rules! bad_macro (($ex:expr) => ({match 9 {x | x => x + $ex}}))
0 commit comments