Skip to content

Array.new of a boxed value works in JS but native fail-stops at depth 24 #1267

Closed as not planned

Description

@jasisz

What you did

I built the same checked pure program for native C and JavaScript.

import Base

def got(r: Array<String> & String) -> Nat:
  (a, s) = r
  String.length(s)

def main() -> Nat:
  got(Array.get(String, Array.new(String, 24n, F32.show(1.0)), 0))

What happened

Both --check-only and --verdict print ALL PROOFS CHECK. Emitted JS exits 0 and prints 1n. The native C binary exits 1 with bend: runtime fail-stop.

Changing only 24n to 23n makes both backends exit 0 and print 1n.

F32.show(1.0) matters only because it creates a dynamic heap-allocated String. In native blk_new, a boxed array shares its default term across 2^d cells. The redirect reference count has 24 bits (RFC_CNT = (1 << 24) - 1), so blk_new explicitly posts ERR_RFCS when d >= 24 and a default word is nontrivial. The JS implementation is Array(2 ** d).fill(v) and has no corresponding restriction.

The 24-bit count itself is a documented BendRT limitation, and #975 mentions that refcount overflow is rejected with ERR_RFCS. The observable issue here is the resulting backend acceptance split in the public Array.new operation: the same checked source completes on JS and fail-stops on native C. I found no issue or WONTFIX entry covering that depth-24 boxed-array split.

This is below the documented/common array-depth wall. tests/io/array_depth_bound.bend makes both lanes reject only a depth past 31 and describes depth 31 itself as legal subject to host memory. Native C therefore has a hidden lower acceptance wall based on the runtime representation of the element: a static or flat value can work at depth 24, while a dynamic boxed value fail-stops.

The lanes should either share an explicit boxed-array limit or represent the repeated native default without requiring a 2²⁴ reference count. The current source type and Array.new API do not expose the native-only restriction.

bend --version

Reproduced on v2.0.34 (7d8a3eb036042c6549461054d25a10f26d361c5c) and current main (57b6c150f63704f53d533776deb3859578d4b09e).

uname -sm

Darwin arm64

clang --version

Apple clang version 21.0.0 (clang-2100.0.123.102)

Activity

  1. Lorenzobattistela commented on Oct 6, 2026

    @Lorenzobattistela
    Collaborator

    Closing as not planned. On the C lane, copies of a shared value are tracked by a 24-bit count, so one value can have at most 2^24-1 live copies. Array.new(T, 24n, x) makes 2^24 copies of x, which goes past that. JS keeps no count, so it runs on, and that is why the two lanes differ. Removing the cap would make every reference count wider, which costs memory and speed everywhere, so we're keeping it.

    #1366 gives this case its own error message instead of the generic 'runtime fail-stop', and adds a WONTFIX entry with the workaround: build the value again for each group, so each build has its own count. For example, two depth-23 arrays joined by ANode:

    def half(d: Nat) -> Array<String>:
      Array.new(String, d, F32.show(1.0))
    
    def join(a: Array<String>, b: Array<String>) -> Array<String>:
      ANode{a, b}
    
    def main() -> Nat:
      xs = join(half(23n), half(23n))
      got(Array.get(String, xs, 0))
    

    This gives 2^24 elements and prints 1n on both C and JS. Sharing one +x between the two halves still stops on C, because it is the same value.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions