@@ -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 ( ) ,
@@ -1338,25 +1315,22 @@ impl DefCollector<'_> {
13381315 return recollect_without ( self ) ;
13391316 }
13401317
1341- let call_id = call_id ( ) ;
13421318 if let MacroDefKind :: ProcMacro ( _, exp, _) = def. kind {
13431319 // If there's no expander for the proc macro (e.g.
13441320 // because proc macros are disabled, or building the
13451321 // proc macro crate failed), report this and skip
13461322 // expansion like we would if it was disabled
1347- if exp. is_dummy ( ) {
1348- self . def_map . diagnostics . push ( DefDiagnostic :: unresolved_proc_macro (
1323+ if let Some ( err ) = exp. as_expand_error ( def . krate ) {
1324+ self . def_map . diagnostics . push ( DefDiagnostic :: macro_error (
13491325 directive. module_id ,
1350- self . db . lookup_intern_macro_call ( call_id ) . kind ,
1351- def . krate ,
1326+ ast_id ,
1327+ err ,
13521328 ) ) ;
13531329 return recollect_without ( self ) ;
13541330 }
1355- if exp. is_disabled ( ) {
1356- return recollect_without ( self ) ;
1357- }
13581331 }
13591332
1333+ let call_id = call_id ( ) ;
13601334 self . def_map . modules [ directive. module_id ]
13611335 . scope
13621336 . add_attr_macro_invoc ( ast_id, call_id) ;
@@ -1395,7 +1369,6 @@ impl DefCollector<'_> {
13951369 }
13961370 let file_id = macro_call_id. as_file ( ) ;
13971371
1398- // Then, fetch and process the item tree. This will reuse the expansion result from above.
13991372 let item_tree = self . db . file_item_tree ( file_id) ;
14001373
14011374 let mod_dir = if macro_call_id. as_macro_file ( ) . is_include_macro ( self . db . upcast ( ) ) {
@@ -2433,7 +2406,7 @@ mod tests {
24332406 unresolved_macros : Vec :: new ( ) ,
24342407 mod_dirs : FxHashMap :: default ( ) ,
24352408 cfg_options : & CfgOptions :: default ( ) ,
2436- proc_macros : Ok ( vec ! [ ] ) ,
2409+ proc_macros : Default :: default ( ) ,
24372410 from_glob_import : Default :: default ( ) ,
24382411 skip_attrs : Default :: default ( ) ,
24392412 is_proc_macro : false ,
0 commit comments