@@ -10,9 +10,7 @@ use cfg::{CfgExpr, CfgOptions};
10
10
use either:: Either ;
11
11
use hir_expand:: {
12
12
attrs:: { Attr , AttrId } ,
13
- builtin_attr_macro:: find_builtin_attr,
14
- builtin_derive_macro:: find_builtin_derive,
15
- builtin_fn_macro:: find_builtin_macro,
13
+ builtin:: { find_builtin_attr, find_builtin_derive, find_builtin_macro} ,
16
14
name:: { AsName , Name } ,
17
15
proc_macro:: CustomProcMacroExpander ,
18
16
ExpandTo , HirFileId , InFile , MacroCallId , MacroCallKind , MacroDefId , MacroDefKind ,
@@ -76,34 +74,11 @@ pub(super) fn collect_defs(db: &dyn DefDatabase, def_map: DefMap, tree_id: TreeI
76
74
}
77
75
78
76
let proc_macros = if krate. is_proc_macro {
79
- match db. proc_macros ( ) . get ( & def_map. krate ) {
80
- Some ( Ok ( proc_macros) ) => Ok ( {
81
- let ctx = db. syntax_context ( tree_id. file_id ( ) ) ;
82
- proc_macros
83
- . iter ( )
84
- . enumerate ( )
85
- . map ( |( idx, it) | {
86
- let name = Name :: new_symbol ( it. name . clone ( ) , ctx) ;
87
- (
88
- name,
89
- if !db. expand_proc_attr_macros ( ) {
90
- CustomProcMacroExpander :: dummy ( )
91
- } else if it. disabled {
92
- CustomProcMacroExpander :: disabled ( )
93
- } else {
94
- CustomProcMacroExpander :: new (
95
- hir_expand:: proc_macro:: ProcMacroId :: new ( idx as u32 ) ,
96
- )
97
- } ,
98
- )
99
- } )
100
- . collect ( )
101
- } ) ,
102
- Some ( Err ( e) ) => Err ( e. clone ( ) . into_boxed_str ( ) ) ,
103
- None => Err ( "No proc-macros present for crate" . to_owned ( ) . into_boxed_str ( ) ) ,
104
- }
77
+ db. proc_macros ( )
78
+ . for_crate ( def_map. krate , db. syntax_context ( tree_id. file_id ( ) ) )
79
+ . unwrap_or_default ( )
105
80
} else {
106
- Ok ( vec ! [ ] )
81
+ Default :: default ( )
107
82
} ;
108
83
109
84
let mut collector = DefCollector {
@@ -252,10 +227,10 @@ struct DefCollector<'a> {
252
227
mod_dirs : FxHashMap < LocalModuleId , ModDir > ,
253
228
cfg_options : & ' a CfgOptions ,
254
229
/// List of procedural macros defined by this crate. This is read from the dynamic library
255
- /// built by the build system, and is the list of proc. macros we can actually expand. It is
256
- /// empty when proc. macro support is disabled (in which case we still do name resolution for
257
- /// them).
258
- proc_macros : Result < Vec < ( Name , CustomProcMacroExpander ) > , Box < str > > ,
230
+ /// built by the build system, and is the list of proc- macros we can actually expand. It is
231
+ /// empty when proc- macro support is disabled (in which case we still do name resolution for
232
+ /// them). The bool signals whether the proc-macro has been explicitly disabled for name-resolution.
233
+ proc_macros : Box < [ ( Name , CustomProcMacroExpander , bool ) ] > ,
259
234
is_proc_macro : bool ,
260
235
from_glob_import : PerNsGlobImports ,
261
236
/// If we fail to resolve an attribute on a `ModItem`, we fall back to ignoring the attribute.
@@ -278,10 +253,6 @@ impl DefCollector<'_> {
278
253
let attrs = item_tree. top_level_attrs ( self . db , self . def_map . krate ) ;
279
254
let crate_data = Arc :: get_mut ( & mut self . def_map . data ) . unwrap ( ) ;
280
255
281
- if let Err ( e) = & self . proc_macros {
282
- crate_data. proc_macro_loading_error = Some ( e. clone ( ) ) ;
283
- }
284
-
285
256
let mut process = true ;
286
257
287
258
// Process other crate-level attributes.
@@ -608,11 +579,17 @@ impl DefCollector<'_> {
608
579
fn_id : FunctionId ,
609
580
) {
610
581
let kind = def. kind . to_basedb_kind ( ) ;
611
- let ( expander, kind) =
612
- match self . proc_macros . as_ref ( ) . map ( |it| it. iter ( ) . find ( |( n, _) | n == & def. name ) ) {
613
- Ok ( Some ( & ( _, expander) ) ) => ( expander, kind) ,
614
- _ => ( CustomProcMacroExpander :: dummy ( ) , kind) ,
615
- } ;
582
+ let ( expander, kind) = match self . proc_macros . iter ( ) . find ( |( n, _, _) | n == & def. name ) {
583
+ Some ( _)
584
+ if kind == hir_expand:: proc_macro:: ProcMacroKind :: Attr
585
+ && !self . db . expand_proc_attr_macros ( ) =>
586
+ {
587
+ ( CustomProcMacroExpander :: disabled_proc_attr ( ) , kind)
588
+ }
589
+ Some ( & ( _, _, true ) ) => ( CustomProcMacroExpander :: disabled ( ) , kind) ,
590
+ Some ( & ( _, expander, false ) ) => ( expander, kind) ,
591
+ None => ( CustomProcMacroExpander :: missing_expander ( ) , kind) ,
592
+ } ;
616
593
617
594
let proc_macro_id = ProcMacroLoc {
618
595
container : self . def_map . crate_root ( ) ,
@@ -1415,25 +1392,23 @@ impl DefCollector<'_> {
1415
1392
return recollect_without ( self ) ;
1416
1393
}
1417
1394
1418
- let call_id = call_id ( ) ;
1419
1395
if let MacroDefKind :: ProcMacro ( _, exp, _) = def. kind {
1420
1396
// If there's no expander for the proc macro (e.g.
1421
1397
// because proc macros are disabled, or building the
1422
1398
// proc macro crate failed), report this and skip
1423
1399
// expansion like we would if it was disabled
1424
- if exp. is_dummy ( ) {
1425
- self . def_map . diagnostics . push ( DefDiagnostic :: unresolved_proc_macro (
1400
+ if let Some ( err ) = exp. as_expand_error ( def . krate ) {
1401
+ self . def_map . diagnostics . push ( DefDiagnostic :: macro_error (
1426
1402
directive. module_id ,
1427
- self . db . lookup_intern_macro_call ( call_id) . kind ,
1428
- def. krate ,
1403
+ ast_id,
1404
+ ( * * path) . clone ( ) ,
1405
+ err,
1429
1406
) ) ;
1430
1407
return recollect_without ( self ) ;
1431
1408
}
1432
- if exp. is_disabled ( ) {
1433
- return recollect_without ( self ) ;
1434
- }
1435
1409
}
1436
1410
1411
+ let call_id = call_id ( ) ;
1437
1412
self . def_map . modules [ directive. module_id ]
1438
1413
. scope
1439
1414
. add_attr_macro_invoc ( ast_id, call_id) ;
@@ -1472,7 +1447,6 @@ impl DefCollector<'_> {
1472
1447
}
1473
1448
let file_id = macro_call_id. as_file ( ) ;
1474
1449
1475
- // Then, fetch and process the item tree. This will reuse the expansion result from above.
1476
1450
let item_tree = self . db . file_item_tree ( file_id) ;
1477
1451
1478
1452
let mod_dir = if macro_call_id. as_macro_file ( ) . is_include_macro ( self . db . upcast ( ) ) {
@@ -2510,7 +2484,7 @@ mod tests {
2510
2484
unresolved_macros : Vec :: new ( ) ,
2511
2485
mod_dirs : FxHashMap :: default ( ) ,
2512
2486
cfg_options : & CfgOptions :: default ( ) ,
2513
- proc_macros : Ok ( vec ! [ ] ) ,
2487
+ proc_macros : Default :: default ( ) ,
2514
2488
from_glob_import : Default :: default ( ) ,
2515
2489
skip_attrs : Default :: default ( ) ,
2516
2490
is_proc_macro : false ,
0 commit comments