Skip to content

Commit

Permalink
Fix coercion of async block
Browse files Browse the repository at this point in the history
  • Loading branch information
Kohei316 committed Apr 30, 2024
1 parent b2c3774 commit 39a653f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/tools/rust-analyzer/crates/hir-ty/src/infer/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -933,8 +933,23 @@ impl InferenceContext<'_> {
let prev_ret_coercion =
mem::replace(&mut self.return_coercion, Some(CoerceMany::new(ret_ty.clone())));

let expected = &Expectation::has_type(ret_ty);
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);
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 src/tools/rust-analyzer/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;
};
}"#,
);
}
}

0 comments on commit 39a653f

Please sign in to comment.