Skip to content

fix(executor): scalar-vs-array verdict and malformed-expansion diagnostics (#755, #758) - #785

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/755-758-parameter-expansion
Oct 17, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/755-758-parameter-expansion

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Fixes #755 and #758. The working branch was committed by a session that ended before it built, pushed or opened the PR; I completed the loop (build, byte-comparison against GNU, both new test files, push) and verified the behaviour before opening this.

#755 — a scalar whose text looks like a parenthesised list was re-parsed as an array

probe GNU bash 5.3.0 master this PR
x='(a b c)'; printf '[%s] len=%s\n' "${x[@]}" "${#x[@]}" [(a b c)] len=1 [a] len=b/[c] len=3 [(a b c)] len=1
set -- "${x[@]}"; echo argc=$# argc=1 argc=3 argc=1

The array-vs-scalar verdict is now typed rather than re-derived from the value's text, so "${x[@]}" (the documented "may be scalar or array" idiom) stops fabricating elements from ordinary data — ls-style globs, regexes, config snippets.

#758 — a malformed parameter expansion was silently swallowed

probe GNU bash 5.3.0 master this PR
v=abc; echo "[${v[0]x}]"; echo status=$? no stdout, stderr [${v[0]x}]: bad substitution, rc 1 [], status 0, no stderr no stdout, stderr [${v[0]x}]: bad substitution, rc 1
w=abcdef; echo "[${w:}]"; echo status=$? no stdout, stderr [${w:}]: bad substitution, rc 1 [], status 0 as GNU

The whole word is discarded before expansion, as GNU does, so set -e / if ! cmd / && guards see the failure instead of success.

Verification

  • Both probes byte-compared via script files (stdout, stderr, rc separately) against WSL GNU bash 5.3.0; the only textual difference left is the absolute script-path prefix in the diagnostic, which is environment-inherent.
  • New tests: tests/issue755_scalar_array_expansion.rs (10 passed) and tests/issue758_bad_substitution_status.rs (8 passed), sandboxed CWD.
  • cargo fmt --check clean (from the original commit).

…expansions (#755, #758)

#755: a scalar whose text looks like a parenthesised list was re-parsed as an
array by `"${x[@]}"`, so `x='(a b c)'` expanded to three elements and `$#`
shifted. GNU decides array-ness from the variable's CELL TYPE (subst.c
get_var_and_type: VT_VARIABLE vs VT_ARRAYVAR/VT_ASSOCVAR), never the value
text. Add `Executor::parameter_is_array_variable(name)` (ARRAY/ASSOC marks,
the structured `shell_state.variables` cell, or the dynamic-array registry)
and thread it through the list/subscript read paths, so a scalar is a
one-element list everywhere and a genuine array still fans out.

#758: a malformed parameter expansion was swallowed as an empty string with
status 0 and no diagnostic. Tighten the `valid_brace_expansion_word` gate to
require an `identifier[non-empty-subscript]` whose `]` is the last byte
(so `${v[0]x}`/`${v[a]b}`/`${v[@]x}` fail), reject the empty substring
operand `${v:}`/`${@:}` (while `${v::}`/`${v: }`/`${v:0:}` stay valid), and
report the WHOLE word GNU's `report_error` names (subst.c:10278) via a
one-shot enclosing-word record set by the immutable and mutable walkers.

Tests: tests/issue755_scalar_array_expansion.rs and
tests/issue758_bad_substitution_status.rs (sandboxed CWD; value/stdout/
stderr/status asserted separately, with genuine-array and valid-form guards).
@caomengxuan666
caomengxuan666 merged commit 657317c into master Oct 17, 2026
7 of 8 checks passed
caomengxuan666 added a commit that referenced this pull request Oct 17, 2026
 (#755, #758)

PR #785 typed the array-vs-scalar verdict (`parameter_is_array_variable`)
but missed the prefixed-storage case: `local v=("${foo[@]}")` /
`declare d=("${foo[@]}")` render `\u{1d}([0]=...)` while the ARRAY mark
lands late, so the copy collapsed into a single element holding the whole
storage render (a regression in the merged #785 —
`command_chaining::part_003::test_local_compound_array_assignment_preserves_quoted_array_at`).

The STORAGE_WORD_PREFIX is an UNAMBIGUOUS array marker (a scalar value never
carries it), so it is now accepted; the bare `(a b c)` shape that #755 is
about deliberately stays a scalar. Adds the declared-compound-copy guard to
tests/issue755_scalar_array_expansion.rs.

Refs #755. Fixes #758.
caomengxuan666 added a commit that referenced this pull request Oct 18, 2026
 (#755, #758) (#786)

PR #785 typed the array-vs-scalar verdict (`parameter_is_array_variable`)
but missed the prefixed-storage case: `local v=("${foo[@]}")` /
`declare d=("${foo[@]}")` render `\u{1d}([0]=...)` while the ARRAY mark
lands late, so the copy collapsed into a single element holding the whole
storage render (a regression in the merged #785 —
`command_chaining::part_003::test_local_compound_array_assignment_preserves_quoted_array_at`).

The STORAGE_WORD_PREFIX is an UNAMBIGUOUS array marker (a scalar value never
carries it), so it is now accepted; the bare `(a b c)` shape that #755 is
about deliberately stays a scalar. Adds the declared-compound-copy guard to
tests/issue755_scalar_array_expansion.rs.

Refs #755. Fixes #758.
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.

a scalar whose text looks like a parenthesised list is re-parsed as an array by "${x[@]}" (element count and $# change)

1 participant