Skip to content

fix(lexer): record argument-position ! as Word in reader history (Refs #3639) - #3646

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/3639-brace-fold-after-bang
Nov 16, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/3639-brace-fold-after-bang

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Refs #3639 (P3-low, area:parser).

What this fixes

Trailing unquoted { } after an argument-position ! was folded into
one word "{ }"; GNU keeps three separate words (printf '[%s]\n' ! { } prints [!], [{], [}]).

Root cause

GNU parse.y:3168 CHECK_FOR_RESERVED_WORD + :2378 word_token_alist
({"!", BANG}): ! is the BANG token only where
reserved_word_acceptable (parse.y:5899) holds; elsewhere the reader
returns a plain WORD, and it is the PRODUCED token that
last_read_token then names. Rubash recorded the dedicated-branch !
as Keyword in the reader history unconditionally, so
reserved_word_position() armed the { fold after an argument !.
The same chain explains the second trigger shape: in ! { } { } the
first {'s } is rejected as a group close (followed by {), so
the first { is emitted as a bare Keyword {, which makes the } a
Keyword, which arms the fold for the LAST pair only.

Fix

In record_token (src/lexer/scanner.rs), demote ! to Word in the
reader history exactly when it was not read in a reserved-word
position — the ! exception to the verbatim-history rule for
structural tokens. A genuine BANG (command position, after time,
after (/;/etc.) keeps its Keyword history, so ! { ...; } still
folds and negates. Stream token kind is untouched; only the history
classification changes.

Pins

tests/issue3639_brace_fold_after_bang_regressions.rs, 13 cells:
fail-first 5 passed / 8 failed on the base; head 13/13. Issue seed
9/9 on head (base 2/9). GNU-oracle matrix 27/29 — the 2 remaining
cells are pre-existing divergences in paths this change cannot affect
(echo hi | ! true omits the source-line echo; ! } { } runs }
as a command instead of a syntax error); both have the ! in a
reserved position, so no demotion fires and the token stream is
identical to base.

Legs run by this lane

  • Pin head 13/13; fail-first 5/8 (top-up form, scrubbed_cargo).
  • Neighbors: lexer lib 108/108, parser lib 33/33, brace/group/bang
    stems 36/36 (issue1625 3/3, issue1652 6/6, issue1721 8/8,
    issue1046 1/1, issue1128 1/1, array_compound_quote 17/17).
  • RUSTFLAGS="-D warnings" cargo check --lib --bins: rc=0.
  • fmt clean on both touched files.
  • Lib bilateral + CI: left to review.

本 PR 删除了什么

  • 未删除任何产品行为、公开 API 或测试;仅在注释中改述了 history
    demotion 规则的例外说明(! 从 verbatim 结构 token 表中移出,
    另立一臂说明)。

UNVERIFIED-BY-CAPTAIN

…efs #3639)

GNU parse.y:3168 CHECK_FOR_RESERVED_WORD + :2378 word_token_alist
({"!", BANG}): `!` is BANG only where reserved_word_acceptable
(parse.y:5899) holds; elsewhere the reader returns a WORD and it is
the PRODUCED token that last_read_token names. Rubash recorded the
dedicated-branch `!` as Keyword in history unconditionally, so
reserved_word_position() armed the `{` fold after an argument `!`
and `printf "! { }"` saw one "{ }" word. Demote `!` to Word in
history exactly when it was not read in a reserved position; a
genuine BANG keeps Keyword history, so `! { ...; }` still folds.

UNVERIFIED-BY-CAPTAIN
@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 16, 2026 12:07
@caomengxuan666
caomengxuan666 merged commit 6089aef into master Nov 16, 2026
8 of 11 checks passed
@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】
PR #3646 第1轮 — 建议:PASS(已合)
head: 8bdf5d5 base(merge-base): 6171293 文件: 2 个 +188/-4
关联 issue: #3639(状态:open→closed,我核过的时间:2026-11-16 21:40 本地)

机制核查:

  • 根因与 GNU 一致:! 不是 dedicated-branch 特权 token,GNU 走 read_token_word + CHECK_FOR_RESERVED_WORD(parse.y:3168-3172,gate 为 reserved_word_acceptable),只有命令位置才是 BANG(parse.y:2378 word_token_alist { "!", BANG });不在保留位时读到的是 WORD,last_read_token 记录的是产出的 token。rubash 原来把 dedicated ! 无条件记成 Keyword history,使 reserved_word_position() 在参数位 ! 之后武装 { 折叠 → { } 合成一个词。
  • 修法对:src/lexer/scanner.rs:838-849 只在 !read_time_reserved_position(= reserved_word_position(),scanner.rs:497)时把 history 降级为 Word;token 流 kind 不动,! { ...; } 的真 BANG 仍折叠并取反(钉里 negated_group_still_folds_and_negates 绿)。属于「修正错误不变量」而不是加症状守卫。
  • 无关改动:无。未碰 src/lexer/continuation.rs(队长专属)。
  • 公开分歧披露(PR 正文 27/29 的两格):echo hi | ! true 与 ! } { } 两格里 ! 都在保留位(pipe/命令位置 → | 在 reserved_word_acceptable 集合内),降级臂不触发,token 流与 base 逐字节相同 → 与本改动无因果,属既存面。

腿:

  • 钉自跑:issue3639_brace_fold_after_bang_regressions 13 passed / 0 failed
  • fail-first:base 6171293 上 5 passed / 8 failed,红名 = {printf_bang_braces_are_three_words, command_capital_v_bang_braces_three_lines_no_stderr, command_v_bang_braces_prints_three_names, type_bang_braces_rc0_three_lines, printf_bang_not_first_still_three_words, printf_two_brace_pairs_stay_separate, printf_three_brace_pairs_stay_separate, for_loop_over_bang_braces_iterates},与 PR 声称的 5/8 完全一致。
  • lib 双边(Windows 本机):head 1351 passed / 1 failed(失败名 {api::consumer_tests::issue2084_session_end_restores_host_process_state})/ base 6171293 同一失败名 / 差集 ∅
  • -D warnings:Windows cargo check --lib --bins 唯一 error = 既存 src/executor/command_substitution_values.rs:4 unused import: std::io::Read(本 PR 未触碰);新增警告 0
  • 卫生:rustfmt --check 2 文件 clean;marker-lint OK (117 consts, 268 allowlisted);utf8 clean
  • CI(run 43623575038,head 8bdf5d5):红 3 项,唯一红均为 api::consumer_tests::issue2875_input_procsub_child_runs_as_rubash_shell(Rust tests / Rust tests (macOS) / GNU regression goldens and canaries,基线);cross-target/Windows compile/API ledger/niubash 全绿。
  • GNU 对照:GNU bash 5.3.20(1)-release(WSL x86_64)。脚本 target/probe-3646.sh(10 形状:printf 参数位 !、command -v/-V、type、双/三对 { }、for 迭代、echo 夹位、无 ! 控制、! { ...; } 取反)→ GNU 与 rubash head 逐字节一致(含 command -V ! { } 三行 keyword 与 stderr 空)。

F 点:无
续作记录:未动代码。
squash: 6089aef(fix(lexer): record argument-position \!` as Word in reader history (Refs #3639)`),head 8bdf5d5 未变即合。
未跑/VOID 的腿:无

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