Skip to content

fix(reader): end grouped gather at a stray close inside an open group (niubash#482) - #955

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/482
Oct 21, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/482

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Summary

Fixes unixwin/niubash#482 (engine half): a stray ) inside an OPEN multi-line grouped command made every gather loop unbounded — the reader waited forever for the unclosed opener, so niu -i and the REPL sat in PS2 with no diagnostic. GNU abandons the group at the token: syntax error near unexpected token ')', rc 2, PS1 recovery.

The niubash#431 stray-close guard only audited input already judged complete; an unresolved opener meant the parser never saw the ). Root cause: the completeness readers had no terminal condition for an unclosable close.

  • Token::case_pattern_close: lexer stamps the ) that ends a case pattern list (PST_CASEPAT, parse.y:3787-3788) — the one legal ) that pops no paren.
  • stdin_source_needs_more_posix + GroupScanFeeder::fold_token: an unclosable ) (and ;;-family outside case/arith, and foreign keyword closers under keyword openers) ends the group — the armed parse then owns the diagnostic.
  • unclosed_brace_eof_node / unclosed_paren_eof_node run first_mismatched_closer before the EOF node, so the abandoned group reports unexpected token ')' at the stray line (GNU print_offending_line/yyerror, parse.y:6813-6824) instead of unexpected end of file from '{'.
  • first_mismatched_closer honors the flag: ) inside a case BODY is stray; pattern closers stay legal.

Refs unixwin/niubash#482, unixwin/niubash#431.

Test plan

  • { echo c\n) stdin/-s/-c/run_interactive_stdin: line 2: syntax error near unexpected token ')' + verbatim echo, rc 2 — byte-matches GNU 5.2.37 script-file probes (WSL forbidden by task)
  • f() {\n), { echo c\necho a), case x in\na) e\n) → stray-token diagnostic at the right line, session recovers
  • Normal groups: { }, ( ), case incl. (a|b), f() {}, for ((;;)), a=(1 2\n), $() — all still gather and run
  • niubash regression suite tests/issue482_stray_close_gather.rs (8 tests) green on the patched build

… (niubash#482)

GNU's reader reports `syntax error near unexpected token `)'' the moment
a `)` that closes no `(` and is not a case-pattern terminator is read
inside an open compound — yyerror + jump_to_top_level abandons the whole
group and the interactive shell returns to PS1 (parse.y yyerror /
print_offending_line 6813-6824; brace_group grammar parse.y:1196).
The completeness readers had no such terminal condition: a stray `)`
inside an unclosed `{`/function body kept the group open forever, so
run_interactive_stdin / run_stdin_script / read_next_source_group wedged
in PS2 with no diagnostic, and the niubash#431 stray-close guard only
armed on input already judged complete.

- Lexer stamps `Token::case_pattern_close` on the `)` that leaves
  PST_CASEPAT (scanner.rs record_token, parse.y:3787-3788): it is the one
  legal `)` that pops no paren.
- stdin_source_needs_more_posix and GroupScanFeeder::fold_token now end
  the group on an unclosable `)` and on `;;`/`;&`/`;;;&` outside
  case/arithmetic — the "complete" verdict hands the offending text to
  the armed parse, which owns the diagnostic.
- unclosed_brace_eof_node/unclosed_paren_eof_node run the existing
  first_mismatched_closer scan before the EOF node, so the abandoned
  group reports `unexpected token `)'' at the stray line (GNU) instead
  of `unexpected end of file from '{''.
- first_mismatched_closer honors the new flag so `)` inside a case BODY
  is recognized as stray while pattern closers stay legal.

Verified vs Git Bash GNU 5.2.37 script-file probes (WSL forbidden by
task): `{ echo c\n)` / `f() {\n)` / `case x in\na) e\n)` all print
`line 2/3: syntax error near unexpected token `)'' + verbatim echo,
rc 2, session recovers and runs the next line — byte-matching output.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@caomengxuan666
caomengxuan666 merged commit 95700a5 into master Oct 21, 2026
8 checks passed
@caomengxuan666

Copy link
Copy Markdown
Member Author

Review report (pre-merge verification, all green)

Reviewed as the engine half of the paired niubash#482 fix. Merged as 95700a5 (--squash); post-merge master CI confirmation to follow in this thread.

GNU conformance (oracle: Git Bash GNU bash 5.2.37, script-file / -c / piped -i): every ticket shape byte-matches — { echo c\n), f() {\n), { echo c\necho a), case x in\na) e\n), if true; then\n) all report line N: syntax error near unexpected token ')' + verbatim offending-line echo, rc 2, PS1 recovery; ( echo c\n) and (a|b) patterns stay legal; empty case x in esac (single- and multi-line) unchanged.

Red/green discriminative check (this machine):

  • fix/482 build: -c/-s/-i faces all produce the GNU token diagnostic at the stray line, -i recovers (rc=2-recovered runs), exactly one diagnostic printed.
  • db5a4134 (master) build: -c/-s report the old EOF-from-{ diagnostic; -i wedges in PS2 swallowing every later line — the ticket's defect, reproduced.

No regressions: targeted suites on the branch and on a local merge of fix/482 into current master — lexer 94/94, script_driver 19/19, issue845 7, issue381 9, issue435 7, issue516 7, issue512 11, issue564 6, issue508 5 (all pass); normal-group battery ({ }, ( ), f() {}, if/fi, case a), case (a|b)) green; merge-tree clean, no test in either repo pins the old EOF diagnostic.

Pairing: niubash#484 (host half) verified in combination locally — its 8/8 issue482_stray_close_gather + 8/8 issue431_stray_close_paren + 68/68 repl:: pass against this engine. With this engine landed, niubash-master's own failure set on that suite shrinks 6 → exactly the 2 host-only faces (mid_command, case_body), confirming the two halves are complementary with zero double-termination. niubash#484 itself is on hold with a read-only review comment (one-line lockfile fix needed; CI red is mechanical, not semantic).

One environment note for the record: the winuxcmd-bundled GNU bash 5.3.0 (msys) swallows a stray ) inside an open { (EOF diagnostic, no token error) — 5.2.37 and the WSL 5.3.0 ticket transcript both error at the token, so the conformance target chosen here is the right one, but the bundled-5.3.0 divergence is worth knowing about for future reader work.

caomengxuan666 added a commit to unixwin/niubash that referenced this pull request Oct 21, 2026
…#484)

* fix(repl): terminate the gather on a stray close inside an open group (niubash#482)

`{ echo c` opens a brace group; the next line `)` closes nothing — GNU
reports `syntax error near unexpected token `)'' at that line, abandons
the whole group, rc 2, and the session returns to PS1. Niubash's input
completeness scanner had no such terminal condition: the open `{` kept
the buffer "incomplete" forever, so the REPL (reedline, --noediting and
piped `-i` alike) wedged in PS2 with no diagnostic. The niubash#431
stray-close guard only audits COMPLETE groups, which this input never
became.

- scan_repl_input now emits `;;`/`;&`/`;;;&` as single operators (they
  are case-clause terminators, not two `;`).
- is_repl_input_complete tracks an open `case` by phase (operand / in /
  pattern / body): `)` and `;;`-family that cannot close anything are
  judged complete so the armed engine parse reports GNU's diagnostic
  instead of gathering forever; `a)`/`(a|b)`/`;;` inside case stay
  legal so real multi-line cases still gather.
- Foreign keyword closers (`fi`/`done`/`esac`/`}`) mismatched under a
  keyword opener end the gather the same way — GNU's read-time verdict;
  under `(`/case frames they remain legal words (arithmetic, patterns).
- Engine side (unixwin/rubash#955): the group gather stops at the stray
  token and the unclosed-`{`/`(` EOF paths report the mismatched closer
  at its line instead of `unexpected end of file`.

GNU 5.2.37 script-file probes (WSL forbidden by task): `niu -i`, stdin,
`-s` and `-c` transcripts all byte-match GNU for the ticket family.

Tests: tests/issue482_stray_close_gather.rs — 8 cases covering the
ticket repro, PS1 recovery + rc=2, mid-command/case-body strays and a
normal-group battery; `cargo test -p niubash-runtime --lib repl::` 68/68.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* chore(deps): add sha2 to Cargo.lock (rubash master dep shift)

Same one-line fix as 9f4621a on master: rubash master gained a sha2
dependency (#939 provenance work); this branch's lock was refreshed
against a pre-sha2 checkout. Unblocks the 8 --locked CI jobs.

---------

Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@caomengxuan666
caomengxuan666 deleted the fix/482 branch October 30, 2026 01:41
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.

[P1][reader] stray ")" inside an open multi-line group wedges the interactive REPL in PS2 with no diagnostic (GNU 5.3 recovers)

1 participant