Skip to content

Release unclosed-case bodies at the completeness gate (stacked on the read-time validation change) - #3303

Merged
caomengxuan666 merged 5 commits into
masterfrom
fix/gate-case-2789
Nov 11, 2026
Merged

caomengxuan666 merged 5 commits into
masterfrom
fix/gate-case-2789

Conversation

@caomengxuan666

@caomengxuan666 caomengxuan666 commented Nov 10, 2026 •

Copy link
Copy Markdown
Member

Refs #2789

What this does

Stacked on the read-time validation change (the two carried commits at the bottom of this branch are that change, verbatim): the completeness gate in src/lexer/continuation.rs held any ) hostage while a case opened inside the unit was deeper than at push, so an unterminated-case body such as $(case x in) died as an unclosed-EOF (status 2) before the parser could classify it — where GNU's body parser owns the paren (parse.y:4451 parse_comsub; parse.y:4519-4521 shell_eof_token) and raises a grammar error (-c: 127, file: 2).

The gate's existing pattern-content witness (#2784, refined by #3241) cannot judge shapes where the witness stays set or never applies: a pattern separator tail (a|), the pattern-list opener (()), an outer case's pattern polluting the witness for an inner substitution, and a clause terminator in the still-empty region (a;;).

Changes

  • src/lexer/continuation.rs — both gate implementations (the one-shot oracle unclosed_input_close_char_posix and its staged twin close_char_residuals_advance) now carry the parser arm's pattern-content bookkeeping (case_pattern_content, case_pattern_paren_depth, case_pattern_opener_available, case_pattern_region_seen); in the staged twin the four live as checkpointed CloseCharResidualState fields so a parked scan resumes with the same judgment. Each pop arm judges by a pre-update snapshot (region/content/stage/word captured before the case-depth update, carried to the pop arm in the same iteration): the ) closes the delimiter when the armed region has no content yet, or it is glued to in, or glued to the case subject — the same three conditions the parser arm reclassifies by. The detention boundary is unchanged: only a case opened inside the delimiter is judged, and a ) after real pattern content is still held.
  • src/parser/command_substitution.rs — a completion latch for the spaced variant $(case x in ): when in completes at a blank, the reserved-word machine's word-boundary preempt zeroes the stage before the pattern region arms, so no close-arm condition can see the shape. The latch is noted at the completion char, applied after the region bookkeeping, cleared by pattern content and by region disarm, and accepted as a fourth close condition. Also: extractor unit tests that pin the reclassification arm directly (three trigger predicates, four non-trigger controls, the latch pair) — the arm is unreachable on every product path while the gate detains the unit, so these tests are its independent proof.
  • tests/issue2789_comsub_read_time_validation.rs — two added cells: the spaced variant via -c aborts 127, and the spaced variant followed by a second substitution via -c aborts 127.
  • src/lexer/tests.rs — gate-verdict test for the released shapes and the two controls.

本 PR 删除了什么

Nothing. The change is purely additive bookkeeping plus tests; no code path, arm, or test was removed or narrowed. (UnclosedDelim, the staged case-depth function body, and the funsub / pattern_paren / arith arms are untouched by design.)

Verification

  • Extractor unit tests (test-first): on the pre-fix tree 7 passed / 1 failed — the three trigger predicates and four controls already green, the spaced-variant latch test red with left: None, right: Some("case x in "). After this change: 8/8.
  • The read-time validation pin: 11/11. Marginal attribution, measured head-to-head against the carried change alone on the same base: the pin's three originally red cells are already green there (the [待队长 diff 复核] extglob 组与 case-first comsub 提取(#2647/#2680/#2784) #3241 gate refinement, which is on the base, releases the glued forms), so this change's measured end-to-end deltas are the two shapes below — plus the gate-level releases in the equivalence surface.
    • case a in a) echo $(case x in);; esac: 2 → 127 (an outer case's pattern no longer detains the inner unit).
    • echo $(case x in ) $(echo ok): silently accepted (0) → 127. Without the latch the extractor scans past the case's own ) to the next substitution's close, so the malformed body is never bounded at all.
  • Gate equivalence: the staged-vs-oracle incremental module gains the released shapes and controls at every line prefix (both modes, parked and unparked); the pre-existing lexer pin case_pattern_paren_does_not_close_command_substitution still holds ($(case x in a) echo x) stays detained). The pattern-list opener shapes are agreement shapes only: the opener disarms the region (stage 4) in both machines, so both still hold the unit — recorded as a residual, not coverage.
  • Spot-check matrix vs GNU bash 5.3: 39/39 shapes agree (rc + stdout byte-for-byte, stderr class). Eval-concatenation witness (eval over a runtime-assembled echo $(case x in)): 127 with a syntax diagnostic, matching GNU's classification.
  • Neighbour and case-family stems (28 test targets): all green except issue381_empty_case_stray_delimiter_regressions, whose 3 red cells reproduce byte-identically on the base tree — a pre-existing harness artifact (its stderr normalizer mangles the prefix when the binary path ends in rubash), not a regression of this branch; the product behavior (status 2, token, line, echoed line) matches expectations on all three trees.
  • Full lib suite: base 1230 passed / 0 failed / 4 ignored; head 1240 passed / 0 failed / 4 ignored — failure name sets equal (both empty).
  • Legs: RUSTFLAGS="-D warnings" on the lib and bins rc=0; cargo check --tests rc=0; cargo fmt --check rc=0; scripts/check-marker-literals.py rc=0; scripts/check-utf8-boundary-hygiene.sh (whole tree) rc=0.

Notes for the merge round

  • This branch stacks the read-time validation change because the acceptance surface (pin cells, extractor arm, latch site) is defined against its code, which is not on master yet. If that change lands first, the two carried commits drop out on rebase; if this lands first, they go in as part of this branch.
  • Merge-tree against current master (bc049e16) is clean.
  • Observed, deliberately not fixed here: the extractor's own pattern region still never arms for the separator-tail shapes (a|, a;;), so those keep the EOF classification (status 2) end to end even though the gate now releases them — the gate/extractor machine divergence predates this change; and the skip-path family in skip.rs carries no equivalent of this predicate, so the composite has_unclosed_input_syntax still judges these units unclosed for the interactive feeder.

caomengxuan666 added a commit that referenced this pull request Nov 11, 2026
Refs #2789

## What this changes

GNU validates a substitution body when the enclosing command is read (parse.y:4451 parse_comsub hands the body to the grammar; `)` is the substitution's shell_eof_token, parse.y:4519-4521). Rubash screened bodies only on execution paths, so a body the program skips never reported, and an executed body in a -c string missed the 127 remap (shell.c:1471-1472).

- `src/script_driver.rs` (+16): the grouped reader screens each freshly parsed non-interactive unit before execution and before unit voiding; interactive readers and diagnostic-owned prefix runs are unchanged.
- `src/executor/command_substitution.rs` (+373, plus the +4/−3 marker-lint rework): `screen_comsub_bodies_at_read` plus a recursive walker over process-substitution bodies, word-shaped texts and nested command bodies. Word texts are re-extracted with a live-context scan (single-quoted and backslash-escaped openers are literal and yield no node). Backquote subtrees are skipped (GNU parses those in the substituted child); here-document bodies are data and never walked. On a hit the error is reported once through the existing body-error report — with the parked marker's body-relative lines translated into the reader unit's frame — and the unit aborts with the driver's own status (-c 127, file 2).
- `src/parser/command_substitution.rs` (+182): live-context word scan, and the in-field unclosed-case reclassification: a `)` that cannot close any pattern (armed pattern region with no content yet, or glued to `in` or to the case subject) ends the substitution, so the truncated body gets the body parser's grammar verdict instead of an unclosed-EOF classification.
- `src/parser/mod.rs` (+7): executor-facing wrapper for the live scan.
- `scripts/marker-lint-allowlist.txt` (+1): the U+0001 intra-record separator literal of the `__RUBASH_PARSE_ERROR_COND__` payload is allowlisted (no registered constant exists for this codepoint; producer and same-shape consumers share the allowlist precedent). Marker-lint whole-tree passes.

## 本 PR 删除了什么

No tracked file is deleted and no product line is removed: the change is +580/−0 across the five files above. The lane-local probe file used during development was untracked and was removed before the commit; it was never part of the tree.

## Verification (merge round, on the merged tree)

Captain review: `~/workspace/review-3282/CAPTAIN-VERIFY.md`; the gate interaction was measured independently in the PR #3303 captain review (§3 of its CAPTAIN-VERIFY).

- Pin `issue2789_comsub_read_time_validation` on the merged tree: 9 passed / 0 failed. (The three cells red on the lane's stale base were the completeness-gate face; on the current base this stack's pin already measures 9/9. The gate itself is PR #3303, stacked behind this PR.)
- Neighbor stems (12): all green.
- Lib tests, merged head vs current master: failure name sets empty on both sides (bin built first on both trees).
- `cargo fmt --check`: clean. Marker-lint whole-tree: clean. UTF-8 boundary hygiene whole-tree: clean.
- `RUSTFLAGS="-D warnings"` on this change's own targets: clean. Whole-tree stops only at pre-existing base test-target defects byte-identical between base and head (recorded in the captain review).
- CI: all checks finished; zero failures outside the master baseline set; ledger canary green.

#2789 stays open: its remaining faces (the completeness gate in PR #3303 and the residuals filed from that review) are accounted separately; the captain rules its closure after the gate merges.
…tion arm

Three trigger predicates (armed empty region / paren glued to `in` /
paren glued to the subject) and four non-trigger controls, per the
captain review of the P4B read-time validation work. The space-variant
test is the fail-first witness for the completion latch that lands in
a later commit: at this commit it fails, the other seven pass.

GNU anchor: parse.y:4451 parse_comsub, parse.y:4519-4521
shell_eof_token. Refs #2789
The pop arms of the completeness gate (oracle
unclosed_input_close_char_posix and its staged twin
close_char_residuals_advance) detained any ')' while a case opened
inside the unit was deeper than at push, so '$(case x in ...)' unit
bodies could not reach the parser whenever the pattern-content
witness could not tell the shape apart: a pattern separator tail
(a|), the pattern-list opener ((), an outer case's pattern setting
the witness for an inner substitution, and a clause terminator in
the still-empty region (a;;).

Both implementations now carry the parser arm's pattern-content
bookkeeping (case_pattern_content / case_pattern_paren_depth /
case_pattern_opener_available / case_pattern_region_seen — in the
staged twin as checkpointed CloseCharResidualState fields, so a
parked scan resumes with the same judgment) and judge the pop arm
by a pre-update snapshot: the ')' closes the delimiter when the
armed region has no content yet, or it is glued to 'in', or glued
to the case subject — the same three conditions the parser arm in
dollar_command_substitution reclassifies by (parse.y:4451
parse_comsub takes the whole unit; parse.y:4519-4521 raises the
grammar error there). The detention boundary is unchanged: only a
case opened inside the delimiter is judged, and a ')' after real
pattern content is still held.

Non-regression surfaces: the staged-vs-oracle equivalence module
gains the released shapes plus controls at every line prefix;
src/lexer/tests.rs gains the gate-verdict test. UnclosedDelim, the
staged case-depth function body, and the funsub / pattern_paren /
arith arms are untouched.
'$(case x in )' (space before the paren) could not reclassify: when
'in' completes at a blank, the reserved-word machine's word-boundary
preempt zeroes the stage before the pattern region arms, so none of
the close arm's three conditions sees the shape and the extractor
scans off the end of the word.

dollar_command_substitution now latches the completion: a pre-update
capture notes the char that completes 'in' at stage 2, the latch is
applied after the region bookkeeping (so the disarm-clear cannot
erase it in its own iteration), pattern-content chars and region
disarm clear it, and the close arm accepts it as a fourth condition.
The glued forms keep judging by conditions (a)-(c) exactly as
before; '$(case x in a)' still yields no truncation because the
pattern char clears the latch.

Pinned by the extractor unit test
reclassifies_space_variant_via_completion_latch (fail-first against
the pre-latch tree: None where Some("case x in ") is required).
@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 11, 2026 00:30
@caomengxuan666
caomengxuan666 merged commit 2c676ed into master Nov 11, 2026
11 checks passed
caomengxuan666 added a commit that referenced this pull request Nov 17, 2026
… (Refs #3307) (#3756)

The non-interactive gate (#3303) releases unclosed-case comsub units
whose armed pattern region has no content yet, but the three #2784-
witness unit scans (Lexer::skip_cmd_subst, skip_parenthesized_unit_
corrected and its staged mirror skip_parenthesized_unit_corrected_ex)
carried no equivalent predicate, so the interactive face held
separator-tail shapes such as `echo $(case x in a|)` to PS2 while the
gate released them. Port the gate/parser arm's per-pattern content
bookkeeping (content, paren depth, opener availability, pending-opener
count) into all three scans and add the reclassification predicate
`(region_before && !content_before)` to their `)` arms.

Tests: tests/issue3307_repl_unclosed_case_comsub.rs (fail-first: the
two released-family tests fail on the base tree).

Co-authored-by: caomengxuan666 <[email protected]>
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