Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 9d787e1

Browse files
committed
Add hir::Function::async_ret_type method
Adjust completion detail for `async fn` return types
1 parent f972adc commit 9d787e1

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

crates/hir/src/lib.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,6 +1364,23 @@ impl Function {
13641364
Type::new_with_resolver_inner(db, &resolver, ty)
13651365
}
13661366

1367+
pub fn async_ret_type(self, db: &dyn HirDatabase) -> Option<Type> {
1368+
if !self.is_async(db) {
1369+
return None;
1370+
}
1371+
let resolver = self.id.resolver(db.upcast());
1372+
let substs = TyBuilder::placeholder_subst(db, self.id);
1373+
let callable_sig = db.callable_item_signature(self.id.into()).substitute(Interner, &substs);
1374+
let ret_ty = callable_sig.ret().clone();
1375+
for pred in ret_ty.impl_trait_bounds(db).into_iter().flatten() {
1376+
if let WhereClause::AliasEq(output_eq) = pred.into_value_and_skipped_binders().0 {
1377+
return Type::new_with_resolver_inner(db, &resolver, output_eq.ty).into();
1378+
}
1379+
}
1380+
never!("Async fn ret_type should be impl Future");
1381+
None
1382+
}
1383+
13671384
pub fn self_param(self, db: &dyn HirDatabase) -> Option<SelfParam> {
13681385
if !db.function_data(self.id).has_self_param() {
13691386
return None;

crates/ide_completion/src/render/function.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ fn should_add_parens(ctx: &CompletionContext) -> bool {
228228
}
229229

230230
fn detail(db: &dyn HirDatabase, func: hir::Function) -> String {
231-
let ret_ty = func.ret_type(db);
231+
let ret_ty = func.async_ret_type(db).unwrap_or_else(|| func.ret_type(db));
232232
let mut detail = String::new();
233233

234234
if func.is_const(db) {

crates/ide_completion/src/tests/expression.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -624,20 +624,21 @@ fn main() {
624624

625625
#[test]
626626
fn detail_async_fn() {
627-
// FIXME: #11438
628627
check_empty(
629628
r#"
630629
//- minicore: future, sized
631630
trait Trait<T> {}
632631
async fn foo() -> u8 {}
632+
async fn bar<U>() -> impl Trait<U> {}
633633
fn main() {
634634
self::$0
635635
}
636636
"#,
637637
expect![[r"
638638
tt Trait
639639
fn main() fn()
640-
fn foo() async fn() -> impl Future<Output = u8>
640+
fn bar() async fn() -> impl Trait<U>
641+
fn foo() async fn() -> u8
641642
"]],
642643
);
643644
}

0 commit comments

Comments
 (0)