Skip to content

Commit e26b1bf

Browse files
committed
explicit error for while condition
1 parent b984634 commit e26b1bf

File tree

4 files changed

+17
-4
lines changed

4 files changed

+17
-4
lines changed

compiler/ml/error_message_utils.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ type type_clash_context =
8080
| TryReturn
8181
| StringConcat
8282
| ComparisonOperator
83+
| WhileCondition
8384
| MathOperator of {
8485
for_float: bool;
8586
operator: string;
@@ -110,6 +111,8 @@ let error_expected_type_text ppf type_clash_context =
110111
fprintf ppf "But it's being compared to something of type:"
111112
| Some SwitchReturn -> fprintf ppf "But this switch is expected to return:"
112113
| Some TryReturn -> fprintf ppf "But this try/catch is expected to return:"
114+
| Some WhileCondition ->
115+
fprintf ppf "But a @{<info>while@} loop condition must always be of type:"
113116
| Some IfCondition ->
114117
fprintf ppf "But @{<info>if@} conditions must always be of type:"
115118
| Some IfReturn ->

compiler/ml/typecore.ml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1884,7 +1884,6 @@ let rec approx_type env sty =
18841884
| Ptyp_poly (_, sty) -> approx_type env sty
18851885
| _ -> newvar ()
18861886
1887-
(* TODO: Needs type clash context? *)
18881887
let rec type_approx env sexp =
18891888
match sexp.pexp_desc with
18901889
| Pexp_let (_, _, e) -> type_approx env e
@@ -2846,8 +2845,7 @@ and type_expect_ ~context ?in_function ?(recarg = Rejected) env sexp ty_expected
28462845
}
28472846
| Pexp_while (scond, sbody) ->
28482847
let cond =
2849-
(* TODO: Add explicit WhileCondition *)
2850-
type_expect ~context:(Some IfCondition) env scond Predef.type_bool
2848+
type_expect ~context:(Some WhileCondition) env scond Predef.type_bool
28512849
in
28522850
let body = type_statement ~context:None env sbody in
28532851
rue
@@ -3899,7 +3897,6 @@ and type_cases ~(call_context : [`Switch | `Function | `Try]) ?in_function env
38993897
match pc_guard with
39003898
| None -> None
39013899
| Some scond ->
3902-
(* TODO: Add explicit SwitchIfCondition *)
39033900
Some
39043901
(type_expect ~context:(Some IfCondition) ext_env
39053902
(wrap_unpacks scond unpacks)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/while_condition.res:1:7-13
4+
5+
1 │ while "horse" {
6+
2 │ Console.log("What")
7+
3 │ }
8+
9+
This has type: string
10+
But a while loop condition must always be of type: bool
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
while "horse" {
2+
Console.log("What")
3+
}

0 commit comments

Comments
 (0)