@@ -32,7 +32,7 @@ use crate::def_collector::collect_definitions;
32
32
use crate :: imports:: { ImportData , ImportKind } ;
33
33
use crate :: macros:: { MacroRulesBinding , MacroRulesScope , MacroRulesScopeRef } ;
34
34
use crate :: {
35
- BindingKey , Determinacy , ExternPreludeEntry , Finalize , MacroData , Module , ModuleKind ,
35
+ BindingKey , Determinacy , ExternPreludeEntry , Finalize , Macro , MacroData , Module , ModuleKind ,
36
36
ModuleOrUniformRoot , NameBinding , NameBindingData , NameBindingKind , ParentScope , PathResult ,
37
37
ResolutionError , Resolver , ResolverArenas , Segment , ToNameBinding , Used , VisResolutionError ,
38
38
errors,
@@ -162,28 +162,35 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
162
162
}
163
163
}
164
164
165
- pub ( crate ) fn get_macro ( & mut self , res : Res ) -> Option < & MacroData > {
165
+ pub ( crate ) fn get_macro ( & mut self , res : Res ) -> Option < Macro < ' ra > > {
166
166
match res {
167
167
Res :: Def ( DefKind :: Macro ( ..) , def_id) => Some ( self . get_macro_by_def_id ( def_id) ) ,
168
- Res :: NonMacroAttr ( _) => Some ( & self . non_macro_attr ) ,
168
+ Res :: NonMacroAttr ( _) => Some ( self . non_macro_attr ) ,
169
169
_ => None ,
170
170
}
171
171
}
172
172
173
- pub ( crate ) fn get_macro_by_def_id ( & mut self , def_id : DefId ) -> & MacroData {
174
- if self . macro_map . contains_key ( & def_id) {
175
- return & self . macro_map [ & def_id] ;
176
- }
177
-
178
- let loaded_macro = self . cstore ( ) . load_macro_untracked ( def_id, self . tcx ) ;
179
- let macro_data = match loaded_macro {
180
- LoadedMacro :: MacroDef { def, ident, attrs, span, edition } => {
181
- self . compile_macro ( & def, ident, & attrs, span, ast:: DUMMY_NODE_ID , edition)
173
+ pub ( crate ) fn get_macro_by_def_id ( & self , def_id : DefId ) -> Macro < ' ra > {
174
+ match def_id. as_local ( ) {
175
+ Some ( local_def_id) => {
176
+ // local macros are always compiled.
177
+ self . local_macro_map
178
+ . get ( & local_def_id)
179
+ . copied ( )
180
+ . expect ( "Local Macros should be compiled and available." )
182
181
}
183
- LoadedMacro :: ProcMacro ( ext) => MacroData :: new ( Arc :: new ( ext) ) ,
184
- } ;
182
+ None => * self . external_macro_map . borrow_mut ( ) . entry ( def_id) . or_insert_with ( || {
183
+ let loaded_macro = self . cstore ( ) . load_macro_untracked ( def_id, self . tcx ) ;
184
+ let macro_data = match loaded_macro {
185
+ LoadedMacro :: MacroDef { def, ident, attrs, span, edition } => {
186
+ self . compile_macro ( & def, ident, & attrs, span, ast:: DUMMY_NODE_ID , edition)
187
+ }
188
+ LoadedMacro :: ProcMacro ( ext) => MacroData :: new ( Arc :: new ( ext) ) ,
189
+ } ;
185
190
186
- self . macro_map . entry ( def_id) . or_insert ( macro_data)
191
+ self . arenas . alloc_macro ( macro_data)
192
+ } ) ,
193
+ }
187
194
}
188
195
189
196
pub ( crate ) fn build_reduced_graph (
@@ -1203,7 +1210,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
1203
1210
fn insert_unused_macro ( & mut self , ident : Ident , def_id : LocalDefId , node_id : NodeId ) {
1204
1211
if !ident. as_str ( ) . starts_with ( '_' ) {
1205
1212
self . r . unused_macros . insert ( def_id, ( node_id, ident) ) ;
1206
- let nrules = self . r . macro_map [ & def_id. to_def_id ( ) ] . nrules ;
1213
+ let nrules = self . r . local_macro_map [ & def_id] . nrules ;
1207
1214
self . r . unused_macro_rules . insert ( node_id, DenseBitSet :: new_filled ( nrules) ) ;
1208
1215
}
1209
1216
}
@@ -1222,7 +1229,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
1222
1229
Some ( ( macro_kind, ident, span) ) => {
1223
1230
let res = Res :: Def ( DefKind :: Macro ( macro_kind) , def_id. to_def_id ( ) ) ;
1224
1231
let macro_data = MacroData :: new ( self . r . dummy_ext ( macro_kind) ) ;
1225
- self . r . macro_map . insert ( def_id. to_def_id ( ) , macro_data) ;
1232
+ self . r . new_local_macro ( def_id, macro_data) ;
1226
1233
self . r . proc_macro_stubs . insert ( def_id) ;
1227
1234
( res, ident, span, false )
1228
1235
}
0 commit comments