Skip to content

fix(arith): trap set -u at GNU's read points, not a text pre-scan (Refs #3496 #3497 #3498) - #3561

Merged
caomengxuan666 merged 3 commits into
masterfrom
fix/3496-3497-3498-arith-nounset-read-points
Nov 17, 2026
Merged

caomengxuan666 merged 3 commits into
masterfrom
fix/3496-3497-3498-arith-nounset-read-points

Conversation

@caomengxuan666

@caomengxuan666 caomengxuan666 commented Nov 14, 2026 •

Copy link
Copy Markdown
Member

修了什么

set -u 下算术的 unbound 判定改由 GNU 的读点与展开相序决定,不再由文本预扫描决定。三处文本扫描各自误判:

  1. let 运算元 $ 形被 nounset 误捕:GNU 报算术语法错误,rubash 报 unbound variable #3496 — 入口文本扫描:(( )) / $(( )) / let / 下标写路径在求值前对展开后文本跑 arithmetic_unbound_variable,于是 let '$undef' 报 undef: unbound variable(GNU:let: $undef: arithmetic syntax error: operand expected),0 && i、0 ? i : 2、for ((;0 && n;)) 在 noeval 下误陷。删去这三处扫描,改由求值器自己的读点陷阱(rubash#3200 已有的 variable_value 读点)覆盖全部读:
    • 下标帧(eval_subscript_index)里的 UnboundVariable 原样传出(GNU 为 FORCE_EOF 跳出,不是下标 evalerror),(( b[i]++ )) 仍报 i;
    • 先查变量格(expr.c:1183-1190 array_variable_part 先于下标求值):不存在或 att_invisible(declare -a e = DECLARED_UNSET_VARS + 空镜像)报数组名;h=() 已赋空数组可见、读 0。
  2. 算术下标单引号内 $ 键被 nounset 误捕:GNU 报算术语法错误,rubash 报 unbound variable #3497 — 下标引号上下文:(( ))/$(( )) 文本含展开字符时,下标经 expand_array_subscript 在不带引号的上下文展开(单引号去引号、体为字面),b['$undef'] 交给 evalexp 的是字面 $undef → operand expected;let/[[ 仍走 array_expand_index 的双引号上下文(' 是数据,$undef 照展开);无展开字符时走 quote-removal 分支保留 '(b['1'] 报 '1')。新增 expand_arith_command_subscripts(命令入口)与 unquoted_subscript_single_quotes_as_data。
  3. eval 时裸名 nounset 报错误名:GNU 报 i,rubash 报下标 $undef #3498 — 词级预扫描相序:词级 nounset 预扫描遇 $((...)) 时就地裁决:先按展开相扫其 $ 读(下标按不带引号上下文),再在变量副本上用真求值器判裸名陷阱(保存/恢复全部算术 thread-local,只出判决),然后才走向后续词。echo $(( i++ )) + b[$undef] 报 i。

删了什么

  • 三处入口的 arithmetic_unbound_variable(&expression, ..) 文本扫描调用(src/executor/arithmetic/mod.rs 命令入口 / 下标写路径 / $(( )) 求值入口)。函数本身保留([[ ]] 操作数与 expand_word 整词 $(( 仍在用,见 Owed)。
  • 无公开 API 删除。

GNU 依据

  • expr.c:1150-1216 expr_streval(noeval 短路 :1164-1167;数组格查找 :1183;v == 0 || invisible_p (v) :1190);expr.c:1393-1401 readtok 对 = 目标不读
  • subst.c:11395-11405 / 11107 expand_array_subscript,subst.c:11147 expand_subscript_string (exp, quoted & ~(Q_ARITH|Q_DOUBLE_QUOTES))
  • arrayfunc.c:1370 array_expand_index → expand_arith_string (exp, Q_DOUBLE_QUOTES|Q_ARITH|Q_ARRAYSUB)
  • subst.c:3802 ARITH_EXP_CHAR,subst.c:3981-4033 expand_arith_string(展开分支 vs quote-removal 分支)
  • subst.c:11229 expand_word_internal(逐词左到右、$(( 就地展开求值)

验证

  • 钉 tests/issue3496_arith_nounset_read_points.rs(8 测,全部期望为 GNU 5.3.20 bash -c 实测逐字):base 2/8 → head 8/8(fail-first:base 上 6 红)。
  • GNU 对照矩阵(脚本文件双跑,target/probe/{m,n},共 52 格):SAME 格 base 31 → head 45(m 17→27,n 14→18),base SAME 的格在 head 全部仍 SAME(无新增 DIFF);改进格含 noeval ×5、let ×2、nn[0]=3、declare -a e ×2、h=()、local -a l、b['$undef']、$(( i++ )) + b[$undef]。另 target/probe/s 下标引号矩阵 19 格 head 18 SAME(剩 ${undef:-'$q'},见 算术 nounset 同族残差:[[ ]] 操作数文本扫描、预扫描不见 $((k=1)) 绑定、nameref/declare -i/元素赋值诊断 #3563)。
  • 相邻 stem(同族 16 个):arith_cache_fallback 7/7、issue1130 4/4、issue1149_1150 2/2、issue1709 4/4、issue200 2/2、issue2486 5/5、issue3024 23/23、issue3199 6/6、issue319 5/5、issue3200 11/11、issue3429 14/14、issue638 20/20;issue0067… 4/5、issue639 2/11、issue705_nounset 3/4、oils_spec_arith 57/68 —— 红格与 base 同名同数,失败全是本车道路径导致的 $0/路径前缀不等(/d/repo/rubash-lanes/L03/target/debug/rubash.exe: vs 期望 bash:),归一化前缀后左右逐字相等。
  • 扩展 194 个 arith/nounset/subscript/cond stem:60 红,逐条归一化后全为临时目录/$0 路径差(Windows 车道环境),另 issue1932/1933 为 /dev/fd 进程替换环境差;未见语义差。
  • cargo test --lib executor 461/461;cargo fmt --check(WSL,pre-commit 钩子实跑)绿。

Owed

Fixes #3496
Fixes #3497
Fixes #3498

caomengxuan666 and others added 2 commits November 14, 2026 13:18
#3496 #3497 #3498)

GNU raises the arithmetic nounset error only when evalexp READS a
variable (expr.c:1150-1216 expr_streval), after the text has been
expanded (subst.c:3981 expand_arith_string), and at the position the
word walk reaches the `$((...))` (subst.c:11229 expand_word_internal).
Three rubash text scans judged it instead:

- the (( ))/$(( ))/let/write-path entries ran arithmetic_unbound_variable
  over the expanded text, so `let '$undef'` reported `undef` (GNU: operand
  expected on `$undef`) and `0 && i` / `0 ? i : 2` trapped under noeval.
  The scan is removed; the evaluator's own read-point trap now covers
  every read: a subscript frame's UnboundVariable propagates out of
  eval_subscript_index (FORCE_EOF, not a subscript evalerror), and the
  variable cell is looked up first (expr.c:1183-1190: absent or
  att_invisible `declare -a e` traps the array name; `h=()` does not).
- (( ))/$(( )) subscripts are expanded unquoted (subst.c:11107/11147
  expand_array_subscript) when the text holds an expansion character:
  `b['$undef']` hands evalexp the literal `$undef`; `let` keeps the
  double-quoted array_expand_index pass (arrayfunc.c:1370).
- the word pre-scan now judges each `$((...))` in place: its expansion
  `$` reads, then a scratch evaluation's bare-name trap, before later
  words (`echo $(( i++ )) + b[$undef]` reports `i`).

Pin: tests/issue3496_arith_nounset_read_points.rs (8 tests; base 2/8,
head 8/8).

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Same content as af0a6d2 on another lane: origin/master carries this stem
unformatted, so `cargo fmt --check` (the pre-commit gate) fails on any
branch cut from it.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】s10 lane — 建议:PENDING(CONFLICTING,跳过不合)

head: 6229dec base(merge-base): 8b48c16 文件: 5 个 +549/-27
关联 issue: #3496 / #3497 / #3498(状态:均 open,我核过的时间:2026-11-16 20:15 CST)

处置:不合并。gh pr view 实时 mergeable=CONFLICTING / mergeStateStatus=DIRTY;对现尖 origin/master 12fb4e61 的 git merge-tree --write-tree rc=1,冲突文件:src/executor/parameter_errors.rs。

背景提示(供续作参考):

腿:本轮全部未跑(按 CONFLICTING 跳过规则),不记为通过。

未跑/VOID 的腿:

  • 钉自跑(issue3496_arith_nounset_read_points)/ fail-first / lib 双边 / -D warnings / 卫生 / GNU 对照 —— 全部因 CONFLICTING 跳过。

@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】
PR #3561 第2轮 — 建议:PENDING(head 本体机制/钉/腿已核合格;CONFLICTING 待解,合入被 master 冲突阻塞)
head: 6229dec(未变)base(merge-base): 8b48c16 文件: 5 个 +549/-27
关联 issue: #3496 / #3497 / #3498(状态:均 open,我核过的时间:2026-11-17 18:4x CST)

机制核查:

  • let 运算元 $ 形被 nounset 误捕:GNU 报算术语法错误,rubash 报 unbound variable #3496(读点 vs 文本扫描):GNU expr_streval 仅在 evalexp 真读变量时陷阱——noeval 短路(expr.c:1164-1167 if (noeval) return 0)、赋值目标不读(expr.c:1393-1401 peektok != EQ 才走 expr_streval)、v == 0 || invisible_p (v) 时 err_unboundvar(expr.c:1188-1199)。PR 删三处入口 arithmetic_unbound_variable 文本扫描、改由求值器 variable_value/lvalue_value 读点 + trap_unbound_variable_cell(value.rs,数组格先于下标求值,对应 expr.c:1183 array_variable_part)覆盖——方向与 GNU 一致。
  • 算术下标单引号内 $ 键被 nounset 误捕:GNU 报算术语法错误,rubash 报 unbound variable #3497(下标引号上下文):expand_array_subscript 内 expand_subscript_string (exp, quoted & ~(Q_ARITH|Q_DOUBLE_QUOTES))(subst.c:11147)= 不带引号上下文;array_expand_index 走 expand_arith_string (exp, Q_DOUBLE_QUOTES|Q_ARITH|Q_ARRAYSUB)(arrayfunc.c:1370)= 双引号上下文;expand_arith_string 无 ARITH_EXP_CHAR 时走 quote-removal 保留 '(subst.c:3981-4033)。PR 的 expand_arith_command_subscripts/unquoted_subscript_single_quotes_as_data/arith_expansion_char_present 与该三分支对应。
  • eval 时裸名 nounset 报错误名:GNU 报 i,rubash 报下标 $undef #3498(词走相序):expand_word_internal 逐词左到右、$(( 就地展开求值(subst.c:11229);PR 的 nounset_arith_expansion_verdict + arith_expansion_body_at(parameter_errors.rs)在预扫描就地裁决 $((body)) 的展开相 $ 读 + evalexp 裸名陷阱(真求值器跑变量副本、thread-local 存取恢复只出判决)——建模与 GNU 相序一致。
  • diff 可疑面:d11 stem 改动为纯 rustfmt 重排(commit style(tests) 单列,已披露);无桥接/翻译层;无关面改动无。

腿(逐项数字):

  • 钉自跑(head 6229dec,Windows):issue3496_arith_nounset_read_points → test result: ok. 8 passed; 0 failed;d11_pipeline_squote_carrier_regressions → test result: ok. 6 passed; 0 failed。
  • fail-first:base 8b48c16 上 issue3496_arith_nounset_read_points → 2 passed; 6 failed,红名 = {let_dollar_operand_is_a_syntax_error, noeval_short_circuits_never_trap, array_cell_lookup_precedes_the_element, single_quoted_subscript_dollar_is_data, other_subscript_contexts_keep_their_reads, earlier_arith_expansion_traps_first},绿名 = {real_reads_still_trap, walk_order_is_kept}——6 红覆盖三 issue 判别形,红因正是文本预扫描误判(如 let '$undef' 空 stdout、$(( i++ )) + b[$undef] 报 undef 而非 i)。d11 在 base 6/6 绿(格式化改动无语义)。
  • lib 双边:head 失败名集 {api::consumer_tests::issue2084_session_end_restores_host_process_state}(1350/1/4)/ base 同名集(1350/1/4)/ 差集 ∅(issue2084 = Windows 环境基线红)。
  • -D warnings:cargo check --lib 0 警告通过;--all-targets 在两个基线死代码处失败——tests/issue2298_*.rs 的 RUBASH/Fixture(该文件在 merge-base 与 head 逐字节相同)与本 stem d11_pipeline_squote_carrier_regressions.rs:32 的 RunOutcome.stderr(字段只写不读,base/head 间仅 rustfmt 折行差异,非本 PR 引入);issue3496 stem 单独 -D warnings 编译通过。
  • 卫生:rustfmt --edition 2021 --check 5 文件全 clean;marker-lint OK (117/268);utf8 hygiene clean。
  • CI(head 6229dec,run 43561337761):红 = GNU regression goldens and canaries、Rust tests、Rust tests (macOS)——逐一核为 issue2875_input_procsub_child_runs_as_rubash_shell(两 job 唯一失败名)+ goldens 基线,与 master 基线红同形;无本 PR 引入红。
  • GNU 对照:WSL wsl bash = GNU bash 5.3.20(1)-release;24 个 bash -c 形状(target/e3-probes/3561/gnu-cases.txt + run-gnu.sh)与钉期望逐字节一致(stdout/stderr 尾/退出码全对上,含 y: unbound variable、'1': operand expected、127/1/0 三档状态)。

F 点:无(代码与钉本体合格)。

阻塞(判 PENDING 的理由):

未跑/VOID 的腿:无。

@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】f1 续作(第3轮)— 建议:PASS(冲突已解,merge head 全腿绿;合入与关单口径留队长)

head: e714cb472be2a529d957a7d46ff64c130a619f75(6229decf + merge origin/master@a41909d3)
mergeable: MERGEABLE / UNSTABLE(gh 实时,2026-11-17 20:1x CST)
关联 issue: #3496 / #3497 / #3498(状态:均 open)

机制核查:

腿(逐项数字):

  • 钉自跑(merged head e714cb472,Windows):issue3496_arith_nounset_read_points → test result: ok. 8 passed; 0 failed;d11_pipeline_squote_carrier_regressions → test result: ok. 6 passed; 0 failed。
  • fail-first:master a41909d3 + 钉文件原样放入 → 2 passed; 6 failed,红名 = {array_cell_lookup_precedes_the_element, earlier_arith_expansion_traps_first, let_dollar_operand_is_a_syntax_error, noeval_short_circuits_never_trap, other_subscript_contexts_keep_their_reads, single_quoted_subscript_dollar_is_data},绿名 = {real_reads_still_trap, walk_order_is_kept} —— 与第2轮 base 8b48c164c 红集逐一相同;master 的 nounset 面重写未误修任何一格,钉非废钉。
  • lib 双边:head 失败名集 {api::consumer_tests::issue2084_session_end_restores_host_process_state}(1375/1/4)/ master a41909d3 同名集(1375/1/4)/ 差集 ∅(issue2084 = Windows 环境 flake,两侧同现)。
  • -D warnings:cargo check --lib --bins 0 警告;--test issue3496_arith_nounset_read_points --test d11_pipeline_squote_carrier_regressions 0 警告。
  • 卫生:rustfmt --edition 2021 --check 5 个 PR 文件全 clean;marker-lint OK (118 registry consts, 269 allowlisted sites);utf8-boundary clean。
  • CI:旧 head 6229decf 三红 = GNU regression goldens and canaries / Rust tests / Rust tests (macOS),第2轮已逐名核为 issue2875_input_procsub_child_runs_as_rubash_shell 基线;新 head e714cb472 CI 推送后另跑,红名单以新 run 为准。
  • GNU 对照:WSL /usr/local/bin/bash = GNU bash 5.3.20(1)-release;12 个形状(scratch-f1/gnu-cases/*.sh,覆盖 let 运算元 $ 形被 nounset 误捕:GNU 报算术语法错误,rubash 报 unbound variable #3496/算术下标单引号内 $ 键被 nounset 误捕:GNU 报算术语法错误,rubash 报 unbound variable #3497/eval 时裸名 nounset 报错误名:GNU 报 i,rubash 报下标 $undef #3498 三面判别格)经 scratch-f1/drive.sh 双侧 -c 执行,stdout / stderr(去 shell 名前缀)/ 退出码 12/12 逐字节一致。

F 点:无。
正文提示(沿用第2轮,未动正文):正文尾部 Fixes #3496 / Fixes #3497 / Fixes #3498 与 issue 同句——按 1.7 口径需队长确认是否指定关单;若非指定,合入将自动关三单,建议改 Refs。标题已用 Refs 规范。

续作记录:

  • e714cb472 Merge remote-tracking branch 'origin/master' —— parameter_errors.rs 两 hunk 双留解冲突;新 head: e714cb472be2a529d957a7d46ff64c130a619f75(已 push,gh 复核 MERGEABLE)。

未跑/VOID 的腿:无。

@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 17, 2026 12:26
@caomengxuan666
caomengxuan666 merged commit 3178afa into master Nov 17, 2026
10 of 12 checks passed
caomengxuan666 added a commit that referenced this pull request Nov 19, 2026
Refs #3563

Arithmetic `set -u` residuals from the #3561 review — seven shapes, one family rule: GNU traps nounset only where evalexp actually reads (expr.c:1190-1216 `expr_streval`), with the context each read site owns.

- Row 1 (`[[ 0 -eq '0 && u' ]]`, `$(( 0 && u ))` words): the text scan `arithmetic_unbound_variable` trapped names evalexp never reads (noeval short-circuit, assignment targets). Deleted with its last callers; `[[ ]]` arithcomp and whole-word `$(( ))` now take the evaluator's own category + recorded name.
- Rows 2-3 (`echo $(( k=1 )) $k`, `$(( h[k]=4 )) ${h[k]}`): the word pre-scan's `$(( ))` verdict discarded the scratch evaluation's writes. `arith_body_nounset_verdict` now returns them and the scan records them (per changed array element) in its simulated bindings, mirroring GNU's left-to-right walk (subst.c:11229). Round 2 fixed the remaining gap: the verdict env excluded SCAN_SIM_BINDINGS, so an arithmetic unit's scratch eval could not see an earlier bind in the same command (`scan_evolved_env_vars`, applied at both verdict call points).
- Row 4 (`(( b[${undef:-'$q'}] ))`): a subscript's `${...}` word inherits the subscript's unquoted context (subst.c:11147), so `'$q'` is literal data. The subscript masker recurses into braced bodies inside subscripts, and the subscript nounset scan masks the same spans; the top-level arithmetic context (where the quote is data and `$q` does read) is unchanged.
- Row 5 (`declare -n r=zz; $(( r[0] ))`): array lvalues now carry the written name alongside the resolved one; the nounset verdict reads the resolved cell but records the written name (expr.c:1194 `array_variable_name`).
- Row 6 (`declare -i di; di=undef+1`): the integer-assignment evaluation keeps the evaluator's category; an unbound read reports `undef: unbound variable` and takes the nounset FORCE_EOF rails instead of a synthesized operand-expected text (variables.c:2905 `make_variable_value`).
- Row 7 (`a[undef]=1` abort class): verified already conformant on current master; pinned as a guard.

Regression pins: `tests/issue3563_arith_nounset_residuals.rs` (fail-first on 622ee66: rows 1-6 red, row 7 green).

## 本 PR 删除了什么

- Deleted the `arithmetic_unbound_variable` text scan and its last callers (row 1: it trapped names evalexp never reads). No public API, no builtin, no user-visible behaviour other than the fixed nounset mis-traps was removed.

## Captain adjudication (head 5945fc5, run 44461379248)

- Regression pin stem census: 69 fail names, **zero new** vs the previous head's 71. The defect cell `arith_assignment_binds_for_later_words` (all three shapes: `k=1`, `set -u` arith-to-arith `j`, assoc `h[k]`) now PASSES; the whole stem is 11/11 PASS in CI. Guard cells `subscript_default_single_quoted_dollar_is_data` and `arith_compound_assignment_still_reads_first` PASS. `issue1902_stdin_bound_to_regular_file_follows_the_binding` remains red — documented-unstable fd-alias flake family, adjudicated, unreachable from this diff. The `issue2883` read-prompt cell flipped green (same flake family).
- GNU regression goldens: exactly the baseline 2 (`issue_753::case_9_declare_indexed_operand_binds_nothing`, `upstream_suite_slice_snapshots`), 232 passed.
- Compatibility CLI tests, Rust tests (Linux + macOS), Shell functionality tests, cross-target checks, Windows compile check, niubash-builds-against-tree, API ledger tripwire: all PASS.
- The two red job lamps (census, goldens) are the adjudicated-by-name failures above, not unreviewed breakage.
- Zero-intersection vs tip f37b90e proven programmatically: base 622ee66...tip = 6 commits / 15 files, none of them among this PR's 10 files — merged without update-branch.
- History: round-1 rework commits c768b9f + f25cd5b were committed and pushed by the captain from the lane's written, fmt-clean fix (disclosed on the PR); head 5945fc5 is the captain's paren-hygiene commit removing two `-D warnings` findings (PR-introduced, base 0).

## Verification debt (owed post-merge)

Fresh server legs for the final head (stem + lib failure-name sets both directions, `RUSTFLAGS="-D warnings"`, per-file rustfmt) were not run: the earlier server legs covered prior heads. Recorded as debt; the CI census by name above is the merge basis.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant