mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-27 05:23:24 +00:00
Merge #2618
2618: Fix coercion of last expression in function body r=flodiebold a=flodiebold Co-authored-by: Florian Diebold <florian.diebold@freiheit.com>
This commit is contained in:
commit
1d7931e5ff
3 changed files with 18 additions and 2 deletions
|
@ -460,7 +460,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
|
|||
}
|
||||
|
||||
fn infer_body(&mut self) {
|
||||
self.infer_expr(self.body.body_expr, &Expectation::has_type(self.return_ty.clone()));
|
||||
self.infer_expr_coerce(self.body.body_expr, &Expectation::has_type(self.return_ty.clone()));
|
||||
}
|
||||
|
||||
fn resolve_into_iter_item(&self) -> Option<TypeAliasId> {
|
||||
|
|
|
@ -41,7 +41,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
|
|||
|
||||
/// Infer type of expression with possibly implicit coerce to the expected type.
|
||||
/// Return the type after possible coercion.
|
||||
fn infer_expr_coerce(&mut self, expr: ExprId, expected: &Expectation) -> Ty {
|
||||
pub(super) fn infer_expr_coerce(&mut self, expr: ExprId, expected: &Expectation) -> Ty {
|
||||
let ty = self.infer_expr_inner(expr, &expected);
|
||||
let ty = if !self.coerce(&ty, &expected.ty) {
|
||||
self.result
|
||||
|
|
|
@ -369,6 +369,22 @@ fn test() {
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn return_coerce_unknown() {
|
||||
assert_snapshot!(
|
||||
infer_with_mismatches(r#"
|
||||
fn foo() -> u32 {
|
||||
return unknown;
|
||||
}
|
||||
"#, true),
|
||||
@r###"
|
||||
[17; 40) '{ ...own; }': !
|
||||
[23; 37) 'return unknown': !
|
||||
[30; 37) 'unknown': u32
|
||||
"###
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn coerce_autoderef() {
|
||||
assert_snapshot!(
|
||||
|
|
Loading…
Reference in a new issue