Skip to content

Type checking a large Nat literal overflows the host stack without identifying the literal #779

Closed

Description

@iamjoaovytor

What you did

bend main.bend

What happened

Error: the machine stack overflowed (a deep recursion, or a literal too large to expand)

The source contains a single Nat literal and no recursive function.

On my environment (Bend 2.0.5, Bun 1.3.3, macOS 27.0 build 26A428, Apple M4 Pro, arm64):
- 5314n succeeds and prints 5314n.
- 5315n fails with the error above.
I reproduced both cases with bend main.bend and with bend main.bend -o main. The successful native executable also prints 5314n.

These are observed thresholds in this environment, not a documented language limit.

Cause investigated at commit e6676b0:
The parser already expands Nat literals iteratively into Succ constructors (bend2/bend.ts:2248-2250). Calling book_load and book_valid directly shows that loading succeeds and the RangeError occurs in book_valid, with this repeating stack:

RangeError: Maximum call stack size exceeded.
    at term_check (bend2/bend.ts:3480:21)
    at tele_check (bend2/bend.ts:1111:19)
    at term_check (bend2/bend.ts:3497:26)
    at tele_check (bend2/bend.ts:1111:19)
    ...

The CLI catches RangeError in bend2/main.ts:469-474 and replaces it with the generic message above. That message identifies neither the literal nor its source location. In a larger program, it is difficult to distinguish this from recursion in user code.

Expected:
- Avoid exhausting the host stack while checking the expanded literal, or reject unsupported depth with a clear diagnostic.
- Identify the offending literal and its source location when possible.
- If an intentional depth limit is introduced, document it and report it explicitly; 5314 should not be treated as a universal cap.

The file

import Base

def main() -> Nat:
  5315n

bend --version

bend 2.0.5

uname -sm

Darwin arm64

clang --version (the first line)

Apple clang version 21.0.0 (clang-2100.3.34.2)

Activity

  1. added
    bug
    on Sep 18, 2026
  2. phenomenon0 commented on Sep 18, 2026

    @phenomenon0

    Extra data points on this class, from a fork experiment (main 0b7e2b11; also reproduced via the 2.0.5 CLI):

    • Minimal case: def main() -> Nat: 4096n.
      • Non-deterministic on one machine: idle, it frequently fails in <1 s with RangeError: Maximum call stack size exceeded (0–2/5 pass across several series); with other CPU cores busy, the identical command passes (3/3 recorded).
    • Book-size sensitive: the same file passes 5/5 against a 27,844-ttok base.bend, and flakes frequently against a 40,017-ttok base.bend — the literal's depth fits or doesn't depending on how much stack headroom the book already spent.
    • The fault surfaces as a plain JS stack exception through tele_check ← term_check (unlike Checker recursion depth: valid programs with ~10k-deep terms (e.g. a 10000n literal) fail with "machine stack overflowed" #791's machine stack overflowed diagnostic), so the depth limit arrives as an exception rather than a clean error.
    • Workaround in use: express large counts as products (Nat.mul(64n, 64n), depth ≈ 64) — deterministic pass.
  3. etafund commented on Sep 18, 2026

    @etafund

    A distinct resource-exhaustion regime on bend 2.0.5 (main @ e6676b0, Linux x86_64, bun 1.4.2): a sufficiently large legal literal never reaches a clean error at all.

    import Base
    
    def main() -> Nat:
      281474976710655n
    

    bend main.bend produced no output for 90+ seconds at 99% CPU before being killed. (2^48−1 is the documented runtime immediate cap; the parser accepts literals up to Number.MAX_SAFE_INTEGER, so this literal is in-bounds as far as the parser is concerned.)

    Unlike the moderate-literal case in this issue (validation stack overflow after parsing), the cost here appears to be the unary expansion itself — roughly one constructor node per successor, i.e. on the order of 10^14 nodes for this literal — though I did not instrument phases; the practical observation is "no diagnostic, unbounded work," and the threshold for overflow-vs-never-finishing varies with the host stack.

    A hard, documented literal-size limit with a clean message (the U32 literal path already reports cleanly: expected : a u32 literal up to 4294967295) would turn both regimes into portable, legible errors.

  4. VictorTaelin commented on Sep 18, 2026

    @VictorTaelin
    Contributor

    Fixed in 2.0.7. A Nat literal up to 256n still expands into its Succ chain (small literals must unfold in patterns); a larger literal parses as U32.to_nat(n), an intrinsic on every lane, up to 4294967295n; above that the parser says a nat literal up to 4294967295n (got ...). 5315n, 100000n and 281474976710655n no longer overflow or spin: the first two check, prove ({1000n == 1n+999n} reduces) and print on the interpreter, C and JS; the last is a clean parse error. Thanks to @phenomenon0 and @etafund for the extra cases. The general ~10k-deep host-stack limit of the checker on other shapes stays a known item.

    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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions