[
https://issues.apache.org/jira/browse/GROOVY-12780?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Daniel Sun reassigned GROOVY-12780:
-----------------------------------
Assignee: Daniel Sun
> STC: Comparator.comparing(Type::method) is rejected when the method returns a
> type parameter of a generic Type
> --------------------------------------------------------------------------------------------------------------
>
> Key: GROOVY-12780
> URL: https://issues.apache.org/jira/browse/GROOVY-12780
> Project: Groovy
> Issue Type: Bug
> Components: Static Type Checker
> Reporter: Paul King
> Assignee: Daniel Sun
> Priority: Major
>
> code:groovy}
> @groovy.transform.CompileStatic
> class C {
> static main(args) {
> Comparator<Map.Entry<String, Integer>> byKey =
> Comparator.comparing(Map.Entry::getKey)
> println([b: 1, a: 2].entrySet().stream().sorted(byKey).toList())
> }
> }
> {code}
> *Actual*, with 5.1.3, 6.0.0 and master, under {{@CompileStatic}} and
> {{@TypeChecked}}:
> {noformat}
> [Static type checking] - Invalid return type: java.lang.Object is not
> convertible to java.lang.Comparable<? super java.lang.Object>
> {noformat}
> *Expected:* it compiles and prints {{[a=2, b=1]}}, as dynamic code does.
> javac accepts the equivalent.
> The same error is given for the call as an argument
> ({{stream.sorted(Comparator.comparing(Map.Entry::getKey))}},
> {{list.sort(...)}}) and for other generic classes:
> {{Comparator<Optional<String>> c = Comparator.comparing(Optional::get)}}, and
> a user class {{Box<T>}} with {{T getValue()}}.
> These compile:
> * a class that is not generic: {{Comparator.comparing(String::length)}},
> {{Comparator.comparing(Person::getName)}};
> * a method that does not return a type parameter of the class:
> {{Comparator.comparing(List::size)}};
> * a lambda with a declared parameter type:
> {{Comparator.comparing((Map.Entry<String, Integer> e) -> e.key)}};
> * explicit type arguments: {{Comparator.<Map.Entry<String, Integer>,
> String>comparing(Map.Entry::getKey)}}.
> The return type of the reference is checked when it is first visited
> ({{visitMethodPointerExpression}}, the check from GROOVY-10858). The receiver
> is then the raw {{Map.Entry}}, so {{getKey}} returns {{Object}}, which is not
> within {{U extends Comparable<? super U>}}. In Java the receiver of
> {{Type::instanceMethod}} takes the type arguments of its class from the first
> parameter of the functional interface (JLS 15.13.1). The changes for
> GROOVY-12755 apply that rule when a call is typed by its target, but this
> error is reported earlier.
> A lambda or closure without a declared parameter type fails differently, also
> in every version, and may want an issue of its own:
> {code:groovy}
> Comparator<Map.Entry<String, Integer>> byKey = Comparator.comparing(e ->
> e.key)
> {code}
> {noformat}
> [Static type checking] - No such property: key for class: java.lang.Object
> {noformat}
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)