Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TC: Check that we haven't inferred a nonsensical type for if conditions #764

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading