[
https://issues.apache.org/jira/browse/GROOVY-12824?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18149513#comment-18149513
]
ASF GitHub Bot commented on GROOVY-12824:
-----------------------------------------
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.
> STC: a closure written as an operator operand, a branch of a conditional
> argument or a named constructor argument gets no parameter types from the
> functional interface
> -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
>
> Key: GROOVY-12824
> URL: https://issues.apache.org/jira/browse/GROOVY-12824
> Project: Groovy
> Issue Type: Bug
> Reporter: Scott Murphy Heiberg
> Priority: Major
>
> {code:groovy}
> import java.util.function.Function
> class Bean { Function<String, String> value }
> class Sink { Function<String, String> last; Sink leftShift(Function<String,
> String> f) { last = f; this } }
> @groovy.transform.CompileStatic
> class C {
> static String apply(Function<String, String> f) { f.apply('a') }
> static List<String> test(boolean flag) {
> def sink = new Sink()
> sink << { it.toUpperCase() }
> [apply(flag ? { it.toUpperCase() } : { it.trim() }),
> new Bean(value: { it.toUpperCase() }).value.apply('a'),
> sink.last.apply('a')]
> }
> }
> assert C.test(true) == ['A', 'A', 'A']
> {code}
> With 6.0.0 and master, under @CompileStatic or @TypeChecked, each of the
> three closures is rejected:
> {noformat}
> [Static type checking] - Cannot find matching method
> java.lang.Object#toUpperCase(). Please check if the declared type is correct
> and if the method exists.
> {noformat}
> A closure passed as an argument, or assigned to a variable or property of a
> functional interface type, gets the types of its parameters from the
> interface, so its implicit parameter is a String. A closure written in these
> three places does not:
> * as the operand of an operator method, such as leftShift(Function<String,
> String>);
> * as a branch of a conditional expression passed as an argument;
> * as a named argument of a constructor call, for a property of the interface
> type.
> The same holds for a lambda with an untyped parameter.
> In each, the type checker visits the closure before it knows the operator
> method, the method or the property: the right operand of an operator before
> its method is looked up, an argument that is not itself a closure before the
> method is chosen, and the map of named arguments before the properties are
> checked.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)