Skip to content

Commit

Permalink
TC: Check that we haven't inferred a nonsensical type for if conditio…
Browse files Browse the repository at this point in the history
…ns (#764)
  • Loading branch information
Alasdair authored Nov 6, 2024
1 parent a93d125 commit 2cf3f1c
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib/type_check.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3599,6 +3599,7 @@ and infer_exp env (E_aux (exp_aux, (l, uannot)) as exp) =
(* Try to infer the type of the condition - in some cases it may be a constant `true`
or `false`, e.g. `xlen == 32`. If that fails check it is a bool without inference. *)
let cond' = try irule infer_exp env cond with Type_error _ -> crule check_exp env cond bool_typ in
subtyp (exp_loc cond) env (typ_of cond') bool_typ;

(* Constraints to apply when reasoning about the branch types. The condition must be
true when evaluating the type of the `then` branch, and false for `else`. *)
Expand Down
5 changes: 5 additions & 0 deletions test/typecheck/fail/non_bool_if.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Type error:
fail/non_bool_if.sail:2.11-17:
2 |let _ = if "test" then 1 else 2
 | ^----^
 | Type mismatch between bool('ex1#) and string
2 changes: 2 additions & 0 deletions test/typecheck/fail/non_bool_if.sail
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

let _ = if "test" then 1 else 2

0 comments on commit 2cf3f1c

Please sign in to comment.