Skip to content

fix(local): walk local -p operands once, in order (Fixes #3529) - #3548

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/3529-local-p-operand-order
Nov 15, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/3529-local-p-operand-order

Conversation

@caomengxuan666

@caomengxuan666 caomengxuan666 commented Nov 14, 2026 •

Copy link
Copy Markdown
Member

修了什么

local -p NAME... 改为 GNU 的单趟、按操作数顺序处理:每个操作数逐字查「当前变量上下文的局部变量」,找到就打印(option 快照局部打印 local -),否则报 not found 并令命令失败。由此对齐:

  • local -p nope1 -x:修前漏报 -x(预扫后无剩余普通名就跳过核心调用),现报 nope1、-x 两行,rc 1。
  • local -p x --help:master 上已一致,钉住防回退。
  • local -p nope1 nope2 -x nope3 / local -p a -x b nope:修前缺失的普通名被提前到所有诊断之前(stderr 顺序错),现按操作数顺序。
  • local -p nope +x --:修前 +x、-- 被吞,现逐个报 not found。
  • local -; local -p x - x:修前 local - 总是最先打印,现按位置打印。
  • local -g -p x:修前 -g 让核心看到全局值(打印 x="g"),GNU 的 show_localname_attributes 不看 -g,现打印帧内局部 x="1"。

机制:execute_local 新增 print 路由(-p 且有操作数时进入)execute_local_print_operands:按 local_operands 取选项区之后的后缀,逐个判定;连续的找到名作为一段,经同一个 declare 打印核心(execute_with_io_named_in_context)渲染,因此 stdout/stderr 各自保持操作数顺序。

删了什么

execute_local 通用路由里的 -p 预扫(print_missing 收集 + 先行报错 + args.retain 过滤)及其在两处 status 计算里的分支。无操作数的 local/local -p 列表路径不变(其改写出的名字全部来自当前帧,预扫本来就无事可做)。

GNU 依据

  • builtins/declare.def:389-406 declare_internal():if (pflag) 分支 for (...; list; list = list->next),local_var 时调 show_localname_attributes (list->word->word, nodefs),返回非 0 则 sh_notfound + any_failed++。
  • builtins/setattr.def:555-572 show_localname_attributes():find_variable_noref (name) 逐字查找,local_p (var) && var->context == variable_context 才打印;- 打印 local -。不读 -g。

验证

  • 新钉 tests/issue3529_local_p_operand_order.rs(7 格,期望全部取自 WSL GNU 5.3.20 脚本文件实测,stdout/stderr 分开断言):修前 2 passed / 5 failed,修后 7/7。
  • 11 函数探针 lpall.sh(覆盖上列全部形状):修后 rubash 与 GNU stdout、stderr 逐字相同(stderr 仅路径前缀归一)。
  • 邻接 stem:issue1471 1/1、issue1719 7/7、issue2582 4/4、issue2720 3/3、issue2753 20/20、issue2770 3/3、issue2944 11/11、issue3338 8/8、issue3344 14/14、issue3345 21/21、issue383_posix_roundtrip 9/9、issue529 8/8、issue530 16/16、issue970 14/14;cargo test --lib local 30/30,--lib declare 7/7。
  • 以下 stem 在 origin/master 已红,修前修后失败集相同(非本 PR):issue0077_…_declare(issue189/issue272 两格)、issue2424(21/21 全红)、issue2972(declare_plus_x + common::issue2197 两格)、issue2973 / issue2974(仅 common::issue2197 两格)。
  • upstream varenv.tests(含 varenv25.sub 的 local -p;拷贝 tests 目录运行,diff --strip-trailing-cr):修前 17 / 修后 17 差异行,输出逐字节相同(17 行为 master 既有残差:assoc 顺序、varenv19 readonly、global:bcde 组)。
  • fmt:本机 cargo fmt --check 因 Windows 命令行过长(os error 206)无法运行,以 rustfmt --edition 2021 --check 检查本 PR 两文件干净;hook 同样被 206 卡死,提交时走了其「cargo 不在 PATH 则跳过」分支——如实记录。

Owed

Fixes #3529

…ame_attributes (Refs #3529)

GNU builtins/declare.def:389-406 (declare_internal, pflag branch with
local_var) calls builtins/setattr.def:555 show_localname_attributes on
each operand word verbatim, in order: a local of the current variable
context prints (`local -` for the option-snapshot local), every other
word is `not found` and fails the command; -g does not enter the lookup.

execute_local used a pre-pass that reported missing plain names first,
then skipped the declare-printer call whenever no plain name survived,
so `local -p nope1 -x` never reported `-x`; it also printed `local -`
ahead of every other operand and, under -g, unshadowed the frame local
so the printer showed the global. A dedicated print route now walks the
operands in order and renders each run of found names through the same
declare printer; the old pre-pass and its status plumbing are removed.

Generated with [Devin](https://devin.ai)
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】

PR #3548 第1轮 — 建议:PASS
head: 1235ee7 base(merge-base): 8b48c16 文件: 2 个
关联 issue: #3529(状态:open,我核过的时间:2026-11-15 本地)

机制核查:

  • 新增 local -p NAME... 专用分支 execute_local_print_operands(src/executor/declare_local.rs):operands 一次按序走,对每词 verbatim 判 innermost 帧 local(scope.contains_key)——对应 GNU builtins/declare.def:389-406 pflag+local_var 分支逐个调 setattr.def:555 show_localname_attributes(local_p && var->context == variable_context)。连续命中名经一次 declare-printer 调用输出,命中 - 打 local -,其余(缺失名、选项形词 -x/--help/+x/--、赋值形 x=1、下标词 a[0])报 local: WORD: not found 且 status=1;-g 不进查找。
  • 删除旧 pre-pass(先报缺失名再决定是否走 core),该旧路正是 local -p nope1 -x 漏报 -x、- 提前打印、-g 打全局的根因。
  • 可疑/无关改动:无。

腿:

  • 钉自跑:issue3529_local_p_operand_order head 上 7/7。
  • fail-first:base 8b48c16 上 2 passed / 5 failed,红名 = {dash_local_prints_in_operand_order, global_flag_does_not_change_the_frame_lookup, missing_name_then_option_shaped_word, missing_names_and_option_word_keep_operand_order, plus_and_double_dash_operands_are_names};对照绿 {found_name_then_help_word, valued_and_subscripted_words_are_looked_up_verbatim}——红名与修复面一致。
  • lib 双边:head {api::consumer_tests::issue2084_session_end_restores_host_process_state} / base 同 / 差集 ∅。
  • -D warnings:errors 集中在 tests/issue1657_tilde_passwd_contract_regressions.rs——不在本 PR diff,基线存量。
  • 卫生:rustfmt --check 两个改动文件全过。
  • CI:3 个红 job 红名单各只有 issue2875_input_procsub_child_runs_as_rubash_shell,全为基线。
  • GNU 对照:WSL /usr/local/bin/bash 5.3.20(1)-release,探针 target/audit-w2/probe3529.sh 六组:nope1 -x 两词按序各报 not found rc=1;x --help 先打 declare -- x="1" 再报 --help;a -x b nope 中 a/b 合批输出、-x/nope 按序报错;x - x 输出 declare/local -/declare rc=0;x=1/a[0] verbatim 报错、a/x 正常打;-g -p x 仍打帧内 local rc=0——两侧逐流内容一致(stderr 在合并流的交错顺序不同,属仓库已知缓冲限制,钉按流断言)。

F 点:无

未跑/VOID 的腿:无(lib base 集复用同 merge-base 实跑结果)。

@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 15, 2026 02:05
@caomengxuan666
caomengxuan666 merged commit a41b359 into master Nov 15, 2026
8 of 11 checks passed
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.

[bug] local -p 两形态诊断仍与 GNU 有差(execute_local 预-pass 机制)

1 participant