aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-11-21 12:24:51 +0000
committerAleksey Kladov <[email protected]>2019-11-21 12:24:51 +0000
commit00684d708b64fe81a0264795f27594d450a8d08d (patch)
tree031d8f3859fd7d38279c96be34afb09c85e2f74d /crates/ra_hir/src
parenteb53aa37a336b6c9369ea931812f6c491e8f4a71 (diff)
Decouple
Diffstat (limited to 'crates/ra_hir/src')
-rw-r--r--crates/ra_hir/src/from_id.rs8
-rw-r--r--crates/ra_hir/src/resolve.rs42
-rw-r--r--crates/ra_hir/src/source_binder.rs13
-rw-r--r--crates/ra_hir/src/ty/autoderef.rs4
-rw-r--r--crates/ra_hir/src/ty/infer.rs9
-rw-r--r--crates/ra_hir/src/ty/infer/coerce.rs4
-rw-r--r--crates/ra_hir/src/ty/lower.rs7
-rw-r--r--crates/ra_hir/src/ty/method_resolution.rs15
8 files changed, 53 insertions, 49 deletions
diff --git a/crates/ra_hir/src/from_id.rs b/crates/ra_hir/src/from_id.rs
index e294e957b..a3e9d8525 100644
--- a/crates/ra_hir/src/from_id.rs
+++ b/crates/ra_hir/src/from_id.rs
@@ -9,10 +9,16 @@ use hir_def::{
9}; 9};
10 10
11use crate::{ 11use crate::{
12 ty::TypableDef, Adt, AssocItem, Const, DefWithBody, EnumVariant, Function, GenericDef, 12 ty::TypableDef, Adt, AssocItem, Const, Crate, DefWithBody, EnumVariant, Function, GenericDef,
13 ModuleDef, Static, TypeAlias, 13 ModuleDef, Static, TypeAlias,
14}; 14};
15 15
16impl From<ra_db::CrateId> for Crate {
17 fn from(crate_id: ra_db::CrateId) -> Self {
18 Crate { crate_id }
19 }
20}
21
16macro_rules! from_id { 22macro_rules! from_id {
17 ($(($id:path, $ty:path)),*) => {$( 23 ($(($id:path, $ty:path)),*) => {$(
18 impl From<$id> for $ty { 24 impl From<$id> for $ty {
diff --git a/crates/ra_hir/src/resolve.rs b/crates/ra_hir/src/resolve.rs
index 7ff0c43ae..a616f0ea5 100644
--- a/crates/ra_hir/src/resolve.rs
+++ b/crates/ra_hir/src/resolve.rs
@@ -2,25 +2,23 @@
2use std::sync::Arc; 2use std::sync::Arc;
3 3
4use hir_def::{ 4use hir_def::{
5 body::scope::{ExprScopes, ScopeId},
5 builtin_type::BuiltinType, 6 builtin_type::BuiltinType,
6 db::DefDatabase2, 7 db::DefDatabase2,
7 expr::ExprId, 8 expr::{ExprId, PatId},
8 generics::GenericParams, 9 generics::GenericParams,
9 nameres::CrateDefMap, 10 nameres::{per_ns::PerNs, CrateDefMap},
10 path::{Path, PathKind}, 11 path::{Path, PathKind},
11 AdtId, AstItemDef, ConstId, ContainerId, CrateModuleId, DefWithBodyId, EnumId, EnumVariantId, 12 AdtId, AstItemDef, ConstId, ContainerId, CrateModuleId, DefWithBodyId, EnumId, EnumVariantId,
12 FunctionId, GenericDefId, ImplId, Lookup, ModuleDefId, ModuleId, StaticId, StructId, TraitId, 13 FunctionId, GenericDefId, ImplId, Lookup, ModuleDefId, ModuleId, StaticId, StructId, TraitId,
13 TypeAliasId, UnionId, 14 TypeAliasId, UnionId,
14}; 15};
15use hir_expand::name::{self, Name}; 16use hir_expand::{
16use rustc_hash::FxHashSet; 17 name::{self, Name},
17 18 MacroDefId,
18use crate::{
19 code_model::Crate,
20 db::HirDatabase,
21 expr::{ExprScopes, PatId, ScopeId},
22 DefWithBody, GenericDef, MacroDef, PerNs,
23}; 19};
20use ra_db::CrateId;
21use rustc_hash::FxHashSet;
24 22
25#[derive(Debug, Clone, Default)] 23#[derive(Debug, Clone, Default)]
26pub(crate) struct Resolver { 24pub(crate) struct Resolver {
@@ -318,9 +316,9 @@ impl Resolver {
318 &self, 316 &self,
319 db: &impl DefDatabase2, 317 db: &impl DefDatabase2,
320 path: &Path, 318 path: &Path,
321 ) -> Option<MacroDef> { 319 ) -> Option<MacroDefId> {
322 let (item_map, module) = self.module()?; 320 let (item_map, module) = self.module()?;
323 item_map.resolve_path(db, module, path).0.get_macros().map(MacroDef::from) 321 item_map.resolve_path(db, module, path).0.get_macros()
324 } 322 }
325 323
326 pub(crate) fn process_all_names( 324 pub(crate) fn process_all_names(
@@ -355,8 +353,8 @@ impl Resolver {
355 }) 353 })
356 } 354 }
357 355
358 pub(crate) fn krate(&self) -> Option<Crate> { 356 pub(crate) fn krate(&self) -> Option<CrateId> {
359 self.module().map(|t| Crate { crate_id: t.0.krate() }) 357 self.module().map(|t| t.0.krate())
360 } 358 }
361 359
362 pub(crate) fn where_predicates_in_scope<'a>( 360 pub(crate) fn where_predicates_in_scope<'a>(
@@ -484,7 +482,7 @@ impl Scope {
484 482
485// needs arbitrary_self_types to be a method... or maybe move to the def? 483// needs arbitrary_self_types to be a method... or maybe move to the def?
486pub(crate) fn resolver_for_expr( 484pub(crate) fn resolver_for_expr(
487 db: &impl HirDatabase, 485 db: &impl DefDatabase2,
488 owner: DefWithBodyId, 486 owner: DefWithBodyId,
489 expr_id: ExprId, 487 expr_id: ExprId,
490) -> Resolver { 488) -> Resolver {
@@ -493,7 +491,7 @@ pub(crate) fn resolver_for_expr(
493} 491}
494 492
495pub(crate) fn resolver_for_scope( 493pub(crate) fn resolver_for_scope(
496 db: &impl HirDatabase, 494 db: &impl DefDatabase2,
497 owner: DefWithBodyId, 495 owner: DefWithBodyId,
498 scope_id: Option<ScopeId>, 496 scope_id: Option<ScopeId>,
499) -> Resolver { 497) -> Resolver {
@@ -623,15 +621,3 @@ impl HasResolver for ImplId {
623 .push_impl_block_scope(self) 621 .push_impl_block_scope(self)
624 } 622 }
625} 623}
626
627impl HasResolver for GenericDef {
628 fn resolver(self, db: &impl DefDatabase2) -> Resolver {
629 GenericDefId::from(self).resolver(db)
630 }
631}
632
633impl HasResolver for DefWithBody {
634 fn resolver(self, db: &impl DefDatabase2) -> Resolver {
635 DefWithBodyId::from(self).resolver(db)
636 }
637}
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs
index f13d43740..898b823c0 100644
--- a/crates/ra_hir/src/source_binder.rs
+++ b/crates/ra_hir/src/source_binder.rs
@@ -10,6 +10,7 @@ use std::sync::Arc;
10use hir_def::{ 10use hir_def::{
11 expr::{ExprId, PatId}, 11 expr::{ExprId, PatId},
12 path::known, 12 path::known,
13 DefWithBodyId,
13}; 14};
14use hir_expand::{name::AsName, AstId, MacroCallId, MacroCallLoc, MacroFileKind, Source}; 15use hir_expand::{name::AsName, AstId, MacroCallId, MacroCallLoc, MacroFileKind, Source};
15use ra_syntax::{ 16use ra_syntax::{
@@ -51,7 +52,9 @@ fn try_get_resolver_for_node(db: &impl HirDatabase, node: Source<&SyntaxNode>) -
51 }, 52 },
52 _ => match node.value.kind() { 53 _ => match node.value.kind() {
53 FN_DEF | CONST_DEF | STATIC_DEF => { 54 FN_DEF | CONST_DEF | STATIC_DEF => {
54 Some(def_with_body_from_child_node(db, node)?.resolver(db)) 55 let def = def_with_body_from_child_node(db, node)?;
56 let def = DefWithBodyId::from(def);
57 Some(def.resolver(db))
55 } 58 }
56 // FIXME add missing cases 59 // FIXME add missing cases
57 _ => None 60 _ => None
@@ -232,7 +235,7 @@ impl SourceAnalyzer {
232 ) -> Option<MacroDef> { 235 ) -> Option<MacroDef> {
233 // This must be a normal source file rather than macro file. 236 // This must be a normal source file rather than macro file.
234 let path = macro_call.path().and_then(Path::from_ast)?; 237 let path = macro_call.path().and_then(Path::from_ast)?;
235 self.resolver.resolve_path_as_macro(db, &path) 238 self.resolver.resolve_path_as_macro(db, &path).map(|it| it.into())
236 } 239 }
237 240
238 pub fn resolve_hir_path( 241 pub fn resolve_hir_path(
@@ -275,7 +278,9 @@ impl SourceAnalyzer {
275 .take_types() 278 .take_types()
276 .map(|it| PathResolution::Def(it.into())); 279 .map(|it| PathResolution::Def(it.into()));
277 types.or(values).or(items).or_else(|| { 280 types.or(values).or(items).or_else(|| {
278 self.resolver.resolve_path_as_macro(db, &path).map(|def| PathResolution::Macro(def)) 281 self.resolver
282 .resolve_path_as_macro(db, &path)
283 .map(|def| PathResolution::Macro(def.into()))
279 }) 284 })
280 } 285 }
281 286
@@ -420,7 +425,7 @@ impl SourceAnalyzer {
420 }; 425 };
421 426
422 let canonical_ty = crate::ty::Canonical { value: ty, num_vars: 0 }; 427 let canonical_ty = crate::ty::Canonical { value: ty, num_vars: 0 };
423 implements_trait(&canonical_ty, db, &self.resolver, krate, std_future_trait) 428 implements_trait(&canonical_ty, db, &self.resolver, krate.into(), std_future_trait)
424 } 429 }
425 430
426 pub fn expand( 431 pub fn expand(
diff --git a/crates/ra_hir/src/ty/autoderef.rs b/crates/ra_hir/src/ty/autoderef.rs
index 872a4517d..f77492170 100644
--- a/crates/ra_hir/src/ty/autoderef.rs
+++ b/crates/ra_hir/src/ty/autoderef.rs
@@ -39,7 +39,7 @@ fn deref_by_trait(
39 ty: &Canonical<Ty>, 39 ty: &Canonical<Ty>,
40) -> Option<Canonical<Ty>> { 40) -> Option<Canonical<Ty>> {
41 let krate = resolver.krate()?; 41 let krate = resolver.krate()?;
42 let deref_trait = match db.lang_item(krate, "deref".into())? { 42 let deref_trait = match db.lang_item(krate.into(), "deref".into())? {
43 crate::lang_item::LangItemTarget::Trait(t) => t, 43 crate::lang_item::LangItemTarget::Trait(t) => t,
44 _ => return None, 44 _ => return None,
45 }; 45 };
@@ -71,7 +71,7 @@ fn deref_by_trait(
71 71
72 let canonical = super::Canonical { num_vars: 1 + ty.num_vars, value: in_env }; 72 let canonical = super::Canonical { num_vars: 1 + ty.num_vars, value: in_env };
73 73
74 let solution = db.trait_solve(krate, canonical)?; 74 let solution = db.trait_solve(krate.into(), canonical)?;
75 75
76 match &solution { 76 match &solution {
77 Solution::Unique(vars) => { 77 Solution::Unique(vars) => {
diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs
index 684d66946..c3d65afa6 100644
--- a/crates/ra_hir/src/ty/infer.rs
+++ b/crates/ra_hir/src/ty/infer.rs
@@ -24,7 +24,7 @@ use rustc_hash::FxHashMap;
24use hir_def::{ 24use hir_def::{
25 path::known, 25 path::known,
26 type_ref::{Mutability, TypeRef}, 26 type_ref::{Mutability, TypeRef},
27 AdtId, 27 AdtId, DefWithBodyId,
28}; 28};
29use hir_expand::{diagnostics::DiagnosticSink, name}; 29use hir_expand::{diagnostics::DiagnosticSink, name};
30use ra_arena::map::ArenaMap; 30use ra_arena::map::ArenaMap;
@@ -65,7 +65,7 @@ mod coerce;
65/// The entry point of type inference. 65/// The entry point of type inference.
66pub fn infer_query(db: &impl HirDatabase, def: DefWithBody) -> Arc<InferenceResult> { 66pub fn infer_query(db: &impl HirDatabase, def: DefWithBody) -> Arc<InferenceResult> {
67 let _p = profile("infer_query"); 67 let _p = profile("infer_query");
68 let resolver = def.resolver(db); 68 let resolver = DefWithBodyId::from(def).resolver(db);
69 let mut ctx = InferenceContext::new(db, def, resolver); 69 let mut ctx = InferenceContext::new(db, def, resolver);
70 70
71 match def { 71 match def {
@@ -378,8 +378,9 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
378 for obligation in obligations { 378 for obligation in obligations {
379 let in_env = InEnvironment::new(self.trait_env.clone(), obligation.clone()); 379 let in_env = InEnvironment::new(self.trait_env.clone(), obligation.clone());
380 let canonicalized = self.canonicalizer().canonicalize_obligation(in_env); 380 let canonicalized = self.canonicalizer().canonicalize_obligation(in_env);
381 let solution = 381 let solution = self
382 self.db.trait_solve(self.resolver.krate().unwrap(), canonicalized.value.clone()); 382 .db
383 .trait_solve(self.resolver.krate().unwrap().into(), canonicalized.value.clone());
383 384
384 match solution { 385 match solution {
385 Some(Solution::Unique(substs)) => { 386 Some(Solution::Unique(substs)) => {
diff --git a/crates/ra_hir/src/ty/infer/coerce.rs b/crates/ra_hir/src/ty/infer/coerce.rs
index 6ea135126..6d297c268 100644
--- a/crates/ra_hir/src/ty/infer/coerce.rs
+++ b/crates/ra_hir/src/ty/infer/coerce.rs
@@ -49,8 +49,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
49 resolver: &Resolver, 49 resolver: &Resolver,
50 ) -> FxHashMap<(TypeCtor, TypeCtor), usize> { 50 ) -> FxHashMap<(TypeCtor, TypeCtor), usize> {
51 let krate = resolver.krate().unwrap(); 51 let krate = resolver.krate().unwrap();
52 let impls = match db.lang_item(krate, "coerce_unsized".into()) { 52 let impls = match db.lang_item(krate.into(), "coerce_unsized".into()) {
53 Some(LangItemTarget::Trait(trait_)) => db.impls_for_trait(krate, trait_), 53 Some(LangItemTarget::Trait(trait_)) => db.impls_for_trait(krate.into(), trait_),
54 _ => return FxHashMap::default(), 54 _ => return FxHashMap::default(),
55 }; 55 };
56 56
diff --git a/crates/ra_hir/src/ty/lower.rs b/crates/ra_hir/src/ty/lower.rs
index 8ea2806dd..e477b2439 100644
--- a/crates/ra_hir/src/ty/lower.rs
+++ b/crates/ra_hir/src/ty/lower.rs
@@ -12,6 +12,7 @@ use hir_def::{
12 builtin_type::{BuiltinFloat, BuiltinInt, BuiltinType}, 12 builtin_type::{BuiltinFloat, BuiltinInt, BuiltinType},
13 path::{GenericArg, PathSegment}, 13 path::{GenericArg, PathSegment},
14 type_ref::{TypeBound, TypeRef}, 14 type_ref::{TypeBound, TypeRef},
15 GenericDefId,
15}; 16};
16 17
17use super::{ 18use super::{
@@ -574,7 +575,7 @@ pub(crate) fn generic_predicates_for_param_query(
574 def: GenericDef, 575 def: GenericDef,
575 param_idx: u32, 576 param_idx: u32,
576) -> Arc<[GenericPredicate]> { 577) -> Arc<[GenericPredicate]> {
577 let resolver = def.resolver(db); 578 let resolver = GenericDefId::from(def).resolver(db);
578 resolver 579 resolver
579 .where_predicates_in_scope() 580 .where_predicates_in_scope()
580 // we have to filter out all other predicates *first*, before attempting to lower them 581 // we have to filter out all other predicates *first*, before attempting to lower them
@@ -600,7 +601,7 @@ pub(crate) fn generic_predicates_query(
600 db: &impl HirDatabase, 601 db: &impl HirDatabase,
601 def: GenericDef, 602 def: GenericDef,
602) -> Arc<[GenericPredicate]> { 603) -> Arc<[GenericPredicate]> {
603 let resolver = def.resolver(db); 604 let resolver = GenericDefId::from(def).resolver(db);
604 resolver 605 resolver
605 .where_predicates_in_scope() 606 .where_predicates_in_scope()
606 .flat_map(|pred| GenericPredicate::from_where_predicate(db, &resolver, pred)) 607 .flat_map(|pred| GenericPredicate::from_where_predicate(db, &resolver, pred))
@@ -609,7 +610,7 @@ pub(crate) fn generic_predicates_query(
609 610
610/// Resolve the default type params from generics 611/// Resolve the default type params from generics
611pub(crate) fn generic_defaults_query(db: &impl HirDatabase, def: GenericDef) -> Substs { 612pub(crate) fn generic_defaults_query(db: &impl HirDatabase, def: GenericDef) -> Substs {
612 let resolver = def.resolver(db); 613 let resolver = GenericDefId::from(def).resolver(db);
613 let generic_params = def.generic_params(db); 614 let generic_params = def.generic_params(db);
614 615
615 let defaults = generic_params 616 let defaults = generic_params
diff --git a/crates/ra_hir/src/ty/method_resolution.rs b/crates/ra_hir/src/ty/method_resolution.rs
index 26dd06171..5ad72ef9f 100644
--- a/crates/ra_hir/src/ty/method_resolution.rs
+++ b/crates/ra_hir/src/ty/method_resolution.rs
@@ -172,9 +172,14 @@ pub(crate) fn iterate_method_candidates<T>(
172 // rustc does an autoderef and then autoref again). 172 // rustc does an autoderef and then autoref again).
173 173
174 for derefed_ty in autoderef::autoderef(db, resolver, ty.clone()) { 174 for derefed_ty in autoderef::autoderef(db, resolver, ty.clone()) {
175 if let Some(result) = 175 if let Some(result) = iterate_inherent_methods(
176 iterate_inherent_methods(&derefed_ty, db, name, mode, krate, &mut callback) 176 &derefed_ty,
177 { 177 db,
178 name,
179 mode,
180 krate.into(),
181 &mut callback,
182 ) {
178 return Some(result); 183 return Some(result);
179 } 184 }
180 if let Some(result) = iterate_trait_method_candidates( 185 if let Some(result) = iterate_trait_method_candidates(
@@ -192,7 +197,7 @@ pub(crate) fn iterate_method_candidates<T>(
192 LookupMode::Path => { 197 LookupMode::Path => {
193 // No autoderef for path lookups 198 // No autoderef for path lookups
194 if let Some(result) = 199 if let Some(result) =
195 iterate_inherent_methods(&ty, db, name, mode, krate, &mut callback) 200 iterate_inherent_methods(&ty, db, name, mode, krate.into(), &mut callback)
196 { 201 {
197 return Some(result); 202 return Some(result);
198 } 203 }
@@ -240,7 +245,7 @@ fn iterate_trait_method_candidates<T>(
240 } 245 }
241 if !known_implemented { 246 if !known_implemented {
242 let goal = generic_implements_goal(db, env.clone(), t, ty.clone()); 247 let goal = generic_implements_goal(db, env.clone(), t, ty.clone());
243 if db.trait_solve(krate, goal).is_none() { 248 if db.trait_solve(krate.into(), goal).is_none() {
244 continue 'traits; 249 continue 'traits;
245 } 250 }
246 } 251 }