Repository navigation
fix(executor): normalize assoc subscripts so ANSI-C spellings match variable lookups (#734) - #822
Conversation
…ariable lookups (#734) A key written through $'...' and the same key reached through another entry point landed in different cells, and the internal marker leaked into the stored representation: k=$'x\ty'; declare -A A; A[$'x\ty']=1 printf '[%s]\n' "${A[$k]}" # GNU: [1] rubash: [] declare -p A # GNU: [$'x\ty'] rubash: marker bytes in key declare -A B; B[$'a\\\\b']=1 # GNU key 61 5c 62 rubash key 61 62 Root cause. Both write and read funnel through the one expand_subscript_string pass (GNU subst.c:11063), but its output kept two decode products the finished key must not carry: 1. The ANSI_C_IFS_GUARD (U+E401) that escape_decoded_ansi_c_quotes puts before every decoded $'...' blank stayed in the resolved key. The guard-form key matched only another $'...' spelling; the variable and dq-literal spellings computed the clean bytes and missed the cell, declare -p rendered the marker inside the key, and the kvlist entry (which already strips) disagreed with the direct entry. 2. On the assignment entries the decoded backslash of $'a\\b' was then re-read as escape syntax by mask_subscript_escapes and dropped. GNU decodes the assignment word at parse time (parse.y:5551-5572 read_token_word $' arm: ansiexpand :5566 + sh_single_quote re-quote), so the subscript reaches expand_subscript_string (arrayfunc.c:392-398 assign_array_element_internal) with the backslash single-quoted and it survives. The ${A[...]} reader hands the raw spelling to the runtime decode without that protection, so GNU's own read of ${A[$'a\\b']} keys on ab and misses the a\b cell — the asymmetry is GNU behavior and is pinned as parity. Fix. expand_subscript_string strips the bare (E400-unescaped) guard from the resolved key — user data U+E401 rides the E400 pair and survives (rubash#735) — and the two assignment entries (prepare_array_element_assignment_word, the quoted-assignment arm in command_prepare) use expand_assignment_subscript_string, which doubles the decoded $'...' backslashes for the escape pass to collapse, the way GNU's parse-time quoting protects them. Verification: 25-row differential against GNU bash 5.3.0 (WSL, script files) — write/read cross-spelling matrix, stored-key bytes, declare -p rendering bytes, membership (-v and ${+x}), unset via variable and via raw ANSI-C operand, compound kvlist, user PUA U+E401, unrecognized escape — all rows byte-identical after the fix. New corpus in tests/issue734_assoc_ansic_subscript_regressions.rs (15 tests). Full cargo test --no-fail-fast vs baseline 7047baf^: zero new failures, zero flipped tests both directions (the 2 baseline-only failures are the known-flaky executor::path cache tests, green in isolation). Rebased over #788 (d248d9f, declare compound operand) — no overlap; declare/assign.rs untouched.
caomengxuan666
left a comment
There was a problem hiding this comment.
Independent review (cr-822 @ b92b6e6, separate worktree)
Verdict: APPROVE-WITH-NITS — fix is correct and well-anchored; no regressions; two nits below, neither blocking.
Verified independently (WSL GNU bash 5.3.0 oracle, own probes)
- Anchors check out: vendored
arrayfunc.c—akey = expand_subscript_string (sub, 0)at :393 (the 392-398assign_array_element_internalwindow);parse.y—ansiexpandat :5566 and thesh_single_quotere-quote at :5572 (5551-5572$'arm). The reader/assignment phase-asymmetry story matches the sources and the observed GNU behavior. - Differential re-run, 4 sampled rows + extras, all byte-identical: guard-form row (
${A[$k]}→[1]), item-3 key bytes (A[$'a\\b']→615c62),declare -prendering bytes (identical hex, noee9081),unset "A[$k]"→A=(). Extras: compoundA=([k]=v)anddeclare -A B=([k]=v)entries, kvlist alternation, user$'\uE401'(ee9081+[g], #735 E400-pair discipline holds),$'p\\q'(705c71), blank keys (" x "), trailing backslash (615c), mixed$'a\\$x'(615c2478), glob char (612a62), and the pinned reader asymmetry ([]/[1]/[1]). - Suites:
issue734_assoc_ansic_subscript_regressions15/15 green. Fullcargo test --no-fail-fast --workspacefailure sets on cr-822 vs baseline 7047baf are identical in my environment (41 env-dependent failures on both sides, zero flips in either direction);executor::path::tests72/72 green in isolation. - Hygiene:
cargo fmt --checkclean; single commit over 7047baf, 4 files, nothing extraneous; PROVENANCE GPL discipline respected (line-ref citations as traceability pointers, no GPL fixtures in MIT paths). The pinned GNU quirk is clearly labeled in the corpus (ansi_c_backslash_read_asymmetry_matches_gnu, "pinned here as parity") and inexpand_assignment_subscript_string's doc — if GNU ever fixes the asymmetry, the red diff will point straight at the decision.
Nit 1 — arithmetic assignment entry still mints raw-spelling keys (pre-existing; please annotate)
((A[$'x\\ty']=1)) and let are A[...]= assignment entries outside the two swapped entries, and they still store the undecoded spelling:
- GNU 5.3.0:
((N[$'x\\ty']=1))stores78 09 79;${N[$k]}withk=$'x\\ty'prints[1]. - rubash (cr-822 and baseline alike): stores
$'x\\ty'verbatim (24 27 78 5c 74 79 27), so the variable read prints[]— the ticket's item-1 symptom on a fourth write entry.
This is not a regression from this PR and the fix surface is right to keep narrow, but the PR body's out-of-scope list doesn't mention it while the ticket's own taxonomy calls this the "四入口各一份" family and the PR says "Closes #734 (items 1-3)". Suggest adding an out-of-scope bullet + follow-up ticket (the fix likely belongs in the arithmetic lvalue path, with its own differential — let "O[$'a\\\\b']=2" is additionally a GNU-inconsistent-operand row: GNU stores $a\\b, rubash $'a\\\\b').
Nit 2 — data point for the parked unset-operand follow-up
With the operand spelling that actually decodes to the stored key, GNU and rubash diverge (pre-existing, both sides of this PR):
declare -A A; A[$'x\\ty']=1
unset 'A[$'"'"'x\\ty'"'"']' # operand is literally A[$'x\\ty']GNU 5.3.0 keeps the cell (declare -p A=([$'x\\ty']="1" ) — unset does not ANSI-C-decode the operand); rubash removes it (A=()). The PR's r18 row covers the x\\\\ty spelling where both keep the cell, so this is just an extra row for the ticket the out-of-scope section already promises — no change requested here.
…(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).
fix(#822-engine): winsize bind writes both storages
bind_lines_columns inserted only into shell_state.env_vars (the mirror read by get_env / child-env composition) while $name expansion reads shell_state.variables (the record store) FIRST with a mirror fallthrough (variable_state.rs shell_variable_value). Once a LINES/COLUMNS record existed - parent env import at Executor::new (init.rs:949 -> VariableStore::from_environment) or one user assignment - the resize/startup bind left the record stuck at its old value while the child env tracked: shell-internal and child views diverged (niubash#822, Linux PTY probes A4/A8). GNU citation (vendored tree, bash.git @9c465866): - variables.c:1039-1055 sh_set_lines_and_columns: binds LINES (:1051) then COLUMNS (:1054) via bind_variable - ONE storage in GNU; rubash's variables/env_vars split is our implementation detail, and a bind writing only one of the two is the bug. - variables.c:3225 bind_variable -> :3054 bind_variable_internal: an existing entry takes the assign_value: arm (:3151); only the value cell is replaced (:3207 var_setvalue) so attributes survive, and exported_p triggers the export-env rebuild (:3214-3215) so an exported COLUMNS stays exported with the updated value. Fix: mirror set_env's dual-write (public_accessors.rs:839): after the mirror insert, update the record's scalar in place; absent records stay absent (the mirror fallthrough keeps serving them, matching GNU's non-exporting fresh bind); the G-01 process-env mirror is unchanged. bind_interactive_console_winsize and bind_interactive_screen_size ride the same path. WSL GNU Bash 5.3.x pins (evidence/822-dual/wsl/pin-output.txt): parent exports COLUMNS=200 in a 30x100 pty -> $COLUMNS=100, child env COLUMNS=100, declare -x COLUMNS="100"; resize -> tracks both ways; user assignment COLUMNS=55 then resize -> 120 then 80/24; exported assignment -> value tracks, stays exported. Engine tests (api/consumer_tests.rs): the two record-present shapes fail on base ($COLUMNS stuck at 200/55) and pass with the fix; a no-record regression guard pins the mirror fallthrough and that the bind creates no record and exports nothing. pywinpty ConPTY matrix (headless, evidence/822-dual/matrix): import-200 stuck at 200/55 across 100/120/80 columns pre-fix, tracks with the fix; fresh-shell case byte-identical before/after.
Closes #734 (items 1-3; item 4 lives with the quote-state family as noted in the ticket).
Root cause
Both the write (
A[sub]=v) and the read (${A[sub]}) funnel through the singleexpand_subscript_stringpass (GNUsubst.c:11063), but its output kept two decode products the finished key must not carry:escape_decoded_ansi_c_quotesprefixes every decoded$'...'blank with the guard; the resolved assoc key kept it. A guard-form key only matched another$'...'spelling, while the variable and dq-literal spellings computed the clean bytes and missed the cell — the ticket's item-1 repro (A[$'x\ty']=1then${A[$k]}→[]vs GNU[1]).declare -pthen rendered the marker inside the stored key (item 2:ee 90 81in the output bytes), and the compound-kvlist entry (which already strips) disagreed with the direct entry.$'a\\b'→a\\b, the escape pass re-read the decoded backslash as syntax and dropped it, storingab(item 3). GNU decodes the assignment word at parse time —parse.y:5551-5572 read_token_word()$'arm:ansiexpand(:5566) +sh_single_quotere-quote — so the subscript reachesexpand_subscript_string(arrayfunc.c:392-398 assign_array_element_internal) with the backslash single-quoted and it survives (GNU stores61 5c 62). The${A[...]}reader hands the raw spelling to the runtime decode without that protection, so GNU's own read of${A[$'a\\b']}keys onaband misses thea\\bcell — a write/read asymmetry that is GNU behavior, pinned as parity in the corpus (r08/r11).Fix (
src/executor/subscript_expansion.rs)expand_subscript_stringstrips the bare (E400-unescaped) guard from the resolved key. User data U+E401 rides the E400 pair and survives (rubash#735 discipline). All spellings now store and look up the same clean cell;declare -prenders GNU's[$'x\ty']form with no marker bytes.expand_assignment_subscript_string— used by the two assignment entries (prepare_array_element_assignment_word, the quoted-assignment arm incommand_prepare.rs) — doubles decoded$'...'backslashes so the escape pass collapses them back to one literal backslash, the way GNU's parse-time quoting protects them. Readers keep the eating semantics, matching GNU.GNU differential evidence (GNU bash 5.3.0, WSL, script files; all rows byte-identical after the fix)
A[$'x\ty']=1→${A[$k]}[1][][1]write → $'...'read[1][1][1]A[$k]=1→${A[$'x\ty']}[1][][1][1][][1][1][][1]780979780979*780979615c626162615c62${A[$'a\\b']}self-read[](GNU quirk)[1][]${A[a\\b]}bare read of r07 cell[1][1][1]${A['a\\b']}sq read of r07 cell[1][1][1]a\\bwrite → $'...'` read[][][]+a\b` writes["a\\\\b"][$'x\ty']declare -pkey bytes — item 2[$'x\ty']no markeree9081in key[$'x\ty']no marker${+x}/[[ -v ]]var+ansiunset "A[$k]"A=()A=()unset 'A[$'x\\ty']'(raw ANSI-C operand)A=([$'x\ty']=cv)→ var read[cv][cv][cv]615c62615c62615c62$'\\uE401'key (#735 interplay)ee9081+[g][]ee9081+[g]$'p\\q'(unrecognized escape) key705c71, self-read[]705c71, self-read[][]/[][]/[][]/[]* r06 iterated clean before only because the key-listing path strips markers at its own boundary; the stored key still carried them (visible to
declare -p, r14) and key matching used the guard form (r01/r03/r04/r05).Tests
tests/issue734_assoc_ansic_subscript_regressions.rs— 15 tests encoding the GNU-verified expectations above (storage bytes, cross-spelling reads, declare -p rendering bytes, membership, unset, kvlist agreement, #735 PUA interplay, unrecognized-escape parity).Suite impact
Full
cargo test --no-fail-fastvs baseline7047baf3: zero new failures, zero flipped tests in either direction. The only delta was 2 baseline-only failures in the known-flakyexecutor::pathcache tests (green in isolation). Rebased over #788 (d248d9f) — no file overlap.Out of scope (same family, different entry points)
printf -v/unsetANSI-C operand spellings: GNU 5.3.0 itself behaves erratically there (aprintf -v 'A[$'a\\b']'operand created a literal$a\\bkey on GNU); left untouched, worth its own ticket.declare b=($h)plain-array entry-point) belongs to the quote-state family, as the ticket notes.