mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-29 05:15:04 +00:00
Move impls_future to Type, where it belongs
This commit is contained in:
parent
0358f5fdeb
commit
52e7f67128
4 changed files with 21 additions and 28 deletions
|
@ -21,8 +21,8 @@ use hir_expand::{
|
||||||
MacroDefId,
|
MacroDefId,
|
||||||
};
|
};
|
||||||
use hir_ty::{
|
use hir_ty::{
|
||||||
autoderef, display::HirFormatter, expr::ExprValidator, ApplicationTy, Canonical, InEnvironment,
|
autoderef, display::HirFormatter, expr::ExprValidator, method_resolution::implements_trait,
|
||||||
TraitEnvironment, Ty, TyDefId, TypeCtor, TypeWalk,
|
ApplicationTy, Canonical, InEnvironment, TraitEnvironment, Ty, TyDefId, TypeCtor, TypeWalk,
|
||||||
};
|
};
|
||||||
use ra_db::{CrateId, Edition, FileId};
|
use ra_db::{CrateId, Edition, FileId};
|
||||||
use ra_prof::profile;
|
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.
|
// FIXME: this method is broken, as it doesn't take closures into account.
|
||||||
pub fn as_callable(&self) -> Option<CallableDef> {
|
pub fn as_callable(&self) -> Option<CallableDef> {
|
||||||
Some(self.ty.value.as_callable()?.0)
|
Some(self.ty.value.as_callable()?.0)
|
||||||
|
|
|
@ -21,10 +21,7 @@ use hir_def::{
|
||||||
use hir_expand::{
|
use hir_expand::{
|
||||||
hygiene::Hygiene, name::AsName, AstId, HirFileId, InFile, MacroCallId, MacroCallKind,
|
hygiene::Hygiene, name::AsName, AstId, HirFileId, InFile, MacroCallId, MacroCallKind,
|
||||||
};
|
};
|
||||||
use hir_ty::{
|
use hir_ty::{method_resolution, Canonical, InEnvironment, InferenceResult, TraitEnvironment, Ty};
|
||||||
method_resolution::{self, implements_trait},
|
|
||||||
Canonical, InEnvironment, InferenceResult, TraitEnvironment, Ty,
|
|
||||||
};
|
|
||||||
use ra_prof::profile;
|
use ra_prof::profile;
|
||||||
use ra_syntax::{
|
use ra_syntax::{
|
||||||
ast::{self, AstNode},
|
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(
|
pub fn expand(
|
||||||
&self,
|
&self,
|
||||||
db: &impl HirDatabase,
|
db: &impl HirDatabase,
|
||||||
|
|
|
@ -465,7 +465,7 @@ fn transform_receiver_ty(
|
||||||
pub fn implements_trait(
|
pub fn implements_trait(
|
||||||
ty: &Canonical<Ty>,
|
ty: &Canonical<Ty>,
|
||||||
db: &impl HirDatabase,
|
db: &impl HirDatabase,
|
||||||
resolver: &Resolver,
|
env: Arc<TraitEnvironment>,
|
||||||
krate: CrateId,
|
krate: CrateId,
|
||||||
trait_: TraitId,
|
trait_: TraitId,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
|
@ -474,7 +474,6 @@ pub fn implements_trait(
|
||||||
// anyway, but currently Chalk doesn't implement `dyn/impl Trait` yet
|
// anyway, but currently Chalk doesn't implement `dyn/impl Trait` yet
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
let env = TraitEnvironment::lower(db, resolver);
|
|
||||||
let goal = generic_implements_goal(db, env, trait_, ty.clone());
|
let goal = generic_implements_goal(db, env, trait_, ty.clone());
|
||||||
let solution = db.trait_solve(krate.into(), goal);
|
let solution = db.trait_solve(krate.into(), goal);
|
||||||
|
|
||||||
|
|
|
@ -27,7 +27,7 @@ pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) {
|
||||||
complete_methods(acc, ctx, &receiver_ty);
|
complete_methods(acc, ctx, &receiver_ty);
|
||||||
|
|
||||||
// Suggest .await syntax for types that implement Future trait
|
// 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")
|
CompletionItem::new(CompletionKind::Keyword, ctx.source_range(), "await")
|
||||||
.detail("expr.await")
|
.detail("expr.await")
|
||||||
.insert_text("await")
|
.insert_text("await")
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue