Skip to content

fix(parser): quoted parens are not a function-definition head (Refs #3388) - #3744

Closed
arookieofc-ai wants to merge 1 commit into
masterfrom
ai-fix/rubash-3388
Closed

arookieofc-ai wants to merge 1 commit into
masterfrom
ai-fix/rubash-3388

Conversation

@arookieofc-ai

Copy link
Copy Markdown
Contributor

Root cause

The function-definition probe in src/parser/function_command.rs matched the () head by token VALUE only, so the quoted words '(' / ')' / '()' — ordinary Word tokens whose dequoted value is ( / ) / () — were mistaken for the WORD '(' ')' function head. The probe then demanded a { body and reported a syntax error at whatever followed (;, ], the next word, or EOF), aborting the whole script.

Reproduction (before the fix)

$ rubash -c "[ '(' ')'; echo rc=\0"
rubash: -c: line 1: syntax error near unexpected token `;`
$ rubash seed.sh   # issue seed: two controls PASS, then the script dies

GNU (bash 5.2.21 here; the issue was filed against 5.3.20, same behaviour) treats [ as an ordinary builtin: missing ] is only a runtime [: missing ]'' error, status 2, and the script continues.

Fix

Gate the paren shapes in parse_function_command_with_diagnostic (admission, compact/separated (), and the !! reassembly) on genuine operators: is_keyword (kind Keyword) for ( / ), and unquoted raw text (raw == value, the codebase's unquoted idiom) for a lone () token — the lexer never emits an unquoted single () operator token, so that arm previously could only ever fire on quoted words.

Verification (box, GNU bash 5.2.21 baseline)

  • Issue seed: 4/4 PASS; -c form prints rc=2, overall rc 0, stderr [: missing ]'' — byte-identical behaviour to GNU.
  • 18 neighbour cases checked against GNU, all identical (modulo the program-name prefix): quoted parens as plain words for any command (echo '(' ')', echo '()'), closed [ with quoted parens is a runtime test error, ! / test forms, and — unchanged — unquoted parens still parse-error at the same token GNU blames ([ ( a = a ), [ a ( b ], echo (), [ ( ) ]), unclosed [[ ]] still a parse error, and all three function-definition spellings (f() {}, f () {}, function f() {}`) still define and run.
  • cargo test --lib: 1249 passed, 0 failed (4 ignored). Note: with --test-threads 2 the run showed one failure in issue2084_session_end_restores_host_process_state, which passes in isolation and single-threaded — that test's own comment documents it is only deterministic with no overlapping sibling session era, so this is its known parallelism sensitivity, not a regression.
  • Related integration suites green: audit_a1_parser_regressions (7), issue1041, issue1154, issue1275, issue1610 (3), plus the new suite below (8).
  • cargo fmt --check, git diff --check clean.
  • New regression suite: tests/issue3388_unclosed_bracket_regressions.rs (8 tests: seed, -c form, quoted parens as plain words, closed-bracket runtime error, !/test forms, unquoted-paren parse errors, unclosed [[ still a parse error, function definitions still work).
  • Not run on this box: Windows builds (cross-compile) — CI covers those.

Refs #3388

…3388)

The function-definition probe matched the '()' head by token value only, so the quoted words '(' / ')' / '()' (ordinary Word tokens) were mistaken for the WORD '(' ')' head. The probe then demanded a '{' body and reported a syntax error at whatever followed, aborting the whole script: '[ '(' ')' died at ';' instead of reaching the '[' builtin, which must report '[: missing ']'' (status 2) at runtime and let the script continue, as GNU does. Gate the paren shapes on genuine operators (is_keyword / unquoted raw text).
@caomengxuan666 caomengxuan666 added bug Something isn't working P2-medium 中:次要缺陷或边缘路径,排期修复 area:parser lexer/parser: heredoc collection, comsub body re-parse, quoting state gnu-baseline Tracked by the GNU Bash 5.3.0 true-baseline ledger labels Nov 15, 2026
@caomengxuan666 caomengxuan666 self-assigned this Nov 15, 2026
@caomengxuan666

Copy link
Copy Markdown
Member

Thanks for the PR — and for the thorough write-up and GNU comparison.

First-pass review (intake, not a merge verdict — the captain reviews external PRs first):

  • Mechanism read-through: the fix gates all three paren-shape sites in parse_function_command_with_diagnostic / bang_group_function_name on genuine operators (is_keyword for ( / ), raw == value for a lone ()), matching the GNU read_token / read_token_word reasoning in the body. The three sites are consistent with each other, and the negative cases (unquoted parens still parse errors, unclosed [[ still a parse error, all three function-definition spellings still work) are covered by the new suite.
  • New suite: tests/issue3388_unclosed_bracket_regressions.rs, 8 tests including the issue seed verbatim. Note: CI's Rust-tests job runs a selected nextest table and does not run new-nail stems, so the 8/8 is your local claim for now — the captain's own re-run is what verifies the nails.
  • CI (run 43749001908 + canary 43749001997), checked by name: all reds are the known baseline, none PR-specific. Rust tests (Linux and macOS) and GNU goldens fail on exactly one cell each, issue2875_input_procsub_child_runs_as_rubash_shell; niubash-builds fails only on the ledger step (MISSING session-method rubash::api::Session::cwd, the known ledger drift). Everything else is green, including Compatibility CLI, Shell functionality, both cross-target checks, and the Windows compile check.

One thing the captain will need to decide: #3388 already has an in-house implementation in flight — PR #3558 (owner, Fixes #3388, head dba90ff1, 2026-11-14), on the same mechanism (operator kind, not text) but with two differences from this PR: it uses a Keyword-kind predicate for a lone () where this PR uses raw == value, and it additionally converts the name() ( EOF arm and the lt_group_function_name paren-depth counting, which this PR leaves on value-only checks. This PR is based on the current tip (e3a7b7b3); #3558's branch is 118 behind master. Which one lands first, and whether the nail suites get merged, is for the captain to decide — please don't rebase or close anything on the basis of this comment.

Queued for captain review; CI watched. Thanks again.

@caomengxuan666

Copy link
Copy Markdown
Member

队长裁决(#3388 双在途):先行实现 = #3558,本 PR 暂缓、不关闭、不并行改产品面。

核查依据(队长逐 hunk 对照两支 diff + 按名两支 CI):

本 PR 的存活价值:钉文件保留并将被吸收。tests/issue3388_unclosed_bracket_regressions.rs(247 行)比 #3558 的钉(107 行)覆盖更广,两套钉文件名不冲突、可并存。#3558 合入后,请将本 PR rebase 为只留钉文件(产品 hunk 丢弃),队长按 fail-first(在未修尖 e3a7b7b 上红、合并尖上绿)复核后合入,作者署名保留。在此之前请勿自行 rebase 或关闭本 PR(与首评口径一致)。

#3558 的下一步在队长侧:update-branch 到现尖 e3a7b7b 后重跑 CI 按名 + 队长复核,再合入(其正文为 Fixes #3388,合入即关单)。

@caomengxuan666

Copy link
Copy Markdown
Member

【WIN-AGENT-AUDIT】PR #3744 第1轮 — 建议:PENDING(缺腿)
head: 4d9f51c(gh 实时)state: OPEN(非 draft)
关联 issue: #3388

处置:队长已裁先行实现 = #3558(覆盖面更广:另含 EOF 诊断臂与 lt_group_function_name 括号深度两处同病灶),本 PR 暂缓、不关闭、不并行改产品面。

@caomengxuan666

Copy link
Copy Markdown
Member

【WIN-AGENT-AUDIT】PR #3744 第2轮 — 建议:PENDING(钉已验证可吸收;产品面被已合入的 #3558 覆盖,按队长既定口径 rebase 为只留钉)
head: 4d9f51c(gh 实时)base(merge-base): e3a7b7b 文件: 2 个 +268/-14
关联 issue: #3388(closed via #3558 Fixes;我核过的时间:2026-11-17 09:2x +08)

机制核查:

  • GNU 侧属实:function_def: WORD '(' ')'(parse.y:1054-1056)消费的是 read_token 的算子 token——(/) 仅在未引用时走 shellmeta 分支成算子(parse.y:3667 if MBTEST(shellmeta (character))),引用/转义括号落入 read_token_word(parse.y:5305)成为 WORD。按 value 判定会把 '('/')'/'()' WORD 误当函数头。
  • 本 PR 三处闸(admission paren_follows、compact/separated ()、bang_group_function_name)用 is_keyword(kind==Keyword && value==text,另带 regexp_rhs_fragment 排除)+ value=="()" && raw=="()"。与 fix(parser): match function-head parens on the operator token, not its text (Fixes #3388) #3558 已合入的 is_paren_operator(kind==Keyword && value==text)在本 issue 形状上语义等价;差异仅在覆盖面——fix(parser): match function-head parens on the operator token, not its text (Fixes #3388) #3558 另含 name() ( EOF 诊断臂与 lt_group_function_name 深度计数两处同病灶,本 PR 未动,属严格子集。
  • diff 无顺手改动、无桥接层;可疑点:无。

腿:

  • 钉自跑:issue3388_unclosed_bracket_regressions head 8/8 pass。
  • 钉对 master 尖(969230b9,含 fix(parser): match function-head parens on the operator token, not its text (Fixes #3388) #3558 合入 72ae835):8/8 pass——钉与已合入机制语义一致,可直接吸收。
  • fail-first:merge-base e3a7b7b 同文件 3 passed / 5 failed,红名 = {issue_seed_scores_four_of_four, dash_c_form_reports_rc2_and_continues, quoted_parens_are_plain_words_for_any_command, closed_bracket_with_quoted_parens_is_a_runtime_error, bang_and_test_forms_match_gnu},绿控制 = {unquoted_parens_still_parse_errors, double_bracket_unclosed_still_parse_error, function_definitions_still_work}——有效 fail-first。
  • lib 双边:head 失败名集 {api::consumer_tests::issue2084_session_end_restores_host_process_state} / base 同集 / 差集 ∅(1351p/1f/4i 两边一致;issue2084 在本机为环境性红,隔离重跑同红,与本 PR 无关)。
  • -D warnings:cargo check --all-targets 命中 2 处预存在错误——tests/issue2036_s2_introspection_windows.rs:56 dead_code stderr、tests/issue566_bare_drive_root_enoent_regressions unused import Path;两文件均不在本 PR diff(git diff 为空),PR 自身文件 0 警告。
  • 卫生:rustfmt --edition 2021 --check 两文件 clean;marker-lint OK(117 consts/268 sites);utf8-boundary clean。
  • CI(run 43749001908/997 按名核):Rust tests( Linux/macOS ) 与 goldens 红格唯一 = issue2875_input_procsub_child_runs_as_rubash_shell(基线);niubash-builds 红 = MISSING session-method rubash::api::Session::cwd(已知 ledger 漂移,docs(ledger): record Session::cwd #3704/niu#1021 在途)。无 PR 专属红。
  • GNU 对照:WSL /usr/local/bin/bash 5.3.20(1)-release,脚本文件 target/probe-3744-safe.sh(LF)——stdout 逐字节一致、stderr 逐行一致(仅脚本路径前缀差)、退出码一致(尾部未闭合 [[ 双双 rc=2);三个 unquoted-paren 解析错误形状(target/probe-3744-pe{1,2,3}.sh)诊断与 rc=2 全等。Windows 对照已按规程标注:结论依赖 Linux 行为处待队长 Linux oracle 复核(本例语义平台无关)。

F 点:无(判 PENDING 系队长已裁暂缓 + #3558 已合入后的吸收路径,非质量问题)。

说明:本 PR 正文无「删除了什么」一节(产品 diff 仅替换判定式,-14 行),属正文卫生小疵,不影响判词。

未跑/VOID 的腿:无。

@caomengxuan666

Copy link
Copy Markdown
Member

评审结论:SKIP — superseded(重复实现),不合入,不关闭。

#3744 与已合入 master 的 #3558(72ae83516 fix(parser): match function-head parens on the operator token, not its text (Refs #3388))是同一个 issue #3388 的同一处修复,GNU 锚点也相同(parse.y:3667 read_token 的 shellmeta 分支 + parse.y:1054-1056 function_def: WORD '(' ')')。

master 侧的实现已经更完整:统一谓词 is_paren_operator(token, text) = token.kind == TokenKind::Keyword && token.value == text(src/parser/function_command.rs:15),并覆盖了本 PR 只改到的三处之外的其余 paren 站点(:468 parenthesized body、:1149-1190 !!/<(...) 重拼),而本 PR 的 token.raw == "()" 只在三处使用。

证据(在本评审 worktree,detached + merge origin/master da256e71a,冲突处取 master 侧 src/parser/function_command.rs,仅保留本 PR 的测试文件):

  • cargo build 绿;cargo test --test issue3388_unclosed_bracket_regressions → 8 passed / 0 failed,即本 PR 自带的 8 个 pin 在不含本 PR src 改动的 master 树上全绿(fail-first 已由 fix(parser): match function-head parens on the operator token, not its text (Fixes #3388) #3558 满足)。
  • probe3744.sh(LF,MSYS_NO_PATHCONV=1 wsl bash,GNU 5.3.20(1))与本 PR 合并树二进制输出逐字节一致(仅 $0 前缀差异):[ '(' ')'; … → [: missing \]'+rc=2 后继续;echo '(' ')'→( );echo '()'→();[ '(' ')' ]→[: (: unary operator expected rc=2;! '(' ')'→(: command not found rc=0;test '(' ')' → rc=2;f() {}/f () {}/function g() {}/function h {} 四种定义均正常;echo ()→syntax error near unexpected token `;'`,整体 rc=2。

下一步建议:关闭为 duplicate 由人工决定(本车道不关闭 PR);若 #3388 仍有残留面未覆盖,请另开基于 master 的最小 PR。

@caomengxuan666

Copy link
Copy Markdown
Member

【P1 车道复核】PR #3744 — 判词:SKIP / superseded(当前 master 已用 #3558 的实现修掉同一缺陷,本 PR 的修法与 master 在同一 hunk 语义冲突)

  • head:4d9f51c6c35b22add3ca7b0969e71ef38383c2bc;git merge origin/master b2e488e54 → CONFLICT (content): src/parser/function_command.rs(3 处),已 abort
  • master 侧已合入:72ae83516 fix(parser): match function-head parens on the operator token, not its text (Refs #3388) (#3558),并带 tests/issue3388_quoted_paren_function_head.rs
  • 冲突性质:master 引入 is_paren_operator(token, "(") / is_paren_operator(token, "()")(kind == TokenKind::Keyword),本 PR 的同一位置是 is_keyword(...) || (token.value == "()" && token.raw == "()")。两者都表达 GNU parse.y:3667 read_token 的「引号内 paren 是 WORD、裸 paren 才是 operator」,同一判定的两种写法,不是相邻行夹具冲突。

覆盖核验:本 PR 的测试在 master 上全绿

把本 PR 的 tests/issue3388_unclosed_bracket_regressions.rs(8 格)原样放到 master 树上跑:

running 8 tests
bang_and_test_forms_match_gnu ... ok
closed_bracket_with_quoted_parens_is_a_runtime_error ... ok
dash_c_form_reports_rc2_and_continues ... ok
double_bracket_unclosed_still_parse_error ... ok
function_definitions_still_work ... ok
issue_seed_scores_four_of_four ... ok
quoted_parens_are_plain_words_for_any_command ... ok
unquoted_parens_still_parse_errors ... ok
test result: ok. 8 passed; 0 failed

即本 PR 想钉的行为(含 [ '(' ')' ]、-c rc2、bang/test 形、未引号 paren 仍报错、函数定义仍可用)在 master 上已全部成立;本 PR 只剩测试增量价值,无产品增量。

未关闭 PR / 未关闭 #3388(保持 OPEN)。

@caomengxuan666

Copy link
Copy Markdown
Member

【WIN-AGENT-AUDIT】
PR #3744 第3轮 — 建议:FAIL(需返工:按队长既定口径 rebase 为只留钉;产品面已被 #3558 完整覆盖)
head: 4d9f51c(gh 实时,未变)base(merge-base): e3a7b7b 文件: 2 个 +268/-14
关联 issue: #3388(closed via #3558;核于 2026-11-17 17:5x +08)

机制核查:

  • GNU 侧属实(沿用第2轮):function_def: WORD '(' ')'(parse.y:1054-1056)消费 read_token 算子 token;引用/转义括号是 WORD(parse.y:5305 read_token_word、parse.y:3667 shellmeta 分支)。
  • master 已合入 72ae83516(fix(parser): match function-head parens on the operator token, not its text (Fixes #3388) #3558):统一谓词 is_paren_operator(src/parser/function_command.rs:15),覆盖面为本 PR 三处闸的严格超集(另含 EOF 诊断臂与 lt_group_function_name 深度计数)。git merge origin/master 在 function_command.rs 3 处语义冲突(P1 车道已实证 abort)。
  • 本 lane 无权续作:本 PR 非 draft 且为外部贡献者分支,规程 §1.2 只允许在草稿 PR 原分支续作;队长口径亦要求作者自行 rebase。

钉增量核查(本轮核心,决定 合钉/关/补形状):

  • 逐格比对 tests/issue3388_unclosed_bracket_regressions.rs(8 钉,247 行)vs master tests/issue3388_quoted_paren_function_head.rs(4 钉,107 行):
  • 重复面:dash_c_form_reports_rc2_and_continues ≡ master dash_c_unclosed_bracket_continues(同一脚本 [ '(' ')'; echo rc=$?);function_definitions_still_work ⊂ master unquoted_function_heads_still_define_functions(master 另含 g() () 子shell 体形);seed 格与 master seed 近似(PR 多一个 plain-bracket 对照行)。
  • 净增量(master 钉文件未覆盖):① closed_bracket_with_quoted_parens_is_a_runtime_error——闭合 [ '(' ')' ] 的 unary/binary operator expected 运行时错(master 只钉未闭合 missing-]);② bang_and_test_forms_match_gnu——! [与test两形;③unquoted_parens_still_parse_errors——4 条未引用括号仍 rc2 解析错的阴性对照(master 钉文件无此类);④ double_bracket_unclosed_still_parse_error——[[未闭合仍解析错;⑤quoted_parens_are_plain_words_for_any_command中echo '()'与未知命令foo '(' ')'` 两格。
  • 结论:钉有真实增量价值,判 合钉——即按队长 2026-11-15 裁决把本 PR rebase 为只留钉文件(产品 hunk 全弃),两钉文件名不冲突可并存。

腿:

  • 钉自跑:沿用第2轮 head 实测 issue3388_unclosed_bracket_regressions 8/8 pass。
  • 钉对 master:两轮独立实测(07:42Z 在 da256e7 合并树、08:16Z 在 b2e488e)均 8/8 pass;function_command.rs 自 fix(parser): match function-head parens on the operator token, not its text (Fixes #3388) #3558(72ae83516)后零提交(git log 复核),da256e71a..ccb8614 仅动 command_substitution.rs/redirections.rs,与本面无关 → 证据对当前尖 ccb8614 仍有效,未重跑(需整构建,判为可接受沿用)。
  • fail-first:merge-base e3a7b7b 同文件 3p/5f(第2轮实测),红名与声称一致。
  • lib 双边 / -D warnings / 卫生 / CI:沿用第2轮(lib 差集 ∅;warnings 2 处预存在且不在本 PR diff;fmt/marker/utf8 clean;CI 红格仅基线 issue2875 + 已知 niubash-builds 漂移)。
  • GNU 对照:沿用第2轮 WSL /usr/local/bin/bash 5.3.20(1),stdout/stderr/rc 逐字节一致。

F 点:

未跑/VOID 的腿:钉对当前尖 ccb8614 未重跑(理由见上,function_command.rs 零漂移);其余无。

@caomengxuan666

Copy link
Copy Markdown
Member

Thank you for this fix — the root-cause analysis is right, and the same mechanism landed via #3558 (filed 2026-11-14, a day before this PR, and merged 2026-11-16): the probe now matches parens on the operator token (is_paren_operator) instead of token text. This PR's three product hunks now conflict with master precisely because #3558 already replaced those exact checks, so the product change cannot land.

Your regression suite is not lost: it covers four shapes #3558's own pins lack (closed-bracket runtime error, !/test forms, unquoted-paren parse-error preservation, unclosed [[ guard). I have absorbed the pin file verbatim into #3844 with your authorship preserved; it will merge once its CI census is green. #3388 is already closed via #3558. Closing this PR as superseded — thank you again.

caomengxuan666 added a commit that referenced this pull request Nov 17, 2026
The fix for #3388 landed via #3558 (same operator-token mechanism).
#3744's pin file adds four coverage shapes #3558's pins lack: the
closed-bracket runtime error, !/test forms, unquoted-paren parse-error
preservation, and the unclosed [[ guard. Pin file taken verbatim from
#3744, authorship preserved.

Co-authored-by: arookieofc-ai <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:parser lexer/parser: heredoc collection, comsub body re-parse, quoting state bug Something isn't working gnu-baseline Tracked by the GNU Bash 5.3.0 true-baseline ledger P2-medium 中:次要缺陷或边缘路径,排期修复

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants