Skip to content

Commit

Permalink
Throw a static error when lvalue of an augmented assignment is a list.
Browse files Browse the repository at this point in the history
RELNOTES:
  When the lvalue of an augmented assignment is a list, we now throw an error
  before evaluating the code (e.g. `a, b += 2, 3`).
PiperOrigin-RevId: 165906611
  • Loading branch information
laurentlb authored and damienmg committed Aug 21, 2017
1 parent 9d79c60 commit e368449
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,16 @@ public void visit(IfStatement node) {
super.visit(node);
}

@Override
public void visit(AugmentedAssignmentStatement node) {
if (node.getLValue().getExpression() instanceof ListLiteral) {
throw new ValidationException(
node.getLocation(), "cannot perform augmented assignment on a list or tuple expression");
}
// Other bad cases are handled when visiting the LValue node.
super.visit(node);
}

/** Returns true if the current block is the top level i.e. has no parent. */
private boolean isTopLevel() {
return block.parent == null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,8 @@ public void testAugmentedAssignmentHasNoSideEffects() throws Exception {

@Test
public void testInvalidAugmentedAssignment_ListLiteral() throws Exception {
new SkylarkTest().testIfErrorContains("cannot perform augmented assignment on a list literal",
new SkylarkTest().testIfErrorContains(
"cannot perform augmented assignment on a list or tuple expression",
"def f(a, b):",
" [a, b] += []",
"f(1, 2)");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public void testAssignmentNotValidLValue() {
checkError("cannot assign to '\"a\"'", "'a' = 1");
}

@Test
public void testAugmentedAssignmentWithMultipleLValues() {
checkError("cannot perform augmented assignment on a list or tuple expression", "a, b += 2, 3");
}

@Test
public void testReturnOutsideFunction() throws Exception {
checkError("Return statements must be inside a function", "return 2\n");
Expand Down

0 comments on commit e368449

Please sign in to comment.