Skip to content

fix(builtins): declare 'a[]=x' binds the empty array GNU creates - #3854

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/3494-declare-empty-subscript-creates-array
Nov 17, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/3494-declare-empty-subscript-creates-array

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Fixes #3494

Summary

declare 'a[]=x' reported a[]: bad array subscript and then declare: a: not found, leaving no variable. GNU binds the array before the element assignment fails: declare -a a=(), an existing scalar promoted to [0], -A -> declare -A a=(), and the function-scope attribute-only form declare -a a (local, typeset, and declare inside a function).

Mechanism (GNU C source is the spec):

  • builtins/declare.def:613 — for a valued name[subscript]=value operand, declare_internal validates only the bare identifier; valid_array_reference is skipped because assignment() already validated the shape.
  • builtins/declare.def:988-1011 — making_array_special / convert_var_to_array and VSETATTR run before assign_array_element is attempted.
  • arrayfunc.c:1422 array_variable_name — ni <= ind + 1 rejects the empty subscript with err_badarraysub after the array exists.

Rubash dropped the operand at the empty-subscript gate in declare.rs, so the create/convert stage never ran. The operand now flows into assign_declare_names, where a truly empty index takes the same post-creation failure path as FAILED_SUBSCRIPT_SENTINEL: the array is created/converted (scalar promoted, assoc keeps its 0 element, function scope binds attribute-only via DECLARED_UNSET_VARS) and the already-emitted a[]: bad array subscript diagnostic stands — no second diagnostic.

Preserved GNU carve-outs, all oracle-verified:

  • readonly base -> declare: a: readonly variable, nothing bound (err_readonly precedes subscript parsing)
  • declare -p 'a[]=x' stays display-only a[]=x: not found
  • declare 'a[]' (no =), export/readonly 'a[]=x' stay `a[]': not a valid identifier
  • unquoted compound declare a[]=(1 2) keeps the prior drop
  • quoted compound declare 'a[]=(1 2)' -> deprecation warning + badsub + declare -a a=(); declare -A 'a[]=(x=1)' -> declare -A a=([x=1]="" ) — both now byte-match GNU
  • deferred -n is dropped for the array-shaped operand (declare -n 'a[]=x' -> declare -a a=())

What this PR removes

Nothing — additive fix plus one regression file.

Validation

  • New pin tests/issue3494_declare_empty_subscript_creates_array.rs: 22 tests, all pass on head.
  • fail-first on merge-base 6ecb7cab: 15 failed / 7 passed — red set is exactly the array-creation assertions; the 7 green are unchanged-behavior guards.
  • tests/issue3186_readonly_export_invalid_ident_compound.rs (diagnostic pins for the same operand family): 22/22 still green.
  • lib two-sided cargo test --lib: head 1376/1/4 vs base 1376/1/4, failure-name set difference empty (shared failure api::consumer_tests::issue2084_session_end_restores_host_process_state reproduces identically on base — environmental).
  • RUSTFLAGS="-D warnings" cargo check --all-targets: clean.
  • rustfmt clean on touched files; check-marker-literals.py OK; check-utf8-boundary-hygiene.sh clean.
  • WSL GNU Bash 5.3.20 (/usr/local/bin/bash, 9c465866) script-file probes, 30 shapes (plain/-a/-A/+=/typeset/local/in-function/multi-operand/scalar-promotion/existing-array/assoc-scalar/readonly/nameref/-p/noassign/export/readonly-bad/quoted+unquoted compound/negative-subscript): stdout+stderr+rc byte-identical modulo the program-name prefix, except the noted residuals below.

Known residuals (adjacent, not fixed here)

  • Unquoted compound declare a[]=(1 2) / a[]+=(...): GNU reports shell-level `a[]': not a valid identifier during argument expansion (no declare: prefix, nothing bound); rubash reports a[]: bad array subscript + declare: a: not found. Identical on base — the owner is the compound-assignment expansion layer for declare-family args, a different subsystem.
  • Quoted compound stderr ordering: GNU prints the quoted compound array assignment deprecated warning before the bad-subscript line; rubash prints it after (same lines, swapped order).
  • local a[]=(1 2) inside a function: GNU emits a double diagnostic (f: `a[]' then local: `a[]'); untested edge.

…#3494)

GNU declare.def:613 validates only the bare name for valued operands;
declare.def:988-1011 runs making_array_special/convert_var_to_array and
VSETATTR before assign_array_element rejects the empty subscript
(arrayfunc.c:1422 array_variable_name), leaving `declare -a a=()` — an
existing scalar promoted to [0], -A -> `declare -A a=()`, and the
function-scope attribute-only form `declare -a a`. The operand was
dropped at the empty-subscript gate, so `declare -p a` reported not
found. Keep the operand flowing into the assign stage, which now treats
a truly empty index like FAILED_SUBSCRIPT_SENTINEL: create/convert,
then fail silently. Readonly bases still report readonly and unquoted
compound `a[]=(` operands keep the prior drop.
@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】i1 审计 — 建议:PASS(#3854)

PR #3854 第1轮 — 建议:PASS
head: 9de6ded8d09976159e67c7bb3641e5fda3e02b6d base(merge-base): 6ecb7caba23c765735647b62e6f4792b7be8fd7a 文件: 3 个 +295/-12
关联 issue: #3494(open,我核过的时间:2026-11-17 23:20 +0800)

机制核查:

  • GNU 链路属实:declare.def:595-615 值赋 operand offset!=0 时跳过 valid_array_reference、只对裸名跑 valid_identifier;declare.def:988-1011 VSETATTR/making_array_special 先于 assign_array_element;arrayfunc.c:1398-1422 array_variable_name 的 ni <= ind + 1 在元素赋值期才 err_badarraysub —— 数组先建/转换后元素失败,declare -a a=() 残留。与 PR 描述一致。
  • index_expression.is_empty() 与 trim().is_empty() 的区分是必要的:GNU declare 'a[ ]=x' 实测绑定 a[0]="x"(空白下标走算术求值=0),只有真空 [] 走 badsub——实现守住了该边界。
  • readonly 基座在 declare.rs 门先走 DiagKind::Readonly(err_readonly 先于下标解析),compound marker 与 unquoted a[]=( 保持旧 drop,-n 经既有 drop_nameref_attrs 丢弃——carve-out 与 GNU 次序一致。
  • diff 无顺手无关改动;无桥接/翻译层。

腿(逐项给数字与名字):

  • 钉自跑:issue3494_declare_empty_subscript_creates_array 22 passed / 0 failed(head,本地 Windows)。
  • fail-first:base 6ecb7caba + 原样钉文件 → 15 failed / 7 passed,红名 = {declare_dash_a_empty_subscript_creates_empty_array, declare_dash_cap_a_empty_subscript_creates_empty_assoc, declare_empty_subscript_append_creates_empty_array, declare_empty_subscript_continues_to_later_operands, declare_empty_subscript_creates_empty_indexed_array, declare_empty_subscript_drops_deferred_nameref, declare_empty_subscript_empty_scalar_to_assoc_keeps_empty_element, declare_empty_subscript_promotes_existing_scalar, declare_empty_subscript_readonly_array_reports_readonly, declare_empty_subscript_readonly_base_reports_readonly, declare_empty_subscript_scalar_to_assoc_moves_value_to_zero, declare_in_function_empty_subscript_is_attribute_only, local_dash_cap_a_empty_subscript_is_attribute_only, local_empty_subscript_creates_attribute_only_local, typeset_empty_subscript_creates_empty_array},与 PR 声称 15F/7P 一致。
  • 相邻钉:issue3186_readonly_export_invalid_ident_compound head 22/22、base 22/22。
  • lib 双边:head 1376 passed / 1 failed / 4 ignored(api::consumer_tests::issue2084_session_end_restores_host_process_state,环境性)/ base 1376 / 1 / 4 同名 / 差集 ∅。
  • -D warnings:cargo check --all-targets 报 tests/issue3806_procsub_input_rebind_body.rs:23 run_driver is never used(dead_code)——该文件在 merge-base 原样存在(938587bb9 引入),与本 PR diff 无关,记基线。
  • 卫生:rustfmt --check 三个触及文件 clean;check-marker-literals.py OK;check-utf8-boundary-hygiene.sh clean(均在 head 树跑)。
  • CI(head 9de6ded8,run 44100295354):红任务 2 个——Regression pin stem census(失败名集为 master run 44096234749 失败集的子集,无新增;缺失项均为 funsub/heredoc/pty 类已知 flake)、GNU regression goldens and canaries(issue_753::case_9_declare_indexed_operand_binds_nothing + upstream_suite_slice_snapshots,与 master 同红=基线;golden_funsub_valsub 在本 PR 上 PASS)。Compatibility CLI tests 绿。
  • GNU 对照:WSL /usr/local/bin/bash GNU Bash 5.3.20(1)-release,脚本 scratch-i1/3494-cmp.sh,24 形状(plain/-a/-A/+=/typeset/local/declare-in-fn/多操作数/标量提升/已有数组/assoc/空标量→assoc/readonly/-n/-p/无赋值/export/readonly-bad/quoted+unquoted compound/负下标/空白下标/状态/函数内可见性)→ 22/24 逐字节一致。两处分歧均为 PR 已披露残差且已在 base 复核为既有:quoted compound stderr 行序(warning↔badsub 互换)、unquoted compound 诊断文案(属 compound 展开层,base 同输出)。

F 点:无。

续作记录:本轮未动代码。

未跑/VOID 的腿:无(-D warnings 的 dead_code 为基线项,已注明)。

相邻残差(供队长裁量是否另立 issue,PR 已自述):unquoted compound 诊断文案分歧、quoted compound stderr 行序——均非本 PR 引入。

@caomengxuan666
caomengxuan666 merged commit fa656f3 into master Nov 17, 2026
10 of 12 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] declare 'a[]=x' does not create the empty array GNU creates

1 participant