Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 5db0e1a

Browse files
committed
Restructure ImportMap collection
1 parent 2b10664 commit 5db0e1a

File tree

1 file changed

+50
-47
lines changed

1 file changed

+50
-47
lines changed

crates/hir-def/src/import_map.rs

Lines changed: 50 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,12 @@ impl ImportMap {
5353
pub(crate) fn import_map_query(db: &dyn DefDatabase, krate: CrateId) -> Arc<Self> {
5454
let _p = profile::span("import_map_query");
5555

56-
let mut import_map = collect_import_map(db, krate);
56+
let map = collect_import_map(db, krate);
5757

58-
let mut importables: Vec<_> = import_map
59-
.map
58+
let mut importables: Vec<_> = map
6059
.iter()
6160
// 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()))
6362
.collect();
6463
importables.sort_by(|(_, lhs_name), (_, rhs_name)| lhs_name.cmp(rhs_name));
6564

@@ -70,61 +69,30 @@ impl ImportMap {
7069
let _ = builder.insert(name, start_idx as u64);
7170
}
7271

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+
})
7777
}
7878

7979
pub fn import_info_for(&self, item: ItemInNs) -> Option<&ImportInfo> {
8080
self.map.get(&item)
8181
}
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-
}
11682
}
11783

118-
fn collect_import_map(db: &dyn DefDatabase, krate: CrateId) -> ImportMap {
84+
fn collect_import_map(db: &dyn DefDatabase, krate: CrateId) -> FxIndexMap<ItemInNs, ImportInfo> {
11985
let _p = profile::span("collect_import_map");
12086

12187
let def_map = db.crate_def_map(krate);
122-
let mut import_map = ImportMap::default();
88+
let mut map = FxIndexMap::default();
12389

12490
// We look only into modules that are public(ly reexported), starting with the crate root.
12591
let root = def_map.module_id(DefMap::ROOT);
12692
let mut worklist = vec![(root, 0)];
93+
// Records items' minimum module depth.
12794
let mut depth_map = FxHashMap::default();
95+
12896
while let Some((module, depth)) = worklist.pop() {
12997
let ext_def_map;
13098
let mod_data = if module.krate == krate {
@@ -166,15 +134,16 @@ fn collect_import_map(db: &dyn DefDatabase, krate: CrateId) -> ImportMap {
166134
}
167135

168136
if let Some(ModuleDefId::TraitId(tr)) = item.as_module_def_id() {
169-
import_map.collect_trait_assoc_items(
137+
collect_trait_assoc_items(
170138
db,
139+
&mut map,
171140
tr,
172141
matches!(item, ItemInNs::Types(_)),
173142
&import_info,
174143
);
175144
}
176145

177-
import_map.map.insert(item, import_info);
146+
map.insert(item, import_info);
178147

179148
// If we've just added a module, descend into it. We might traverse modules
180149
// 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 {
186155
}
187156
}
188157

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+
}
190193
}
191194

192195
impl PartialEq for ImportMap {

0 commit comments

Comments
 (0)