Repository navigation
fix(builtins): declare 'a[]=x' binds the empty array GNU creates - #3854
Merged
caomengxuan666 merged 1 commit intoNov 17, 2026
Merged
Conversation
…#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.
Member
Author
|
【WIN-AGENT-AUDIT】i1 审计 — 建议:PASS(#3854) PR #3854 第1轮 — 建议:PASS 机制核查:
腿(逐项给数字与名字):
F 点:无。 续作记录:本轮未动代码。 未跑/VOID 的腿:无(-D warnings 的 dead_code 为基线项,已注明)。 相邻残差(供队长裁量是否另立 issue,PR 已自述):unquoted compound 诊断文案分歧、quoted compound stderr 行序——均非本 PR 引入。 |
6 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3494
Summary
declare 'a[]=x'reporteda[]: bad array subscriptand thendeclare: 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 formdeclare -a a(local,typeset, anddeclareinside a function).Mechanism (GNU C source is the spec):
builtins/declare.def:613— for a valuedname[subscript]=valueoperand,declare_internalvalidates only the bare identifier;valid_array_referenceis skipped becauseassignment()already validated the shape.builtins/declare.def:988-1011—making_array_special/convert_var_to_arrayandVSETATTRrun beforeassign_array_elementis attempted.arrayfunc.c:1422array_variable_name—ni <= ind + 1rejects the empty subscript witherr_badarraysubafter 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 intoassign_declare_names, where a truly empty index takes the same post-creation failure path asFAILED_SUBSCRIPT_SENTINEL: the array is created/converted (scalar promoted, assoc keeps its0element, function scope binds attribute-only viaDECLARED_UNSET_VARS) and the already-emitteda[]: bad array subscriptdiagnostic stands — no second diagnostic.Preserved GNU carve-outs, all oracle-verified:
declare: a: readonly variable, nothing bound (err_readonly precedes subscript parsing)declare -p 'a[]=x'stays display-onlya[]=x: not founddeclare 'a[]'(no=),export/readonly 'a[]=x'stay`a[]': not a valid identifierdeclare a[]=(1 2)keeps the prior dropdeclare 'a[]=(1 2)'-> deprecation warning + badsub +declare -a a=();declare -A 'a[]=(x=1)'->declare -A a=([x=1]="" )— both now byte-match GNU-nis 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
tests/issue3494_declare_empty_subscript_creates_array.rs: 22 tests, all pass on head.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.cargo test --lib: head 1376/1/4 vs base 1376/1/4, failure-name set difference empty (shared failureapi::consumer_tests::issue2084_session_end_restores_host_process_statereproduces identically on base — environmental).RUSTFLAGS="-D warnings" cargo check --all-targets: clean.check-marker-literals.pyOK;check-utf8-boundary-hygiene.shclean./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)
declare a[]=(1 2)/a[]+=(...): GNU reports shell-level`a[]': not a valid identifierduring argument expansion (nodeclare:prefix, nothing bound); rubash reportsa[]: 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 array assignment deprecatedwarning 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[]'thenlocal: `a[]'); untested edge.