Skip to content
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
12 changes: 8 additions & 4 deletions docs/ON-RAMP.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,14 @@ pub fn f(o: Opt) -> Int {
}
----

* A statement that starts with `if` or `match` and is followed by a token
that can continue an expression (`*`, `-`, `(`, `[`, `&`, `|`) is read as
that longer expression, so `match o { .. } -1;` subtracts. Put a `;` after
the `}` when the next statement starts with one of those tokens.
* A statement `if` or `match` ends at its own closing `}`, as in Rust (owner
ruling 2026-12-01, replacing the #644 continuation rule). The next line is
a new statement even when it starts with `(`, `[`, `*`, `-`, `&` or `|`:
`if c { .. }` then `(a, b)` is a statement and a tuple, never a call, and
`match o { .. } -1;` is the match, then `-1;`. To use an `if`/`match` value
in a larger expression, bind it with `let` or wrap it in parentheses.
* A trailing `if` or `match` with no `;` is the block's value, so
`{ let x = 1; if c { x } else { 2 } }` is still an `Int`.

Empty arm bodies (`Pat => {}`) are fine, in any position. Both rules are
pinned in `test/test_e2e.ml` (`test_match_statement_self_terminates`).
Expand Down
81 changes: 51 additions & 30 deletions lib/parser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ let trait_ref_of_type_expr (t : type_expr) : trait_ref =
(* Fold a non-empty comma-separated braced effect row `{E1, E2, ...}`
into the binary EffUnion AST. Right-associated; union is
order-insensitive downstream so association is immaterial. *)

(* A block from its statements, each paired with "block-like and ended
without a semicolon" (see [stmt]). With no trailing expression, a last
block-like statement is the block's value: the Rust rule, which keeps
`{ ..; if c { a } else { b } }` and `{ match x { .. } }` valued now that a
statement `if`/`match` ends at its closing brace. *)
let block_of_stmts (stmts : (stmt * bool) list) (final : expr option) : block =
match final, List.rev stmts with
| None, (StmtExpr e, true) :: rest ->
{ blk_stmts = List.rev_map fst rest; blk_expr = Some e }
| _ -> { blk_stmts = List.map fst stmts; blk_expr = final }

let rec effect_union_of_list = function
| [] -> assert false (* separated_nonempty_list guarantees >= 1 *)
| [e] -> e
Expand Down Expand Up @@ -933,6 +945,42 @@ expr_postfix:
et_catch = None; et_finally = None } }
| e = expr_primary { e }

/* Statements. Defined before [expr_primary] on purpose (owner ruling
2026-12-01, superseding the #644 continuation rule). A statement-position
`if` or `match` and the expression `if`/`match` reduce on the same
lookahead tokens (the six that can continue an expression, and the
closing brace of the block), a reduce/reduce conflict that Menhir resolves
in favour of the production that appears first. Placing the statement
first makes a statement `if`/`match` end at its own closing brace, as in
Rust: `if c { .. }` followed by `(a, b)` is two statements, never a call.

Each production also says whether it is block-like and ended without a
semicolon (the [bool]). [block_of_stmts] makes a trailing one the block's
value, so a block ending in `if c { a } else { b }` or `match x { .. }`
keeps that value, as Rust's trailing expression statement does. */
stmt:
| LET mut_ = MUT? pat = pattern ty = type_annotation? EQ value = expr SEMICOLON
{ (StmtLet { sl_mut = Option.is_some mut_; sl_quantity = None;
sl_pat = pat; sl_ty = ty; sl_value = value }, false) }
| qty_attr = quantity_attr LET mut_ = MUT? pat = pattern ty = type_annotation? EQ value = expr SEMICOLON
{ (StmtLet { sl_mut = Option.is_some mut_; sl_quantity = Some qty_attr;
sl_pat = pat; sl_ty = ty; sl_value = value }, false) }
| LET mut_ = MUT? pat = pattern qty = quantity_b_sugar ty = type_annotation? EQ value = expr SEMICOLON
{ (StmtLet { sl_mut = Option.is_some mut_; sl_quantity = Some qty;
sl_pat = pat; sl_ty = ty; sl_value = value }, false) }
| e = expr SEMICOLON { (StmtExpr e, false) }
| IF cond = expr then_blk = block else_part = else_part?
{ (StmtExpr (ExprIf { ei_cond = cond; ei_then = ExprBlock then_blk; ei_else = else_part }), true) }
| MATCH scrutinee = expr LBRACE arms = list(match_arm) RBRACE
{ (StmtExpr (ExprMatch { em_scrutinee = scrutinee; em_arms = arms }), true) }
| lhs = expr_postfix EQ rhs = expr SEMICOLON { (StmtAssign (lhs, AssignEq, rhs), false) }
| lhs = expr_postfix PLUSEQ rhs = expr SEMICOLON { (StmtAssign (lhs, AssignAdd, rhs), false) }
| lhs = expr_postfix MINUSEQ rhs = expr SEMICOLON { (StmtAssign (lhs, AssignSub, rhs), false) }
| lhs = expr_postfix STAREQ rhs = expr SEMICOLON { (StmtAssign (lhs, AssignMul, rhs), false) }
| lhs = expr_postfix SLASHEQ rhs = expr SEMICOLON { (StmtAssign (lhs, AssignDiv, rhs), false) }
| WHILE cond = expr body = block { (StmtWhile (cond, body), false) }
| FOR pat = pattern IN iter = expr body = block { (StmtFor (pat, iter, body), false) }

expr_primary:
/* Literals */
| n = INT { ExprLit (LitInt (n, mk_span $startpos $endpos)) }
Expand Down Expand Up @@ -1267,45 +1315,18 @@ block:
| LBRACE RBRACE
{ { blk_stmts = []; blk_expr = None } }
| LBRACE stmts = nonempty_list(stmt) RBRACE
{ { blk_stmts = stmts; blk_expr = None } }
{ block_of_stmts stmts None }
| LBRACE stmts = nonempty_list(stmt) final = block_terminator RBRACE
{ { blk_stmts = stmts; blk_expr = Some final } }
{ block_of_stmts stmts (Some final) }
| LBRACE stmts = stmt_list_nonempty_trailing_expr RBRACE
{ { blk_stmts = fst stmts; blk_expr = Some (snd stmts) } }
{ block_of_stmts (fst stmts) (Some (snd stmts)) }
| LBRACE e = expr RBRACE
{ { blk_stmts = []; blk_expr = Some e } }

stmt_list_nonempty_trailing_expr:
| s = stmt rest = stmt_list_nonempty_trailing_expr { (s :: fst rest, snd rest) }
| s = stmt e = expr { ([s], e) }

stmt:
| LET mut_ = MUT? pat = pattern ty = type_annotation? EQ value = expr SEMICOLON
{ StmtLet { sl_mut = Option.is_some mut_; sl_quantity = None;
sl_pat = pat; sl_ty = ty; sl_value = value } }
| qty_attr = quantity_attr LET mut_ = MUT? pat = pattern ty = type_annotation? EQ value = expr SEMICOLON
{ StmtLet { sl_mut = Option.is_some mut_; sl_quantity = Some qty_attr;
sl_pat = pat; sl_ty = ty; sl_value = value } }
| LET mut_ = MUT? pat = pattern qty = quantity_b_sugar ty = type_annotation? EQ value = expr SEMICOLON
{ StmtLet { sl_mut = Option.is_some mut_; sl_quantity = Some qty;
sl_pat = pat; sl_ty = ty; sl_value = value } }
| e = expr SEMICOLON { StmtExpr e }
| IF cond = expr then_blk = block else_part = else_part?
{ StmtExpr (ExprIf { ei_cond = cond; ei_then = ExprBlock then_blk; ei_else = else_part }) }
(* A statement `match`, like a statement `if`, ends with its own `}` and
needs no `;` before the next statement (#644). On the tokens that could
continue an expression (`*`, `-`, `(`, `[`, `&`, `|`) and before the
closing `}`, Menhir keeps the expression reading, as it does for `if`. *)
| MATCH scrutinee = expr LBRACE arms = list(match_arm) RBRACE
{ StmtExpr (ExprMatch { em_scrutinee = scrutinee; em_arms = arms }) }
| lhs = expr_postfix EQ rhs = expr SEMICOLON { StmtAssign (lhs, AssignEq, rhs) }
| lhs = expr_postfix PLUSEQ rhs = expr SEMICOLON { StmtAssign (lhs, AssignAdd, rhs) }
| lhs = expr_postfix MINUSEQ rhs = expr SEMICOLON { StmtAssign (lhs, AssignSub, rhs) }
| lhs = expr_postfix STAREQ rhs = expr SEMICOLON { StmtAssign (lhs, AssignMul, rhs) }
| lhs = expr_postfix SLASHEQ rhs = expr SEMICOLON { StmtAssign (lhs, AssignDiv, rhs) }
| WHILE cond = expr body = block { StmtWhile (cond, body) }
| FOR pat = pattern IN iter = expr body = block { StmtFor (pat, iter, body) }

/* ========== Patterns ========== */

pattern:
Expand Down
61 changes: 48 additions & 13 deletions test/test_e2e.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1114,8 +1114,8 @@ let test_empty_match_arm_block_parses () =

(* A `}`-terminated `match` self-terminates as a statement, like `if`
(#644, owner decision 2026-11-26; docs/ON-RAMP.adoc, "Statements and
`;`"). A following token that can continue an expression keeps the
expression reading, as for `if`. *)
`;`"). Since the owner ruling of 2026-12-01 a statement `if`/`match`
always ends at its `}`, as in Rust; a trailing one is the block's value. *)
let test_match_statement_self_terminates () =
let parse_ok what source =
match Parse_driver.parse_string ~file:"<match-stmt>" source with
Expand Down Expand Up @@ -1144,23 +1144,58 @@ let test_match_statement_self_terminates () =
match o { SomeV(v) => { return v; } NoneV => {} };
return 0;
}|}));
(* `match o { .. } - 1;` is one expression statement, a subtraction. *)
let p = parse_ok "a match continued by `-`" (header ^ {|pub fn f(o: Opt) -> Int {
(* Owner ruling 2026-12-01 (supersedes the #644 continuation rule): a
statement `match`/`if` ends at its own `}`, as in Rust, so
`match o { .. } - 1;` is two statements: the match, then `-1;`. *)
let p = parse_ok "a match followed by `-1;`" (header ^ {|pub fn f(o: Opt) -> Int {
match o { SomeV(v) => v, NoneV => 0 } - 1;
return 0;
}|}) in
let body =
let body_of p =
List.find_map
(function Ast.TopFn { fd_body = Ast.FnBlock b; _ } -> Some b | _ -> None)
p.prog_decls
p.Ast.prog_decls
in
let rec strip = function Ast.ExprSpan (e, _) -> strip e | e -> e in
match body with
| Some { blk_stmts = Ast.StmtExpr e :: _; _ } ->
(match strip e with
| Ast.ExprBinary (_, Ast.OpSub, _) -> ()
| _ -> Alcotest.fail "`match .. } - 1;` should be one subtraction")
| _ -> Alcotest.fail "expected an expression statement first"
(match body_of p with
| Some { blk_stmts = Ast.StmtExpr m :: Ast.StmtExpr n :: _; _ } ->
(match strip m, strip n with
| Ast.ExprMatch _, Ast.ExprUnary (Ast.OpNeg, _) -> ()
| _ -> Alcotest.fail "expected a match statement, then a `-1` statement")
| _ -> Alcotest.fail "expected two expression statements");
(* `if c { .. }` then `(a, b)` on the next line: a statement and a tuple
value, never a call on the if's Unit. *)
let p = parse_ok "an if followed by a tuple" (header ^ {|pub fn f(t: Int) -> (Int, Int) {
let mut u = t;
if u > 0 { u = 1; }
(u, 2)
}|}) in
(match body_of p with
| Some { blk_stmts = [ _; Ast.StmtExpr i ]; blk_expr = Some t } ->
(match strip i, strip t with
| Ast.ExprIf _, Ast.ExprTuple _ -> ()
| _ -> Alcotest.fail "expected an if statement and a tuple value")
| _ -> Alcotest.fail "expected let, if statement, tuple value");
(* A trailing `if`/`match` without `;` is still the block's value. *)
let tail what src =
match body_of (parse_ok what (header ^ src)) with
| Some { blk_expr = Some e; _ } ->
(match strip e with
| Ast.ExprIf _ | Ast.ExprMatch _ -> ()
| _ -> Alcotest.failf "%s: the value should be the if/match" what)
| _ -> Alcotest.failf "%s: the block should have a value" what
in
tail "a trailing if is the value" {|pub fn f(c: Bool) -> Int {
let x = 1;
if c { x } else { 2 }
}|};
tail "a trailing match is the value" {|pub fn f(o: Opt) -> Int {
let x = 1;
match o { SomeV(v) => v, NoneV => x }
}|};
tail "a lone match is the value" {|pub fn f(o: Opt) -> Int {
match o { SomeV(v) => v, NoneV => 0 }
}|}

let error_tests = [
Alcotest.test_case "bad syntax" `Quick test_error_parse_bad_syntax;
Expand All @@ -1170,7 +1205,7 @@ let error_tests = [
test_total_is_valid_binding_name;
Alcotest.test_case "empty match arm block parses (#644)" `Quick
test_empty_match_arm_block_parses;
Alcotest.test_case "mid-block `match` self-terminates (#644)" `Quick
Alcotest.test_case "statement if/match end at their `}` (Rust rule)" `Quick
test_match_statement_self_terminates;
]

Expand Down
Loading