Skip to content

parser/executor: first-element line stamps (time prefix, procsub targets, respawn span, function-body plant) - #3266

Merged
caomengxuan666 merged 4 commits into
masterfrom
fix/3053-3054-3209-stamps
Nov 10, 2026
Merged

caomengxuan666 merged 4 commits into
masterfrom
fix/3053-3054-3209-stamps

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

What this changes

One mechanism family: a simple command's first-element line stamp
(GNU simplecmd_lineno) and the places that stamp failed to reach.

GNU anchors (bash 5.3 sources): parse.y:316 (stamp initialised to
-1), parse.y:5849-5851 read_token_word (a word read at command
position plants the stamp), parse.y:810-811 (a redirection element
captures the stamp only while it is still unset, after its target
word was read), parse.y:832 make_simple_command (stored once as
Simple->line), parse.y:838 clean_simple_command (the plant is
consumed by the first completed simple command, and by nothing
else), parse.y:1055 (the bare-name function definition form plants
through the same command-position capture; the keyword forms plant
nothing), execute_cmd.c:936 SET_LINE_NUMBER (diagnostics and
$LINENO report the stored stamp).

1. time prefix and procsub redirect targets (parser)

rubash parsed time as the node's own first word, so the
provisional stamp was the time token's and the real first element
could never restamp it; the prefix fold stripped the head without
restamping. While a node's recorded elements are exactly a foldable
time-prefix chain, every following element's intake now records its
end line as a transient candidate on the node (word intakes re-run
the fold's own chain scan with the word appended, so admission is
the fold's, not a second spelling list), and the fold applies the
candidate at the moment it strips the head. A node that never folds
is byte-identical to before.

The process-substitution target consumption sites were never wired
to the redirect-target stamp correction that plain targets and
heredoc delimiters already had, so the stamp stayed at the
operator's line. The correction token for a procsub target is the
closing ) token the collector stops on — the whole span's end
line, i.e. the GNU word end. Wired sites: the RedirectIn /
RedirectErr / RedirectErrAppend / HereString procsub branches in
token actions, and the combined / output / append target paths in
redirect assignment. Deliberately not wired:
collect_trailing_redirections, which only ever collects trailing
redirects, never a first element.

2. Respawn span transport (executor)

CommandLineSpan carried only line / end_line / logical_line, so a
respawned child re-derived the first-element stamp from its
re-parse of the collapsed body text and the executor's guard
degraded split first elements to their start line. The span now
carries first_word_end_line end to end: collect copies it, apply
writes it back unconditionally (the parent's value is
authoritative), and the shared wire codec for both span carriers
emits a fourth dotted field. The FN carrier's encoder, which
open-coded the old three-field format, now uses the shared
formatter. The parser still accepts the three-field form (reads as
no stamp), so a hand-built stamp cannot misalign a re-seed; live
transports are always same-binary and always emit four fields.

3. Function body's first command, redirection-first (parser + executor)

New regression pin (24 cells, every expectation byte-verified
against GNU Bash 5.3.20 script-file probes): a bare name()
definition plants the stamp with the definition line; when the
body's first simple command is redirection-first, its stamp is the
definition line, not the line its first element finished on. (The
pin file's tail was lost to a truncated tool output written back
during the write-wall episode and was reconstructed from the
wave's surviving GNU probe runners with every expectation
re-measured; the pin header records this.)

Element order is not stored on the node, so "this command's first
element was a redirection" is proven where it is exact — inside the
redirect-target correction, whose admission is precisely "no other
element recorded yet"; both of its branches now set a
first_element_redirect flag. finish_function_command, the
single exit every definition path shares, then walks the body in
parse order for bare-name definitions only and restamps the first
simple command's line and first_word_end_line to the definition
line when the flag is set. Wrapper commands are descended in token
order; arithmetic and conditional commands are skipped over (they
never capture or reset the stamp); nested definitions and case
commands stop the walk (a case clause's stamp comes from the
pattern token — a distinct compound-command mechanism, recorded
separately). The two executor sites where a definition body is
re-parsed at definition time apply the same restamp to the retried
body. logical_line is untouched.

Fail-first

Measured on the pristine base (f7fed212); per-stem test result
lines:

  • issue3053 stem: 5 passed / 15 failed.
  • issue3054 stem: 2 passed / 10 failed.
  • issue3209 stem (new pin): 18 passed / 6 failed — the reds are
    the plant cells (multiline body, brace body on its own line,
    split header, subshell body form, background-first, coproc).
  • issue3210 stem (cross-check only, not part of this change):
    8 passed / 2 failed.

Head verification

  • issue3053 stem: 20 passed / 0 failed.
  • issue3054 stem: 12 passed / 0 failed.
  • issue3209 stem: 23 passed / 1 failed — the one red is
    pre-registered: coproc_first_reports_definition_line is
    blocked by a parser defect outside this change (the coproc
    grammar rejects a redirection-first coproc body outright, on
    base and head alike, so the walk's coproc arm cannot be
    reached; it needs its own issue).
  • issue3210 cross-check: 8 passed / 2 failed, unchanged from
    base. One of the two reds is the d8 cell, whose own repair wave
    follows this PR. The other red's expectation contradicts the
    GNU oracle (the pin expects rc=0 where GNU measures rc=127,
    and rubash matches GNU byte-for-byte there); recorded in the
    wave report for that pin's owners.
  • Library tests, both trees: 1207 passed / 0 failed on base and
    1207 passed / 0 failed on head — the failure name sets are
    identical (empty).
  • Neighbour stems, all green: issue2202 span transport 10/0,
    issue2332 respawn text body 12/0, issue3028 continuation
    error line 23/0, issue1184_f4 procsub diagnostics 7/0,
    issue1520 parse-time POSIX time 18/0, issue1548 time quoted
    option operand 12/0.
  • RUSTFLAGS="-D warnings" cargo check on the head tree: clean.
  • cargo fmt applied; the change is formatted.

Residuals recorded in the wave report

  • The coproc grammar defect above (redirection-first coproc
    bodies do not parse).
  • Seven matrix shapes (keyword definition forms, a second body
    command, an assignment-completed first command, a word-first
    split, a pipeline second stage, a command after a
    redirect-only first command) report line 1 on both trees where
    GNU reports line 2 — a flattening of the first body command's
    stamp inside definition capture, unchanged by this change and
    outside its mechanism. Measured values are in the wave report
    addendum; the shapes are deliberately not pinned here.

What this PR deletes (本 PR 删除了什么)

Nothing is deleted outright. The span wire format's three-field
rendering is replaced by the four-field form (the parser keeps
reading three-field entries), and the FN carrier's open-coded span
rendering is replaced by the shared formatter. No files removed;
the net change is additive (new node fields, the restamp walker,
the candidate hooks, and the correction call at nine procsub
sites).

Refs #3053
Refs #3054
Refs #3209

…dies

GNU bash plants the function name's line in the first body
command when that command's first element is a redirection
(parse.y:5849-5851 plant, parse.y:810-811 first capture,
parse.y:838 consume on completion). The pin covers the plant,
the wrappers that do not consume it, and the guards that must
not inherit it; every expectation is byte-verified against
GNU Bash 5.3.20 script-file probes.

Refs #3209
…sub targets, respawn spans, and function definitions

Three stamp losses, one per face:

A node whose words are a foldable time prefix chain records the
end line of the first element after the chain at that element's
intake, and the fold applies it when it strips the prefix
(GNU parse.y:1055 keeps the timed command's own lineno).
Process-substitution redirection targets now run the same
first-element correction as plain targets, with the closing
paren token as the correction point. Synthetic procsub words
carry the assembled word's real end line.

The respawn span codec gains a fourth field carrying the first
element's end line end to end; three-field entries still parse
with no stamp. Both span carriers share one formatter/parser
pair (GNU variable.c:5703-5707: the child already runs with
SHELL set to the parent's path).

A bare name() definition restamps its body's first simple
command to the definition line when that command's first
element was a redirection, at the function parser and at both
executor definition-reparse sites (GNU parse.y:316,
parse.y:5849-5851, parse.y:810-811, parse.y:838;
execute_cmd.c:936).

Refs #3053
Refs #3054
Refs #3209
@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 10, 2026 17:25
@caomengxuan666
caomengxuan666 merged commit d7a3725 into master Nov 10, 2026
11 checks passed
caomengxuan666 added a commit that referenced this pull request Nov 10, 2026
…3221)

The output process-substitution spawn carries fd>=3 through the __RUBASH_FD_* env channel as inheritable dups delivered AT their stamped numbers; the child's init restores the stamped handle instead of adopting whatever descriptor sits at slot fd. Without this declaration the consumer adopted the inherited outer file at slot 3 (direct-test6: tap.log 7 lines, comsub empty, INNER-RC=1). GNU anchor: subst.c:6362 process_substitute forks the consumer at expansion time inheriting the expansion-time table, which is what the stamped handle carries.

The comsub-nesting attribution half of this shape is already in master via #3132 (dollar_group_end opacity, d65441d): a span inside $(...) belongs to the comsub child, so the clone's stage machinery materializes it via this stamped spawn route. This commit carries the stamped-delivery half plus the pin that proves the combination.

Verified on the Simular box. run6 (pre-rebase tree): fail-first pin 3 passed / 2 failed (cells 04/05 red); head pin 5 passed / 0 failed; probe-adopt, V1, V2, V3 all pass; direct-test6 INNER-RC=0; RUSTFLAGS="-D warnings" cargo check rc=0; fmt rc=0; marker-lint rc=0; lib head 1207 passed / 0 failed, failure name set empty. run7 (rebased head, box pid 718998): Phase A on pure master d7a3725 — pin 5/0 and probe-adopt PASS without this PR (#3132 + #3234 already fix the reduced shapes); Phase B on the head — probe-adopt, V1, V2, V3 pass, pin 5/0, direct-test6 INNER-RC=0, -D warnings/fmt/marker-lint rc=0, lib head 1230 passed / 0 failed / 4 ignored, failure names none. run8 (discrimination, 917870f binary, box pid 721997): direct-test6 INNER-RC=1 with the byte-identical leak and pin 5/0 — #3266 exonerated, and the stamped insert is what fixes the real bats shape (the reduced pin passes on master without it; the full shape is guarded by the direct-test6 box leg). Adjacent gap, out of scope: the backquote shape (out=`exec 3<&1; ...`) still fails on the head (BQ-FAIL: log non-empty) — #3132's rule covers only $-groups; GNU expects OUT hello twice, log empty.

Refs #3221
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