Skip to content

lexer: snapshot the extglob gate at read-unit entry (Refs #3205) - #3223

Merged
caomengxuan666 merged 3 commits into
masterfrom
fix/rd4-readunit-snapshot
Nov 10, 2026
Merged

caomengxuan666 merged 3 commits into
masterfrom
fix/rd4-readunit-snapshot

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Refs #3205

问题

同一读单元内 shopt -s extglob 在行内执行之前就改变了该单元后段的词法:

  • shopt -s extglob && echo !(bbc)(脚本文件):GNU 在读时以单元入口的门(关)解析整表,!( 是否定子 shell,( 处报读错误 rc=2、无 stdout;rubash 旧行为把 !(bbc) 当 extglob 排除模式求值,rc=0 输出 glob 结果。
  • shopt -s extglob; !(bbc)(命令位):GNU 读为否定子 shell,运行时 bbc: command not found、后续行继续执行;rubash 旧行为在读时即以模式语义处理。

GNU 口径:一个读单元在执行任何部分之前已被完整读入,整单元的词法由单元开始时的选项态决定(parse.y 读时 extended_glob)。

修法(读单元级不变量,无个案特判)

  1. lexer:extglob 翻转改为延迟落地(与既有 posix 模式的延迟模型同构)——单元内检出的翻转先停放,只在单元边界生效;extglob_toggled 检测标记保持即时置位。
  2. script_driver / source 驱动:分组新鲜重词法化时,把门钉在 gather 入口快照(GroupGatherInfo::entry_extended_glob),重词法化结束后恢复;交互 stdin 调用点传 None(无分组快照语义)。
  3. parser:parse-time extglob 错误标记仅当「启用 extglob 的 shopt token 本身是在门关闭时被词法化的」才布防(token 上的门戳记),避免单元入口门已开时误报(z1 格)。

不按连接符逐段取实时态,不对 extglob 或 !( 形状做特判。

本 PR 删除了什么

无删除。本 PR 为纯增量修法(4 文件 +170/−40,差额为被替换的即时翻转逻辑);未删除任何公开 API、测试或文档。

验证摘要

  • 钉 tests/issue3205_read_unit_extglob_snapshot.rs(11 格,期望值全部对 GNU bash 5.3.20 oracle 在钉内同形布置下实测):
    • 基底(master 9cbe0293 + 仅钉):4 过 / 7 红,红格恰为简报集 {x4, x1b} + 扩展格 {z1, m2, y3, v7, w9};对照格(x0b 无 shopt、上行 shopt、-c 内嵌换行、; 参数位)全绿。
    • 修法头:11 过 / 0 红。
  • lib 双边:修法头 cargo test --locked --lib = 1206 过 / 0 红 / 4 忽略(失败名集为空)。
  • 邻茎:issue1114 1/0、issue2683 10/0、issue2876 7/0、issue3028 23/0、issue3050 4/0 全绿;issue332 头/基底同为 2 过 / 10 红且失败名集逐字相同(该茎在本 harness 下因 argv[0] 全路径前缀与期望 bash: 不符而整茎环境形红,基底同样如此,与本改动无关)。
  • RUSTFLAGS="-D warnings" cargo check --locked --lib:rc=0 无警告。(--all-targets 下 issue660 测试文件有一处 unused import: std::io::Write,已核基底同形存在,属既有、与本 PR 无关。)
  • cargo fmt --check 干净;scripts/check-marker-literals.py OK;scripts/check-utf8-boundary-hygiene.sh clean;改动文件无 CR。

范围外(已确认不属本单)

Same-line shopt -s extglob must not leak into the same read unit's
lexing (GNU parse.y read_command cadence; read_token_word consults
the read-time extended_glob at parse.y:5466). Defect cells x4
(&& segment glob-expanded, rc=0 vs GNU rc=2) and x1b (command-
position !( read as pattern head, rc=2 vs GNU negated subshell
rc=0) plus five extended cells of the same invariant (previous-
unit gate already on, multi-line if-body unit, earlier && segment,
@( under &&, pipeline stage) fail on master 9cbe029; the four
control cells (no-shopt !(, previous-line shopt, -c newline form,
';' argument position) pass. Expectations byte-verified against
GNU bash 5.3.20.

Refs #3205
A read unit is fully read before any of it executes (GNU parse.y
read-time extended_glob), so a same-line 'shopt -s extglob' must not
change how the rest of the unit is lexed. The gather feeder flipped
the global parse gate per line while simulating the unit, and the
fresh re-lex of a flipping group then started from the group-end
state, leaking the unit's own shopt into its trailing segments.

- lexer: park extglob flips in a deferred slot (mirroring the
  deferred posix-mode model) and land them only at unit boundaries.
- script_driver / source driver: pin the gate to the group-entry
  snapshot around the fresh re-lex of a gathered group and restore
  it afterwards.
- parser: the parse-time extglob error marker now arms only when
  the enabling shopt token itself was lexed with the gate off, so a
  unit that starts with the gate already on is not misdiagnosed.

Refs #3205
@caomengxuan666 caomengxuan666 changed the title fix(lexer): snapshot the extglob gate at read-unit entry (Refs #3205) lexer: snapshot the extglob gate at read-unit entry (Refs #3205) Nov 10, 2026
@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 10, 2026 03:56
@caomengxuan666
caomengxuan666 merged commit f7fed21 into master Nov 10, 2026
11 checks passed
caomengxuan666 added a commit that referenced this pull request Nov 10, 2026
executor: stop swallowing expanded '#' command words as comments

`execute_assignment_or_comment_command` in `src/executor/command_prepare.rs` carried an arm that discarded any simple command whose first expanded word starts with `#`, reporting exit code 0. GNU bash treats comments as purely lexical (parse.y:3630-3643): the reader discards a `#` only when it begins a word in the source text where a word can start. Quoted, expansion-produced, command-substitution-produced and variable-carried `#...` words are command names and report command-not-found (127). Source-text comments never reach the executor as words because the scanner consumes them, so the arm had no legitimate input and is removed (4 lines).

Captain verification (Simular box, per-stem result lines):

- Pin stem issue3020_hash_command_word: base e1afd25 3 passed / 6 failed (exactly the six quoted/expansion/comsub/variable cells) -> head 9 passed / 0 failed.
- Pin stem issue2683_same_line_shopt_extglob: 10 passed / 0 failed on head and on base (see the attribution note below).
- Neighbour stems all green: issue3205 11/11, issue1114 13/13, issue1573 9/9, issue120 4/4, audit_b4 1/1.
- Lib: head 1207 passed / 0 failed / 4 ignored; base identical; failure name sets empty on both sides.
- `RUSTFLAGS="-D warnings"` check at lib+bins scope: rc=0. The all-targets scope is red on base and head alike from pre-existing warnings in unrelated test files (untouched by this change); left for their owners.
- `cargo fmt --check` rc=0; marker-literal lint OK. CI on the head: 10 pass + 1 skipping, zero red.

## Note on the second issue

The same-line shopt/extglob pins were re-measured: all 10 pass on the current base. Captain attribution check: the pin stem scores 8 passed / 2 failed at f7aee5c (the two failed cells are exactly the banked ones) and 10 passed / 0 failed at f7fed21, the merge commit of PR #3223, whose deferred extglob parking, grouped-feeder entry snapshot and parse-loop marker arming cover the same mechanism. This PR therefore carries no product change for that side; its closure is the captain's manual call on this evidence.

## 本 PR 删除了什么

Removed the 4-line executor arm described above (one `if` block in `src/executor/command_prepare.rs`). Nothing else is deleted; no test files change (the pins are already on master via PR #3163).

Fixes #3020

Refs #2683
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.

1 participant