@@ -50,26 +50,37 @@ crate struct FromPrelude(bool);
5050
5151#[ derive( Clone ) ]
5252pub struct InvocationData < ' a > {
53- pub module : Cell < Module < ' a > > ,
54- pub def_index : DefIndex ,
55- // The scope in which the invocation path is resolved.
56- pub legacy_scope : Cell < LegacyScope < ' a > > ,
57- // The smallest scope that includes this invocation's expansion,
58- // or `Empty` if this invocation has not been expanded yet.
59- pub expansion : Cell < LegacyScope < ' a > > ,
53+ crate module : Cell < Module < ' a > > ,
54+ def_index : DefIndex ,
55+ // Legacy scope in which the macro was invoked.
56+ // The invocation path is resolved in this scope.
57+ crate parent_legacy_scope : Cell < LegacyScope < ' a > > ,
58+ // Legacy scope *produced* by expanding this macro invocation,
59+ // includes all the macro_rules items, other invocations, etc generated by it.
60+ // `Empty` is used if for invocations that has not been expanded yet.
61+ output_legacy_scope : Cell < LegacyScope < ' a > > ,
6062}
6163
6264impl < ' a > InvocationData < ' a > {
6365 pub fn root ( graph_root : Module < ' a > ) -> Self {
6466 InvocationData {
6567 module : Cell :: new ( graph_root) ,
6668 def_index : CRATE_DEF_INDEX ,
67- legacy_scope : Cell :: new ( LegacyScope :: Empty ) ,
68- expansion : Cell :: new ( LegacyScope :: Empty ) ,
69+ parent_legacy_scope : Cell :: new ( LegacyScope :: Empty ) ,
70+ output_legacy_scope : Cell :: new ( LegacyScope :: Empty ) ,
6971 }
7072 }
7173}
7274
75+ // Binding produced by a `macro_rules` item.
76+ // Not modularized, can shadow previous legacy bindings, etc.
77+ pub struct LegacyBinding < ' a > {
78+ binding : & ' a NameBinding < ' a > ,
79+ // Legacy scope into which the `macro_rules` item was planted.
80+ parent_legacy_scope : Cell < LegacyScope < ' a > > ,
81+ ident : Ident ,
82+ }
83+
7384#[ derive( Copy , Clone ) ]
7485pub enum LegacyScope < ' a > {
7586 Empty ,
@@ -78,14 +89,6 @@ pub enum LegacyScope<'a> {
7889 Binding ( & ' a LegacyBinding < ' a > ) ,
7990}
8091
81- // Binding produced by a `macro_rules` item.
82- // Not modularized, can shadow previous legacy bindings, etc.
83- pub struct LegacyBinding < ' a > {
84- binding : & ' a NameBinding < ' a > ,
85- parent : Cell < LegacyScope < ' a > > ,
86- ident : Ident ,
87- }
88-
8992pub struct ProcMacError {
9093 crate_name : Symbol ,
9194 name : Symbol ,
@@ -105,8 +108,8 @@ impl<'a, 'crateloader: 'a> base::Resolver for Resolver<'a, 'crateloader> {
105108 self . invocations . insert ( mark, self . arenas . alloc_invocation_data ( InvocationData {
106109 module : Cell :: new ( module) ,
107110 def_index : module. def_id ( ) . unwrap ( ) . index ,
108- legacy_scope : Cell :: new ( LegacyScope :: Empty ) ,
109- expansion : Cell :: new ( LegacyScope :: Empty ) ,
111+ parent_legacy_scope : Cell :: new ( LegacyScope :: Empty ) ,
112+ output_legacy_scope : Cell :: new ( LegacyScope :: Empty ) ,
110113 } ) ) ;
111114 mark
112115 }
@@ -178,11 +181,11 @@ impl<'a, 'crateloader: 'a> base::Resolver for Resolver<'a, 'crateloader> {
178181 }
179182 let mut visitor = BuildReducedGraphVisitor {
180183 resolver : self ,
181- legacy_scope : LegacyScope :: Invocation ( invocation) ,
184+ current_legacy_scope : LegacyScope :: Invocation ( invocation) ,
182185 expansion : mark,
183186 } ;
184187 fragment. visit_with ( & mut visitor) ;
185- invocation. expansion . set ( visitor. legacy_scope ) ;
188+ invocation. output_legacy_scope . set ( visitor. current_legacy_scope ) ;
186189 }
187190
188191 fn add_builtin ( & mut self , ident : ast:: Ident , ext : Lrc < SyntaxExtension > ) {
@@ -481,7 +484,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
481484 }
482485
483486 let legacy_resolution =
484- self . resolve_legacy_scope ( path[ 0 ] , invoc_id, & invocation. legacy_scope , false ) ;
487+ self . resolve_legacy_scope ( path[ 0 ] , invoc_id, & invocation. parent_legacy_scope , false ) ;
485488 let result = if let Some ( legacy_binding) = legacy_resolution {
486489 Ok ( legacy_binding. def ( ) )
487490 } else {
@@ -814,18 +817,20 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
814817
815818 macro_rules! continue_search { ( ) => {
816819 where_to_resolve = match where_to_resolve. get( ) {
817- LegacyScope :: Binding ( binding) => & binding. parent,
818- LegacyScope :: Invocation ( invocation) => & invocation. legacy_scope,
819- LegacyScope :: Expansion ( invocation) => match invocation. expansion. get( ) {
820- LegacyScope :: Empty => & invocation. legacy_scope,
821- LegacyScope :: Binding ( ..) |
822- LegacyScope :: Expansion ( ..) => & invocation. expansion,
823- LegacyScope :: Invocation ( ..) => {
824- where_to_resolve. set( invocation. legacy_scope. get( ) ) ;
825- where_to_resolve
820+ LegacyScope :: Empty => break , // nowhere else to search
821+ LegacyScope :: Binding ( binding) => & binding. parent_legacy_scope,
822+ LegacyScope :: Invocation ( invocation) => & invocation. parent_legacy_scope,
823+ LegacyScope :: Expansion ( invocation) => {
824+ match invocation. output_legacy_scope. get( ) {
825+ LegacyScope :: Empty => & invocation. parent_legacy_scope,
826+ LegacyScope :: Binding ( ..) |
827+ LegacyScope :: Expansion ( ..) => & invocation. output_legacy_scope,
828+ LegacyScope :: Invocation ( ..) => {
829+ where_to_resolve. set( invocation. parent_legacy_scope. get( ) ) ;
830+ where_to_resolve
831+ }
826832 }
827833 }
828- LegacyScope :: Empty => break , // nowhere else to search
829834 } ;
830835
831836 continue ;
@@ -880,8 +885,9 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
880885
881886 for & ( invoc_id, ident, kind, def) in module. legacy_macro_resolutions . borrow ( ) . iter ( ) {
882887 let span = ident. span ;
883- let legacy_scope = & self . invocations [ & invoc_id] . legacy_scope ;
884- let legacy_resolution = self . resolve_legacy_scope ( ident, invoc_id, legacy_scope, true ) ;
888+ let invoc_parent_legacy_scope = & self . invocations [ & invoc_id] . parent_legacy_scope ;
889+ let legacy_resolution =
890+ self . resolve_legacy_scope ( ident, invoc_id, invoc_parent_legacy_scope, true ) ;
885891 let resolution = self . resolve_lexical_macro_path_segment (
886892 ident, MacroNS , invoc_id, true , true , kind == MacroKind :: Attr , span
887893 ) ;
@@ -1007,8 +1013,8 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
10071013 arenas. alloc_invocation_data ( InvocationData {
10081014 def_index : invoc. def_index ,
10091015 module : Cell :: new ( graph_root) ,
1010- expansion : Cell :: new ( LegacyScope :: Empty ) ,
1011- legacy_scope : Cell :: new ( LegacyScope :: Empty ) ,
1016+ parent_legacy_scope : Cell :: new ( LegacyScope :: Empty ) ,
1017+ output_legacy_scope : Cell :: new ( LegacyScope :: Empty ) ,
10121018 } )
10131019 } ) ;
10141020 } ;
@@ -1023,7 +1029,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
10231029 pub fn define_macro ( & mut self ,
10241030 item : & ast:: Item ,
10251031 expansion : Mark ,
1026- legacy_scope : & mut LegacyScope < ' a > ) {
1032+ current_legacy_scope : & mut LegacyScope < ' a > ) {
10271033 self . local_macro_def_scopes . insert ( item. id , self . current_module ) ;
10281034 let ident = item. ident ;
10291035 if ident. name == "macro_rules" {
@@ -1043,9 +1049,10 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
10431049 let def = Def :: Macro ( def_id, MacroKind :: Bang ) ;
10441050 let vis = ty:: Visibility :: Invisible ; // Doesn't matter for legacy bindings
10451051 let binding = ( def, vis, item. span , expansion) . to_name_binding ( self . arenas ) ;
1046- * legacy_scope = LegacyScope :: Binding ( self . arenas . alloc_legacy_binding (
1047- LegacyBinding { parent : Cell :: new ( * legacy_scope) , binding, ident }
1048- ) ) ;
1052+ let legacy_binding = self . arenas . alloc_legacy_binding ( LegacyBinding {
1053+ parent_legacy_scope : Cell :: new ( * current_legacy_scope) , binding, ident
1054+ } ) ;
1055+ * current_legacy_scope = LegacyScope :: Binding ( legacy_binding) ;
10491056 self . all_macros . insert ( ident. name , def) ;
10501057 if attr:: contains_name ( & item. attrs , "macro_export" ) {
10511058 let module = self . graph_root ;
0 commit comments