Skip to content

fix(builtins): invalid-identifier compound operands report plain word; declare 'a[]=x' reports bare bad array subscript (Refs #3186) - #3464

Merged
caomengxuan666 merged 6 commits into
masterfrom
fix/3186-readonly-export-invalid-ident
Nov 13, 2026
Merged

caomengxuan666 merged 6 commits into
masterfrom
fix/3186-readonly-export-invalid-ident

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Refs-only draft PR for rubash #3186 (bug, P3-low). UNVERIFIED-BY-CAPTAIN.

根因

  • Executor::rewrite_declare_operand_subscripts (src/executor/declare_local.rs:520) 的 is_compound 门在 -a/-A 提示臂不查 LHS 合法性:无效名复合操作数也被当复合展开(字段打 \x10 + 元素加合成引号),随后内建 InvalidId 诊断直接引用带标记词。
  • valid_compound_assignment_lhs (src/parser/token_actions.rs:1762) 偏松:只要求全 alnum/_ 且含字母,不要求首字符为字母/_,故 9bad 被当合法复合 LHS 标记。
  • declare 'a[]=x' 走 InvalidId 分支,未按 GNU 报裸 a[]: bad array subscript。

GNU 锚点

  • builtins/setattr.def valid_identifier 检查 + builtins/common.c:208 sh_invalidid(\X': not a valid identifier);builtins/declare.def:613`(offset==0 先 valid_array_reference 再 valid_identifier;offset!=0 时 assignment() 已校验下标)。
  • arrayfunc.c:359 assign_array_element sublen<=1 -> err_badarraysub(error.c:440-443,裸诊断)。
  • parse.y:5648-5657 token_is_assignment + subst.c:12949 expand_declaration_argument:W_COMPASSIGN 只授给有效标识符 LHS。
  • GNU 探针(bash 5.3.20,脚本文件):非法复合形主语为普通展开后的整词(双引号内 $x 展开,单引号内原文);declare 'a[]=x'(含 -a/-A、+=、typeset、函数内 local)均为裸 a[]: bad array subscript、rc=1、后继操作数继续绑定;declare 'a[]'(无 =)、declare -p 'a[]=x'、export/readonly a[]=x 各守其原诊断。

修复(3 处,均不在护栏文件面)

  1. src/executor/declare_local.rs is_compound 门:提示臂加 LHS 合法条件(严格 valid_identifier)。
  2. src/parser/token_actions.rs:1762 收紧:首字符须 ASCII 字母或 _。
  3. src/builtins/declare.rs 验证环:base[] 空下标赋值形改报裸 a[]: bad array subscript(-p 面仍走 not found)。

本 PR 删除了什么

未删除任何公开 API、内建、测试或文档。仅收紧两处判定谓词并改一处诊断分支;钉文件为新增。

已知残差(待队长裁)

  • (a) 未加引号 declare -a 9bad=(x):GNU 为解析期 syntax error,修后为内建 InvalidId(同 rc=1、信息不同);钉锁定 InvalidId interim 行为。
  • GNU 在 declare 'a[]=x' 报错后仍创建空数组(declare -a a=());本修只修诊断,不建数组。
  • X299 Windows 面验证待补(跳板密码未存于本车道)。

验证(待补)

钉双边、三方探针、相邻茎、lib 双边、RUSTFLAGS="-D warnings" 全靶、hygiene + rustfmt、终头 CI 读数见 REPORT(~/workspace/fix-3186/REPORT.md,不入仓)。

…ipt diagnostic (#3186)

Refs-only: regression nail first, fix to follow. GNU anchors:
builtins/setattr.def valid_identifier (sh_invalidid, builtins/common.c:208);
builtins/declare.def:613 valid_array_reference/valid_identifier;
arrayfunc.c:359 assign_array_element sublen<=1 -> err_badarraysub
(error.c:440-443); parse.y:5648-5657 token_is_assignment +
subst.c:12949 expand_declaration_argument (W_COMPASSIGN only for
valid-identifier LHS).

UNVERIFIED-BY-CAPTAIN
…; declare 'a[]=x' reports bare bad array subscript (#3186)

GNU anchors: builtins/setattr.def valid_identifier (sh_invalidid,
builtins/common.c:208); builtins/declare.def:613; arrayfunc.c:359
assign_array_element sublen<=1 -> err_badarraysub (error.c:440-443);
parse.y:5648-5657 token_is_assignment + subst.c:12949
expand_declaration_argument (W_COMPASSIGN only for valid-identifier
LHS); general.c:288 valid_identifier (letter/_ first).

- executor/declare_local.rs: the -a/-A hint arms of the is_compound
  gate now require a strictly valid identifier LHS base, so an invalid
  compound operand takes the plain word path and the builtin's InvalidId
  diagnostic quotes the plain expanded word (double-quoted $x expands,
  single-quoted text stays verbatim) instead of the marker/quoted
  compound expansion.
- parser/token_actions.rs: valid_compound_assignment_lhs now requires
  an ASCII-letter/_ first character, so unquoted 9bad=(x) is no longer
  marked a compound assignment and reports builtin InvalidId.
- builtins/declare.rs: an assignment to an empty subscript (declare
  'a[]=x', also -a/-A, +=, typeset, local-in-function) reports the bare
  'a[]: bad array subscript'; -p stays 'not found', no-assignment
  'a[]' and export/readonly 'a[]=x' stay InvalidId.

Known residual: GNU also creates the empty array (declare -a a=())
after the bad-subscript failure; this change repairs the diagnostic
only. Unquoted 9bad=(x) is a GNU parse-time syntax error; rubash
reports builtin InvalidId (same rc=1), locked by the nail pending a
captain ruling.

UNVERIFIED-BY-CAPTAIN
cargo fmt --check flagged 10 hunks in
tests/issue3186_readonly_export_invalid_ident_compound.rs; formatting
only, no semantic change. Refs #3186.

UNVERIFIED-BY-CAPTAIN
@caomengxuan666

Copy link
Copy Markdown
Member Author

复核结论:FAIL(1 项返工,钉子本身;产品修法已验证通过)

产品修法在 Linux 与 Windows 双侧都已验证正确;钉子在 Windows 上不通过——
不是产品问题,是钉子自己的路径归一化在 Windows 下失效。返工只有 1 项,
机械改动,改完在 Windows head 上重跑钉子到 22/22 即可合入。

返工项(唯一)

  • 修 tests/issue3186_readonly_export_invalid_ident_compound.rs 里
    run_script 的 normalize(约 56–58 行):除 Rust 侧 C:\... 字符串外,
    还要替换 rubash 在 Windows 下打印的翻译后路径——stderr 诊断前缀里的
    /c/Users/... 形,以及 stdout 路径里的 C:/Users/... 形(或直接按
    ": line " 截断归一化)。改完后 Windows head 重跑钉子,期望 22/22。
    Linux 侧已是 22/22,改动不得破坏它。产品代码无需为此项改动。

钉双边(按测试名)

  • Linux(box):head 22 passed; 0 failed;base fail-first 5 passed; 17 failed
    (5 绿恰为守卫格;17 红均为预期基底行为:\x10 字段标记、\u{e31a}/\u{e309} PUA、合成引号、InvalidId 代替裸诊断)。
  • Windows(X299-2607):head 2 passed; 20 failed;base 1 passed; 21 failed。
    20 个 head 红全是同一原因:诊断文本在 line N: 之后与期望逐字节相同,
    只差脚本路径前缀——实际为 rubash 打印的 /c/Users/WWW/AppData/Local/Temp/...
    形,期望为 SCRIPT:;钉子的 normalize 只替换 Rust 侧 C:\... 字符串,
    替换从未命中。2 个 head 绿恰是不依赖路径前缀的断言(local ... 用 contains、
    valid_compound_still_binds 只断言 stdout)。base 侧 17 个是真诊断差异
    (fail-first 成立:base 印 InvalidId/marker 泄漏,head 印裸诊断/平词),
    另 4 个是前缀-only。产品行为在 Windows 上 22/22 正确(按诊断文本计)。

三方探针(GNU 5.3.20 oracle,27 形 s01–s27)

  • head vs GNU:25/27 逐字节同(Linux 与 Windows 两侧同一结论)。
    仅 2 处差,均为已裁残差:
    • s04 未加引号 declare -a 9bad=(x):GNU 解析期 syntax error(rc=2,整脚本夭折)vs 本修内建 InvalidId(rc=1)——残差 (a),钉锁定 interim,接受
    • s23 declare 'a[]=x' 后 declare -p a:GNU 建空数组 declare -a a=()(rc=0)vs 本修不建(declare: a: not found,rc=1)——残差 (e),真分歧但小,合入后另立单修
  • base vs GNU:15+ 处差,全为 marker 泄漏 + InvalidId。

lib 双边(Windows,bin 先建)

  • head 与 base 完全相同:1349 passed; 1 failed; 4 ignored,唯一红是
    api::consumer_tests::issue2084_session_end_restores_host_process_state
    (双侧同红,pre-existing)。**无 lib 回归。**另:bin 先建后 issue2875 通过,
    印证 CI 的 issue2875 红是 lib-only 运行的 VOID 格(见下)。

-D warnings 强制重查(Windows)

  • PR 自身代码干净:lib 与新钉目标均通过;唯一红是 pre-existing 死代码——
    head 在 tests/issue2302_...(5 项:RUBASH/Fixture/fixture/write_script/
    path_env, run, script_abs 从未使用),base 在 tests/issue2317_...
    (同类 5 项)。两个文件都不在本 PR 的 4 文件改动内。非本 PR 返工;
    建议另立单清理这两个测试文件的死代码。非空闲证明:head 137 次 rustc
    调用、base 70 次。

相邻茎(Windows head)

  • issue1317:1/1 通过。
  • issue2454:0/5——5 个全是同一路径归一化原因(诊断文本正确;其中 2 个还涉及
    WORK: 目录归一化,stdout 印 C:/... 形)。
  • issue2462:1/6——5 个全是同一原因(诊断文本正确)。
  • issue2700:1/2——1 红是环境因素:declare -p 全量清单里多出 TMPDIR
    (X299 测试环境继承而来),与本 PR 无关。
  • **无任何相邻茎出现诊断文本回归。**2454/2462 的红是钉子辅助函数的系统性
    Windows 盲点(与本钉同形),本 PR 不动它们;是否顺手修另议。

CI(终头 22fc31c,run 43309334881)

  • 3 红全是同一格:api::consumer_tests::issue2875_input_procsub_child_runs_as_rubash_shell
    (Linux/macOS/goldens 三 job,各仅此 1 红;特征 Unrecognized option: 'c' +
    consumer_tests.rs:2911)。master 最新 run(8a73f079)同 3 job 同格同数红——
    pre-existing,VOID 证据,与本 PR 无关。其余 job 全绿。CI 不跑新钉
    (CI 不跑新回归钉 stem:Rust tests 只跑精选 nextest 表 #3430,issue3186 在失败日志中出现 0 次),钉证据以本次复核为准。

卫生(head 树)

marker lint OK(117 consts / 268 sites)、utf8 clean、cargo fmt --check clean。

残差裁定

  • (a) 未加引号 9bad=(x) 解析期错误:接受为已记录的 interim(钉锁定),低优先级另立单。
  • (b) 多操作数续跑:已确认,关。
  • (c) 函数内 local 'a[]=x' 裸诊断:已确认,关。
  • (d) codegraph 不可用:rg 代行 + 三处改动点已全读,接受。
  • (e) 空数组创建副作用:真分歧,合入后另立单修(本 PR 不扩范围)。
  • (f) 自提交纪律冲突:记一笔,非代码问题。

与上一版复核的关系

上一版复核(ee80124a)只跑了 Linux 腿,给出 CONDITIONAL PASS;本次复核补跑了
Windows(X299)全套腿,钉子在 Windows 上 20/22 红是新证据,故结论改为 FAIL
(返工项见上)。产品修法本身在双侧均已验证,返工只动钉子。

完整卷宗:~/workspace/review-3464/CAPTAIN-VERIFY.md

Rework of PR #3464 per captain review FAIL: the pin's run_script
normalize only replaced the Rust-side native spelling (C:\...),
which never matches the translated paths rubash prints on Windows
(stderr /c/Users/..., stdout C:/Users/...). path_variants now covers
native + forward-slash + /c/... + /C/... spellings, script path first
(it contains the scratch dir as a prefix). Diagnostic-text
expectations untouched. On unix path_variants returns only the native
spelling, so Linux behavior is unchanged.

Windows head pin 22/22, base fail-first 5 passed / 17 failed with the
17 names matching the review dossier §4.2 exactly; rustfmt clean.
@caomengxuan666

Copy link
Copy Markdown
Member Author

复核结论:PASS(聚焦重审,第 13 轮,新头 5b5ee0f)

FAIL 的唯一项——钉子 normalize 在 Windows 下失效——已由双侧新证据清除:X299 Windows 头钉 22/22,box Linux 头钉 22/22(0.43 秒,22 格逐名 ok;钉二进制由新钉文件构建);Windows 基底 fail-first 5/17 与 §4.2 逐名一致。新头 GNU 探针 25/27 与 GNU 一致,仅有的 2 处差异是已立案的残差 (a)=#3495 与 (e)=#3494。新头 CI 仅红 issue2875 单格(已按名核对)。卫生三项新跑全绿;钉文件在 -D warnings 下干净。lib 双边与基底钉由已银行的同产品代码 runs + 身份证明承载——无任何结论依赖未跑的腿。PASS。

合入:先 update-branch(与活尖交集非空但 hunk 不重叠),再做合并形重验(钉 22/22 + lib 双边名集 ∅ + -D warnings)。合入后 #3487 与 #3494 可派。

@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 13, 2026 10:00
@caomengxuan666
caomengxuan666 merged commit 5219be9 into master Nov 13, 2026
8 of 11 checks passed
caomengxuan666 added a commit that referenced this pull request Nov 17, 2026
…yntax error (Refs #3495) (#3862)

* fix(lexer): unquoted digit-leading compound LHS is a GNU parse-time syntax error (Refs #3495)

GNU anchors: parse.y:5653 (compound branch gated on token_is_assignment)
-> parse.y:5270 token_is_assignment -> general.c:480 assignment() with the
legal_variable_starter gate (general.c:498-502); parse.y:1054 function-head
'(' shift names the next token for a bare '9bad=(x)'.

- lexer/word.rs: compound_assignment_start now requires an ASCII-letter/'_'
  first character, matching classification::is_assignment and
  token_actions::valid_compound_assignment_lhs (both already enforced it
  since #3464). '9bad=(x)' no longer folds into one word: the word ends at
  '9bad=' and the parser's stray-'(' arm reports 'syntax error near
  unexpected token `('`', rc 2, whole script dies.
- test: the #3464 nail that locked the InvalidId interim now expects the
  parse error; new tests/issue3495_digit_compound_parse_error.rs nails the
  class (declare/typeset/export/readonly/local, '+=', subscript, bare,
  echo-arg, eval containment) and the guards (quoted, paren-less scalar,
  valid names still bind).

UNVERIFIED-BY-CAPTAIN

* style(tests): rustfmt issue3495 digit-compound parse-error pins (Refs #3495)

---------

Co-authored-by: 曹梦轩 <[email protected]>
Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
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