Skip to content

fix(executor): keep a $( body parse error fatal to the enclosing command (Refs #3756) - #3843

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/3756-comsub-parse-discard
Nov 17, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/3756-comsub-parse-discard

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

根因

#3756(72e160b55)放开 $(case x in a|) 这类截断 case 单元后,comsub 体语法错误只在执行期判、且判据可被同一命令行里的后续替换覆盖,于是 GNU 的「读入期语法错误整段丢弃」退化成「打印错误后继续执行」:

$ cat case.sh
echo $(case x in a|) $(echo ok)
echo after

GNU 5.3.20 : syntax error near unexpected token `)'   rc=2   (无输出)
master     : syntax error near unexpected token `)'   rc=0   stdout=ok / after

-c 形态 GNU 127 / master 0。

GNU 锚点

  • parse.y:4451 parse_comsub():comsub 体在读词时交给语法分析器;体语法错误 → yyerror → jump_to_top_level (DISCARD),整个读入单元被丢弃,里面任何命令都不执行。
  • parse.y:3369-3386(special_case_tokens rule 6):case WORD in 的 in 即使在非保留字边界上也是 IN;parse.y:3379/3396 在此 arm pattern region。

修法(三处,共 46 行)

  1. expand_command_substitution_with_context 的入口复位不再清除非 contained($( 体,非 backtick/算术回退)的体错误闩锁 —— GNU 的体语法错误属于外层输入,同命令内的后续替换不得复活它;contained 体(subst.c string_locale -> parse_and_execute,子进程内解析)保持原逐点复位语义。
  2. execute_command_with_procsub_scope 的中止判据改为只看闩锁。原先还要求 last_command_substitution_status == Some(2),而该槽是文档化的 $? 左到右覆盖槽,下一个替换会写入自己子进程的 0,正好把致命结论抹掉(这就是 ok 被打印的直接原因)。所有闩锁点都同时置 2,判据不丢信息。
  3. 提取器 dollar_command_substitution 的 case 机器在 case WORD in 处未 arm pattern region(word_boundary 在主语词之后为 false),$(case x in a|) 被扫成未闭合 → 读入期 screen(rubash#2789)看不到截断体,跳过分支里的坏体(false && echo $(...))更是完全静默。按 rule 6 在 in 上 arm。

验证(WSL GNU bash 5.3.20 逐字节比对 rc/stdout/stderr)

-c 15 格:普通/引号/赋值/管道/and-or 跳过/相邻词/带后续命令 —— 除下列残差外全部一致(127、无输出、同诊断):

形态 GNU 修前 master 本 PR
`echo $(case x in a )` 127 127
`echo $(case x in a ) $(echo ok)` 127 0 (ok)
`echo $(case x in a ) $(echo ok) $(echo ok2)` 127 0 (ok ok2)
`v=$(case x in a ) w=$(echo ok); echo after` 127 0 (after)
`false && echo $(case x in a ); echo after` 127 0 (after, 无诊断)
`echo a$(case x in a )b` 127 1(错诊断)
`` x=`case x in a `; echo rc=$? `` 0 / rc=2 0 / rc=2
echo $(case a in a) echo hi;; esac) 0 / hi 同上 同上

脚本文件形态 12 格同样逐字一致(rc=2、无输出)。

回归

  • 新增 tests/issue3756_comsub_parse_error_discard.rs(4 格):fail-first 已确认 —— 在 master a593395f1 上 3 格失败(sibling_substitution…、skipped_branch…、script_file…),本 PR 全绿。
  • #3756 自带 tests/issue3307_repl_unclosed_case_comsub.rs 4/4 绿(原修复不回退);issue2784 5/5、issue2789 11/11、issue3229 10/10、issue435 9/9、case_command_head 4/4、issue1091_1092_1093 3/3、issue1184_f6_backquote_tail 8/8、issue2025 6/6、issue2026 5/5、issue1401 2/2、issue1618 7/7、issue1619 6/6、issue186_case_fallthrough 1/1、d11_pipeline_squote_carrier 6/6、comsub_nonascii_span 10/10、issue1485 1/1、issue1589 7/7 全绿。
  • 既有失败(同一测试在 master a593395f1 上逐字复现,非本 PR):issue1184_f6_backquote_eof 2、issue160_unclosed_quote_command_string 6、issue0069_…_comsub_capture 1、issue1982 3;lib 1374 passed / 1 failed(api::consumer_tests::issue2084_…,master 上同样失败)。
  • cargo fmt:仓级 cargo fmt --check 在本机报 os error 206(路径过长),改用 rustfmt --check --edition 2021 逐文件,通过。
  • 未编辑 src/lexer/continuation.rs(captain-exclusive)。

残差(本 PR 不动)

  1. heredoc 体内的坏体:cat <<EOF\n$(case x in a|)\nEOF —— 两者 rc=0 且后续命令照跑,但诊断文本不同(GNU line 2: syntax error near unexpected token ')',rubash line 3: unexpected EOF while looking for matching ')');修前 master 即如此。
  2. opener 形态 $(case x in () / (a|) / (|):GNU 127,rubash 2(修前 master 相同,属 #3307 的 gate 边界,见 #3756 正文)。
  3. $(case x in esac) 在提取器里现在返回整段 span 而非 None(等价的扫描层变化,行为与 GNU 一致,单测期望已随之更新并注明)。

Refs #3756 #3307 #3388

…mmand (Refs #3756)

GNU early.md anchor: parse.y:4451 parse_comsub hands the body to the
grammar while the word is read; a body grammar failure is yyerror ->
jump_to_top_level (DISCARD), so the enclosing input unit is abandoned
whole and NOTHING in it executes. Three rubash defects kept that from
holding after #3756 released truncated-case comsub units:

1. expand_command_substitution_with_context reset the body-error latch
   at every substitution entry, so a later substitution in the same
   word list cleared a fatal `$(` verdict and the command still ran
   (`echo $(case x in a|) $(echo ok)` printed `ok`, rc 0).
2. The abort in execute_command_with_procsub_scope keyed on
   last_command_substitution_status == Some(2); that slot is the
   documented left-to-right `$?` overlay and the next substitution
   overwrites it with its own child exit (0), masking the verdict. The
   latch alone now decides (every latch site also sets status 2).
3. The extractor's case machine never armed its pattern region for a
   plain `case WORD in` (word_boundary false after the subject word),
   so `$(case x in a|)` scanned as unclosed: the read-time body screen
   (rubash#2789) saw no node, and a skipped-branch body
   (`false && echo $(case x in a|)`) was silently ignored instead of
   killing the unit. GNU special_case_tokens rule 6 (parse.y:3369-3386)
   makes that `in` a reserved word regardless of boundary and
   parse.y:3379/3396 arms the region.

Verified byte-identical to WSL GNU bash 5.3.20 on 15 shapes x {plain,
quoted, assignment, pipeline, and-or-skip, adjacent-word, trailing
command} at rc/stdout/stderr; issue3307/2784/2789/3229/435 and the case
pins stay green.
…r (Refs #3756)

The pattern region now arms at the `in` token (GNU special_case_tokens
rule 6, parse.y:3369-3386), so the empty-case shape scans over its whole
span instead of running off the end. Executed behavior is unchanged
(`echo $(case x in esac)` and its sibling/assignment forms are
byte-identical to GNU 5.3.20), so the old scan-level None expectation is
replaced with the extracted span and the verified behaviors.
@caomengxuan666
caomengxuan666 merged commit 1a1b690 into master Nov 17, 2026
7 of 12 checks passed
caomengxuan666 added a commit that referenced this pull request Nov 18, 2026
…3701 (#3897)

1) Update keeps_comsub_case_subject empty-case expectation to Some("case $(ls) in esac") matching #3843 keeps_empty_case_esac_shape.
2) Restore exit status 2 for unmatched-delimiter EOF diagnostics in script_driver.rs (verified against GNU bash 5.3.20 in WSL: all word-internal pairs and -c/-n/file modes exit 2), keeping #3701 ANSI-C $'...' comsub walker fixes.
3) Restore status 2 assertions in consumer_tests.rs, issue1184_f6_backquote_eof_regressions.rs, and issue3605_eof_exit_status_word_pairs.rs.
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