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

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

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

   Backport of #3354 (GROOVY-12829) to 6.0.x.
   
   An implicit call of `toString()`, `hashCode()`, `equals`, `getClass()` or an
   extension method of `Object` in a nested closure was answered by the 
enclosing
   closure as an ordinary object. The enclosing closure now passes the call on 
to
   its own owner and delegate, by its own resolve strategy, so the call means 
what
   it would in the enclosing closure. An enclosing closure with `TO_SELF`, or 
with
   no owner and no delegate, still answers it. Dynamic, type checked and
   statically compiled code agree.
   
   The code changes are the same as on master. Differences from #3354:
   
   - The closure-guide changes and their spec examples are left out, since the
     improved guide is for Groovy 7. The guide's example is kept as a regression
     test, `bugs/Groovy12829`, which fails without the `ClosureMetaClass` 
change.
   - The `COMPATIBILITY.md` entry is the same, without the link to the guide's
     "Resolution rules" section, which 6.0.x does not have.
   
   Under `@PackedClosures` a closure making such a call is still left as a 
class,
   as for any implicit call of a method of `Object` (GROOVY-12576, #3351).
   
   Tests: the closure, lambda, `@DelegatesTo`, packed-closure and closure
   metaclass tests pass on 6.0.x (2494 tests, 0 failures).
   




> An implicit call of a method of Object in a nested closure is answered by the 
> enclosing closure instead of being passed on to its owner or delegate
> ---------------------------------------------------------------------------------------------------------------------------------------------------
>
>                 Key: GROOVY-12829
>                 URL: https://issues.apache.org/jira/browse/GROOVY-12829
>             Project: Groovy
>          Issue Type: Bug
>            Reporter: Paul King
>            Assignee: Paul King
>            Priority: Major
>
> h3. Description
> GROOVY-11858 (6.0.0) and GROOVY-12576 made an implicit call of 
> {{toString()}}, {{hashCode()}}, {{equals()}}, {{getClass()}} or an {{Object}} 
> extension method in a closure go to the closure's delegate or owner, instead 
> of the closure itself.
> That holds only for the closure whose body makes the call. When the owner is 
> itself a closure, the owner answers the call as an ordinary object and does 
> not pass it on as it would for a call in its own body. So wrapping a call in 
> another closure changes which object answers.
> {code:groovy}
> class D { String toString() { 'D' } }
> class C {
>     String toString() { 'C' }
>     def single()   { def k = { -> toString() }; k() }
>     def nested()   { def k = { -> def j = { -> toString() }; j() }; k() }
>     def each()     { def r = []; def k = { -> [1].each { r << toString() } }; 
> k(); r[0] }
>     def withEach() { new D().with { def r = []; [1].each { r << toString() }; 
> r[0] } }
> }
> def c = new C()
> assert c.single() == 'C'      // passes
> assert c.nested() == 'C'      // fails: C$_nested_closure1@...
> assert c.each() == 'C'        // fails: C$_each_closure2@...
> assert c.withEach() == 'D'    // fails: C$_withEach_closure3@...
> {code}
> ||Case||up to 5.1.3||6.0.0 dynamic||Expected||
> |closure in a method|closure|C|C|
> |{{new D().with \{ toString() \}}}|closure|D|D|
> |nested closure|inner closure|outer closure|C|
> |{{each \{ toString() \}}} inside a closure|inner closure|outer closure|C|
> |nested closure inside {{with}}|inner closure|outer closure|D|
> Under {{@CompileStatic}}, 6.0.0 still gives the 5.x results; current master 
> gives the "6.0.0 dynamic" results. A lambda in dynamic code is a closure and 
> behaves the same. A statically compiled lambda inside a closure already gives 
> the expected result, so {{() -> toString()}} and {{\{ -> toString() \}}} in 
> the same closure currently differ.
> h3. Expected
> A closure that is the owner or delegate of another closure applies its own 
> resolution rules to such a call, as the user guide states ("A candidate that 
> is itself a closure ... applies these same rules with its own owner, delegate 
> and resolve strategy"). It answers the call itself only when it has no owner 
> or delegate left to ask.
> h3. Unchanged
> Methods that {{Closure}} declares ({{call}}, {{curry}}, ...), explicit 
> receivers ({{owner.toString()}}, {{this.toString()}}), a call on a closure 
> from outside it, {{TO_SELF}}, and a closure with no owner or delegate.
> h3. Notes
> * Dynamic: {{ClosureMetaClass.invokeMethod}} invokes the method found on an 
> owner closure directly.
> * Static: {{StaticTypeCheckingVisitor}} selects the owner closure's own 
> method when the owner is a closure.
> * Tests that pin the current behaviour: 
> {{ClosuresSTCTest.testClosureObjectMethodResolveStrategy}}, 
> {{PackedClosuresTransformTest.dynamicClosuresCallingObjectMethodsInANestedClosureDecline}}
>  and 
> {{PackedClosuresTransformTest.closuresCallingObjectMethodsOnAClosureDecline}}.
>  The packing decline added for GROOVY-12663 may no longer be needed.
> * This is a behaviour change and needs a COMPATIBILITY.md entry.



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

Reply via email to