@@ -46,6 +46,7 @@ use hir_def::{
46
46
item_tree:: ItemTreeNode ,
47
47
lang_item:: { LangItem , LangItemTarget } ,
48
48
layout:: ReprOptions ,
49
+ macro_id_to_def_id,
49
50
nameres:: { self , diagnostics:: DefDiagnostic , ModuleOrigin } ,
50
51
per_ns:: PerNs ,
51
52
resolver:: { HasResolver , Resolver } ,
@@ -86,12 +87,12 @@ pub use crate::{
86
87
attrs:: { HasAttrs , Namespace } ,
87
88
diagnostics:: {
88
89
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 ,
95
96
} ,
96
97
has_source:: HasSource ,
97
98
semantics:: { PathResolution , Semantics , SemanticsScope , TypeInfo , VisibleTraits } ,
@@ -563,6 +564,7 @@ impl Module {
563
564
}
564
565
emit_def_diagnostic ( db, acc, diag) ;
565
566
}
567
+
566
568
for decl in self . declarations ( db) {
567
569
match decl {
568
570
ModuleDef :: Module ( m) => {
@@ -601,9 +603,11 @@ impl Module {
601
603
}
602
604
acc. extend ( decl. diagnostics ( db) )
603
605
}
606
+ ModuleDef :: Macro ( m) => emit_macro_def_diagnostics ( db, acc, m) ,
604
607
_ => acc. extend ( decl. diagnostics ( db) ) ,
605
608
}
606
609
}
610
+ self . legacy_macros ( db) . into_iter ( ) . for_each ( |m| emit_macro_def_diagnostics ( db, acc, m) ) ;
607
611
608
612
let inherent_impls = db. inherent_impls_in_crate ( self . id . krate ( ) ) ;
609
613
@@ -685,8 +689,31 @@ impl Module {
685
689
}
686
690
}
687
691
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
+
688
707
fn 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 {
690
717
DefDiagnosticKind :: UnresolvedModule { ast : declaration, candidates } => {
691
718
let decl = declaration. to_node ( db. upcast ( ) ) ;
692
719
acc. push (
@@ -794,6 +821,17 @@ fn emit_def_diagnostic(db: &dyn HirDatabase, acc: &mut Vec<AnyDiagnostic>, diag:
794
821
None => stdx:: never!( "derive diagnostic on item without derive attribute" ) ,
795
822
}
796
823
}
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
+ }
797
835
}
798
836
}
799
837
0 commit comments