[
https://issues.apache.org/jira/browse/GROOVY-12825?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18149514#comment-18149514
]
ASF GitHub Bot commented on GROOVY-12825:
-----------------------------------------
codeconsole opened a new pull request, #3339:
URL: https://github.com/apache/groovy/pull/3339
[GROOVY-12825](https://issues.apache.org/jira/browse/GROOVY-12825). Stacked
on #3337 (GROOVY-12823), whose commit is the first one here.
Under `@CompileStatic`, a method reference passed to a generic call that is
itself an argument did not compile when only the enclosing call's parameter
gives its type parameter:
```groovy
@CompileStatic
class C {
static <T> T id(T t) { t }
static String take(Function<String, String> f) { f.apply(' a ') }
static String test() {
take(id(String::trim))
// [Static type checking] - Argument is a method reference, but
parameter type 'T' is not a functional interface
}
}
```
javac infers `T` from the parameter of `take`. The type checker visits
`id(String::trim)` before it chooses `take`, so when it checks the method
reference nothing gives `T` yet.
When the call is an argument of an enclosing call, the method reference is
now resolved once the enclosing call's method is chosen. The reference is
recorded on the call, and `visitMethodCallArguments` resolves it in the
enclosing call's functor pass, giving the call the parameter it is passed for
as its target type. `getFunctionalTypeArgument` then resolves it as it does for
an assignment since #3337. When that parameter is itself a type parameter of
the enclosing call, the enclosing call's own target type gives it, so
`take(wrap(id(String::trim)))`, `Function<String, String> f =
wrap(id(String::trim))` and the cast form compile too. If nothing gives the
type parameter, as for `take(Object)`, or `id(String::trim)` as a statement,
the method reference is rejected as before.
## Tests
`MethodReferenceTest.testNotFunctionalInterface5` fails without the change:
a method reference and a constructor reference passed through `id` to a method,
to a constructor and to a generic method's `Function<U, String>`, through two
calls bare, assigned and cast, and the error for a parameter of type `Object`.
The full `:test` suite and the lint gate pass;
`GenericsSTCTest.testSelfReferentialTypeParameter5`, a `@Grab` test, failed
once on a corrupt file in the local Ivy cache and passes when run again.
Seen in 6.0.0 and master.
> SC: a method reference passed to a generic call that is itself an argument is
> rejected when only the enclosing parameter gives its type
> ---------------------------------------------------------------------------------------------------------------------------------------
>
> Key: GROOVY-12825
> URL: https://issues.apache.org/jira/browse/GROOVY-12825
> Project: Groovy
> Issue Type: Bug
> Reporter: Scott Murphy Heiberg
> Priority: Major
>
> {code:groovy}
> import java.util.function.Function
> @groovy.transform.CompileStatic
> class C {
> static <T> T id(T t) { t }
> static String take(Function<String, String> f) { f.apply(' a ') }
> static String test() {
> take(id(String::trim))
> }
> }
> assert C.test() == 'a'
> {code}
> With 6.0.0 and master this does not compile:
> {noformat}
> [Static type checking] - Argument is a method reference, but parameter type
> 'T' is not a functional interface
> {noformat}
> The call id(String::trim) is an argument of take, whose parameter gives T, as
> javac infers it. The type checker visits id(String::trim) before it chooses
> take, so when it checks the method reference nothing gives T yet. The same
> holds for a constructor reference, for a call that is a constructor argument,
> and through more than one call, as in take(wrap(id(String::trim))).
> This is the case of GROOVY-12823 where the target type is the parameter of
> the enclosing call rather than an assignment, a return or a cast. A lambda in
> the same place is compiled as a closure and converted when it is passed, so
> it works.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)