From 20487a1b4a7c2fdffdb1de61c7837ee6f673f21a Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Thu, 3 Jun 2021 23:12:35 +0200 Subject: Fix coercion in match with expected type Plus add infrastructure to test type mismatches without expect. --- crates/hir_ty/src/infer/expr.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'crates/hir_ty/src/infer') diff --git a/crates/hir_ty/src/infer/expr.rs b/crates/hir_ty/src/infer/expr.rs index f73bf43b2..e34f194ff 100644 --- a/crates/hir_ty/src/infer/expr.rs +++ b/crates/hir_ty/src/infer/expr.rs @@ -337,10 +337,15 @@ impl<'a> InferenceContext<'a> { Expr::Match { expr, arms } => { let input_ty = self.infer_expr(*expr, &Expectation::none()); + let expected = expected.adjust_for_branches(&mut self.table); + let mut result_ty = if arms.is_empty() { TyKind::Never.intern(&Interner) } else { - self.table.new_type_var() + match &expected { + Expectation::HasType(ty) => ty.clone(), + _ => self.table.new_type_var(), + } }; let matchee_diverges = self.diverges; -- cgit v1.2.3