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

Narrower generic type not taken into account when calling another generic method

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.4.0, 2.4.1
    • 2.4.3
    • Static Type Checker
    • None

    Description

      Given the following class:

      @TypeChecked
      class A { 
          static <T extends Number> T id(T value) {
              value
          }   
      
          // Narrower generic type: doesn't compile
          static <T extends Integer> T id2(T value) {
              id(value)
          }   
      
          // Narrower generic type: doesn't compile either
          static <T extends Integer> T id3(T value) {
              A.<T>id(value)
          }   
      
          // Fixed type: compiles
          static Integer id4(Integer value) {
              id(value)
          }   
      }
      

      id2 and id3 generate compilation errors by the static type checker:

      [ERROR] Failed to execute goal org.codehaus.gmavenplus:gmavenplus-plugin:1.3:compile (default) on project generic-return-type: Error occurred while calling a method on a Groovy class from classpath. InvocationTargetException: startup failed: 
      [ERROR] /Users/fpavageau/devs/bugs/generic-return-type/src/main/groovy/A.groovy: 26: [Static type checking] - Cannot return value of type T on method returning type T
      [ERROR] @ line 26, column 9.
      [ERROR] id(value)
      [ERROR] ^
      [ERROR] 
      [ERROR] /Users/fpavageau/devs/bugs/generic-return-type/src/main/groovy/A.groovy: 31: [Static type checking] - Cannot return value of type T on method returning type T
      [ERROR] @ line 31, column 9.
      [ERROR] A.<T>id(value)
      [ERROR] ^
      [ERROR] 
      [ERROR] 2 errors
      

      I've attached a test case, which is also available on Github. It also contains a Java class (B) doing the same things, but without error.

      Attachments

        1. generic-return-type.tar.gz
          5 kB
          Frank Pavageau

        Issue Links

          Activity

            People

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

              Dates

                Created:
                Updated:
                Resolved: