Add hir::Function::async_ret_type method

Adjust completion detail for `async fn` return types
This commit is contained in:
iDawer 2022-04-14 17:44:17 +05:00
parent f972adc201
commit 9d787e1bfe
3 changed files with 21 additions and 3 deletions

View file

@ -1364,6 +1364,23 @@ impl Function {
Type::new_with_resolver_inner(db, &resolver, ty)
}
pub fn async_ret_type(self, db: &dyn HirDatabase) -> Option<Type> {
if !self.is_async(db) {
return None;
}
let resolver = self.id.resolver(db.upcast());
let substs = TyBuilder::placeholder_subst(db, self.id);
let callable_sig = db.callable_item_signature(self.id.into()).substitute(Interner, &substs);
let ret_ty = callable_sig.ret().clone();
for pred in ret_ty.impl_trait_bounds(db).into_iter().flatten() {
if let WhereClause::AliasEq(output_eq) = pred.into_value_and_skipped_binders().0 {
return Type::new_with_resolver_inner(db, &resolver, output_eq.ty).into();
}
}
never!("Async fn ret_type should be impl Future");
None
}
pub fn self_param(self, db: &dyn HirDatabase) -> Option<SelfParam> {
if !db.function_data(self.id).has_self_param() {
return None;

View file

@ -228,7 +228,7 @@ fn should_add_parens(ctx: &CompletionContext) -> bool {
}
fn detail(db: &dyn HirDatabase, func: hir::Function) -> String {
let ret_ty = func.ret_type(db);
let ret_ty = func.async_ret_type(db).unwrap_or_else(|| func.ret_type(db));
let mut detail = String::new();
if func.is_const(db) {

View file

@ -624,12 +624,12 @@ fn main() {
#[test]
fn detail_async_fn() {
// FIXME: #11438
check_empty(
r#"
//- minicore: future, sized
trait Trait<T> {}
async fn foo() -> u8 {}
async fn bar<U>() -> impl Trait<U> {}
fn main() {
self::$0
}
@ -637,7 +637,8 @@ fn main() {
expect![[r"
tt Trait
fn main() fn()
fn foo() async fn() -> impl Future<Output = u8>
fn bar() async fn() -> impl Trait<U>
fn foo() async fn() -> u8
"]],
);
}