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

incrementing a Static Compiled Map with default Integer value won't compile

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.2.1
    • Fix Version/s: 2.2.2
    • Component/s: Static compilation
    • Labels:
      None

      Description

      The following code:

      import groovy.transform.CompileStatic
      
      @CompileStatic
      def test() {
          Map<String,Integer> frequencies = [:].withDefault { 0 }
          frequencies[ 'a' ]++
          frequencies
      }
      
      test()
      

      Causes a BUG exception:

      BUG! exception in phase 'class generation' in source unit 'ConsoleScript75' Error while popping argument from operand stack tracker in class ConsoleScript75 method java.lang.Object test().
      	at org.codehaus.groovy.classgen.asm.OperandStack.popWithMessage(OperandStack.java:74)
      	at org.codehaus.groovy.classgen.asm.OperandStack.remove(OperandStack.java:215)
      	at org.codehaus.groovy.classgen.asm.sc.StaticInvocationWriter.writeDirectMethodCall(StaticInvocationWriter.java:187)
      	at org.codehaus.groovy.classgen.asm.InvocationWriter.makeDirectCall(InvocationWriter.java:243)
      	at org.codehaus.groovy.classgen.asm.InvocationWriter.makeCall(InvocationWriter.java:342)
      	at org.codehaus.groovy.classgen.asm.InvocationWriter.makeCall(InvocationWriter.java:89)
      	at org.codehaus.groovy.classgen.asm.sc.StaticInvocationWriter.makeCall(StaticInvocationWriter.java:498)
      	at org.codehaus.groovy.classgen.asm.InvocationWriter.makeInvokeMethodCall(InvocationWriter.java:73)
      	at org.codehaus.groovy.classgen.asm.InvocationWriter.writeInvokeMethod(InvocationWriter.java:412)
      	at org.codehaus.groovy.classgen.asm.sc.StaticInvocationWriter.writeInvokeMethod(StaticInvocationWriter.java:87)
      	at org.codehaus.groovy.classgen.AsmClassGenerator.visitMethodCallExpression(AsmClassGenerator.java:686)
      	at org.codehaus.groovy.ast.expr.MethodCallExpression.visit(MethodCallExpression.java:64)
      	at org.codehaus.groovy.classgen.asm.BinaryExpressionHelper.loadWithSubscript(BinaryExpressionHelper.java:680)
      	at org.codehaus.groovy.classgen.asm.BinaryExpressionHelper.evaluatePostfixMethod(BinaryExpressionHelper.java:602)
      	at org.codehaus.groovy.classgen.asm.BinaryExpressionHelper.evaluatePostfixMethod(BinaryExpressionHelper.java:625)
      	at org.codehaus.groovy.classgen.AsmClassGenerator.visitPostfixExpression(AsmClassGenerator.java:570)
      	at org.codehaus.groovy.ast.expr.PostfixExpression.visit(PostfixExpression.java:43)
      	at org.codehaus.groovy.classgen.asm.StatementWriter.writeExpressionStatement(StatementWriter.java:604)
      	at org.codehaus.groovy.classgen.AsmClassGenerator.visitExpressionStatement(AsmClassGenerator.java:547)
      	at org.codehaus.groovy.ast.stmt.ExpressionStatement.visit(ExpressionStatement.java:40)
      	at org.codehaus.groovy.classgen.asm.StatementWriter.writeBlockStatement(StatementWriter.java:81)
      	at org.codehaus.groovy.classgen.asm.sc.StaticTypesStatementWriter.writeBlockStatement(StaticTypesStatementWriter.java:49)
      	at org.codehaus.groovy.classgen.AsmClassGenerator.visitBlockStatement(AsmClassGenerator.java:493)
      	at org.codehaus.groovy.ast.stmt.BlockStatement.visit(BlockStatement.java:69)
      	at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitClassCodeContainer(ClassCodeVisitorSupport.java:101)
      	at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitConstructorOrMethod(ClassCodeVisitorSupport.java:112)
      	at org.codehaus.groovy.classgen.AsmClassGenerator.visitStdMethod(AsmClassGenerator.java:357)
      	at org.codehaus.groovy.classgen.AsmClassGenerator.visitConstructorOrMethod(AsmClassGenerator.java:314)
      	at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitMethod(ClassCodeVisitorSupport.java:123)
      	at org.codehaus.groovy.classgen.AsmClassGenerator.visitMethod(AsmClassGenerator.java:434)
      	at org.codehaus.groovy.ast.ClassNode.visitContents(ClassNode.java:1055)
      	at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitClass(ClassCodeVisitorSupport.java:50)
      	at org.codehaus.groovy.classgen.AsmClassGenerator.visitClass(AsmClassGenerator.java:177)
      	at org.codehaus.groovy.control.CompilationUnit$15.call(CompilationUnit.java:794)
      	at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1036)
      	at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:572)
      	at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:550)
      	at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:527)
      	at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:279)
      	at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:258)
      	at groovy.lang.GroovyShell.parseClass(GroovyShell.java:613)
      	at groovy.lang.GroovyShell.run(GroovyShell.java:480)
      	at groovy.lang.GroovyShell.run(GroovyShell.java:163)
      	at groovy.lang.GroovyShell$run$1.call(Unknown Source)
      	at groovy.ui.Console$_runScriptImpl_closure17.doCall(Console.groovy:977)
      	at groovy.ui.Console$_runScriptImpl_closure17.doCall(Console.groovy)
      	at sun.reflect.GeneratedMethodAccessor307.invoke(Unknown Source)
      	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      	at java.lang.reflect.Method.invoke(Method.java:606)
      	at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
      	at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
      	at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:272)
      	at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:909)
      	at groovy.lang.Closure.call(Closure.java:423)
      	at groovy.lang.Closure.call(Closure.java:417)
      	at groovy.lang.Closure.run(Closure.java:504)
      	at java.lang.Thread.run(Thread.java:744)
      

      (stacktrace from Groovy 2.2.1, but also fails similarly with todays 2.3.0 from the master branch)

          frequencies[ 'a' ] += 1
      

      Also fails in a similar fashion

      However,

          frequencies[ 'a' ]  = frequencies[ 'a' ] + 1
      

      works fine

        Attachments

          Activity

            People

            • Assignee:
              melix C├ędric Champeau
              Reporter:
              tim_yates Tim Yates
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: