Repository navigation
fix(parser): for/while gets a no-loops diagnostic, not PAR020 - #2622
Merged
Merged
Conversation
`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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
An AI tester of v1.0.0-rc.3 wrote a
for x in xs { println(x) }loop.ailang checkreported:None of the three says the construct does not exist. The cause:
for,while,loopanddoare not keywords. They lex asIDENT, and they are valid names today (let for = 1,func while(n: int), record fieldr.for,match loop {all check and run). No parser diagnostic for loops existed. The no-loops text was only in prompts and ininternal/eval_harness/errors.go.Secondary: PAR020's hint said that statements in a block are separated by
;. Butailang promptsays newline separation is canonical, it is whatailang fmtwrites, and the parser accepts it (R2,peekStartsNewlineBlockStatement). PAR020 now fires only for two statements on one line.Approach
internal/errors/codes.go. I checked that it was unallocated across internal/, cmd/, docs/, tools/ and design_docs/.gen-error-codespicks 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 inforfollowed by a newline statement stays valid:parseIdentifier:for IDENT(for x in),for (IDENT in|IDENT(for (x in xs),for (const x of xs))while+ IDENT/literal/true/false/!/notparseBlockStatement:loop {anddo {. Restricted to statement position becausematch 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.{ … }body, Allman braces included, plus ado … while (c)tail. The scan stops at EOF, at an enclosing}, or at the end of the line for a Python-stylefor x in xs:. Nothing cascades.AILANG has no loops (foundforstarting one): iterate with recursion or a std/list function such as map, foldl or forEachE. The suggestions areforEachE/map/foldl/range/ a recursive state func, all verified withailang checkandailang run. The link is https://ailang.sunholo.com/docs/reference/no-loops, which returns 200.missing separator between block statements (foundxwhere 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:
Tests
TestPAR022_ForeignLoopsGetOneNoLoopsDiagnosticcovers 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=-bodyfor (x in xs),for (const x of xs), Allman brace, Python colonwhile cond,while !done, top-levelwhile true,while (cond) {loop {,do { } while (true)TestPAR022_LoopWordsRemainValidNamesis the non-regression test. It parses with zero errors:for/while/loop/doas functions, lets, lambda params and record fieldswhile(x)call statement,match loop {,let f = forwith a newline before the next identifier, andfor(1)with a block on the next lineTestPAR020_MissingBlockSemicolonnow asserts the newline-first hint text.for_in_loop(internal/diag), with a row added tofootguns.md.examples/expected_fail/for_loop.ail, pinned byTestExpectedFail_ForLoop(internal/pipeline). It asserts PAR022 and no PAR020 or PAR_NO_PREFIX_PARSE..ailfiles under std/examples/tests/benchmarks/docs with loop-like text, PAR022 fires only on the new expected-fail example and on twoexamples/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:while (cond),loop,do)for(treated as a loop without thex inshape checkLoopWordsRemainValidNamesLoopWordsRemainValidNames. This mutation survived at first; I added thelet f = for+ newline case and it now fails;-firstTestPAR020_MissingBlockSemicolonNot covered
for (let i = 0; i < n; i++)is left alone. With 4-token lookahead it cannot be told apart from a validfor(let x = … in …)call, so it still gives the old generic error.🤖 Generated with Claude Code
Fixes #2582