@@ -53,13 +53,12 @@ impl ImportMap {
53
53
pub ( crate ) fn import_map_query ( db : & dyn DefDatabase , krate : CrateId ) -> Arc < Self > {
54
54
let _p = profile:: span ( "import_map_query" ) ;
55
55
56
- let mut import_map = collect_import_map ( db, krate) ;
56
+ let map = collect_import_map ( db, krate) ;
57
57
58
- let mut importables: Vec < _ > = import_map
59
- . map
58
+ let mut importables: Vec < _ > = map
60
59
. iter ( )
61
60
// We've only collected items, whose name cannot be tuple field.
62
- . map ( |( item, info) | ( item, info. name . as_str ( ) . unwrap ( ) . to_ascii_lowercase ( ) ) )
61
+ . map ( |( & item, info) | ( item, info. name . as_str ( ) . unwrap ( ) . to_ascii_lowercase ( ) ) )
63
62
. collect ( ) ;
64
63
importables. sort_by ( |( _, lhs_name) , ( _, rhs_name) | lhs_name. cmp ( rhs_name) ) ;
65
64
@@ -70,61 +69,30 @@ impl ImportMap {
70
69
let _ = builder. insert ( name, start_idx as u64 ) ;
71
70
}
72
71
73
- import_map. fst = builder. into_map ( ) ;
74
- import_map. importables = importables. into_iter ( ) . map ( |( & item, _) | item) . collect ( ) ;
75
-
76
- Arc :: new ( import_map)
72
+ Arc :: new ( ImportMap {
73
+ map,
74
+ fst : builder. into_map ( ) ,
75
+ importables : importables. into_iter ( ) . map ( |( item, _) | item) . collect ( ) ,
76
+ } )
77
77
}
78
78
79
79
pub fn import_info_for ( & self , item : ItemInNs ) -> Option < & ImportInfo > {
80
80
self . map . get ( & item)
81
81
}
82
-
83
- fn collect_trait_assoc_items (
84
- & mut self ,
85
- db : & dyn DefDatabase ,
86
- tr : TraitId ,
87
- is_type_in_ns : bool ,
88
- trait_import_info : & ImportInfo ,
89
- ) {
90
- let _p = profile:: span ( "collect_trait_assoc_items" ) ;
91
- for ( assoc_item_name, item) in & db. trait_data ( tr) . items {
92
- let module_def_id = match item {
93
- AssocItemId :: FunctionId ( f) => ModuleDefId :: from ( * f) ,
94
- AssocItemId :: ConstId ( c) => ModuleDefId :: from ( * c) ,
95
- // cannot use associated type aliases directly: need a `<Struct as Trait>::TypeAlias`
96
- // qualifier, ergo no need to store it for imports in import_map
97
- AssocItemId :: TypeAliasId ( _) => {
98
- cov_mark:: hit!( type_aliases_ignored) ;
99
- continue ;
100
- }
101
- } ;
102
- let assoc_item = if is_type_in_ns {
103
- ItemInNs :: Types ( module_def_id)
104
- } else {
105
- ItemInNs :: Values ( module_def_id)
106
- } ;
107
-
108
- let assoc_item_info = ImportInfo {
109
- container : trait_import_info. container ,
110
- name : assoc_item_name. clone ( ) ,
111
- is_trait_assoc_item : true ,
112
- } ;
113
- self . map . insert ( assoc_item, assoc_item_info) ;
114
- }
115
- }
116
82
}
117
83
118
- fn collect_import_map ( db : & dyn DefDatabase , krate : CrateId ) -> ImportMap {
84
+ fn collect_import_map ( db : & dyn DefDatabase , krate : CrateId ) -> FxIndexMap < ItemInNs , ImportInfo > {
119
85
let _p = profile:: span ( "collect_import_map" ) ;
120
86
121
87
let def_map = db. crate_def_map ( krate) ;
122
- let mut import_map = ImportMap :: default ( ) ;
88
+ let mut map = FxIndexMap :: default ( ) ;
123
89
124
90
// We look only into modules that are public(ly reexported), starting with the crate root.
125
91
let root = def_map. module_id ( DefMap :: ROOT ) ;
126
92
let mut worklist = vec ! [ ( root, 0 ) ] ;
93
+ // Records items' minimum module depth.
127
94
let mut depth_map = FxHashMap :: default ( ) ;
95
+
128
96
while let Some ( ( module, depth) ) = worklist. pop ( ) {
129
97
let ext_def_map;
130
98
let mod_data = if module. krate == krate {
@@ -166,15 +134,16 @@ fn collect_import_map(db: &dyn DefDatabase, krate: CrateId) -> ImportMap {
166
134
}
167
135
168
136
if let Some ( ModuleDefId :: TraitId ( tr) ) = item. as_module_def_id ( ) {
169
- import_map . collect_trait_assoc_items (
137
+ collect_trait_assoc_items (
170
138
db,
139
+ & mut map,
171
140
tr,
172
141
matches ! ( item, ItemInNs :: Types ( _) ) ,
173
142
& import_info,
174
143
) ;
175
144
}
176
145
177
- import_map . map . insert ( item, import_info) ;
146
+ map. insert ( item, import_info) ;
178
147
179
148
// If we've just added a module, descend into it. We might traverse modules
180
149
// multiple times, but only if the module depth is smaller (else we `continue`
@@ -186,7 +155,41 @@ fn collect_import_map(db: &dyn DefDatabase, krate: CrateId) -> ImportMap {
186
155
}
187
156
}
188
157
189
- import_map
158
+ map
159
+ }
160
+
161
+ fn collect_trait_assoc_items (
162
+ db : & dyn DefDatabase ,
163
+ map : & mut FxIndexMap < ItemInNs , ImportInfo > ,
164
+ tr : TraitId ,
165
+ is_type_in_ns : bool ,
166
+ trait_import_info : & ImportInfo ,
167
+ ) {
168
+ let _p = profile:: span ( "collect_trait_assoc_items" ) ;
169
+ for ( assoc_item_name, item) in & db. trait_data ( tr) . items {
170
+ let module_def_id = match item {
171
+ AssocItemId :: FunctionId ( f) => ModuleDefId :: from ( * f) ,
172
+ AssocItemId :: ConstId ( c) => ModuleDefId :: from ( * c) ,
173
+ // cannot use associated type aliases directly: need a `<Struct as Trait>::TypeAlias`
174
+ // qualifier, ergo no need to store it for imports in import_map
175
+ AssocItemId :: TypeAliasId ( _) => {
176
+ cov_mark:: hit!( type_aliases_ignored) ;
177
+ continue ;
178
+ }
179
+ } ;
180
+ let assoc_item = if is_type_in_ns {
181
+ ItemInNs :: Types ( module_def_id)
182
+ } else {
183
+ ItemInNs :: Values ( module_def_id)
184
+ } ;
185
+
186
+ let assoc_item_info = ImportInfo {
187
+ container : trait_import_info. container ,
188
+ name : assoc_item_name. clone ( ) ,
189
+ is_trait_assoc_item : true ,
190
+ } ;
191
+ map. insert ( assoc_item, assoc_item_info) ;
192
+ }
190
193
}
191
194
192
195
impl PartialEq for ImportMap {
0 commit comments