Repository navigation
fix(lexer): complete the fully-single-quoted for-word fast path (#629) - #633
Merged
Merged
Conversation
The fully-single-quoted for-list fast path in expand_for_word_values_result
stands in for the whole expansion walker, but returned a word the walker would
never produce. Two downstream finalisation steps were missing.
1. The quote-data carrier decode. The `'`-quoted interior carries DATA_DOLLAR
(0x1f, markers.rs:135), DATA_DQUOTE (0x18, markers.rs:89) and DATA_BACKTICK
(0x1a, markers.rs:95) for every expansion-trigger byte inside it
(lexer/quotes.rs:321/325/329), and the walker's last step decodes the whole
family (decode_word_position_carriers, markers.rs:378). The fast path
restored only the protected-dollar marker:
return Ok(vec![word.replace('\u{1f}', "$")]);
so the loop variable was bound with live 0x18/0x1a bytes, and the raw
compound element serialised them back through quote_array_value /
decode_ansi_c_quoted as `$'...\030...'`.
2. The word-prefix marker drop. A word that begins with a quoted tilde is
prefixed with QUOTED_WORD_PREFIX (0x1b, markers.rs:107) so that no later
tilde pass touches it; the walker drops it in expand_marked_or_special_word
(expand_word.rs:515). The fast path kept it, so `for v in '~'` bound
`\x1b~`.
GNU keeps no marker inside a single-quoted span: parse.y:3877 parse_matched_pair
reads the interior literally, only CTLESC/CTLNUL are protected (parse.y:5694
got_character), and subst.c:4807 dequote_string is the only removal. A quoted
tilde is literal too - the tilde is special only unquoted, at word start. So the
fast path must run the same whole-family decode and the same prefix drop: it
must return what the walker returns for this word.
Fix: strip QUOTED_WORD_PREFIX, then call decode_word_position_carriers(word).
Byte evidence (`printf %s "${a[0]}" | od -An -tx1`) for
`a=(); for v in 'he said "hi"'; do a+=("$v"); done`:
before: 68 65 20 73 61 69 64 20 18 68 69 18 (raw 0x18 carriers)
after : 68 65 20 73 61 69 64 20 22 68 69 22 == GNU bash
and for `for v in '~'; do printf %s "$v"`:
before: 1b 7e
after : 7e == GNU bash
Regression tests added to tests/array_compound_quote_regressions.rs: the
compound `=`, `+=`, `[i]=` and `+=([i]=)` forms, the backtick and dollar
carriers, the leading-tilde prefix (`'~'`, `'~user'`, `'~/`', `'~$x'`), the
non-prefix tilde (`'x~y'`), and the clean paths (`a[0]="$v"`, non-fully-quoted
for-words).
Gates: lib 584/584. Byte-exact differential against GNU over 880 generated
cases (for-word quoting shape x consumer, compared with od): branch 880/880,
master 821/880 - the 59 pre-existing divergences are all in this change's blast
radius and are now fixed, none introduced. A 16-case hand matrix is 16/16, and
the leading-tilde family (`'~'`, `'~x'`, `'~root'`) matches GNU where master
emitted the 0x1b prefix. Test-target failure sets vs master 2f40ddb, compared
serial (--test-threads=1): cli_tests 1 = 1 with no new failure;
array_compound_quote_regressions, issue316_for_list_glob_regressions,
issue316_quoted_head_glob_residue, marker_leak_golden and array_store_parity are
0 = 0; executor_tests is 1172 = 1172 apart from the nondeterministic
command_chaining::part_080 coproc family, whose membership moves between runs of
the same tree (three consecutive runs gave 1173, 1172 and 1174, each with a
different set of part_080 rows), and every flagged row passes in isolation on
both the branch and master - host-timing flakiness, not a regression.
Measured on Termux/Android aarch64 against the local bash 5.3.20; the repository
oracle is WSL bash 5.3.0, and the rule being applied is from the vendored 5.3
source (parse.y:3877, parse.y:5694, subst.c:4807) rather than from a version.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Closes #629.
The fully-single-quoted for-list fast path in
expand_for_word_values_result(src/executor/command_words.rs) stands in forthe whole expansion walker, but returned a word the walker would never produce.
Two downstream finalisation steps were missing.
1. The quote-data carrier decode. The
'-quoted interior carriesDATA_DOLLAR(0x1f,markers.rs:135),DATA_DQUOTE(0x18,markers.rs:89)and
DATA_BACKTICK(0x1a,markers.rs:95) for every expansion-trigger byteinside it (
lexer/quotes.rs:321/325/329), and the walker's last step decodesthe whole family (
decode_word_position_carriers,markers.rs:378). The fastpath restored only the protected-dollar marker:
so the loop variable was bound with live 0x18/0x1a bytes, and the raw compound
element serialised them back through
quote_array_value/decode_ansi_c_quotedas
$'...\030...'.2. The word-prefix marker drop. A word that begins with a quoted tilde is
prefixed with
QUOTED_WORD_PREFIX(0x1b,markers.rs:107) so that no latertilde pass touches it; the walker drops it in
expand_marked_or_special_word(
expand_word.rs:515). The fast path kept it, sofor v in '~'bound\x1b~(a second defect in the same contract, pre-existing on master).
Why (GNU C source is the spec)
GNU keeps no marker inside a single-quoted span:
parse.y:3877 parse_matched_pairreads the interior literally, only
CTLESC/CTLNULare protected(
parse.y:5694 got_character), andsubst.c:4807 dequote_stringis the onlyremoval. A quoted tilde is literal too — the tilde is special only unquoted, at
word start. So the fast path must run the same whole-family decode and the same
prefix drop: it must return what the walker returns for this word.
Fix
Strip
QUOTED_WORD_PREFIX, then calldecode_word_position_carriers(word).Verification
consumer:
=,+=,[i]=,+=([i]=),a[i]=, scalar, bare binding, with$/`/"/~/glob content) compared byte-for-byte withod.Branch 880/880; master 821/880 — the 59 divergences are all inside this
change's blast radius and are fixed here, none introduced.
'~','~x','~root') matches GNU where master emitted the 0x1b prefix.cargo test --lib: 584/584.2f40ddb, serial(
--test-threads=1):cli_tests1 vs 1;executor_tests1172 = 1172 withonly the nondeterministic
command_chaining::part_080rows flipping (eachpasses in isolation; the same family flips in the other direction between runs
of master itself);
array_compound_quote_regressions,issue316_for_list_glob_regressions,issue316_quoted_head_glob_residue,marker_leak_golden,array_store_parityall 0 = 0.Measured on Termux/Android aarch64 against the local bash 5.3.20; the repository
oracle is WSL bash 5.3.0, and the rule being applied is taken from the vendored
5.3 source (
parse.y:3877,parse.y:5694,subst.c:4807) rather than from aversion's behaviour.