Skip to content

fix(parser): coproc rejects '!' in command position as a syntax error (rubash#1652) - #1693

Merged
arookieofc merged 1 commit into
masterfrom
fix-1652-coproc-bang-syntax
Oct 28, 2026
Merged

arookieofc merged 1 commit into
masterfrom
fix-1652-coproc-bang-syntax

Conversation

@arookieofc

Copy link
Copy Markdown
Member

Fixes #1652.

GNU parse.y:1125-1175 coproc grammar is COPROC [WORD] shell_command, and ! is a pipeline modifier, not a shell_command, so coproc ! ... is a syntax error (rc 2). Rubash collected the ! as a simple-command word and ran the coproc with the negation silently applied inside the child.

Two positions error, both verified against GNU bash 5.3.20:

  • body position: coproc ! ... (the ! keyword is also excluded from NAME candidacy, so coproc ! { ...; } no longer falls through to the runtime 'not a valid identifier' error);
  • second word of the simple command: coproc a ! ..., coproc NAME ! ... (after COPROC WORD the parser commits to the named form and demands a shell_command).
    A ! after two or more words stays a plain word (coproc a b ! c, coproc echo hi !), as does a quoted !.

The error reuses the __RUBASH_PARSE_ERROR_NEAR__ node shape (case #1641): the executor reports syntax error near unexpected token \!'` with the offending line echoed and aborts the script at that line with status 2.

Notes from the investigation, deliberately out of scope here:

  • coproc time ! ... also errors in GNU but is still accepted here: it goes through the separate time-prefix coproc path, and coproc time <simple> itself diverges (GNU runs it as a simple command whose argv[0] is time, i.e. 'time: command not found' in the child; rubash runs a timed pipeline). That path needs its own issue.
  • Pre-existing cosmetic leak, unchanged by this PR: a quoted invalid coproc name (coproc \"!\" { ...; }) prints a lexer marker byte before the ! in the 'not a valid identifier' diagnostic (verified identical on master).

Verification (local, master 499a5e2 + this fix, vs GNU):

  • Error cases (coproc ! echo hi, coproc a ! b, coproc NAME ! echo hi, coproc ! { echo hi; }, bare coproc !) all produce GNU-identical diagnostics and exit 2; earlier script lines run, later lines do not.
  • New regression file tests/issue1652_coproc_bang_syntax_regressions.rs: 6/6 passed; existing coproc suites (parser_coproc_tests, issue352, issue387, issue1528) still pass.

@caomengxuan666

Copy link
Copy Markdown
Member

Thanks for this PR — we verified it on your tree (cherry-picked onto master 160e792, full build + probes against the GNU bash 5.3.20 oracle). Your own tests pass (6/6, plus parser 31/31), and 5 of 6 probe shapes match GNU byte-for-byte. One shape does not, and it is a real divergence:

coproc x=1 ! echo hi is mis-rejected on your tree.

  • Your tree: syntax error near unexpected token '!', rc=2, command never runs.
  • GNU bash 5.3.20: legal — prints hi, rc=0.

Reading the diff, the cause looks structural: the name-collection check that rejects reserved words / operators in coproc position has no exemption for a leading assignment word, so x=1 followed by ! is parsed as if ! were in command position after a bare name. GNU settles the coproc NAME first (an assignment prefix is not a name candidate), so the pipeline negation is accepted.

Could you add the assignment exemption to the collection check and a regression test for this shape? Once that lands we will re-verify this one shape and merge — everything else in the PR is already green on our side. (For context: our in-house slice for the same issues is being stood down in favour of your PRs wherever they verify, which is why several sibling coproc PRs of yours were merged today.)

… (rubash#1652)

GNU parse.y:1125-1175 coproc grammar is COPROC [WORD]
shell_command, and '!' is a pipeline modifier, not a
shell_command, so 'coproc ! ...' is a syntax error (rc 2).
Rubash collected the '!' as a simple-command word and ran
the coproc with the negation silently applied inside the
child.

Two positions error, both verified against GNU bash 5.3.20:
- body position: 'coproc ! ...' (the '!' keyword is also
  excluded from NAME candidacy, so 'coproc ! { ...; }' no
  longer falls through to the runtime 'not a valid
  identifier' error);
- second word of the simple command: 'coproc a ! ...',
  'coproc NAME ! ...' (after COPROC WORD the parser commits
  to the named form and demands a shell_command).
A '!' after two or more words stays a plain word ('coproc
a b ! c', 'coproc echo hi !'), as does a quoted '!'.

The error reuses the __RUBASH_PARSE_ERROR_NEAR__ node shape
(case #1641): the executor reports 'syntax error near
unexpected token `!\'' with the offending line echoed and
aborts the script at that line with status 2.

Regression tests:
tests/issue1652_coproc_bang_syntax_regressions.rs (6 tests).
@arookieofc
arookieofc force-pushed the fix-1652-coproc-bang-syntax branch from c1d4f62 to 70689f6 Compare October 28, 2026 02:23
@arookieofc
arookieofc merged commit b028f3b into master Oct 28, 2026
9 checks passed
caomengxuan666 added a commit that referenced this pull request Oct 29, 2026
…ection (rubash#1983)

GNU's lexer returns an assignment-shaped first word as
ASSIGNMENT_WORD, and parse.y:5899 reserved_word_acceptable has no
ASSIGNMENT_WORD in its accept set, so a `!` after `coproc X=1`
never becomes BANG — it lexes as a plain WORD and lands as the
child command's name (GNU bash 5.3.20: the child reports
`!: command not found`, script rc 0). The #1693 implementation of
the #1652 rejection fired for every one-word collection, so the
GNU-legal spaced shape `coproc X=1 ! echo hi` was rejected rc 2.

Exempt assignment-shaped first words (name / name[subscript],
optional `+`, then `=`), judged on the raw spelling — a quoted
name is not an assignment word in GNU either. Non-name shapes
(`9a=1`, `a-b=1`) stay WORDs and keep the rc-2 rejection. The
word_metadata Vec gets an explicit element type: the exemption
reads [0].raw before the first push, which leaves the element
type ambiguous to inference (E0282). Judgment shape first
measured in the #1712 line's 537e177.

Regression pins: tests/issue1983_coproc_assignment_bang_regressions.rs
(X=1 / a+=1 / _a9= spaced forms, a[1]=x subscript form, 9a=1 /
a-b=1 negative controls, glued X=1! control). The pins assert the
parse-admission face only; the coproc wait status after the
output fd drains is a separate, pre-existing reap-face race
(GNU deterministically reports 0 there) and is deliberately not
pinned here.
@caomengxuan666
caomengxuan666 deleted the fix-1652-coproc-bang-syntax 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.

[bug] coproc accepts '!' negation prefix; GNU rejects with syntax error (negation silently dropped)

2 participants