Details
-
Bug
-
Status: Closed
-
Major
-
Resolution: Fixed
-
2.0.5, 2.3.0, 2.4.0-rc-1
-
None
Description
@groovy.transform.CompileStatic class IndexTest { private int f = 0 public int getIndex() {f++;0} def foo() { List<String> x = ["1","2"] x[index] += "1" assert x[0] == "11" assert f == 1 } } def x = new IndexTest() x.foo()
The program above shows code in which we use an operator in combination with array assignment and an index with a side effect. This code must run without calling getIndex() twice. Since the field f is incremented twice, this is not the case. Normal Groovy executed the code as wished. The probable implementation is x[index] = x[index] + "1", which not only evals index twice, but also x. x and index must be evaluated only once!