@@ -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 > ) {
@@ -458,7 +461,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
458
461
}
459
462
460
463
let legacy_resolution =
461
- self . resolve_legacy_scope ( path[ 0 ] , invoc_id, & invocation. legacy_scope , false ) ;
464
+ self . resolve_legacy_scope ( path[ 0 ] , invoc_id, & invocation. parent_legacy_scope , false ) ;
462
465
let result = if let Some ( legacy_binding) = legacy_resolution {
463
466
Ok ( legacy_binding. def ( ) )
464
467
} else {
@@ -791,18 +794,20 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
791
794
792
795
macro_rules! continue_search { ( ) => {
793
796
where_to_resolve = match where_to_resolve. get( ) {
794
- LegacyScope :: Binding ( binding) => & binding. parent,
795
- LegacyScope :: Invocation ( invocation) => & invocation. legacy_scope,
796
- LegacyScope :: Expansion ( invocation) => match invocation. expansion. get( ) {
797
- LegacyScope :: Empty => & invocation. legacy_scope,
798
- LegacyScope :: Binding ( ..) |
799
- LegacyScope :: Expansion ( ..) => & invocation. expansion,
800
- LegacyScope :: Invocation ( ..) => {
801
- where_to_resolve. set( invocation. legacy_scope. get( ) ) ;
802
- where_to_resolve
797
+ LegacyScope :: Empty => break , // nowhere else to search
798
+ LegacyScope :: Binding ( binding) => & binding. parent_legacy_scope,
799
+ LegacyScope :: Invocation ( invocation) => & invocation. parent_legacy_scope,
800
+ LegacyScope :: Expansion ( invocation) => {
801
+ match invocation. output_legacy_scope. get( ) {
802
+ LegacyScope :: Empty => & invocation. parent_legacy_scope,
803
+ LegacyScope :: Binding ( ..) |
804
+ LegacyScope :: Expansion ( ..) => & invocation. output_legacy_scope,
805
+ LegacyScope :: Invocation ( ..) => {
806
+ where_to_resolve. set( invocation. parent_legacy_scope. get( ) ) ;
807
+ where_to_resolve
808
+ }
803
809
}
804
810
}
805
- LegacyScope :: Empty => break , // nowhere else to search
806
811
} ;
807
812
808
813
continue ;
@@ -857,8 +862,9 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
857
862
858
863
for & ( invoc_id, ident, kind, def) in module. legacy_macro_resolutions . borrow ( ) . iter ( ) {
859
864
let span = ident. span ;
860
- let legacy_scope = & self . invocations [ & invoc_id] . legacy_scope ;
861
- let legacy_resolution = self . resolve_legacy_scope ( ident, invoc_id, legacy_scope, true ) ;
865
+ let invoc_parent_legacy_scope = & self . invocations [ & invoc_id] . parent_legacy_scope ;
866
+ let legacy_resolution =
867
+ self . resolve_legacy_scope ( ident, invoc_id, invoc_parent_legacy_scope, true ) ;
862
868
let resolution = self . resolve_lexical_macro_path_segment (
863
869
ident, MacroNS , invoc_id, true , true , kind == MacroKind :: Attr , span
864
870
) ;
@@ -984,8 +990,8 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
984
990
arenas. alloc_invocation_data ( InvocationData {
985
991
def_index : invoc. def_index ,
986
992
module : Cell :: new ( graph_root) ,
987
- expansion : Cell :: new ( LegacyScope :: Empty ) ,
988
- legacy_scope : Cell :: new ( LegacyScope :: Empty ) ,
993
+ parent_legacy_scope : Cell :: new ( LegacyScope :: Empty ) ,
994
+ output_legacy_scope : Cell :: new ( LegacyScope :: Empty ) ,
989
995
} )
990
996
} ) ;
991
997
} ;
@@ -1000,7 +1006,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
1000
1006
pub fn define_macro ( & mut self ,
1001
1007
item : & ast:: Item ,
1002
1008
expansion : Mark ,
1003
- legacy_scope : & mut LegacyScope < ' a > ) {
1009
+ current_legacy_scope : & mut LegacyScope < ' a > ) {
1004
1010
self . local_macro_def_scopes . insert ( item. id , self . current_module ) ;
1005
1011
let ident = item. ident ;
1006
1012
if ident. name == "macro_rules" {
@@ -1020,9 +1026,10 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
1020
1026
let def = Def :: Macro ( def_id, MacroKind :: Bang ) ;
1021
1027
let vis = ty:: Visibility :: Invisible ; // Doesn't matter for legacy bindings
1022
1028
let binding = ( def, vis, item. span , expansion) . to_name_binding ( self . arenas ) ;
1023
- * legacy_scope = LegacyScope :: Binding ( self . arenas . alloc_legacy_binding (
1024
- LegacyBinding { parent : Cell :: new ( * legacy_scope) , binding, ident }
1025
- ) ) ;
1029
+ let legacy_binding = self . arenas . alloc_legacy_binding ( LegacyBinding {
1030
+ parent_legacy_scope : Cell :: new ( * current_legacy_scope) , binding, ident
1031
+ } ) ;
1032
+ * current_legacy_scope = LegacyScope :: Binding ( legacy_binding) ;
1026
1033
self . all_macros . insert ( ident. name , def) ;
1027
1034
if attr:: contains_name ( & item. attrs , "macro_export" ) {
1028
1035
let module = self . graph_root ;
0 commit comments