Skip to content

Commit

Permalink
starlark: allow parenthesized expressions on LHS of augmented assignm…
Browse files Browse the repository at this point in the history
…ents (google#29)

Fixes google#25
  • Loading branch information
josharian authored and adonovan committed Nov 30, 2018
1 parent ca22672 commit 8d353c1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,7 @@ func (fcomp *fcomp) stmt(stmt syntax.Stmt) {
var set func()

// Evaluate "address" of x exactly once to avoid duplicate side-effects.
switch lhs := stmt.LHS.(type) {
switch lhs := unparen(stmt.LHS).(type) {
case *syntax.Ident:
// x = ...
fcomp.lookup(lhs)
Expand Down
17 changes: 17 additions & 0 deletions starlark/testdata/assign.star
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,10 @@ assert.eq(type(z), "tuple")
assert.eq(len(z), 1)
assert.eq(z[0], 1)

# assign value to parenthesized variable
(a) = 1
assert.eq(a, 1)

---
# assignment to/from fields.
load("assert.star", "assert", "freeze")
Expand Down Expand Up @@ -278,3 +282,16 @@ def g():
pass
return a
assert.eq(g(), {"one": 1, "two": 2})

---
# parenthesized LHS in augmented assignment (success)
load("assert.star", "assert")

a = 5
(a) += 3
assert.eq(a, 8)

---
# parenthesized LHS in augmented assignment (error)

(a) += 5 ### "global variable a referenced before assignment"

0 comments on commit 8d353c1

Please sign in to comment.