Skip to content

JS lane: List.length over a long list dies with a stack overflow (the base recursion is not in tail position); a tail counter over the same list completes #802

Closed

Description

@phenomenon0

What you did

A 200,000-element list (built by a tail-recursive helper, no large literals), counted two ways:

  • bend llist.bend (JS lane), List.length
  • bend llist.bend -o llist && ./llist (native C lane)
  • same with an inner tail counter instead of List.length

What happened

JS lane, List.length:

bend: memory fault (machine stack overflow?)

JS lane, tail counter over the same list: len=200000. Native, both: len=200000.

Threshold on the JS lane: 10,000 elements count fine, 50,000 fails. Base.List.length is not tail-recursive (1n+List.length(a, A, t), bend2/base.bend:742-747 at 0b7e2b1), so the emitted JS keeps one frame per element. Same class as #798 for String.split/String.length (which is why our parser lane avoids it with a counter), but this is the List helper. Reproduces on 2.0.4 and 2.0.5.

The file

import Base

def gen(n: Nat, acc: List<&2, U32>) -> List<&2, U32>:
  match n:
    case 0n:
      acc
    case 1n+p:
      gen(p, 1 <> acc)

def main() -> IO(Unit):
  do IO<Unit>:
    IO.print("len=" ++ Nat.show(List.length(&2, U32, gen(Nat.mul(100n, 2000n), Nil{}))))

The counter variant replaces List.length(...) with count.go(gen(...), 0n); count.go is Nil{} -> n; h <> t -> count.go(t, 1n+n).

bend --version

bend 2.0.5 (2.0.4 the same)

uname -sm

Linux x86_64

clang --version (the first line)

clang version 21.1.7 (Fedora 21.1.7-1.fc43)

Activity

  1. VictorTaelin commented on Sep 18, 2026

    @VictorTaelin
    Contributor

    Same root cause as #798 (a non-tail call is a host frame on the JS lane). String.length got an intrinsic in 2.0.7; List.length and the other folds wait for continuation passing in the JS emitter, which we will add soon (WONTFIX.txt, SOON). Closing to keep the thread in #798.

    Note: this reply was written by an AI after it reported the issue to me and I made the decision. If anything here is wrong, reply and I will review it myself.

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