You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Lyuben Atanasov (Jira)" <ji...@apache.org> on 2021/04/21 11:18:00 UTC

[jira] [Updated] (GROOVY-10049) STC fails when calling a generic method from another generic method

     [ https://issues.apache.org/jira/browse/GROOVY-10049?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Lyuben Atanasov updated GROOVY-10049:
-------------------------------------
    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:
{code}
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");
	}
	
}
{code}
With static type checking enabled, compilation of this class fails:
{noformat}
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)
{noformat}

If we call the {{generateNumbers()}} method with a specific class, everything works:
{code}
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");
	}
	
}
{code}

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.

  was:
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:
{code}
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");
	}
	
}
{code}
With static type checking enabled, compilation of this class fails:
{noformat}
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)
{noformat}

If we call the {{generateNumbers()}} method with a specific, everything works:
{code}
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");
	}
	
}
{code}

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.


> STC fails when calling a generic method from another generic method
> -------------------------------------------------------------------
>
>                 Key: GROOVY-10049
>                 URL: https://issues.apache.org/jira/browse/GROOVY-10049
>             Project: Groovy
>          Issue Type: Bug
>          Components: Static Type Checker
>    Affects Versions: 3.0.8, 4.0.0-alpha-3
>         Environment: OpenJDK 8
>            Reporter: Lyuben Atanasov
>            Priority: Major
>
> 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:
> {code}
> 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");
> 	}
> 	
> }
> {code}
> With static type checking enabled, compilation of this class fails:
> {noformat}
> 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)
> {noformat}
> If we call the {{generateNumbers()}} method with a specific class, everything works:
> {code}
> 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");
> 	}
> 	
> }
> {code}
> 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.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)