Skip to content

feat(grammar): a statement if/match ends at its closing brace (Rust rule) - #863

Merged
hyperpolymath merged 2 commits into
mainfrom
claude/gifted-lamport-k0mc5l-stmt-ends
Dec 2, 2026
Merged

hyperpolymath merged 2 commits into
mainfrom
claude/gifted-lamport-k0mc5l-stmt-ends

Conversation

@hyperpolymath

Copy link
Copy Markdown
Owner

Owner ruling 2026-12-01 ("Rust rule: statement ends"), superseding the #644 continuation rule.

What changes

A statement-position if or match ends at its own closing }, as in Rust:

Source Before After
if c { .. } then (a, b) on the next line a call on the if's Unit (type error) a statement, then a tuple
match o { .. } -1; one subtraction the match, then -1;
{ let x = 1; if c { x } else { 2 } } Int Int (unchanged)
{ match x { .. } } the match's value the match's value (unchanged)
if c { .. }; (trailing ;) accepted accepted (unchanged)

How

  • The continuation came from two reduce/reduce conflicts: statement against expression if/match, on *, -, (, [, &, | and }. Menhir resolves those in favour of the production that appears first.
  • stmt now sits before expr_primary, so the statement reading wins. Each statement also reports whether it is block-like and ended without ;.
  • block_of_stmts makes a trailing block-like statement the block's value, as Rust's trailing-expression rule does. That keeps blocks ending in if/match valued.
  • The conflict counts are unchanged (2 reduce/reduce states); only their resolution flips.

Measured before changing

  • I diffed the old parser (main 4a55253) against the new one across 611 .affine files: the affinescript stdlib, tests, examples and tools, plus consent-aware-inspector/src/core. 0 print a different AST.
  • 85 of those files parse under neither compiler (other faces, negative fixtures).
  • Not measured: other estate repos with .affine sources that are not cloned here.

Tests

  • test_match_statement_self_terminates now pins the new rule:
    • a match followed by -1; gives two statements;
    • an if followed by a tuple gives a statement and a tuple value;
    • a trailing if, a trailing match and a lone match keep the block's value.
  • dune runtest: 849 pass. run_codegen_bun_tests.sh: pass. check-soundness-ledger.sh: 5/5. dune build @fmt: exit 0. Governance: PASS.

Docs and ledger

Interplay with #862

#862 adds StatementContinued, an error that names the old rule. With this PR, the shapes it targets no longer produce an error. Whichever merges second should drop StatementContinued and keep #862's located NotAFunction and char_to_string. I'll do that in the second PR once the first lands.


Generated by Claude Code

…ule)

Owner ruling 2026-12-01. It supersedes the #644 continuation rule, under
which a statement `if`/`match` followed by `(`, `[`, `*`, `-`, `&` or `|`
continued the expression. So `if c { .. }` followed on the next line by
`(a, b)` was a call on Unit, and `match o { .. } -1;` subtracted.

How: the continuation came from two reduce/reduce conflicts (statement
vs expression `if`/`match`, on those six tokens and `}`). Menhir resolves
those in favour of the production that appears first.
- `stmt` now appears before `expr_primary`, so the statement reading wins.
- Each statement also reports whether it is block-like and ended without
  `;`.
- `block_of_stmts` makes a trailing one the block's value. That is Rust's
  trailing-expression rule, and it keeps `{ ..; if c { a } else { b } }`
  and `{ match x { .. } }` valued.

Measured before changing:
- The old (main 4a55253) and new parser print identical ASTs for all 611
  .affine files available locally: the affinescript stdlib, tests,
  examples and tools, plus consent-aware-inspector's src/core.
- 85 of those parse under neither compiler (other faces, negative
  fixtures).
- Other estate repos with .affine code were not measured.

Tests: test_match_statement_self_terminates now pins the new rule (match
then `-1;`; if then a tuple; a trailing if/match and a lone match keep
the value). dune runtest: 849 pass. Bun harnesses pass.

docs/ON-RAMP.adoc "Statements and `;`" restated. The ledger part of #856
is ported again, because main is red on property 3, and the stamp is
advanced to 4a55253.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01J4QvDiosfvobXq67wbduuK
@coderabbitai

coderabbitai Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 58 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: f5f9fd03-9696-451f-8826-c5c830400912
Commits

Reviewing files that changed from the base of the PR and between 8f2b80d and 8d65126.

Files selected for processing: 3

  • docs/ON-RAMP.adoc
  • lib/parser.mly
  • test/test_e2e.ml
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@gitnotebooks

gitnotebooks Bot commented Dec 1, 2026

Copy link
Copy Markdown

hyperpolymath added a commit that referenced this pull request Dec 2, 2026
Owner ruling 2026-12-01: first-class wrappers for intrinsics on Bun-ESM.

## Problem

An intrinsic used as a value, rather than called, was emitted as a bare
name with no JS binding:

```affine
let f = len;   f("abcd")         // ReferenceError: len is not defined
apply1(len, s)                    // same
```

Declared externs were already wrapped (#818). Non-extern intrinsics
(`len`, `string_sub`, `trim`, `int_to_string`, …) were not.

## Change

- **`builtin_arity`**: each builtin's arity, read once from its type in
`Typecheck.register_builtins`. That is one source of truth, with no
hand-kept arity list.
- **`wrap_builtin_value`**: the builtin's lowering wrapped in an arrow
of that arity, e.g. `((__a0, __a1, __a2) => __as_strSub(__a0, __a1,
__a2))`.
- The #818 extern path now uses the same helper, so externs and
intrinsics follow one rule. As before #818, an awaiting lowering keeps
the bare name.

## Coverage (measured)

- All **40** Bun builtins the type checker types are wrapped.
`test_intrinsic_values` fails if any typed builtin ever is not.
- The other 345 Bun builtins are stdlib `extern fn`s, which #818 wraps
when they are declared.
- **Pre-existing gap, not fixed here:** `acos` and `asin` have a Bun
lowering but are neither resolvable nor typed (`undefined value: acos`),
so no program can use them. Fixing that needs types and interpreter
support, so it should be its own change.

## Tests

- `tests/codegen-bun/intrinsic_values`:
  - `len` and `to_uppercase` are passed to higher-order functions;
- `trim`, `string_sub`, `string_find` and `int_to_string` are bound with
`let`.
- It fails on `main` with `ReferenceError: len is not defined` and
passes here.
- `test/test_intrinsic_values.ml`: coverage, arity from type, and exact
wrapper text.
- `dune runtest`: **852 pass**. `run_codegen_bun_tests.sh`: pass,
including #818's harnesses.
- `check-soundness-ledger.sh`: 5/5. `dune build @fmt`: exit 0.
Governance: PASS.

## Ledger

`main` is red on property 3. This ports the ledger part of
#856, the same port as
#862 and #863. It
becomes a no-op once #856 merges.

## Relation to #862

#862 adds `char_to_string`. Once both are merged, `char_to_string` is
wrapped like any other typed intrinsic, with no extra code.

---
_Generated by [Claude Code](https://claude.ai/code)_

Co-authored-by: Claude <noreply@anthropic.com>
@hyperpolymath
hyperpolymath marked this pull request as ready for review December 2, 2026 15:19
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 2, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-02T15:21:56.255183Z 5437e18 Draft marked ready
🔒 Security Review ✅ Completed 2026-12-02T15:23:05.857754Z 5437e18 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@hyperpolymath
hyperpolymath merged commit 15dbde4 into main Dec 2, 2026
15 checks passed
@hyperpolymath
hyperpolymath deleted the claude/gifted-lamport-k0mc5l-stmt-ends branch December 2, 2026 15:20
@sonarqubecloud

sonarqubecloud Bot commented Dec 2, 2026

Copy link
Copy Markdown

hyperpolymath pushed a commit that referenced this pull request Dec 5, 2026
test/test_jcs.ml, from #865, did not compile, so dune built no test
runner. main's 'build' job has been red since #865, and every later
regression was hidden behind it.

- a top-level 'let open' with no 'in' becomes 'open Affinescript',
  which is where Jcs lives;
- the canonical_inputs list was missing its ';' separators;
- 'tests' is now a flat test_case list, as test_main expects.

With the runner building again, #865's canonical key order shows up
in one older expectation: eval --json now prints
{"diagnostics":..,"success":..,"version":1}. Updated.

Two failures remain, not fixed here: #862's continued if/match tests
assert the grammar from before #863. That needs an owner call.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0136P7Zo5h4xmXVEeSeQEDvw
hyperpolymath pushed a commit that referenced this pull request Dec 5, 2026
#863 made a statement if/match end at its closing brace, so the #644
shape (if c { .. } then (u, 2), on the next line or the same line) now
type-checks. #862, merged after it, still asserted that shape was
StatementContinued, and both tests failed once the runner compiled.

The diagnostic is still reachable when a statement's head is a
parenthesised if/match that is then called:
(if c { () } else { () })(3);
The two tests now use that shape. A new test pins that the old #644
shapes type-check.

SOUNDNESS.adoc stamp advanced to 28d949f; anchors resealed
(test_interp_eval.ml changed in the previous commit).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0136P7Zo5h4xmXVEeSeQEDvw
hyperpolymath added a commit that referenced this pull request Dec 5, 2026
… loud (#848) (#867)

Closes #848

## Summary

On core WASM a write to a `mut` parameter was lost: `fn set(mut m: Int)
{ m = 100 }` left the caller's `x` at 5, while the interpreter gives
100. Bun-ESM lost the same write. ADR-028's gate requires every backend
to either write through or fail loudly.

**Core WASM writes through (copy-out):**
- A prepass gives each `mut` parameter of a defined fn its own mutable
write-back global.
- The callee stores the parameter to that global before every `Return`,
including inside nested blocks, and at the fall-through end.
- A direct caller then copies the global into the argument: a local, or
a mutable module global.
- Parameter types don't change, so exports and the host ABI are
unchanged.

**Core WASM fails loudly (`UnsupportedFeature`, naming #848) on:**
- a field, element, tuple-index or deref argument (the message suggests
binding it to a `let mut` first);
- a `mut` parameter on an imported fn or a closure;
- any `&mut` expression. ADR-028 option E withdraws reference
expressions anyway.

**Bun-ESM fails loudly.** A JS parameter can't be written back without
changing the JS ABI of exported functions, so Bun-ESM rejects:
- `&mut` (before this, it died with an internal "Pattern matching
failed");
- any program that *calls* a function which reassigns a `mut` parameter.

Only *defining* such a function still compiles, so
`stdlib/option.affine` (`take`) keeps building; the AOT smoke test
caught this. Writes to a field of a `mut` param go through the shared
object and are untouched.

## Not covered

- **Other text backends** (JS-text, C, Julia, …) aren't audited here.
They're still open under ADR-028's every-backend gate.
- **A function value passed rather than called** (`map(take, xs)`) isn't
caught by the Bun call-site check.
- **The typed-wasm Level 7 check** (informational on `compile`) reports
the write-back read as an extra use of an ExclBorrow param. It already
flagged any `mut` param read twice on main (e.g. `m = m + 1; m`), so I
left the verifier alone.

## main is red independently

`test/test_jcs.ml` (#865) doesn't compile, so `build` can't run the
OCaml tests on any branch, this one included.
- I checked this PR with a local repair applied: 882 tests run, and the
only failures are 2 that are also on main. #862's continued-if/match
tests assert the grammar from before #863.
- The repair is pushed as branch `fix/main-test-suite-compiles`, with no
PR yet, pending the owner.

## Test plan

- `dune build @runtest --force` (with the local test_jcs repair): the
new `mut parameter write-through or loud failure (#848)` suite, 8 cases,
passes. Nothing else regresses against main.
- `bash tools/run_codegen_wasm_tests.sh`: 49 harnesses pass. The new
`test_mut_param_writeback.mjs` fails on `28d949f` (`early()=5, want
100`).
- `tools/check-soundness-ledger.sh`: all 5 properties hold. There is a
new #848 row, the stamp is advanced to `28d949f`, and the anchors are
resealed.

---
_Generated by [Claude Code](https://claude.ai/code)_

Co-authored-by: CLAUDE <355856132+HoTT-stuff@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
hyperpolymath added a commit that referenced this pull request Dec 5, 2026
## Summary

main's `build`, `lint` and `coverage-visibility` jobs have been red
since #865: `test/test_jcs.ml` doesn't compile, so dune builds no OCaml
test runner. Every regression since then has been hidden behind it. This
PR gets main's tests compiling and passing again.

**1. `test/test_jcs.ml` (#865)**
- The top-level `let open Yojson.Basic` had no `in`; it is now `open
Affinescript`, where `Jcs` lives.
- The `canonical_inputs` list was missing its `;` separators.
- `tests` is now a flat `test_case` list, as `test_main` expects.

**2. `test/test_interp_eval.ml`**
- The `eval --json` expectation now uses #865's canonical key order
(`{"diagnostics":..,"success":..,"version":1}`).

**3. `test/test_call_diagnostics.ml` (#862 vs #863; owner ruling: #863's
grammar wins)**
- #863 made a statement `if`/`match` end at its `}`, so the #644 shape
(`if c { .. }` then `(u, 2)`) type-checks. #862 still asserted that
shape was a `StatementContinued` error.
- The diagnostic is still reachable when a statement's head is a
parenthesised `if`/`match` that is then called (`(if c { () } else { ()
})(3);`). The two tests now use that shape, and a new test pins that the
old shapes type-check.
- The diagnostic's advice ("put `;` after its closing `}`") no longer
fits the remaining shape. That's filed separately, not changed here.

`docs/SOUNDNESS.adoc`: stamp advanced to `28d949f`, anchors resealed
(`test_interp_eval.ml` is an anchor).

No compiler code changes.

## Test plan

- `dune build @runtest --force`: 875 tests run, 0 failures.
- `tools/check-soundness-ledger.sh`: all 5 properties hold.

Unblocks #867 (#848), whose CI is red only because of this.

---
_Generated by [Claude Code](https://claude.ai/code)_

---------

Co-authored-by: CLAUDE <355856132+HoTT-stuff@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants