Skip to content

parser: backquote groups are opaque to the raw procsub word scans (Refs #3337) - #3351

Merged
caomengxuan666 merged 3 commits into
masterfrom
fix-3337-backquote-opacity
Nov 12, 2026
Merged

caomengxuan666 merged 3 commits into
masterfrom
fix-3337-backquote-opacity

Conversation

@caomengxuan666

@caomengxuan666 caomengxuan666 commented Nov 11, 2026 •

Copy link
Copy Markdown
Member

概要

命令替换的 backquote 形态下,组内的进程替换被外层词的 raw 扫描误认领:赋值材料化在外层先把组内的 >(...) 生成出来,消费者在外层上下文运行——此时 backquote 本体内 exec 3<&1 的 fd 3 重绑定尚未发生,fd 3 是环境 fd(外层 exec 3>tap.log 的 TAP 文件),hello 落进日志而非 comsub 捕获管道。

$(...) 同形已由 #3221 一线处理:parser 的三处 raw 词扫描(embedded 提取、has-unquoted 判定、verbatim 跨度定位,src/parser/process_substitution.rs)在 #3132 时补上了「$ 引入的成组跨度不透明」(dollar_group_end,复用 lexer 的 dollar_word_group_len)。backquote 组不在其列。本 PR 给同一组扫描补上 backquote 跨度的不透明臂(backquote_group_end,复用 lexer 的 skip_backtick_corrected,与 tokenizer 同一 walker),谓词口径按 GNU 分工分开:

  • GNU expand_word_internal(subst.c)在 backquote 臂(subst.c:11572 case '')以 string_extract整段取走组体、交command_substitute(subst.c:7143)作为内层命令文本执行;组内的 </>` 永远到不了本词层面的进程替换臂(subst.c:11364-11378)。
  • backquote 在双引号内仍然有效(只有 </> 臂被 Q_DOUBLE_QUOTES 抑制,backquote 臂不被抑制),故跳过臂只以 !single 为门,不以 !double 为门——同时避免组体里的 " 污染扫描的引号状态。

按机制修:凡 backquote 组出现在词扫描路径上(赋值 RHS、参数词、组内嵌组、双引号内组),组内跨度一律归内层命令文本自身,不按复现字符串特判。

本 PR 删除了什么

无删除。三处 raw 扫描各补一个与既有 $ 臂同形的 backquote 跳过臂,并新增一个内部助手函数;钉文件为新增。

回归钉

tests/issue3337_backquote_procsub_opacity.rs(8 格,镜像 issue3221 钉的单进程 harness):

  • 01 赋值 RHS 为 backquote 组、体内 exec 3<&1 + >(cat >&3)(立案原形)
  • 02 组嵌在更大词中(前后缀)
  • 03 组在双引号内
  • 04 组在参数词中
  • 05 组体内为输入进程替换 <(...)
  • 06(linux)消费者 fd 3 的 readlink 读回须为捕获管道而非环境文件
  • 07 守卫:转义 backquote 是字面字符,其后的 >(...) 仍是本词的真进程替换
  • 08 守卫:无组词的顶层 embedded 进程替换仍正常材料化

期望全部以 GNU bash 5.3.20 实测锁定(车道探针矩阵 probe-bq-matrix.sh,GNU 侧 10/10)。

验证

全部腿在 Simular box 跑(树 ~/work/fix-3337,日志 ~/work/f3337-logs{,2}/),基底 = master db0c1a40,头 = 本 PR 头:

  • 探针矩阵(probe-bq-matrix.sh,10 格,脚本文件形态):GNU 5.3.20 侧 10/10;基底红恰三格(bq-out 测得 1/1、bq-fd3-type 消费者 fd 3 为日志文件路径、bq-pre-suffix 测得 prehellopost//1),其余七格(含 $() 对照、转义 backquote 守卫、算术对照)基底全绿;头 10/10。
  • fail-first:基底 + top-up 钉 5 过 / 3 败,红恰 {01 赋值原形、02 嵌词形、06 fd 3 读回} 三个 fd 判别格(基底实测 01 为 OUT:[hello] TAP:[hello|]);03/04/05 三个同族锁格与 07/08 两个守卫格基底全绿。头钉 8 过 / 0 败。
  • 邻茎 12 茎(issue3221、issue2648、issue364、issue1523、issue1561、issue1931、issue1932、issue1933、issue2024、issue2196、issue2492、issue1021)逐茎各有 test result 行、全绿。
  • lib 双边:头 1244 过 / 0 败 / 4 忽略,基底同为 1244 / 0 / 4,失败名集两侧皆空。
  • RUSTFLAGS="-D warnings"(--lib --bins)rc=0;marker-lint OK;UTF-8 boundary hygiene clean;cargo fmt --check rc=0。

定界中另发现两条姊妹残差(基底与本头同形、非本 PR 引入,已立案):#3353(内建参数词的中段跨度不拼接 /dev/fd)、#3354(跨度提取括号匹配对 backquote 组盲)。

Refs #3337

Eight cells mirroring the issue3221 pin harness (single-process
scripts, outer exec 3>tap.log): the backquote assignment shape,
the group embedded in a larger word, the group inside double
quotes, the group in an argument word, an input procsub inside
the body, the consumer fd-3 target readback (linux), and two
guards (escaped backquote, plain top-level embedded procsub).
Expectations measured on GNU bash 5.3.20 with the lane probe
matrix (10/10).

Refs #3337
The three raw word scans in process_substitution.rs (embedded
extraction, has-unquoted admission, verbatim span location) skip
dollar-introduced groups whole (dollar_group_end) but walked
straight through backquote groups, so a process substitution
inside a backquoted command substitution was claimed by the
outer word: the consumer materialized at the outer level, where
an exec 3<&1 inside the body has not rebound fd 3 and the fd
resolves to the ambient context instead of the capture pipe.

GNU expand_word_internal consumes a backquote unit whole in its
own arm (subst.c:11572 case backquote: string_extract pulls the
span, command_substitute (subst.c:7143) executes the body), so
an angle bracket inside one never reaches the process
substitution arms (subst.c:11364-11378) at this word level.
Mirror that invariant with a backquote_group_end helper reusing
the lexer backquote walker (skip_backtick_corrected) and a skip
arm beside each dollar arm. The backquote arm is not suppressed
inside double quotes (only the angle arms are), so the skip
gates on not-single only.

Refs #3337
The echo-argument form of the top-level embedded guard hits a
separate pre-existing face (builtin argument words execute a
mid-word procsub body but do not splice the fd path; identical
on base and head, delimited with probe-embedded-forms.sh and
probe-faceA-delimit.sh in the lane probes). The assignment form
asserts the same scan claim through the synchronous value
readback and is deterministic on both trees.

Refs #3337
@caomengxuan666

Copy link
Copy Markdown
Member Author

复核员在 Simular box 独立复跑(树内现建二进制、干净 fd 上下文),申报逐项对账全合:

钉双边(茎名 ls 实查)

  • head:issue3337_backquote_procsub_opacity 8 passed / 0 failed。
  • base(真分叉 db0c1a40 + top-up 钉):5 过 / 3 败,红名恰 {01 赋值原形、02 嵌词形、06 fd 3 读回},03/04/05 锁格与 07/08 守卫格全 ok —— fail-first 与申报逐名同。

探针矩阵(车道 probe-bq-matrix.sh 同文件三方,sha256 已核)

其余腿

  • 邻茎 12 茎逐茎各自 test result 行全 ok(3221/2648/364/1523/1561/1931/1932/1933/2024/2196/2492/1021)。
  • lib 双边 1244 过 / 0 败 / 4 忽略,失败名集双边 ∅。
  • fmt rc=0;marker-lint OK;UTF-8 boundary hygiene clean。
  • 作用域 -D warnings(--lib --bins)rc=0;全靶强制重查(touch + -v 旗标计数,head 1455 次实带旗标调用):错误点集 16 点全为分叉基底既存的 tests/ 钉文件点、与 base 点集逐字相同,本 PR 两文件零点。
  • PR 头 CI:11 个 check-runs 全部 finished,10 success + 1 skipped,零红(goldens 已 finished success)。
  • git:真实 merge-base = 申报基底 db0c1a40;全区间交集(对现尖 f6e8c674)= ∅,试演合并零冲突。

GNU 锚点亲核:subst.c:11572 backquote 臂以 string_extract 整段取组体交 command_substitute(:7143);</> 臂(:11364-11378)才走 extract_process_subst(:1311),且 Q_DOUBLE_QUOTES 只抑制 </> 臂 —— 跳过臂门控只取 !single 有源码依据。

附带定界(#3353 / #3354):均不被本 PR 覆盖。 复核探针在 base/head/现尖三树隔离实测:#3353 的内建中段跨度形三树同形(组体执行、拼接丢失,外部/赋值路线正常);#3354 的赋值形三树同形(跨度漏认领、cap 为空),词首对照形三树与 GNU 全同。两单维持独立修法面,合入后仍 OPEN。附注:#3354 的匹配器缺的正是 backquote 跳过臂,本 PR 的 backquote_group_end 是现成可复用的 walker。

一处复核侧记录:中段曾以树里现成的旧 --bin 工件跑出「头红三格」假象,判别为车道 driver2 末步在基底树构建后切回头未重建留下的基底工件冒充(时间线 + 同轮现建复测坐实),非产品证据;合入轮已按此立条件(同轮现建 + 身份断言)。

合入轮按卷宗 §9 执行:活尖重算交集 → update-branch → 合并头同轮现建二进制重跑钉/邻茎/lib/探针 → 全靶警告按现尖 rc=0 全清口径 → 最终头 CI finished 全绿 → 以既备 merge-body squash(唯一 closing 共现 Fixes #3337)。

@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 12, 2026 11:47
@caomengxuan666
caomengxuan666 merged commit dcc2c22 into master Nov 12, 2026
11 checks passed
caomengxuan666 added a commit that referenced this pull request Nov 12, 2026
…efs #3354) (#3431)

## 根因

嵌入式进程替换跨度提取的括号匹配器(`embedded_procsub_close`,`src/parser/process_substitution.rs`;其字节孪生 `skip_procsub_paren`,`src/parser/token_actions.rs`)对 backquote 组盲:组体内未配平的 `(` 直接计入深度,真闭合符永远配不平,整段跨度被漏认领;随后展开器把组体文本当作词层文本处理,连组体内的 backquote 都在外层被展开掉。组体内未配平的 `"` 以同机理腐蚀匹配器的引号状态,同样吞掉真闭合符。

## GNU 锚点

- `subst.c:1311` `extract_process_subst` → `xparse_dolparen`(`parse.y:4686`):跨度体由真正的解析器重解析;
- `parse.y:3877` `parse_matched_pair`:嵌套替换单元(含 backquote 组)经 shellquote 臂整段当作单位消费。
- backquote 组内的 `(` 是内层命令文本的数据,绝不是外层跨度的嵌套层级。

## 修法

两个匹配器现都把 backquote 组整段跳过:字符版复用 PR #3351 已有的 `backquote_group_end`(转发 lexer `skip_backtick_corrected`);字节版新增其孪生 `skip_backtick_bytes`(与 `skip_backtick_corrected` 同规则:反斜杠携带下一字节,首个其余 backquote 闭合)。两处门限均为 `!single`(与本文件内同族扫描一致:backquote 组在双引号内仍有效);未闭合的组回落到原逐字行走。

## 回归钉

`tests/issue3354_procsub_backquote_paren.rs`,6 格,期望以 GNU bash 5.3.20 实测为准:

| 格 | 内容 | 基底 | 本头 |
|----|------|------|------|
| 01 | issue 形态:赋值 RHS,组内未配平 `(` | 红 | 绿 |
| 02 | 词首形态对照(GNU 与基底一致) | 绿 | 绿 |
| 03 | 复合赋值面(`skip_procsub_paren`) | 红 | 绿 |
| 04 | 组内配平括号守卫 | 绿 | 绿 |
| 05 | 转义 backquote 守卫 | 绿 | 绿 |
| 06 | 组内未配平 `"` 的引号状态面 | 红 | 绿 |

## 验证(队长复核亲跑,见 `~/workspace/review-3431/CAPTAIN-VERIFY.md`)

- 新钉:基底顶补 3/6(红恰 01/03/06)、本头 6/6,逐名核对。
- 全量 lib 双边:本头 1246 passed / 0 failed / 4 ignored,与基底逐数对齐,失败名集双边皆空。
- 三方探针(GNU 5.3.20 / 基底 / 本头):复现、矩阵、引号三脚本,本头与 GNU 逐字一致(仅 /dev/fd 数字随分配顺序浮动);基底在 issue 形与复合赋值面分歧如 issue 所述。
- 邻茎:issue3337 8/8、issue339_340 10/10、issue1561 9/9、issue1523 6/6、issue1690 1/1。
- `RUSTFLAGS="-D warnings" cargo check --locked --all-targets`:rc=0,1479 次 rustc 调用,0 警告、0 错误。
- rustfmt、marker-lint、UTF-8 边界卫生全绿;CI 11 腿全绿(含 goldens 与双 cross-target)。注:CI 的 nextest 精选表不含新钉 stem(见 #3430),钉面以复核员亲跑为准。

## 本 PR 删除了什么

无。纯加法修法:两处匹配器各加一个 backquote 跳过臂(+59 行产品代码),新增一个 6 格回归钉文件。未删任何函数、分支、测试或公开面;未改 `docs/api-ledger.md`;未碰 `src/lexer/continuation.rs`。

Fixes #3354
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