@@ -86,7 +86,7 @@ pub(super) fn collect_defs(db: &dyn DefDatabase, mut def_map: DefMap, tree_id: T
86
86
let proc_macros = if is_proc_macro {
87
87
match db. proc_macros ( ) . get ( & def_map. krate ) {
88
88
Some ( Ok ( proc_macros) ) => {
89
- proc_macros
89
+ Ok ( proc_macros
90
90
. iter ( )
91
91
. enumerate ( )
92
92
. map ( |( idx, it) | {
@@ -95,20 +95,13 @@ pub(super) fn collect_defs(db: &dyn DefDatabase, mut def_map: DefMap, tree_id: T
95
95
tt:: Ident { text : it. name . clone ( ) , span : tt:: TokenId :: unspecified ( ) } ;
96
96
( name. as_name ( ) , ProcMacroExpander :: new ( base_db:: ProcMacroId ( idx as u32 ) ) )
97
97
} )
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 ( ) )
108
99
}
100
+ Some ( Err ( e) ) => Err ( e. clone ( ) . into_boxed_str ( ) ) ,
101
+ None => Err ( "No proc-macros present for crate" . to_owned ( ) . into_boxed_str ( ) ) ,
109
102
}
110
103
} else {
111
- vec ! [ ]
104
+ Ok ( vec ! [ ] )
112
105
} ;
113
106
114
107
let mut collector = DefCollector {
@@ -263,7 +256,7 @@ struct DefCollector<'a> {
263
256
/// built by the build system, and is the list of proc. macros we can actually expand. It is
264
257
/// empty when proc. macro support is disabled (in which case we still do name resolution for
265
258
/// them).
266
- proc_macros : Vec < ( Name , ProcMacroExpander ) > ,
259
+ proc_macros : Result < Vec < ( Name , ProcMacroExpander ) > , Box < str > > ,
267
260
is_proc_macro : bool ,
268
261
from_glob_import : PerNsGlobImports ,
269
262
/// If we fail to resolve an attribute on a `ModItem`, we fall back to ignoring the attribute.
@@ -290,6 +283,11 @@ impl DefCollector<'_> {
290
283
let module_id = DefMap :: ROOT ;
291
284
292
285
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
+ }
293
291
294
292
// Process other crate-level attributes.
295
293
for attr in & * attrs {
@@ -306,7 +304,7 @@ impl DefCollector<'_> {
306
304
if * attr_name == hir_expand:: name![ recursion_limit] {
307
305
if let Some ( limit) = attr. string_value ( ) {
308
306
if let Ok ( limit) = limit. parse ( ) {
309
- self . def_map . data . recursion_limit = Some ( limit) ;
307
+ crate_data . recursion_limit = Some ( limit) ;
310
308
}
311
309
}
312
310
continue ;
@@ -320,17 +318,17 @@ impl DefCollector<'_> {
320
318
}
321
319
322
320
if * attr_name == hir_expand:: name![ no_core] {
323
- self . def_map . data . no_core = true ;
321
+ crate_data . no_core = true ;
324
322
continue ;
325
323
}
326
324
327
325
if * attr_name == hir_expand:: name![ no_std] {
328
- self . def_map . data . no_std = true ;
326
+ crate_data . no_std = true ;
329
327
continue ;
330
328
}
331
329
332
330
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 ;
334
332
continue ;
335
333
}
336
334
@@ -344,7 +342,7 @@ impl DefCollector<'_> {
344
342
[ name] => Some ( name. to_smol_str ( ) ) ,
345
343
_ => None ,
346
344
} ) ;
347
- self . def_map . data . unstable_features . extend ( features) ;
345
+ crate_data . unstable_features . extend ( features) ;
348
346
}
349
347
350
348
let attr_is_register_like = * attr_name == hir_expand:: name![ register_attr]
@@ -359,14 +357,15 @@ impl DefCollector<'_> {
359
357
} ;
360
358
361
359
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 ( ) ) ;
363
361
cov_mark:: hit!( register_attr) ;
364
362
} 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 ( ) ) ;
366
364
cov_mark:: hit!( register_tool) ;
367
365
}
368
366
}
369
367
368
+ crate_data. shrink_to_fit ( ) ;
370
369
self . inject_prelude ( ) ;
371
370
372
371
ModCollector {
@@ -598,24 +597,29 @@ impl DefCollector<'_> {
598
597
def : ProcMacroDef ,
599
598
id : ItemTreeId < item_tree:: Function > ,
600
599
fn_id : FunctionId ,
601
- module_id : ModuleId ,
602
600
) {
601
+ if self . def_map . block . is_some ( ) {
602
+ return ;
603
+ }
604
+ let crate_root = self . def_map . module_id ( DefMap :: ROOT ) ;
605
+
603
606
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
+ } ;
608
612
609
613
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 ) ;
611
615
self . define_proc_macro ( def. name . clone ( ) , proc_macro_id) ;
616
+ let crate_data = Arc :: get_mut ( & mut self . def_map . data ) . unwrap ( ) ;
612
617
if let ProcMacroKind :: CustomDerive { helpers } = def. kind {
613
- self . def_map
614
- . data
618
+ crate_data
615
619
. exported_derives
616
620
. insert ( macro_id_to_def_id ( self . db , proc_macro_id. into ( ) ) , helpers) ;
617
621
}
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) ;
619
623
}
620
624
621
625
/// Define a macro with `macro_rules`.
@@ -1639,12 +1643,10 @@ impl ModCollector<'_, '_> {
1639
1643
let vis = resolve_vis ( def_map, & self . item_tree [ it. visibility ] ) ;
1640
1644
if self . def_collector . is_proc_macro && self . module_id == DefMap :: ROOT {
1641
1645
if let Some ( proc_macro) = attrs. parse_proc_macro_decl ( & it. name ) {
1642
- let crate_root = def_map. module_id ( DefMap :: ROOT ) ;
1643
1646
self . def_collector . export_proc_macro (
1644
1647
proc_macro,
1645
1648
ItemTreeId :: new ( self . tree_id , id) ,
1646
1649
fn_id,
1647
- crate_root,
1648
1650
) ;
1649
1651
}
1650
1652
}
@@ -2165,11 +2167,12 @@ impl ModCollector<'_, '_> {
2165
2167
& self . item_tree [ mac. visibility ] ,
2166
2168
) ;
2167
2169
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
+ }
2173
2176
}
2174
2177
}
2175
2178
@@ -2277,7 +2280,7 @@ mod tests {
2277
2280
unresolved_macros : Vec :: new ( ) ,
2278
2281
mod_dirs : FxHashMap :: default ( ) ,
2279
2282
cfg_options : & CfgOptions :: default ( ) ,
2280
- proc_macros : Default :: default ( ) ,
2283
+ proc_macros : Ok ( vec ! [ ] ) ,
2281
2284
from_glob_import : Default :: default ( ) ,
2282
2285
skip_attrs : Default :: default ( ) ,
2283
2286
is_proc_macro : false ,
0 commit comments