groovy
  1. groovy
  2. GROOVY-5180

Enum Comparison results in incorrect map behavior when using add assignment operator

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Blocker Blocker
    • Resolution: Fixed
    • Affects Version/s: 1.8.2
    • Fix Version/s: 1.8.4
    • Component/s: Compiler
    • Labels:
      None
    • Environment:
      java version "1.6.0_23"
      OpenJDK Runtime Environment (IcedTea6 1.11pre) (6b23~pre11-0ubuntu1.11.10)
      OpenJDK 64-Bit Server VM (build 20.0-b11, mixed mode)

      Groovy Version: 1.8.2 JVM: 1.6.0_23
    • Testcase included:
      yes
    • Number of attachments :
      1

      Description

      In the attached test case you will see four methods which do the same three things. Each initializes a map, increments a value in the map, and then returns the map. The method called "works()" uses the add assignment operator to do this. The "buggy()" method is the same, but adds an enum comparison.

      There is some interaction between the enum comparison and the += operator. The method "numericComparison()" is just like "buggy()," but performs an integer comparison. The "workaround()" method avoids the += operator. Both of these methods return a Map as expected.

        Activity

        Hide
        Paul King added a comment -

        Strange. I just ran your example as a script and had no problems:

        enum TheEnum { ONE, TWO }
        
        def works() {
          def ret = [ '1': 100.00 ]
          ret['1'] += 45.00
          ret
        }
        
        def workaround() {
          def ret = [ '1': 100.00 ]
          if (TheEnum.ONE == TheEnum.ONE) ret['1'] = ret['1'] + 45.00
          ret
        }
          
        def buggy() {
          def ret = [ '1': 100.00 ]
          if (TheEnum.ONE == TheEnum.ONE) ret['1'] += 45.00
          ret
        }
        
        def numericComparison() {
          def ret = [ '1': 100.00 ]
          if (1 == 1) ret['1'] += 45.00
          ret
        }
        
        assert works() instanceof Map
        assert workaround() instanceof Map
        assert numericComparison() instanceof Map
        assert buggy() instanceof Map
        

        I.e. buggy() wasn't buggy for me.

        I was using Java 7.0_01, Groovy 1.8.4, Windows 7, running in the groovyConsole.

        Show
        Paul King added a comment - Strange. I just ran your example as a script and had no problems: enum TheEnum { ONE, TWO } def works() { def ret = [ '1': 100.00 ] ret['1'] += 45.00 ret } def workaround() { def ret = [ '1': 100.00 ] if (TheEnum.ONE == TheEnum.ONE) ret['1'] = ret['1'] + 45.00 ret } def buggy() { def ret = [ '1': 100.00 ] if (TheEnum.ONE == TheEnum.ONE) ret['1'] += 45.00 ret } def numericComparison() { def ret = [ '1': 100.00 ] if (1 == 1) ret['1'] += 45.00 ret } assert works() instanceof Map assert workaround() instanceof Map assert numericComparison() instanceof Map assert buggy() instanceof Map I.e. buggy() wasn't buggy for me. I was using Java 7.0_01, Groovy 1.8.4, Windows 7, running in the groovyConsole.
        Hide
        David Clark added a comment -

        Correction, only 1.8.2 is affected. 1.8.4 is NOT affected. However, because of issue #5173 I am stuck using 1.8.2 for now.

        Show
        David Clark added a comment - Correction, only 1.8.2 is affected. 1.8.4 is NOT affected. However, because of issue #5173 I am stuck using 1.8.2 for now.
        Hide
        Paul King added a comment -

        Running your script as is from the command-prompt also worked fine.

        Show
        Paul King added a comment - Running your script as is from the command-prompt also worked fine.
        Paul King made changes -
        Field Original Value New Value
        Affects Version/s 1.8.4 [ 17852 ]
        Paul King made changes -
        Environment java version "1.6.0_23"
        OpenJDK Runtime Environment (IcedTea6 1.11pre) (6b23~pre11-0ubuntu1.11.10)
        OpenJDK 64-Bit Server VM (build 20.0-b11, mixed mode)

        Groovy Version: 1.8.4 JVM: 1.6.0_23
        java version "1.6.0_23"
        OpenJDK Runtime Environment (IcedTea6 1.11pre) (6b23~pre11-0ubuntu1.11.10)
        OpenJDK 64-Bit Server VM (build 20.0-b11, mixed mode)

        Groovy Version: 1.8.2 JVM: 1.6.0_23
        Hide
        Paul King added a comment -

        So should we close this issue since it is already fixed?

        Show
        Paul King added a comment - So should we close this issue since it is already fixed?
        Hide
        David Clark added a comment -

        This is definitely a bug in 1.8.2. However it appears to be fixed in 1.8.4. I am now downgrading to groovy 1.8.1 to avoid this bug and #5173. I have added a test to my application to check for a regression of this bug in the next version. You can close this and I will open a new version if it reappears in a later version.

        Show
        David Clark added a comment - This is definitely a bug in 1.8.2. However it appears to be fixed in 1.8.4. I am now downgrading to groovy 1.8.1 to avoid this bug and #5173. I have added a test to my application to check for a regression of this bug in the next version. You can close this and I will open a new version if it reappears in a later version.
        Guillaume Laforge made changes -
        Status Open [ 1 ] Closed [ 6 ]
        Fix Version/s 1.8.4 [ 17852 ]
        Resolution Fixed [ 1 ]

          People

          • Assignee:
            Unassigned
            Reporter:
            David Clark
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: