fix(interpreter): treat assoc array subscripts as literal strings#864
Merged
fix(interpreter): treat assoc array subscripts as literal strings#864
Conversation
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
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.
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'sexpand_variable_or_literal()incorrectly checkedself.variables.get(s)for bare names, causing${assoc[x]}to return the value at$x's value instead of at key"x"when a variablexexisted in scope.How
Removed the
self.variables.get(s)fallback inexpand_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:
$kexpansion (ensures${data[$k]}still works via explicit$prefix)Closes #861