Skip to content

codegen(bun-esm): extern fn used as a value emits an unbound name #818

Done

Description

@hyperpolymath

Bug

An extern fn used as a value (not called) compiles on the Bun-ESM backend, then fails at runtime with an unbound name.

use Crypto::{ sha256 };
fn apply(f: ([Int]) -> [Int], b: [Int]) -> [Int] = f(b);
pub fn go(b: [Int]) -> [Int] = apply(sha256, b);

go([]) throws ReferenceError: sha256 is not defined.

Cause

lib/codegen_deno.ml, gen_expr, the ExprVar value-position branch. A direct call goes through the builtin table, which lowers to a host expression. A bare name emits the AffineScript name. An extern has no JavaScript binding with that name. This applies to every extern, not only the crypto ones.

Found by CodeRabbit on #816 (outside the diff).

Fix

In value position, lower a known builtin to an arrow function that calls the same lowering as a direct call.


Generated by Claude Code

Activity

  1. added
    bug
    on Nov 27, 2026
  2. whatproblem-ai commented on Nov 27, 2026

    @whatproblem-ai

    Problem: On the Bun-ESM backend, an extern fn used as a value rather than called, such as apply(sha256, b), compiles but throws ReferenceError: sha256 is not defined at runtime. The ExprVar value-position branch of gen_expr in lib/codegen_deno.ml emits the bare AffineScript name. Externs have no JavaScript binding with that name. This affects every extern.

    Expected: go([]) returns the same result as calling sha256([]) directly. Passing any extern as a function value should run without a ReferenceError.


    Clarity: 93%. Clear enough to work on.

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

  3. added
    architecture
    migration
    on Nov 27, 2026
  4. added
    bindings
    on Nov 27, 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

    architecturebindingsbugmigration

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions