Remove syntactic sugar when assigning to a dict item.
e.g. a['key'] = value
is handled through a proper lvalue, instead of using syntactic sugar.
Benefits include:
- better error messages (reference to the '+' operator was cryptic)
- more robust, e.g. it is compatible with the += operator
- can be used in a tuple, e.g. a[1], a[2] = 3, 4
- it is a step towards mutable dict
--
MOS_MIGRATED_REVID=111597545
diff --git a/src/test/java/com/google/devtools/build/lib/syntax/SkylarkEvaluationTest.java b/src/test/java/com/google/devtools/build/lib/syntax/SkylarkEvaluationTest.java
index 752a051..e2f4ca7 100644
--- a/src/test/java/com/google/devtools/build/lib/syntax/SkylarkEvaluationTest.java
+++ b/src/test/java/com/google/devtools/build/lib/syntax/SkylarkEvaluationTest.java
@@ -816,6 +816,24 @@
}
@Test
+ public void testDictTupleAssignmentAsLValue() throws Exception {
+ new SkylarkTest().setUp("def func():",
+ " d = {'a' : 1}",
+ " d['b'], d['c'] = 2, 3",
+ " return d",
+ "d = func()").testLookup("d", ImmutableMap.of("a", 1, "b", 2, "c", 3));
+ }
+
+ @Test
+ public void testDictItemPlusEqual() throws Exception {
+ new SkylarkTest().setUp("def func():",
+ " d = {'a' : 2}",
+ " d['a'] += 3",
+ " return d",
+ "d = func()").testLookup("d", ImmutableMap.of("a", 5));
+ }
+
+ @Test
public void testDictAssignmentAsLValueNoSideEffects() throws Exception {
new SkylarkTest().setUp("def func(d):",
" d['b'] = 2",
@@ -827,7 +845,7 @@
public void testListIndexAsLValueAsLValue() throws Exception {
new SkylarkTest()
.testIfErrorContains(
- "unsupported operand type(s) for +: 'list' and 'dict'",
+ "can only assign an element in a dictionary, not in a 'list'",
"def id(l):",
" return l",
"def func():",