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

STC fails when calling a generic method from another generic method

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 3.0.8, 4.0.0-alpha-3, 4.0.0-beta-1
    • 4.0.0-beta-2, 3.0.12
    • Static Type Checker
    • None
    • OpenJDK 8

    Description

      The static type checker fails to properly determine the return type of a generic method when that method is called from another generic method. Here's an example:

      class Test {
      
      	<T extends Number> Set<T> generateNumbers(Class<T> numberType) {
      		// mock return value, needed to demonstrate the issue
      		return Collections.emptySet();
      	}
      
      	<T extends Number> void printNumbers(Class<T> numberType) {
      		generateNumbers(numberType).stream()
      			.filter(num -> num.doubleValue() > 0)
      			.forEach(num -> println "$num");
      	}
      	
      }
      

      With static type checking enabled, compilation of this class fails:

      Script_bbf0c00c9b2872d2a3528c2d80bbaa49.groovy: 10: [Static type checking] - Cannot find matching method java.lang.Object#doubleValue(). Please check if the declared type is correct and if the method exists.
       @ line 10, column 19.
         			.filter(num -> num.doubleValue() > 0)
      

      If we call the generateNumbers() method with a specific class, everything works:

      class Test {
      
      	<T extends Number> Set<T> generateNumbers(Class<T> numberType) {
      		return Collections.emptySet();
      	}
      
      	void printNumbers() {
      		generateNumbers(Integer).stream()
      			.filter(num -> num.doubleValue() > 0)
      			.forEach(num -> println "$num");
      	}
      	
      }
      

      It looks like the static type checker does not properly take into account the generic types of the surrounding method and thus fails to determine the type of the returned value.

      Attachments

        1. screenshot-1.png
          41 kB
          Eric Milles
        2. screenshot-2.png
          40 kB
          Eric Milles

        Issue Links

          Activity

            People

              emilles Eric Milles
              latanasov Lyuben Atanasov
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: