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

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

paulk-asert opened a new pull request, #3350:
URL: https://github.com/apache/groovy/pull/3350

   builds on #3349




> SC: a delegate method or property used in a lambda inside a closure throws 
> ClassCastException
> ---------------------------------------------------------------------------------------------
>
>                 Key: GROOVY-12827
>                 URL: https://issues.apache.org/jira/browse/GROOVY-12827
>             Project: Groovy
>          Issue Type: Bug
>    Affects Versions: 5.1.3, 6.0.0
>            Reporter: Paul King
>            Priority: Major
>              Labels: breaking
>
> Under \{{@CompileStatic}}, a native lambda inside a closure can compile an 
> implicit call to a method or property of the closure's delegate, but the call 
> fails at run time. Dynamic Groovy runs the same code correctly.
> {code:groovy}
> import java.util.function.Supplier
> class D {
>     String hi() \{ 'hi from D' }
> }
> @groovy.transform.CompileStatic
> class C {
>     def m() {
>         new D().with {
>             Supplier<String> s = () -> hi()
>             s.get()
>         }
>     }
> }
> assert new C().m() == 'hi from D'
> {code}
> Actual result:
> {noformat}
> java.lang.ClassCastException: class C cannot be cast to class 
> groovy.lang.Closure
> {noformat}
> h3. Cause
> The type checker records a receiver path through the enclosing closure, such 
> as \{{owner.delegate}}. The owner of a native lambda is the enclosing 
> instance rather than that closure, so the path cannot be followed at run time.
> h3. Proposed fix
> A lambda that resolves a name through the enclosing closure captures that 
> closure. The name then resolves as it would directly in the closure, 
> honouring the resolve strategy and the delegate at call time, including a 
> later \{{setDelegate}}. The lambda's own \{{owner}}, \{{delegate}} and 
> \{{thisObject}} remain the enclosing instance. Other lambdas keep their 
> current shape.
> A serializable lambda that captures the closure is generated as a 
> \{{ConvertedClosure}} proxy, as in dynamic Groovy. The JDK's 
> \{{SerializedLambda}} is replaced only after its captured values are read, so 
> a delegate that refers back to the lambda would otherwise receive the 
> \{{SerializedLambda}} and fail on deserialization. A functional interface 
> that is or implements a trait still uses the metafactory, so this 
> deserialization failure remains possible there.
> h3. Compatibility
> These affect only a serializable lambda containing such a delegate reference. 
> The reference could not run successfully before.
> * If the closure's delegate, or another value held by the closure, is not 
> serializable, writing the lambda now throws \{{NotSerializableException}}, 
> even if the delegate call is on a branch that never runs. Dynamic Groovy 
> behaves the same way.
> * An undeclared checked exception thrown from the lambda now reaches the 
> caller as \{{UndeclaredThrowableException}}. Dynamic Groovy behaves the same 
> way.
> h3. Known limitation
> A map-key property on a \{{Map}} delegate still fails with 
> \{{MissingPropertyException}}, because it is resolved dynamically and has no 
> receiver path:
> {code:groovy}
> [a: 'A'].with {
>     Supplier<String> s = () -> a
>     s.get()
> }
> {code}



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

Reply via email to