From 52e7f67128fdaf604e4563eb6445fdd69d42c91e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 14 Jan 2020 11:27:00 +0100 Subject: Move impls_future to Type, where it belongs --- crates/ra_hir/src/code_model.rs | 20 ++++++++++++++++++-- crates/ra_hir/src/source_analyzer.rs | 24 +----------------------- crates/ra_hir_ty/src/method_resolution.rs | 3 +-- crates/ra_ide/src/completion/complete_dot.rs | 2 +- 4 files changed, 21 insertions(+), 28 deletions(-) diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index a177cebca..346118350 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, ApplicationTy, Canonical, InEnvironment, - TraitEnvironment, Ty, TyDefId, TypeCtor, TypeWalk, + autoderef, display::HirFormatter, expr::ExprValidator, method_resolution::implements_trait, + ApplicationTy, Canonical, InEnvironment, TraitEnvironment, Ty, TyDefId, TypeCtor, TypeWalk, }; use ra_db::{CrateId, Edition, FileId}; use ra_prof::profile; @@ -878,6 +878,22 @@ impl Type { } } + /// Checks that particular type `ty` implements `std::future::Future`. + /// This function is used in `.await` syntax completion. + pub fn impls_future(&self, db: &impl HirDatabase) -> bool { + let krate = self.krate; + + let std_future_trait = + db.lang_item(krate, "future_trait".into()).and_then(|it| it.as_trait()); + let std_future_trait = match std_future_trait { + Some(it) => it, + None => return false, + }; + + 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) + } + // FIXME: this method is broken, as it doesn't take closures into account. pub fn as_callable(&self) -> Option { Some(self.ty.value.as_callable()?.0) diff --git a/crates/ra_hir/src/source_analyzer.rs b/crates/ra_hir/src/source_analyzer.rs index bd5ef1466..3df48842d 100644 --- a/crates/ra_hir/src/source_analyzer.rs +++ b/crates/ra_hir/src/source_analyzer.rs @@ -21,10 +21,7 @@ use hir_def::{ use hir_expand::{ hygiene::Hygiene, name::AsName, AstId, HirFileId, InFile, MacroCallId, MacroCallKind, }; -use hir_ty::{ - method_resolution::{self, implements_trait}, - Canonical, InEnvironment, InferenceResult, TraitEnvironment, Ty, -}; +use hir_ty::{method_resolution, Canonical, InEnvironment, InferenceResult, TraitEnvironment, Ty}; use ra_prof::profile; use ra_syntax::{ ast::{self, AstNode}, @@ -395,25 +392,6 @@ impl SourceAnalyzer { ) } - /// Checks that particular type `ty` implements `std::future::Future`. - /// This function is used in `.await` syntax completion. - pub fn impls_future(&self, db: &impl HirDatabase, ty: Type) -> bool { - let krate = match self.resolver.krate() { - Some(krate) => krate, - None => return false, - }; - - let std_future_trait = - db.lang_item(krate, "future_trait".into()).and_then(|it| it.as_trait()); - let std_future_trait = match std_future_trait { - Some(it) => it, - None => return false, - }; - - let canonical_ty = Canonical { value: ty.ty.value, num_vars: 0 }; - implements_trait(&canonical_ty, db, &self.resolver, krate, std_future_trait) - } - pub fn expand( &self, db: &impl HirDatabase, diff --git a/crates/ra_hir_ty/src/method_resolution.rs b/crates/ra_hir_ty/src/method_resolution.rs index 888dc3116..55435e6ea 100644 --- a/crates/ra_hir_ty/src/method_resolution.rs +++ b/crates/ra_hir_ty/src/method_resolution.rs @@ -465,7 +465,7 @@ fn transform_receiver_ty( pub fn implements_trait( ty: &Canonical, db: &impl HirDatabase, - resolver: &Resolver, + env: Arc, krate: CrateId, trait_: TraitId, ) -> bool { @@ -474,7 +474,6 @@ pub fn implements_trait( // anyway, but currently Chalk doesn't implement `dyn/impl Trait` yet return true; } - let env = TraitEnvironment::lower(db, resolver); let goal = generic_implements_goal(db, env, trait_, ty.clone()); let solution = db.trait_solve(krate.into(), goal); diff --git a/crates/ra_ide/src/completion/complete_dot.rs b/crates/ra_ide/src/completion/complete_dot.rs index fe02e36b3..9ab43644e 100644 --- a/crates/ra_ide/src/completion/complete_dot.rs +++ b/crates/ra_ide/src/completion/complete_dot.rs @@ -27,7 +27,7 @@ pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) { complete_methods(acc, ctx, &receiver_ty); // Suggest .await syntax for types that implement Future trait - if ctx.analyzer.impls_future(ctx.db, receiver_ty) { + if receiver_ty.impls_future(ctx.db) { CompletionItem::new(CompletionKind::Keyword, ctx.source_range(), "await") .detail("expr.await") .insert_text("await") -- cgit v1.2.3