From da3fa2b7f02b8f8dfa42f6e75a453b45554400d0 Mon Sep 17 00:00:00 2001 From: morine0122 Date: Fri, 26 Apr 2024 22:02:58 +0900 Subject: [PATCH] Fix coercion of async block --- crates/hir-ty/src/infer/expr.rs | 17 ++++++++++++++++- crates/ide/src/inlay_hints/bind_pat.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/crates/hir-ty/src/infer/expr.rs b/crates/hir-ty/src/infer/expr.rs index 38076fce8f..bccad751fc 100644 --- a/crates/hir-ty/src/infer/expr.rs +++ b/crates/hir-ty/src/infer/expr.rs @@ -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; diff --git a/crates/ide/src/inlay_hints/bind_pat.rs b/crates/ide/src/inlay_hints/bind_pat.rs index 0cb8c485b2..3311bb48ad 100644 --- a/crates/ide/src/inlay_hints/bind_pat.rs +++ b/crates/ide/src/inlay_hints/bind_pat.rs @@ -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 +}"#, + ); + } + + #[test] + fn type_hints_async_block_with_tail_return_exp() { + check_types( + r#" +//- minicore: future +async fn main() { + let _x = async { + //^^ impl Future + return 8_i32; + }; +}"#, + ); + } }