@@ -10,9 +10,7 @@ use cfg::{CfgExpr, CfgOptions};
1010use either:: Either ;
1111use hir_expand:: {
1212 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} ,
1614 name:: { AsName , Name } ,
1715 proc_macro:: CustomProcMacroExpander ,
1816 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
7674 }
7775
7876 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 ( )
10580 } else {
106- Ok ( vec ! [ ] )
81+ Default :: default ( )
10782 } ;
10883
10984 let mut collector = DefCollector {
@@ -252,10 +227,10 @@ struct DefCollector<'a> {
252227 mod_dirs : FxHashMap < LocalModuleId , ModDir > ,
253228 cfg_options : & ' a CfgOptions ,
254229 /// 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 ) ] > ,
259234 is_proc_macro : bool ,
260235 from_glob_import : PerNsGlobImports ,
261236 /// If we fail to resolve an attribute on a `ModItem`, we fall back to ignoring the attribute.
@@ -278,10 +253,6 @@ impl DefCollector<'_> {
278253 let attrs = item_tree. top_level_attrs ( self . db , self . def_map . krate ) ;
279254 let crate_data = Arc :: get_mut ( & mut self . def_map . data ) . unwrap ( ) ;
280255
281- if let Err ( e) = & self . proc_macros {
282- crate_data. proc_macro_loading_error = Some ( e. clone ( ) ) ;
283- }
284-
285256 let mut process = true ;
286257
287258 // Process other crate-level attributes.
@@ -608,11 +579,17 @@ impl DefCollector<'_> {
608579 fn_id : FunctionId ,
609580 ) {
610581 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+ } ;
616593
617594 let proc_macro_id = ProcMacroLoc {
618595 container : self . def_map . crate_root ( ) ,
@@ -1415,25 +1392,23 @@ impl DefCollector<'_> {
14151392 return recollect_without ( self ) ;
14161393 }
14171394
1418- let call_id = call_id ( ) ;
14191395 if let MacroDefKind :: ProcMacro ( _, exp, _) = def. kind {
14201396 // If there's no expander for the proc macro (e.g.
14211397 // because proc macros are disabled, or building the
14221398 // proc macro crate failed), report this and skip
14231399 // 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 (
14261402 directive. module_id ,
1427- self . db . lookup_intern_macro_call ( call_id) . kind ,
1428- def. krate ,
1403+ ast_id,
1404+ ( * * path) . clone ( ) ,
1405+ err,
14291406 ) ) ;
14301407 return recollect_without ( self ) ;
14311408 }
1432- if exp. is_disabled ( ) {
1433- return recollect_without ( self ) ;
1434- }
14351409 }
14361410
1411+ let call_id = call_id ( ) ;
14371412 self . def_map . modules [ directive. module_id ]
14381413 . scope
14391414 . add_attr_macro_invoc ( ast_id, call_id) ;
@@ -1472,7 +1447,6 @@ impl DefCollector<'_> {
14721447 }
14731448 let file_id = macro_call_id. as_file ( ) ;
14741449
1475- // Then, fetch and process the item tree. This will reuse the expansion result from above.
14761450 let item_tree = self . db . file_item_tree ( file_id) ;
14771451
14781452 let mod_dir = if macro_call_id. as_macro_file ( ) . is_include_macro ( self . db . upcast ( ) ) {
@@ -2510,7 +2484,7 @@ mod tests {
25102484 unresolved_macros : Vec :: new ( ) ,
25112485 mod_dirs : FxHashMap :: default ( ) ,
25122486 cfg_options : & CfgOptions :: default ( ) ,
2513- proc_macros : Ok ( vec ! [ ] ) ,
2487+ proc_macros : Default :: default ( ) ,
25142488 from_glob_import : Default :: default ( ) ,
25152489 skip_attrs : Default :: default ( ) ,
25162490 is_proc_macro : false ,
0 commit comments