Skip to content

Commit 6e8646d

Browse files
committed
Auto merge of #17174 - Kohei316:fix-infer-async-block-with-tail-return-expr, r=Veykril
Fix: infer type of async block with tail return expr Fixes #17106 The `infer_async_block` method calls the `infer_block` method internally, which returns the never type without coercion when `tail_expr` is `None` and `ctx.diverges` is `Diverges::Always`.This is the reason for the bug in this issue. https://github.com/rust-lang/rust-analyzer/blob/cfce2bb46da62950a8b70ddb0b2a12332da1b1e1/crates/hir-ty/src/infer/expr.rs#L1411-L1413 This PR solves the bug by adding a process to coerce after calling `infer_block` method. This code passes all the tests, including tests I added for this isuue, however, I am not sure if this solution is right. I think that this solution is an ad hoc solution. So, I would appreciate to have your review. I apologize if I'm off the mark, but `infer_async_block` method should be rewritten to share code with the process of infering type of `expr::Closure` instead of the `infer_block` method. That way it will be closer to the infer process of rustc.
2 parents 68fe34a + 425ed6a commit 6e8646d

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

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

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

936+
// FIXME: We should handle async blocks like we handle closures
937+
let expected = &Expectation::has_type(ret_ty);
936938
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))
939+
let ty = this.infer_block(tgt_expr, *id, statements, *tail, None, expected);
940+
if let Some(target) = expected.only_has_type(&mut this.table) {
941+
match this.coerce(Some(tgt_expr), &ty, &target) {
942+
Ok(res) => res,
943+
Err(_) => {
944+
this.result.type_mismatches.insert(
945+
tgt_expr.into(),
946+
TypeMismatch { expected: target.clone(), actual: ty.clone() },
947+
);
948+
target
949+
}
950+
}
951+
} else {
952+
ty
953+
}
938954
});
939955

940956
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)