Skip to content

Commit 583dc7a

Browse files
committed
fix(diagnostics): name a continued if/match only when its value is Unit
CodeRabbit review on #862: `(if c { 1 } else { 2 })(3)` in value position was reported as a continued statement and told to add a `;` it does not need. A statement `if`/`match` has the value Unit, so that type is now the evidence. Any other callee type keeps located NotAFunction. Test: value_position_if_keeps_not_a_function. dune runtest: 857 pass. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01J4QvDiosfvobXq67wbduuK
1 parent 6d390c6 commit 583dc7a

2 files changed

Lines changed: 22 additions & 3 deletions

File tree

‎lib/typecheck.ml‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,12 +254,18 @@ let rec expr_start_span (e : expr) : Span.t option =
254254
(** The error for calling [callee] whose type [ty] is not a function:
255255
[StatementContinued] when the callee is a statement `if`/`match`
256256
continued by `(` (#644), else [NotAFunction]; located when the
257-
callee's start is known. *)
257+
callee's start is known.
258+
259+
The evidence for "a statement was continued" is the callee's type: a
260+
statement `if`/`match` has the value Unit. A value-position
261+
`(if c { 1 } else { 2 })(3)` calls an Int and keeps [NotAFunction],
262+
so it is not told to add a `;` it does not need. *)
258263
let not_a_function_error (callee : expr) (ty : ty) : type_error =
259264
let rec strip = function ExprSpan (e, _) -> strip e | e -> e in
265+
let is_unit = (match ty with TCon "Unit" -> true | TTuple [] -> true | _ -> false) in
260266
let err = match strip callee with
261-
| ExprIf _ -> StatementContinued { kind = "if"; ty }
262-
| ExprMatch _ -> StatementContinued { kind = "match"; ty }
267+
| ExprIf _ when is_unit -> StatementContinued { kind = "if"; ty }
268+
| ExprMatch _ when is_unit -> StatementContinued { kind = "match"; ty }
263269
| _ -> NotAFunction ty
264270
in
265271
match expr_start_span callee with

‎test/test_call_diagnostics.ml‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,18 @@ let continued_match_is_named_and_located () =
6464
| Typecheck.Located (Typecheck.StatementContinued { kind = "match"; _ }, _) -> ()
6565
| _ -> Alcotest.failf "expected StatementContinued match, got %s" (Typecheck.format_type_error e)
6666

67+
(* A value-position `if` that is not Unit was not a continued statement:
68+
plain E0104, located, with no `;` advice (CodeRabbit review on #862). *)
69+
let value_position_if_keeps_not_a_function () =
70+
let e = expect_error
71+
"pub fn f(c: Bool) -> Int {\n\
72+
\ let y = (if c { 1 } else { 2 })(3);\n\
73+
\ y\n\
74+
}\n" in
75+
match e with
76+
| Typecheck.Located (Typecheck.NotAFunction _, _) -> ()
77+
| _ -> Alcotest.failf "expected located NotAFunction, got %s" (Typecheck.format_type_error e)
78+
6779
let the_documented_remedy_typechecks () =
6880
match typecheck
6981
"pub fn f(t: Int) -> (Int, Int) {\n\
@@ -106,6 +118,7 @@ let tests = [
106118
Alcotest.test_case "continued if: named and located" `Quick continued_if_is_named_and_located;
107119
Alcotest.test_case "continued match: named and located" `Quick continued_match_is_named_and_located;
108120
Alcotest.test_case "the ; remedy type-checks" `Quick the_documented_remedy_typechecks;
121+
Alcotest.test_case "value-position if keeps NotAFunction" `Quick value_position_if_keeps_not_a_function;
109122
Alcotest.test_case "plain call on a non-function is located" `Quick plain_bad_call_is_located;
110123
Alcotest.test_case "text and JSON carry the location" `Quick text_and_json_carry_the_location;
111124
Alcotest.test_case "char_to_string : Char -> String" `Quick char_to_string_types;

0 commit comments

Comments
 (0)