Skip to content

fix(parser): accept terminator-less then/do after ]] and (( )) in compound bodies - #475

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/terminator-less-then
Oct 9, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/terminator-less-then

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Summary

Closes #460.

GNU bash accepts a terminator-less then directly after a closed [[ ]] inside any compound body — f() { if [[ $x == y ]] then echo a; fi; } parses clean, and the real-world GaelGosse/shell_function_n_shortcuts/.shortcut battery-prompt function depends on it (elif [[ $charge == "Full" ]] then). rubash rejected it with syntax error near unexpected token 'then', but only through the function-body path: the top-level parse parked the same error node invisibly inside the if-condition, so rubash -n failed only with the function wrapper.

Root cause

  • run_command_loop's "a complete compound command must be followed by a connector" check fired on the reserved word then after the [[ ]] compound. GNU grammar: if_command: IF compound_list THEN ... where compound_list: newline_list list1 (parse.y:1262) admits a single pipeline_command with no terminator (parse.y:1323-1324), and reserved_word_acceptable (parse.y:5899) has COND_END in its predecessor set, so then is recognized as THEN right after ]].
  • The (( )) form needed one more admission: rubash folds (( x == y )) into a single ARITH_CMD-shaped keyword token, so command_boundary_keyword_allowed (the section-stop predicate) did not see a command boundary before then. GNU's reserved_word_acceptable accepts ARITH_CMD as a predecessor, same as the folded { ... } group already handled there.

Changes

  • src/parser/parse_loop.rs: the compound-connector check no longer fires when the next token is the reserved word then/do at a command boundary (command_boundary_keyword_allowed).
  • src/parser/support.rs: a folded (( ... )) keyword token is a reserved-word-acceptable predecessor, alongside the existing folded { ... } case.

Rejection semantics preserved (pinned to GNU)

  • if true then / if echo hi >/dev/null then — after a plain WORD, then is still an argument → rejected, as GNU.
  • while [[ $x == y ]] then ... done (THEN/DONE mismatch) → rejected, as GNU.
  • [[ a == a ]] echo x, [[ a == ]], [[ -n — [[ ]] existing semantics and diagnostics unchanged.
  • Bonus convergence: if (( x == y )) then (top level and function body) and while (( x )) do / while [[ x == y ]] do now match GNU, which previously failed at top level too.

Testing

  • New black-box suite tests/issue460_terminator_less_then_regressions.rs (19 tests): the issue's minimal repro, function brace/subshell/same-line/nested-group bodies, elif chains, terminator shapes (;/newline/none), arithmetic-predecessor forms, and [[ ]] non-regression. Every accept/reject verdict was pinned against the GNU oracle.
  • Oracle: GNU bash 5.2.37 msys (D:\Git\usr\bin\bash.exe, bash -n). The ISSUE records WSL GNU bash 5.3.0 with identical verdicts on the repro; no 5.2/5.3 behavioral split was observed for this grammar production, but reviewers should note the recorded 5.3 oracle if any divergence surfaces in CI.
  • Full cargo test --no-fail-fast on this branch vs. stashed master: per-target results identical; no new failures (the pre-existing pwd logical-mode and cli_tests bashdb/examples failures reproduce on master in this environment). The parser suite actually gains ~16 passes from the (( ))-form convergence. Related suites (issue322_cond_*, issue360_parser_overlax_*, issue443_*, issue424_*) all green.
  • cargo fmt applied (pre-commit hook passed).

@caomengxuan666
caomengxuan666 merged commit 87362a0 into master Oct 9, 2026
8 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/terminator-less-then branch October 9, 2026 15:37
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.

[parse] terminator-less 'then' after ]] inside a function body rejected — GNU accepts (breaks shell_function_n_shortcuts/.shortcut)

1 participant