@@ -46,6 +46,7 @@ use hir_def::{
4646 item_tree:: ItemTreeNode ,
4747 lang_item:: { LangItem , LangItemTarget } ,
4848 layout:: ReprOptions ,
49+ macro_id_to_def_id,
4950 nameres:: { self , diagnostics:: DefDiagnostic , ModuleOrigin } ,
5051 per_ns:: PerNs ,
5152 resolver:: { HasResolver , Resolver } ,
@@ -86,12 +87,12 @@ pub use crate::{
8687 attrs:: { HasAttrs , Namespace } ,
8788 diagnostics:: {
8889 AnyDiagnostic , BreakOutsideOfLoop , ExpectedFunction , InactiveCode , IncoherentImpl ,
89- IncorrectCase , InvalidDeriveTarget , MacroError , MalformedDerive , MismatchedArgCount ,
90- MissingFields , MissingMatchArms , MissingUnsafe , NeedMut , NoSuchField , PrivateAssocItem ,
91- PrivateField , ReplaceFilterMapNextWithFindMap , TypeMismatch , UndeclaredLabel ,
92- UnimplementedBuiltinMacro , UnreachableLabel , UnresolvedExternCrate , UnresolvedField ,
93- UnresolvedImport , UnresolvedMacroCall , UnresolvedMethodCall , UnresolvedModule ,
94- UnresolvedProcMacro , UnusedMut ,
90+ IncorrectCase , InvalidDeriveTarget , MacroDefError , MacroError , MalformedDerive ,
91+ MismatchedArgCount , MissingFields , MissingMatchArms , MissingUnsafe , NeedMut , NoSuchField ,
92+ PrivateAssocItem , PrivateField , ReplaceFilterMapNextWithFindMap , TypeMismatch ,
93+ UndeclaredLabel , UnimplementedBuiltinMacro , UnreachableLabel , UnresolvedExternCrate ,
94+ UnresolvedField , UnresolvedImport , UnresolvedMacroCall , UnresolvedMethodCall ,
95+ UnresolvedModule , UnresolvedProcMacro , UnusedMut ,
9596 } ,
9697 has_source:: HasSource ,
9798 semantics:: { PathResolution , Semantics , SemanticsScope , TypeInfo , VisibleTraits } ,
@@ -563,6 +564,7 @@ impl Module {
563564 }
564565 emit_def_diagnostic ( db, acc, diag) ;
565566 }
567+
566568 for decl in self . declarations ( db) {
567569 match decl {
568570 ModuleDef :: Module ( m) => {
@@ -601,9 +603,11 @@ impl Module {
601603 }
602604 acc. extend ( decl. diagnostics ( db) )
603605 }
606+ ModuleDef :: Macro ( m) => emit_macro_def_diagnostics ( db, acc, m) ,
604607 _ => acc. extend ( decl. diagnostics ( db) ) ,
605608 }
606609 }
610+ self . legacy_macros ( db) . into_iter ( ) . for_each ( |m| emit_macro_def_diagnostics ( db, acc, m) ) ;
607611
608612 let inherent_impls = db. inherent_impls_in_crate ( self . id . krate ( ) ) ;
609613
@@ -685,8 +689,31 @@ impl Module {
685689 }
686690}
687691
692+ fn emit_macro_def_diagnostics ( db : & dyn HirDatabase , acc : & mut Vec < AnyDiagnostic > , m : Macro ) {
693+ let id = macro_id_to_def_id ( db. upcast ( ) , m. id ) ;
694+ if let Err ( e) = db. macro_def ( id) {
695+ let Some ( ast) = id. ast_id ( ) . left ( ) else {
696+ never ! ( "MacroDefError for proc-macro: {:?}" , e) ;
697+ return ;
698+ } ;
699+ emit_def_diagnostic_ (
700+ db,
701+ acc,
702+ & DefDiagnosticKind :: MacroDefError { ast, message : e. to_string ( ) } ,
703+ ) ;
704+ }
705+ }
706+
688707fn emit_def_diagnostic ( db : & dyn HirDatabase , acc : & mut Vec < AnyDiagnostic > , diag : & DefDiagnostic ) {
689- match & diag. kind {
708+ emit_def_diagnostic_ ( db, acc, & diag. kind )
709+ }
710+
711+ fn emit_def_diagnostic_ (
712+ db : & dyn HirDatabase ,
713+ acc : & mut Vec < AnyDiagnostic > ,
714+ diag : & DefDiagnosticKind ,
715+ ) {
716+ match diag {
690717 DefDiagnosticKind :: UnresolvedModule { ast : declaration, candidates } => {
691718 let decl = declaration. to_node ( db. upcast ( ) ) ;
692719 acc. push (
@@ -794,6 +821,17 @@ fn emit_def_diagnostic(db: &dyn HirDatabase, acc: &mut Vec<AnyDiagnostic>, diag:
794821 None => stdx:: never!( "derive diagnostic on item without derive attribute" ) ,
795822 }
796823 }
824+ DefDiagnosticKind :: MacroDefError { ast, message } => {
825+ let node = ast. to_node ( db. upcast ( ) ) ;
826+ acc. push (
827+ MacroDefError {
828+ node : InFile :: new ( ast. file_id , AstPtr :: new ( & node) ) ,
829+ name : node. name ( ) . map ( |it| it. syntax ( ) . text_range ( ) ) ,
830+ message : message. clone ( ) ,
831+ }
832+ . into ( ) ,
833+ ) ;
834+ }
797835 }
798836}
799837
0 commit comments