Skip to content

Bind ${var=word} in case-subject and for-list expansion - #3077

Merged
caomengxuan666 merged 3 commits into
masterfrom
fix/3045-case-for-assignment-word
Nov 8, 2026
Merged

caomengxuan666 merged 3 commits into
masterfrom
fix/3045-case-for-assignment-word

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Refs #3045

UNVERIFIED-BY-CAPTAIN — captain verification pending; do not merge on lane evidence alone.

Root cause: the ${var=word} bind lives in the &mut apply pass (apply_parameter_assignment_expansions_in_word), which the case subject (expand_case_word) and the for word list (expand_for_word_values_impl) never ran — both rendered through the &self expand_word only, so the value came out right while the assignment was silently lost (modernish dies at harden.mm:337, 0/389).

GNU anchors: case subject via expand_word_leave_quoted (execute_cmd.c:3686); for list via expand_words_no_vars (execute_cmd.c:3014) -> expand_word_list_internal (subst.c:13219) -> shell_expand_word_list (subst.c:13062); both through parameter_brace_expand (subst.c:9777) case '=' (subst.c:10346) -> parameter_brace_expand_rhs (subst.c:7966) -> bind_variable.

Fix (4 parts, one mechanism — run the apply pass wherever a word-list word expands, once per word under one cross-pass scope):

  • shell_options.rs expand_case_word: apply pass before tilde/expand_word dispatch.
  • command_words.rs expand_for_word_values_impl: apply pass ONCE at the top, before every arm (the $@ / array-at early arms return before the brace path and lost affix binds on mixed words).
  • expand_word.rs expand_word: whole-word ${...} arm only when braced_parameter_spans_whole_word holds — the naive strip_prefix/strip_suffix installed a spurious site (word, 0) for multi-fragment words ending in }, breaking the [bug] :- sees later := assignment in the same word (eval-order leak) #1169 freeze for unquoted words everywhere.
  • parameter_words.rs scan_word_prefix_quote_state: track <(...)/>(...) like $(...) (GNU subst.c:6362 process_substitute forks, subst.c:6412) so procsub-body assignments never bind in the parent; consume the closing quote in the in-double arm (a "..." span inside a comsub body left in_double stuck and swallowed its closing ')').

Verification (full numbers in workdir REPORT.md): pin tests/issue3045_case_for_assignment_word.rs 24/24 on head (5p/19f fail-first on base a7d3176); lib 1202/0/4 head vs 1198/0/4 base (delta = 4 new scanner unit tests); 21 neighbouring stems 168/0; GNU differential probes probe-diff + probe2-13 all SAME except two known pre-existing residuals (P12 DQ-backslash case subject, P14 tilde case subject); RUSTFLAGS='-D warnings' cargo check clean; cargo fmt / UTF-8 hygiene clean; no CR.

本 PR 删除了什么:

  • 无删除。纯加法修复:4 个源文件 +192/−28 行(含注释与 4 个新增 lib 单元测试),新增回归钉文件 tests/issue3045_case_for_assignment_word.rs(24 格)。未删任何公开面、测试或行为分支;两处已知残余(P12/P14)未动,见 REPORT。

The ${var=word} assignment renders the right value in a case
subject and in a for word list, but the bind never persists: the
case subject (expand_case_word) and the for list
(expand_for_word_values_impl) render through the &self expand_word
only, while the bind lives in the &mut apply pass
(apply_parameter_assignment_expansions_in_word) that only the plain
argument and case-pattern paths run.

24 tests: case/for basic + colon forms, set -u re-read, the
modernish harden.mm:337 shape, left-to-right freeze, comsub-once,
mixed $@ / array-at affixes, procsub-body no-leak, comsub-quoted-
span follow-on, plus the already-working case-pattern / plain-argv
guards. Fail-first on base a7d3176: 5 passed / 19 failed.

Refs #3045
The ${var=word} bind lives in the &mut apply pass
(apply_parameter_assignment_expansions_in_word), which the case
subject and the for word list never ran: both rendered through the
&self expand_word only, so the value came out right while the
assignment was silently lost (modernish dies at harden.mm:337).
GNU runs the same bind in both positions — the case subject via
expand_word_leave_quoted (execute_cmd.c:3686), the for list via
expand_words_no_vars (execute_cmd.c:3014) ->
expand_word_list_internal (subst.c:13219) ->
shell_expand_word_list (subst.c:13062), all through
parameter_brace_expand (subst.c:9777) case '=' (subst.c:10346) ->
parameter_brace_expand_rhs (subst.c:7966) -> bind_variable.

Four parts, one mechanism (run the apply pass wherever a word-list
word expands, under one cross-pass scope per word):

* shell_options.rs (expand_case_word): run the apply pass under a
  fresh SubXpassFrame + WordCtxGuard before the tilde/expand_word
  dispatch, mirroring expand_command_word.
* command_words.rs (expand_for_word_values_impl): run the apply
  pass ONCE at the top, before every expansion arm. The $@ /
  array-at early arms return before the brace path and render
  literal affixes without any apply, so mixed words they claim
  (${v=mix}"$@", "${a[@]}"${v=mix}, ${v=mix}${a[@]})
  lost the bind; a second run in the brace path would re-freeze
  earlier fragments against the post-assignment value.
* expand_word.rs (expand_word): take the whole-word ${...} arm
  only when braced_parameter_spans_whole_word holds. The naive
  strip_prefix/strip_suffix paired the first ${ with the LAST
  `, installing a spurious fragment site (word, 0) for
  multi-fragment words ending in `}` (${a:-d}/${a:=x}); the
  cross-pass memos keyed at [0]/[1] then missed on the render, so
  the rubash#1169 freeze never fired for unquoted words (argv,
  case and for alike). Same pitfall as parameter_ops.rs:345-351.
* parameter_words.rs (scan_word_prefix_quote_state): track
  <(...)/>(...) spans on the frame stack like $(...). GNU
  subst.c:6362 process_substitute forks (subst.c:6412 make_child
  FORK_ASYNC|FORK_PROCSUB), so a ${...} in a procsub body must
  never bind in the parent — the case apply pass was about to
  extend the pre-existing argv leak into case subjects, and now
  fixes the argv leak too. Also consume the closing quote in the
  in-double arm: without the advance the same quote was
  re-processed by the unquoted arm, leaving in_double stuck true
  and swallowing the span's closing ')', so a fragment after a
  comsub body containing a "..." span read as inside_cs and
  lost its bind (case/for).

Verification (see REPORT in the workdir): pin 24/24 on head
(5p/19f on base); lib 1202/0/4 head vs 1198/0/4 base (delta is
exactly the 4 new scanner unit tests); 21 neighbouring stems
168/0; GNU differential probes probe-diff + probe2-13 all SAME
except the two known pre-existing residuals (P12 DQ-backslash
case subject, P14 tilde case subject); RUSTFLAGS='-D warnings'
cargo check clean; cargo fmt / UTF-8 hygiene clean; no CR.

Refs #3045
@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 8, 2026 05:34
@caomengxuan666
caomengxuan666 merged commit a516873 into master Nov 8, 2026
11 checks passed
caomengxuan666 added a commit that referenced this pull request Nov 8, 2026
…3082)

Refs #3027

test(expansion): pin same-word assign-order regressions

代码门已被 #3077 的整词臂重构 subsume:与 master a516873 合并解冲突后,expand_word.rs 与 master 逐字相同,本 PR 净落 11 格回归钉坐实 #3027 的修复面(同词后片段的 ${a=b} 绑定向前泄漏进 ${a:+b} 一族)。

验证:钉 11/0;fail-first 4p/7f 失败名集恰为 7 个泄漏格;lib 双边 1198/0/4;邻茎 10 茎 70/0;GNU 探针 7 形全 SAME;合并树双钉复验 issue3027 11/0 + issue3045 24/0。

## 本 PR 删除了什么

无删除。净 diff 仅新增 tests/issue3027_in_word_assign_order_regressions.rs(+146)。
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