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.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to