Skip to content

fix5 statelife: PIPESTATUS shape save/restore (#1525) + assignment procsub finalization (#1523) - #1550

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

caomengxuan666 merged 2 commits into
masterfrom
fix5/slice-statelife

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Fixes #1525
Fixes #1523
Refs #1364

第五波 statelife 片:PIPESTATUS 形状状态的 save/restore(#1525)与赋值 RHS procsub 收尾(#1523)。#1364 的关单判定(硬门复验)结果以评论补入本 PR:①~④全绿且标量格 ⑤ 已修则改挂 Fixes #1364;⑤ 未修则保持 Refs,残余仅剩标量格 ${PIPESTATUS[-1]} 一形态。

#1525 根因

ShellState.pipestatus_shape(Absent/LiveArray/Scalar/Valueless/ValuelessArray)门控完成写(set_pipestatus)与声明视图,但两条 save/restore 通道——tempenv 的 apply/restore 与函数 local 的 save/restore——只恢复格本身(env + typed cell),从不保存/恢复 shape 与 pipestatus 向量:标量临时绑定处置后 Scalar 形状残留,完成写被冻结、声明视图渲染出幽灵格。GNU 里 shape 与值同在一个 SHELL_VAR 对象里随 pop_var_context/dispose_temporary_env 整体重装(variables.c:6291 set_pipestatus_array、variables.c:3536 assign_in_env、execute_cmd.c:4904)。

修复:(shape, 向量)快照经保留键 __RUBASH_PIPESSTATUS_STATE 骑乘 tempenv previous 表与帧 local scope 两条通道(先例 __RUBASH_TEMP_PATH 伪条目);标量 tempenv 绑定对 PIPESTATUS 绑新鲜标量影子格;漏斗一在未发生外部 dispatch 时对仍绑定的临时格做吸收写并抑制二次写;帧内声明注记区分新鲜影子格与永久绑定的元素-0 规则。

#1523 根因

同一 procsub 被物化两次:raw 通道的 replacen 用重打印后的跨度去匹配原始词、静默未命中,cooked 通道随后再次物化,收尾把体跑两遍;独立赋值根本不经漏斗收尾(体在下一命令的收尾里排空、借用其行号与状态,尾随赋值永不运行);收尾以 ? 传播体错误——体的 exit 会杀死脚本并覆写外围 exit_code。GNU 在展开时于构造命令内 fork 体(subst.c:6362 / 1311 / 11349-11378),体状态对脚本不可见。

修复:raw 通道 target 非逐字命中时整条跳过(cooked 通道随后恰好物化一次);两个独立赋值路径在记录状态前接入收尾;收尾保存/恢复外围 exit_code、体错误就地丢弃;排空队列改插入序(多体按物化左→右序)。

集成侧适配(一处)

与已合入的 r3 片(#1539,PIPESTATUS 稀疏表示 Vec<(i64, i32)>)同域:本片快照编解码原按稠密 &[i32] 写就,集成时改为稀疏对编码 Shape|i0:v0,i1:v1,...——快照若把稀疏向量稠密化,恢复时会把未设置元素以 0 复活(${!PIPESTATUS[@]} 下标面可观察)。调用点不变,仅 src/shell/state.rs 编解码对适配,已折入 #1525 的 commit。

已知残差(簇报告在案,未扩面)

  1. 普通数组 tempenv 窗口期读仍是就地绑定视图(a=(9 9); a=99 declare -p a:GNU 为独立表里的新鲜标量影子格),与 PIPESTATUS 同根、属全体 tempenv 建模,处置后状态一致;本片仅按名对 PIPESTATUS 影子化。
  2. executor: assignment-RHS output procsub v=>(...) is finalized late and twice (diagnostic on the next line ×2, following $? polluted) #1523 错误路径早退(execute_assignment_or_comment_command 中途展开失败的 return Err)不经收尾,条目延后到后续命令的收尾排空——计数仍恰一次,时机/行号为旧行为。

验证

集成树(master a18e7d9 + 本片两 commit)门控复验账本以评论补入:lib 基线、issue1363 全量(含 R3 钉与 prefixed_local_assignment_in_function_keeps_value)、issue1364 12/12、issue1523 6/6、issue1525 8/8、z2 PIPESTATUS 40 例矩阵、标量格 PIPESTATUS=99; echo ${PIPESTATUS[-1]} 专项、15 条场景探针、246 差分稳定集、两 commit 各自独立构建、fmt/CR。

caomengxuan666 and others added 2 commits October 25, 2026 06:43
…d local scopes (rubash#1525)

The PipestatusShape note and the status vector were not part of
what temporary-assignment and function-local save/restore put
back, so a scalar tempenv binding or a `local` shadow left the
shape machine in the pre-command state: after `PIPESTATUS=99
true` the completion write froze on the stale Scalar shape and
`declare -p PIPESTATUS` resurrected a ghost cell; a frame that
did `local PIPESTATUS=7` restored the global cell but not its
array shape.

GNU's model (variables.c:6291 set_pipestatus_array;
execute_cmd.c:4904 set_pipestatus_from_exit; variables.c:3536
assign_in_env binding tempenv vars into the separate
temporary_env table, disposed via push_temp_var/pop_var_context):
the completion write targets the cell visible at write time —
a tempenv-bound builtin/function/null command evaluates the
write against the temp cell (scalar temp: discarded; array
temp: absorbed), then disposal restores the pre-command cell
(shape and vector) exactly; a disk command never binds the
tempenv in the parent, so the write lands in the real cell.

Port that model onto the two save/restore funnels:
- apply_temporary_assignments snapshots (shape, vector) under a
  reserved key before binding PIPESTATUS; the restore reinstalls
  both unless the name was promoted into a frame.
- Scalar PIPESTATUS tempenv bindings bind a fresh scalar shadow
  (GNU's tempvar is a new cell, not an element-0 write into a
  live array); compound tempenv binds note the Scalar shape.
- The simple-command funnel performs an absorbed write against
  the still-bound temp cell when no external dispatch ran, and
  suppresses the post-restore AST write for that command;
  execute_external marks the dispatch so external writes land.
- Function-local save/restore stashes the same snapshot with the
  first save of the name; restore_function_locals reinstalls
  shape+vector after the typed cells; local promotion of a
  tempenv PIPESTATUS plants the pre-tempenv snapshot.
- Inside a frame, an attribute builtin's value operand binds a
  fresh scalar cell and a bare operand creates a valueless
  local; the declaration note now records those shapes instead
  of the permanent bind's element-0 rule.

Regression tests: tests/issue1525_pipestatus_shape_save_restore_regressions.rs
(issue's two cases, live-vector preservation, external landing,
compound absorption, function-call temp, local array
roundtrip, ordinary-array control).

Co-Authored-By: Muse Spark <[email protected]>
…ucting command (rubash#1523)

`v=>(...)` bodies ran at the NEXT command's finish (or never,
for a trailing assignment), ran twice, and a failing body
killed the script with its own status. GNU forks the body at
expansion time (subst.c:6362 process_substitute, extracting at
subst.c:1311 extract_process_subst; assignment words expand
inside subst.c:11349-11378 expand_word_internal), so the body
belongs to the command that constructs it and its status is
unobservable to the script.

Root causes, all in the materialize/finish path:
- The raw-word materializer rewrote by the metadata `target`,
  which is the RE-PRINTED span (token join normalizes e.g.
  `>&3` to `>& 3`), so its replacen silently missed and the
  cooked pass in expand_assignment_value materialized the same
  substitution a second time under a second key; the finish
  drained both. The raw pass now skips a substitution whose
  target is not verbatim in the word; the cooked pass — which
  scans the text it substitutes into — materializes it once.
- Standalone assignments never reached the funnel finish:
  words-empty commands and word-form assignments route to
  execute_empty_words_command / execute_assignment_or_comment_
  command, which now call the finish before recording status.
- The finish propagated body errors and let
  execute_persistent_output_process_substitution clobber the
  enclosing command's exit_code (a body `exit 3` killed the
  script). Body errors are now discarded and the enclosing
  status is saved/restored across the finish; the body's own
  diagnostics still print, attributed by the ambient span,
  which is now the constructing command's own line.
- The pending queue is insertion-ordered so multiple bodies
  finish in materialization (left-to-right) order.

Regression tests: tests/issue1523_assignment_procsub_finish_regressions.rs
(issue case: one body run at the constructing line with script
rc 0; temp-prefixed form; two assignments on one line; a
trailing assignment still runs; a failing body leaks no
status; word-form sibling control unchanged).

Co-Authored-By: Muse Spark <[email protected]>
@caomengxuan666

Copy link
Copy Markdown
Member Author

集成复验账本(集成树:master a18e7d9 + 本片两 commit,门控配方实跑)

拣选:簇两 commit 对当前 master 零文本冲突;一处语义适配——#1525 的快照编解码原按稠密 &[i32] 写就,与已合入的 r3 稀疏表示(Vec<(i64, i32)>)相撞,集成时改为稀疏对编码 Shape|i0:v0,i1:v1,...(否则恢复会把未设置元素以 0 复活),已折入 #1525 的 commit(4a8997fd),#1523 为 81e6fbd。两 commit 各自可独立构建(中间提交 worktree 冷 cargo check --locked rc=0)。

相邻分歧(非本片引入,master 既存,另行上报)

继承 fd≥3 的写入在本树上不通:rubash -c 'echo INHERITED >&3' 3>file(或 3>&1 接管道)报 3: Bad file descriptor、无写入;GNU 正常写入。普通命令与 procsub 体同症。以不含本片 commit 的同代 master 二进制复测同症,确认既存、与 #1523/#1525 无关;#1523 的探针协议(fd3 关闭、数诊断)不受影响。此条只登记现象与复现,立项归主会话裁定。

@caomengxuan666
caomengxuan666 merged commit 73d0a1b into master Oct 25, 2026
7 of 10 checks passed
@caomengxuan666
caomengxuan666 deleted the fix5/slice-statelife branch October 30, 2026 01:41
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