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

STC: Wrong type inferred for lambda parameter in a generic method

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 4.0.0-beta-1, 3.0.18
    • Static Type Checker
    • None
    • OpenJDK8

    Description

      Following the fix of GROOVY-10049, a new issue has appeared. Consider the following code example:

      class Test {
      
      	Set<Number> getNumbers() {
      		// mock return value, needed to demonstrate the issue
      		return Collections.emptySet();
      	}
      	
      	<T extends Number> Set<T> filterNumbers(Class<T> numberType) {
      		Set<T> filteredNumbers = new HashSet<>();
      		getNumbers().stream()
      			.filter(numberType::isInstance)
      			.map(numberType::cast)
      			.forEach(num -> {
      				filteredNumbers.add(num);
      			});
      		return filteredNumbers;
      	}
      }
      
      def test = new Test()'
      test.filterNumbers(Integer);
      

      Compiling with static type checking enabled produces an error:

      Script_de4295e2a5171ce6c5ecd73605504d73.groovy: 14: [Static type checking] - Cannot call java.util.Set#add(T) with arguments [R] 
       @ line 14, column 5.
         				filteredNumbers.add(num);
      

      This used to work fine before applying the fix for GROOVY-10049. Tested against https://github.com/apache/groovy/commit/e07f0112c5eff8d9c6828bd0ddb69e4b7f7cc1d6

      Attachments

        Activity

          People

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

            Dates

              Created:
              Updated:
              Resolved: