Repository navigation
GROOVY-12660: SC: do not pack a closure that calls a method on a closure object - #3181
codeconsole wants to merge 1 commit into
Conversation
β¦ure object With packing (@PackedClosures or groovy.target.closure.pack), a statically compiled closure is hoisted into a method of the owner class. A call that the type checker resolved against a closure object then went to the owner instead: - toString(), hashCode(), equals() and getClass() under TO_SELF called the owner's method, not the closure's; - the same methods in a nested closure, which resolve against the enclosing closure since GROOVY-12576, failed with MissingPropertyException: No such property: owner, because the nested closure got the owner instance as its owner; - methods that Closure declares, such as memoize() or dehydrate(), produced a VerifyError. The packing decision now declines a closure when an implicit-this call in its body, nested closures included, targets a Closure method, has a receiver typed as Closure (TO_SELF) or the receiver path "owner" (an enclosing closure). WARN and STRICT report the reason.
Codecov Reportβ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #3181 +/- ##
==================================================
+ Coverage 74.1533% 74.1597% +0.0064%
- Complexity 43751 43760 +9
==================================================
Files 1692 1692
Lines 149516 149534 +18
Branches 28366 28374 +8
==================================================
+ Hits 110871 110894 +23
+ Misses 28448 28447 -1
+ Partials 10197 10193 -4
π New features to boost your workflow:
|
β All tests passed βπ·οΈ Commit: ef5ec22 Learn more about TestLens at testlens.app/docs. |
daniellansun
left a comment
There was a problem hiding this comment.
LGTM for GROOVY-12660.
Under SC packing, an implicit-this call that STC resolved against a closure object (a Closure-declared method, a Closure-typed receiver under TO_SELF, or receiver path "owner" for a nested closure) was emitted against the owner once the body was hoisted β wrong results or VerifyError. Declining those in StaticTypesClosureWriter.isPackTriggered via callsClosureMethod, with a clear WARN/STRICT reason, is the right SC-side gate; owner-resolved calls still pack. Complements the dynamic counterpart in #3184.
Tests cover Object methods under TO_SELF, nested-closure owner resolution, Closure methods such as memoize(), and STRICT reporting; ClosuresStaticCompileTest.testClosureObjectMethodResolveStrategy passes with the packing flag. CI green.
Merge note: conflicts with #3187 only where both append helpers on StaticTypesClosureWriter β keep both. APPROVE.
Under
@CompileStaticwith closure packing (@PackedClosuresor-Dgroovy.target.closure.pack=true), a closure body is hoisted into a method of the owner class. A call that the type checker resolved against a closure object then went to the owner:Without packing, and in dynamic Groovy,
a()returns the closure's owntoString(),b()returns the outer closure'stoString()twice, andc()returns the memoized closure. Seen in 6.0.0 too.StaticTypesClosureWriter.callsClosureMethodnow makes the packing decision decline when an implicit-this call in the body, nested closures included, targets a method thatClosuredeclares, has the receiver typeClosurethat STC records underTO_SELF, or has the receiver path"owner"that STC records for an enclosing closure.WARNandSTRICTreport the reason. Calls resolved against the owner class still pack.With #3187 (GROOVY-12666),
StaticTypesClosureWriterconflicts where this PR addscallsClosureMethodand #3187 addshoistedBodyUsesReceiver: keep both methods, each with its own Javadoc.PackedClosuresTransformTestconflicts with the escape-walk stack only where both append tests: keep both.Tests
PackedClosuresTransformTest.closuresCallingObjectMethodsOnAClosureDecline,closuresCallingClosureMethodsDeclineandstrictModeReportsClosureReceiverDeclinefail without the change. With-Pgroovy.target.closure.pack=true,ClosuresStaticCompileTest.testClosureObjectMethodResolveStrategynow passes. The packing test classes,org.codehaus.groovy.classgen.asm.sc.*andgroovy.transform.stc.*pass without the flag; with it, the only failures are ones that also fail on master and have their own issues. The Checkstyle and CodeNarc gates pass.