Skip to content

allow nounset arithmetic on declared vars - #772

Merged
ewhauser merged 1 commit into
mainfrom
ewhauser/nounset-local-arith-fix
Mar 28, 2026
Merged

ewhauser merged 1 commit into
mainfrom
ewhauser/nounset-local-arith-fix

Conversation

@ewhauser

Copy link
Copy Markdown
Owner

What changed

  • treat declared-but-unset local/declare/typeset variables as arithmetic zero under set -u
  • apply that behavior to both plain arithmetic reads and arithmetic lvalues
  • add unit and runtime regressions for ((i++)), ((i+=2)), and for ((; i<2; i++))

Why

Provider resolution in the local agent command override could fail immediately under set -u when it declared a local index variable before the arithmetic path that initialized it.

The root cause was that gbash treated 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=0 initializer for this pattern. Ordinary parameter expansion semantics such as $i under set -u are unchanged.

Note: this is intentionally more permissive than the pinned bash 5.3.9 oracle for these arithmetic cases.

Validation

  • make lint
  • go test ./shell/expand
  • go test ./internal/shell/interp
  • go test ./internal/runtime

@ewhauser
ewhauser marked this pull request as ready for review March 28, 2026 21:46
@ewhauser ewhauser changed the title [codex] allow nounset arithmetic on declared vars allow nounset arithmetic on declared vars Mar 28, 2026
@ewhauser
ewhauser merged commit 895877a into main Mar 28, 2026
16 checks passed
@ewhauser
ewhauser deleted the ewhauser/nounset-local-arith-fix branch March 28, 2026 21:49

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment on lines +577 to +579
switch vr.Kind {
case expand.String, expand.Indexed, expand.Associative:
vr.UnsetArithmZero = d.declName == "declare" || d.declName == "local" || d.declName == "typeset"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

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