Repository navigation
allow nounset arithmetic on declared vars - #772
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 1b6c736ed8
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| switch vr.Kind { | ||
| case expand.String, expand.Indexed, expand.Associative: | ||
| vr.UnsetArithmZero = d.declName == "declare" || d.declName == "local" || d.declName == "typeset" |
There was a problem hiding this comment.
Handle KeepValue variables when enabling arithmetic-zero mode
This block only assigns UnsetArithmZero for String/Indexed/Associative, but in processNamedOperand already-declared names are converted to Kind=KeepValue before this point. That means an unset variable declared earlier by another builtin (for example export i) and then redeclared with local/declare/typeset skips this path, so under set -u arithmetic like ((i++)) still errors as unbound even though it was just redeclared by one of the allowed declaration builtins. The flag should also be applied for the KeepValue case when the preserved underlying kind is scalar/indexed/assoc.
Useful? React with 👍 / 👎.
What changed
local/declare/typesetvariables as arithmetic zero underset -u((i++)),((i+=2)), andfor ((; i<2; i++))Why
Provider resolution in the local
agentcommand override could fail immediately underset -uwhen it declared a local index variable before the arithmetic path that initialized it.The root cause was that
gbashtreated these declared-but-unset variables as unbound in arithmetic contexts instead of permitting the arithmetic expression to proceed from zero.Impact
Arithmetic code no longer needs a defensive
local i=0initializer for this pattern. Ordinary parameter expansion semantics such as$iunderset -uare unchanged.Note: this is intentionally more permissive than the pinned bash 5.3.9 oracle for these arithmetic cases.
Validation
make lintgo test ./shell/expandgo test ./internal/shell/interpgo test ./internal/runtime