From 7ec62ea5e686dcb6de5d3fcc413f32af9d1c9cb5 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 14 Jan 2020 14:11:07 +0100 Subject: Push resolver up --- crates/ra_hir_ty/src/method_resolution.rs | 72 ++++++++++++++++++++++--------- 1 file changed, 51 insertions(+), 21 deletions(-) (limited to 'crates/ra_hir_ty/src') diff --git a/crates/ra_hir_ty/src/method_resolution.rs b/crates/ra_hir_ty/src/method_resolution.rs index 55435e6ea..e43fadf20 100644 --- a/crates/ra_hir_ty/src/method_resolution.rs +++ b/crates/ra_hir_ty/src/method_resolution.rs @@ -12,7 +12,7 @@ use hir_def::{ use hir_expand::name::Name; use ra_db::CrateId; use ra_prof::profile; -use rustc_hash::FxHashMap; +use rustc_hash::{FxHashMap, FxHashSet}; use super::Substs; use crate::{ @@ -177,6 +177,9 @@ pub fn iterate_method_candidates( mode: LookupMode, mut callback: impl FnMut(&Ty, AssocItemId) -> Option, ) -> Option { + let traits_in_scope = resolver.traits_in_scope(db); + let krate = resolver.krate()?; + let env = TraitEnvironment::lower(db, resolver); match mode { LookupMode::MethodCall => { // For method calls, rust first does any number of autoderef, and then one @@ -209,7 +212,9 @@ pub fn iterate_method_candidates( if let Some(result) = iterate_method_candidates_with_autoref( &deref_chain[i..], db, - resolver, + env.clone(), + krate, + &traits_in_scope, name, &mut callback, ) { @@ -220,7 +225,15 @@ pub fn iterate_method_candidates( } LookupMode::Path => { // No autoderef for path lookups - iterate_method_candidates_for_self_ty(&ty, db, resolver, name, &mut callback) + iterate_method_candidates_for_self_ty( + &ty, + db, + env, + krate, + &traits_in_scope, + name, + &mut callback, + ) } } } @@ -228,7 +241,9 @@ pub fn iterate_method_candidates( fn iterate_method_candidates_with_autoref( deref_chain: &[Canonical], db: &impl HirDatabase, - resolver: &Resolver, + env: Arc, + krate: CrateId, + traits_in_scope: &FxHashSet, name: Option<&Name>, mut callback: impl FnMut(&Ty, AssocItemId) -> Option, ) -> Option { @@ -236,7 +251,9 @@ fn iterate_method_candidates_with_autoref( &deref_chain[0], &deref_chain[1..], db, - resolver, + env.clone(), + krate, + &traits_in_scope, name, &mut callback, ) { @@ -250,7 +267,9 @@ fn iterate_method_candidates_with_autoref( &refed, deref_chain, db, - resolver, + env.clone(), + krate, + &traits_in_scope, name, &mut callback, ) { @@ -264,7 +283,9 @@ fn iterate_method_candidates_with_autoref( &ref_muted, deref_chain, db, - resolver, + env.clone(), + krate, + &traits_in_scope, name, &mut callback, ) { @@ -277,14 +298,15 @@ fn iterate_method_candidates_by_receiver( receiver_ty: &Canonical, rest_of_deref_chain: &[Canonical], db: &impl HirDatabase, - resolver: &Resolver, + env: Arc, + krate: CrateId, + traits_in_scope: &FxHashSet, name: Option<&Name>, mut callback: impl FnMut(&Ty, AssocItemId) -> Option, ) -> Option { // We're looking for methods with *receiver* type receiver_ty. These could // be found in any of the derefs of receiver_ty, so we have to go through // that. - let krate = resolver.krate()?; for self_ty in std::iter::once(receiver_ty).chain(rest_of_deref_chain) { if let Some(result) = iterate_inherent_methods(self_ty, db, name, Some(receiver_ty), krate, &mut callback) @@ -296,7 +318,9 @@ fn iterate_method_candidates_by_receiver( if let Some(result) = iterate_trait_method_candidates( self_ty, db, - resolver, + env.clone(), + krate, + &traits_in_scope, name, Some(receiver_ty), &mut callback, @@ -310,17 +334,25 @@ fn iterate_method_candidates_by_receiver( fn iterate_method_candidates_for_self_ty( self_ty: &Canonical, db: &impl HirDatabase, - resolver: &Resolver, + env: Arc, + krate: CrateId, + traits_in_scope: &FxHashSet, name: Option<&Name>, mut callback: impl FnMut(&Ty, AssocItemId) -> Option, ) -> Option { - let krate = resolver.krate()?; if let Some(result) = iterate_inherent_methods(self_ty, db, name, None, krate, &mut callback) { return Some(result); } - if let Some(result) = - iterate_trait_method_candidates(self_ty, db, resolver, name, None, &mut callback) - { + if let Some(result) = iterate_trait_method_candidates( + self_ty, + db, + env, + krate, + traits_in_scope, + name, + None, + &mut callback, + ) { return Some(result); } None @@ -329,14 +361,13 @@ fn iterate_method_candidates_for_self_ty( fn iterate_trait_method_candidates( self_ty: &Canonical, db: &impl HirDatabase, - resolver: &Resolver, + env: Arc, + krate: CrateId, + traits_in_scope: &FxHashSet, name: Option<&Name>, receiver_ty: Option<&Canonical>, mut callback: impl FnMut(&Ty, AssocItemId) -> Option, ) -> Option { - let krate = resolver.krate()?; - // FIXME: maybe put the trait_env behind a query (need to figure out good input parameters for that) - let env = TraitEnvironment::lower(db, resolver); // if ty is `impl Trait` or `dyn Trait`, the trait doesn't need to be in scope let inherent_trait = self_ty.value.inherent_trait().into_iter(); // if we have `T: Trait` in the param env, the trait doesn't need to be in scope @@ -344,8 +375,7 @@ fn iterate_trait_method_candidates( .trait_predicates_for_self_ty(&self_ty.value) .map(|tr| tr.trait_) .flat_map(|t| all_super_traits(db, t)); - let traits = - inherent_trait.chain(traits_from_env).chain(resolver.traits_in_scope(db).into_iter()); + let traits = inherent_trait.chain(traits_from_env).chain(traits_in_scope.iter().copied()); 'traits: for t in traits { let data = db.trait_data(t); -- cgit v1.2.3 From 21c5fd8b1b8f9536449325738baea1e48efdefe0 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 14 Jan 2020 14:20:33 +0100 Subject: Push resolver further up --- crates/ra_hir_ty/src/infer/expr.rs | 19 +++++++++----- crates/ra_hir_ty/src/infer/path.rs | 9 +++++-- crates/ra_hir_ty/src/method_resolution.rs | 41 ++++++++++++++++++------------- 3 files changed, 44 insertions(+), 25 deletions(-) (limited to 'crates/ra_hir_ty/src') diff --git a/crates/ra_hir_ty/src/infer/expr.rs b/crates/ra_hir_ty/src/infer/expr.rs index 3af05394c..d6a17e469 100644 --- a/crates/ra_hir_ty/src/infer/expr.rs +++ b/crates/ra_hir_ty/src/infer/expr.rs @@ -569,12 +569,19 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { ) -> Ty { let receiver_ty = self.infer_expr(receiver, &Expectation::none()); let canonicalized_receiver = self.canonicalizer().canonicalize_ty(receiver_ty.clone()); - let resolved = method_resolution::lookup_method( - &canonicalized_receiver.value, - self.db, - method_name, - &self.resolver, - ); + + let traits_in_scope = self.resolver.traits_in_scope(self.db); + + let resolved = self.resolver.krate().and_then(|krate| { + method_resolution::lookup_method( + &canonicalized_receiver.value, + self.db, + self.trait_env.clone(), + krate, + &traits_in_scope, + method_name, + ) + }); let (derefed_receiver_ty, method_ty, def_generics) = match resolved { Some((ty, func)) => { let ty = canonicalized_receiver.decanonicalize_ty(ty); diff --git a/crates/ra_hir_ty/src/infer/path.rs b/crates/ra_hir_ty/src/infer/path.rs index ffd358367..2c1d4831d 100644 --- a/crates/ra_hir_ty/src/infer/path.rs +++ b/crates/ra_hir_ty/src/infer/path.rs @@ -11,7 +11,7 @@ use hir_expand::name::Name; use crate::{db::HirDatabase, method_resolution, Substs, Ty, TypeWalk, ValueTyDefId}; -use super::{ExprOrPatId, InferenceContext, TraitRef}; +use super::{ExprOrPatId, InferenceContext, TraitEnvironment, TraitRef}; impl<'a, D: HirDatabase> InferenceContext<'a, D> { pub(super) fn infer_path( @@ -193,11 +193,16 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { } let canonical_ty = self.canonicalizer().canonicalize_ty(ty.clone()); + let env = TraitEnvironment::lower(self.db, &self.resolver); + let krate = self.resolver.krate()?; + let traits_in_scope = self.resolver.traits_in_scope(self.db); method_resolution::iterate_method_candidates( &canonical_ty.value, self.db, - &self.resolver.clone(), + env, + krate, + &traits_in_scope, Some(name), method_resolution::LookupMode::Path, move |_ty, item| { diff --git a/crates/ra_hir_ty/src/method_resolution.rs b/crates/ra_hir_ty/src/method_resolution.rs index e43fadf20..5bacbbd7c 100644 --- a/crates/ra_hir_ty/src/method_resolution.rs +++ b/crates/ra_hir_ty/src/method_resolution.rs @@ -6,8 +6,8 @@ use std::sync::Arc; use arrayvec::ArrayVec; use hir_def::{ - lang_item::LangItemTarget, resolver::Resolver, type_ref::Mutability, AssocContainerId, - AssocItemId, FunctionId, HasModule, ImplId, Lookup, TraitId, + lang_item::LangItemTarget, type_ref::Mutability, AssocContainerId, AssocItemId, FunctionId, + HasModule, ImplId, Lookup, TraitId, }; use hir_expand::name::Name; use ra_db::CrateId; @@ -144,14 +144,24 @@ impl Ty { pub(crate) fn lookup_method( ty: &Canonical, db: &impl HirDatabase, + env: Arc, + krate: CrateId, + traits_in_scope: &FxHashSet, name: &Name, - resolver: &Resolver, ) -> Option<(Ty, FunctionId)> { - iterate_method_candidates(ty, db, resolver, Some(name), LookupMode::MethodCall, |ty, f| match f - { - AssocItemId::FunctionId(f) => Some((ty.clone(), f)), - _ => None, - }) + iterate_method_candidates( + ty, + db, + env, + krate, + &traits_in_scope, + Some(name), + LookupMode::MethodCall, + |ty, f| match f { + AssocItemId::FunctionId(f) => Some((ty.clone(), f)), + _ => None, + }, + ) } /// Whether we're looking up a dotted method call (like `v.len()`) or a path @@ -172,14 +182,13 @@ pub enum LookupMode { pub fn iterate_method_candidates( ty: &Canonical, db: &impl HirDatabase, - resolver: &Resolver, + env: Arc, + krate: CrateId, + traits_in_scope: &FxHashSet, name: Option<&Name>, mode: LookupMode, mut callback: impl FnMut(&Ty, AssocItemId) -> Option, ) -> Option { - let traits_in_scope = resolver.traits_in_scope(db); - let krate = resolver.krate()?; - let env = TraitEnvironment::lower(db, resolver); match mode { LookupMode::MethodCall => { // For method calls, rust first does any number of autoderef, and then one @@ -190,9 +199,7 @@ pub fn iterate_method_candidates( // Also note that when we've got a receiver like &S, even if the method we // find in the end takes &self, we still do the autoderef step (just as // rustc does an autoderef and then autoref again). - let environment = TraitEnvironment::lower(db, resolver); - let ty = InEnvironment { value: ty.clone(), environment }; - let krate = resolver.krate()?; + let ty = InEnvironment { value: ty.clone(), environment: env.clone() }; // We have to be careful about the order we're looking at candidates // in here. Consider the case where we're resolving `x.clone()` @@ -214,7 +221,7 @@ pub fn iterate_method_candidates( db, env.clone(), krate, - &traits_in_scope, + traits_in_scope, name, &mut callback, ) { @@ -230,7 +237,7 @@ pub fn iterate_method_candidates( db, env, krate, - &traits_in_scope, + traits_in_scope, name, &mut callback, ) -- cgit v1.2.3