Skip to content

Checker recursion depth: valid programs with ~10k-deep terms (e.g. a 10000n literal) fail with "machine stack overflowed" #791

Closed as not planned

Description

@docxology

What you did

# lit.bend
import Base

def main() -> Nat:
  10000n

bend lit.bend (same via the bun/node import hook).

What happened

Error: the machine stack overflowed (a deep recursion, or a literal too large to expand)
  • 5000n checks and runs fine; 10000n (and everything larger up to the advertised Number.MAX_SAFE_INTEGER bound) dies.
  • The same overflow hits valid non-literal programs: a single def body with ~4,000 sequential do-block statements fails the same way while the same program at 2,000 statements compiles (in 73s — see the separate quadratic-compile report).

So the practical term-depth budget of the checker is ~10k constructor nodes / call frames, well below any documented limit, and the MAX_SAFE_INTEGER guard in parse_term_num (bend.ts:2234) is decorative: nothing between ~10k and 9e15 can ever expand.

The failure itself is handled (caught RangeError with a decent message), so this is a robustness/usability issue, not a crash: valid programs fail with a message that doesn't say which literal or construct was too deep.

Expected

  • Make the checker's recursion depth budget explicit and iterative where cheap (the Nat literal expansion loop itself is iterative; the overflow happens downstream in checking/normalization).
  • At minimum, catch the overflow at the offending span and report "this literal/term is too large (limit ~N)".

Version / system

  • bend 2.0.5 (main @ e6676b0)
  • Darwin arm64

Activity

  1. VictorTaelin commented on Sep 18, 2026

    @VictorTaelin
    Contributor

    Duplicate of #779 (same recursion: term_check <-> tele_check over the Succ chain of a Nat literal, same host-stack limit). The literal cap and the message are being decided there. Closing this one to keep the discussion in one place.

    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