Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: infer type of async block with tail return expr #17174

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 17 additions & 1 deletion crates/hir-ty/src/infer/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -933,8 +933,24 @@ impl InferenceContext<'_> {
let prev_ret_coercion =
mem::replace(&mut self.return_coercion, Some(CoerceMany::new(ret_ty.clone())));

// FIXME: We should handle async blocks like we handle closures
let expected = &Expectation::has_type(ret_ty);
Veykril marked this conversation as resolved.
Show resolved Hide resolved
let (_, inner_ty) = self.with_breakable_ctx(BreakableKind::Border, None, None, |this| {
this.infer_block(tgt_expr, *id, statements, *tail, None, &Expectation::has_type(ret_ty))
let ty = this.infer_block(tgt_expr, *id, statements, *tail, None, expected);
Veykril marked this conversation as resolved.
Show resolved Hide resolved
if let Some(target) = expected.only_has_type(&mut this.table) {
match this.coerce(Some(tgt_expr), &ty, &target) {
Ok(res) => res,
Err(_) => {
this.result.type_mismatches.insert(
tgt_expr.into(),
TypeMismatch { expected: target.clone(), actual: ty.clone() },
);
target
}
}
} else {
ty
}
});

self.diverges = prev_diverges;
Expand Down
26 changes: 26 additions & 0 deletions crates/ide/src/inlay_hints/bind_pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1120,4 +1120,30 @@ fn test() {
"#,
);
}

#[test]
fn type_hints_async_block() {
check_types(
r#"
//- minicore: future
async fn main() {
let _x = async { 8_i32 };
//^^ impl Future<Output = i32>
}"#,
);
}

#[test]
fn type_hints_async_block_with_tail_return_exp() {
check_types(
r#"
//- minicore: future
async fn main() {
let _x = async {
//^^ impl Future<Output = i32>
return 8_i32;
};
}"#,
);
}
}