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

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

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

   Thanks Daniel. Master is merged in 3090824. #3089 landed squashed as 
ad1832f, which matches the #3089 tip 41eb183 in every file it touches, so the 
branch now has 5d3c725 (the `groovy.target.lambda.typearg` gate) and 41eb183 
(`list << String::trim`). It also has f1cf705 and e3b8697: the full `:test` 
suite passes locally and the lint gate is green.
   
   The operator conflict needed more than a union of the two sides. This branch 
converts an operand of type `Closure` like an argument, and a lambda has that 
type. Without the option, `list << (() -> 'x')` was still compiled for the 
interface, and #3089's `testLambdaIsCompiledAsAClosureWithoutTheOption` failed. 
That check now leaves lambdas to `isFunctionalLiteral`, so a lambda for a 
type-parameter slot is compiled as a closure by default everywhere, as on 
master.
   
   fba3a94 fixes a false warning from this branch. A lambda or method reference 
assigned to a subscript, property or field, such as `map['k'] = (String s) -> 
s`, is compiled for the interface, but it was also warned about as a Closure 
stored as it is. Only closures are converted at those assignments now, and a 
test covers it with the option on and off.




> 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