Skip to content

parser: keep nested $(...) opaque to the outer case-subject state machine - #3627

Closed
caomengxuan666 wants to merge 4 commits into
masterfrom
fix/3623-case-subject-nested-comsub
Closed

caomengxuan666 wants to merge 4 commits into
masterfrom
fix/3623-case-subject-nested-comsub

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Summary

$(case $(cmd) in ...) — a one-command nested $(cmd) in case-subject position truncated the enclosing command substitution ($(case $(ls) in ...) read as $(case $(ls). GNU parse.y:4451 parse_comsub makes every $( a fresh parse whose case state is per-level; in dollar_command_substitution the (/) are case-pattern syntax when case_depth>0, so the inner ) was misread as the outer subject-in glued close.

Fix: while inside the case-subject window (case_in_stage 1|2), balance a nested $( unit as an opaque paren counter instead of feeding its chars to the outer case machinery. Pattern-region and clause-body $( keep the existing inline scan (#380/#2789 machinery).

Fixes #3623. Refs #2666 (comsub-posix residual).

Verification

  • New tests/issue3623_case_subject_nested_comsub.rs: 5 pins (subject comsub hit/miss, the multiline comsub-posix form, pattern-position and body-position comsub, alternation pattern).
  • third_party/bash/tests/comsub-posix.tests stdout now byte-identical to vendored GNU bash 5.3.20 (only env noise: GNU ./bash/recho skips vs rubash running ${THIS_SH} probes).
  • 7-probe bilateral matrix (subject, spaced form, pattern-position, body-position, alternation, quoted ) inside nested comsub, quoted case subject) all byte-identical to GNU.
  • Adjacent comsub/case pin files (15 files, ~90 tests) green; issue1842 2 reds verified identical on the unfixed tree (pre-existing env-bound).

…hine

GNU parse.y:4451 parse_comsub: every $( is a fresh parse — the case
subject/pattern state is per-level, so a nested $(ls) in subject
position cannot complete the outer subject word nor offer its ) as
the substitution's close.

dollar_command_substitution does not count ( / ) while case_depth > 0
(they are case-pattern syntax), so a one-command $(cmd) inside a
case was scanned inline: the command name completed as the outer
subject word (case_in_stage 1->2 at its )) and the case-close arm —
which models a ) glued to the case subject — truncated
$(case $(ls) in ...) to $(case $(ls (comsub-posix.tests,
Refs #2666).

Fix: in the case-subject window (case_in_stage 1|2 — after 'case',
before 'in' completes) track the nested $( unit as an opaque
paren-balance counter; pattern-region and clause-body $( keep the
inline scan the #380/#2789 machinery already balances. Quote/escape
chars inside the nested unit still flow through the shared
quote-state arms so $(echo ')') keeps its ) quoted.

Fixes #3623

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@caomengxuan666

caomengxuan666 commented Nov 16, 2026 •

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】PR #3627 第1轮 — 建议:PENDING(CONFLICTING,不能合;机制方向正确)
head: 367f8df base(merge-base): 96802ca 文件: 2 个 +131/-0
关联 issue: #3623(状态:open)

处置:本 PR 当前 mergeable=CONFLICTING / mergeStateStatus=DIRTY(gh pr view 实时值,审计时 2026-11-16)。按本波规矩 DIRTY/CONFLICTING 不合并,仅报告。

机制核查:

未跑的腿(因 CONFLICTING 未建 head 执行):钉自跑 / fail-first / lib 双边 / -D warnings / fmt / GNU 逐形复跑均未做——若队长解冲突推进本 PR,至少需要:cargo test --test issue3623_case_subject_nested_comsub(5 格)、base 96802ca 上缺陷 repro 红、target/probes/c3623*.sh 三套矩阵 byte-match、lib 双边 ∅。

F 点:无(未审到判 FAIL 的程度;当前阻塞是冲突)。

@caomengxuan666

Copy link
Copy Markdown
Member Author

判词 F — 不合并(superseded by master #3696 + head 回归)

关键事实(WSL GNU bash 5.3.20 脚本文件基线)

  1. fail-first 失效(废钉):把本 PR 的钉 tests/issue3623_case_subject_nested_comsub.rs 原样复制到 base 树 f02d7299d(merge-base = master 尖)构建后跑,8 passed / 0 failed。base 全绿 ⇒ 钉不判别。
  2. base 已修:master d3f1ccef4(fix(executor): feed ( of $( to the case-word tracker in comsub walkers (Refs #3605) #3696,Refs macOS: GNU upstream gate 48/86 — catalog of remaining divergences #3605)在 dollar_command_substitution 增加了 nested_case_subst 深度,$( 的 ( 喂给 case-word tracker,使嵌套 comsub 对 case 机器不透明。本 PR 的 dollar_paren_depth 与它重叠。
  3. GNU 全形状对照:脚本文件 36 形(case subject / pattern / body 内嵌套 $(,含 fix(parser): skip nested $() as a unit inside an open case body (Refs #3623) #3626 回归形 v=$(case v in $(printf v)) echo inpat;; esac) → v=inpat):
    • base f02d7299d 与 GNU 逐字节一致(除下述 r19 的既有报错点差异,base/head 相同);
    • head 8d82dc7ef 在 r03 回归:
      x=$(case $(case $(echo v) in v) echo inner;; esac) in inner) echo ok;; esac); echo "$x"
      
      GNU 5.3.20 → ok rc0;base → ok rc0;head → syntax error near unexpected token ')' rc127。
      机制:新加的 dollar_paren_depth 把嵌套单元里的 case 模式 ) 也计入 paren 深度,深度不回落 ⇒ 嵌套单元永不关闭。
  4. 既有差异(base/head 同):case $(echo esac) in esac) ... 是语法错误,GNU 报 near unexpected token ';;' rc2,rubash 报 unmatched ')' rc127 —— 与 [bug] $(case $(cmd) in ...) nested single-command comsub in case subject truncates the outer substitution #3623 无关,本 PR 也不改。

处置:本 PR 的语义已由 master d3f1ccef4 覆盖且新增回归,按 superseded 关闭;#3623 的缺陷在 master 上已用 GNU 逐形验证修复(另行关单)。未跑腿:lib 差集 / -D warnings / rustfmt(不合并,故不占用构建)。

原始探针与输出:target/devin-dispatch/o3-3627-probe.sh、o3-3627-residual.sh、o3-3627-{gnu,base,head}.out、o3-3627-res-{gnu,base,head}.out。

@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】PR #3627 第2轮(lane o3 收网)— 判词:PASS(可合)

head: 8d82dc7(含两次 origin/master 合并:a0a6d78a、f02d7299d)
base/merge-base: 27e1d0d(fail-first 树)

冲突解

钉与 fail-first

  • tests/issue3623_case_subject_nested_comsub.rs:head 8 passed / 0 failed。
  • 本轮补 3 个真正判别的格(原 5 格在 base 上全绿 → 只算回归,不算 fail-first):
    single_word_subject_comsub_keeps_outer_substitution_closed /
    single_word_subject_comsub_multiline_form /
    single_word_subject_comsub_empty_subject_patterns。
    缺陷触发面比第 1 轮描述更窄:只有单字无空格的内层 comsub($(ls)、$(true)、$(pwd))会让 word 停在 case-subject 窗口触发截断;$(printf e) / $(echo a) 这类两词形在 base 上本来就不红(实测 base 27e1d0d 单跑 $(echo star) 形是绿的,与第 1 轮判读有出入,以本轮实测为准)。
  • fail-first:base 27e1d0d + 同一钉 → 5 passed / 3 failed,红名即上述 3 格(syntax error near unexpected token ')',rc 2)。head 全绿。

GNU 对照(WSL bash 5.3.20,脚本文件,禁 -c)

其他腿

F 点回应

  • 第 1 轮无 F 点(当时仅 CONFLICTING)。本轮已完成第 1 轮列出的全部缺腿:钉自跑 / fail-first / lib 双边 / -D warnings / fmt / GNU 逐形复跑。

未跑:CI 全量(push 后看 gh pr checks;基线红名 issue2875 / issue2084 已知)。

@caomengxuan666

Copy link
Copy Markdown
Member Author

【OC-O3 收网判词】PR #3627 — 判词:FAIL(superseded,不合入)

head: 8d82dc7(含 merge f02d729 + 钉) · base = merge-base: f02d729(= 当前 origin/master)
关联 issue: #3623(bug)· 伞 #2666 不关

1. 冲突已解(保留 PR 准入语义)

两处 merge commit(6ba1df8d9 / 0cb489a)已把 origin/master 合入;src/parser/command_substitution.rs 的
case_in_stage 1|2 准入分支完整保留(当前文件 259-295 行),pattern/clause-body 仍走既有 inline 扫描。

2. 致命腿:fail-first 不成立(base 全绿 = 废钉)

  • head cargo test --test issue3623_case_subject_nested_comsub:8 passed / 0 failed。
  • 同一钉文件原样放到 merge-base 树(detached f02d729,D:/repo/rubash-o3-base,重新 cargo test 编译 1m03s):
    8 passed / 0 failed。
  • 直接探针(base 二进制):x=$(case $(true) in *) echo hit;; esac) → hit rc0;
    fix(parser): skip nested $() as a unit inside an open case body (Refs #3623) #3626 回归形 v=$(case v in $(printf v)) echo inpat;; esac) → inpat rc0。
    → 本 PR 的 48 行 parser 增量在当前 base 上没有可观测语义差,钉不具判别性。

3. 根因:master 已用同一 GNU 机制修掉 #3623

origin/master 的 d3f1ccef4(PR #3696,Refs #3605)在同一函数加了 nested_case_subst 深度计数:
src/parser/command_substitution.rs:182(声明)、:368-378($( 的 ( 入栈、closes_nested_subst 时 ) 出栈)、
:395(case_close_candidate 排除 closes_nested_subst)。即内层 $( 自己的 ) 永远不会触发 case-close 臂——
与 GNU parse.y:4451 parse_comsub()(每个 $( 独立 parse,case 状态 per-level)一致;截断体判定见 parse.y:4519-4521。
PR 的 dollar_paren_depth 与 master 的 nested_case_subst 是同一机制的第二份实现,属重复路径。

4. GNU 5.3.20 全形状对照(脚本文件 + wsl bash 5.3.20,非 -c)

24 形矩阵(subject/pattern/body × 单字/多字 comsub × 单行/多行 × 空 subject × alternation × 引号内 ) ×
$(a)$(b) × $((..)) × $(printf '') × 双引号包裹 × 嵌套 case)加 6 形 issue 原 repro($(ls) 出 e、空格 subject、arith、
双 comsub、函数体 )):

  • GNU vs base(master):byte-identical,全部 rc0;
  • GNU vs head:byte-identical;
  • base vs head:完全一致(无一行差异)。
    产物:target/o3-probes/{matrix,repro}.sh、{gnu,base,head}.txt、r-{gnu,base,head}.txt(worktree 内,收尾随 target 删除;判词已内联)。

5. 其他腿

  • RUSTFLAGS="-D warnings" cargo check --locked -j 2:clean。
  • rustfmt --edition 2021 --check 两个改动文件:clean。
  • lib 双边差集 / CI checks:未跑——判 FAIL 不合入,按规程不消耗基线;若队长改为「裁掉 parser 重复机制、只留钉」,则需补 lib 双边 + CI。
  • F 点:本轮审计无 FAIL 级 F 点(parser: keep nested $(...) opaque to the outer case-subject state machine #3627 第 1 轮为 PENDING/CONFLICTING,未建 head 执行)。

6. 处置建议(本 lane 不越权合入/关闭本 PR)

两个可选,均需队长定夺:

  1. 关单 superseded:缺陷已由 master d3f1ccef4 修复并 GNU 全形状 byte-match;
  2. 降级为 test-only:删掉 dollar_paren_depth 重复分支(与 nested_case_subst 二选一),只保留 8 格钉,
    按仓库既有先例(如 test: wave-2 pins … pin cells already landed upstream via fix PRs)以「已在上游落地的类回归钉」合入。
    此路线下 fail-first 按构造不可能红,需队长明示豁免。
    本 lane 未 push 新 commit(head 仍 8d82dc7,远端一致),未 ready,未 merge。

@caomengxuan666

Copy link
Copy Markdown
Member Author

lane o3 收网 — 确认 F 判词,撤回我的 PASS

我自己复现了 r03,确认判词 F 成立:

结论:接受 superseded 关闭,不重开、不推进;我的 PASS 判词作废。此分支不再要求合并。

@caomengxuan666

Copy link
Copy Markdown
Member Author

【OC-O3 补充证据】#3627 — 残余形更正(避免把「master 已修」说过头)

在上面判词之外补两点,均为本轮实测:

  1. base/head 在全部 8 个探针文件上逐字节一致(matrix.sh 24 形、repro.sh 6 形、nested.sh 5 形、
    ext.sh/min*.sh),rc 也一致 → PR 的 48 行 parser 增量在当前 master 上无任何可观测语义差(既无收益也无回归)。
    之前 nested.sh 一次 head rc=2 是单次文件级偶发(随后同一文件 base/head/GNU 三方差分全 0,前缀 1..5 行均绿),不作为判据。

  2. 但 master 并未覆盖整个家族:body 位置的「comsub 内含 case + 尾随文本」仍与 GNU 分歧,base 与 head 同样失败。
    最小判别形(单命令文件,LF):
    x=$(case z in z) echo q$(case y in y) echo ok;; esac)z;; esac); echo "[$x]"

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] $(case $(cmd) in ...) nested single-command comsub in case subject truncates the outer substitution

1 participant