Skip to content

fix(arithmetic): normalize assoc subscripts on the ((...))/let entry (rubash#828) - #839

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/828
Oct 19, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/828

Conversation

@caomengxuan666

@caomengxuan666 caomengxuan666 commented Oct 19, 2026 •

Copy link
Copy Markdown
Member

Closes #828 (the fourth write entry left out of #734/#822's two-entry scope, per the cr-822 Nit 1 annotation).

Root cause

Both arithmetic assignment entries — ((A[sub]=v)) and its let "A[sub]=v" form — expand the associative subscript through expand_assoc_subscript_once (arithmetic/mod.rs) before the parser sees it, and that pass never touched the literal $'...' spans: the RAW spelling (24 27 78 5c 74 79 27 for $'x\ty') became the stored key, so ${A[$k]} printed empty where GNU prints [1]. GNU reaches expand_subscript_string from this entry too, but the runtime pass behaves differently per caller, and the two let/arithmetic shapes sit on OPPOSITE sides of the line:

  • ((...)) / $((...)) / nested-subscript readers: the raw spelling reaches expand_subscript_string, whose expand_word_internal arm ANSI-C-decodes the span with the product quoted — decoded bytes ride as DATA and decoded backslashes survive (((D[$'a\\b']=2)) stores 61 5c 62). This is the same assignment-entry semantics fix(executor): normalize assoc subscripts so ANSI-C spellings match variable lookups (#734) #822 gave the two word entries; the arithmetic entries now share it (ArithAnsiSpanMode::Decode).
  • let: the operand was word-expanded once (let.def passes EXP_EXPANDED), and GNU does NOT re-recognize $' as ANSI-C there — the $ stays literal data, the span quotes drop as shell quotes and the raw spelling rides verbatim: let "B[$'x\ty']=1" stores the scrambled key $x\ty (24 78 5c 74 79), which only let's own reader hits. GNU's own self-scrambling quirk — pinned as parity (ArithAnsiSpanMode::LetOperand), per the ticket's "let 双引号形态 GNU 亦自乱……按 parity 固定".

Bare (non-$') spellings are deliberately untouched: the (( )) body reaches the evaluator after the lexer's own escape pass, so the bare-escape product stays as the assoc corpus rows recorded (re-processing it would eat one backslash too many).

Fix (src/executor/subscript_expansion.rs + callers)

transform_arith_ansi_c_spans rewrites each literal $'...' span of an arithmetic assoc subscript into carrier-marked data (no live \$/quote/backslash inside a span product; scanning mirrors decode_ansi_c_spans), per entry mode; expand_arithmetic_assoc_subscripts threads the mode from the ((``))/let flag (expr.c:340,368 EXP_EXPANDED), with the reader-family call sites ($(( )), nested subscript scan, substring offsets) hard-wired to Decode. The shopt -s array_expand_once AV_NOEXPAND verbatim branch is unchanged (GNU stores the spelling verbatim there too).

GNU differential evidence (GNU bash 5.3.0, WSL, script files; byte-verified 2026-10-13; all rows byte-identical after the fix)

row case GNU 5.3.0 rubash before rubash after
r01 ((A[$'x\ty']=1)) key bytes 780979 2427785c747927 780979
r02 r01 → ${A[$k]} [1] [] [1]
r03 r01 → $((A[$'x\\ty'])) 1 1 (raw self-hit) 1
r04 r01 → declare -p ([$'x\\ty']="1" ) (raw-spelling row) ([$'x\\ty']="1" )
r05 ((D[$'a\\\\b']=2)) key bytes 615c62 2427615c5c6227 615c62
r06 r05 → arith self-read / ${D[$'a\\\\b']} 2 / [] (pinned ${} asymmetry) 2 / [] 2 / []
r07 ((F[$'p\\q']=1)) key bytes + arith self-read 705c71 / 1 2427705c7127 / 1 705c71 / 1
r08 ((A[$'x\\ty']++)) 780979, ${A[$k]}=[1] raw, [] 780979, [1]
r09 (( z = A[$'x\\ty'] )) after clean write 1 1 (raw self-hit) 1
r10 var write A[$k]=1 → $((A[$'x\\ty'])) 1 0 1
r11 $'x\\ty' + dq-literal-tab writes via ((...)) one cell two cells one cell
r12 $'\\uE401' key via ((...)) (#735 interplay) ee9081 — ee9081
r13 let "B[$'x\\ty']=1" key bytes 24785c7479 ($x\\ty) 2427785c747927 24785c7479
r14 r13 → ${B[$k]} [] [] []
r15 r13 → let "v=B[$'x\\ty']" [1] [1] [1]
r16 r13 → declare -p ([\"\\$x\\\\ty\"]="1" ) quotes kept in key ([\"\\$x\\\\ty\"]="1" )
r17 let 'B[$'"'"'x\\ty'"'"']=1' (sq operand) 24785c7479 raw 24785c7479
r18 let "O[$'a\\\\b']=2" (CR Nit 1 row) 24615c62 ($a\\b) 2427615c6227 24615c62
r19 let 'O2[$'"'"'a\\\\b'"'"']=2' (both backslashes kept) 24615c5c62 raw 24615c5c62
r20 text around the span (M[$'x\\ty'post], P[pre$'x\\ty']) 24785c7479706f7374 / 70726524785c7479 quotes kept same as GNU
r21 let "J[$k]=9" / let "K[x<TAB>y]=7" (guard) 780979 / 780979 780979 / 780979 780979 / 780979
r22 let-read of a clean cell; $(( )) read of a let-scrambled cell 0 / 0 1 / 1 0 / 0
r23 membership after arith write (${I[$k]+x} / [[ -v ]] / ${I[$'x\\ty']+x}) set/set/set unset/unset/unset set/set/set
r24 unset "A[$k]" on an arith-written cell A=() cell survived A=()
r25 shopt -s array_expand_once + let / + ((...)) verbatim raw / 780979 verbatim raw / raw verbatim raw / 780979

CR Nit 2 data point (pinned divergence, deliberately not fixed here): unset 'A[$'"'"'x\\ty'"'"']' after A[$'x\\ty']=1 (any write entry) — GNU's unset does not ANSI-C-decode the operand and KEEPS the cell (declare -A A=([$'x\\ty']="1" )); rubash decodes it and DELETES (A=()). Pre-existing on master, both sides of this PR; pinned in the new suite (unset_ansi_c_operand_spelling_divergence_pinned) as the documented divergence for the parked unset-operand follow-up ticket.

Other pre-existing divergences left untouched (out of scope, noted for the record): bare escapes inside arithmetic subscripts keep one more backslash than GNU (((G[a\\\\b]=2)) second cell x\\ty vs xty); the indexed-array path's error text for (( y = undeclared[$'x\\ty'] )); the issue-734 catalogue row still reads diverges though its case now matches on master (stale row, pre-existing — PROMOTE pending, unrelated to this fix).

Tests

  • tests/issue828_arith_let_assoc_ansic_subscript_regressions.rs — 22 tests: the ticket's headline rows, the CR table rows (r13-r19), storage bytes, cross-entry reads, ++/command-read forms, declare -p rendering, membership, unset controls, the Nit 2 pinned divergence, and the array_expand_once boundary. All expectations byte-verified against the GNU oracle.
  • scripts/regression-catalog/cases/issue-828.sh + manifest.tsv row (must-match) — the case body is byte-identical between the GNU oracle and rubash.

Suite impact

Full cargo test --no-fail-fast on this branch (ee8cf9b, rebased over master 992d5c5) vs baseline 992d5c5, run side by side: failure sets identical (41 vs 43 raw rows — all 8 differing tests green in isolation on BOTH sides; they are the known env/concurrency-flaky executor::path/wait_n/fixture families that flaked because the two full suites ran concurrently on one machine, same shape as the #822 suite-impact note). Zero new failures, zero flips in either direction. cargo fmt --check clean; scripts/check-utf8-boundary-hygiene.sh clean.

GPL

Behavior implemented from WSL GNU bash 5.3.0 probes; the vendored submodule was consulted for semantics only. Citations in comments are traceability pointers (PROVENANCE §4).

@caomengxuan666

Copy link
Copy Markdown
Member Author

CR verdict: REQUEST-CHANGES (1 blocker; independent review of ee8cf9b vs 992d5c5, GNU oracle = WSL bash 5.3.0, own worktree + own binary builds)

Verified good

  • Headline rows re-probed on GNU and reproduced by the branch binary (master stored the raw spelling on every one): ((A[$'x\ty']=1)) → 78 09 79, ((F[$'p\\q']=1)) → 70 5c 71, let "B[$'x\ty']=1" → 24 78 5c 74 79, let 'O2[$'"'"'a\\b'"'"']=2' → 24 61 5c 5c 62, cross-entry misses 0/0, nested default ((E[${x:-$'a\tb'}]=1)) → 61 09 62, ((A[$k]+=1)) → 61 09 62, ((E6[$'x\ty']+=1)) → 78 09 79. All fixed as claimed.
  • Mode polarity correct: expand=true is the (( label (arithmetic/mod.rs:325), expand=false the let label (:377); LetOperand is reachable only from the let entry; $(( )) (parameter_core.rs:808), the nested-subscript reader (mod.rs:729) and substring offsets are hardwired Decode; the AV_NOEXPAND branch is untouched.
  • expr.c:340,368 coordinate check passes: both lines are the already_expanded branches (expr_bind_variable / expr_skipsubscript); let.def:102,125 pass EXP_EXPANDED. Semantic citation accurate; no C fragments in the diff.
  • 22/22 new tests pass; catalog issue-828 = must-match match; full-catalog diverging set identical branch vs master (same 11 cases both sides); cargo fmt --check clean; check-utf8-boundary-hygiene clean; zero compiler warnings; merge-tree vs origin/master = 0 conflicts (master has not advanced past the base, so no fix(executor): normalize assoc subscripts so ANSI-C spellings match variable lookups (#734) #822/fix(#802/#804): interactive line-editor carrier panic + GNU rbash restricted-mode direction semantics #823 overlap risk).
  • LetOperand handles the scrambled-quote siblings: let "B['a$'b'c']=1" stores a$bc = GNU (master kept quotes).
  • Hygiene: 6-file diff matches the commit message, no process-kill identifiers, no CHANGELOG entry (matches fix(executor): normalize assoc subscripts so ANSI-C spellings match variable lookups (#734) #822 precedent).

BLOCKER

1. New GNU-parity regression on the very entry this PR fixes: a $'...' span inside double quotes is decoded where GNU keeps it literal.

((E3["pre$'x'post"]=1)) — GNU 5.3.0 and master both store pre$'x'post (70 72 65 24 27 78 27 70 6f 73 74); this branch stores prexpost (70 72 65 78 70 6f 73 74). ANSI-C spans are not recognized inside double quotes, but transform_arith_ansi_c_spans detects $' with no quote-context tracking. Note the #822 sibling decode_ansi_c_spans tracks in_single and fires only outside single quotes — so the "Scanning mirrors decode_ansi_c_spans" doc claim does not hold today.

Same root, second symptom: ((E1['a$'b'c']=1)) — GNU and rubash's own assignment entry (E1assign, both master and branch) store a$bc (61 24 62 63); master's arith entry kept 'a$b'c' (divergent but byte-preserving); this branch stores abc (61 62 63), dropping the $ byte — the $' match at the close of the single-quoted span swallows the closing quote's $.

Fix: track single- and double-quote state in transform_arith_ansi_c_spans (skip span detection inside either, mirroring/extending decode_ansi_c_spans). That restores master behavior on both rows — E3 back to parity, E1 back to the known master shape — and makes the mirror claim true. Please pin both rows as regression tests; neither spelling is in the 22-case suite or the catalog case, which is why the bidirectional comparison could not see them. (Full parity for E1 would additionally need quote removal in this path's single-round expansion — out of scope, same bucket as the documented bare-escape divergence.)

Non-blocking

  • The assignment entry shares the dquote half of this blind spot pre-existing via fix(executor): normalize assoc subscripts so ANSI-C spellings match variable lookups (#734) #822's decode_ansi_c_spans (E3b["pre$'x'post"]=1 stores prexpost on master too). Worth a follow-up ticket on that scanner; do not widen this PR.
  • Full-suite bidirectional comparison (no-fail-fast, -t1, both binaries): zero branch-new failures; the touched-surface suites strictly improve (one binary: master 24 failed → branch 13). The 16 counter-flips in my master baseline are environmental (git-less archive copy in /tmp lacks third_party submodule content; those modules are untouched by this PR and pass on the branch).

No merge until the blocker lands; happy to re-review the quote-tracking delta alone.

…(rubash#828)

The fourth write entry — arithmetic ((...)) assignments and their let
"A[...]=v" form — stored the raw $'...' spelling instead of the decoded
key: ((A[$'x\ty']=1)) kept 24 27 78 5c 74 79 27 where GNU bash 5.3.0
stores 780979 and ${A[$k]} reads [1].

The assoc subscript of an arithmetic expression is expanded once by
expand_arithmetic_assoc_subscripts (arithmetic/mod.rs) before the parser
sees it, and that pass never touched the ANSI-C spans. GNU reaches
expand_subscript_string from this entry with the raw spelling, and the
runtime pass behaves differently per caller:

- ((...))/$((...))/nested-subscript readers: expand_word_internal's
  ANSI-C arm decodes the span with the product quoted, so decoded bytes
  ride as data and decoded backslashes survive (((D[$'a\\b']=2))
  stores 61 5c 62; the ${} reader keeps its pinned write/read
  asymmetry).
- let: the operand was word-expanded once (let.def passes EXP_EXPANDED),
  so $' is NOT re-recognized as ANSI-C — the $ stays literal data, the
  span quotes drop as shell quotes and the raw spelling rides verbatim
  (let "B[$'x\ty']=1" stores $x\ty = 24 78 5c 74 79). GNU's own
  scrambled-key quirk, pinned as parity.

expand_assoc_subscript_once now rewrites literal $'...' spans per entry
(ArithAnsiSpanMode::Decode / ::LetOperand — transform_arith_ansi_c_spans,
carrier-marked so the embedded walker expands only the text outside the
spans). Bare (non-$') spellings are deliberately untouched: the (( ))
body reaches the evaluator after the lexer's own escape pass, so the
bare-escape product stays as recorded in assoc corpus rows.

Known divergences that stay (probed 2026-10-13, pre-existing on master):
- bare escapes inside arithmetic subscripts keep one more backslash than
  GNU (((G[a\\b]=2)) second cell: x\ty vs xty);
- the unset operand that decodes to a stored key still deletes where GNU
  keeps the cell (CR Nit 2 — pinned in the new suite as the documented
  divergence for the parked unset-operand follow-up).

Tests: tests/issue828_arith_let_assoc_ansic_subscript_regressions.rs —
22 tests encoding the GNU-verified rows (storage bytes, cross-entry
reads, ++/command-read forms, let dq/sq operand forms, declare -p
rendering, membership, unset, array_expand_once boundary). GPL
discipline: behavior implemented from WSL GNU 5.3.0 probes; vendored
source used as semantic reference only.

cr-839 round 1 (BLOCKER): the span scanner now tracks single/double quote
state (mirroring #822 decode_ansi_c_spans� in_single discipline and
extending it with in_double — arithmetic subscript text carries live
dollar-quote-blind double-quoted segments). A $�...� span only opens
outside both quotes: ((E3["pre$�x�post"]=1)) stores the raw spelling
pre$�x�post (70 72 65 24 27 78 27 70 6f 73 74, GNU/master parity — was
prexpost), and �a$�b�c� sits back on the master-known raw shape
�27 61 24 27 62 27 63 27� (was abc, losing the dollar bytes). Three new
pins (both CR shapes + the let-entry forms through the shared scanner)
and an E3 row in the issue-828 catalogue case. Serial
(--test-threads=1) full-suite bidirectional comparison vs master
992d5c5: failure sets identical (38 vs 40; outliers are the known
signal-timing/path-cache env flakes, green in isolation on both sides —
issue526 fails 2/7 identically on master and this branch).
@caomengxuan666

caomengxuan666 commented Oct 19, 2026 •

Copy link
Copy Markdown
Member Author

CR round 1 addressed on the new head 540c933 (force-pushed over ee8cf9b).

BLOCKER fixed: transform_arith_ansi_c_spans now tracks single/double quote state (mirrors #822's in_single discipline, extended with in_double — arithmetic subscript text carries live dquoted segments the assignment-word entries never see). A $'...' span only opens outside both quotes. Verified against the oracle:

  • ((E3["pre$$'x'post"]=1)) → 70726524277827706f7374 (pre$$'x'post) — GNU/master parity restored (was prexpost).
  • ((E1['a$$'b'c']=1)) → 2761242762276327 — back on the master-known raw shape (was abc). The pre-existing divergence from GNU's assignment-entry ``a$$bc'' (61246263) is annotated in the pin; let-entry shapes (`L1`/`L2`) probed identical on GNU/master/branch and pinned too.

Pins added: 3 new tests (dq_quoted_dollar_quote_stays_data, sq_close_boundary_dollar_quote_not_a_span, let_entry_quote_state_shapes_unchanged) — the suite is now 25/25 — plus an E3 row appended to the issue-828 catalogue case (GNU-vs-rubash byte-identical). E1 itself is not in the must-match catalogue case because it diverges from GNU (pre-existing, master-same-shape); its coverage is the annotated Rust pin.

Out-of-scope note (E3b, the #822 scanner's dquote blind spot on master): untouched here per your triage — confirmed pre-existing on 992d5c5, left for your ticket.

Re-verification on the new head: the full probe battery re-run against the WSL GNU 5.3.0 oracle — all previously-green rows still byte-identical; the only remaining diffs are the documented families (bare-escape r08/r09/r11, unset-operand u1/u2/u3, the a9 lexer shape identical to master). Serial --test-threads=1 full-suite bidirectional comparison vs master 992d5c5: failure sets identical (38 vs 40; the 4 outliers are the known signal-timing/path-cache env flakes — issue526 fails 2/7 identically on master and this branch in isolation, path-cache green in isolation). cargo fmt --check + UTF-8 hygiene clean.

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.

arithmetic/let assignment entry ((A[$'x\ty']=1)) stores raw-spelling key: fourth subscript write entry misses the #734 normalization

1 participant