Skip to content

fix(interpreter): treat assoc array subscripts as literal strings#864

Merged
chaliy merged 2 commits intomainfrom
fix/issue-861-assoc-array-literal-keys
Mar 27, 2026
Merged

fix(interpreter): treat assoc array subscripts as literal strings#864
chaliy merged 2 commits intomainfrom
fix/issue-861-assoc-array-literal-keys

Conversation

@chaliy
Copy link
Copy Markdown
Contributor

@chaliy chaliy commented Mar 27, 2026

What

Fix associative array subscript evaluation to treat bare names as literal string keys instead of looking them up as variable references.

Why

In real bash, ${assoc[key]} always uses the literal string "key" as the lookup key. Bashkit's expand_variable_or_literal() incorrectly checked self.variables.get(s) for bare names, causing ${assoc[x]} to return the value at $x's value instead of at key "x" when a variable x existed in scope.

How

Removed the self.variables.get(s) fallback in expand_variable_or_literal(). The function now only expands explicit $var/${var} references and returns bare names as literal strings.

Tests

Added 4 spec tests covering:

  • Key lookup with same-named variable in scope
  • Numeric variable collision
  • Assignment path literal key verification
  • Loop iteration with $k expansion (ensures ${data[$k]} still works via explicit $ prefix)

Closes #861

chaliy added 2 commits March 26, 2026 22:17
Issue #861: associative array subscripts are evaluated as arithmetic
instead of literal strings. When a variable with the same name as a key
exists, ${assoc[key]} looks up the variable's value instead of the
literal string "key".
In real bash, associative array subscripts are always treated as literal
strings. A bare name in ${assoc[key]} is the string "key", not the
value of variable $key.

The expand_variable_or_literal() function incorrectly looked up bare
names as variable references, causing wrong values when a variable
with the same name as the key existed in scope.

Closes #861
@chaliy chaliy merged commit f4b7183 into main Mar 27, 2026
23 checks passed
@chaliy chaliy deleted the fix/issue-861-assoc-array-literal-keys branch March 27, 2026 03:45
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: associative array subscripts evaluated as arithmetic instead of literal strings

1 participant