Move impls_future to Type, where it belongs

This commit is contained in:
Aleksey Kladov 2020-01-14 11:27:00 +01:00
parent 0358f5fdeb
commit 52e7f67128
4 changed files with 21 additions and 28 deletions

View File

@ -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<CallableDef> {
Some(self.ty.value.as_callable()?.0)

View File

@ -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,

View File

@ -465,7 +465,7 @@ fn transform_receiver_ty(
pub fn implements_trait(
ty: &Canonical<Ty>,
db: &impl HirDatabase,
resolver: &Resolver,
env: Arc<TraitEnvironment>,
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);

View File

@ -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")