Paul King created GROOVY-12829:
----------------------------------

             Summary: 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


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