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.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]