Paul King created GROOVY-12777:
----------------------------------

             Summary: STC: a method reference whose method has a bounded type 
parameter is not checked when the generic call it is passed to is itself an 
argument
                 Key: GROOVY-12777
                 URL: https://issues.apache.org/jira/browse/GROOVY-12777
             Project: Groovy
          Issue Type: Bug
          Components: Static Type Checker
            Reporter: Paul King


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

@groovy.transform.CompileStatic
class C {
    static <T extends Number> void takeNumber(T t) { }
    static <T> Consumer<T> same(Consumer<T> c) { c }
    static void use(Consumer<String> c) { c.accept('x') }
    static main(args) {
        use(same(C::takeNumber))
    }
}
{code}

This compiles with 5.1.3, 6.0.0 and master, and then fails. Under 
{{@CompileStatic}}:
{noformat}
java.lang.ClassCastException: class java.lang.String cannot be cast to class 
java.lang.Number
{noformat}
Under {{@TypeChecked}}:
{noformat}
groovy.lang.MissingMethodException: No signature of method: takeNumber for 
class: C is applicable for argument types: (String) values: [x]
{noformat}

javac rejects the equivalent: "incompatible types: cannot infer 
type-variable(s) T#1".

With the changes for GROOVY-12755, the assignment form, {{Consumer<String> c = 
same(C::takeNumber)}}, is a compile error, because the target gives {{T}} and 
{{String}} is not within {{T extends Number}}. As an argument, the type of 
{{same(C::takeNumber)}} still has an open type parameter, so it is resolved 
from the parameter of {{use}} by the rule for {{f(g())}} 
({{resolvePlaceholdersFromImplicitTypeHints}}) and the method reference is not 
checked again.

*Expected:* a compile error, as for the assignment.



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

Reply via email to