Skip to content

Commit 39a653f

Browse files
committed
Fix coercion of async block
1 parent b2c3774 commit 39a653f

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

src/tools/rust-analyzer/crates/hir-ty/src/infer/expr.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -933,8 +933,23 @@ impl InferenceContext<'_> {
933933
let prev_ret_coercion =
934934
mem::replace(&mut self.return_coercion, Some(CoerceMany::new(ret_ty.clone())));
935935

936+
let expected = &Expectation::has_type(ret_ty);
936937
let (_, inner_ty) = self.with_breakable_ctx(BreakableKind::Border, None, None, |this| {
937-
this.infer_block(tgt_expr, *id, statements, *tail, None, &Expectation::has_type(ret_ty))
938+
let ty = this.infer_block(tgt_expr, *id, statements, *tail, None, expected);
939+
if let Some(target) = expected.only_has_type(&mut this.table) {
940+
match this.coerce(Some(tgt_expr), &ty, &target) {
941+
Ok(res) => res,
942+
Err(_) => {
943+
this.result.type_mismatches.insert(
944+
tgt_expr.into(),
945+
TypeMismatch { expected: target.clone(), actual: ty.clone() },
946+
);
947+
target
948+
}
949+
}
950+
} else {
951+
ty
952+
}
938953
});
939954

940955
self.diverges = prev_diverges;

src/tools/rust-analyzer/crates/ide/src/inlay_hints/bind_pat.rs

+26
Original file line numberDiff line numberDiff line change
@@ -1120,4 +1120,30 @@ fn test() {
11201120
"#,
11211121
);
11221122
}
1123+
1124+
#[test]
1125+
fn type_hints_async_block() {
1126+
check_types(
1127+
r#"
1128+
//- minicore: future
1129+
async fn main() {
1130+
let _x = async { 8_i32 };
1131+
//^^ impl Future<Output = i32>
1132+
}"#,
1133+
);
1134+
}
1135+
1136+
#[test]
1137+
fn type_hints_async_block_with_tail_return_exp() {
1138+
check_types(
1139+
r#"
1140+
//- minicore: future
1141+
async fn main() {
1142+
let _x = async {
1143+
//^^ impl Future<Output = i32>
1144+
return 8_i32;
1145+
};
1146+
}"#,
1147+
);
1148+
}
11231149
}

0 commit comments

Comments
 (0)