Skip to content

Parser: add a depth guard mirroring the type-checker's MAX_EXPR_DEPTH #15

Closed

Description

@hyperpolymath

Follow-up to #1 / #12.

The type-checker now bails out cleanly on overly-deep expressions (MAX_EXPR_DEPTH = 256, see crates/my-lang/src/checker.rs). The parser, however, is recursive-descent and has no such guard: on similarly pathological inputs, parse_expr recurses through every precedence level (parse_or_expr → … → parse_postfix_expr → parse_primary_expr → parse_paren_expr → parse_expr again) and overflows the thread stack with a SIGABRT ("has overflowed its stack, aborting") before the checker ever sees the AST.

This was demonstrated incidentally while writing the regression test for #12: parsing a depth-528 str_concat(...) chain SIGABRTs the test runner. The fix in PR #12 sidesteps it by constructing the AST programmatically.

What to do

  1. Add a MAX_PARSE_EXPR_DEPTH constant (suggested: same as the checker's, or slightly higher, but well below stack-overflow territory for cargo test defaults).
  2. Track recursion depth in the Parser struct (a usize counter, incremented at the entry of parse_expr and decremented on exit).
  3. When exceeded, emit a single ParseError::ExpressionTooDeep { line, column, limit } and stop recursing — let the existing error-recovery machinery (test_error_recovery_*) handle the rest of the file.
  4. Add a regression test that feeds in a generated depth-N source and asserts the parse fails with the new error variant instead of SIGABRT.

Related

Out of scope

  • Switching the parser to iterative precedence parsing (Pratt / shunting yard). That's a larger refactor; the depth guard is the cheap, safe intermediate step.

Activity

  1. added
    bug
    on May 11, 2026
  2. added a commit that references this issue on May 15, 2026
    e0e6b6b
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

    bugparser

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions