Skip to content

-o x.mjs: a Nat leaves through a return type the marshal cannot resolve as a number, not a BigInt #1150

Closed

Description

@nohzafk

bend 2.0.32, macOS arm64, bun 1.4.2.

GUIDE.md says a Nat crossing into JS is a BigInt, and the out-marshal does convert
one when the return type is concrete. It does not when the type is one the marshal
cannot resolve (a type family), and by then the value is the runtime's own — a number,
because the in-marshal converted it.

mini.bend

import Base

type S is Data:
  SNat{}

def M(s: S) -> Data:
  match s:
    case SNat{}:
      Nat

def nat_of(s: S, n: Nat) -> Maybe<&2, M(s)>:
  match s:
    case SNat{}:
      Some{n}

bend mini.bend -o mini.mjs, then from JS:

nat_of({$: "SNat"}, 7n).value   ->  7        (typeof "number"; expected a BigInt)

The emitted wrapper marshals both arguments and nothing else:

"nat_of": run_lib((a0, a1) => { const r = (run_loop($nat_of$((a0), nat_host(a1)))); (a0); BigInt(a1); return r; }, 2),

With a concrete return type the result is marshalled, and the Nat is converted:

def concrete(n: Nat) -> Maybe<&2, Nat>:
  Some{n}

"concrete": run_lib((a0) => { const r = $0m0(run_loop($concrete$(nat_host(a0)))); BigInt(a0); return r; }, 1),
concrete(7n).value   ->  7n

Seen in a real program: dec(s: Schema, r: Raw) -> Maybe<&2, Meaning(s)> in a schema
library returns { $: "Some", value: 6 } for a Nat while the declaration it emits for the
same module says bigint — Meaning(s) is a type family, so the result is unmarshalled.

Same shape as #1105 (a value on a path the marshal does not resolve keeps the runtime's
representation), and the value is exact either way (a Nat is at most 2^48-1 < 2^53), so
this is a representation bug rather than a wrong number. It is reachable from any def whose
return type the marshal cannot resolve, and the fix has to be at the boundary: a caller
cannot tell an internal number from a host number.

Activity

  1. added
    later
    on Oct 9, 2026
  2. oxura commented on Oct 29, 2026

    @oxura
    Contributor

    Checked this reproducer on main 4f99f4c. The current JS boundary contract uses a number for Nat now: concrete(7).value and nat_of({$: "SNat"}, 7).value both return 7 with typeof number. The old 7n input is rejected with “expected a Nat host number”. I couldn't reproduce the Nat representation mismatch with the current documented input type; no compiler changes made.

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

    later

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions