Skip to content

fix(parser): for/while gets a no-loops diagnostic, not PAR020 - #2622

Merged
sunholo-voight-kampff merged 2 commits into
devfrom
fix/parser-no-loops-diagnostic
Dec 2, 2026
Merged

sunholo-voight-kampff merged 2 commits into
devfrom
fix/parser-no-loops-diagnostic

Conversation

@sunholo-voight-kampff

@sunholo-voight-kampff sunholo-voight-kampff commented Dec 2, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

An AI tester of v1.0.0-rc.3 wrote a for x in xs { println(x) } loop. ailang check reported:

PAR020 at bad.ail:4:7: missing ';' between block statements (found `x` where `;` or `}` was expected)
PAR_NO_PREFIX_PARSE ... unexpected token in expression: in
PAR_NO_PREFIX_PARSE ... (closing brace)

None of the three says the construct does not exist. The cause: for, while, loop and do are not keywords. They lex as IDENT, and they are valid names today (let for = 1, func while(n: int), record field r.for, match loop { all check and run). No parser diagnostic for loops existed. The no-loops text was only in prompts and in internal/eval_harness/errors.go.

Secondary: PAR020's hint said that statements in a block are separated by ;. But ailang prompt says newline separation is canonical, it is what ailang fmt writes, and the parser accepts it (R2, peekStartsNewlineBlockStatement). PAR020 now fires only for two statements on one line.

Approach

  • New code PAR022, registered in internal/errors/codes.go. I checked that it was unallocated across internal/, cmd/, docs/, tools/ and design_docs/. gen-error-codes picks it up.
  • internal/parser/parser_no_loops.go. Detection goes by shape, and only when the next token is on the same line, so a line ending in for followed by a newline statement stays valid:
    • Any expression position, hooked in parseIdentifier:
      • for IDENT (for x in), for (IDENT in|IDENT (for (x in xs), for (const x of xs))
      • while + IDENT/literal/true/false/!/not
      • In each of these, the name followed by that token can never be a valid expression.
    • Statement position only, hooked in parseBlockStatement:
      • loop { and do {. Restricted to statement position because match loop { is a valid scrutinee.
      • while(cond) { / for(...) {, recognised after the call parses. A call followed by a same-line { is never a valid statement.
  • One diagnostic, then the loop is skipped: the header and the balanced { … } body, Allman braces included, plus a do … while (c) tail. The scan stops at EOF, at an enclosing }, or at the end of the line for a Python-style for x in xs:. Nothing cascades.
  • Message: AILANG has no loops (found for starting one): iterate with recursion or a std/list function such as map, foldl or forEachE. The suggestions are forEachE / map / foldl / range / a recursive state func, all verified with ailang check and ailang run. The link is https://ailang.sunholo.com/docs/reference/no-loops, which returns 200.
  • PAR020 message: missing separator between block statements (found xwhere a newline,;or} was expected). The first suggestion is now one statement per line (canonical, ailang fmt). The one-line ; form comes second.

Before/after for the repro:

before: PAR020 at bad.ail:4:7: missing ';' between block statements ... + 2× PAR_NO_PREFIX_PARSE
after:  PAR022 at bad.ail:4:3: AILANG has no loops (found `for` starting one): iterate with recursion or a std/list function such as map, foldl or forEachE
        See: https://ailang.sunholo.com/docs/reference/no-loops

Tests

  • TestPAR022_ForeignLoopsGetOneNoLoopsDiagnostic covers 13 shapes and asserts that each gives exactly one error, PAR022, a forEachE suggestion and the docs URL. The shapes:
    • for x in xs { in a block, at top level, and as an =-body
    • for (x in xs), for (const x of xs), Allman brace, Python colon
    • while cond, while !done, top-level while true, while (cond) {
    • loop {, do { } while (true)
  • TestPAR022_LoopWordsRemainValidNames is the non-regression test. It parses with zero errors:
    • for/while/loop/do as functions, lets, lambda params and record fields
    • a while(x) call statement, match loop {, let f = for with a newline before the next identifier, and for(1) with a block on the next line
  • TestPAR020_MissingBlockSemicolon now asserts the newline-first hint text.
  • Footgun fixture for_in_loop (internal/diag), with a row added to footguns.md.
  • examples/expected_fail/for_loop.ail, pinned by TestExpectedFail_ForLoop (internal/pipeline). It asserts PAR022 and no PAR020 or PAR_NO_PREFIX_PARSE.
  • Corpus sweep: of 273 .ail files under std/examples/tests/benchmarks/docs with loop-like text, PAR022 fires only on the new expected-fail example and on two examples/experimental/ files that were already failing for unrelated reasons.

Gates (run unpiped): make test-changed, make test-parser, make lint (0 issues), make check-file-sizes, make check-changelog. All pass.

Mutation result

I applied each mutation to a scratch copy and restored afterwards (no git checkout). All 6 were killed:

Mutation Killed by
expression-position hook disabled 10 PAR022 subtests
statement-position hooks disabled 3 subtests (while (cond), loop, do)
for( treated as a loop without the x in shape check LoopWordsRemainValidNames
same-line check dropped LoopWordsRemainValidNames. This mutation survived at first; I added the let f = for + newline case and it now fails
cascade skip removed 12 subtests
PAR020 hint reverted to ;-first TestPAR020_MissingBlockSemicolon

Not covered

for (let i = 0; i < n; i++) is left alone. With 4-token lookahead it cannot be told apart from a valid for(let x = … in …) call, so it still gives the old generic error.

🤖 Generated with Claude Code

Fixes #2582

sunholo-voight-kampff and others added 2 commits December 2, 2026 16:17
`for`, `while`, `loop` and `do` lex as identifiers, so `for x in xs {`
reported PAR020 "missing ';'" on `x` plus PAR_NO_PREFIX_PARSE on `in`
and `}`. A new PAR022 recognises never-valid loop shapes (same line
only) in expression and statement position, reports one diagnostic
with std/list / recursion replacements and the no-loops docs link, and
skips the loop header and body so nothing cascades. The words remain
valid names.

PAR020's hint now leads with newline separation (the canonical form
`ailang fmt` writes); `;` is the one-line alternative.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The error-fails-correctly fixture is a deliberate `loop { 1 }`. With the
no-loops diagnostic its first error is PAR022, the intended better code,
not PAR_UNEXPECTED_TOKEN. Every CI job on this PR failed on this one test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sunholo-voight-kampff
sunholo-voight-kampff merged commit 4f5db77 into dev Dec 2, 2026
23 checks passed
@sunholo-voight-kampff
sunholo-voight-kampff deleted the fix/parser-no-loops-diagnostic branch December 2, 2026 16:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A for/while loop gives a misleading PAR020 "missing ';'" instead of a no-loops diagnostic; PAR020 hint leads with ';'

1 participant