starlark: permit assigments to () and [] (#294)
See https://github.com/bazelbuild/starlark/issues/93
diff --git a/doc/spec.md b/doc/spec.md
index 7c2e66a..41ccc7f 100644
--- a/doc/spec.md
+++ b/doc/spec.md
@@ -2565,7 +2565,6 @@
sequence with the same number of elements as the target.
Each element of the sequence is then assigned to the corresponding
element of the target, recursively applying the same logic.
-It is a static error if the sequence is empty.
```python
pi, e = 3.141, 2.718
diff --git a/resolve/resolve.go b/resolve/resolve.go
index 9510fdb..e4b6752 100644
--- a/resolve/resolve.go
+++ b/resolve/resolve.go
@@ -605,9 +605,6 @@
case *syntax.TupleExpr:
// (x, y) = ...
- if len(lhs.List) == 0 {
- r.errorf(syntax.Start(lhs), "can't assign to ()")
- }
if isAugmented {
r.errorf(syntax.Start(lhs), "can't use tuple expression in augmented assignment")
}
@@ -617,9 +614,6 @@
case *syntax.ListExpr:
// [x, y, z] = ...
- if len(lhs.List) == 0 {
- r.errorf(syntax.Start(lhs), "can't assign to []")
- }
if isAugmented {
r.errorf(syntax.Start(lhs), "can't use list expression in augmented assignment")
}
diff --git a/resolve/testdata/resolve.star b/resolve/testdata/resolve.star
index 64e0081..4f1a270 100644
--- a/resolve/testdata/resolve.star
+++ b/resolve/testdata/resolve.star
@@ -214,8 +214,9 @@
[a, b] = 0
[c, d] += 0 ### "can't use list expression in augmented assignment"
(e, f) += 0 ### "can't use tuple expression in augmented assignment"
-[] = 0 ### "can't assign to \\[\\]"
-() = 0 ### "can't assign to ()"
+
+[] = 0 # ok
+() = 0 # ok
---
# break and continue statements must appear within a loop
diff --git a/starlark/testdata/assign.star b/starlark/testdata/assign.star
index 978d60d..38108f9 100644
--- a/starlark/testdata/assign.star
+++ b/starlark/testdata/assign.star
@@ -5,49 +5,73 @@
# tuple assignment
load("assert.star", "assert")
+() = () # empty ok
+
a, b, c = 1, 2, 3
assert.eq(a, 1)
assert.eq(b, 2)
assert.eq(c, 3)
-def f1(): (x,) = 1
-assert.fails(f1, "int in sequence assignment")
-def f2(): a, b, c = 1, 2
-assert.fails(f2, "too few values to unpack")
-def f3(): a, b = 1, 2, 3
-assert.fails(f3, "too many values to unpack")
-def f4(): a, b = (1,)
-assert.fails(f4, "too few values to unpack")
-def f5(): (a,) = [1, 2, 3]
-assert.fails(f5, "too many values to unpack")
-
+(d, e, f,) = (1, 2, 3) # trailing comma ok
+---
+(a, b, c) = 1 ### "got int in sequence assignment"
+---
+(a, b) = () ### "too few values to unpack"
+---
+(a, b) = (1,) ### "too few values to unpack"
+---
+(a, b, c) = (1, 2) ### "too few values to unpack"
+---
+(a, b) = (1, 2, 3) ### "too many values to unpack"
+---
+() = 1 ### "got int in sequence assignment"
+---
+() = (1,) ### "too many values to unpack"
+---
+() = (1, 2) ### "too many values to unpack"
---
# list assignment
load("assert.star", "assert")
+[] = [] # empty ok
+
[a, b, c] = [1, 2, 3]
assert.eq(a, 1)
assert.eq(b, 2)
assert.eq(c, 3)
-def f1(): [a, b, c,] = 1
-assert.fails(f1, "got int in sequence assignment")
-def f2(): [a, b, c] = 1, 2
-assert.fails(f2, "too few values to unpack")
-def f3(): [a, b] = 1, 2, 3
-assert.fails(f3, "too many values to unpack")
-def f4(): [a, b] = (1,)
-assert.fails(f4, "too few values to unpack")
-
+[d, e, f,] = [1, 2, 3] # trailing comma ok
+---
+[a, b, c] = 1 ### "got int in sequence assignment"
+---
+[a, b] = [] ### "too few values to unpack"
+---
+[a, b] = [1] ### "too few values to unpack"
+---
+[a, b, c] = [1, 2] ### "too few values to unpack"
+---
+[a, b] = [1, 2, 3] ### "too many values to unpack"
+---
+[] = 1 ### "got int in sequence assignment"
+---
+[] = [1] ### "too many values to unpack"
+---
+[] = [1, 2] ### "too many values to unpack"
---
# list-tuple assignment
load("assert.star", "assert")
+# empty ok
+[] = ()
+() = []
+
[a, b, c] = (1, 2, 3)
assert.eq(a, 1)
assert.eq(b, 2)
assert.eq(c, 3)
+[a2, b2, c2] = 1, 2, 3 # bare tuple ok
+
(d, e, f) = [1, 2, 3]
assert.eq(d, 1)
assert.eq(e, 2)