Skip to content

fix(executor): silent pattern word for empty/absent list subjects (Refs #3501) - #3599

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/3501-pattern-word-silent
Nov 14, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/3501-pattern-word-silent

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Root cause

GNU never expands the pattern word when the parameter word it would
operate on is absent — and, for #/%, empty:

  • parameter_brace_patsub (bash 5.3, subst.c:9362-9367) and
    parameter_brace_casemod (subst.c:9604-9608) both start with
    if (value == 0) return NULL, before getpattern.
  • parameter_brace_expand's case '#': case '%':
    (subst.c:10320-10327) breaks out before
    parameter_brace_remove_pattern when
    value == 0 || *value == '\0'.

The scalar entry points (expand_parameter_pattern_removal,
expand_braced_replacement_parameter, expand_braced_case_parameter,
compound-assignment arms) already gate on this. The whole-list entry
points expanded the pattern unconditionally before mapping it per
element, and indirect removal never reached its handler at all:

  • Executor::array_pattern_word_values (name[@]/name[*]) and
    Executor::positional_modified_word_values (@/*) now return
    Some(vec![]) when pattern_word_is_silent holds — removal on the
    joined word (silent for one "" only when unquoted),
    patsub/casemod on absence only.
  • at_modified_segment_values now threads the AtModified segment's
    own quote state instead of hardcoding quoted, so an unquoted
    pre${a[@]#...} segment gates as unquoted.
  • Indirect removal (${!x#...}): has_indirect_parameter_word_operator
    scanned operator characters inside nested ${...} (the = in
    ${-y}), so expand_braced_special_or_indirect_parameter declined
    before indirect_pattern_removal ran. The predicate is now
    top-level-only (a pattern operator ends the scan), and
    indirect_pattern_removal applies the same
    pattern_word_is_silent("!holder", true) gate before expanding its
    pattern. The routed-body = arm was ruled out:
    TopLevelOpIndex skips nested ${...}, and
    is_parameter_error_name("!x#*a") is false.

GNU anchors

subst.c:9362-9367 (patsub), subst.c:9604-9608 (casemod),
subst.c:10320-10327 (#/% removal). Pin
tests/issue3501_pattern_word_silent.rs (68 cells) replays every cell
against the GNU 5.3 oracle (68/68 MATCH).

Known residual

#3581: a QUOTED [@]/[*]/@ word with exactly one "" element
diagnoses in GNU but the direct "${a[@]#...}" form is the only face
this PR preserves by the quoted flag; the remaining quoted-flag
threading gap is tracked in #3581, not papered over here.

本 PR 删除了什么

  • 6 行临时 [TRACE3501] eprintln! 探针(续跑轮的诊断插桩,
    arrays/executor.rs、command_substitution_values.rs、
    parameter_words.rs、parameter_patterns.rs 各 1–3 行)。除此之外
    未删除任何产品代码与测试。

UNVERIFIED-BY-CAPTAIN

#3501)

GNU subst.c:10320-10327 (`case '#': case '%':`) breaks out before
`parameter_brace_remove_pattern` when the parameter word is absent or
empty, and `parameter_brace_patsub` (subst.c:9362-9367) /
`parameter_brace_casemod` (subst.c:9604-9608) return NULL before
`getpattern` when the value is absent — so a `bad substitution` inside
the pattern word is never diagnosed for such subjects. The scalar
entry points already gated this way; the whole-list entry points
expanded the pattern unconditionally:

- `Executor::array_pattern_word_values` (`name[@]`/`name[*]` targets)
  and `Executor::positional_modified_word_values` (`@`/`*` targets)
  now return `Some(vec![])` when `pattern_word_is_silent` holds
  (removal: joined-word gate, silent for one "" only when unquoted;
  patsub/casemod: absent-only).
- `at_modified_segment_values` now threads the segment's own quote
  state instead of hardcoding quoted, so an unquoted
  `pre${a[@]#...}` segment gates as unquoted.
- Indirect removal (`${!x#...}`) was blocked one level up:
  `has_indirect_parameter_word_operator` scanned operator characters
  inside nested `${...}` (the `=` in `${-y}`), so
  `expand_braced_special_or_indirect_parameter` declined before
  `indirect_pattern_removal` ran; the predicate is now top-level-only
  (a pattern operator ends the scan), and `indirect_pattern_removal`
  applies the same `pattern_word_is_silent("!holder", true)` gate
  before expanding its pattern.

Quoted boundary (oracle-measured, #3581): a quoted one-"" list still
diagnoses; only the absent-or-empty-list (unquoted one-"") case is
silent. The routed-body `=` arm was ruled out as the cause:
`TopLevelOpIndex` skips nested `${...}`, and
`is_parameter_error_name("!x#*a")` is false.

Refs #3501
@caomengxuan666

Copy link
Copy Markdown
Member Author

复核 PASS(队长)

独立复核完成(卷宗 review-3599/CAPTAIN-VERIFY.md),判词 PASS。

合入正文将去除 UNVERIFIED-BY-CAPTAIN 并换入以上已验腿,口径 Refs #3501;#3501 由队长于合入后手动关单。

@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 14, 2026 14:41
@caomengxuan666
caomengxuan666 merged commit 27e1d0d into master Nov 14, 2026
8 of 11 checks passed
caomengxuan666 added a commit that referenced this pull request Nov 14, 2026
…(Refs #3500)

## Summary

A bad substitution inside a compound-assignment element was diagnosed with the `${...}` fragment instead of the element word. GNU quotes the whole element and reports the word being expanded, e.g. `a=([0]=${-x})` → `\[0\]=${-x}`.

## Root cause

`subscripted_parameter_element` hands the RHS to `whole_word_parameter_single_value`, whose `has_word_operator` arm misjudges `${-x}` (empty `var_name` at position 0) and expands it standalone with no pending diagnostic word installed, so `expand_braced_parameter_word` falls back to the synthesized `${-x}` fragment. The plain-element path had the same gap (the walker default names the raw token, without `quote_assign`'s `\[sub\]` quoting).

## Fix

- New `compound_element_diag_word` (module level, mirrors GNU `quote_assign`): indexed `[sub]=value` → `\[sub\]=value`; assoc elements → the bare value; a quoted value keeps the fragment's own word.
- The element word is installed as the pending diagnostic word at the two element-expansion sites (`subscripted_parameter_element`, `compound_plain_element_value[_fields]`), install-if-absent.
- An `assoc` parameter is threaded through `expand_compound_positional_at_assignment[_bare]` (4 call sites), computed by new `compound_assignment_is_assoc` (live `ASSOC_VARS` marking OR the declare-assoc hint).
- New declare-assoc thread-local hint (`DECLARE_ASSOC_HINT` + RAII guard), installed by the `declare_assoc_operand_names()` scan in `command_prepare.rs`, covering the `declare -A` command-word expansion path, which runs before the declare builtin.
- Bare (eval-argument) flatten installs no word: documented residual, not fixed here.

GNU anchors: `arrayfunc.c:557/:581/:594-599/:606/:630/:938-977/:1108-1129`; `subst.c:10278`.

## Regression pin

`tests/issue3500_compound_elem_diag_word.rs` — 19 cells (10 fix shapes + 6 guard shapes + 2 quiet guards + 1 first-bad-wins). Expectations verified against GNU bash 5.3.20.

## Verification (captain-verified legs)

- Merged head `d11ffbcf` (update-branch of `00c0a07` onto tip `021705cf`, three update-branch rounds: `8eb07872` (#3510), `12aa434e` (#3455, disjoint files), `021705cf` (#3220, disjoint files); intersection `{src/executor/assignment_expansion.rs}` via #3599, hunks ≥164 lines apart, no overlap): pin **19/19**; same-file neighbor issue3501 pin **68/68**; adjacent assoc-face issue3573 pin **14/14**.
- Fail-first (top-up form on base `11719895`): pin 11/19 — 8 red, each reporting `Some("${-x}")` where the element word is expected.
- Neighbor `array_compound_quote_regressions` 17/17 on head.
- Full lib bilateral (bin built first): base 1247 passed / 0 failed / 4 ignored; merged head 1249 passed / 0 failed / 4 ignored — failure name-set diff empty (the +2 are the tip's Mach-O unit tests from #3510).
- `RUSTFLAGS="-D warnings" cargo check` RC=0, 0 warnings; `cargo fmt --check` RC=0; utf8-boundary hygiene clean.
- Head CI run `43619058744` (on `10da84b4`, the second update-branched head): 3 failing jobs, sole FAILED name `issue2875` (known baseline). A fresh CI run starts on the final head `d11ffbcf` from the third update-branch; it differs from `10da84b4` only by the disjoint #3220 merge.
- `continuation.rs` untouched.

Refs #3500

## 本 PR 删除了什么

Nothing — purely additive fix plus the new regression pin. No code, test, file, or behaviour removed.
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