Skip to content

WIP: $[...] verdict names the double-quoted segment (rubash#2227 face 1) - #2477

Merged
caomengxuan666 merged 4 commits into
masterfrom
fix/2227-bracket-extract-quote-state
Nov 1, 2026
Merged

caomengxuan666 merged 4 commits into
masterfrom
fix/2227-bracket-extract-quote-state

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

WIP: fix(expansion): $[...] verdict names the double-quoted segment (rubash#2227 face 1)

Refs #2227. Draft — do not merge (per task discipline). This is the "own brief" the existing tests/issue2227_bracket_quote_regressions.rs header called for: the face-1 completion the recovered face1-layers.diff attempt anticipated. The issue's three seed shapes were already byte-identical on master (landed via #2373/#2374/#2375/#2441); this PR closes the residual faces found by re-probing the whole class against WSL GNU Bash 5.3.20.

What still diverged on master (all reproduced byte-for-byte before this PR)

shape GNU 5.3.20 rubash master
x="a=$["1+1"] b" bad substitution: no closing `]' in a=$[ in a=$[1+1] b (whole RHS + invisible carriers)
declare q="a=$["1+1"] b" in a=$[ in q=a=$[ (name prefix)
same for export / local / readonly / a[0]= in a=$[ name-prefixed
echo pre"a=$["1+1"]post" in a=$[ in prea=$[
echo "a""b=$["1+1"]" in b=$[ in ab=$[
x="a=$['1+1']" '1+1': arithmetic syntax error ... (error token is "'1+1'") token 1+1 with leaked 0x16 carriers
x="a=$['a]b']" 'a]b' error token ] inside quotes closed the span early

GNU C-source basis (vendored tree, bash-5.3 patch 20 @9c465866)

  • subst.c:11687-11723 expand_word_internal case '"': the double-quoted segment is extracted by string_extract_double_quoted (subst.c:852) and handed to a recursive expand_word_internal(tword, Q_DOUBLE_QUOTES|…) — the segment is a new expansion-level string.
  • subst.c:944-1000 string_extract_double_quoted: its recursive passthrough covers only $( and ${ (944-985); it has no $[ case, and the first unescaped " ends the segment (989-1000).
  • subst.c:10912-10928 case '[': at that level, $[ → extract_arithmetic_subst (subst.c:1301) → extract_delimited_string(string, sindex, "$[", "[", "]", 0) (subst.c:1303).
  • subst.c:1361-1527 extract_delimited_string: quote-aware counting — backslash/CTLESC passes the next char (1419-1424), '[ / ` spans skipped whole via skip_single_quoted (1127) / skip_double_quoted (1014) (1488-1494), nested [ / $[ recurse (1448-1465).
  • subst.c:1500-1506 failure site: bad substitution: no closing ]' in +exp_jump_to_top_level(DISCARD)—` is the expansion-level string, i.e. the segment.

Layer boundary (why each layer counts or ignores quotes)

  1. Parse layer — parse.y:5527 → 4166/4186 parse_matched_pair(0,'[',']',P_ARITH): grouping-construct branch parse.y:4040-4050 consumes '/"/` spans whole (shellquote, class at syntax.h:32 shell_quote_chars "\"'"`), so brackets inside quotes never count there; backslash via LEX_PASSNEXT (3997). 老式算术 $[expr] 完全不展开(GNU 正常求值) #2217/WIP: rubash 2216+2217 — dollar-bracket arith in dquotes + expand_word result protection #2226 already aligned this.
  2. Segmentation layer — subst.c:852 string_extract_double_quoted: quote-blind to $[ — the first raw " after $[ cuts the string. This asymmetry is already modeled in rubash ([bug] $[...] containing the enclosing quote char inside double quotes prints literally instead of bad-substitution #2441, dquote_segment_end).
  3. Extraction layer — subst.c:1361: quote-aware bracket counting over the segment.

The layers intentionally disagree on where the ] is; the verdict string comes from layer 3's own level string (the segment), which is what this PR makes rubash name.

Changes (the four-site family, one rule everywhere)

The unified rule: on a segment-cut $[...] span, the cooked word keeps the segment's opening delimiter, and the walkers' verdict names the segment — GNU's expansion-level string.

  1. quotes.rs cookers (byte-cursor family): copy_dollar_bracket_body_raw_cursor / copy_dollar_bracket_arithmetic_cursor / remove_double_quoted_into_cursor now report the cut; the dequote walk's " and $" arms splice the opener back at the segment start (reopen_doomed_segment).
  2. quotes.rs cookers (char family): same reporting through copy_dollar_bracket_body_raw / copy_dollar_bracket_arithmetic / remove_double_quoted_into and its two callers.
  3. walkers (both collectors) — embedded_mutations.rs + embedded_parameters.rs verdict sites: pass the walker's unconsumed tail to dollar_bracket_no_closing_string, which now reconstructs the level string exactly (from after the last "/E302 opener through the failing $[, plus the collector-consumed body, stopping at the cut).
  4. collector carrier-awareness — collect_dollar_bracket_arithmetic_expansion: PROTECTED_ESCAPED_SQUOTE (0x16, the assignment-RHS channel's re-encoding of DATA_SQUOTE via expand_embedded_parameters_preserving_escaped_single_quotes) now arms as a single-quote unit and decodes to '; a source \' keeps its backslash and is passed by the backslash arm first, so escaped and plain quotes stay distinct.

src/lexer/continuation.rs untouched (captain-exclusive; not needed).

Verification

  • Probe battery (script files, LF) run through both MSYS_NO_PATHCONV=1 wsl bash <file> (GNU 5.3.20 at /usr/local/bin/bash) and the worktree debug build, stdout/stderr/rc compared byte-for-byte after path normalization: p2227a/c/d/e/f/g/h/j MATCH (seeds, assignment contexts, declare/export/local/readonly/a[0]=, glued pre-segment text, multi-segment words, whole-segment bodies, heredoc / $( ) / $(( )) interplay, nested $[, backtick bodies, single-quoted outer).
  • New regression tests: tests/issue2227_verdict_names_dquote_segment_regressions.rs — 7/7 pass, every expectation hand-measured against GNU 5.3.20.
  • Family regressions: issue2217 / issue2227_bracket_quote (unix-gated, 0 run locally) / issue2227_dollar_bracket_extraction / issue2374 / issue2375 / issue2441 / issue543 x2 — all pass.
  • cargo test --lib: 1123 passed / 2 failed — identical on clean master (env-bound issue2084_session_end…, shell_identity_is_used_for_default_dollar_zero).
  • Integration sweep (224 test binaries matching quote|arith|expansion|assign|dollar): only issue1184_f6 (2 tests) and issue2237 fail — identical on clean master (stash-verified; environment-bound, e.g. /dev/null globs on Windows).

Known remaining residuals (pre-existing, stash-verified identical on master, documented for follow-ups)

  1. echo "8|$[x\y]z]|"-shaped backquote EOF: message matches, rc differs (GNU 1, rubash 2). Minimal backquote/bracket EOF shapes match (rc 2 both) — the delta needs the earlier-errored-lines interplay. Same domain as the issue's face 2 (EOF line accounting).
  2. x=a$[\'1+1\'] (unquoted assignment, escaped quotes): the unquoted-RHS hoist encodes the second \' as ASSIGN_SQ_BACKSLASH+ASSIGN_HOISTED_SQUOTE (E30C+E307), which breaks extraction (bad substitution vs GNU's \'1+1\' error token). Root cause is the hoist channel's encoding, not the extraction machine — needs its own brief.

Process notes

  • The pre-commit rustfmt hook was bypassed with --no-verify only because cargo fmt --check fails environment-wide with os error 206 (rustfmt argv exceeds the Windows CreateProcess limit; reproduces in the main checkout too, and src/debugger/wiring_tests.rs on clean master is unformatted). The five committed files are proven formatted via direct rustfmt --edition 2021 --check (rc=0). Evidence in the task report.
  • git push was impossible: ~/.gitconfig pins a dead proxy (http://127.0.0.1:65532) and direct github.com:443 is unreachable from this network (api.github.com is reachable; ssh.github.com:443 works but the local key isn't registered and admin:public_key scope can't be granted headlessly). The branch was pushed byte-identically through the Git Data REST API (blobs → tree → commit beb3018c → ref), based on 1c93ea87 (the stale local master head, an ancestor of remote master; the 34 files remote master moved since do not overlap this PR).

caomengxuan666 and others added 4 commits November 1, 2026 22:43
…h#2227 face 1)

GNU expand_word_internal (subst.c:11692-11723) expands each
double-quoted segment (string_extract_double_quoted, subst.c:852,
which cuts at the first unescaped `"` — no `$[` passthrough at
subst.c:944) as its own recursive expansion level, so a `$[...]`
extraction that fails there (extract_arithmetic_subst subst.c:1301 ->
extract_delimited_string subst.c:1361, error site subst.c:1505) names
THE SEGMENT in `bad substitution: no closing `]' in <string>`, not the
word prefix. rubash named the whole cooked RHS, or the `q=`/`a[0]=`
prefix for assignment-shaped words, and leaked the assignment
channel's quote carriers into the string.

- lexer/quotes.rs (both cooker families, byte-cursor and char): when
  the #2441 cut fires (a `$[...]` span cut by the segment end), the
  segment's opening `"` is re-emitted at the segment start
  (reopen_doomed_segment) so the level boundary survives into the
  cooked word. Only doomed (verdict-bound) words carry it.
- executor/command_subst_helpers.rs (dollar_bracket_no_closing_string):
  reconstruct the level string from the walker's unconsumed tail —
  from after the last raw `"`/E302 opener through the failing `$[` plus
  the collector's consumed body, stopping at the cut.
- executor/embedded_mutations.rs collector: PROTECTED_ESCAPED_SQUOTE
  (0x16 — the assignment-RHS channel's re-encoding of DATA_SQUOTE)
  arms as a single-quote unit and decodes back to `'` (a source `\'`
  keeps its backslash and is passed by the backslash arm first).

Probes byte-identical to WSL GNU Bash 5.3.20 (stdout/stderr/rc) on the
issue's three seed shapes plus assignment/declare/export/local/
readonly/array-element words, glued pre-segment text, whole-segment
bodies, heredoc/$( )/$(( )) interplay. Remaining known residuals are
pre-existing and documented in the PR (backquote-EOF exit code; the
unquoted-assignment `\'` hoist encoding).

pre-commit hook note: `cargo fmt --check` fails environment-wide with
os error 206 (rustfmt argv over the Windows CreateProcess limit; also
fails in the main checkout, and src/debugger/wiring_tests.rs on clean
master is unformatted). The five files in this commit are proven
formatted via direct `rustfmt --edition 2021 --check` (rc=0); the
bypass is justified by the environment defect, not skipped formatting.

Refs #2227
…ub raw copiers bound $[ by extraction (rubash#2227 follow-up)

Three same-family residuals from cluster G's review of beb3018:

- w1: dollar_bracket_no_closing_string cut the verdict name at the
  LAST raw quote in the word prefix, so a quote inside a ${ } unit of
  the same segment posed as a segment opener
  (echo "a=${x:-"} "} $["1"] b" named `} $[`). GNU delimits the
  segment first (subst.c:1014 skip_double_quoted / subst.c:860
  string_extract_double_quoted skip $( ) / ${ } units whole; $[ is
  not special) and extract_delimited_string (subst.c:1361, error at
  subst.c:1505) names the whole level string. The name is now found
  by replaying that machine over the prefix: the segment still open
  at the failing span owns the name.
- n16: the segment's escaped quote reaches the cooked word as the
  DATA_DQUOTE carrier (0x18) and leaked into the name raw; GNU
  prints the resolved quote (a"b=$[). The reconstructed name now
  decodes the carrier.
- t_s3: the comsub-body raw copiers (copy_double_quoted_raw and its
  cursor form) bounded a $[...] span by plain bracket counting, so a
  ] inside the span's own quotes cut the copy, closed the enclosing
  quoted span early, and ended the $( ) body at the inner ) —
  x=$(echo "a=$["]"] b) c") reported unexpected EOF where GNU
  reports the span's bad substitution. Both raw paths now bound the
  span by dollar_bracket_body_end(_chars) (subst.c:1361 extraction);
  copied bytes unchanged, only the boundary moves; unterminated
  spans copy to end of input.

New pins in tests/issue2227_verdict_names_dquote_segment_regressions.rs
cover all three shapes (oracle: GNU bash 5.3.20, byte-for-byte).

Refs #2227. Follow-up to beb3018 (PR #2477); does not rewrite it.
…r the s2 line (rubash#2227)

issue2227_nested_and_subscript_controls expected `s2: 21` for
echo "s2: $[a["1"]+1]", but GNU 5.3.20 rejects that line itself:
inside double quotes the bare " after a[ closes the segment
(subst.c:1014 skip_double_quoted has no $[...] case), extraction
over the segment `s2: $[a[` finds no ] and the command is discarded
with `bad substitution: no closing ']' in s2: $[a[` (subst.c:1505)
while the script continues — stdout s1: 21 / n: 3 / bt: 6, rc 0.
The expectation predates the #2441 segment semantics (written for
#2373). The pin now asserts the GNU outcome; no product change.
Verified byte-for-byte against GNU 5.3.20 with the pin's own script.

Refs #2227.
@caomengxuan666

Copy link
Copy Markdown
Member Author

[win-acceptance] #2477 PASS — new pin 9/9 x2 @--test-threads=4; 23-shape probe byte-identical to WSL GNU Bash 5.3.20 (stdout+stderr+rc, 0 diff lines); family green (one pre-existing -j4 flake, see below).

Environment: Windows 10 19045 real machine, MSVC Rust debug build of master b3ae3b8 (tarball via gh api; github.com:443 unreachable for git fetch), GNU oracle = wsl bash 5.3.20 script-file comparison.

  1. New pin tests/issue2227_verdict_names_dquote_segment_regressions.rs (merged version = 9 test fns, grew from the 7 listed in the PR body by the cluster-G follow-ups): 9/9 passed twice at --test-threads=4 (overrides the repo's forced RUST_TEST_THREADS=1).
  2. Issue [bug] $[...] 提取不感知引号状态:外层双引号内含未转义引号的形状定界分歧(#2217 残差,既存) #2227 seed + PR-body shapes as one LF script (23 shapes incl. declare/export/local-in-fn/readonly/a[0]=, glued pre-segment text, "a""b= two-segment, '1+1'/'a]b' single-quote bodies, comsub raw-copier shape, unquoted controls, w1 ${x:-"} "} braced unit, escaped-quote carrier decode): stdout 0 diff lines, stderr 0 diff lines, rc both 0 — every verdict names the segment exactly as GNU.
  3. Family: issue2217 (0 tests, unix-gated), issue2227_bracket_quote (0 tests, unix-gated), issue2227_dollar_bracket_extraction 1/1, issue2374 1/1, issue2375 1/1, issue2441 6/6, issue543_eof_heredoc_cap 12/12, issue543_eof 14/14 serial.

Non-blocking finding (classified test-infra, NOT this PR's semantics): tests/issue543_dollar_bracket_eof_regressions.rs fails 4/14 deterministically under --test-threads=4 (passes 14/14 at -j1). Root cause is in the test harness itself: rubash_file() names its probe file by pid + script.len() and deletes it right after the run, so concurrent tests whose scripts have equal length race on one filename — evidence: two different tests both reported using rubash_i543_9544_11.sh, one got rc=127 "No such file or directory" for its own just-written file, another saw a different test's EOF diagnostic. File last changed 2026-10-31 (pre-#2477); serial runs — including the PR author's sweep and CI under the forced RUST_TEST_THREADS=1 — never see it. Suggested follow-up (not done here per acceptance-lane rules): make the scratch name unique (e.g. a static AtomicUsize counter) or drop the eager remove_file.

No BLOCKER.

@caomengxuan666
caomengxuan666 deleted the fix/2227-bracket-extract-quote-state branch November 2, 2026 01:08
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