@@ -53,6 +53,7 @@ use rustc::ty::subst::{ParamSpace, FnSpace, TypeSpace};
5353use rustc:: hir:: { Freevar , FreevarMap , TraitCandidate , TraitMap , GlobMap } ;
5454use rustc:: util:: nodemap:: { NodeMap , NodeSet , FnvHashMap , FnvHashSet } ;
5555
56+ use syntax:: ext:: mtwt;
5657use syntax:: ast:: { self , FloatTy } ;
5758use syntax:: ast:: { CRATE_NODE_ID , Name , NodeId , CrateNum , IntTy , UintTy } ;
5859use syntax:: parse:: token:: { self , keywords} ;
@@ -651,6 +652,9 @@ enum RibKind<'a> {
651652
652653 // We passed through a module.
653654 ModuleRibKind ( Module < ' a > ) ,
655+
656+ // We passed through a `macro_rules!` statement with the given expansion
657+ MacroDefinition ( ast:: Mrk ) ,
654658}
655659
656660#[ derive( Copy , Clone ) ]
@@ -927,6 +931,10 @@ pub struct Resolver<'a> {
927931
928932 pub definitions : Definitions ,
929933
934+ // Maps the node id of a statement to the expansions of the `macro_rules!`s
935+ // immediately above the statement (if appropriate).
936+ macros_at_scope : HashMap < NodeId , Vec < ast:: Mrk > > ,
937+
930938 graph_root : Module < ' a > ,
931939
932940 prelude : Option < Module < ' a > > ,
@@ -1113,6 +1121,7 @@ impl<'a> Resolver<'a> {
11131121 session : session,
11141122
11151123 definitions : Definitions :: new ( ) ,
1124+ macros_at_scope : HashMap :: new ( ) ,
11161125
11171126 // The outermost module has def ID 0; this is not reflected in the
11181127 // AST.
@@ -1421,6 +1430,16 @@ impl<'a> Resolver<'a> {
14211430 } ;
14221431 }
14231432 }
1433+
1434+ if let MacroDefinition ( mac) = self . get_ribs ( ns) [ i] . kind {
1435+ // If an invocation of this macro created `ident`, give up on `ident`
1436+ // and switch to `ident`'s source from the macro definition.
1437+ if let Some ( ( source_ident, source_macro) ) = mtwt:: source ( ident) {
1438+ if mac == source_macro {
1439+ ident = source_ident;
1440+ }
1441+ }
1442+ }
14241443 }
14251444
14261445 None
@@ -2069,6 +2088,7 @@ impl<'a> Resolver<'a> {
20692088 let orig_module = self . current_module ;
20702089 let anonymous_module = self . module_map . get ( & block. id ) . cloned ( ) ; // clones a reference
20712090
2091+ let mut num_value_ribs = 1 ;
20722092 if let Some ( anonymous_module) = anonymous_module {
20732093 debug ! ( "(resolving block) found anonymous module, moving down" ) ;
20742094 self . value_ribs . push ( Rib :: new ( ModuleRibKind ( anonymous_module) ) ) ;
@@ -2079,11 +2099,22 @@ impl<'a> Resolver<'a> {
20792099 }
20802100
20812101 // Descend into the block.
2082- visit:: walk_block ( self , block) ;
2102+ for stmt in & block. stmts {
2103+ if let Some ( marks) = self . macros_at_scope . remove ( & stmt. id ) {
2104+ num_value_ribs += marks. len ( ) as u32 ;
2105+ for mark in marks {
2106+ self . value_ribs . push ( Rib :: new ( MacroDefinition ( mark) ) ) ;
2107+ }
2108+ }
2109+
2110+ self . visit_stmt ( stmt) ;
2111+ }
20832112
20842113 // Move back up.
20852114 self . current_module = orig_module;
2086- self . value_ribs . pop ( ) ;
2115+ for _ in 0 .. num_value_ribs {
2116+ self . value_ribs . pop ( ) ;
2117+ }
20872118 if let Some ( _) = anonymous_module {
20882119 self . type_ribs . pop ( ) ;
20892120 }
@@ -2497,7 +2528,7 @@ impl<'a> Resolver<'a> {
24972528 Def :: Local ( _, node_id) => {
24982529 for rib in ribs {
24992530 match rib. kind {
2500- NormalRibKind | ModuleRibKind ( ..) => {
2531+ NormalRibKind | ModuleRibKind ( ..) | MacroDefinition ( .. ) => {
25012532 // Nothing to do. Continue.
25022533 }
25032534 ClosureRibKind ( function_id) => {
@@ -2546,7 +2577,7 @@ impl<'a> Resolver<'a> {
25462577 for rib in ribs {
25472578 match rib. kind {
25482579 NormalRibKind | MethodRibKind ( _) | ClosureRibKind ( ..) |
2549- ModuleRibKind ( ..) => {
2580+ ModuleRibKind ( ..) | MacroDefinition ( .. ) => {
25502581 // Nothing to do. Continue.
25512582 }
25522583 ItemRibKind => {
0 commit comments