Skip to content

fix(eval): hold the extglob parse gate for the whole eval unit (Refs #2876) - #2912

Merged
caomengxuan666 merged 3 commits into
masterfrom
fix/2876-eval-extglob-gate
Nov 6, 2026
Merged

caomengxuan666 merged 3 commits into
masterfrom
fix/2876-eval-extglob-gate

Conversation

@caomengxuan666

@caomengxuan666 caomengxuan666 commented Nov 6, 2026 •

Copy link
Copy Markdown
Member

Fixes #2876 — UNVERIFIED-BY-CAPTAIN (lane delivery; captain verifies centrally)

What this fixes

With extglob on, an eval'd compound array assignment whose element carries an extglob pattern was a syntax error for all five operators (! + * ? @), in scripts, -c strings, functions, and $( ). This is the bash-completion _comp_expand_glob shape (eval -- "$1=($2)") that broke shell startup with the distro bash-completion 2.14 loaded.

GNU keeps ONE live extglob value for parse and execution alike: builtins/shopt.def:640 (shopt_set_extglob: extended_glob = extglob_flag), parse.y:3502 (reset_parser: extended_glob = extglob_flag), and builtins/evalstring.c parse_and_execute parses and runs the string against that single value. Two rubash reads disagreed with it:

  1. The eval-string shape scan rejected the string before the real parse ran. lexer::unclosed_comsub_eof_shape (the eval-family EOF diagnostics report the function-definition line instead of the invocation line (GNU: call site) #318/Regression gate gap: upstream_suite_slice_snapshots (errors.snap) red since before 12374592 — errors.tests stdout collapses 112→9 lines under stdin=null; snapshot suites absent from CI #333 diagnostic classifier in front of execute_eval) treated EVERY ( inside a compound-assignment list as the parse.y:7173-7179 token error. But read_token_word (parse.y:5466, PST_COMPASSIGN) consumes word-internal openers into the element word — $(, $((, <(, >(, a ${...} body's parens, and a pattern operator's group under the live gate — so the classifier also rejected arr=($(cmd)), arr=($((1+2))), arr=(${y:-(a)}) through eval (all verified against GNU 5.3.20). The scan now models those constructs (new ExtglobGroup / Brace / BraceParen frames, backslash-escaped opener tracking) and keeps the token error for a BARE ( only — including the escaped-opener negatives GNU keeps (x=(\$(a)), x=(a\!(b)) stay errors). The eval caller passes the live option state (the same value the parse pin uses); the script-driver caller passes the parse gate in force for its unit.

  2. The parse gate was restored before execution. Executor::execute_eval_source_inner pinned PARSE_EXTENDED_GLOB to the live shopt state only around tokenize + parse and restored it before the eval'd text executed, while compound-assignment element splitting (split_compound_assignment_words, src/parser/assignment.rs) reads the gate at execution time. The pin now lives in Executor::execute_eval_source — the parse_and_execute funnel shared by eval and the mapfile -C callback — for the whole unit, parse AND execution. On exit the gate is re-pinned to the live option state as it then stands, so a shopt -s/-u extglob executed by the eval'd text itself persists exactly as GNU's single variable does. Trap action strings use their own adjacent parse+execute site and are untouched.

Pins (fail-first)

New stem tests/issue2876_eval_extglob_array_regressions.rs, 7 tests: five operators through eval; the _comp_expand_glob call shape against real files; non-regression controls (direct assignment, eval×case from #1114, eval×bare glob word, nested eval, trap action); extglob-off parity controls (GNU rc=1, and enabling extglob inside the same string does not rescue it); shopt toggles inside eval persisting across evals; the word-internal construct family (comsub / arithmetic / procsub / ${} bodies, plus bare and escaped negatives); function + command-substitution contexts.

On master e8f1bb04: 1 passed / 5 failed of the original 6 (the pass is the extglob-off parity control). Corpus check for #2900: sourcing the real bash-completion 2.14.0 file (3,349 lines, sha256 859ddde4…) and calling _comp_expand_glob reproduces the user's startup symptom on master (eval line 356 syntax error, empty result); GNU 5.3.20 and the fixed build both return the two non-.bak entries.

本 PR 删除了什么

  • The parse-only pin/restore pair inside execute_eval_source_inner (the saved-gate snapshot and its immediate post-parse restore) — replaced by the whole-unit pin in execute_eval_source.
  • The shape scan's blanket "every ( in a compound list is a token error" rule — replaced by the word-internal-construct model; the bare-( error itself is kept.
  • No behavior surface, test, or public API is deleted.

Net lines

Total +453 / −37 across 4 files (2 commits: pins 153459b5, fix 2365ca40): new test stem +259; src/lexer/skip.rs +133 / −12 net; src/executor/trap_exec.rs +57 / −23 net; src/script_driver.rs +4 / −2 net.

Lane validation (captain re-verifies centrally)

  • cargo check --locked --all-targets: clean.
  • Pins: 7 passed / 0 failed (master: 1/5 of the original 6).
  • Full lib suite: 1139 passed / 0 failed / 3 ignored.
  • Neighbourhood (26 eval / extglob / compound-assignment stems): 23 fully green; the 3 red stems are pre-existing environment artifacts with identical counts before and after this change — issue332 + issue637 expect a literal bash: argv0 prefix in diagnostics (the local binary path differs; content otherwise identical), issue1639 is a PTY Ctrl-C timing test whose failure count varies between runs (1→2) under load.
  • GNU 5.3.20 differentials (probes in the lane workdir): five operators, completion shape, controls, persistence, contexts, and the word-internal construct composite all byte-identical.

Fail-first pins for #2876: with extglob on, an eval'd compound array
assignment whose element carries an extglob pattern dies with a
syntax error for all five operators, in callers, functions and
command substitution, because the eval path restores the parse gate
right after its reparse while compound-assignment element splitting
reads the gate at execution time.

GNU keeps one live value: builtins/shopt.def:640 (shopt_set_extglob
writes extended_glob = extglob_flag), parse.y:3502 (reset_parser:
extended_glob = extglob_flag), builtins/evalstring.c
parse_and_execute parses and runs the string against that value.

On master e8f1bb0 these pins read 1 passed / 5 failed; the single
pass is the extglob-off parity control, which already agrees with
GNU and must keep agreeing.
Two gate reads disagreed with GNU's single live extglob_flag
(builtins/shopt.def:640 shopt_set_extglob writes
extended_glob = extglob_flag; parse.y:3502 reset_parser propagates
it; builtins/evalstring.c parse_and_execute parses and runs the
string against it):

1. The eval-string shape scan (lexer::unclosed_comsub_eof_shape)
   classified EVERY `(` inside a compound-assignment list as the
   parse.y:7173-7179 token error, before the real parse ever ran.
   read_token_word (parse.y:5466, PST_COMPASSIGN) consumes
   word-internal openers into the element word — `$(`, `$((`, `<(`,
   `>(`, a `${...}` body's parens, and a pattern operator's group
   under the live gate — so an eval'd `arr=(x/!(a))`, `arr=($(cmd))`
   or `arr=(${y:-(a)})` all died with the same syntax error. The
   scan now models those constructs (new ExtglobGroup / Brace /
   BraceParen frames, escaped-opener tracking) and keeps the token
   error for a BARE `(` only; the eval caller passes the live option
   state, the script driver passes the parse gate in force.

2. execute_eval_source_inner pinned PARSE_EXTENDED_GLOB only
   around tokenize + parse and restored it before execution, while
   compound-assignment element splitting reads the gate at
   execution time (split_compound_assignment_words). The pin moves
   to execute_eval_source — the parse_and_execute funnel shared by
   eval and the mapfile -C callback — for the unit's whole
   duration, and on exit re-pins to the live option state as it
   then stands, so a shopt toggle executed by the eval'd text
   persists exactly like GNU's single variable.

Also pins the word-internal construct family through eval (comsub,
arithmetic, procsub, ${} bodies, and the bare/escaped negatives).
@caomengxuan666
caomengxuan666 merged commit 752e6ea into master Nov 6, 2026
19 of 20 checks passed
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] eval'd compound array assignment with an extglob element is a syntax error even with extglob on (bash-completion _comp_expand_glob shape)

1 participant