Repository navigation
GROOVY-12576: SC: resolve Object methods in closures through the delegate and owner - #3102
Conversation
… and owner
Under static compilation, an implicit-this call to a method of Object in
a closure or lambda, such as toString(), hashCode(), equals(Object) or
getClass(), was made on the closure itself:
@CompileStatic
class C {
String m() { new StringBuilder().with { append('w'); toString() } }
}
new C().m() returned "C$_m_closure1@1b2c3d4" instead of "w". The type
checker looks up the methods of Closure before the delegate and the
owner, and that lookup also returned the methods Closure inherits from
Object. With with, tap, identity or a @DelegatesTo(strategy =
DELEGATE_FIRST) method this ignored the delegate. In a plain closure it
ignored the owner, and in a lambda it ignored the enclosing instance,
which Java and dynamic Groovy both use. This happened in methods,
constructors, field initializers and static initializers alike.
Dynamic Groovy only tries the closure first for methods that Closure or
GroovyObject declare; a method the closure inherits from elsewhere goes
to the delegate or the owner according to the resolve strategy. The type
checker now leaves the methods of Object out of that first lookup, so
the delegate or owner is chosen, and the static invocation writer calls
a method of Object that was resolved against the owner on the closure's
this object (the class itself in a static context), as it already did
for other owner methods. Methods declared by Closure, such as
getDelegate() or getMetaClass(), still resolve against the closure.
@TypeChecked code already behaved like dynamic Groovy at run time; only
the inferred types of such calls change there.
…is object is null
Under static compilation, toString(), hashCode(), equals(Object) and
getClass() called without a receiver in a closure now go to the
closure's this object, as other owner methods do. A closure can have no
this object: dehydrate() returns a copy whose owner, delegate and this
object are all null, which is how closures are made serializable or
shared between threads. For such a closure these calls failed with a
NullPointerException:
@CompileStatic
class C {
Closure<Integer> c() { { -> hashCode() } }
}
new C().c().dehydrate()() threw "Cannot invoke "Object.hashCode()"
because the return value of "C$_c_closure1.getThisObject()" is null".
Before the previous fixes these calls always went to the closure itself.
When the method is a public method of Object, or overrides one, the
static invocation writer now loads the this object and uses the closure
instead if it is null, and calls the method as declared by Object, so
the override of the this object still runs when there is one:
ALOAD 0
INVOKEVIRTUAL getThisObject()
DUP
IFNONNULL L
POP
ALOAD 0
L: INVOKEVIRTUAL java/lang/Object.hashCode()I
Lambdas are left alone, since their enclosing instance is never null.
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #3102 +/- ##
==================================================
+ Coverage 74.1016% 74.1128% +0.0112%
- Complexity 43596 43625 +29
==================================================
Files 1692 1692
Lines 149245 149290 +45
Branches 28284 28295 +11
==================================================
+ Hits 110593 110643 +50
+ Misses 28475 28468 -7
- Partials 10177 10179 +2
🚀 New features to boost your workflow:
|
daniellansun
left a comment
There was a problem hiding this comment.
LGTM for GROOVY-12576.
Under @CompileStatic, bare toString/hashCode/equals/getClass in a closure/lambda must follow resolve strategy to delegate/owner (Groovy 4 / dynamic / @TypeChecked), not the closure’s own Object methods. Dropping Object-declared methods from the closure-method short-circuit in StaticTypeCheckingVisitor, and teaching StaticInvocationWriter to rewrite the receiver (including ThisObjectOrClosureExpression for dehydrated / null-thisObject closures), is the right split.
Tests cover with/tap/identity, ctor/field/static contexts, DELEGATE_FIRST, lambdas vs closures, and dehydrate/rehydrate fallbacks in ClosuresStaticCompileTest.
CI green. Codecov ~86% patch (a few StaticInvocationWriter partials on the null-thisObject branch) — non-blocking given the dedicated dehydrate test.
Note: overlaps StaticInvocationWriter with #3103/#3106/#3107 — rebase carefully.
|
Friendly ping: this PR is approved. If there are no objections within 72 hours, this PR will be merged. Thanks! |
|
Friendly ping: this PR is approved and CI is green. If there are no objections within 72 hours, this PR will be merged. Thanks! |
|
Thanks, this looks good overall. Two things before merge: 1. Closures using On master it compiles, and both dynamic and 2. The 4.0 claim in the description. Results for
Dynamic code only started going to the delegate or owner with GROOVY-11858, which is on master and 6.0.x but not on 5.1.x. So in 4.0, SC and dynamic disagreed, and in 5.x they agree. This PR lines SC up with 6.0+ dynamic behaviour. For that reason I'd backport to 6.0.x (with the Minor and optional: with |
…ject-methods # Conflicts: # src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticInvocationWriter.java
…in closures
Under static compilation and type checking, toString(), hashCode(),
equals(Object) and getClass() called without a receiver in a closure
whose resolve strategy is TO_SELF no longer compiled:
class C {
static Object run(@DelegatesTo(value=String, strategy=Closure.TO_SELF) Closure c) { ... }
Object m() { run { -> toString() } }
}
failed with "Cannot find matching method C#toString()". With TO_SELF only
the closure is searched, and its methods of Object had been left out of
that search. They are kept for TO_SELF again, and the static compiler
calls them on the closure, as dynamic Groovy does.
A closure nested in another closure has that closure as its owner. With
OWNER_FIRST or OWNER_ONLY, dynamic Groovy calls the owner closure's own
method of Object; the type checker resolved it further out, against the
enclosing class or the outer closure's delegate. It now resolves it
against the owner closure, so { -> { -> toString() }() }() returns the
outer closure's toString(), and a nested closure in a TO_SELF closure no
longer fails with a ClassCastException.
…is object
Under static compilation, toString(), hashCode(), equals(Object) and
getClass() called without a receiver in a closure without a this object
fell back to the closure's own method. rehydrate(null, owner, null)
returns such a closure that still has an owner, and dynamic Groovy calls
the owner's method there:
@CompileStatic
class C {
String toString() { 'C' }
Closure<String> c() { { -> toString() } }
}
new C().c().rehydrate(null, new C(), null)() returned the closure's
toString() instead of "C". The static invocation writer now loads the
owner when the this object is null, and uses the closure only when the
owner is null too, as after dehydrate().
|
@paulk-asert Thanks.
Also merged master (8d1b545); it conflicted with GROOVY-12574 in |
✅ All tests passed ✅🏷️ Commit: 65c8326 Learn more about TestLens at testlens.app/docs. |
Under
@CompileStatic,toString(),hashCode(),equals()andgetClass()called without a receiver inside a closure or lambda now go to the delegate or the owner according to the resolve strategy, as they do in dynamic and@TypeCheckedcode since GROOVY-11858. Sonew StringBuilder().with { append('w'); toString() }returns'w', not the closure'stoString(). In a lambda,toString()is the enclosing instance's, as in Java; in a static method it is the class's. Methods thatClosuredeclares, such asgetDelegate()orgetMetaClass(), still resolve against the closure.toString()called inside a closure:@CompileStaticDynamic code started going to the delegate or owner with GROOVY-11858, which is on master and 6.0.x but not on 5.1.x. This PR lines SC up with that 6.0+ dynamic behaviour.
The resolve strategy is followed as at run time:
TO_SELF: the closure's own method.DELEGATE_FIRST,DELEGATE_ONLY: the delegate's.OWNER_FIRST,OWNER_ONLY: the owner's. When the owner is an enclosing closure, that is the enclosing closure's own method, so{ -> { -> toString() }() }()returns the outer closure'stoString().The type checker looked up the methods of
Closurebefore the delegate and the owner, and that lookup also returned the methodsClosureinherits fromObject. It now leaves those out unless the strategy isTO_SELF, and in a closure whose owner is a closure it resolves them against that owner. The static invocation writer calls a method ofObjectthat was resolved against the owner on the closure's this object, as it already did for other owner methods.A closure can have no this object.
rehydrate(null, owner, null)keeps only the owner, and the writer then calls the owner's method, as dynamic code does. Afterdehydrate()there is no owner either, and the closure's own method is called; dynamic code calls the method onnullthere.Tests:
ClosuresSTCTest.testClosureObjectMethodPrecedence,testClosureObjectMethodResolveStrategyandtestClosureObjectMethodWithoutThisObject(also run statically compiled byClosuresStaticCompileTest), andClosuresStaticCompileTest.testObjectMethodsInClosureWithoutThisObject, fail without the change. The closure test classes and thegroovy.transform.stcandorg.codehaus.groovy.classgen.asm.scpackages pass, apart fromMethodReferenceTest.testMethodReferencesInStaticAndDefaultMethodsOfInterface, which fails on master since #3056 (#3116 fixes it). The Checkstyle and CodeNarc gates pass.Backport to 6.0.x, not 5.1.x: on 5.1.x dynamic code still calls the closure's method, so backporting would split SC from dynamic there, and the
@TypeCheckedvariant oftestClosureObjectMethodPrecedencefails on 5.1.x.