Skip to content

fix(codegen): a write to a mut parameter reaches the caller, or fails loud (#848) - #867

Merged
hyperpolymath merged 1 commit into
mainfrom
fix/848-mut-param-writethrough
Dec 5, 2026
Merged

hyperpolymath merged 1 commit into
mainfrom
fix/848-mut-param-writethrough

Conversation

@hyperpolymath

Copy link
Copy Markdown
Owner

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.

Test plan


Generated by Claude Code

@gitnotebooks

gitnotebooks Bot commented Dec 5, 2026

Copy link
Copy Markdown

Copy link
Copy Markdown
Owner Author

@coderabbitai review


Generated by Claude Code

@coderabbitai

coderabbitai Bot commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

Explain Change →

Note

CodeRabbit ASCII loading animation

Currently processing new changes in this PR. This may take a few minutes, please wait...

Review info

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: 1832cffb-d325-4025-9816-aa3628dbee4b
Commits

Reviewing files that changed from the base of the PR and between 28d949f and 7c7db42.

Files selected for processing: 9

  • CHANGELOG.adoc
  • docs/SOUNDNESS.adoc
  • lib/codegen.ml
  • lib/codegen_deno.ml
  • test/test_main.ml
  • test/test_mut_param_848.ml
  • tests/codegen/mut_param_writeback.affine
  • tests/codegen/test_mut_param_writeback.mjs
  • tools/soundness-anchors.sha256

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

Copy link
Copy Markdown
Owner Author

coverage-visibility (and build) fail here for the reason they fail on main (28d949f): test/test_jcs.ml from #865 does not compile (line 17: Error: Syntax error). That stops dune from building any OCaml test runner. This PR doesn't touch that file. A repair is pushed as branch fix/main-test-suite-compiles and is waiting on the owner. With it applied locally, this PR's suite passes apart from the 2 #862/#863 grammar-conflict failures that are also on main.


Generated by Claude Code

… loud (#848)

Core WASM lowered a mut parameter as a copy, so
fn set(mut m: Int) { m = 100 } left the caller's x at 5. The interpreter
gives 100.

Core WASM now writes through. A prepass gives each mut parameter of a
defined fn a mutable write-back global. The callee stores the parameter
to it before every Return (inside nested blocks too) 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 do not
change, so exports and the host ABI are unchanged.

These fail with a named UnsupportedFeature error instead of losing the
write:
- a field, element, tuple-index or deref argument;
- a mut parameter on an imported fn or on a closure;
- any &mut expression.

Bun-ESM cannot write a JS parameter back. It fails loudly on &mut, and
on a program that calls a function which reassigns a mut parameter.
Defining such a function still compiles, so stdlib/option.affine
(take) keeps building.

Tests: test/test_mut_param_848.ml covers the fences on both backends.
tests/codegen/mut_param_writeback.affine checks the values at runtime:
explicit and early returns, fall-through, Float, two mut params, and an
rvalue argument. Its harness reads early() = 5 on 28d949f.
SOUNDNESS.adoc gets a #848 row; stamp advanced and anchors resealed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0136P7Zo5h4xmXVEeSeQEDvw
@hyperpolymath
hyperpolymath force-pushed the fix/848-mut-param-writethrough branch from 7c7db42 to ad0ac44 Compare December 5, 2026 13:04
@sonarqubecloud

sonarqubecloud Bot commented Dec 5, 2026

Copy link
Copy Markdown

@hyperpolymath
hyperpolymath marked this pull request as ready for review December 5, 2026 13:05
@hyperpolymath
hyperpolymath merged commit 85fa326 into main Dec 5, 2026
13 of 16 checks passed
@hyperpolymath
hyperpolymath deleted the fix/848-mut-param-writethrough branch December 5, 2026 13:05
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>
@coderabbitai

coderabbitai Bot commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor
Action not completed

Pull request is closed.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

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.

codegen(wasm): &mut / mut-parameter writes are lost — references are lowered as copies (silent wrong result)

2 participants