From b939a6dcacc0460f49781e922c979d235a52eeca Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 15 May 2021 19:35:12 +0200 Subject: [PATCH] Fix if/else coercion --- crates/hir_ty/src/infer/expr.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/crates/hir_ty/src/infer/expr.rs b/crates/hir_ty/src/infer/expr.rs index aab4d31534c..f439169ea4a 100644 --- a/crates/hir_ty/src/infer/expr.rs +++ b/crates/hir_ty/src/infer/expr.rs @@ -131,17 +131,20 @@ impl<'a> InferenceContext<'a> { let condition_diverges = mem::replace(&mut self.diverges, Diverges::Maybe); let mut both_arms_diverge = Diverges::Always; + let mut result_ty = self.table.new_type_var(); let then_ty = self.infer_expr_inner(*then_branch, &expected); both_arms_diverge &= mem::replace(&mut self.diverges, Diverges::Maybe); + result_ty = self.coerce_merge_branch(&result_ty, &then_ty); let else_ty = match else_branch { Some(else_branch) => self.infer_expr_inner(*else_branch, &expected), None => TyBuilder::unit(), }; both_arms_diverge &= self.diverges; + result_ty = self.coerce_merge_branch(&result_ty, &else_ty); self.diverges = condition_diverges | both_arms_diverge; - self.coerce_merge_branch(&then_ty, &else_ty) + result_ty } Expr::Block { statements, tail, label, id: _ } => { let old_resolver = mem::replace(