Paul King created GROOVY-12778:
----------------------------------

             Summary: STC: a method reference to a method whose type parameter 
is bounded by itself is inferred as Object for a generic call
                 Key: GROOVY-12778
                 URL: https://issues.apache.org/jira/browse/GROOVY-12778
             Project: Groovy
          Issue Type: Bug
            Reporter: Paul King


{code:groovy}
import java.util.function.BinaryOperator

@groovy.transform.CompileStatic
class C {
    static <T extends Comparable<T>> T max(T a, T b) { a.compareTo(b) >= 0 ? a 
: b }
    static <T> BinaryOperator<T> same(BinaryOperator<T> op) { op }
    static main(args) {
        BinaryOperator<Object> op = same(C::max)
        println op.apply(1, 'x')
    }
}
{code}

This compiles with 5.1.3, 6.0.0 and master, under {{@CompileStatic}} and 
{{@TypeChecked}}, and then fails:
{noformat}
java.lang.ClassCastException: class java.lang.String cannot be cast to class 
java.lang.Integer
{noformat}

javac rejects the equivalent: "incompatible types: cannot infer 
type-variable(s) T#1". {{Object}} is not within {{T extends Comparable<T>}}.

The call does not seem to be typed by its target here: the type parameter of 
{{same}} appears to be inferred as {{Object}} from the method reference alone, 
without checking the bound of {{max}}, and {{BinaryOperator<Object>}} then fits 
the declared type.

*Expected:* a compile error. {{BinaryOperator<String> op = same(C::max)}} 
should compile, and does with the changes for GROOVY-12755.




--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to