Skip to content

wasm: a local/parameter does not shadow a same-named top-level fn (silent wrong value); closure call types not interned (invalid wasm) #838

Done

Description

@hyperpolymath

Found by the #829 thread. Reproduced independently on main ee211d3.

Evidence

fn helper(s: String) -> Int { 7 }
fn call(helper: fn(String) -> Int) -> Int { helper("x") }
fn main() -> () { println(int_to_string(call(|s| 9))); }
  • interp and Bun-ESM: print 9.
  • core wasm: prints 7. The call goes to the top-level helper and silently ignores the argument. This happens with any name, and also with a let-bound closure.

While fixing this, I found a second bug in the same area. A higher-order function compiled before any function with its closure signature produces invalid wasm (CompileError: WebAssembly.Module doesn't validate):

fn call(f: fn(String) -> Int) -> Int { f("x") }
fn main() -> () { println(int_to_string(call(|s| 9))); }

Causes

  1. Silent wrong value. The generic ExprApp arm of gen_expr (lib/codegen.ml) checked func_indices before ctx.locals. The ExprVar and value paths already checked locals first.
  2. Invalid wasm (loud). 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 then took that slot.

This complements #824, which stopped a parameter from pulling in a module import. That PR changed only the import filter. This issue is about call lowering.

Classification

  • (1) is a soundness hole: a silent wrong value.
  • (2) is a loud divergence: the module fails to load.

Fix

The fix is in a draft PR:

  • Look up locals first in the call arm and in expr_val_type.
  • Add one intern_type helper, used at all three CallIndirect sites.

Generated by Claude Code

Activity

  1. added
    bindings
    bug
    on Dec 1, 2026
  2. whatproblem-ai commented on Dec 1, 2026

    @whatproblem-ai

    Problem: In core wasm codegen, a call through a local or parameter (or let-bound closure) that shares a name with a top-level fn resolves to the top-level fn. The argument is silently ignored and the wrong value comes back (7 instead of 9). Separately, a higher-order fn compiled before any fn with its closure signature emits a CallIndirect type index that was never interned, so the module fails validation.

    Expected: wasm matches interp and Bun-ESM (prints 9), and both repros produce valid modules.

    Missing: link to the draft PR


    Clarity: 94%. Clear enough to work on.

    ⌨️ Commands · 💬 Have feedback? Tell us · 🎯 New here? Add WhatProblem to your repo · 🔒 Privacy

  3. added
    architecture
    priority:p1
    scope:repo
    status:ready
    on Dec 1, 2026
  4. added a commit that references this issue on Dec 1, 2026
  5. added 6 commits that reference this issue on Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    architecturebindingsbugpriority:p1scope:repostatus:ready

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions