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') 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