You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit c2a5493
Browse filesBrowse the repository at this point in the historyBrowse files
fix(codegen): a local shadows a same-named fn on wasm; intern call_indirect types (#838) (#840)
Closes#838
Found by the #829 thread; reproduced independently on `main` `ee211d3`.
This is split off from #835, which fixes the heap and literal layout, so
each PR stays reviewable.
## The holes
**1. Silent wrong value.**
```affine
fn helper(s: String) -> Int { 7 }
fn call(helper: fn(String) -> Int) -> Int { helper("x") }
fn main() -> () { println(int_to_string(call(|s| 9))); }
```
The interpreter and Bun-ESM print `9`. Core wasm printed **`7`**: the
call went to the top-level `helper`, and the argument was silently
ignored. The same happens with a let-bound closure.
Cause: the generic `ExprApp` arm of `gen_expr` (`lib/codegen.ml`)
consulted `func_indices` before `ctx.locals`. The `ExprVar` value paths
already looked up locals first.
**2. Invalid wasm (found while fixing 1).**
`fn call(f: fn(String) -> Int) -> Int { f("x") }`, followed by `call(|s|
9)`, failed with `CompileError: WebAssembly.Module doesn't validate`.
Cause: the three `CallIndirect` sites looked up the signature in the
type section but never added it. If the signature was absent, they
returned the next free index, and a later, different type took that
slot. On `main`, this breaks any higher-order function compiled before a
function with its closure signature exists.
## The fix
- **Call resolution:** the call arm and `expr_val_type`'s `ExprApp` case
now check locals first, so a local shadows a same-named top-level
function. A name that is not a local resolves as before.
- **Type interning:** a shared `intern_type` helper adds a missing
signature, and all three `CallIndirect` sites now use it.
This complements #824, which only stopped a parameter from pulling in a
module import. It is consistent with #824: a parameter is never a
reference to a top-level or imported function.
## Tests (`test/test_wasm_call_resolution.ml`, run under Bun's WASI;
SKIP without `bun`)
| Test | `main` `ee211d3` | This PR |
|---|---|---|
| `test_wasm_param_shadows_fn` | **FAIL**: got `7` | pass: `9` |
| `test_wasm_let_shadows_fn` | **FAIL**: got `7` | pass: `9` |
| `test_wasm_unshadowed_fn_still_direct` (direct call plus closure,
`16`) | **FAIL**: invalid wasm (bug 2) | pass |
| `test_wasm_call_indirect_type_interned` (every `call_indirect` type
index is in the section, and the program runs) | **FAIL** | pass |
## Gates (local, this branch)
```
just check → exit 0 (test_main: 666 tests; soundness ledger: all 5 properties hold)
tools/run_codegen_wasm_tests.sh → All codegen WASM tests passed (48 JS harnesses)
tools/run_codegen_deno_tests.sh → All codegen Bun-ESM (legacy codegen-deno corpus) tests passed (42 harnesses)
tools/run_codegen_bun_tests.sh → All native Bun-ESM tests passed (8 harnesses)
```
The new `SOUNDNESS.adoc` row for #838 is `fixed`. The stamp is set to
`ee211d3` / 2026-12-01, and the anchors are resealed.
## Coordination
- **#835:** adds ledger rows at the same place in `docs/SOUNDNESS.adoc`.
It also changes `tools/soundness-anchors.sha256` and
`test/test_main.ml`. Whichever PR lands second needs a rebase and a
reseal. I own both and will do it.
- **#829:** sets the same stamp.
## Not in this PR
- #839: a named function passed as a value (`call(nine)`) is refused at
compile time with `UnboundVariable`. That fails loudly, and the fix is a
feature (named functions as null-environment closures).
- #836: a user function named like an intrinsic is silently replaced by
the intrinsic. It is pinned in #835. Each of its 20 interceptions needs
checking first.
---
- **Commit signature:** signed with the session key. The owner re-signs
before merge.
- **Constraints:** no licence or SPDX edits, and no TypeScript or JS.
---
_Generated by [Claude Code](https://claude.ai/code)_
Co-authored-by: Claude <noreply@anthropic.com>
0 commit comments