@@ -50,26 +50,37 @@ crate struct FromPrelude(bool);
50
50
51
51
#[ derive( Clone ) ]
52
52
pub 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 > > ,
60
62
}
61
63
62
64
impl < ' a > InvocationData < ' a > {
63
65
pub fn root ( graph_root : Module < ' a > ) -> Self {
64
66
InvocationData {
65
67
module : Cell :: new ( graph_root) ,
66
68
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 ) ,
69
71
}
70
72
}
71
73
}
72
74
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
+
73
84
#[ derive( Copy , Clone ) ]
74
85
pub enum LegacyScope < ' a > {
75
86
Empty ,
@@ -78,14 +89,6 @@ pub enum LegacyScope<'a> {
78
89
Binding ( & ' a LegacyBinding < ' a > ) ,
79
90
}
80
91
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
-
89
92
pub struct ProcMacError {
90
93
crate_name : Symbol ,
91
94
name : Symbol ,
@@ -105,8 +108,8 @@ impl<'a, 'crateloader: 'a> base::Resolver for Resolver<'a, 'crateloader> {
105
108
self . invocations . insert ( mark, self . arenas . alloc_invocation_data ( InvocationData {
106
109
module : Cell :: new ( module) ,
107
110
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 ) ,
110
113
} ) ) ;
111
114
mark
112
115
}
@@ -178,11 +181,11 @@ impl<'a, 'crateloader: 'a> base::Resolver for Resolver<'a, 'crateloader> {
178
181
}
179
182
let mut visitor = BuildReducedGraphVisitor {
180
183
resolver : self ,
181
- legacy_scope : LegacyScope :: Invocation ( invocation) ,
184
+ current_legacy_scope : LegacyScope :: Invocation ( invocation) ,
182
185
expansion : mark,
183
186
} ;
184
187
fragment. visit_with ( & mut visitor) ;
185
- invocation. expansion . set ( visitor. legacy_scope ) ;
188
+ invocation. output_legacy_scope . set ( visitor. current_legacy_scope ) ;
186
189
}
187
190
188
191
fn add_builtin ( & mut self , ident : ast:: Ident , ext : Lrc < SyntaxExtension > ) {
@@ -481,7 +484,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
481
484
}
482
485
483
486
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 ) ;
485
488
let result = if let Some ( legacy_binding) = legacy_resolution {
486
489
Ok ( legacy_binding. def ( ) )
487
490
} else {
@@ -814,18 +817,20 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
814
817
815
818
macro_rules! continue_search { ( ) => {
816
819
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
+ }
826
832
}
827
833
}
828
- LegacyScope :: Empty => break , // nowhere else to search
829
834
} ;
830
835
831
836
continue ;
@@ -880,8 +885,9 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
880
885
881
886
for & ( invoc_id, ident, kind, def) in module. legacy_macro_resolutions . borrow ( ) . iter ( ) {
882
887
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 ) ;
885
891
let resolution = self . resolve_lexical_macro_path_segment (
886
892
ident, MacroNS , invoc_id, true , true , kind == MacroKind :: Attr , span
887
893
) ;
@@ -1007,8 +1013,8 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
1007
1013
arenas. alloc_invocation_data ( InvocationData {
1008
1014
def_index : invoc. def_index ,
1009
1015
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 ) ,
1012
1018
} )
1013
1019
} ) ;
1014
1020
} ;
@@ -1023,7 +1029,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
1023
1029
pub fn define_macro ( & mut self ,
1024
1030
item : & ast:: Item ,
1025
1031
expansion : Mark ,
1026
- legacy_scope : & mut LegacyScope < ' a > ) {
1032
+ current_legacy_scope : & mut LegacyScope < ' a > ) {
1027
1033
self . local_macro_def_scopes . insert ( item. id , self . current_module ) ;
1028
1034
let ident = item. ident ;
1029
1035
if ident. name == "macro_rules" {
@@ -1043,9 +1049,10 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
1043
1049
let def = Def :: Macro ( def_id, MacroKind :: Bang ) ;
1044
1050
let vis = ty:: Visibility :: Invisible ; // Doesn't matter for legacy bindings
1045
1051
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) ;
1049
1056
self . all_macros . insert ( ident. name , def) ;
1050
1057
if attr:: contains_name ( & item. attrs , "macro_export" ) {
1051
1058
let module = self . graph_root ;
0 commit comments