@@ -8,7 +8,7 @@ use crate::build_reduced_graph::{BuildReducedGraphVisitor, IsMacroExport};
8
8
use crate :: resolve_imports:: ImportResolver ;
9
9
use rustc:: hir:: def_id:: { CrateNum , DefId , DefIndex , CRATE_DEF_INDEX } ;
10
10
use rustc:: hir:: def:: { self , DefKind , NonMacroAttrKind } ;
11
- use rustc:: hir:: map:: { self , DefCollector } ;
11
+ use rustc:: hir:: map:: DefCollector ;
12
12
use rustc:: middle:: stability;
13
13
use rustc:: { ty, lint, span_bug} ;
14
14
use syntax:: ast:: { self , Ident } ;
@@ -32,14 +32,14 @@ use rustc_data_structures::sync::Lrc;
32
32
33
33
type Res = def:: Res < ast:: NodeId > ;
34
34
35
+ // FIXME: Merge this with `ParentScope`.
35
36
#[ derive( Clone , Debug ) ]
36
37
pub struct InvocationData < ' a > {
37
- def_index : DefIndex ,
38
38
/// The module in which the macro was invoked.
39
- crate module : Cell < Module < ' a > > ,
39
+ crate module : Module < ' a > ,
40
40
/// The legacy scope in which the macro was invoked.
41
41
/// The invocation path is resolved in this scope.
42
- crate parent_legacy_scope : Cell < LegacyScope < ' a > > ,
42
+ crate parent_legacy_scope : LegacyScope < ' a > ,
43
43
/// The legacy scope *produced* by expanding this macro invocation,
44
44
/// includes all the macro_rules items, other invocations, etc generated by it.
45
45
/// `None` if the macro is not expanded yet.
@@ -49,10 +49,9 @@ pub struct InvocationData<'a> {
49
49
impl < ' a > InvocationData < ' a > {
50
50
pub fn root ( graph_root : Module < ' a > ) -> Self {
51
51
InvocationData {
52
- module : Cell :: new ( graph_root) ,
53
- def_index : CRATE_DEF_INDEX ,
54
- parent_legacy_scope : Cell :: new ( LegacyScope :: Empty ) ,
55
- output_legacy_scope : Cell :: new ( Some ( LegacyScope :: Empty ) ) ,
52
+ module : graph_root,
53
+ parent_legacy_scope : LegacyScope :: Empty ,
54
+ output_legacy_scope : Cell :: new ( None ) ,
56
55
}
57
56
}
58
57
}
@@ -74,9 +73,6 @@ pub struct LegacyBinding<'a> {
74
73
/// can potentially expand into macro definitions.
75
74
#[ derive( Copy , Clone , Debug ) ]
76
75
pub enum LegacyScope < ' a > {
77
- /// Created when invocation data is allocated in the arena;
78
- /// must be replaced with a proper scope later.
79
- Uninitialized ,
80
76
/// Empty "root" scope at the crate start containing no names.
81
77
Empty ,
82
78
/// The scope introduced by a `macro_rules!` macro definition.
@@ -139,11 +135,11 @@ impl<'a> base::Resolver for Resolver<'a> {
139
135
fn get_module_scope ( & mut self , id : ast:: NodeId ) -> Mark {
140
136
let mark = Mark :: fresh ( Mark :: root ( ) ) ;
141
137
let module = self . module_map [ & self . definitions . local_def_id ( id) ] ;
138
+ self . definitions . set_invocation_parent ( mark, module. def_id ( ) . unwrap ( ) . index ) ;
142
139
self . invocations . insert ( mark, self . arenas . alloc_invocation_data ( InvocationData {
143
- module : Cell :: new ( module) ,
144
- def_index : module. def_id ( ) . unwrap ( ) . index ,
145
- parent_legacy_scope : Cell :: new ( LegacyScope :: Empty ) ,
146
- output_legacy_scope : Cell :: new ( Some ( LegacyScope :: Empty ) ) ,
140
+ module,
141
+ parent_legacy_scope : LegacyScope :: Empty ,
142
+ output_legacy_scope : Cell :: new ( None ) ,
147
143
} ) ) ;
148
144
mark
149
145
}
@@ -160,16 +156,20 @@ impl<'a> base::Resolver for Resolver<'a> {
160
156
161
157
fn visit_ast_fragment_with_placeholders ( & mut self , mark : Mark , fragment : & AstFragment ,
162
158
derives : & [ Mark ] ) {
163
- let invocation = self . invocations [ & mark] ;
164
- self . collect_def_ids ( mark, invocation, fragment) ;
159
+ fragment. visit_with ( & mut DefCollector :: new ( & mut self . definitions , mark) ) ;
165
160
166
- self . current_module = invocation. module . get ( ) ;
161
+ let invocation = self . invocations [ & mark] ;
162
+ self . current_module = invocation. module ;
167
163
self . current_module . unresolved_invocations . borrow_mut ( ) . remove ( & mark) ;
168
164
self . current_module . unresolved_invocations . borrow_mut ( ) . extend ( derives) ;
165
+ let parent_def = self . definitions . invocation_parent ( mark) ;
166
+ for & derive_invoc_id in derives {
167
+ self . definitions . set_invocation_parent ( derive_invoc_id, parent_def) ;
168
+ }
169
169
self . invocations . extend ( derives. iter ( ) . map ( |& derive| ( derive, invocation) ) ) ;
170
170
let mut visitor = BuildReducedGraphVisitor {
171
171
resolver : self ,
172
- current_legacy_scope : invocation. parent_legacy_scope . get ( ) ,
172
+ current_legacy_scope : invocation. parent_legacy_scope ,
173
173
expansion : mark,
174
174
} ;
175
175
fragment. visit_with ( & mut visitor) ;
@@ -259,9 +259,9 @@ impl<'a> Resolver<'a> {
259
259
fn invoc_parent_scope ( & self , invoc_id : Mark , derives : Vec < ast:: Path > ) -> ParentScope < ' a > {
260
260
let invoc = self . invocations [ & invoc_id] ;
261
261
ParentScope {
262
- module : invoc. module . get ( ) . nearest_item_scope ( ) ,
262
+ module : invoc. module . nearest_item_scope ( ) ,
263
263
expansion : invoc_id. parent ( ) ,
264
- legacy : invoc. parent_legacy_scope . get ( ) ,
264
+ legacy : invoc. parent_legacy_scope ,
265
265
derives,
266
266
}
267
267
}
@@ -829,10 +829,9 @@ impl<'a> Resolver<'a> {
829
829
binding. parent_legacy_scope
830
830
) ,
831
831
LegacyScope :: Invocation ( invoc) => WhereToResolve :: MacroRules (
832
- invoc. output_legacy_scope . get ( ) . unwrap_or ( invoc. parent_legacy_scope . get ( ) )
832
+ invoc. output_legacy_scope . get ( ) . unwrap_or ( invoc. parent_legacy_scope )
833
833
) ,
834
834
LegacyScope :: Empty => WhereToResolve :: Module ( parent_scope. module ) ,
835
- LegacyScope :: Uninitialized => unreachable ! ( ) ,
836
835
}
837
836
WhereToResolve :: CrateRoot => match ns {
838
837
TypeNS => {
@@ -1084,31 +1083,6 @@ impl<'a> Resolver<'a> {
1084
1083
}
1085
1084
}
1086
1085
1087
- fn collect_def_ids ( & mut self ,
1088
- mark : Mark ,
1089
- invocation : & ' a InvocationData < ' a > ,
1090
- fragment : & AstFragment ) {
1091
- let Resolver { ref mut invocations, arenas, graph_root, .. } = * self ;
1092
- let InvocationData { def_index, .. } = * invocation;
1093
-
1094
- let visit_macro_invoc = & mut |invoc : map:: MacroInvocationData | {
1095
- invocations. entry ( invoc. mark ) . or_insert_with ( || {
1096
- arenas. alloc_invocation_data ( InvocationData {
1097
- def_index : invoc. def_index ,
1098
- module : Cell :: new ( graph_root) ,
1099
- parent_legacy_scope : Cell :: new ( LegacyScope :: Uninitialized ) ,
1100
- output_legacy_scope : Cell :: new ( None ) ,
1101
- } )
1102
- } ) ;
1103
- } ;
1104
-
1105
- let mut def_collector = DefCollector :: new ( & mut self . definitions , mark) ;
1106
- def_collector. visit_macro_invoc = Some ( visit_macro_invoc) ;
1107
- def_collector. with_parent ( def_index, |def_collector| {
1108
- fragment. visit_with ( def_collector)
1109
- } ) ;
1110
- }
1111
-
1112
1086
crate fn check_reserved_macro_name ( & mut self , ident : Ident , res : Res ) {
1113
1087
// Reserve some names that are not quite covered by the general check
1114
1088
// performed on `Resolver::builtin_attrs`.
0 commit comments