From 747192ad8e7633fdf89821a5642e8c0734c01ce5 Mon Sep 17 00:00:00 2001 From: CLAUDE <355856132+HoTT-stuff@users.noreply.github.com> Date: Thu, 26 Nov 2026 02:25:22 +0000 Subject: [PATCH 1/2] feat(parser): a mid-block `match` self-terminates like `if` (#644) The empty arm `Pat => {}` already parsed (7963b46, ruling D193). What still failed in the #644 repro was a `match` statement with no `;` after its `}`. Owner decision (2026-11-26): `match` self-terminates, as `if`, `while` and `for` do. One `stmt` rule. Its reduce/reduce conflict mirrors the existing `if` one (same seven tokens) and resolves the same way, keeping the expression reading: `match o { .. } - 1;` is a subtraction. Parse output for all 557 tracked `.affine` files is byte-identical before and after. The pinning test test_match_statement_requires_semicolon, which said to delete it on this decision, becomes test_match_statement_self_terminates (including the `-` continuation case). docs/ON-RAMP.adoc updated. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0136P7Zo5h4xmXVEeSeQEDvw --- CHANGELOG.adoc | 4 +++ docs/ON-RAMP.adoc | 23 +++++++------- lib/parser.mly | 6 ++++ test/test_e2e.ml | 78 ++++++++++++++++++++++++++++++----------------- 4 files changed, 71 insertions(+), 40 deletions(-) diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 0ae67c50..eef190ad 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -193,6 +193,10 @@ probe (#294) ==== Changed +* parser: a mid-block `match` self-terminates like `if`, `while` and +`for`; the `;` after its `}` is optional (#644). A following `*`, `-`, +`(`, `[`, `&` or `|` still continues the expression, as after `if`. Parse +output for every `.affine` file in the repository is unchanged. * refactor(codegen): extract affinescript.ownership emission to lib/tw_… (#347) diff --git a/docs/ON-RAMP.adoc b/docs/ON-RAMP.adoc index 9d261b2a..eefa6697 100644 --- a/docs/ON-RAMP.adoc +++ b/docs/ON-RAMP.adoc @@ -247,28 +247,27 @@ whole round-trip. Two grammar rules cost consumers time, so they are stated here: -* `if`, `while` and `for` are self-terminating statements — the closing `}` - is enough. -* A `match` that is *not* a block's final expression is an ordinary - expression statement and needs its `;`: +* `if`, `while`, `for` and `match` are self-terminating statements — the + closing `}` is enough, and a trailing `;` is also accepted (#644): [source,affine] ---- pub fn f(o: Opt) -> Int { - match o { // mid-block: statement, needs the `;` + match o { // mid-block statement: no `;` needed SomeV(v) => { return v; } NoneV => {} - }; + } return 0; } ---- -Omitting that `;` fails at the `}` that closes the `match`, which reads as -"the match is broken" rather than "the statement is unterminated". Empty arm -bodies (`Pat => {}`) are fine, in any position. The rule is pinned by -`test_match_statement_requires_semicolon` in `test/test_e2e.ml`, and the -grammar question ("should a `}`-terminated `match` self-terminate, as `if` -does?") is open on `#644`. +* 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. + +Empty arm bodies (`Pat => {}`) are fine, in any position. Both rules are +pinned in `test/test_e2e.ml` (`test_match_statement_self_terminates`). == The worked example diff --git a/lib/parser.mly b/lib/parser.mly index 11da5d72..ea4d2016 100644 --- a/lib/parser.mly +++ b/lib/parser.mly @@ -1292,6 +1292,12 @@ stmt: | 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) } diff --git a/test/test_e2e.ml b/test/test_e2e.ml index 18b8030c..1c3d58c5 100644 --- a/test/test_e2e.ml +++ b/test/test_e2e.ml @@ -1089,13 +1089,12 @@ let test_empty_match_arm_block_parses () = - empty arm, `match` mid-block, no `;` -> fails AT the match's `}` - empty arm, `match` mid-block, with `;` -> parses - non-empty arm, `match` mid-block, no `;` -> fails at the same place - So the empty arm was never the cause: only `if`/`while`/`for` are + So the empty arm was never the cause: only `if`/`while`/`for` were self-terminating statements (`lib/parser.mly`, `stmt:`), and a `match` - that is not a block's final expression is an ordinary expression - statement needing its `;`. This case keeps the empty arm *and* - terminates the statement, so it asserts #644's actual property rather - than the `;`-less shape the grammar has never accepted. The `;` rule - itself is pinned by test_match_statement_requires_semicolon below. *) + that was not a block's final expression needed its `;`. Since + 2026-11-26 `match` self-terminates too (owner decision on #644); this + case keeps the explicit `;`, which stays legal, and the `;`-less form + is pinned by test_match_statement_self_terminates below. *) let source = {|module EmptyArm; enum Opt { SomeV(Int), NoneV } pub fn f(o: Opt) -> Int { @@ -1113,32 +1112,55 @@ let test_empty_match_arm_block_parses () = | Lexer.Lexer_error (msg, _) -> Alcotest.failf "empty match-arm block should lex: %s" msg -(* The statement-termination rule the #644 repro tripped over, pinned so it - stops being folklore. `if`/`while`/`for` end with a `}` and terminate - themselves; a `match` in non-final statement position is an expression - statement and needs an explicit `;`. If the language decides instead that - a `}`-terminated `match` should self-terminate (that is #644's open - question — see docs/ON-RAMP.adoc, "Statements and `;`"), then this test is - the one to delete, and the positive form belongs next to the #644 case - above. *) -let test_match_statement_requires_semicolon () = - let source = {|module NoSemi; +(* 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`. *) +let test_match_statement_self_terminates () = + let parse_ok what source = + match Parse_driver.parse_string ~file:"" source with + | p -> p + | exception Parse_driver.Parse_error (msg, _) -> + Alcotest.failf "%s should parse: %s" what msg + in + let header = {|module M; enum Opt { SomeV(Int), NoneV } - pub fn f(o: Opt) -> Int { + |} in + ignore (parse_ok "the #644 repro" (header ^ {|pub fn f(o: Opt) -> Int { match o { SomeV(v) => { return v; } NoneV => {} } return 0; - }|} in - match Parse_driver.parse_string ~file:"" source with - | exception Parse_driver.Parse_error _ -> () - | exception Lexer.Lexer_error _ -> () - | _ -> - Alcotest.fail - "a mid-block `match` with no trailing `;` parsed; if the grammar was \ - deliberately extended to make `match` self-terminating, delete this \ - test and record the decision on #644" + }|})); + ignore (parse_ok "a match followed by let and an assignment" (header ^ {|pub fn f(o: Opt) -> Int { + let mut n = 0; + match o { SomeV(v) => { n = v; } NoneV => {} } + n = n + 1; + let k = n; + return k; + }|})); + ignore (parse_ok "a trailing `;` is still accepted" (header ^ {|pub fn f(o: Opt) -> Int { + 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 { + match o { SomeV(v) => v, NoneV => 0 } - 1; + return 0; + }|}) in + let body = + List.find_map + (function Ast.TopFn { fd_body = Ast.FnBlock b; _ } -> Some b | _ -> None) + p.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" let error_tests = [ Alcotest.test_case "bad syntax" `Quick test_error_parse_bad_syntax; @@ -1148,8 +1170,8 @@ 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` needs its `;` (#644 repro)" `Quick - test_match_statement_requires_semicolon; + Alcotest.test_case "mid-block `match` self-terminates (#644)" `Quick + test_match_statement_self_terminates; ] (* ============================================================================ From 80667b8ac5be48eba7f159cfcfac958b26725b5c Mon Sep 17 00:00:00 2001 From: CLAUDE <355856132+HoTT-stuff@users.noreply.github.com> Date: Thu, 26 Nov 2026 02:49:03 +0000 Subject: [PATCH 2/2] docs(on-ramp): a trailing `;` is accepted only after if and match (#644) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `while … {};` and `for … {};` are parse errors; the statement list said all four accepted one. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0136P7Zo5h4xmXVEeSeQEDvw --- docs/ON-RAMP.adoc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/ON-RAMP.adoc b/docs/ON-RAMP.adoc index eefa6697..078958e6 100644 --- a/docs/ON-RAMP.adoc +++ b/docs/ON-RAMP.adoc @@ -248,7 +248,8 @@ whole round-trip. Two grammar rules cost consumers time, so they are stated here: * `if`, `while`, `for` and `match` are self-terminating statements — the - closing `}` is enough, and a trailing `;` is also accepted (#644): + closing `}` is enough (#644). A trailing `;` is accepted only after `if` + and `match`; after `while` or `for` it is a parse error: [source,affine] ----