[
https://issues.apache.org/jira/browse/GROOVY-10067?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Paul King closed GROOVY-10067.
------------------------------
> STC: Wrong return value type inferred for generic method with upper bound
> without parameters
> --------------------------------------------------------------------------------------------
>
> Key: GROOVY-10067
> URL: https://issues.apache.org/jira/browse/GROOVY-10067
> Project: Groovy
> Issue Type: Bug
> Components: Static Type Checker
> Affects Versions: 3.0.8, 4.0.0-alpha-3
> Environment: OpenJDK8
> Reporter: Lyuben Atanasov
> Assignee: Eric Milles
> Priority: Major
> Fix For: 4.0.0-beta-1
>
>
> When static compilation is enabled, and we have a method call for a generic
> method that returns a generic value with an upper bound, and the method does
> not have any parameters, the inferred return type is the upper bound instead
> of the type of the variable to which that value is assigned. To better
> illustrate the issue, here's an example of what doesn't work and some
> work-arounds:
> {code}
> class Test {
> <T extends Number> T getValue() {
> return null;
> }
>
> void methodWithIntParam(Integer param) {
> // do nothing
> }
>
> void test() {
> // not working
> Integer int1 = getValue();
> methodWithIntParam(getValue());
>
> // working
> Integer int2 = this.<Integer>getValue();
> Integer int3 = (Integer) getValue();
> methodWithIntParam(this.<Integer>getValue());
> methodWithIntParam((Integer) getValue());
> }
> }
> {code}
> Compiling this code with static type checking enabled causes the following
> errors:
> {noformat}
> Script_e94ec281a61eb83bce1a38580b498a48.groovy: 13: [Static type checking] -
> Cannot assign value of type java.lang.Number to variable of type
> java.lang.Integer
> @ line 13, column 18.
> Integer int1 = getValue();
> Script_e94ec281a61eb83bce1a38580b498a48.groovy: 14: [Static type checking] -
> Cannot find matching method Test#methodWithIntParam(java.lang.Number). Please
> check if the declared type is correct and if the method exists.
> @ line 14, column 3.
> methodWithIntParam(getValue());
> {noformat}
> Note that if we just a define a generic method without any upper bounds to
> the generic value, type checking works as expected:
> {code}
> <T> T getNumber() {
> return null;
> }
> Integer myInt = getNumber(); // this works!
> {code}
--
This message was sent by Atlassian Jira
(v8.20.1#820001)