codeconsole opened a new pull request, #3338: URL: https://github.com/apache/groovy/pull/3338
[GROOVY-12824](https://issues.apache.org/jira/browse/GROOVY-12824). Stacked on #3336 (GROOVY-12822), whose commit is the first one here: a lambda branch of a conditional argument, which this change gives the parameter's interface, is compiled for it only with that fix. A closure passed as an argument, or assigned to a variable or property of a functional interface type, takes the types of its parameters from the interface. Written in three other places it did not, under `@CompileStatic` and `@TypeChecked`: ```groovy class Bean { Function<String, String> value } class Sink { Sink leftShift(Function<String, String> f) { ... } } String apply(Function<String, String> f) { ... } sink << { it.toUpperCase() } // operand of an operator method apply(flag ? { it.toUpperCase() } : { it.trim() }) // branch of a conditional argument new Bean(value: { it.toUpperCase() }) // named constructor argument // [Static type checking] - Cannot find matching method java.lang.Object#toUpperCase() ``` The same held for a lambda with an untyped parameter. In each place the type checker visited the closure before it knew the operator method, the method or the property. - The right operand of an operator now takes, before it is visited, the interface that the operator method found for a `Closure` operand declares its parameter with, as an assignment's value does through `applyTargetType`. - The value of a named constructor argument takes the interface its property is declared with, unless a constructor takes the map. - As an argument, a conditional expression whose branches are closures or lambdas is visited once the method is chosen, as a closure is, and its branches take the parameter's interface. The first two apply only where the closure has parameters to type: an implicit parameter for a method of one parameter, or as many parameters as the method with one of them untyped. So `xs -= { x -> x }`, which `minus(List, Iterable)` also accepts, keeps its checks. A parameter or property declared with a type parameter is left as it was. ## Tests Three tests in `ClosuresSTCTest`, run under `@TypeChecked` and, through `ClosuresStaticCompileTest`, `@CompileStatic`, fail without the change: an operator with a closure, a lambda, a conditional and a receiver's type argument, plus the `minus(List, Iterable)` case; conditional arguments of closures, lambdas, both and nested; named arguments of a closure, a lambda and a conditional, and a class whose constructor takes the map. The full `:test` suite and the lint gate pass; `GenericsStaticCompileTest.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]
