Skip to content

Stage-2 ④ Phase 4: variable-attribute family onto Diag (Refs #2707) - #2968

Merged
caomengxuan666 merged 12 commits into
masterfrom
p4-phase4-var-attr-family
Nov 10, 2026
Merged

caomengxuan666 merged 12 commits into
masterfrom
p4-phase4-var-attr-family

Conversation

@caomengxuan666

@caomengxuan666 caomengxuan666 commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

Stage-2 ④ Phase 4: the variable-attribute family
(declare/typeset, local, readonly, export, unset,
alias/unalias) migrates onto the (table row + BuiltinOpt +
Diag) triple, and the assignment machinery takes &mut Diag
with its sink bound at the dispatch point to the command's own
IO. That closes A3-R1 (Refs #2707): readonly-variable
diagnostics can no longer bypass the command's stderr
redirection.

UNVERIFIED-BY-CAPTAIN — lane delivery; all numbers below are
raw lane counts awaiting central verification. Full REPORT in the
lane workdir stage2-p4-phase4/REPORT.md (not committed).

What "fd-2" means here

Not the shell's stderr. The Diag sink is bound at the builtin
dispatch point to the command's own fd 2 as bound by its
redirections
: 2>f puts the diagnostic in file f, 2>&1
puts it on stdout, and only with no redirection does it land on
the shell's stderr. Evidence (oracle GNU bash 5.3.20, byte
compare): readonly a=old; (( a = 6 )) 2>f leaves f holding
a3r1.sh: line 3: a: readonly variable with the shell stderr
empty, byte-identical to GNU; readonly v=old; read v 2>f
likewise. Prefix assignments (v=new true 2>f) are the
documented exception: GNU itself puts that diagnostic on the
shell stderr (assignments are evaluated before redirections
take effect), and rubash matches.

GNU anchors

  • builtins/common.c:240 sh_readonly — the one skeleton the
    declare family's readonly diagnostics converge on
    (DiagKind::Readonly).
  • error.c:453 err_readonly — the bind-layer shape
    ({prefix}VAR: readonly variable, no builtin name) used by
    read/arithmetic/let binds; routed through the
    dispatch-point-bound command IO (Diag::err_readonly).
  • builtins/declare.def:145 DECLARE_OPTS "+acfgilnprtuxAFGI";
    builtins/declare.def:389-406 (-p passes operands verbatim),
    :465-476 (cannot-use--f), local_builtin (straight
    local --help catch); builtins/setattr.def:112 ATTROPTS "aAfnp" ('n' = undo); builtins/set.def:855 (unset invalid
    option → EX_USAGE_ORIG 258); builtins/getopts.def:301
    (OPTARG bind).

Ledgers

  • Pins tests/issue2707_readonly_diag_routing_regressions.rs:
    base 7 passed / 4 failed → after 11/11 (re-checked 11/11 after
    every migration stage and on the final binary).
  • E1 (232 cells, per-builtin getopt differential): before 189
    SAME / 43 DIFF → after 230 SAME / 2 DIFF, 41 flips, 0
    regressions. Remaining 2: export -aA / readonly -aA
    (pre-existing attribute-precedence + assoc print artifact).
  • E2 (A3-R1 probe set): probe-a3r1.sh 12/12 stdout+stderr
    byte-identical to GNU; probe-fd2.sh 11/12 (only c12 differs —
    pre-existing getopts OPTARG divergence, REPORT §7.1, not fixed).
  • E3 (D5 full matrix, 3112 cases): before 2756 SAME / 352 DIFF /
    4 TIMEOUT → after 2801 SAME / 307 DIFF / 4 TIMEOUT, 45
    flips DIFF→SAME, 0 regressions. Residuals are pre-existing
    listing/filter semantics (REPORT §5), mapped by the captain at
    filing time.
  • E4 (shellbench, raw numbers only, no PASS claim): base
    e4/20261107-085036-p4-base.md vs lane
    e4/20261107-090139-p4-lane.md; median per-op µs in REPORT
    §10. Control cases swing both ways between the two single runs
    (echo −34%, call_empty −29%, colon +31%) — machine noise;
    µs-level verdicts are for the captain's alternating A/B
    arbitration.
  • Neighbourhood (138 stems, scrubbed cargo): 132 passed-stems /
    6 failed-stems; all 6 fail identically on the base tree
    (c095a5e, same counts and same failing test names), 0
    regressions.
    Read batch 62/64, family batch 70/74.
  • Pre-push gates: utf8-boundary hygiene clean; rustfmt --check
    clean on every touched file; RUSTFLAGS="-D warnings" cargo check --locked --lib --bins rc=0; full lib via scrubbed cargo:
    lane 1188 passed / 0 failed / 4 ignored, identical to base
    c095a5e (1188/0/4)
    — see REPORT §9.

本 PR 删除了什么

  • src/builtins/declare/diagnostic.rs 整文件删(私有前缀工厂)。
  • declare/assign.rs 等多处自选汇诊断点:print_declare_usage、
    declare.rs/assign.rs/attrs.rs/print.rs 的手写诊断前缀(16 行含
    "readonly variable" 字面量的旧 emit 点,见 REPORT §8)。
  • setattr/value.rs 的 diagnostic_prefix + readonly_error_prefix;
    set/unset.rs 的本地 diagnostic_prefix;alias.rs 的
    diagnostic_prefix(含 "rubash: " 兜底份)。
  • declare_local.rs 的 scan_declare_options +
    DECLARE_OPTION_LETTERS。
  • buffer_assignment_diagnostics 机制整删(executor/mod.rs
    字段 + 文档、init.rs、command_substitution.rs 初始化、
    emit_assignment_diag 内分支;rg 零残留)。

净行数:工作区 38 文件 +1438/−1222(净 +216;含前两提交已合入
的钉与算术修复)。

Scope-out(只记录,不修、不立案)

  • c12 getopts OPTARG:GNU 每次合法选项都绑定 OPTARG
    (getopts.def:301),rubash 无参分支静默解绑且抹只读标志——
    预存分叉,REPORT §7.1。
  • export -aA / readonly -aA assoc 打印残影;E3 列表/过滤
    残余(declare -c/-t 过滤忽略、动态值渲染、local -f/-F
    无名、裸 unalias rc 2 vs 1)。
  • "readonly variable" 字面量账:master 301 行 → HEAD 312 行
    (钉添 11)→ 本期 302 行(+6/−16);门禁按字面写未达成,
    诚实账目见 REPORT §8(族内手写点已收敛经 Diag;剩余在族外
    预存 emit 点与 Err 载荷,豁免与否待队长裁)。

A3-R1 fail-first pins: arithmetic/let readonly binds must route
their err_readonly diagnostic through the command's fd 2 as bound
(2>&1 lands on stdout, 2>file lands in the file, shell stderr
empty); read/declare/export cells are the already-conforming
control half; bare assignment statements keep the GNU shape of
ignoring their own redirect for the bind diagnostic.

GNU ground: error.c:453 err_readonly, builtins/common.c:240
sh_readonly; oracle-verified against GNU bash 5.3.20 on base
c095a5e (arith/let cells diverge there, controls agree).
…efs #2707)

report_arithmetic_readonly_error printed via raw eprintln!, bypassing
the command's fd-2 binding: (( a = 6 )) 2>&1 and let 'a = 6' 2>&1 with
a readonly leaked the diagnostic to the shell's real stderr instead of
the dup'd stdout. GNU error.c:453 err_readonly goes to fd 2 as bound.
Route through arith_diag (write_diagnostic_fd2) like the 21 sibling
arithmetic diagnostic sites.
…2707)

declare/typeset, local, readonly, export, unset, alias/unalias move to
the (table row + BuiltinOpt + Diag) triple; the assignment machinery
takes `&mut Diag` with its sink bound at the dispatch point to the
command's own IO, closing A3-R1: readonly-variable diagnostics can no
longer bypass the command's stderr redirection.

Deleted: declare/diagnostic.rs (private prefix factory),
print_declare_usage, hand-rolled diagnostic prefixes in
declare/assign.rs and siblings, diagnostic_prefix in
setattr/value.rs, set/unset.rs and alias.rs (incl. the "rubash: "
fallback), scan_declare_options + DECLARE_OPTION_LETTERS, and the
buffer_assignment_diagnostics mechanism.

GNU anchors: common.c:240 sh_readonly, error.c:453 err_readonly,
declare.def:389-406/465-476, setattr.def:112, set.def:855.
E1 230/232, E2 probe-a3r1 12/12 byte-identical, E3 2801/3112 (+45,
0 regressions), pins 11/11, neighbourhood 132/138 (6 pre-existing
base-identical failures). UNVERIFIED-BY-CAPTAIN.
…ts once (Refs #2707)

Phase 4's execute_unset kept the pre-migration branches that open a
plain `2>file` target directly and hand the File to the Diag as its
sink, but the migration also appended a trailing
write_buffered_builtin_output for the new stdout buffer. That route
re-applies cmd's redirects: its second, truncating open of the same
target erased the readonly diagnostic the Diag had just flushed into
the first handle, so `unset uv 2>f` produced an empty file and an
empty shell stderr (append targets survived — no truncation).

GNU applies a command's redirections exactly once:
execute_cmd.c:5606 do_redirections binds fd 2 before the builtin
runs, and unset_builtin's readonly verdict (builtins/set.def:961-967)
speaks through builtin_error (builtins/common.c:98) into that bound
fd. Mirror the shape the rest of the migrated family already uses:
buffer both streams, let the ordered buffered route apply the
redirects once. An unopenable target stays settled before the body
runs by the dispatch pregate (probe_output_redirect), verified
byte-identical to GNU for unset/export/declare/alias.

Pins: seven new cells in issue2707_readonly_diag_routing_regressions
(unset readonly x file/dup/no-redirect/append, unset -v invalid id,
unset -f readonly function, unopenable-redirect suppression).
Fail-first 3 failed / 4 controls passed; after the fix the stem is
18 passed / 0 failed and the full lib is unchanged at 1188/0/4.
@caomengxuan666

Copy link
Copy Markdown
Member Author

队长复核:功能面全过,E4 性能门 FAIL — 暂不合,已派同分支性能专修

功能复核全部通过(详见复核记录):E1 232 格 SAME 189→230(41 翻转全 DIFF→SAME、0 回归);E3 3112 例 SAME 2756→2801(恰 45 翻转、全在本族 --help 格、0 回归);回归钉 18/18;全量 lib 1188/0/4;RUSTFLAGS="-D warnings"、UTF-8 hygiene、逐文件 rustfmt 全清;队长 14 格探针(unset 四种重定向形态 + 本族每成员一格诊断)与 GNU 5.3.20 逐字节一致。迁移引入的 unset 诊断丢失回归已在本分支修复并复验(ba25d2ee)。

但 E4 不过。 队长同机 A/B(base = master 89e18a92,交替 9 轮取中位数,两轮主测 + 一轮判别测):

形状 第一轮 第二轮
unset 循环 +9.0% +8.7%
local 循环 +7.7% +5.1%
export 循环 +6.6% +4.7%
typeset 循环 — +4.1%
alias 循环 — +3.4%
declare 循环 +10.7% +1.1%(噪声大)

未迁移对照全部居中于零:: −3.0/−2.2%、true −3.8/+1.3%、echo +1.6/−1.8%、hash(Phase 2 族)−0.04%、shopt(Phase 3 族)+0.85%。即仪器合格,税是 Phase 4 族特有的每调用成本(绝对量约 +5~20 µs/调用),与 Phase 2 当年急切构造诊断序言的病型同类(那次的修法是延迟渲染)。预登记门:回归 >2% = FAIL。

处置:本 PR 保持开启;已在同分支派性能专修车道定位成功路径上的每调用成本并按机制修复(成功路径不得做诊断/缓冲的急切工作),钉与全量 lib 须保持全绿。队长复跑 E4 全族 ≤2% 后再合。

…th (Refs #2707)

Phase 4 entries built the diagnostic Prolog eagerly at dispatch
(~7us/call, see builtin_diag.rs) even when no diagnostic was
pending — the Phase 2 tax again. Remove the eager builds at the
unset / export / readonly / alias / setattr entries (flush sites
build inside the closure, the Phase 1-3 shape; prolog-only
parameters dropped), and give the declare/local in-context
crossings a deferred PrologParts: only the two fields that do not
derive from the variable table cross by value, the rest is
composed from the VarTable at the diagnostic site, byte-identical
in both the executor and library flavors.
Executor::diagnostic_prolog now composes the BASH_SOURCE view
once (it previously fed error_name and script_file from two
compositions). BuiltinOpt spec parsing is memoized per spec
string (the declare family's predicate probes construct the
scanner several times per call).

No semantic change; diagnostics verified byte-identical to the
base build in script, function and -c modes. Gates: pin stem
issue2707 18/18, full lib 1188/0/4, RUSTFLAGS="-D warnings"
check rc=0, utf8 hygiene clean, rustfmt clean.
caomengxuan666 added a commit that referenced this pull request Nov 8, 2026
Refs #3045

`${var=word}` 绑定在 case 主词与 for/select 词表里丢失——此前只走 `&self` 的 `expand_word`,没有 apply pass。修法(同一机制,四处):

- `expand_case_word` 与 `expand_for_word_values_impl` 两处入口,每词跑一次 apply pass(`SubXpassFrame` + `WordCtxGuard`;brace 路径不再重跑)。
- `expand_word` 整词臂以 `braced_parameter_spans_whole_word` 设门(此前 naive strip 把首 `${` 与末 `}` 配对,多片段词破 #1169 冻结——亦影响 argv)。
- `scan_word_prefix_quote_state`(parameter_words.rs)追踪 `<(...)` / `>(...)` procsub 帧:父级永不绑定 procsub 体内的 `${...}`(既存 argv 泄漏 P9)并消费 in-double 臂的收尾 `"`。

GNU 锚点 execute_cmd.c:3686/3014、subst.c:13219/13062/9777/10346/7966/6362/6412。

## 本 PR 删除了什么

无删除。5 文件 +500/−28:两处入口 apply pass + 整词臂设门 + procsub 帧追踪 + 24 格钉子。未动 `src/lexer/continuation.rs`、`docs/api-ledger.md`。

## 队长复核(`~/workspace/review-3045/verify.log`,全腿 PASS)

- 钉 24/0;fail-first(真基底 `a7d3176f` + 仅钉文件)5p/19f,失败名集恰为 19 个绑定格。
- lib 头 1202/0/4、基底 1198/0/4,差值恰为新增 4 个 scanner_tests;邻茎 21 茎 168/0。
- `RUSTFLAGS="-D warnings"` rc=0;fmt/hygiene/CR 净。
- GNU 探针 14 SAME;残余 P12(DQ 反斜杠 case 主词丢反斜杠)已立 #3084(门 #2968),P14(tilde+param case 主词)队长探针 SAME,不立案。
caomengxuan666 added a commit that referenced this pull request Nov 8, 2026
…#2819)

GNU subst.c string_extract_verbatim (subst.c:1148) decides separator
membership in the current locale: whole characters via wcschr only
when the locale is multibyte; with MB_CUR_MAX == 1 membership is the
per-byte ifs_cmap setifs builds, so a multibyte IFS character
separates on each of its bytes. field_split_values_with_ifs now
routes non-multibyte-locale splits (past the locale-invariant
unset/empty/default-whitespace branches) to a byte-domain twin that
decodes value and IFS to bytes with glue provenance, tests membership
per byte, mirrors the whitespace/mixed/non-whitespace field shapes,
and re-encodes fields to storage text. read follows through
read_split.rs: the scalar and non-raw -a value paths run the existing
logical-unit walk on a byte-domain rewrite of line and IFS (every
byte >= 0x80 becomes a raw-byte marker pair, so one logical unit is
one byte) and normalize the produced values back. The raw -a path
slices the caller's line by ranges and cannot represent mid-char
byte boundaries; it is filed as a follow-up gated on PR #2968, whose
live file table owns read_helpers.rs.
caomengxuan666 added a commit that referenced this pull request Nov 9, 2026
test: wave-2 pins W2b (Refs #3046, #3089)

钉先行:为两单各落一枚回归钉,仅新增测试文件、零产品代码改动。两单在本 PR 中只作引用,修法另行派车道(门控 PR #2968 合入),不在本 PR 处理完毕。

- `tests/issue3046_exec_pipeline_chardev_stdin.rs`(+309/−0):15 格——管道位 `exec` 读字符设备 stdin 塌陷(urandom/head/dev-zero/三级链/comsub mktemp 生产死亡形)+ stderr 重定向丢弃两形,余为子 shell 位/顶层/无 exec/普通文件诚实对照。
- `tests/issue3089_redirect_target_quoted_space_join.rs`(+279/−0):13 格——redirect 目标的引号空格段与替换段拼接被误报 ambiguous redirect(issue 三形 + 反序拼接、非空无空格 param、写读链、追加链、输入形、fd 链),余为纯引号对照与三形真 ambiguous 负对照。

## 本 PR 删除了什么

零删除。本 PR 为纯新增:两枚钉文件合计 +588/−0,不删任何文件、任何代码行,无行为变更。

## 验证(队长复核,详见 review-3173/CAPTAIN-VERIFY.md)

- A0 diff 审计 PASS:恰两文件纯新增、零 `src/` 改动;提交链 `23d0d5be` → `269060f5`,头 `269060f5` / 基底 `f17644d7` 经 gh 亲核。
- B-3046 实跑(基底产品代码):6 绿 / 9 红,逐格定性完毕——9 红全归因于该单两条缺陷机制(管道位 stdin 字符设备塌陷、stderr 重定向丢弃),6 绿均为诚实对照/回归锁(其中 2 格为混合格,红仅源于脚本内 `2>/dev/null` 同被丢弃,stdin 面行为正确)。
- C GNU 抽查 PASS:GNU bash 5.3.20 与基底二进制同跑 6 格,钉期望逐格与 GNU 一致,无一格钉错。
- D 卫生 PASS:两钉文件 rustfmt --check 干净、无 CR、E7 marker lint OK。
- E CI PASS(仅编译面):10 pass + 1 skipping;按纪律钉执行证据以本地实跑为准。
- B-3089 实跑(基底产品代码,补跑 attempt 6):4 绿 / 9 红,恰合预判——9 缺陷格全红、形态一致(拼接目标被误报 `ambiguous redirect`、rc 1、无文件建成;链格的 cat/fd 报错均为同一拒绝的下游级联),4 对照格全绿(纯引号对照 + 3 真 ambiguous 负对照)。此前五次尝试作废于 2026-11-09 重启后写墙(零测试执行);第六次于探针电池 v2 三轮全 PASS(跨度 15 分钟)后实跑落地,逐格表与逐次账本见 CAPTAIN-VERIFY.md 的 B-3089 节。总判 PASS。
caomengxuan666 added a commit that referenced this pull request Nov 9, 2026
…#2819)

GNU subst.c string_extract_verbatim (subst.c:1148) decides separator
membership in the current locale: whole characters via wcschr only
when the locale is multibyte; with MB_CUR_MAX == 1 membership is the
per-byte ifs_cmap setifs builds, so a multibyte IFS character
separates on each of its bytes. field_split_values_with_ifs now
routes non-multibyte-locale splits (past the locale-invariant
unset/empty/default-whitespace branches) to a byte-domain twin that
decodes value and IFS to bytes with glue provenance, tests membership
per byte, mirrors the whitespace/mixed/non-whitespace field shapes,
and re-encodes fields to storage text. read follows through
read_split.rs: the scalar and non-raw -a value paths run the existing
logical-unit walk on a byte-domain rewrite of line and IFS (every
byte >= 0x80 becomes a raw-byte marker pair, so one logical unit is
one byte) and normalize the produced values back. The raw -a path
slices the caller's line by ranges and cannot represent mid-char
byte boundaries; it is filed as a follow-up gated on PR #2968, whose
live file table owns read_helpers.rs.
caomengxuan666 added a commit that referenced this pull request Nov 9, 2026
第二波钉先行车道 B1:只落回归钉与根因/修法设计,**不含产品代码**。两单的修法面被在飞 PR #2968 占着,门开后另派修法车道按本 PR 的钉与设计动手。

## #2634

Refs #2634

`tests/issue2634_extdebug_bash_argc_sticky.rs`(13 格):extdebug 开→关后 BASH_ARGC 底层帧粘性缺失。

GNU 定案:底层帧由 `init_bash_argv` 一次性闩锁(variables.c:5640-5650),`shopt -s extdebug` 经 builtins/shopt.def:618-626 `shopt_set_debug_mode` 触发;`shopt -u` 不清闩。rubash 实现了函数入口(function_calls.rs:616-653)与顶层读(dynamic_arrays.rs:1399-1420)两个触发点,唯独缺 shopt 开启触发点(builtins/shopt.rs:671-675 只同步 functrace/errtrace)。期望值全部取自 GNU bash 5.3.20 oracle 实测,缺陷格与回归锁分开标注。队长复核 fail-first 独立重跑:基底 6 红 / 7 绿、头树 6 红 / 7 绿,红恰缺陷格、逐格与车道实测一致(详见复核记录)。

## #2641

Refs #2641

`tests/issue2641_let_readonly_redirect.rs`(12 格):`let` 对只读变量的报错绕过命令级 stderr 重定向。

GNU 定案:`let` 赋值经 expr.c:331-349 `expr_bind_variable`,只读诊断由 error.c:455 `report_error` 在重定向已施加后写向当前 fd 2。rubash 的算术只读 reporter(src/executor/arithmetic/mod.rs:1464-1481)用裸 `eprintln!` 直写进程 stderr,绕过 executor 的重定向管线——三个求值入口(963/1161/1427)共用此一汇。期望值取自 GNU bash 5.3.20 oracle 实测;缺陷格覆盖 `2>&1`/`2>file`/`2>/dev/null`/管道/子 shell/函数内/只读数组元/链形,回归锁覆盖无重定向/普通赋值致命形/eval 对照/除零对照。队长复核 fail-first 独立重跑:基底 8 红 / 4 绿、头树 8 红 / 4 绿,红恰缺陷格、逐格与车道实测一致(详见复核记录)。

## 本 PR 删除了什么

无删除:纯测试新增,零产品代码改动。净行数 +603 / −0(`tests/issue2634_extdebug_bash_argc_sticky.rs` +238、`tests/issue2641_let_readonly_redirect.rs` +365)。

车道 REPORT(含逐单根因、fail-first 逐格结果、修法设计):`~/workspace/fix-B1/REPORT.md`(本地,随交付信交队长)。
caomengxuan666 added a commit that referenced this pull request Nov 9, 2026
test: wave-2 pins W2d (Refs #3066, #3084, #3097)

第二波「钉先行」车道 W2d 的回归钉(基底 master `f17644d7`)。三单的修法面均被在飞 PR #2968 的文件表占住(declare/assign.rs、compound_exec.rs、temporary_assignments.rs),本 PR 只落钉 + 根因/修法设计(见车道 REPORT),门开后修法车道按钉施工。

Refs #3066 — declare 通道 locale 漏斗触发次数:`declare -a`/`declare -A` 转换既有标量、复合绑定、`local` 复合绑定在 GNU 均双响(declare.def:1105 的 per-variable hack),rubash 只响一次。钉文件 `tests/issue3066_declare_conversion_locale_funnel.rs`(含 fresh/有效值/标量 declare 对照与链路读回)。

Refs #3084 — DQ 反斜杠 case 主词:`case "${v=a\ b}"` 主词在 GNU 保留反斜杠(nomatch),rubash 丢失(match)。钉文件 `tests/issue3084_case_word_dq_backslash.rs`。

Refs #3097 — locale 漏斗三残余:元素赋值与 for/标量覆数组绑定不喂漏斗(存储臂早退跳过 note);declare 通道漏斗投影判据(attr_of)与标量读路径(标记集)不同源,警告命名原始存储形。钉文件 `tests/issue3097_locale_funnel_array_channels.rs`(含 #3080 移出的 3 格原文断言)。

队长复核:A0 纯钉审计、C 腿 GNU 5.3.20 抽查(20 探针全吻合)、D 卫生(rustfmt/marker-lint 均净)、E CI 10 success + 1 skipped 均已落;B 腿 fail-first 实测:基底 5 绿/17 红(#3066 2 绿/6 红、#3084 3 绿/3 红、#3097 0 绿/8 红),头树逐 fn 完全相同(详见复核记录)。

## 本 PR 删除了什么

无(纯测试新增)。净行数:+720 行(3 个新钉文件:`tests/issue3066_declare_conversion_locale_funnel.rs` +263、`tests/issue3084_case_word_dq_backslash.rs` +150、`tests/issue3097_locale_funnel_array_channels.rs` +307;删除 0 行)。

注意:master 已推进到 `a432632d`(#3098 合入),合入前须 update-branch。
caomengxuan666 added a commit that referenced this pull request Nov 10, 2026
) (#3124)

type: interleave per-operand stdout/stderr in merged streams (Refs #2699)

多操作数内建在合并流(2>&1、同一文件、同一捕获)里的 stdout/stderr 交错
丢失:共享缓冲输出路径把两流分别收进两个 Vec、命令结束时整段先发 stdout
再发 stderr。GNU 按操作数逐个发出(builtins/type.def describe_command
每名一次、诊断走 unbuffered 的 builtin_error),故
`type echo nosuchcmd123 ls 2>&1` 的诊断行应落在两个 describe 行之间
(GNU bash 5.3.20 oracle 已验)。

修法(机制层):给共享缓冲路径增加有序 chunk 通道——
trap_stack_builtins.rs 新增 OrderedOutputLog / OrderedChunkWriter
(两流的 Write 句柄把每次写按序记入同一日志,fd 标签 + 字节)与
write_buffered_builtin_output_chunks;redirection.rs 新增
route_builtin_buffered_output_chunks 在一次重定向应用内按记录序逐 chunk
写出;type_builtin.rs 的 type / command -v/-V describe 入口改走有序通道,
execute_type_with_io 逐名 flush 诊断;既有 pair 路径退化为 2-chunk 情形、
行为不变,command fallback 路径保持 Vec + pair flush。
原 FAIL 点(fallback 分支 stdout/stderr 声明缺失,E0425)由恢复提交补回
两行声明消除。

## 本 PR 删除了什么

无删除。declare -p 与 alias 的同机制转换因其入口文件正被 #2968 改写,
不在本 PR,另立残余单待其合入后跟进。

## 验证(批跑 #3 B10 在与合入头逐字等价的树上实测,队长复核卷
## review-3124/CAPTAIN-VERIFY.md)

- 基底 fail-first:钉 7 格 2 绿 / 5 红,红格恰为 5 个交错格。
- 头钉 issue2699_builtin_output_interleaving 7/7;lib 头 1206/0/4 失败集空
  (基底锁定红 issue2875 在头上亦绿,⊆ 基底集)。
- 邻茎六茎全绿:issue1081 1/0、issue1047 4/0、issue1172 3/0、
  issue1648 4/0、issue1080 2/0、a3_r58b 2/0。
- -D warnings rc=0;cargo fmt --check rc=0;marker lint PASS;
  头构建 rc=0(二进制 120,667,032 B)。
- GNU 探针 byte-SAME;残余探针 declare -p HOME nosuchvar123 PATH
  与 GNU 一致(rc=1,stderr 仅二进制名一词之差)。

Refs #2699
@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 10, 2026 08:16
# Conflicts:
#	src/builtins/alias.rs
#	src/builtins/declare/print.rs
#	src/executor/declare_local.rs
#	src/executor/temporary_assignments.rs
caomengxuan666 added a commit that referenced this pull request Nov 10, 2026
…3262)

Refs #3089

## 问题

重定向目标词把一个含 IFS 空格的引号段与一个替换段拼起来时,rubash 误报 `ambiguous redirect`,而 GNU bash 按单字段语义建/读拼接出的文件。例:`> 'a b'$(printf '')`、`> "a b"$V`(V 空)、`>> 'a b c'${V}`(V 空)。

根因(GNU 锚点):GNU 的判词在 `redir.c:298 redirection_expand`——目标词展开后走 `subst.c:12397 word_list_split` 做 IFS 字段切分,字段数 ≠ 1 才报 ambiguous;引号段产出在 `subst.c:4773 quote_string` 经 `subst.c:11862 add_quoted_string` 得到保护,永不被切分。rubash 此前的判词对带标记的展开不做字段切分,而是对剥除载体后的文本做空白扫描,于是引号内的 IFS 空格被误判。

## 改法(机制级,无形状特判)

- `src/executor/expand_word.rs`:`expand_redirect_target` 对带标记的展开改走与参数管线同一个 IFS 字段切分器(`field_split_values_with_ifs`),以字段数作判词。信任条件二选一:(a) 标记展开带保护载体(经 markers.rs 的 pair-aware 助手 `contains_bare_sentinel` 判定,E400 对内成员是数据不是协议);(b) 原始词扫描证明不存在引号内 IFS 字符、也不存在引号内展开点——自定义 IFS 可使引号内空格成为普通数据、永不落载体(如 `IFS=:` 时空格非 IFS 字符),此时仿真标记本就无可保护,计数字段仍是 GNU 语义。信任且切分恰得一字段时,该字段即文件名——替换结果尾部的 IFS 空格按字段语义处理,不再残留在文件名中(`V='x '` 时 GNU 的单字段为 `a bx`)。
- `src/executor/redirection.rs`:`>(...)` 进程替换的同位点同步采用该判词(两行)。
- `scripts/marker-lint-allowlist.txt`:一处裸剥除按仓库既有形态登记为 owner restore chain——依据是 markers.rs `restore_bare_sentinels` 文档的不变量:带类型的 C0 字节永不以裸 C0 字符到达(恒以 raw-byte 标记对承载),故对 C0 载体的裸剥除不可能折入用户数据。其余两处裸字面消费已改走 pair-aware 助手。

## 本 PR 删除了什么

无删除。仅新增字段切分判词、两个模块内扫描助手与一条 allowlist 登记;被替代的文本扫描判词保留为无标记词的回落路径(其语义与基底一致,见基底对账)。

## 验证(Simular box,基底 `beab07dc`,按逐茎 test result 行与失败名集判读)

| 腿 | 基底 | 头 `62e46bb8` |
|---|---|---|
| 本钉 20 格 | 6 过 / 14 红(失败名集与立案登记逐名全等 = 误报面本身) | **20 过 / 0 红** |
| 邻茎 1769 / 1673 / 2999 / 1020 | — | 11/11、3/3、15/15、4/4 |
| lib 双边 | 1207 过 / 0 红 / 4 ignored | 1207 过 / 0 红 / 4 ignored(失败集双边皆 ∅) |
| `-D warnings --all-targets --keep-going` | rc=101,16 个存量测试面目标 | rc=101,错误集与基底逐项全等(同 16 个,零新增;`^error` 计数 32 = 32) |
| `cargo fmt -- --check` | — | rc=0 |
| marker-lint | 基底 rc=0 | rc=0(117 registry consts,265 allowlisted sites) |
| `cargo check --tests --target x86_64-unknown-linux-gnu` | — | rc=0 |

darwin/msvc 跨目标在验证箱上不可判(箱缺跨目标 C 工具链,基底同样失败),由 CI 判读:头提交 11 个 check-runs 全部收官(10 success + 1 skipped),三条 cross-target 腿皆 success。

队长复核(自有纯净克隆、四文件 sha256 双核、逐腿亲跑):八腿全过,判词见 `~/workspace/review-3262/CAPTAIN-VERIFY.md`。基底 lib 首跑两度遇箱上 OOM SIGKILL(无 test result 行,按 VOID 作废),重跑干净收官 1207/0/4。

## 门禁与漂移

- §0 硬门复核:PR #2968 头文件面恰 42 文件,不含 `expand_word.rs` / `redirection.rs`,本面 NOT gated。
- 主干漂移:补丁对已合入 PR #3144 的现行 master 做 `git apply --check` 通过(#3144 的 redirection.rs 六处与本补丁的判词条件零重叠)。

Refs #3089
caomengxuan666 and others added 5 commits November 10, 2026 17:56
…pt spec, flag fast scan, lazy print_names snapshot (Refs #2707)
…-funnel resolution

Conflict resolution, 2 files / 3 blocks, all on the declare face; sole
source master 504c6e8 (PR #3242, rubash#3066/#3097 LocaleFunnelRecord)
landing after this branch's resolution base d4f7d94. The funnel
semantics are re-landed in post-migration (Diag/prolog) form; neither
side is taken wholesale.

- declare/assign.rs: LocaleFunnelRecord struct and its doc kept
  verbatim from master; assign_declare_names keeps the migrated
  signature (diag/prolog, no <W: Write>/stderr) and gains the
  locale_funnel out-parameter. The two note() sites sit exactly on the
  assign pass's sv_locale fire points (variables.c:6272), and the
  #3097 mark-alignment (attribute bits settled before the funnel reads
  the projection, variables.c:2495) is preserved.
- declare.rs call site: HEAD's gate kept. GNU
  builtins/declare.def:389-406: the pflag branch runs before all
  making-variables machinery, so under -p the assign stage is skipped
  outright and mark_unset_declarations stays literal true; master's
  !print-as-argument form would run assign under -p and resurrect the
  E1 defect (`declare -p dv=6` binding). The record is created before
  the gated call and passed in; under -p neither the assign pass nor
  the verdict loop (same !print block) runs.
- declare.rs hack verdict: master's per-operand loop kept (GNU
  builtins/declare.def:1105 stupidly_hack_special_variables runs after
  every processed variable); its diagnostic is re-landed in migration
  form as bare_diag_line(diag, prolog, ...), the same rendering the
  two assign-phase fire sites already use, instead of master's
  writeln!(stderr, diagnostic_prefix ...).

Refs #2707
…nt-log resolution

Master (PR #3244) folded the standalone arithmetic readonly reporter
into the ArithEvalEvent drain; its Readonly arm emitted through a raw
eprintln!, the pre-migration routing A3-R1 had already moved off.
This merge accepts the drain structure and the reporter's deletion,
and re-lands the A3-R1 fd-2-bound routing (arith_diag =
write_diagnostic_fd2; GNU error.c:453 err_readonly, report_error
family) inside the drain's Readonly arm. Marker-by-name consumption,
the reporter latches and the drain's evaluation order stay master's,
verbatim.

Refs #2707
…giene guard

The byte-scan twin of the declare option machine (scan_flag_only_spec)
pushes an option byte only after letter_bytes.contains(&c) succeeds,
where letter_bytes is the row spec's own letter bytes — the static
Getopt literal, all ASCII. The widened byte is therefore ASCII by
construction and can never be a UTF-8 continuation byte, the same
guard basis as the machine-path entry already allowlisted for this
file. The invalid-byte half of the same scan stays unlisted: it comes
from user argv and is rendered lossy via String::from_utf8_lossy.

Disposition form follows the existing precedent for reviewed-safe
widenings in this guard: an allowlist entry with the guard basis
recorded, its regex anchored on this site's distinct tail so a
different widening in the same file still trips the check. No product
code changes; the scan's semantics are untouched.
@caomengxuan666
caomengxuan666 merged commit d955590 into master Nov 10, 2026
10 of 11 checks passed
caomengxuan666 added a commit that referenced this pull request Nov 10, 2026
…3288)

算术 nameref 报错入 ArithEvalEvent 单日志(rubash #3246)

空 cell nameref 在算术里被赋非名字形值时,GNU 在绑定点即时发 `` `1': not a valid identifier ``。本 PR 让该报错进入求值序事件日志(沿 #3244 的 ArithEvalEvent 单日志机制),取代边界后补发的单槽 marker 报告器:

- `src/executor/arithmetic/value.rs`:空 cell 非法值分支改调 `record_diag(ArithEvalDiag::InvalidIdentifier(value))`(绑定点入日志,一绑定一事件)。
- `src/executor/arithmetic_aliases.rs`:删除 `report_arithmetic_nameref_error` 整体及 `((` / `let` 两处调用点;状态改由表达式值决定;`let` 循环续评后操作数。
- `src/executor/arithmetic/mod.rs`:`print_arith_diag` 三臂由裸 `eprintln!` 改走 fd-2 绑定通道 `self.arith_diag(...)`(与 #2968 合入的 Readonly 臂同路由,三臂全走 `arith_diag`);`eval_arithmetic_expansion_value` 内删报告器调用。
- `src/executor/expand_braced_indices.rs`:`apply_pending_subscript_writes` 开头加 `drain_arith_eval_events(None)`(无条件执行;下标求值经 `&self` deferred 路径只评一次且被 memo 化,mut 入口永不重评,故事件无排口;该落点是同次求值的写落地边界,标签 None)。
- `src/executor/markers.rs`:删除 `__RUBASH_ARITH_NAMEREF_ERROR` 注册行。
- `tests/issue3246_arith_nameref_eval_order.rs`:新钉 18 格(五形 + 裸状态、双 nameref、let 状态、展开值、下标两形、cond 操作数、fd2 两形)。

## 本 PR 删除了什么

- `report_arithmetic_nameref_error` 函数整体(`src/executor/arithmetic_aliases.rs`)及 `((` / `let` 两处调用点。
- `__RUBASH_ARITH_NAMEREF_ERROR` 单槽 marker 注册行(`src/executor/markers.rs`)。
- 空 cell 非法值分支的 marker 插入(`src/executor/arithmetic/value.rs`),改为事件日志记录。
- 上述删除均由 ArithEvalEvent 单日志机制承接,无行为真空;全树对 marker 字面与报告器名零残留。

## 队长复核(`~/workspace/review-3288/CAPTAIN-VERIFY.md`)

- 头钉 18/18;基底 fail-first 2 过 / 16 红(逐名核对一致);邻茎 issue3078 19/19 双边;lib 双边 1233/0/4 名集 ∅=∅(双树先建 `--bin rubash`——基底亦全绿,车道账面「基底 1232/1/4 唯一红 issue2875」为未建 bin 的环境产物,已推翻)。
- 五腿 rc=0:`RUSTFLAGS="-D warnings"`(lib+bins,touch + `-v` 亲证 rustc 真载 flag)、fmt 全树、marker-lint(117 注册常量、267 allowlisted sites)、UTF-8 边界卫生、`cargo check --tests`(32 条警告全在既存他茎测试文件,本 PR 文件零警告)。
- GNU oracle:车道 7 份探针 6 份全同,唯 gnu-extra.sh stderr 差 2 处(sub1 子串偏移标签 + sub5 一对);复核员 6 份 adversarial 探针三方对照(GNU/基底/头树):S1/S2/S5 与 GNU 逐字节一致,S4/S6/S7 仅标签一格有差(基底五形更差——缺行/错序/静默/状态错;头树全部修净缺失与错序,只剩标签,见残差)。
- CI:10 pass + 1 skipping,零红;merge-tree 对当前 master 零冲突。
- 三点裁断:(a) apply 点 drain 四调用面确无入口窗口内调用;(b) #3287 与本 PR 合入脱钩(标志泄漏机制基底与头树逐行相同,本 PR 未制造);(c) `print_arith_diag` 合并形态干净。
- 残差(均不阻塞,均须另立案):① 子串偏移标签 `${v:ref=1:2}`(GNU 带 `v: ` 标签、头树无;归宿 subst.c 报告路径车道);② 算术文本内展开期诊断标签(S4 嵌套 `$(( ))`、S6/S7 下标形:头树打 `((:` 标签、GNU 无标签;既存分歧收窄后的残余,修法须另案调查两生产点)。

Refs #3246
caomengxuan666 added a commit that referenced this pull request Nov 11, 2026
…-surface cells (#3304)

Merge `refs/pull/3304/head` (`e47ee03b`) — five additional binding-surface cells for the `let` readonly diagnostic routing pin. Refs #2641.

## What it adds

- `tests/issue2641_let_readonly_redirect.rs` gains five cells (+84/−0, no existing cell touched, no product code): `comsub_capture_redirect`, `function_callsite_redirect`, `compound_assign_redirect`, `unquoted_operand_redirect`, `multi_operand_redirect`. They pin binding surfaces the twelve cells that landed with #2968 do not reach: the command-substitution capture pipe, a redirection placed on the function call site, compound assignment, an unquoted operand, and multi-operand continuation after a failing first operand.

## Verification (captain re-run, local build slot; base = master `5a0aa934`)

- Each new cell's shape was measured against the self-built GNU bash 5.3.20 oracle before acceptance: stream placement, exit status and variable values match the pin's assertions on all five shapes.
- Head pin `issue2641_let_readonly_redirect`: **17 passed / 0 failed** (12 pre-existing + 5 new; each new cell reported individually in the run log).
- Neighbours: issue2707 18/18, issue3078 19/19, each with its own `test result` line.
- Lib bilateral: head 1233 passed / 0 failed / 4 ignored; master `5a0aa934` 1233 passed / 0 failed / 4 ignored, and the stock pin on that master is 12/12. Failure name sets identical (empty on both sides).
- `RUSTFLAGS="-D warnings"` on the pin test target rc=0 (flag load proven from the `-v` log: the target's rustc invocation carries `-D warnings`); `rustfmt --check` on the pin file clean; marker-lint OK; UTF-8 boundary hygiene clean.
- Test-only diff: no product code and no `libc::` anywhere in it, so the cross-target legs are not triggered.

## Notes for the record

- The underlying issue is already closed on the regression-lock evidence. The repair itself landed with #2968; this PR only extends the pin so the adjacent binding surfaces cannot regress silently.

## Deleted

Nothing.
caomengxuan666 added a commit that referenced this pull request Nov 18, 2026
…cwd (Refs #3090) (#3114)

* test(bind/history): pin relative file operands resolving at the session cwd (Refs #3090)

tests/issue3090_relative_operand_session_cwd.rs: 9 cells — cd-then-
relative bind -f (incl. $include), history -r/-n (operand and
$HISTFILE default), -w/-a landing in the session cwd, plus
absolute-operand and diagnostic-spelling guards.

GNU: builtins/bind.def:274 (rl_read_init_file opens the operand
verbatim); builtins/history.def:273,295-316 (the filename is handed
verbatim to read_history / write_history / maybe_append_history).

On pristine base 95ba591 the pin reads 4 passed / 5 failed: the 5
defect cells red, the 4 guard cells green.

* fix(executor): anchor bind -f / history file operands at the session cwd (Refs #3090)

Both builtins opened a relative filename against the process cwd.
GNU's single cwd is the session cwd (builtins/bind.def:274;
builtins/history.def:273,295-316), so after `cd` the operand missed
files sitting in the current directory, and `history -w` silently
created its file in the process cwd instead.

Route both faces through the one G-02 anchor: the new
executor::path::anchored_file_operand composes the spelling
translation with anchor_at_session_cwd (the lookup/cd anchor, now
pub(crate)); bind -f and its $include reads (lib/readline/bind.c:1502
parser_include) and the history -a/-n/-r/-w opens use the anchored
form. Diagnostics keep the as-typed spelling, and the empty operand
passes through so its open keeps GNU's ENOENT shape.

The host-provider history face in job_builtins.rs is NOT touched:
that file is in PR #2968's table (in flight) — see the lane REPORT
for the residual.

This PR deletes nothing: no code removed; the change is +52/-7
across path.rs, bind_builtins.rs and history_exec.rs, plus the
196-line regression pin in the preceding commit.

* style(test): rustfmt the issue3090 pin (Refs #3090)

---------

Co-authored-by: 曹梦轩 <[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