Skip to content

fix5 statelife2: scalar-cell bad subscript (#1555) + array tempenv scalar shadow (#1556) - #1570

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix5/slice-statelife2
Oct 25, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix5/slice-statelife2

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Fixes #1555
Fixes #1556

第五波 statelife2 簇集成片:两条 PIPESTATUS/tempenv 残余修复,各一个 commit,自 statelife2 簇分支按序拣入当前 master(零冲突)。

#1555 — 标量格负下标报 bad array subscript

GNU 的是一般规则(arrayfunc.c:1483 array_value_internal):负下标 rebase(array_max_index + 1 + ind)仅当 array_p(var);标量格(VT_VARIABLE)永不 rebase,仍 <0 即 INDEX_ERROR → 打印 NAME: bad array subscript、读空。该检查在 var==0/无值格早退之前,故未 set、declare 无值的名字负下标同样报错;真数组负下标相对末尾(保持)。

修复按同一规则的四个入口对齐:

  • 值形(public_accessors.rs):pipestatus_element / pipestatus_subscript_resolves 对非 LiveArray 形状不再 dense fallback;Scalar 仅 [0] 读镜像文本,负下标永不 resolve,既有诊断臂开火。
  • 算子/模式/子串形(arrays/executor.rs + expand_braced_ops.rs):元素 fetcher 的标量腿自持诊断(GNU 在 fetch 内报,故 ${v[-1]:-DEF}、${v[-1]#9}、${v[-1]:0:1}、非字面下标都报);ops 整数臂删重复打印防双报;负下标按 unset 参与 -/:-/+ 判定。
  • 长度形(expand_braced_indices.rs):按 subst.c:7488 array_length_reference 重排——declare 无值 → 静默 0;标量基负下标 → 方括号诊断 + 整条命令丢弃;PIPESTATUS 长度改读 cell。
  • 算术形(arithmetic/value.rs + dynamic_arrays.rs):非数组基负下标不再 dense rebase,推 ArithEvalDiag::BadSubscript;ArithDynamicContext 新增 PIPESTATUS 标量形状快照裁决。

立项复现订正:issue 原文的 true | false; PIPESTATUS=99; echo "[${PIPESTATUS[-1]}]" 在 GNU 上输出 [0]——独立赋值命令自身的 completion 写回先把活数组装回,标量格在下一命令读不到。标量格可观测点只有 tempenv 窗口(PIPESTATUS=99 eval '...')与函数 local 影子(local PIPESTATUS=99),两处 GNU 均报错读空。bug 真实,复现形态按此修正,回归测试按可观测形态钉住。

#1556 — 普通数组 tempenv 窗口应见标量影子

GNU 模型 = variables.c:3536 assign_in_env:每个 tempenv 绑定都是在独立 temporary_env 表里新建的标量(仅 att_exported|att_tempvar),外层格从不写入;窗口读全见影子,处置后原状态天然完好。rubash 原状是把标量值就地写进数组格的元素 0,窗口读见被改的数组(declare -ax a=([0]="99" [1]="9")),assoc 还多出伪 [0] 键,只读拒绑后 -x 导出标记泄漏。

修复(temporary_assignments.rs,apply 循环内,#1525 的 PIPESTATUS 分支保持在前不动):

  1. 数组影子分支:有效目标的格为数组时直接换影子——标量 typed cell + 镜像文本 + 全新仅 exported 的 VarAttrs,previous 快照整体还原;append 按旧格标量视图(索引取元素 0、assoc 取空)拼;只读目标走纯 error_prefix 诊断且不再打导出标记;动态栈数组显式排除、保持旧路。
  2. 复合分支补窗口 attrs:绑字面标量文本时一并清数组标记(a=(7 8) declare -p a → declare -x a="(7 8)")。
  3. §8 strip 扩展(command_execute.rs):a[0]=5 b=2 declare -p b 中数组式词之后的简单赋值词原被当命令名(b=2: command not found);现剥除后按 set -k harvest 形态移入 assignments(恒留一词作命令名)。

验证账本(簇侧,集成树复验随后以评论补入)

  • 新回归:issue1555 12/12、issue1556 13/13(净化环境);探针矩阵 m1555 / m1556 与 GNU 逐字全等。
  • 硬门全保:issue1525 8/8、issue1523 6/6、issue1363 21/21、issue1364 12/12;tempenv/declare/export 相邻 18 套件全过;z2 矩阵同名口径 40/40;246 差分 244 PASS、失败集 {gl08, ms10} = 基线稳定集。
  • 红线:未碰 src/lexer/continuation.rs、未动载体字节、[perf] "${a[@]}" iteration is O(n²): 40k elements take 516s (GNU 0.24s) #1510 freshness 面未触;改动文件 fmt 干净、CR 0。

残余(已立项 / 已载明)

…script) (rubash#1555)

GNU arrayfunc.c:1483 array_value_internal rebases a negative
subscript (array_max_index + 1 + ind) only when array_p(var); a
scalar cell (subst.c get_var_and_type VT_VARIABLE) never rebases,
so every negative subscript is INDEX_ERROR (arrayfunc.c:1462,
err_badarraysub) and the read is empty/unset. The rule is general
(measured on GNU 5.3.20 for plain scalars, unset and valueless
names alike); a scalar PIPESTATUS cell — observable inside a
tempenv window or as a function-local shadow, since a standalone
PIPESTATUS=99 command's own completion write reinstalls the live
array — fell back to a dense view and returned the value.

- public_accessors: pipestatus_element/pipestatus_subscript_resolves
  no longer dense-fallback for non-LiveArray shapes; Scalar reads
  element 0 from the mirror, negatives never resolve.
- arrays/executor: the element fetcher's scalar leg now owns the
  diagnostic (GNU reports inside the fetch, so the operator,
  pattern and substring arms see it too); expand_braced_ops drops
  its duplicate report for the plain form.
- expand_braced_indices: ${#...} adjudicates via
  array_length_reference (subst.c:7488) — absent/invisible bases
  return 0 silently, scalar bases take the bracketed
  bad-subscript failure, and PIPESTATUS lengths read the cell
  instead of the empty env mirror.
- arithmetic: indexed reads no longer dense-rebase a non-array
  base and report ArithEvalDiag::BadSubscript; the dynamic
  context snapshots the scalar PIPESTATUS shape.

Regression: tests/issue1555_scalar_subscript_bad_subscript_regressions.rs
(12 tests, GNU 5.3.20 expectations).
…sh#1556)

GNU variables.c:3536 assign_in_env (from subst.c:13142
do_assignment_statements) creates every temporary-environment
binding as a fresh scalar in the separate temporary_env table
(make_new_variable + var_setvalue, att_exported|att_tempvar
only); the outer cell is never written. rubash routed a plain
bind over an array name through the permanent bind, which wrote
the value into element 0 in place: window reads saw a mutated
array (declare -ax a=([0]="99" [1]="9")) instead of the
shadow scalar, and assoc windows grew a bogus [0] key.

- apply_temporary_assignments: when the effective target's cell
  is an array (attribute word or typed cell), swap in the
  tempvar shadow directly — scalar typed cell + mirror text +
  fresh exported-only VarAttrs; the per-name snapshot reinstalls
  cell, mirror and attrs at restore. Append computes against
  the old cell's scalar view (make_variable_value ASS_APPEND:
  element 0 for indexed, empty for assoc). A readonly target
  reports with the plain error prefix and drops the word
  without the export-mark leak the fallthrough tail produced.
  Dynamic stack arrays (FUNCNAME/BASH_ARGV/...) keep the old
  path; PIPESTATUS keeps its #1525 branch.
- The compound tempenv branch now also resets the window attrs
  to the fresh tempvar word (it already bound the literal
  scalar text; the surviving array marks kept declare -p
  rendering it as an array).
- strip_invalid_env_assignment_prefixes: simple assignment
  words following a stripped array-style word move into the
  assignment list (the parser's classification stops at the
  array-style word, so a[0]=5 b=2 declare -p b dispatched b=2
  as the command name); GNU assign_in_env skips the invalid
  word and binds the rest.

Regression: tests/issue1556_array_tempenv_window_regressions.rs
(13 tests, GNU 5.3.20 expectations).
@caomengxuan666

Copy link
Copy Markdown
Member Author

集成树门控复验(master a4c6c2ad + b3f1e33f + d3cac717,净化环境剔除全部 JARVIS_*)

  • build --locked rc=0;两 commit 按序拣入零冲突(master 自 73d0a1b 的增量与本片文件面不重叠);首 commit([bug] 标量格 PIPESTATUS 负下标读不报 bad array subscript(VT_VARIABLE 规则) #1555)独立 worktree cargo check --locked rc=0。
  • lib 732 过 / 1 败 / 1 ignored,与当前 master 基线逐项一致;唯一失败 = ownership_unary_operators_check_existing_file(root 环境项)。
  • 新回归:issue1555 12/12、issue1556 13/13。
  • 硬门全保:issue1525 8/8、issue1523 6/6、issue1363 21/21、issue1364 12/12。
  • 头牌探针 5 形与 GNU 逐字一致:tempenv 窗口 PIPESTATUS=99 eval '...${PIPESTATUS[-1]}...'、函数 local 影子、普通标量 v=99 的值形/长度形、[bug] 普通数组 tempenv 窗口期读走就地绑定视图(全体 tempenv 建模) #1556 窗口 a=99 declare -p a 与复合 + §8 混合词形。
  • z2 矩阵同名口径 40/40。
  • 246 差分 244 PASS,失败集恰为 {gl08, ms10} = 基线稳定集(新沙箱、--target rubash、RUBASH 变量、results-rubash.json mtime 已核新鲜)。
  • fmt 改动文件全清、CR 0。署名作者/提交者双双核实为曹梦轩。

合并归主会话把关。

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