aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src')
-rw-r--r--crates/ra_hir/src/code_model.rs32
-rw-r--r--crates/ra_hir/src/db.rs10
-rw-r--r--crates/ra_hir/src/lib.rs1
-rw-r--r--crates/ra_hir/src/traits.rs82
-rw-r--r--crates/ra_hir/src/ty/method_resolution.rs6
5 files changed, 17 insertions, 114 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs
index cb990f4e2..f436d5d5e 100644
--- a/crates/ra_hir/src/code_model.rs
+++ b/crates/ra_hir/src/code_model.rs
@@ -10,8 +10,9 @@ use hir_def::{
10 adt::VariantData, 10 adt::VariantData,
11 body::scope::ExprScopes, 11 body::scope::ExprScopes,
12 builtin_type::BuiltinType, 12 builtin_type::BuiltinType,
13 traits::TraitData,
13 type_ref::{Mutability, TypeRef}, 14 type_ref::{Mutability, TypeRef},
14 CrateModuleId, ImplId, LocalEnumVariantId, LocalStructFieldId, ModuleId, UnionId, 15 AssocItemId, CrateModuleId, ImplId, LocalEnumVariantId, LocalStructFieldId, ModuleId, UnionId,
15}; 16};
16use hir_expand::{ 17use hir_expand::{
17 diagnostics::DiagnosticSink, 18 diagnostics::DiagnosticSink,
@@ -30,7 +31,6 @@ use crate::{
30 TypeAliasId, 31 TypeAliasId,
31 }, 32 },
32 resolve::{Resolver, Scope, TypeNs}, 33 resolve::{Resolver, Scope, TypeNs},
33 traits::TraitData,
34 ty::{InferenceResult, Namespace, TraitRef}, 34 ty::{InferenceResult, Namespace, TraitRef},
35 Either, HasSource, ImportId, Name, ScopeDef, Source, Ty, 35 Either, HasSource, ImportId, Name, ScopeDef, Source, Ty,
36}; 36};
@@ -230,15 +230,7 @@ impl Module {
230 230
231 pub fn declarations(self, db: &impl DefDatabase) -> Vec<ModuleDef> { 231 pub fn declarations(self, db: &impl DefDatabase) -> Vec<ModuleDef> {
232 let def_map = db.crate_def_map(self.id.krate); 232 let def_map = db.crate_def_map(self.id.krate);
233 def_map[self.id.module_id] 233 def_map[self.id.module_id].scope.declarations().map(ModuleDef::from).collect()
234 .scope
235 .entries()
236 .filter_map(|(_name, res)| if res.import.is_none() { Some(res.def) } else { None })
237 .flat_map(|per_ns| {
238 per_ns.take_types().into_iter().chain(per_ns.take_values().into_iter())
239 })
240 .map(ModuleDef::from)
241 .collect()
242 } 234 }
243 235
244 pub fn impl_blocks(self, db: &impl DefDatabase) -> Vec<ImplBlock> { 236 pub fn impl_blocks(self, db: &impl DefDatabase) -> Vec<ImplBlock> {
@@ -693,7 +685,7 @@ impl Function {
693 685
694 /// The containing trait, if this is a trait method definition. 686 /// The containing trait, if this is a trait method definition.
695 pub fn parent_trait(self, db: &impl DefDatabase) -> Option<Trait> { 687 pub fn parent_trait(self, db: &impl DefDatabase) -> Option<Trait> {
696 db.trait_items_index(self.module(db)).get_parent_trait(self.into()) 688 db.trait_items_index(self.module(db).id).get_parent_trait(self.id.into()).map(Trait::from)
697 } 689 }
698 690
699 pub fn container(self, db: &impl DefDatabase) -> Option<Container> { 691 pub fn container(self, db: &impl DefDatabase) -> Option<Container> {
@@ -757,7 +749,7 @@ impl Const {
757 } 749 }
758 750
759 pub fn parent_trait(self, db: &impl DefDatabase) -> Option<Trait> { 751 pub fn parent_trait(self, db: &impl DefDatabase) -> Option<Trait> {
760 db.trait_items_index(self.module(db)).get_parent_trait(self.into()) 752 db.trait_items_index(self.module(db).id).get_parent_trait(self.id.into()).map(Trait::from)
761 } 753 }
762 754
763 pub fn container(self, db: &impl DefDatabase) -> Option<Container> { 755 pub fn container(self, db: &impl DefDatabase) -> Option<Container> {
@@ -861,11 +853,11 @@ impl Trait {
861 } 853 }
862 854
863 pub fn name(self, db: &impl DefDatabase) -> Option<Name> { 855 pub fn name(self, db: &impl DefDatabase) -> Option<Name> {
864 self.trait_data(db).name().clone() 856 self.trait_data(db).name.clone()
865 } 857 }
866 858
867 pub fn items(self, db: &impl DefDatabase) -> Vec<AssocItem> { 859 pub fn items(self, db: &impl DefDatabase) -> Vec<AssocItem> {
868 self.trait_data(db).items().to_vec() 860 self.trait_data(db).items.iter().map(|it| (*it).into()).collect()
869 } 861 }
870 862
871 fn direct_super_traits(self, db: &impl HirDatabase) -> Vec<Trait> { 863 fn direct_super_traits(self, db: &impl HirDatabase) -> Vec<Trait> {
@@ -912,10 +904,10 @@ impl Trait {
912 pub fn associated_type_by_name(self, db: &impl DefDatabase, name: &Name) -> Option<TypeAlias> { 904 pub fn associated_type_by_name(self, db: &impl DefDatabase, name: &Name) -> Option<TypeAlias> {
913 let trait_data = self.trait_data(db); 905 let trait_data = self.trait_data(db);
914 trait_data 906 trait_data
915 .items() 907 .items
916 .iter() 908 .iter()
917 .filter_map(|item| match item { 909 .filter_map(|item| match item {
918 AssocItem::TypeAlias(t) => Some(*t), 910 AssocItemId::TypeAliasId(t) => Some(TypeAlias::from(*t)),
919 _ => None, 911 _ => None,
920 }) 912 })
921 .find(|t| &t.name(db) == name) 913 .find(|t| &t.name(db) == name)
@@ -930,7 +922,7 @@ impl Trait {
930 } 922 }
931 923
932 pub(crate) fn trait_data(self, db: &impl DefDatabase) -> Arc<TraitData> { 924 pub(crate) fn trait_data(self, db: &impl DefDatabase) -> Arc<TraitData> {
933 db.trait_data(self) 925 db.trait_data(self.id)
934 } 926 }
935 927
936 pub fn trait_ref(self, db: &impl HirDatabase) -> TraitRef { 928 pub fn trait_ref(self, db: &impl HirDatabase) -> TraitRef {
@@ -938,7 +930,7 @@ impl Trait {
938 } 930 }
939 931
940 pub fn is_auto(self, db: &impl DefDatabase) -> bool { 932 pub fn is_auto(self, db: &impl DefDatabase) -> bool {
941 self.trait_data(db).is_auto() 933 self.trait_data(db).auto
942 } 934 }
943 935
944 pub(crate) fn resolver(self, db: &impl DefDatabase) -> Resolver { 936 pub(crate) fn resolver(self, db: &impl DefDatabase) -> Resolver {
@@ -971,7 +963,7 @@ impl TypeAlias {
971 963
972 /// The containing trait, if this is a trait method definition. 964 /// The containing trait, if this is a trait method definition.
973 pub fn parent_trait(self, db: &impl DefDatabase) -> Option<Trait> { 965 pub fn parent_trait(self, db: &impl DefDatabase) -> Option<Trait> {
974 db.trait_items_index(self.module(db)).get_parent_trait(self.into()) 966 db.trait_items_index(self.module(db).id).get_parent_trait(self.id.into()).map(Trait::from)
975 } 967 }
976 968
977 pub fn container(self, db: &impl DefDatabase) -> Option<Container> { 969 pub fn container(self, db: &impl DefDatabase) -> Option<Container> {
diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs
index 0d35014a0..73859e1e9 100644
--- a/crates/ra_hir/src/db.rs
+++ b/crates/ra_hir/src/db.rs
@@ -11,7 +11,6 @@ use crate::{
11 generics::{GenericDef, GenericParams}, 11 generics::{GenericDef, GenericParams},
12 ids, 12 ids,
13 lang_item::{LangItemTarget, LangItems}, 13 lang_item::{LangItemTarget, LangItems},
14 traits::TraitData,
15 ty::{ 14 ty::{
16 method_resolution::CrateImplBlocks, 15 method_resolution::CrateImplBlocks,
17 traits::{AssocTyValue, Impl}, 16 traits::{AssocTyValue, Impl},
@@ -26,7 +25,8 @@ use crate::{
26pub use hir_def::db::{ 25pub use hir_def::db::{
27 BodyQuery, BodyWithSourceMapQuery, CrateDefMapQuery, DefDatabase2, DefDatabase2Storage, 26 BodyQuery, BodyWithSourceMapQuery, CrateDefMapQuery, DefDatabase2, DefDatabase2Storage,
28 EnumDataQuery, ExprScopesQuery, ImplDataQuery, InternDatabase, InternDatabaseStorage, 27 EnumDataQuery, ExprScopesQuery, ImplDataQuery, InternDatabase, InternDatabaseStorage,
29 RawItemsQuery, RawItemsWithSourceMapQuery, StructDataQuery, 28 RawItemsQuery, RawItemsWithSourceMapQuery, StructDataQuery, TraitDataQuery,
29 TraitItemsIndexQuery,
30}; 30};
31pub use hir_expand::db::{ 31pub use hir_expand::db::{
32 AstDatabase, AstDatabaseStorage, AstIdMapQuery, MacroArgQuery, MacroDefQuery, MacroExpandQuery, 32 AstDatabase, AstDatabaseStorage, AstIdMapQuery, MacroArgQuery, MacroDefQuery, MacroExpandQuery,
@@ -37,12 +37,6 @@ pub use hir_expand::db::{
37#[salsa::query_group(DefDatabaseStorage)] 37#[salsa::query_group(DefDatabaseStorage)]
38#[salsa::requires(AstDatabase)] 38#[salsa::requires(AstDatabase)]
39pub trait DefDatabase: HirDebugDatabase + DefDatabase2 { 39pub trait DefDatabase: HirDebugDatabase + DefDatabase2 {
40 #[salsa::invoke(crate::traits::TraitData::trait_data_query)]
41 fn trait_data(&self, t: Trait) -> Arc<TraitData>;
42
43 #[salsa::invoke(crate::traits::TraitItemsIndex::trait_items_index)]
44 fn trait_items_index(&self, module: Module) -> crate::traits::TraitItemsIndex;
45
46 #[salsa::invoke(crate::generics::generic_params_query)] 40 #[salsa::invoke(crate::generics::generic_params_query)]
47 fn generic_params(&self, def: GenericDef) -> Arc<GenericParams>; 41 fn generic_params(&self, def: GenericDef) -> Arc<GenericParams>;
48 42
diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs
index da33c9591..7ac9a9041 100644
--- a/crates/ra_hir/src/lib.rs
+++ b/crates/ra_hir/src/lib.rs
@@ -33,7 +33,6 @@ pub mod source_binder;
33 33
34mod ids; 34mod ids;
35mod adt; 35mod adt;
36mod traits;
37mod type_alias; 36mod type_alias;
38mod ty; 37mod ty;
39mod impl_block; 38mod impl_block;
diff --git a/crates/ra_hir/src/traits.rs b/crates/ra_hir/src/traits.rs
deleted file mode 100644
index 858972c6f..000000000
--- a/crates/ra_hir/src/traits.rs
+++ /dev/null
@@ -1,82 +0,0 @@
1//! HIR for trait definitions.
2
3use std::sync::Arc;
4
5use hir_expand::name::AsName;
6
7use ra_syntax::ast::{self, NameOwner};
8use rustc_hash::FxHashMap;
9
10use crate::{
11 db::{AstDatabase, DefDatabase},
12 ids::LocationCtx,
13 AssocItem, Const, Function, HasSource, Module, Name, Trait, TypeAlias,
14};
15
16#[derive(Debug, Clone, PartialEq, Eq)]
17pub struct TraitData {
18 name: Option<Name>,
19 items: Vec<AssocItem>,
20 auto: bool,
21}
22
23impl TraitData {
24 pub(crate) fn trait_data_query(
25 db: &(impl DefDatabase + AstDatabase),
26 tr: Trait,
27 ) -> Arc<TraitData> {
28 let src = tr.source(db);
29 let name = src.value.name().map(|n| n.as_name());
30 let module = tr.module(db);
31 let ctx = LocationCtx::new(db, module.id, src.file_id);
32 let auto = src.value.is_auto();
33 let items = if let Some(item_list) = src.value.item_list() {
34 item_list
35 .impl_items()
36 .map(|item_node| match item_node {
37 ast::ImplItem::FnDef(it) => Function { id: ctx.to_def(&it) }.into(),
38 ast::ImplItem::ConstDef(it) => Const { id: ctx.to_def(&it) }.into(),
39 ast::ImplItem::TypeAliasDef(it) => TypeAlias { id: ctx.to_def(&it) }.into(),
40 })
41 .collect()
42 } else {
43 Vec::new()
44 };
45 Arc::new(TraitData { name, items, auto })
46 }
47
48 pub(crate) fn name(&self) -> &Option<Name> {
49 &self.name
50 }
51
52 pub(crate) fn items(&self) -> &[AssocItem] {
53 &self.items
54 }
55
56 pub(crate) fn is_auto(&self) -> bool {
57 self.auto
58 }
59}
60
61#[derive(Debug, Clone, PartialEq, Eq)]
62pub struct TraitItemsIndex {
63 traits_by_def: FxHashMap<AssocItem, Trait>,
64}
65
66impl TraitItemsIndex {
67 pub(crate) fn trait_items_index(db: &impl DefDatabase, module: Module) -> TraitItemsIndex {
68 let mut index = TraitItemsIndex { traits_by_def: FxHashMap::default() };
69 for decl in module.declarations(db) {
70 if let crate::ModuleDef::Trait(tr) = decl {
71 for item in tr.trait_data(db).items() {
72 index.traits_by_def.insert(*item, tr);
73 }
74 }
75 }
76 index
77 }
78
79 pub(crate) fn get_parent_trait(&self, item: AssocItem) -> Option<Trait> {
80 self.traits_by_def.get(&item).cloned()
81 }
82}
diff --git a/crates/ra_hir/src/ty/method_resolution.rs b/crates/ra_hir/src/ty/method_resolution.rs
index d20aeaacf..f377fca48 100644
--- a/crates/ra_hir/src/ty/method_resolution.rs
+++ b/crates/ra_hir/src/ty/method_resolution.rs
@@ -232,8 +232,8 @@ fn iterate_trait_method_candidates<T>(
232 // trait, but if we find out it doesn't, we'll skip the rest of the 232 // trait, but if we find out it doesn't, we'll skip the rest of the
233 // iteration 233 // iteration
234 let mut known_implemented = false; 234 let mut known_implemented = false;
235 for &item in data.items() { 235 for &item in data.items.iter() {
236 if !is_valid_candidate(db, name, mode, item) { 236 if !is_valid_candidate(db, name, mode, item.into()) {
237 continue; 237 continue;
238 } 238 }
239 if !known_implemented { 239 if !known_implemented {
@@ -243,7 +243,7 @@ fn iterate_trait_method_candidates<T>(
243 } 243 }
244 } 244 }
245 known_implemented = true; 245 known_implemented = true;
246 if let Some(result) = callback(&ty.value, item) { 246 if let Some(result) = callback(&ty.value, item.into()) {
247 return Some(result); 247 return Some(result);
248 } 248 }
249 } 249 }