From 9c3f00a90651998c2cd4151f43f17cd92ef8eef1 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Fri, 20 Dec 2019 18:27:51 +0100 Subject: Fix coercion of last expression in function body --- crates/ra_hir_ty/src/infer.rs | 2 +- crates/ra_hir_ty/src/infer/expr.rs | 2 +- crates/ra_hir_ty/src/tests/coercion.rs | 16 ++++++++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) (limited to 'crates/ra_hir_ty') diff --git a/crates/ra_hir_ty/src/infer.rs b/crates/ra_hir_ty/src/infer.rs index 9f2ed830e..e97b81473 100644 --- a/crates/ra_hir_ty/src/infer.rs +++ b/crates/ra_hir_ty/src/infer.rs @@ -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 { diff --git a/crates/ra_hir_ty/src/infer/expr.rs b/crates/ra_hir_ty/src/infer/expr.rs index 253332c30..3af05394c 100644 --- a/crates/ra_hir_ty/src/infer/expr.rs +++ b/crates/ra_hir_ty/src/infer/expr.rs @@ -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 diff --git a/crates/ra_hir_ty/src/tests/coercion.rs b/crates/ra_hir_ty/src/tests/coercion.rs index 33d6ca403..793c23e41 100644 --- a/crates/ra_hir_ty/src/tests/coercion.rs +++ b/crates/ra_hir_ty/src/tests/coercion.rs @@ -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!( -- cgit v1.2.3