You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Eric Milles (Jira)" <ji...@apache.org> on 2021/10/20 20:00:10 UTC
[jira] [Resolved] (GROOVY-5746) Array assignment with operator does
double call for index expression and array part
[ https://issues.apache.org/jira/browse/GROOVY-5746?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Eric Milles resolved GROOVY-5746.
---------------------------------
Fix Version/s: 4.0.0-beta-2
Resolution: Fixed
> Array assignment with operator does double call for index expression and array part
> -----------------------------------------------------------------------------------
>
> Key: GROOVY-5746
> URL: https://issues.apache.org/jira/browse/GROOVY-5746
> Project: Groovy
> Issue Type: Bug
> Components: Static compilation
> Affects Versions: 2.0.5, 2.3.0, 2.4.0-rc-1
> Reporter: Jochen Theodorou
> Assignee: Eric Milles
> Priority: Major
> Fix For: 4.0.0-beta-2
>
>
> {code:Java}
> @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()
> {code}
> 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!
--
This message was sent by Atlassian Jira
(v8.3.4#803005)