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

GroovyCastException when using a map while iterating over something in a @CompileStatic object

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.5.14
    • 2.5.15
    • groovy-jdk
    • None

    Description

      This odd code works fine in 2.5.13, but explodes in 2.5.14.
      My workaround for now is to remove @CompileStatic in production.

      Minimal example (spock test):

      import groovy.transform.CompileStatic
      import spock.lang.Specification
      
      class FooTest extends Specification {
          def "test"() {
              when:
              new Foo()
      
              then:
              noExceptionThrown()
          }
      }
      
      @CompileStatic
      class Foo {
          private Map<String, String> cache = [:]
          Foo() {
              [''].each {
                  cache[''] = ''
              }
          }
      }

       Minimal example without Spock, while settings stuff in a map in an .each-iteration outside of Constructor:

      new Foo().init()
      
      @CompileStatic
      class Foo {
      
          private Map<String, String> cache = [:]
          Foo() {
          }
      
          void init() {
              [''].each {
                  cache[it] = it
              }
          }
      }
      

       

       

      Full Error:

      Expected no exception to be thrown, but got 'org.codehaus.groovy.runtime.typehandling.GroovyCastException'	at spock.lang.Specification.noExceptionThrown(Specification.java:118)
      	at FooTest.test(FooTest.groovy:10)
      Caused by: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'Foo$_closure1@3fa2213' with class 'Foo$_closure1' to class 'Foo'
      	at Foo$_closure1.doCall(FooTest.groovy:19)
      	at Foo.<init>(FooTest.groovy:18)
      	at FooTest.test(FooTest.groovy:7)
      

       

      Attachments

        Issue Links

          Activity

            People

              emilles Eric Milles
              shrykull Simon Rehm
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: