rust-lang / rust-analyzer

A Rust compiler front-end for IDEs
https://rust-analyzer.github.io/
Apache License 2.0
13.62k stars 1.49k forks source link

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

Open Kohei316 opened 2 weeks ago

Kohei316 commented 2 weeks ago

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.