You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Paul King (Jira)" <ji...@apache.org> on 2026/06/23 02:11:00 UTC

[jira] [Created] (GROOVY-12097) a[index] = expr evaluates RHS before index, breaking left-to-right evaluation order

Paul King created GROOVY-12097:
----------------------------------

             Summary: a[index] = expr evaluates RHS before index, breaking left-to-right evaluation order
                 Key: GROOVY-12097
                 URL: https://issues.apache.org/jira/browse/GROOVY-12097
             Project: Groovy
          Issue Type: Bug
            Reporter: Paul King


AI read:
The intent of GROOVY-2556 was to stop double-evaluating the object/index in compound subscript assignments (x[i] += v) — the first hunk of that commit. Reordering plain = was an unintended side effect of the "related problems" cleanup.

GROOVY-2556 fixed some cases involved array expressions with side effects but also changed the index value on the LHS of an assignment. The following Java and Groovy program gave the same result prior to the change (Groovy 1.5.2ish) but the behavior below since then:
{code:groovy}
public class C {
  public static void main(String[] args) {
    int[] a = new int[]{-1, -1, -1, -1};
    int x = 0;
    int y = 3;
    a[x] = x++;
    a[y] = --y;
    System.out.println("[" + a[0] + "," + a[1] + "," + a[2] + "," + a[3] + "]");
    // Groovy: [-1,0,2,-1], Java: [0,-1,-1,2]
  }
}
{code}



--
This message was sent by Atlassian Jira
(v8.20.10#820010)