Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-5746

Array assignment with operator does double call for index expression and array part

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.0.5, 2.3.0, 2.4.0-rc-1
    • 4.0.0-beta-2
    • Static compilation
    • 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!

      Attachments

        Activity

          People

            emilles Eric Milles
            blackdrag Jochen Theodorou
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: