@@ -32,7 +32,7 @@ use crate::def_collector::collect_definitions;
3232use crate :: imports:: { ImportData , ImportKind } ;
3333use crate :: macros:: { MacroRulesBinding , MacroRulesScope , MacroRulesScopeRef } ;
3434use crate :: {
35- BindingKey , Determinacy , ExternPreludeEntry , Finalize , MacroData , Module , ModuleKind ,
35+ BindingKey , Determinacy , ExternPreludeEntry , Finalize , Macro , MacroData , Module , ModuleKind ,
3636 ModuleOrUniformRoot , NameBinding , NameBindingData , NameBindingKind , ParentScope , PathResult ,
3737 ResolutionError , Resolver , ResolverArenas , Segment , ToNameBinding , Used , VisResolutionError ,
3838 errors,
@@ -162,28 +162,35 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
162162 }
163163 }
164164
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 > > {
166166 match res {
167167 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 ) ,
169169 _ => None ,
170170 }
171171 }
172172
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." )
182181 }
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+ } ;
185190
186- self . macro_map . entry ( def_id) . or_insert ( macro_data)
191+ self . arenas . alloc_macro ( macro_data)
192+ } ) ,
193+ }
187194 }
188195
189196 pub ( crate ) fn build_reduced_graph (
@@ -1203,7 +1210,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
12031210 fn insert_unused_macro ( & mut self , ident : Ident , def_id : LocalDefId , node_id : NodeId ) {
12041211 if !ident. as_str ( ) . starts_with ( '_' ) {
12051212 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 ;
12071214 self . r . unused_macro_rules . insert ( node_id, DenseBitSet :: new_filled ( nrules) ) ;
12081215 }
12091216 }
@@ -1222,7 +1229,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
12221229 Some ( ( macro_kind, ident, span) ) => {
12231230 let res = Res :: Def ( DefKind :: Macro ( macro_kind) , def_id. to_def_id ( ) ) ;
12241231 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) ;
12261233 self . r . proc_macro_stubs . insert ( def_id) ;
12271234 ( res, ident, span, false )
12281235 }
0 commit comments