Scott Murphy Heiberg created GROOVY-12716:
---------------------------------------------
Summary: STC: an overloaded method pointer assigned to a
functional interface is checked against overloads its parameter types cannot
select
Key: GROOVY-12716
URL: https://issues.apache.org/jira/browse/GROOVY-12716
Project: Groovy
Issue Type: Bug
Components: Static Type Checker
Reporter: Scott Murphy Heiberg
{code:groovy}
import java.util.function.*
@groovy.transform.CompileStatic
class C {
Function<Integer,Integer> f() { Math.&abs }
BiFunction<Integer,Integer,Integer> g() { Math.&max }
void h() { Function<Integer,Integer> i = Math.&abs; assert i.apply(-1) == 1
}
}
{code}
*Actual:* {{Possible loss of precision from double to java.lang.Integer}} for
each method pointer.
*Expected:* it compiles and runs, as on 6.0.0. An {{Integer}} argument selects
{{abs(int)}} and {{max(int,int)}} at run time.
The check for the return value of a method pointer takes the common type of all
the overloads of the name, here {{double}}, including those that values of the
functional interface's parameter types never select. Since GROOVY-12623 (commit
d1710f88eb) a method pointer gets its target type when it is assigned or
returned, so such a pointer now reaches this check.
The overloads that count are the best match for the parameter types, and any
with narrower parameter types, which a value of a subtype selects. So
{{ToIntFunction<Object> g = C.&f}} with {{int f(Object)}} and {{long f(Long)}}
is still reported, as {{f(Long)}} may be called.
Master only: 6.0.0 compiles and runs the example.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)