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

TypeChecked/CompileStatic tries to cast to wrong type in nested closure

    XMLWordPrintableJSON

Details

    Description

      The code

      import groovy.transform.*
      import java.util.function.Function
      
      @CompileStatic
      class Foo {
          
          final <T> T foo(Function<Reader, T> function)  {
              new StringReader("").withCloseable { reader ->
      			function.apply(reader)
              }        
          }
          
          static void main(String... args) {
             println  new Foo().foo { it.text.empty }
          }
      }
      

      fails with

      org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'true' with class 'java.lang.Boolean' to class 'java.io.StringReader'
      	at Foo$_foo_closure1.doCall(Script1.groovy:10)
      	at Foo$_foo_closure1.call(Script1.groovy)
      	at Foo.foo(Script1.groovy:9)
      	at Foo.main(Script1.groovy:15)
      

      while it works fine with @CompileDynamic. As far as I can see this regression was introduced with 3.0.9.

      (see Groovy Web Console as long as deployed version is 3.0.10)

      Attachments

        Activity

          People

            emilles Eric Milles
            leonard84 Leonard Brünings
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: