[ 
https://issues.apache.org/jira/browse/GROOVY-12561?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18149476#comment-18149476
 ] 

ASF GitHub Bot commented on GROOVY-12561:
-----------------------------------------

codeconsole commented on PR #3090:
URL: https://github.com/apache/groovy/pull/3090#issuecomment-7089291895

   Master is merged again in 69d967d. The only conflict was the block of 
private constants in `StaticTypeCheckingVisitor`: GROOVY-12727's 
`CALL_TARGET_TYPE` and this branch's keys are separate declarations, so both 
are kept. Master's new `RegressionTest` had GROOVY-12561 marked as not yet 
implemented. This branch fixes it, so 7867abf removes the case, which 
`FunctionalTypeArgumentTest` covers. A closure passed to a generic call whose 
type parameter GROOVY-12727 takes from the target (assignment, return, cast, 
nested argument, conditional) behaves the same on master, on this branch before 
the merge and after it.
   
   A further review of the closure classes found cases that regressed or did 
not work, now fixed:
   
   - 0939f36: a closure for a `List<Function<? super String, Integer>>` did not 
compile ("must implement R apply(T)"). The class now implements the interface 
with the type each wildcard stands for, so the verifier adds the bridge.
   - 4731d3b: a closure with a variadic parameter, a default value, or more or 
fewer parameters than the interface method no longer compiled. Such a closure 
is now stored as it is, as before.
   - cc72e80: for an interface with a default method `Closure` would replace, 
such as `getOwner()`, the closure class replaced it. Such an interface is not 
implemented now. The check compares parameter types, so `Function`'s 
`andThen(Function)` is not affected.
   - 86cbfc2: dynamic code could not call a default method of the implemented 
interface through `invokeMethod`, a method pointer or `with`. 
`ClosureMetaClass` now finds it.
   - d5cbedc: the "stored as it is" warning is no longer given for a value 
whose type implements the interface already, such as 
`Closures.FunctionClosure`, or for a lambda or method reference in a 
conditional assigned to a subscript or property, which the assignment compiles 
for the interface.
   
   76b60e2 documents these, and that a closure written as an operator operand, 
a branch of a conditional or a named constructor argument does not get its 
parameter types from the interface, as on master, so `it` needs a declared type 
there. The full suite (22,525 tests) and the lint gate pass locally.




> SC: a closure passed or assigned for a type parameter bound to a functional 
> interface is stored unconverted
> -----------------------------------------------------------------------------------------------------------
>
>                 Key: GROOVY-12561
>                 URL: https://issues.apache.org/jira/browse/GROOVY-12561
>             Project: Groovy
>          Issue Type: Bug
>          Components: Static compilation
>    Affects Versions: 4.0.33, 5.0.8, 6.0.0
>            Reporter: Scott Murphy Heiberg
>            Priority: Major
>
> Under {{@CompileStatic}}, a closure written for a slot declared with a type 
> parameter is stored as the closure itself when the call or assignment binds 
> the type parameter to a functional interface. This happens for an argument, 
> such as {{E}} of {{List.add(E)}} or {{V}} of {{Map.put(K, V)}}, and for an 
> assignment, through {{putAt}} of a {{Map<String, Function<String, String>>}} 
> or a {{List<Supplier<String>>}}, or through the setter or field of a 
> {{Holder<T>}} property for a {{Holder<Supplier<String>>}}. The erasure of the 
> slot is {{Object}}, so nothing converts the closure, and a later call of the 
> interface method fails:
> {code:groovy}
> import java.util.function.Function
> @groovy.transform.CompileStatic
> Map<String, Function<String, String>> make() {
>     Map<String, Function<String, String>> map = [:]
>     map['shout'] = { String s -> s + '!' }
>     map.put('whisper') { String s -> s.toLowerCase() }
>     map
> }
> make().shout.apply('hi') // MissingMethodException: No signature of method: 
> apply ... (a Closure was stored)
> {code}
> The same holds for {{list.add { ... }}}, {{list << { ... }}}, varargs, 
> constructor arguments, {{map?['k'] = ...}}, {{map.k = ...}}, {{holder.value = 
> ...}}, {{box.@field = ...}} and the named constructor argument {{new 
> Holder<Supplier<String>>(value: { ... })}}. Assigning to a variable of the 
> interface type converts the closure.
> Expected: the closure is converted to the interface the call or assignment 
> binds the type parameter to, as it is for a variable of that type.
> Lambdas and method references for such a parameter are GROOVY-10979.
> PR: https://github.com/apache/groovy/pull/3090



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to