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

Cannot assign value of type java.lang.Object with varargs, parameterized method and @CompileStatic

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 2.4.7
    • 2.4.8
    • Static compilation
    • None
    • Groovy 2.4.7 (tested also with 2.4.4)

    Description

      Static compilation fails when method is parameterized and there are varargs arguments which are not used in the call.

      Error:(9, 23) Groovyc: [Static type checking] - Cannot assign value of type java.lang.Object to variable of type Ext.
      

      Simple classes to reproduce the problem:

      public class FooInJava { //needs to be in Java, a class in Groovy works fine
          <T> T create(Class<T> type, Object... args) { return null; }
      }
      
      class Ext {}
      
      @CompileStatic
      class FooMain {
          static void main(String[] args) {
              Ext create = new FooInJava().create(Ext) //casting is required to make compilation pass
          }
      }
      

      It only occurs if a class with unfortunate method signature is written in Java and static compilation is enabled. Casting to the right type helps. Originally spotted with Gradle - `ExtensionContainer.create(...)`.

      Attachments

        Issue Links

          Activity

            paulk Paul King added a comment -

            For what it's worth, the same problem happens with pre-compiled Groovy. The Groovy compiler is leaving the upperBounds on the generic type T as null but when loading classes, the upperBounds on T is set as java.lang.Object which then confuses the inferencing code. I haven't had time to work out exactly where we'd need to fix it yet. It might actually fix a couple of similar errors I noticed recently in slightly different contexts.

            paulk Paul King added a comment - For what it's worth, the same problem happens with pre-compiled Groovy. The Groovy compiler is leaving the upperBounds on the generic type T as null but when loading classes, the upperBounds on T is set as java.lang.Object which then confuses the inferencing code. I haven't had time to work out exactly where we'd need to fix it yet. It might actually fix a couple of similar errors I noticed recently in slightly different contexts.
            githubbot ASF GitHub Bot added a comment -

            GitHub user paulk-asert opened a pull request:

            https://github.com/apache/groovy/pull/385

            GROOVY-7907: Cannot assign value of type java.lang.Object with vararg…

            …s, perameterized method and @CompileStatic

            You can merge this pull request into a Git repository by running:

            $ git pull https://github.com/paulk-asert/groovy groovy7907

            Alternatively you can review and apply these changes as the patch at:

            https://github.com/apache/groovy/pull/385.patch

            To close this pull request, make a commit to your master/trunk branch
            with (at least) the following in the commit message:

            This closes #385


            commit 174e64ef7e8eb5d8726c1f55c00c50f8fee2527e
            Author: paulk <paulk@asert.com.au>
            Date: 2016-08-17T02:08:49Z

            GROOVY-7907: Cannot assign value of type java.lang.Object with varargs, perameterized method and @CompileStatic


            githubbot ASF GitHub Bot added a comment - GitHub user paulk-asert opened a pull request: https://github.com/apache/groovy/pull/385 GROOVY-7907 : Cannot assign value of type java.lang.Object with vararg… …s, perameterized method and @CompileStatic You can merge this pull request into a Git repository by running: $ git pull https://github.com/paulk-asert/groovy groovy7907 Alternatively you can review and apply these changes as the patch at: https://github.com/apache/groovy/pull/385.patch To close this pull request, make a commit to your master/trunk branch with (at least) the following in the commit message: This closes #385 commit 174e64ef7e8eb5d8726c1f55c00c50f8fee2527e Author: paulk <paulk@asert.com.au> Date: 2016-08-17T02:08:49Z GROOVY-7907 : Cannot assign value of type java.lang.Object with varargs, perameterized method and @CompileStatic
            githubbot ASF GitHub Bot added a comment -

            Github user asfgit closed the pull request at:

            https://github.com/apache/groovy/pull/385

            githubbot ASF GitHub Bot added a comment - Github user asfgit closed the pull request at: https://github.com/apache/groovy/pull/385
            paulk Paul King added a comment -

            Proposed PR merged. Thanks for raising the issue.

            paulk Paul King added a comment - Proposed PR merged. Thanks for raising the issue.

            Thanks for a quick fix.

            szpak Marcin Zajaczkowski added a comment - Thanks for a quick fix.

            People

              paulk Paul King
              szpak Marcin Zajaczkowski
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: