From a38540771fa93994c369d53a2abc01769c64c0b8 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 14 Jan 2020 14:42:52 +0100 Subject: Move Type API to type --- crates/ra_hir/src/code_model.rs | 74 ++++++++++++++++++++++++++++++++++-- crates/ra_hir/src/source_analyzer.rs | 70 ++++------------------------------ 2 files changed, 78 insertions(+), 66 deletions(-) (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 346118350..3b479356f 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -21,8 +21,8 @@ use hir_expand::{ MacroDefId, }; use hir_ty::{ - autoderef, display::HirFormatter, expr::ExprValidator, method_resolution::implements_trait, - ApplicationTy, Canonical, InEnvironment, TraitEnvironment, Ty, TyDefId, TypeCtor, TypeWalk, + autoderef, display::HirFormatter, expr::ExprValidator, method_resolution, ApplicationTy, + Canonical, InEnvironment, TraitEnvironment, Ty, TyDefId, TypeCtor, TypeWalk, }; use ra_db::{CrateId, Edition, FileId}; use ra_prof::profile; @@ -120,7 +120,8 @@ impl_froms!( BuiltinType ); -pub use hir_def::{attr::Attrs, visibility::Visibility}; +pub use hir_def::{attr::Attrs, visibility::Visibility, AssocItemId}; +use rustc_hash::FxHashSet; impl Module { pub(crate) fn new(krate: Crate, crate_module_id: LocalModuleId) -> Module { @@ -891,7 +892,13 @@ impl Type { }; let canonical_ty = Canonical { value: self.ty.value.clone(), num_vars: 0 }; - implements_trait(&canonical_ty, db, self.ty.environment.clone(), krate, std_future_trait) + method_resolution::implements_trait( + &canonical_ty, + db, + self.ty.environment.clone(), + krate, + std_future_trait, + ) } // FIXME: this method is broken, as it doesn't take closures into account. @@ -1002,6 +1009,65 @@ impl Type { None } + pub fn iterate_method_candidates( + &self, + db: &impl HirDatabase, + krate: Crate, + traits_in_scope: &FxHashSet, + name: Option<&Name>, + mut callback: impl FnMut(&Ty, Function) -> Option, + ) -> Option { + // There should be no inference vars in types passed here + // FIXME check that? + // FIXME replace Unknown by bound vars here + let canonical = Canonical { value: self.ty.value.clone(), num_vars: 0 }; + + let env = self.ty.environment.clone(); + let krate = krate.id; + + method_resolution::iterate_method_candidates( + &canonical, + db, + env, + krate, + traits_in_scope, + name, + method_resolution::LookupMode::MethodCall, + |ty, it| match it { + AssocItemId::FunctionId(f) => callback(ty, f.into()), + _ => None, + }, + ) + } + + pub fn iterate_path_candidates( + &self, + db: &impl HirDatabase, + krate: Crate, + traits_in_scope: &FxHashSet, + name: Option<&Name>, + mut callback: impl FnMut(&Ty, AssocItem) -> Option, + ) -> Option { + // There should be no inference vars in types passed here + // FIXME check that? + // FIXME replace Unknown by bound vars here + let canonical = Canonical { value: self.ty.value.clone(), num_vars: 0 }; + + let env = self.ty.environment.clone(); + let krate = krate.id; + + method_resolution::iterate_method_candidates( + &canonical, + db, + env, + krate, + traits_in_scope, + name, + method_resolution::LookupMode::Path, + |ty, it| callback(ty, it.into()), + ) + } + pub fn as_adt(&self) -> Option { let (adt, _subst) = self.ty.value.as_adt()?; Some(adt.into()) diff --git a/crates/ra_hir/src/source_analyzer.rs b/crates/ra_hir/src/source_analyzer.rs index 5707a5696..76e0bff34 100644 --- a/crates/ra_hir/src/source_analyzer.rs +++ b/crates/ra_hir/src/source_analyzer.rs @@ -16,12 +16,12 @@ use hir_def::{ expr::{ExprId, PatId}, nameres::ModuleSource, resolver::{self, resolver_for_scope, HasResolver, Resolver, TypeNs, ValueNs}, - AssocItemId, DefWithBodyId, + DefWithBodyId, TraitId, }; use hir_expand::{ hygiene::Hygiene, name::AsName, AstId, HirFileId, InFile, MacroCallId, MacroCallKind, }; -use hir_ty::{method_resolution, Canonical, InEnvironment, InferenceResult, TraitEnvironment, Ty}; +use hir_ty::{InEnvironment, InferenceResult, TraitEnvironment}; use ra_prof::profile; use ra_syntax::{ ast::{self, AstNode}, @@ -29,11 +29,11 @@ use ra_syntax::{ SyntaxKind::*, SyntaxNode, SyntaxNodePtr, SyntaxToken, TextRange, TextUnit, }; +use rustc_hash::FxHashSet; use crate::{ - db::HirDatabase, Adt, AssocItem, Const, DefWithBody, Enum, EnumVariant, FromSource, Function, - ImplBlock, Local, MacroDef, Name, Path, ScopeDef, Static, Struct, Trait, Type, TypeAlias, - TypeParam, + db::HirDatabase, Adt, Const, DefWithBody, Enum, EnumVariant, FromSource, Function, ImplBlock, + Local, MacroDef, Name, Path, ScopeDef, Static, Struct, Trait, Type, TypeAlias, TypeParam, }; /// `SourceAnalyzer` is a convenience wrapper which exposes HIR API in terms of @@ -347,63 +347,9 @@ impl SourceAnalyzer { .collect() } - pub fn iterate_method_candidates( - &self, - db: &impl HirDatabase, - ty: &Type, - name: Option<&Name>, - mut callback: impl FnMut(&Ty, Function) -> Option, - ) -> Option { - // There should be no inference vars in types passed here - // FIXME check that? - // FIXME replace Unknown by bound vars here - let canonical = Canonical { value: ty.ty.value.clone(), num_vars: 0 }; - - let env = TraitEnvironment::lower(db, &self.resolver); - let krate = self.resolver.krate()?; - let traits_in_scope = self.resolver.traits_in_scope(db); - - method_resolution::iterate_method_candidates( - &canonical, - db, - env, - krate, - &traits_in_scope, - name, - method_resolution::LookupMode::MethodCall, - |ty, it| match it { - AssocItemId::FunctionId(f) => callback(ty, f.into()), - _ => None, - }, - ) - } - - pub fn iterate_path_candidates( - &self, - db: &impl HirDatabase, - ty: &Type, - name: Option<&Name>, - mut callback: impl FnMut(&Ty, AssocItem) -> Option, - ) -> Option { - // There should be no inference vars in types passed here - // FIXME check that? - // FIXME replace Unknown by bound vars here - let canonical = Canonical { value: ty.ty.value.clone(), num_vars: 0 }; - - let env = TraitEnvironment::lower(db, &self.resolver); - let krate = self.resolver.krate()?; - let traits_in_scope = self.resolver.traits_in_scope(db); - - method_resolution::iterate_method_candidates( - &canonical, - db, - env, - krate, - &traits_in_scope, - name, - method_resolution::LookupMode::Path, - |ty, it| callback(ty, it.into()), - ) + /// Note: `FxHashSet` should be treated as an opaque type, passed into `Type + pub fn traits_in_scope(&self, db: &impl HirDatabase) -> FxHashSet { + self.resolver.traits_in_scope(db) } pub fn expand( -- cgit v1.2.3