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

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

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

   @daniellansun
   
   1. Either order. The only textual conflict with #3089 was that both PRs 
appended a test at the end of `ClosuresStaticCompileTest`; this PR now adds its 
test earlier in the file (e608a44e04), and `git merge-tree` merges the two 
branches cleanly both ways, to the same tree. Neither depends on the other's 
tests: on a merge of both, the `groovy.transform.stc` and 
`org.codehaus.groovy.classgen.asm.sc` packages pass. The STC metadata does not 
overlap: this PR records PARAMETER_TYPE on a named constructor argument's key, 
#3089 on call arguments. A closure assigned to a property becomes a cast here 
before the setter call is written, so #3089's writer sees an argument of the 
interface type and does not convert it again.
   
   2. Intentional. A Closure already is a `Runnable`, a `Callable` and a 
`GroovyCallable`, and `DefaultTypeTransformation.castToType` returns an object 
unchanged when it is an instance of the target type, so a cast would leave the 
Closure anyway; the check only skips a cast that does nothing. That is also 
what an assignment to a `Callable` variable and dynamic Groovy do. 
`ClosuresSTCTest.testClosureAssignedToACallableOrRunnableSubscriptOrPropertyStaysAClosure`
 assigns closures to a `Callable` subscript, property and named constructor 
argument and a `Runnable` property, and checks each stays a Closure and works, 
under `@TypeChecked` and `@CompileStatic` (8b73244656). Added a sentence to the 
description.
   
   3. Added 
`ClosuresSTCTest.testValueAssignedToAFunctionalSubscriptOrPropertyIsStoredAsItIsIfNotAClosure`:
 a `Supplier` variable, a method returning a `Supplier`, and an Elvis and a 
conditional whose branches are such values, assigned to a subscript and a 
property of type `Supplier`. Each stored value is the same object as the 
right-hand side, under `@TypeChecked` and `@CompileStatic` alike (8b73244656).
   
   Also merged master. The test changes are the only changes in this round; the 
Checkstyle and CodeNarc gates pass.
   




> SC: a closure assigned to a subscript or property of a functional interface 
> type 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 assigned to a subscript or a property 
> whose type is a functional interface is stored as the closure itself when the 
> method that stores it declares its parameter with a type parameter: {{putAt}} 
> of a {{Map<String, Function<String, String>>}} or {{List<Supplier<String>>}}, 
> or the setter or field of a {{Holder<T>}} property for a 
> {{Holder<Supplier<String>>}}. 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
> }
> make().shout.apply('hi') // MissingMethodException: No signature of method: 
> apply ... (a Closure was stored)
> {code}
> The same holds for {{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 type of the subscript or property, 
> as for a variable, and the value of the assignment is the converted closure.
> PR: https://github.com/apache/groovy/pull/3090



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

Reply via email to