Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[arith] Fix scalar lvalue of arrays (compat_array). #733

Merged
merged 2 commits into from
Apr 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions osh/sh_expr_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,10 +328,14 @@ def _EvalLhsAndLookupArith(self, node):
lval = self.EvalArithLhs(node, runtime.NO_SPID)
val = OldValue(lval, self.mem, self.exec_opts)

# BASH_LINENO, etc.
# BASH_LINENO, arr (array name with shopt -s compat_array), etc.
if val.tag_() in (value_e.MaybeStrArray, value_e.AssocArray) and lval.tag_() == lvalue_e.Named:
named_lval = cast(lvalue__Named, lval)
if word_eval.CheckCompatArray(named_lval.name, self.exec_opts):
if val.tag_() == value_e.MaybeStrArray:
lval = lvalue.Indexed(named_lval.name, 0)
elif val.tag_() == value_e.AssocArray:
lval = lvalue.Keyed(named_lval.name, '0')
val = word_eval.ResolveCompatArray(val)

# This error message could be better, but we already have one
Expand All @@ -355,7 +359,7 @@ def EvalToInt(self, node):
"""
val = self.Eval(node)

# BASH_LINENO, etc.
# BASH_LINENO, arr (array name with shopt -s compat_array), etc.
if val.tag_() in (value_e.MaybeStrArray, value_e.AssocArray) and node.tag_() == arith_expr_e.VarRef:
tok = cast(Token, node)
if word_eval.CheckCompatArray(tok.val, self.exec_opts):
Expand Down
28 changes: 24 additions & 4 deletions spec/ble-features.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -582,15 +582,15 @@ case ${SH##*/} in (dash|ash) exit 1;; esac # dash/ash does not have arrays
case ${SH##*/} in (osh) shopt -s compat_array;; esac
case ${SH##*/} in (zsh) setopt KSH_ARRAYS;; esac
arr=(foo bar baz)
echo "$arr"
## stdout: foo
argv.py "$arr" "${arr}"
## stdout: ['foo', 'foo']

## N-I dash/ash status: 1
## N-I dash/ash stdout-json: ""

## OK yash stdout: foo bar baz
## OK yash stdout: ['foo', 'bar', 'baz', 'foo', 'bar', 'baz']

#### [compat_array] scalar access to arrays
#### [compat_array] scalar write to arrays
case ${SH##*/} in
(dash|ash) exit 1;; # dash/ash does not have arrays
(osh) shopt -s compat_array;;
Expand All @@ -610,3 +610,23 @@ argv.py "${a[@]}"
# with a scalar.
['1']
## END

#### [compat_array] scalar write to associative arrays
case ${SH##*/} in
(dash|ash|yash|mksh) exit 1;; # dash/ash/yash/mksh does not have associative arrays
(osh) shopt -s compat_array;;
(zsh) setopt KSH_ARRAYS;;
esac

declare -A d=()
d['0']=1
d['foo']=hello
d['bar']=world
((d++))
argv.py ${d['0']} ${d['foo']} ${d['bar']}
## stdout: ['2', 'hello', 'world']

## N-I dash/ash/yash/mksh status: 1
## N-I dash/ash/yash/mksh stdout-json: ""

## N-I zsh stdout: ['1', 'hello', 'world']