Repository navigation
fix(codegen): a write to a mut parameter reaches the caller, or fails loud (#848) - #867
Conversation
|
Review these changes at https://app.gitnotebooks.com/hyperpolymath/affinescript/pull/867 |
|
@coderabbitai review Generated by Claude Code |
|
Note Currently processing new changes in this PR. This may take a few minutes, please wait...
|
|
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
7c7db42 to
ad0ac44
Compare
|
## 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>
Action not completed
|




Closes #848
Summary
On core WASM a write to a
mutparameter was lost:fn set(mut m: Int) { m = 100 }left the caller'sxat 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):
mutparameter of a defined fn its own mutable write-back global.Return, including inside nested blocks, and at the fall-through end.Core WASM fails loudly (
UnsupportedFeature, naming #848) on:let mutfirst);mutparameter on an imported fn or a closure;&mutexpression. 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");mutparameter.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 amutparam go through the shared object and are untouched.Not covered
map(take, xs)) isn't caught by the Bun call-site check.compile) reports the write-back read as an extra use of an ExclBorrow param. It already flagged anymutparam 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, sobuildcan't run the OCaml tests on any branch, this one included.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 newmut 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 newtest_mut_param_writeback.mjsfails on28d949f(early()=5, want 100).tools/check-soundness-ledger.sh: all 5 properties hold. There is a new codegen(wasm): &mut / mut-parameter writes are lost — references are lowered as copies (silent wrong result) #848 row, the stamp is advanced to28d949f, and the anchors are resealed.Generated by Claude Code