aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-11-26 19:26:47 +0000
committerAleksey Kladov <[email protected]>2019-11-26 19:31:04 +0000
commitcace49e9a79a5fe44cda63964412c5bdce7ee90d (patch)
tree6cb1c0b0ef3e24ad3a303c407f8335e4503cb619 /crates
parentd770f22c53a88035e2836cc01533dab4223f80d5 (diff)
Decouple
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir/src/code_model.rs2
-rw-r--r--crates/ra_hir/src/db.rs21
-rw-r--r--crates/ra_hir/src/ty/method_resolution.rs46
-rw-r--r--crates/ra_hir/src/ty/traits.rs16
-rw-r--r--crates/ra_hir/src/ty/traits/chalk.rs2
5 files changed, 42 insertions, 45 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs
index a842dfed6..b4b47057d 100644
--- a/crates/ra_hir/src/code_model.rs
+++ b/crates/ra_hir/src/code_model.rs
@@ -945,7 +945,7 @@ impl ImplBlock {
945 } 945 }
946 pub fn for_trait(db: &impl HirDatabase, krate: Crate, trait_: Trait) -> Vec<ImplBlock> { 946 pub fn for_trait(db: &impl HirDatabase, krate: Crate, trait_: Trait) -> Vec<ImplBlock> {
947 let impls = db.impls_in_crate(krate.crate_id); 947 let impls = db.impls_in_crate(krate.crate_id);
948 impls.lookup_impl_blocks_for_trait(trait_).map(Self::from).collect() 948 impls.lookup_impl_blocks_for_trait(trait_.id).map(Self::from).collect()
949 } 949 }
950 950
951 pub fn target_trait(&self, db: &impl DefDatabase) -> Option<TypeRef> { 951 pub fn target_trait(&self, db: &impl DefDatabase) -> Option<TypeRef> {
diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs
index 3b5aa7516..31b21ca84 100644
--- a/crates/ra_hir/src/db.rs
+++ b/crates/ra_hir/src/db.rs
@@ -2,6 +2,7 @@
2 2
3use std::sync::Arc; 3use std::sync::Arc;
4 4
5use hir_def::{GenericDefId, LocalStructFieldId, TraitId, VariantId};
5use ra_arena::map::ArenaMap; 6use ra_arena::map::ArenaMap;
6use ra_db::{salsa, CrateId}; 7use ra_db::{salsa, CrateId};
7 8
@@ -12,19 +13,15 @@ use crate::{
12 CallableDef, FnSig, GenericPredicate, InferenceResult, Substs, Ty, TyDefId, TypeCtor, 13 CallableDef, FnSig, GenericPredicate, InferenceResult, Substs, Ty, TyDefId, TypeCtor,
13 ValueTyDefId, 14 ValueTyDefId,
14 }, 15 },
15 Crate, DefWithBody, ImplBlock, Trait, 16 Crate, DefWithBody, ImplBlock,
16}; 17};
17 18
18pub use hir_def::{ 19pub use hir_def::db::{
19 db::{ 20 BodyQuery, BodyWithSourceMapQuery, ConstDataQuery, CrateDefMapQuery, CrateLangItemsQuery,
20 BodyQuery, BodyWithSourceMapQuery, ConstDataQuery, CrateDefMapQuery, CrateLangItemsQuery, 21 DefDatabase, DefDatabaseStorage, DocumentationQuery, EnumDataQuery, ExprScopesQuery,
21 DefDatabase, DefDatabaseStorage, DocumentationQuery, EnumDataQuery, ExprScopesQuery, 22 FunctionDataQuery, GenericParamsQuery, ImplDataQuery, InternDatabase, InternDatabaseStorage,
22 FunctionDataQuery, GenericParamsQuery, ImplDataQuery, InternDatabase, 23 LangItemQuery, ModuleLangItemsQuery, RawItemsQuery, RawItemsWithSourceMapQuery,
23 InternDatabaseStorage, LangItemQuery, ModuleLangItemsQuery, RawItemsQuery, 24 StaticDataQuery, StructDataQuery, TraitDataQuery, TypeAliasDataQuery,
24 RawItemsWithSourceMapQuery, StaticDataQuery, StructDataQuery, TraitDataQuery,
25 TypeAliasDataQuery,
26 },
27 GenericDefId, LocalStructFieldId, VariantId,
28}; 25};
29pub use hir_expand::db::{ 26pub use hir_expand::db::{
30 AstDatabase, AstDatabaseStorage, AstIdMapQuery, MacroArgQuery, MacroDefQuery, MacroExpandQuery, 27 AstDatabase, AstDatabaseStorage, AstIdMapQuery, MacroArgQuery, MacroDefQuery, MacroExpandQuery,
@@ -66,7 +63,7 @@ pub trait HirDatabase: DefDatabase {
66 fn impls_in_crate(&self, krate: CrateId) -> Arc<CrateImplBlocks>; 63 fn impls_in_crate(&self, krate: CrateId) -> Arc<CrateImplBlocks>;
67 64
68 #[salsa::invoke(crate::ty::traits::impls_for_trait_query)] 65 #[salsa::invoke(crate::ty::traits::impls_for_trait_query)]
69 fn impls_for_trait(&self, krate: Crate, trait_: Trait) -> Arc<[ImplBlock]>; 66 fn impls_for_trait(&self, krate: CrateId, trait_: TraitId) -> Arc<[ImplBlock]>;
70 67
71 /// This provides the Chalk trait solver instance. Because Chalk always 68 /// This provides the Chalk trait solver instance. Because Chalk always
72 /// works from a specific crate, this query is keyed on the crate; and 69 /// works from a specific crate, this query is keyed on the crate; and
diff --git a/crates/ra_hir/src/ty/method_resolution.rs b/crates/ra_hir/src/ty/method_resolution.rs
index f1bc638ee..fdc87a28d 100644
--- a/crates/ra_hir/src/ty/method_resolution.rs
+++ b/crates/ra_hir/src/ty/method_resolution.rs
@@ -6,9 +6,10 @@ use std::sync::Arc;
6 6
7use arrayvec::ArrayVec; 7use arrayvec::ArrayVec;
8use hir_def::{ 8use hir_def::{
9 lang_item::LangItemTarget, resolver::HasResolver, resolver::Resolver, AssocItemId, AstItemDef, 9 lang_item::LangItemTarget, resolver::HasResolver, resolver::Resolver, type_ref::Mutability,
10 HasModule, ImplId, TraitId, 10 AssocItemId, AstItemDef, HasModule, ImplId, TraitId,
11}; 11};
12use hir_expand::name::Name;
12use ra_db::CrateId; 13use ra_db::CrateId;
13use ra_prof::profile; 14use ra_prof::profile;
14use rustc_hash::FxHashMap; 15use rustc_hash::FxHashMap;
@@ -17,7 +18,7 @@ use crate::{
17 db::HirDatabase, 18 db::HirDatabase,
18 ty::primitive::{FloatBitness, Uncertain}, 19 ty::primitive::{FloatBitness, Uncertain},
19 ty::{utils::all_super_traits, Ty, TypeCtor}, 20 ty::{utils::all_super_traits, Ty, TypeCtor},
20 AssocItem, Crate, Function, Mutability, Name, Trait, 21 AssocItem, Function,
21}; 22};
22 23
23use super::{autoderef, Canonical, InEnvironment, TraitEnvironment, TraitRef}; 24use super::{autoderef, Canonical, InEnvironment, TraitEnvironment, TraitRef};
@@ -87,8 +88,8 @@ impl CrateImplBlocks {
87 fingerprint.and_then(|f| self.impls.get(&f)).into_iter().flatten().copied() 88 fingerprint.and_then(|f| self.impls.get(&f)).into_iter().flatten().copied()
88 } 89 }
89 90
90 pub fn lookup_impl_blocks_for_trait(&self, tr: Trait) -> impl Iterator<Item = ImplId> + '_ { 91 pub fn lookup_impl_blocks_for_trait(&self, tr: TraitId) -> impl Iterator<Item = ImplId> + '_ {
91 self.impls_by_trait.get(&tr.id).into_iter().flatten().copied() 92 self.impls_by_trait.get(&tr).into_iter().flatten().copied()
92 } 93 }
93 94
94 pub fn all_impls<'a>(&'a self) -> impl Iterator<Item = ImplId> + 'a { 95 pub fn all_impls<'a>(&'a self) -> impl Iterator<Item = ImplId> + 'a {
@@ -96,14 +97,18 @@ impl CrateImplBlocks {
96 } 97 }
97} 98}
98 99
99fn def_crates(db: &impl HirDatabase, cur_crate: Crate, ty: &Ty) -> Option<ArrayVec<[Crate; 2]>> { 100fn def_crates(
101 db: &impl HirDatabase,
102 cur_crate: CrateId,
103 ty: &Ty,
104) -> Option<ArrayVec<[CrateId; 2]>> {
100 // Types like slice can have inherent impls in several crates, (core and alloc). 105 // Types like slice can have inherent impls in several crates, (core and alloc).
101 // The corresponding impls are marked with lang items, so we can use them to find the required crates. 106 // The corresponding impls are marked with lang items, so we can use them to find the required crates.
102 macro_rules! lang_item_crate { 107 macro_rules! lang_item_crate {
103 ($($name:expr),+ $(,)?) => {{ 108 ($($name:expr),+ $(,)?) => {{
104 let mut v = ArrayVec::<[LangItemTarget; 2]>::new(); 109 let mut v = ArrayVec::<[LangItemTarget; 2]>::new();
105 $( 110 $(
106 v.extend(db.lang_item(cur_crate.crate_id, $name.into())); 111 v.extend(db.lang_item(cur_crate, $name.into()));
107 )+ 112 )+
108 v 113 v
109 }}; 114 }};
@@ -112,7 +117,7 @@ fn def_crates(db: &impl HirDatabase, cur_crate: Crate, ty: &Ty) -> Option<ArrayV
112 let lang_item_targets = match ty { 117 let lang_item_targets = match ty {
113 Ty::Apply(a_ty) => match a_ty.ctor { 118 Ty::Apply(a_ty) => match a_ty.ctor {
114 TypeCtor::Adt(def_id) => { 119 TypeCtor::Adt(def_id) => {
115 return Some(std::iter::once(def_id.module(db).krate.into()).collect()) 120 return Some(std::iter::once(def_id.module(db).krate).collect())
116 } 121 }
117 TypeCtor::Bool => lang_item_crate!("bool"), 122 TypeCtor::Bool => lang_item_crate!("bool"),
118 TypeCtor::Char => lang_item_crate!("char"), 123 TypeCtor::Char => lang_item_crate!("char"),
@@ -136,7 +141,7 @@ fn def_crates(db: &impl HirDatabase, cur_crate: Crate, ty: &Ty) -> Option<ArrayV
136 LangItemTarget::ImplBlockId(it) => Some(it), 141 LangItemTarget::ImplBlockId(it) => Some(it),
137 _ => None, 142 _ => None,
138 }) 143 })
139 .map(|it| it.module(db).krate.into()) 144 .map(|it| it.module(db).krate)
140 .collect(); 145 .collect();
141 Some(res) 146 Some(res)
142} 147}
@@ -193,14 +198,9 @@ pub(crate) fn iterate_method_candidates<T>(
193 let environment = TraitEnvironment::lower(db, resolver); 198 let environment = TraitEnvironment::lower(db, resolver);
194 let ty = InEnvironment { value: ty.clone(), environment }; 199 let ty = InEnvironment { value: ty.clone(), environment };
195 for derefed_ty in autoderef::autoderef(db, resolver.krate(), ty) { 200 for derefed_ty in autoderef::autoderef(db, resolver.krate(), ty) {
196 if let Some(result) = iterate_inherent_methods( 201 if let Some(result) =
197 &derefed_ty, 202 iterate_inherent_methods(&derefed_ty, db, name, mode, krate, &mut callback)
198 db, 203 {
199 name,
200 mode,
201 krate.into(),
202 &mut callback,
203 ) {
204 return Some(result); 204 return Some(result);
205 } 205 }
206 if let Some(result) = iterate_trait_method_candidates( 206 if let Some(result) = iterate_trait_method_candidates(
@@ -283,11 +283,11 @@ fn iterate_inherent_methods<T>(
283 db: &impl HirDatabase, 283 db: &impl HirDatabase,
284 name: Option<&Name>, 284 name: Option<&Name>,
285 mode: LookupMode, 285 mode: LookupMode,
286 krate: Crate, 286 krate: CrateId,
287 mut callback: impl FnMut(&Ty, AssocItem) -> Option<T>, 287 mut callback: impl FnMut(&Ty, AssocItem) -> Option<T>,
288) -> Option<T> { 288) -> Option<T> {
289 for krate in def_crates(db, krate, &ty.value)? { 289 for krate in def_crates(db, krate, &ty.value)? {
290 let impls = db.impls_in_crate(krate.crate_id); 290 let impls = db.impls_in_crate(krate);
291 291
292 for impl_block in impls.lookup_impl_blocks(&ty.value) { 292 for impl_block in impls.lookup_impl_blocks(&ty.value) {
293 for &item in db.impl_data(impl_block).items.iter() { 293 for &item in db.impl_data(impl_block).items.iter() {
@@ -327,7 +327,7 @@ pub(crate) fn implements_trait(
327 ty: &Canonical<Ty>, 327 ty: &Canonical<Ty>,
328 db: &impl HirDatabase, 328 db: &impl HirDatabase,
329 resolver: &Resolver, 329 resolver: &Resolver,
330 krate: Crate, 330 krate: CrateId,
331 trait_: TraitId, 331 trait_: TraitId,
332) -> bool { 332) -> bool {
333 if ty.value.inherent_trait() == Some(trait_) { 333 if ty.value.inherent_trait() == Some(trait_) {
@@ -337,7 +337,7 @@ pub(crate) fn implements_trait(
337 } 337 }
338 let env = TraitEnvironment::lower(db, resolver); 338 let env = TraitEnvironment::lower(db, resolver);
339 let goal = generic_implements_goal(db, env, trait_, ty.clone()); 339 let goal = generic_implements_goal(db, env, trait_, ty.clone());
340 let solution = db.trait_solve(krate, goal); 340 let solution = db.trait_solve(krate.into(), goal);
341 341
342 solution.is_some() 342 solution.is_some()
343} 343}
@@ -348,11 +348,11 @@ impl Ty {
348 pub fn iterate_impl_items<T>( 348 pub fn iterate_impl_items<T>(
349 self, 349 self,
350 db: &impl HirDatabase, 350 db: &impl HirDatabase,
351 krate: Crate, 351 krate: CrateId,
352 mut callback: impl FnMut(AssocItem) -> Option<T>, 352 mut callback: impl FnMut(AssocItem) -> Option<T>,
353 ) -> Option<T> { 353 ) -> Option<T> {
354 for krate in def_crates(db, krate, &self)? { 354 for krate in def_crates(db, krate, &self)? {
355 let impls = db.impls_in_crate(krate.crate_id); 355 let impls = db.impls_in_crate(krate);
356 356
357 for impl_block in impls.lookup_impl_blocks(&self) { 357 for impl_block in impls.lookup_impl_blocks(&self) {
358 for &item in db.impl_data(impl_block).items.iter() { 358 for &item in db.impl_data(impl_block).items.iter() {
diff --git a/crates/ra_hir/src/ty/traits.rs b/crates/ra_hir/src/ty/traits.rs
index a91c2476b..637e21e9c 100644
--- a/crates/ra_hir/src/ty/traits.rs
+++ b/crates/ra_hir/src/ty/traits.rs
@@ -2,13 +2,13 @@
2use std::sync::{Arc, Mutex}; 2use std::sync::{Arc, Mutex};
3 3
4use chalk_ir::{cast::Cast, family::ChalkIr}; 4use chalk_ir::{cast::Cast, family::ChalkIr};
5use hir_def::{expr::ExprId, DefWithBodyId}; 5use hir_def::{expr::ExprId, DefWithBodyId, TraitId};
6use log::debug; 6use log::debug;
7use ra_db::{impl_intern_key, salsa}; 7use ra_db::{impl_intern_key, salsa, CrateId};
8use ra_prof::profile; 8use ra_prof::profile;
9use rustc_hash::FxHashSet; 9use rustc_hash::FxHashSet;
10 10
11use crate::{db::HirDatabase, Crate, ImplBlock, Trait, TypeAlias}; 11use crate::{db::HirDatabase, Crate, ImplBlock, TypeAlias};
12 12
13use super::{Canonical, GenericPredicate, HirDisplay, ProjectionTy, TraitRef, Ty, TypeWalk}; 13use super::{Canonical, GenericPredicate, HirDisplay, ProjectionTy, TraitRef, Ty, TypeWalk};
14 14
@@ -77,8 +77,8 @@ pub(crate) fn trait_solver_query(
77/// Collects impls for the given trait in the whole dependency tree of `krate`. 77/// Collects impls for the given trait in the whole dependency tree of `krate`.
78pub(crate) fn impls_for_trait_query( 78pub(crate) fn impls_for_trait_query(
79 db: &impl HirDatabase, 79 db: &impl HirDatabase,
80 krate: Crate, 80 krate: CrateId,
81 trait_: Trait, 81 trait_: TraitId,
82) -> Arc<[ImplBlock]> { 82) -> Arc<[ImplBlock]> {
83 let mut impls = FxHashSet::default(); 83 let mut impls = FxHashSet::default();
84 // We call the query recursively here. On the one hand, this means we can 84 // We call the query recursively here. On the one hand, this means we can
@@ -86,10 +86,10 @@ pub(crate) fn impls_for_trait_query(
86 // will only ever get called for a few crates near the root of the tree (the 86 // will only ever get called for a few crates near the root of the tree (the
87 // ones the user is editing), so this may actually be a waste of memory. I'm 87 // ones the user is editing), so this may actually be a waste of memory. I'm
88 // doing it like this mainly for simplicity for now. 88 // doing it like this mainly for simplicity for now.
89 for dep in krate.dependencies(db) { 89 for dep in db.crate_graph().dependencies(krate) {
90 impls.extend(db.impls_for_trait(dep.krate, trait_).iter()); 90 impls.extend(db.impls_for_trait(dep.crate_id, trait_).iter());
91 } 91 }
92 let crate_impl_blocks = db.impls_in_crate(krate.crate_id); 92 let crate_impl_blocks = db.impls_in_crate(krate);
93 impls.extend(crate_impl_blocks.lookup_impl_blocks_for_trait(trait_).map(ImplBlock::from)); 93 impls.extend(crate_impl_blocks.lookup_impl_blocks_for_trait(trait_).map(ImplBlock::from));
94 impls.into_iter().collect() 94 impls.into_iter().collect()
95} 95}
diff --git a/crates/ra_hir/src/ty/traits/chalk.rs b/crates/ra_hir/src/ty/traits/chalk.rs
index 4b0f4f56c..d879382a0 100644
--- a/crates/ra_hir/src/ty/traits/chalk.rs
+++ b/crates/ra_hir/src/ty/traits/chalk.rs
@@ -448,7 +448,7 @@ where
448 let trait_: TraitId = from_chalk(self.db, trait_id); 448 let trait_: TraitId = from_chalk(self.db, trait_id);
449 let mut result: Vec<_> = self 449 let mut result: Vec<_> = self
450 .db 450 .db
451 .impls_for_trait(self.krate, trait_.into()) 451 .impls_for_trait(self.krate.crate_id, trait_.into())
452 .iter() 452 .iter()
453 .copied() 453 .copied()
454 .map(Impl::ImplBlock) 454 .map(Impl::ImplBlock)