Skip to content

Commit d272e53

Browse files
committed
fix(diagnostics): continued if/match also needs statement position
CodeRabbit follow-up on #862: Unit alone is not evidence of a continued statement. `let y = (if c { () } else { () })(3);` is a Unit-valued call in value position, and it was still told to add a `;`. A #644 continuation can only appear as a call that stands as a whole statement or as a block's final expression. synth_block_body now records those calls (statement_calls, compared by physical identity, reset by each check_program). StatementContinued requires that position and the Unit type. The known limit is in the doc comment: parentheses are not in the AST, so `(if c { () } else { () })(3);` standing alone as a statement is still read as a continuation. Tests: unit_valued_value_position_keeps_not_a_function (if and match). dune runtest: 858 pass. Ledger: 5/5. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01J4QvDiosfvobXq67wbduuK
1 parent 583dc7a commit d272e53

2 files changed

Lines changed: 47 additions & 11 deletions

File tree

‎lib/typecheck.ml‎

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -251,21 +251,42 @@ let rec expr_start_span (e : expr) : Span.t option =
251251
| ExprTuple (f :: _) | ExprArray (f :: _) -> expr_start_span f
252252
| _ -> None
253253

254+
(** Calls that stand as a whole statement, or as a block's final
255+
expression, with an `if`/`match` callee: the only places a #644
256+
continuation can produce. Recorded by [synth_block_body], compared by
257+
physical identity. *)
258+
let statement_calls : expr list ref = ref []
259+
260+
let rec strip_spans = function ExprSpan (e, _) -> strip_spans e | e -> e
261+
262+
let note_statement_call (e : expr) : unit =
263+
match strip_spans e with
264+
| ExprApp (f, _) as app ->
265+
(match strip_spans f with
266+
| ExprIf _ | ExprMatch _ -> statement_calls := app :: !statement_calls
267+
| _ -> ())
268+
| _ -> ()
269+
254270
(** The error for calling [callee] whose type [ty] is not a function:
255271
[StatementContinued] when the callee is a statement `if`/`match`
256272
continued by `(` (#644), else [NotAFunction]; located when the
257273
callee's start is known.
258274
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. *)
263-
let not_a_function_error (callee : expr) (ty : ty) : type_error =
264-
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
266-
let err = match strip callee with
267-
| ExprIf _ when is_unit -> StatementContinued { kind = "if"; ty }
268-
| ExprMatch _ when is_unit -> StatementContinued { kind = "match"; ty }
275+
The evidence for "a statement was continued" is twofold: the call
276+
stands as a statement or as a block's final expression
277+
([statement_calls]), and the callee's value is Unit, as a statement
278+
`if`/`match` has. A value-position call such as
279+
`let y = (if c { () } else { () })(3);` keeps [NotAFunction], so it is
280+
not told to add a `;` it does not need. (A parenthesised callee is not
281+
visible in the AST, so `(if c { () } else { () })(3);` standing alone
282+
as a statement is still read as a continuation.) *)
283+
let not_a_function_error (app : expr) (callee : expr) (ty : ty) : type_error =
284+
let continued =
285+
List.memq app !statement_calls
286+
&& (match ty with TCon "Unit" -> true | TTuple [] -> true | _ -> false) in
287+
let err = match strip_spans callee with
288+
| ExprIf _ when continued -> StatementContinued { kind = "if"; ty }
289+
| ExprMatch _ when continued -> StatementContinued { kind = "match"; ty }
269290
| _ -> NotAFunction ty
270291
in
271292
match expr_start_span callee with
@@ -1120,7 +1141,7 @@ let rec synth (ctx : context) (expr : expr) : ty result =
11201141
| ExprApp (fn_expr, args) as app_node ->
11211142
let* fn_ty = synth ctx fn_expr in
11221143
let locate_call r = match r with
1123-
| Error (NotAFunction t) -> Error (not_a_function_error fn_expr t)
1144+
| Error (NotAFunction t) -> Error (not_a_function_error app_node fn_expr t)
11241145
| r -> r
11251146
in
11261147
(* #560: remember the instantiated type at `slice`/`show` calls; the
@@ -1682,6 +1703,8 @@ and synth_block (ctx : context) (blk : block) : ty result =
16821703
Fun.protect ~finally:restore (fun () -> synth_block_body ctx blk)
16831704

16841705
and synth_block_body (ctx : context) (blk : block) : ty result =
1706+
List.iter (function StmtExpr e -> note_statement_call e | _ -> ()) blk.blk_stmts;
1707+
Option.iter note_statement_call blk.blk_expr;
16851708
(* Type-check each statement for side effects *)
16861709
let* () = List.fold_left (fun acc stmt ->
16871710
let* () = acc in
@@ -2758,6 +2781,7 @@ let check_program ?(import_types : (string, scheme) Hashtbl.t option)
27582781
(symbols : Symbol.t) (prog : Ast.program)
27592782
: (context, type_error) Result.t =
27602783
try
2784+
statement_calls := [];
27612785
let ctx = create_context symbols in
27622786
register_builtins ctx;
27632787
(* ADR-014 / #228: record `use Mod;` qualifiers so qualified type/effect

‎test/test_call_diagnostics.ml‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,17 @@ let value_position_if_keeps_not_a_function () =
7676
| Typecheck.Located (Typecheck.NotAFunction _, _) -> ()
7777
| _ -> Alcotest.failf "expected located NotAFunction, got %s" (Typecheck.format_type_error e)
7878

79+
(* Unit-valued value-position calls: Unit alone is not evidence of a
80+
continued statement; the call must also stand as a statement or a
81+
block's final expression (CodeRabbit follow-up on #862). *)
82+
let unit_valued_value_position_keeps_not_a_function () =
83+
List.iter (fun src ->
84+
match expect_error src with
85+
| Typecheck.Located (Typecheck.NotAFunction _, _) -> ()
86+
| e -> Alcotest.failf "expected located NotAFunction, got %s" (Typecheck.format_type_error e))
87+
[ "pub fn f(c: Bool) -> Int {\n let y = (if c { () } else { () })(3);\n 0\n}\n";
88+
"pub fn f(n: Int) -> Int {\n let y = (match n { 0 => (), _ => () })(3);\n 0\n}\n" ]
89+
7990
let the_documented_remedy_typechecks () =
8091
match typecheck
8192
"pub fn f(t: Int) -> (Int, Int) {\n\
@@ -119,6 +130,7 @@ let tests = [
119130
Alcotest.test_case "continued match: named and located" `Quick continued_match_is_named_and_located;
120131
Alcotest.test_case "the ; remedy type-checks" `Quick the_documented_remedy_typechecks;
121132
Alcotest.test_case "value-position if keeps NotAFunction" `Quick value_position_if_keeps_not_a_function;
133+
Alcotest.test_case "Unit-valued value-position if/match keep NotAFunction" `Quick unit_valued_value_position_keeps_not_a_function;
122134
Alcotest.test_case "plain call on a non-function is located" `Quick plain_bad_call_is_located;
123135
Alcotest.test_case "text and JSON carry the location" `Quick text_and_json_carry_the_location;
124136
Alcotest.test_case "char_to_string : Char -> String" `Quick char_to_string_types;

0 commit comments

Comments
 (0)