[
https://issues.apache.org/jira/browse/GROOVY-12829?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18150204#comment-18150204
]
ASF GitHub Bot commented on GROOVY-12829:
-----------------------------------------
paulk-asert opened a new pull request, #3354:
URL: https://github.com/apache/groovy/pull/3354
An implicit call of `toString()`, `hashCode()`, `equals`, `getClass()` or an
extension method of `Object` in a closure is first tried on the closure's
owner and delegate (GROOVY-11858, GROOVY-12576). When the owner is itself a
closure, that closure answered the call as an ordinary object, so wrapping a
call in another closure changed which object answered. An enclosing closure
now applies its own resolution rules to such a call, as it does for any other
name.
```groovy
class C {
String toString() { 'C' }
def m() {
def outer = { ->
def inner = { -> toString() }
[toString(), inner()]
}
outer()
}
}
assert new C().m() == ['C', 'C'] // was ['C', 'C$_m_closure1@...']
```
## Behaviour
`toString()` in a nested closure, by version (dynamic; `C` is the enclosing
instance, `D` a delegate):
| Case | up to 5.1.3 | 6.0.0 | this PR |
|---|---|---|---|
| closure in a method | closure | C | C |
| nested closure, or `each`/`collect` in a closure | inner closure | outer
closure | C |
| closure nested in `new D().with { ... }` | inner closure | outer closure |
D |
| nested in a `DELEGATE_FIRST`/`DELEGATE_ONLY` closure | inner closure |
outer closure | D |
| nested in a `TO_SELF` or dehydrated closure | inner closure | outer
closure | outer closure |
- Dynamic, `@TypeChecked` and `@CompileStatic` code now agree on every row.
Under static compilation only `toString()`, `hashCode()`, `equals` and
`getClass()` change; the GDK methods already reached the enclosing
instance.
- A lambda in a closure, which is a closure in dynamic code, now gives the
same result as a statically compiled lambda there.
- This matches a Java lambda, where a name means what it would in the
enclosing code. Up to 5.1.3 Groovy matched a Java anonymous class instead;
6.0.0 matched neither.
Unchanged: a closure that is not nested, any other method or property name,
the methods `Closure` declares, `this.toString()`, `owner.toString()`, and
`toString()` called on a closure from outside it.
## Changes
- `ClosureMetaClass.invokeMethod`: an owner or delegate that is a generated
closure is handed the call as for one made in its own body.
- `StaticTypeCheckingVisitor`: the branch that selected the owner closure's
own method is removed, and an enclosing `TO_SELF` closure is added as a
receiver for a closure nested in it.
- `@PackedClosures`: a statically compiled closure making such a call in a
nested closure now packs. The dynamic decline (GROOVY-12663) is kept, with
its comment and message corrected.
- Closure guide: rule 3 of "Resolution rules" says the rule holds for methods
every object has, with a new example. The closing note states that
statically compiled code sends a name resolved to the enclosing class to
the `this` object, where dynamic code asks the owner; the two differ only
after `rehydrate`.
- `COMPATIBILITY.md`: an entry under Groovy 7.
## Points for review
- A *delegate* that is a generated closure passes the call on too, not only
an owner.
- Whether the dynamic GROOVY-12663 packing decline can now be removed.
- The `rehydrate` sentence in the guide's note describes existing behaviour
that is worth agreeing on.
## Compatibility
Code that relies on a bare `toString()`, `getClass()`, `with` and the like in
a nested closure reaching the enclosing closure now reaches the enclosing
instance or delegate. `owner.` keeps the old result. No public API changes.
## Tests
`ClosuresSTCTest.testClosureObjectMethodResolveStrategy` and two
`PackedClosuresTransformTest` cases pinned the old behaviour and are updated;
`ClosuresSpecTest.testObjectMethodsInNestedClosures` is new.
> 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)