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

Invalid type inference with a generic method / return type and optional parameter

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.4.2
    • 2.4.3
    • Static Type Checker
    • None
    • MacOS X
      Oracle JDK 1.7u75 / 18u40
      Maven 3.0.5

    Description

      The following class fails to compile:

      @TypeChecked
      class A {
          public <T> T f(T value, int param = 0) {
              value
          }
      
          void run() {
              def s = f('42')
              s.length()
          }
      }
      

      The compilation error is:

      [ERROR] /Users/fpavageau/devs/bugs/generic-method-optional-param/src/main/groovy/A.groovy: 26: [Static type checking] - Cannot find matching method java.lang.Object#length(). Please check if the declared type is right and if the method exists.
      [ERROR] @ line 26, column 9.
      [ERROR] s.length()
      [ERROR] ^
      [ERROR] 
      [ERROR] 1 error
      

      The same code will compile if the optional parameter is removed from f, or if s is explicitely declared as a String instead of using def.

      It seems to be related to GROOVY-7211, but this bug is definitely not fixed in 2.4.2.

      I'm attaching a test case, which is also available on Github.

      Attachments

        Activity

          People

            melix Cédric Champeau
            fpavageau Frank Pavageau
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: