@@ -86,7 +86,7 @@ pub(super) fn collect_defs(db: &dyn DefDatabase, mut def_map: DefMap, tree_id: T
8686 let proc_macros = if is_proc_macro {
8787 match db. proc_macros ( ) . get ( & def_map. krate ) {
8888 Some ( Ok ( proc_macros) ) => {
89- proc_macros
89+ Ok ( proc_macros
9090 . iter ( )
9191 . enumerate ( )
9292 . map ( |( idx, it) | {
@@ -95,20 +95,13 @@ pub(super) fn collect_defs(db: &dyn DefDatabase, mut def_map: DefMap, tree_id: T
9595 tt:: Ident { text : it. name . clone ( ) , span : tt:: TokenId :: unspecified ( ) } ;
9696 ( name. as_name ( ) , ProcMacroExpander :: new ( base_db:: ProcMacroId ( idx as u32 ) ) )
9797 } )
98- . collect ( )
99- }
100- Some ( Err ( e) ) => {
101- def_map. data . proc_macro_loading_error = Some ( e. clone ( ) . into_boxed_str ( ) ) ;
102- Vec :: new ( )
103- }
104- None => {
105- def_map. data . proc_macro_loading_error =
106- Some ( "No proc-macros present for crate" . to_owned ( ) . into_boxed_str ( ) ) ;
107- Vec :: new ( )
98+ . collect ( ) )
10899 }
100+ Some ( Err ( e) ) => Err ( e. clone ( ) . into_boxed_str ( ) ) ,
101+ None => Err ( "No proc-macros present for crate" . to_owned ( ) . into_boxed_str ( ) ) ,
109102 }
110103 } else {
111- vec ! [ ]
104+ Ok ( vec ! [ ] )
112105 } ;
113106
114107 let mut collector = DefCollector {
@@ -263,7 +256,7 @@ struct DefCollector<'a> {
263256 /// built by the build system, and is the list of proc. macros we can actually expand. It is
264257 /// empty when proc. macro support is disabled (in which case we still do name resolution for
265258 /// them).
266- proc_macros : Vec < ( Name , ProcMacroExpander ) > ,
259+ proc_macros : Result < Vec < ( Name , ProcMacroExpander ) > , Box < str > > ,
267260 is_proc_macro : bool ,
268261 from_glob_import : PerNsGlobImports ,
269262 /// If we fail to resolve an attribute on a `ModItem`, we fall back to ignoring the attribute.
@@ -290,6 +283,11 @@ impl DefCollector<'_> {
290283 let module_id = DefMap :: ROOT ;
291284
292285 let attrs = item_tree. top_level_attrs ( self . db , self . def_map . krate ) ;
286+ let crate_data = Arc :: get_mut ( & mut self . def_map . data ) . unwrap ( ) ;
287+
288+ if let Err ( e) = & self . proc_macros {
289+ crate_data. proc_macro_loading_error = Some ( e. clone ( ) ) ;
290+ }
293291
294292 // Process other crate-level attributes.
295293 for attr in & * attrs {
@@ -306,7 +304,7 @@ impl DefCollector<'_> {
306304 if * attr_name == hir_expand:: name![ recursion_limit] {
307305 if let Some ( limit) = attr. string_value ( ) {
308306 if let Ok ( limit) = limit. parse ( ) {
309- self . def_map . data . recursion_limit = Some ( limit) ;
307+ crate_data . recursion_limit = Some ( limit) ;
310308 }
311309 }
312310 continue ;
@@ -320,17 +318,17 @@ impl DefCollector<'_> {
320318 }
321319
322320 if * attr_name == hir_expand:: name![ no_core] {
323- self . def_map . data . no_core = true ;
321+ crate_data . no_core = true ;
324322 continue ;
325323 }
326324
327325 if * attr_name == hir_expand:: name![ no_std] {
328- self . def_map . data . no_std = true ;
326+ crate_data . no_std = true ;
329327 continue ;
330328 }
331329
332330 if attr_name. as_text ( ) . as_deref ( ) == Some ( "rustc_coherence_is_core" ) {
333- self . def_map . data . rustc_coherence_is_core = true ;
331+ crate_data . rustc_coherence_is_core = true ;
334332 continue ;
335333 }
336334
@@ -344,7 +342,7 @@ impl DefCollector<'_> {
344342 [ name] => Some ( name. to_smol_str ( ) ) ,
345343 _ => None ,
346344 } ) ;
347- self . def_map . data . unstable_features . extend ( features) ;
345+ crate_data . unstable_features . extend ( features) ;
348346 }
349347
350348 let attr_is_register_like = * attr_name == hir_expand:: name![ register_attr]
@@ -359,14 +357,15 @@ impl DefCollector<'_> {
359357 } ;
360358
361359 if * attr_name == hir_expand:: name![ register_attr] {
362- self . def_map . data . registered_attrs . push ( registered_name. to_smol_str ( ) ) ;
360+ crate_data . registered_attrs . push ( registered_name. to_smol_str ( ) ) ;
363361 cov_mark:: hit!( register_attr) ;
364362 } else {
365- self . def_map . data . registered_tools . push ( registered_name. to_smol_str ( ) ) ;
363+ crate_data . registered_tools . push ( registered_name. to_smol_str ( ) ) ;
366364 cov_mark:: hit!( register_tool) ;
367365 }
368366 }
369367
368+ crate_data. shrink_to_fit ( ) ;
370369 self . inject_prelude ( ) ;
371370
372371 ModCollector {
@@ -598,24 +597,29 @@ impl DefCollector<'_> {
598597 def : ProcMacroDef ,
599598 id : ItemTreeId < item_tree:: Function > ,
600599 fn_id : FunctionId ,
601- module_id : ModuleId ,
602600 ) {
601+ if self . def_map . block . is_some ( ) {
602+ return ;
603+ }
604+ let crate_root = self . def_map . module_id ( DefMap :: ROOT ) ;
605+
603606 let kind = def. kind . to_basedb_kind ( ) ;
604- let ( expander, kind) = match self . proc_macros . iter ( ) . find ( |( n, _) | n == & def. name ) {
605- Some ( & ( _, expander) ) => ( expander, kind) ,
606- None => ( ProcMacroExpander :: dummy ( ) , kind) ,
607- } ;
607+ let ( expander, kind) =
608+ match self . proc_macros . as_ref ( ) . map ( |it| it. iter ( ) . find ( |( n, _) | n == & def. name ) ) {
609+ Ok ( Some ( & ( _, expander) ) ) => ( expander, kind) ,
610+ _ => ( ProcMacroExpander :: dummy ( ) , kind) ,
611+ } ;
608612
609613 let proc_macro_id =
610- ProcMacroLoc { container : module_id , id, expander, kind } . intern ( self . db ) ;
614+ ProcMacroLoc { container : crate_root , id, expander, kind } . intern ( self . db ) ;
611615 self . define_proc_macro ( def. name . clone ( ) , proc_macro_id) ;
616+ let crate_data = Arc :: get_mut ( & mut self . def_map . data ) . unwrap ( ) ;
612617 if let ProcMacroKind :: CustomDerive { helpers } = def. kind {
613- self . def_map
614- . data
618+ crate_data
615619 . exported_derives
616620 . insert ( macro_id_to_def_id ( self . db , proc_macro_id. into ( ) ) , helpers) ;
617621 }
618- self . def_map . data . fn_proc_macro_mapping . insert ( fn_id, proc_macro_id) ;
622+ crate_data . fn_proc_macro_mapping . insert ( fn_id, proc_macro_id) ;
619623 }
620624
621625 /// Define a macro with `macro_rules`.
@@ -1639,12 +1643,10 @@ impl ModCollector<'_, '_> {
16391643 let vis = resolve_vis ( def_map, & self . item_tree [ it. visibility ] ) ;
16401644 if self . def_collector . is_proc_macro && self . module_id == DefMap :: ROOT {
16411645 if let Some ( proc_macro) = attrs. parse_proc_macro_decl ( & it. name ) {
1642- let crate_root = def_map. module_id ( DefMap :: ROOT ) ;
16431646 self . def_collector . export_proc_macro (
16441647 proc_macro,
16451648 ItemTreeId :: new ( self . tree_id , id) ,
16461649 fn_id,
1647- crate_root,
16481650 ) ;
16491651 }
16501652 }
@@ -2165,11 +2167,12 @@ impl ModCollector<'_, '_> {
21652167 & self . item_tree [ mac. visibility ] ,
21662168 ) ;
21672169 if let Some ( helpers) = helpers_opt {
2168- self . def_collector
2169- . def_map
2170- . data
2171- . exported_derives
2172- . insert ( macro_id_to_def_id ( self . def_collector . db , macro_id. into ( ) ) , helpers) ;
2170+ if self . def_collector . def_map . block . is_none ( ) {
2171+ Arc :: get_mut ( & mut self . def_collector . def_map . data )
2172+ . unwrap ( )
2173+ . exported_derives
2174+ . insert ( macro_id_to_def_id ( self . def_collector . db , macro_id. into ( ) ) , helpers) ;
2175+ }
21732176 }
21742177 }
21752178
@@ -2277,7 +2280,7 @@ mod tests {
22772280 unresolved_macros : Vec :: new ( ) ,
22782281 mod_dirs : FxHashMap :: default ( ) ,
22792282 cfg_options : & CfgOptions :: default ( ) ,
2280- proc_macros : Default :: default ( ) ,
2283+ proc_macros : Ok ( vec ! [ ] ) ,
22812284 from_glob_import : Default :: default ( ) ,
22822285 skip_attrs : Default :: default ( ) ,
22832286 is_proc_macro : false ,
0 commit comments