Skip to content
Draft
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
22 changes: 21 additions & 1 deletion docs/SOUNDNESS.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Freshness stamp — a real main-ancestor commit (survives squash-merge); advance
// it (and --reseal) in any change that touches a soundness path. See "Keeping
// this honest" and tools/check-soundness-ledger.sh (property 4).
:ground-truth-sha: 84eca60
:ground-truth-sha: 4a55253
:ground-truth-date: 2026-12-01
:stamp-verified-by: tools/check-soundness-ledger.sh
:toc: macro
Expand Down Expand Up @@ -311,6 +311,26 @@ write into the literal region. Layout invariant:
`test_wasm_heap_literals_intact_after_alloc` — run under Bun's WASI;
`test_wasm_heap_exhaustion_traps`); `lib/codegen.ml`
`memory_layout_of_datas` / `check_memory_layout`

| #833
| The tree-walking interpreter — the oracle the compiled backends are compared
against — runs the programs the checker accepts and agrees with Bun-ESM on
them. It used to refuse a function that calls itself, one that calls a later
declaration, any `let mut` / `const mut` assignment, and any use of an
imported `Some` / `None` / `Ok` / `Err` (all loud). One shape was *silent*: an
early `return` evaluated its operand and carried on, so
`if c { return 1; } 2` yielded `2`. (The assignment arm also ignored a
compound operator — `a += 5` would have stored `5` — which a
`let mut` program could not reach, because the assignment failed first.) `affinescript eval` also type-checked
with its own fold of `Typecheck.check_decl`, without the signature
pre-registration of `Typecheck.check_program`, so it rejected forward and
mutual references that `check` and `compile` accept; it now calls
`check_program`. Not changed: `eval` still does not run the borrow checker
(`check` and `compile` do), so it accepts programs they reject.
| `fixed`
| `test/test_interp_eval.ml` (18 cases: each program passes the full frontend,
then its interpreter output is compared with the expected lines and with the
Bun-ESM output of the same program; all 18 fail on `4248912`).
|===

== Still open — read before claiming "sound"
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
1 change: 0 additions & 1 deletion tools/soundness-anchors.sha256
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
20e73d88738c8fd075f81a36803554f92fa4ee53f71e33fa6759eae07c6e70f4 suite:test/test_intrinsic_shadow.ml
2bbd1b989fd0097530eede3e5230258e0e1092271a23ff7dff20cca65c0e612e suite:test/test_wasm_call_resolution.ml
2dfe36a21983a7dfb7ffa1157f7ddec9865a531a92682976b8fd677e4825e78d test:test_801_impl_methods_refused
3b74c20f201d0d53c2fbab5582a681d3dfc3cf978a0aea676ecc6400087452d9 suite:test/test_intrinsic_shadow.ml
45eb0f3aa5f643afbd003248998dd5a046688418647b491e0f2d4bb55fcbd0f2 test:test_wasm_heap_exhaustion_traps
546939eaa3c64c82edd23da0a3ba7f74bcf7378cfe1e426132dd1a9f8fd46050 test:test_801_transitive_public_and_const
5b84a4f71aa65f9aa9383e31c0cb624291c4f9d0de7cca4a0ea6a76fe85aea35 fixture:test/e2e/fixtures/handle_resume_nontail.affine
Expand Down
Loading