Fix type for condition expr of if

This commit is contained in:
oxalica 2022-11-09 23:05:04 +08:00
parent f43380d009
commit 1e3fdf59cb
2 changed files with 3 additions and 1 deletions

View file

@ -208,7 +208,8 @@ impl<'db> InferCtx<'db> {
self.infer_expr(body)
}
&Expr::IfThenElse(cond, then, else_) => {
self.infer_expr(cond);
let cond_ty = self.infer_expr(cond);
self.unify_kind(cond_ty, TyKind::Bool);
let then_ty = self.infer_expr(then);
let else_ty = self.infer_expr(else_);
self.unify(then_ty, else_ty);

View file

@ -102,6 +102,7 @@ fn recursive() {
#[test]
fn if_then_else() {
check("a: if a then 1 else 1", expect!["bool → int"]);
check(
"if 1 == 2 then { a = 1; } else { b = 1; }",
expect!["{ a: int, b: int }"],