5
5
6
6
use base_db:: { CrateId , FileId , ProcMacroId } ;
7
7
use cfg:: CfgOptions ;
8
+ use hir_expand:: InFile ;
8
9
use hir_expand:: {
9
10
ast_id_map:: FileAstId ,
10
11
builtin_derive:: find_builtin_derive,
@@ -21,9 +22,7 @@ use crate::{
21
22
attr:: Attrs ,
22
23
db:: DefDatabase ,
23
24
item_scope:: { ImportType , PerNsGlobImports } ,
24
- item_tree:: {
25
- self , FileItemTreeId , ItemTree , ItemTreeId , MacroCall , Mod , ModItem , ModKind , StructDefKind ,
26
- } ,
25
+ item_tree:: { self , ItemTree , ItemTreeId , MacroCall , Mod , ModItem , ModKind , StructDefKind } ,
27
26
nameres:: {
28
27
diagnostics:: DefDiagnostic , mod_resolution:: ModDir , path_resolution:: ReachedFixedPoint ,
29
28
BuiltinShadowMode , CrateDefMap , ModuleData , ModuleOrigin , ResolveMode ,
@@ -111,6 +110,12 @@ impl PartialResolvedImport {
111
110
}
112
111
}
113
112
113
+ #[ derive( Clone , Debug , Eq , PartialEq ) ]
114
+ enum ImportSource {
115
+ Import ( ItemTreeId < item_tree:: Import > ) ,
116
+ ExternCrate ( ItemTreeId < item_tree:: ExternCrate > ) ,
117
+ }
118
+
114
119
#[ derive( Clone , Debug , Eq , PartialEq ) ]
115
120
struct Import {
116
121
pub path : ModPath ,
@@ -120,11 +125,12 @@ struct Import {
120
125
pub is_prelude : bool ,
121
126
pub is_extern_crate : bool ,
122
127
pub is_macro_use : bool ,
128
+ source : ImportSource ,
123
129
}
124
130
125
131
impl Import {
126
- fn from_use ( tree : & ItemTree , id : FileItemTreeId < item_tree:: Import > ) -> Self {
127
- let it = & tree[ id] ;
132
+ fn from_use ( tree : & ItemTree , id : ItemTreeId < item_tree:: Import > ) -> Self {
133
+ let it = & tree[ id. value ] ;
128
134
let visibility = & tree[ it. visibility ] ;
129
135
Self {
130
136
path : it. path . clone ( ) ,
@@ -134,11 +140,12 @@ impl Import {
134
140
is_prelude : it. is_prelude ,
135
141
is_extern_crate : false ,
136
142
is_macro_use : false ,
143
+ source : ImportSource :: Import ( id) ,
137
144
}
138
145
}
139
146
140
- fn from_extern_crate ( tree : & ItemTree , id : FileItemTreeId < item_tree:: ExternCrate > ) -> Self {
141
- let it = & tree[ id] ;
147
+ fn from_extern_crate ( tree : & ItemTree , id : ItemTreeId < item_tree:: ExternCrate > ) -> Self {
148
+ let it = & tree[ id. value ] ;
142
149
let visibility = & tree[ it. visibility ] ;
143
150
Self {
144
151
path : it. path . clone ( ) ,
@@ -148,6 +155,7 @@ impl Import {
148
155
is_prelude : false ,
149
156
is_extern_crate : true ,
150
157
is_macro_use : it. is_macro_use ,
158
+ source : ImportSource :: ExternCrate ( id) ,
151
159
}
152
160
}
153
161
}
@@ -245,9 +253,10 @@ impl DefCollector<'_> {
245
253
246
254
let unresolved_imports = std:: mem:: replace ( & mut self . unresolved_imports , Vec :: new ( ) ) ;
247
255
// show unresolved imports in completion, etc
248
- for directive in unresolved_imports {
249
- self . record_resolved_import ( & directive)
256
+ for directive in & unresolved_imports {
257
+ self . record_resolved_import ( directive)
250
258
}
259
+ self . unresolved_imports = unresolved_imports;
251
260
252
261
// Record proc-macros
253
262
self . collect_proc_macro ( ) ;
@@ -778,7 +787,29 @@ impl DefCollector<'_> {
778
787
. collect ( item_tree. top_level_items ( ) ) ;
779
788
}
780
789
781
- fn finish ( self ) -> CrateDefMap {
790
+ fn finish ( mut self ) -> CrateDefMap {
791
+ for directive in & self . unresolved_imports {
792
+ match directive. import . source {
793
+ ImportSource :: Import ( import) => {
794
+ let item_tree = self . db . item_tree ( import. file_id ) ;
795
+ let import_data = & item_tree[ import. value ] ;
796
+ self . def_map . diagnostics . push ( DefDiagnostic :: unresolved_import (
797
+ directive. module_id ,
798
+ InFile :: new ( import. file_id , import_data. ast_id ) ,
799
+ import_data. index ,
800
+ ) ) ;
801
+ }
802
+ ImportSource :: ExternCrate ( krate) => {
803
+ let item_tree = self . db . item_tree ( krate. file_id ) ;
804
+ let extern_crate = & item_tree[ krate. value ] ;
805
+ self . def_map . diagnostics . push ( DefDiagnostic :: unresolved_extern_crate (
806
+ directive. module_id ,
807
+ InFile :: new ( krate. file_id , extern_crate. ast_id ) ,
808
+ ) ) ;
809
+ }
810
+ }
811
+ }
812
+
782
813
self . def_map
783
814
}
784
815
}
@@ -834,14 +865,20 @@ impl ModCollector<'_, '_> {
834
865
ModItem :: Import ( import_id) => {
835
866
self . def_collector . unresolved_imports . push ( ImportDirective {
836
867
module_id : self . module_id ,
837
- import : Import :: from_use ( & self . item_tree , import_id) ,
868
+ import : Import :: from_use (
869
+ & self . item_tree ,
870
+ InFile :: new ( self . file_id , import_id) ,
871
+ ) ,
838
872
status : PartialResolvedImport :: Unresolved ,
839
873
} )
840
874
}
841
875
ModItem :: ExternCrate ( import_id) => {
842
876
self . def_collector . unresolved_imports . push ( ImportDirective {
843
877
module_id : self . module_id ,
844
- import : Import :: from_extern_crate ( & self . item_tree , import_id) ,
878
+ import : Import :: from_extern_crate (
879
+ & self . item_tree ,
880
+ InFile :: new ( self . file_id , import_id) ,
881
+ ) ,
845
882
status : PartialResolvedImport :: Unresolved ,
846
883
} )
847
884
}
0 commit comments