aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKirill Bulatov <[email protected]>2021-01-03 10:24:50 +0000
committerKirill Bulatov <[email protected]>2021-01-04 15:44:27 +0000
commit8721574a85399cd74319e93c84dc19c7ed7c6605 (patch)
tree83fb39e8c97fb524e6053937ca2b9dbc69ad053f
parentd27dea86b7dd4123f75ad176037b3c754eddfa65 (diff)
Simplify
-rw-r--r--crates/hir_def/src/import_map.rs49
-rw-r--r--crates/hir_def/src/item_scope.rs15
2 files changed, 37 insertions, 27 deletions
diff --git a/crates/hir_def/src/import_map.rs b/crates/hir_def/src/import_map.rs
index 0ba8198b5..a8bffe7ce 100644
--- a/crates/hir_def/src/import_map.rs
+++ b/crates/hir_def/src/import_map.rs
@@ -24,7 +24,7 @@ pub struct ImportInfo {
24 /// The module containing this item. 24 /// The module containing this item.
25 pub container: ModuleId, 25 pub container: ModuleId,
26 /// Whether the import is a trait associated item or not. 26 /// Whether the import is a trait associated item or not.
27 pub is_assoc_item: bool, 27 pub is_trait_assoc_item: bool,
28} 28}
29 29
30#[derive(Debug, Clone, Eq, PartialEq)] 30#[derive(Debug, Clone, Eq, PartialEq)]
@@ -105,11 +105,16 @@ impl ImportMap {
105 for item in per_ns.iter_items() { 105 for item in per_ns.iter_items() {
106 let path = mk_path(); 106 let path = mk_path();
107 let path_len = path.len(); 107 let path_len = path.len();
108 let import_info = ImportInfo { path, container: module, is_assoc_item: false }; 108 let import_info =
109 ImportInfo { path, container: module, is_trait_assoc_item: false };
109 110
110 // If we've added a path to a trait, add the trait's associated items to the assoc map.
111 if let Some(ModuleDefId::TraitId(tr)) = item.as_module_def_id() { 111 if let Some(ModuleDefId::TraitId(tr)) = item.as_module_def_id() {
112 import_map.collect_trait_assoc_items(db, tr, &import_info); 112 import_map.collect_trait_assoc_items(
113 db,
114 tr,
115 matches!(item, ItemInNs::Types(_)),
116 &import_info,
117 );
113 } 118 }
114 119
115 match import_map.map.entry(item) { 120 match import_map.map.entry(item) {
@@ -177,17 +182,24 @@ impl ImportMap {
177 &mut self, 182 &mut self,
178 db: &dyn DefDatabase, 183 db: &dyn DefDatabase,
179 tr: TraitId, 184 tr: TraitId,
180 import_info: &ImportInfo, 185 is_type_in_ns: bool,
186 original_import_info: &ImportInfo,
181 ) { 187 ) {
182 for (assoc_item_name, item) in db.trait_data(tr).items.iter() { 188 for (assoc_item_name, item) in &db.trait_data(tr).items {
183 let assoc_item = ItemInNs::Types(match item.clone() { 189 let module_def_id = match *item {
184 AssocItemId::FunctionId(f) => f.into(), 190 AssocItemId::FunctionId(f) => f.into(),
185 AssocItemId::ConstId(c) => c.into(), 191 AssocItemId::ConstId(c) => c.into(),
186 AssocItemId::TypeAliasId(t) => t.into(), 192 AssocItemId::TypeAliasId(t) => t.into(),
187 }); 193 };
188 let mut assoc_item_info = import_info.to_owned(); 194 let assoc_item = if is_type_in_ns {
195 ItemInNs::Types(module_def_id)
196 } else {
197 ItemInNs::Values(module_def_id)
198 };
199
200 let mut assoc_item_info = original_import_info.to_owned();
189 assoc_item_info.path.segments.push(assoc_item_name.to_owned()); 201 assoc_item_info.path.segments.push(assoc_item_name.to_owned());
190 assoc_item_info.is_assoc_item = true; 202 assoc_item_info.is_trait_assoc_item = true;
191 self.map.insert(assoc_item, assoc_item_info); 203 self.map.insert(assoc_item, assoc_item_info);
192 } 204 }
193 } 205 }
@@ -314,7 +326,7 @@ impl Query {
314} 326}
315 327
316fn import_matches_query(import: &ImportInfo, query: &Query, enforce_lowercase: bool) -> bool { 328fn import_matches_query(import: &ImportInfo, query: &Query, enforce_lowercase: bool) -> bool {
317 let mut input = if import.is_assoc_item || query.name_only { 329 let mut input = if import.is_trait_assoc_item || query.name_only {
318 import.path.segments.last().unwrap().to_string() 330 import.path.segments.last().unwrap().to_string()
319 } else { 331 } else {
320 import.path.to_string() 332 import.path.to_string()
@@ -455,6 +467,8 @@ mod tests {
455 None => ( 467 None => (
456 dependency_imports.path_of(dependency)?.to_string(), 468 dependency_imports.path_of(dependency)?.to_string(),
457 match dependency { 469 match dependency {
470 ItemInNs::Types(ModuleDefId::FunctionId(_))
471 | ItemInNs::Values(ModuleDefId::FunctionId(_)) => "f",
458 ItemInNs::Types(_) => "t", 472 ItemInNs::Types(_) => "t",
459 ItemInNs::Values(_) => "v", 473 ItemInNs::Values(_) => "v",
460 ItemInNs::Macros(_) => "m", 474 ItemInNs::Macros(_) => "m",
@@ -478,7 +492,16 @@ mod tests {
478 dependency_imports: &ImportMap, 492 dependency_imports: &ImportMap,
479 dependency: ItemInNs, 493 dependency: ItemInNs,
480 ) -> Option<String> { 494 ) -> Option<String> {
481 let dependency_assoc_item_id = dependency.as_assoc_item_id()?; 495 let dependency_assoc_item_id = match dependency {
496 ItemInNs::Types(ModuleDefId::FunctionId(id))
497 | ItemInNs::Values(ModuleDefId::FunctionId(id)) => AssocItemId::from(id),
498 ItemInNs::Types(ModuleDefId::ConstId(id))
499 | ItemInNs::Values(ModuleDefId::ConstId(id)) => AssocItemId::from(id),
500 ItemInNs::Types(ModuleDefId::TypeAliasId(id))
501 | ItemInNs::Values(ModuleDefId::TypeAliasId(id)) => AssocItemId::from(id),
502 _ => return None,
503 };
504
482 let trait_ = assoc_to_trait(db, dependency)?; 505 let trait_ = assoc_to_trait(db, dependency)?;
483 if let ModuleDefId::TraitId(tr) = trait_.as_module_def_id()? { 506 if let ModuleDefId::TraitId(tr) = trait_.as_module_def_id()? {
484 let trait_data = db.trait_data(tr); 507 let trait_data = db.trait_data(tr);
@@ -820,7 +843,7 @@ mod tests {
820 dep::Fmt (m) 843 dep::Fmt (m)
821 dep::fmt::Display (t) 844 dep::fmt::Display (t)
822 dep::fmt::Display::fmt (a) 845 dep::fmt::Display::fmt (a)
823 dep::format (v) 846 dep::format (f)
824 "#]], 847 "#]],
825 ); 848 );
826 849
diff --git a/crates/hir_def/src/item_scope.rs b/crates/hir_def/src/item_scope.rs
index 4da1c3ab0..2750e1c91 100644
--- a/crates/hir_def/src/item_scope.rs
+++ b/crates/hir_def/src/item_scope.rs
@@ -12,9 +12,8 @@ use test_utils::mark;
12 12
13use crate::{ 13use crate::{
14 db::DefDatabase, per_ns::PerNs, visibility::Visibility, AdtId, BuiltinType, HasModule, ImplId, 14 db::DefDatabase, per_ns::PerNs, visibility::Visibility, AdtId, BuiltinType, HasModule, ImplId,
15 LocalModuleId, Lookup, MacroDefId, ModuleDefId, TraitId, 15 LocalModuleId, Lookup, MacroDefId, ModuleDefId, ModuleId, TraitId,
16}; 16};
17use crate::{AssocItemId, ModuleId};
18 17
19#[derive(Copy, Clone)] 18#[derive(Copy, Clone)]
20pub(crate) enum ImportType { 19pub(crate) enum ImportType {
@@ -349,18 +348,6 @@ impl ItemInNs {
349 } 348 }
350 } 349 }
351 350
352 pub fn as_assoc_item_id(self) -> Option<AssocItemId> {
353 match self {
354 ItemInNs::Types(ModuleDefId::FunctionId(id))
355 | ItemInNs::Values(ModuleDefId::FunctionId(id)) => Some(id.into()),
356 ItemInNs::Types(ModuleDefId::ConstId(id))
357 | ItemInNs::Values(ModuleDefId::ConstId(id)) => Some(id.into()),
358 ItemInNs::Types(ModuleDefId::TypeAliasId(id))
359 | ItemInNs::Values(ModuleDefId::TypeAliasId(id)) => Some(id.into()),
360 _ => None,
361 }
362 }
363
364 /// Returns the crate defining this item (or `None` if `self` is built-in). 351 /// Returns the crate defining this item (or `None` if `self` is built-in).
365 pub fn krate(&self, db: &dyn DefDatabase) -> Option<CrateId> { 352 pub fn krate(&self, db: &dyn DefDatabase) -> Option<CrateId> {
366 Some(match self { 353 Some(match self {