Skip to content

fix(#3823): funsub ${...} bodies treat # comments and heredocs as opaque spans - #3836

Merged
caomengxuan666 merged 3 commits into
masterfrom
fix/3823-funsub-lexer-residual
Nov 17, 2026
Merged

caomengxuan666 merged 3 commits into
masterfrom
fix/3823-funsub-lexer-residual

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Refs #3823

根因

真 lexer 层残差(#3813 的余项):三处 funsub/${...} 范围扫描器在 # 注释与 heredoc 体内继续做引号跟踪,于是注释里的撇号(# don't)打开一个幻影单引号,把剩余输入全部吞掉;heredoc 体内的 } 行要么提前关闭 funsub,要么被一并吞掉。${x:-$(...)} 的 } 漏进值里是同一机制在下一层:walk 侧 ${...} 体收集器没有 $(...) 单元臂,' 吞掉闭合 },} 就成了默认值的一部分。

GNU 锚点(bash 5.3)

  • parse.y:3631-3645 shell_getc:# 在 token 起始位置恒为注释,丢弃至行尾。
  • parse.y:4451 parse_comsub:funsub/$(...) 体由真实解析器解析,注释与 heredoc 是不透明 span。
  • parse.y:5506 funsub(parser.h:85 FUNSUB_CHAR)。
  • make_cmd.c:512 make_here_document:heredoc 体按原始行读取。
  • subst.c:1825 extract_dollar_brace_string:${...} 内的 $(...) span 交给 parse_comsub。

GNU bash 5.3.20 实测:四形皆 rc=0 且值为预期(v=[hi] / v=[it's here] / v=[}] / v=[hi])。

改动

  • src/lexer/skip.rs:Lexer::skip_braced funsub 分支新增 token-boundary 门控的 # 注释臂与 << heredoc 臂(复用 skip_heredoc_in_command_substitution)。
  • src/executor/command_substitution.rs:不可变 walk 的 extract_funsub_body_tail 补同样的两臂(新增 push_funsub_heredoc 流式 helper:header + 体行原样复制、定界符 dequote、<<- 去 tab、引号定界符、<<< herestring 不当 heredoc)。
  • src/executor/command_subst_helpers.rs:collect_braced_parameter_body_into 把 $(...) 作为一个单元整体跳过(经 skip_parenthesized_unit_corrected)。
  • src/lexer/dolbrace.rs:skip_parenthesized 补 # 注释臂(仅注释;heredoc 仍归 corrected scanner)+ 单元测试 comment_quote_inside_nested_comsub_does_not_swallow_closing_brace。
  • src/lexer/alias_stream.rs:skip_dollar_brace 补 funsub 分支,委托给 skip_funsub_body(否则预扫描把 funsub 词吞到 EOF,闭合别名 E 永不展开)。
  • tests/issue3823_funsub_lexer_residual.rs:回归钉四格(f1/h2/h3/d1,从 modernish builtin.t 007 解析死亡:别名 { f() { + $(…) 内奇数撇号注释致 rc=2 #3813 钉文件拆出,改名 issue3823_*)。

预期钉状态(重要:f1/h2/h3 暂红)

本 PR 删除了什么

无删除。仅新增:push_funsub_heredoc helper、钉文件、dolbrace 单元测试;其余皆为既有扫描器补臂。

验证(欠账,见 REPORT)

  • 本地:四形在近尖二进制(969230b9,src/lexer/ + src/parser/ 与 base a0a6d78 等价)与 GNU 5.3.20 oracle 上已复现/对照;钉的 fail-first 证据见 modernish builtin.t 007 解析死亡:别名 { f() { + $(…) 内奇数撇号注释致 rc=2 #3813 REPORT §6(同四格在预扫描修复后仍红)。
  • 欠:X299 腿(钉 stem 四格 fail-first→修绿、lib 双边、RUSTFLAGS="-D warnings"、rustfmt --check)、服务器 Linux 腿。VM→服务器出口代理中断、本地无 cargo/rustfmt 且 MemAvailable 低于 1.5G 构建门,故本轮未跑;路由恢复后补跑。

UNVERIFIED-BY-CAPTAIN

…que spans

Root cause: three funsub/${...} extent scanners quote-tracked through
# comments and heredoc bodies, so an apostrophe in a comment (# don't)
opened a phantom single-quote that swallowed the rest of the input, and
a } line inside a heredoc body either closed the funsub early or was
swallowed with it. ${x:-$(...)} leaked its closing } into the default
word for the same reason one layer down (the ${...} body collector had
no $(...) unit arm).

GNU anchors (bash 5.3): parse.y:3631-3645 shell_getc (# at a token start
discards to EOL); parse.y:4451 parse_comsub (body parsed by the real
parser); parse.y:5506 funsub (parser.h:85 FUNSUB_CHAR); make_cmd.c:512
make_here_document (body read as raw lines); subst.c:1825
extract_dollar_brace_string.

- src/lexer/skip.rs: Lexer::skip_braced funsub branch gains a
  token-boundary-gated # comment arm and a << heredoc arm.
- src/executor/command_substitution.rs: extract_funsub_body_tail gains
  the same two arms (new push_funsub_heredoc helper).
- src/executor/command_subst_helpers.rs: collect_braced_parameter_body_into
  skips $(...) as one unit via skip_parenthesized_unit_corrected.
- src/lexer/dolbrace.rs: skip_parenthesized gains a # comment arm
  (comment-only; heredocs stay with the corrected scanner) + unit test.
- src/lexer/alias_stream.rs: skip_dollar_brace gains a funsub branch
  delegating to skip_funsub_body.
- tests/issue3823_funsub_lexer_residual.rs: regression pins f1/h2/h3/d1.

Known limitation: f1/h2/h3 also need the mut walker's funsub collector
(expand_current_shell_braced_substitution, embedded_mutations.rs), which
is BLOCKED on in-flight #3817 (same file). Until that hunk lands, the
f1/h2/h3 pins are expected RED (literal-text values); d1 is green with
this change. See REPORT.md in the workdir.

Refs: #3823
@caomengxuan666

Copy link
Copy Markdown
Member Author

【P1 车道复核】PR #3836 — 判词:SKIP / 半成品(按 PR 自述,3/4 钉按设计仍红;缺的 hunk 依赖 #3817,而 #3817 本轮判 FAIL)

  • head:9b0aef6513438493f6dc1643b4b02f117b2a7ad1;本轮 merge origin/master b2e488e54 无冲突
  • 构建:cargo build(Windows MSVC debug)绿
  • 钉实测:cargo test --test issue3823_funsub_lexer_residual → 1 passed / 3 failed
    • 绿:issue3823_funsub_default_value_brace(d1)
    • 红:issue3823_funsub_comment_quote、issue3823_funsub_heredoc_brace_line、issue3823_funsub_heredoc_quote(f1/h2/h3)
  • 与 PR 正文「预期钉状态(重要:f1/h2/h3 暂红)」完全一致:赋值 RHS 走 MUT walk(src/executor/embedded_mutations.rs),该文件的 funsub 收集器仍缺 # 注释臂与 << heredoc 臂;PR 因同文件面护栏没有动它。

红钉即阻断:合入会给 master 带 3 条红(该 stem 当前不在 scripts/ci-pin-exclusions.txt 里)。变绿条件:

  1. test: add fail-first regression pins for literal control-byte constants (Refs #3816) #3817(字面控制字节常量损坏:32 个 CC 常量中 10 个被替换为元字符 #3816)落地或被否决后,在 embedded_mutations.rs 的 funsub 收集器补镜像两臂(token-boundary 的 # 跳至 \n;<< 经既有 heredoc helper;term=true 仅当 paren_depth==0),并把 3 格钉跑绿。
  2. 同时给出 GNU 5.3.20 脚本文件对照(${x:-$(cat <<\) ... } / # don't 四形),确认 heredoc/comment 作为不透明 span 的边界。
  3. 重跑 issue3813_alias_comsub_comment_quote_regressions 等邻接 stem。

未关闭 PR / 未关闭 #3823(保持 OPEN);本 PR 保持 draft。

…opaque spans (Refs #3823)

Complete the two missing funsub arms so all four pins pass:

- expand_current_shell_braced_substitution (embedded_mutations.rs):
  add the at_word_start token-boundary state, the parse.y:3631
  shell_getc `#` comment arm, and the make_cmd.c:512 heredoc arm
  (reusing push_funsub_heredoc, `<<<` stays a herestring), mirroring
  extract_funsub_body_tail.
- scan_braced_chars_from (dolbrace.rs): dispatch `${`+FUNSUB_CHAR to
  alias_stream::skip_funsub_body so the residual gates
  (param_residuals, unquoted_brace_group_depth,
  command_substitutions_balanced, continuation.rs callers) stop
  closing the span at a `}` inside a heredoc body or a `'` inside a
  comment.
- db_scan_dolbrace_body (skip.rs): same FUNSUB_CHAR dispatch into a
  new db_scan_funsub_body carrying comment/heredoc/quote/comsub arms
  and the command-position `}` close, so unclosed_dollar_bracket_eof
  stops reporting a phantom `'` for f1/h2.
- current_shell_command_substitution_span (parameter_core.rs): take
  the extent from extract_funsub_body itself instead of a parallel
  depth count, so the detector and the collectors share one scan;
  `prefix${| echo reply }suffix` now correctly has no span (GNU:
  unexpected EOF while looking for matching `}', rc 2).

GNU anchors: parse.y:3631-3645 shell_getc, parse.y:4451 parse_comsub,
parse.y:5506 FUNSUB_CHAR (parser.h:85), make_cmd.c:512
make_here_document, parse.y:5400-5416 funsub `}` close. Probed on
GNU bash 5.3.20: all four pin shapes and the heredoc `}`-line
variants match byte-for-byte.
@caomengxuan666

caomengxuan666 commented Nov 17, 2026 •

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】 PR #3836 第2轮 — 建议:PASS(待队长 ready/合并裁决)
head: 82e51f8f198ae187fe603c4b599184312d36917b(原 9b0aef651 + merge 26815c265 + 本轮续作 82e51f8f1)
base(merge-base): ccb86141efe863eae054fa6ccc95cbfa58a4a276 文件: 8 个 +938/-54
关联 issue: #3823(Refs;状态 open,核于本地 2026-11-17 晚)

机制核查:

  • 缺的两臂已按 GNU 语义补齐:funsub 体内 # 仅在 token 边界起注释(parse.y:3631-3645 shell_getc 丢弃至行尾);<< 走 heredoc helper、<<< 是 herestring 不吃体(make_cmd.c:512 make_here_document 原始行读取);funsub 闭合 } 须为独立词(parse.y:5400-5416 PST_FUNSUBST、parse.y:5506 lookahead、parser.h:85 FUNSUB_CHAR)。term(命令位)与 at_word_start(token 边界)两态分离——{/} 只在命令位影响嵌套/闭合,# 只看 token 边界。
  • 覆盖面:skip_braced(skip.rs)、skip_funsub_body(alias_stream.rs)、scan_braced_chars_from(dolbrace.rs)、extract_funsub_body_tail+push_funsub_heredoc(command_substitution.rs)、expand_current_shell_braced_substitution(embedded_mutations.rs,上轮缺的 mut-walk 臂)、current_shell_command_substitution_span 改为复用同一 extent 收集器(parameter_core.rs 检测器与收集器同源)。
  • 可疑/无关 diff:无。parameter_core.rs 的测试修正有 GNU 依据——prefix${| echo reply }suffix 中 } 与 suffix 粘连不构成闭合(闭合符须起词),GNU 5.3.20 实测一致;改为 ; }suffix 正例 + 粘连反例。
  • 注:PR 正文「预期钉状态」一节所述 f1/h2/h3 阻塞(test: add fail-first regression pins for literal control-byte constants (Refs #3816) #3817 同文件占用)已随 test: add fail-first regression pins for literal control-byte constants (Refs #3816) #3817 合入(ccb86141)与本轮 hunk 落地解除。

腿(逐项):

  • 钉自跑(head 82e51f8f1):issue3823_funsub_lexer_residual 4 passed / 0 failed(f1/h2/h3/d1 全绿)
  • 相邻回归 slice(head,全绿):issue3813_alias_comsub_comment_quote 11、issue543_dollar_bracket_eof 14、issue543_heredoc_cap 12、issue2227/2374/2375 各 1、issue503_dolbrace_walker 4、wt56_dolbrace_first_close 5、issue977_dolbrace_ifs 9、issue2763_heredoc_funsub_exit_latch 6、issue2033_alias_comsub_comment_park 3、issue3605_alias_expansion_lines 3、posixexp2_comsub 12、issue305_comsub_delimiter 4、issue513_comsub_comment_close 6
  • fail-first:
    • merge-base ccb86141(钉文件原样放入):0 passed / 4 failed,红名 = {funsub_comment_quote, funsub_heredoc_brace_line, funsub_heredoc_quote, nested_comsub_in_brace_expansion} —— 四钉全部有效
    • 续作前 head 26815c265(= 9b0aef651+master 合并):1 passed / 3 failed,红名 = {comment_quote, heredoc_brace_line, heredoc_quote},与 P1 复核「3/4 红」一致
  • lib 双边:head 82e51f8f1 失败名集 {api::consumer_tests::issue2084_session_end_restores_host_process_state}(1375 pass/1 fail/4 ignored)/ base 26815c265 同集合同名单(1375/1/4)/ 差集 ∅。issue2084 两侧同红 = 非本 PR 账(Windows env bookkeeping 消费侧测试,疑环境性,供队长知悉)
  • -D warnings:RUSTFLAGS="-D warnings" cargo check --locked --all-targets 通过,0 警告
  • 卫生:rustfmt 逐文件 --check 7/7 OK(cargo fmt 在本机 Windows 路径下 os error 206,沿用既有逐文件回退);marker-lint check-marker-literals.py OK(118 registry / 269 allowlist);utf8 check-utf8-boundary-hygiene.sh clean
  • CI(旧 head 9b0aef651,run 44032770392/44032770348):红 3 项——Regression pin stem census(已知基线 ~36 stem)、GNU regression goldens and canaries(基线)、niubash builds against this tree(check-api-ledger 报 MISSING consumed rubash::api::BindKindView::Keymap——niubash master 与本树 api-ledger 的跨仓漂移,本 diff 无 api 面改动,不背锅;新 head CI 待跑)
  • GNU 对照:WSL /usr/local/bin/bash = GNU bash 5.3.20(1)-release (x86_64-pc-linux-gnu),脚本文件 target/issue-suites/results/e1-3836/case-{f1,h2,h3,d1}.sh(与钉同形 alias_frame):四形 GNU 与 head 输出逐字一致——v=[hi]/v=[it's here]/v=[}]/v=[hi] + done,rc 全 0

F 点:无

续作记录:

未跑/VOID 的腿:

  • 服务器 Linux 腿未跑(本机 Windows;GNU 对照已用 WSL 5.3.20 脚本文件,Linux 专属行为如有请队长 oracle 复核——本 diff 为纯文本扫描层,无平台分支)
  • 新 head 的 CI 未出结果(push 后触发中)

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