Skip to content

fix(parser): alias pre-pass treats quotes in comsub comments/heredocs as opaque (Refs #3813) - #3821

Merged
caomengxuan666 merged 4 commits into
masterfrom
fix/3813-builtin007-alias-comsub-quote
Nov 17, 2026
Merged

caomengxuan666 merged 4 commits into
masterfrom
fix/3813-builtin007-alias-comsub-quote

Conversation

@caomengxuan666

@caomengxuan666 caomengxuan666 commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

根因

别名展开预扫描(src/lexer/alias_stream.rs)的 $(\...)/${ ... } 范围扫描器把 # 注释与 heredoc 体内的引号字符当成了真引号:注释里的撇号(如 # don't)打开一个幻影单引号,吞掉后续全部输入,连同其后的别名展开一并吞掉。alias E='}; }' 永远得不到展开,别名引入的 { 遂无闭合,报 syntax error: unexpected end of file from {'`(rc=2);偶数个撇号恰好把幻影引号重新闭合,所以奇偶表按奇偶翻转。

落点(仅 src/lexer/alias_stream.rs,未碰护栏文件 src/parser/process_substitution.rs / src/parser/token_actions.rs / src/executor/command_substitution.rs,未碰红线 src/lexer/continuation.rs):

  • skip_dollar_paren 改为委托已修正的 skip_parenthesized_unit_corrected(skip.rs):注释按 token 边界跳过、heredoc 体按不透明处理、case 模式 ) 按体解析器判定。
  • skip_dollar_brace 增加内嵌 $(\...) 分支,同样委托修正扫描器(# 在 ${...} 内仍是运算符,不设注释臂)。
  • skip_funsub_body 增加 # 注释臂(独立的 token-boundary 状态,term 回答的是另一问题)与 << heredoc 臂;;/&/|/换行与 </> 的边界语义与修正扫描器对齐。

GNU 锚点

  • parse.y:3631-3645 shell_getc:# 在 token 起始位置恒为注释("Discard until EOL or EOF");词中 #(a#b、$#、${x:-a # b})不是注释。
  • parse.y:4451 parse_comsub:体用真解析器解析,注释与 heredoc 在 $(\...) / ${ ... } 内为不透明段。
  • make_cmd.c:512 make_here_document:heredoc 体按原始行读取。

GNU 5.3.20 实测:除转义 \# 对照(GNU 同为 rc=2,未闭合真引号)外,本 PR 全部钉用例 GNU 均为 rc=0。

回归钉

  • tests/issue3813_alias_comsub_comment_quote_regressions.rs(新,11 格):issue 奇偶表 p0–p4、$( 后紧跟注释、嵌套/双引号内/case 模式 comsub、注释后别名展开、comsub 内 heredoc、${...} 内嵌 comsub,以及 $# / 词中 # / ${...} 内 # / \# / 无别名对照。4 格 funsub / ${x:-$(...)} 钉(issue3813_funsub_comment_quote、issue3813_funsub_heredoc_quote、issue3813_funsub_heredoc_brace_line、issue3813_nested_comsub_in_brace_expansion)已移出本 PR,移交 rubash funsub ${ ... } 体内 # 注释/heredoc 解析死亡,${x:-$(...)} 闭合 } 漏入输出(真 lexer 层,#3813 续作) #3823(真 lexer 层残差,钉定义见该 issue 正文)。
  • src/lexer/tests.rs:expand_aliases_in_source 级单元钉(E 别名必须展开)。
  • src/lexer/alias_stream.rs stream_tests:stream==batch 一致性钉(comsub 与 funsub 两形)。

本 PR 删除了什么

4 个回归钉从钉文件移出(移交 #3823,非删除——定义见 #3823 正文):issue3813_funsub_comment_quote、issue3813_funsub_heredoc_quote、issue3813_funsub_heredoc_brace_line、issue3813_nested_comsub_in_brace_expansion。其余无删除:仅修改 src/lexer/alias_stream.rs 的三处扫描器与新增回归钉;未删除任何公开 API、文档或行为面。

验证(X299-2607,头 e59c8fc 腿 + 7c8d28d 拆钉;两轮一致 04:52 / 05:31)

UNVERIFIED-BY-CAPTAIN

队长未复核。4 格真 lexer 钉已移交 rubash #3823,本 PR 钉 stem 11/11 绿,只修别名预扫描层(车道 REPORT §6 诊断 / §8 第 4 项);真 lexer 续作门控 #3808/#3797 合入,见 #3823。

…Refs #3813)

Alias T='{ f() {' + $(...) holding a #-comment with an odd number of
apostrophes fails to parse (rc=2, unexpected end of file from `{');
GNU is rc=0. Pins cover the issue parity table (p0-p4), comment after
$(, nested/dquoted/case comsub, alias-after-comment, heredoc in
comsub, nested comsub in ${...}, funsub comment/heredoc faces, and
the literal-$# / mid-word-# / ${...}-# controls that must keep
passing. Fail-first: expected red on base 969230b; fix follows.
… as opaque (Refs #3813)

skip_dollar_paren delegated to skip_parenthesized_unit_corrected
(comments at token boundaries and heredoc bodies opaque, case
`)\`' resolved like the body parser); skip_dollar_brace delegates
nested $(...) the same way; skip_funsub_body gains a #-comment arm
(token-boundary gated) and a << heredoc arm. GNU: parse.y:3631
shell_getc, parse.y:4451 parse_comsub, make_cmd.c:512. Same defect
class as #2033's pre-pass walkers; these three helpers were the
residual.
rustfmt --check (X299 leg) flagged 4 hunks; whitespace-only, no
semantic change: one guard join, one assert! join, one let split,
two control tuples expanded (trailing commas per rustfmt).
The 4 pins (funsub comment/heredoc x3, nested comsub in brace
expansion x1) need the REAL lexer layer: no-alias isolation probes
show base and fixed binaries behave identically on all four, so the
alias pre-scan fix in this PR cannot turn them green. They now live
in rubash #3823 (pin definitions + GNU anchors + the #3808/#3797
merge gate). This stem now holds the 11 pre-scan-layer pins only
(11/11 green on X299, two rounds consistent).

Refs #3813, #3823
@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】
PR #3821 第1轮 — 建议:PASS
head: 7c8d28d base(merge-base): 969230b 文件: 3 个 +399/-26
关联 issue: #3813(状态: open;残余真-lexer 形状已由 #3823 接管,状态 open)

机制核查:

  • 根因修复在 src/lexer/alias_stream.rs:skip_dollar_paren 改为委托既有修正版 skip_parenthesized_unit_corrected(skip.rs:1856,已带 token_boundary 注释/heredoc/case 纪律),skip_dollar_brace 对嵌套 $(...) 同样委托,skip_funsub_body 新增独立 at_word_start 状态实现 parse.y:3631-3645 shell_getc 的「# 仅在 token 边界起注释」+ << heredoc 体不透明(make_cmd.c:512)。是收敛到既有正确扫描器,非症状守卫。
  • diff 通读:无顺手无关改动;tests.rs 仅 +33 行单测;4 个真-lexer 钉按正文声明移出本文件归 funsub ${ ... } 体内 # 注释/heredoc 解析死亡,${x:-$(...)} 闭合 } 漏入输出(真 lexer 层,#3813 续作) #3823。
  • 相邻残差(不阻塞):funsub 体内 {#foo 粘连形状,GNU 将 {#foo 视为单词(探针 probes-y2/gnu-3813-bracehash.sh 双侧 rc=2 一致),扫描器按 { 开组 + # 注释处理——启发式预扫描的既有近似类,未随本 PR 变差。

腿:

  • 钉自跑:issue3813_alias_comsub_comment_quote_regressions 11/11 通过(head);lexer::tests::issue3813_alias_after_comsub_comment_quote_expands 1/1 通过
  • fail-first:base 969230b + 钉文件 → stem 3 passed / 8 failed,红名 = {alias_expands_after_comment_in_comsub, case_pattern_comsub_comment_quote, comment_right_after_comsub_open, comment_then_command_in_comsub, dquoted_comsub_comment_quote, heredoc_in_comsub_with_quote, nested_comsub_comment_quote, parity_apostrophe_count_in_comsub_comment}(8 缺陷钉红、3 控制钉绿,与 PR 声称一致);单测同红(E 不展开、输出止于 T)
  • lib 双边:head 1370 passed / 1 failed / 4 ignored {issue2084 基线};base(+钉) 1368/2/4 {issue2084, issue3813 单测钉};差集 = 本 PR 修复面,无新增失败
  • -D warnings:RUSTFLAGS="-D warnings" cargo check --locked --all-targets 通过,0 警告
  • 卫生:rustfmt 逐文件 3/3 rc=0(cargo fmt 遇 os error 206 按规程改逐文件);marker-lint OK(118/268);utf8-boundary-hygiene clean
  • CI(run 43965219143):census 红 36 stem vs master run 44002101833 红 37 stem,差集仅 master 侧多 issue371(已知 flake),无新增红;本 PR stem 在 CI 全 PASS;goldens 红 = {issue_753::case_9, upstream_suite_slice_snapshots} 与 master 一致 = 基线;其余 job 全绿
  • GNU 对照:WSL /usr/local/bin/bash 5.3.20(1),脚本 probes-y2/gnu-3813-alias.sh:alias T/E + comsub 奇数撇号注释 → GNU v=[hi] rc=0 / rubash v=[hi] rc=0,一致

F 点:无

未跑/VOID 的腿:无(本 PR 无 fd 族面,无需 WSL 原生 fd 复核)

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