Skip to content

fix(builtins): print function definitions in bare declare/set listings (Refs #3487) - #3530

Merged
caomengxuan666 merged 5 commits into
masterfrom
fix/3487-bare-declare-functions
Nov 18, 2026
Merged

caomengxuan666 merged 5 commits into
masterfrom
fix/3487-bare-declare-functions

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Refs #3487

根因

GNU 的无参列表是单一枚举器 print_all_shell_variables(builtins/set.def:506-527):先 print_var_list 印变量,再于非 POSIX 模式下 print_func_list(variables.c:1079-1090)印函数定义。裸 declare 经 declare.def 无参臂(list == 0 且 flags_on == 0)分发到 set_builtin(NULL),与裸 set 共用这张列表。

rubash 侧 print_shell_variables(该枚举器的移植)只印了变量半:函数表在 ShellState.functions,builtin 层看不到。裸 set 此前靠 execute_set 在 builtin 返回后自行 append 函数段(#1056)补上了这一半,裸 declare 没有对应 append——于是同一枚举器的两条入口一全一缺。

修法

按 GNU 的机制面补全枚举器本身,不在 declare 入口再打补丁:

  • print_shell_variables 新增函数半:变量段之后、非 POSIX 模式下(与既有 ansic 引号分支读同一个活 posix 选项),按名序逐个印 FunctionSetView 的多行定义文本。POSIX 门对应 set.def:518-520(PASC Interp declare -f re-serializes from AST instead of replaying definition text: leaks source-context redirect into } (rewrites 2> as 2>>), rewrites nested quotes in $() as \", drops -n in compound [[ ]] #202)。
  • 执行器新增 function_set_views()(与 dynamic_set_views() 同位组合):用 declare -f/type 同一个 ast_print 移植(multiline_function_def_text_with,含 body kind 与定义期重定向元数据)渲染每个函数,四个调用点(execute_set、execute_declare、execute_local 两路)与 dynamic 视图同位供给。
  • 删除 execute_set 的后 append 块:函数半改由枚举器统一打印,两条入口不再分叉(原 append 的 POSIX 门与枚举器内的活选项门等价——__RUBASH_POSIX_MODE 由 set_shell_option 与选项表保持同步)。

GNU 锚点:builtins/declare.def 无参臂(set_builtin ((WORD_LIST *)NULL));builtins/set.def:506-527 print_all_shell_variables;variables.c:1079-1090 print_func_list;variables.c:1152-1162 print_var_function(FUNC_MULTILINE|FUNC_EXTERNAL)。探针以 GNU bash 5.3.20 oracle 干净环境脚本文件复核:函数按名序、位于全部变量之后(含 zzzz_last 变量先于 afunc)、子 shell 体/定义期重定向/keyword 形格式、set -o posix 后函数段消失、declare -p 不含函数定义。

回归钉

tests/issue3487_bare_declare_function_listing_regressions.rs(8 测,随第一笔提交先行):master 8b48c16 上 fail-first 5 过 / 3 红(bare_declare_prints_function_definition_block、bare_declare_functions_sorted_and_after_all_variables、bare_declare_function_shapes);修后 8/8。

本 PR 删除了什么

  • 删除 execute_set(src/executor/alias_set_builtins.rs)中 builtin 返回后 append 函数段的代码块(15 行):其职责由枚举器内的函数半接替,避免两条入口各维护一半。
  • 其余为新增与签名穿线(print_shell_variables / set_with_io 链 / execute_with_io_named_in_context 新增 functions/function_set_views 参数,无状态包装层传空集,行为不变),无其他删除。

UNVERIFIED-BY-CAPTAIN

GNU print_all_shell_variables (builtins/set.def:506-527) prints the
function definitions after the variables for bare `declare` and bare
`set`; rubash bare `declare` omits them entirely. Pin covers the seed
shape, name order after all variables, the bare `set` twin, the POSIX
gate, subshell/redirect/keyword definition shapes, and guards for
`declare -p`, `unset -f`, and `declare -f`.

Fail-first on master 8b48c16: 5 passed / 3 failed
(bare_declare_prints_function_definition_block,
bare_declare_functions_sorted_and_after_all_variables,
bare_declare_function_shapes).
…s (Refs #3487)

GNU print_all_shell_variables (builtins/set.def:506-527) is a single
enumerator: print_var_list, then print_func_list (variables.c:
1079-1090) when posixly_correct == 0. Bare `declare` reaches it via
declare.def's no-operand arm (set_builtin(NULL)), exactly like bare
`set`.

rubash's port, print_shell_variables, printed only the variable
half. Bare `set` masked that with an executor-side append in
execute_set (rubash#1056); bare `declare` had no counterpart and
omitted the function definitions entirely.

Complete the enumerator instead: print_shell_variables takes a
&[FunctionSetView] (rendered by the executor with the same ast_print
port declare -f/type use, metadata included) and prints it after the
variables in name order, gated on the live posix option
(set.def:518-520). The views thread through the set_with_io chain and
execute_with_io_named_in_context from all four executor call sites,
composed beside dynamic_set_views. execute_set's post-append block
is deleted — its POSIX gate read __RUBASH_POSIX_MODE, which
set_shell_option keeps in sync with the option table the enumerator
reads, so the two listings now share one gate and one code path.

Pin: tests/issue3487_bare_declare_function_listing_regressions.rs
8/8 (fail-first 5/8 on master).
@caomengxuan666
caomengxuan666 force-pushed the fix/3487-bare-declare-functions branch from fc9c54f to 689bf01 Compare November 14, 2026 04:07
@caomengxuan666

Copy link
Copy Markdown
Member Author

队长复核 PASS(头 689bf013)

腿(复核员全跑,队长核查原始日志):

  • 钉:头 8/8;基底 fail-first(top-up)5 passed / 3 failed,红格恰为 3 个修复面(bare_declare_prints_function_definition_block、bare_declare_functions_sorted_and_after_all_variables、bare_declare_function_shapes),守卫在基底上全绿。
  • lib 双边(bin-first):头 1247/0/4 vs 基底 1247/0/4,失败名集差 ∅;issue2875 在两边本地都过——CI 上的红是环境敏感,非产品信号。
  • RUSTFLAGS="-D warnings":rc=0,0 warning(强制重编,非空闲)。
  • GNU oracle:8/8 形与 GNU bash 5.3.20 一致。
  • 卫生 clean(唯一 fmt 漂移在 PR 未碰的文件里,既有)。
  • CI run 43555090896:三红唯一格 issue2875(基线)。

合入:Refs #3487(合后队长手关 #3487,随后派 #3494)。合入顺序待定:与 #3548 的 hunk 相邻,须等用户定合入顺序后,合入轮再 update-branch 至 live 尖 + 合并形重验。

@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-BATCH-SUMMARY】 h3 车道批次汇总(#3487 审 #3530 / #3494 / #3502 全部处理完;判词均为建议,合入/关单由队长裁定)

项 载体 建议判词 说明
#3487 本 PR(head 689bf013,未动分支) 建议 PASS 独立机制复核:diff 将函数段收敛进 print_shell_variables 单枚举器(GNU print_all_shell_variables set.def:506-527 = print_var_list + posixly_correct==0 时 print_func_list variables.c:1079-1090),删 execute_set 侧补丁;declare -p 另一枚举器不受影响;local/typeset 抽验与 GNU 一致。队长 2026-11-14 复核 PASS 腿已全绿。
#3494 新 PR #3854 head 9de6ded8d 建议 PASS(待复核) declare 'a[]=x' 按 declare.def:613/988-1011 + arrayfunc.c:1422 建空数组;30 形状 WSL 5.3.20 逐字节;钉 22/22+22/22;fail-first 15 红;lib 差集 ∅;残余(未加引号 compound 的 InvalidId 层、quoted-compound 行序)已在 PR 正文声明。
#3502 新 PR #3855 head 4abbde14f 建议 PASS(待复核) ${v:?${-x}} 预扫先验 bad substitution(subst.c:8216-8239 次序),只报 ${-x}: bad substitution;12 形状 WSL 逐字节;钉 7/7;fail-first 5 红;lib 差集 ∅。

共腿:lib 双边两分支均 1376/1/4 差集 ∅(共有失败 issue2084 环境性);-D warnings 唯一红=基线 issue3806 dead_code(cfg(unix) helper,base 原样存在);rustfmt/marker-lint/utf8 全 clean;未触碰 src/lexer/continuation.rs;终报 target/devin-dispatch/final-report-h3.md。

caomengxuan666 and others added 3 commits November 18, 2026 13:06
… call

Master's local -p operand walk (Refs #3529) added a fourth
execute_with_io_named_in_context call site after this branch's
signature change; the textual merge was clean but the new site missed
the function_set_views argument. The views are unused on that route
(core_args always carries operands, so the bare-listing enumerator is
unreachable) but are supplied for signature parity with the sibling
call sites.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
print_func_list (variables.c:1079-1090) calls named_function_string
with name=NULL, so the `name && *name` gate (print_cmd.c:1419-1425)
skips the `function ` prefix entirely — print_func_list supplies the
`name ` itself. The listing therefore renders an assignment-word
function name (`function a=b { ...; }`) as `a=b () ` even though
`declare -f a=b` prints `function a=b () ` (declare.def:518 passes
the name). Verified against the GNU bash 5.3.20 oracle:

    $ function a=b { echo hi; }; declare
    a=b ()
    {
        echo hi
    }

Split the ast_print port: multiline_function_listing_text takes the
name=NULL arm (no `function ` fallback) while the named form keeps it
for declare -f / type / export -pf. Regression pin added.

GNU C source: print_cmd.c:1419-1425 named_function_string name gate;
variables.c:1158 print_var_function name=NULL call site.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 18, 2026 08:30
@caomengxuan666
caomengxuan666 merged commit 3505855 into master Nov 18, 2026
9 of 12 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/3487-bare-declare-functions branch November 18, 2026 08:30
caomengxuan666 added a commit that referenced this pull request Nov 20, 2026
…3982)

PR #3530 (for #3487) threaded pre-rendered FunctionSetViews through
the set/declare funnels, and the executor materialized them at every
dispatch: Executor::function_set_views() rendered every function's
listing text via ast_print on each set/declare/typeset/local call,
although only the bare-listing arm ever prints them. In a
function-heavy shell (bats loads ~150 library functions and calls
`local` pervasively) that is an O(total definition text) tax per
call: on X299 (release) 2,000 local-carrying calls under 150
twelve-line functions took ~33 s, and bats' junit gather reported
221 ms/test against ~35 ms before the regression (bisect over
9ef7380..81a5bbf: first bad 3505855, the #3530 commit itself;
#3177 is clean).

GNU renders function definitions only inside
print_all_shell_variables' print_func_list arm
(builtins/set.def:506-527; variables.c:1079-1090), reached solely
from the no-operand listing. Mirror that cost shape: the funnels now
take a lazy FunctionSetViewProvider and realize the views only in
the listing arm (set with no args; declare's plain arm). The render
itself (Executor::render_function_set_views over the functions and
function_def_infos maps) is unchanged.

What this change deletes: the eager per-dispatch full-table render
at five executor call sites (execute_set and the four declare/local
routes in declare_local.rs).

Verification (X299, release build of this branch):
- probe (150 functions x 12 lines, local-loop x2000): 33 s -> 1 s
- bats junit suite, direct form: 221/216 ms per test -> 37/37 ms
  (pre-#3177: 34/36 ms; #3177 itself: 35/37 ms)
- bats junit outer form: 43 s -> 17 s wall, 9/9 ok (#3177: 17 s)
- listing outputs (bare declare/set/typeset, -F/-f/-p/-a forms,
  POSIX mode, bare local, declare --) byte-identical to master
  except the self-referential BASH=<exe path> lines
- new pin tests/issue3230_declare_family_lazy_views_regressions.rs
  (scaling-ratio sentinel + listing-arm checks): 2/2 green;
  issue3487 pin 9/9 green; builtins::set/declare lib tests 16/16

UNVERIFIED-BY-CAPTAIN
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