Skip to content

GROOVY-12660: SC: do not pack a closure that calls a method on a closure object - #3181

Open
codeconsole wants to merge 1 commit into
apache:masterfrom
codeconsole:pack-closure-receiver
Open

codeconsole wants to merge 1 commit into
apache:masterfrom
codeconsole:pack-closure-receiver

Conversation

@codeconsole

@codeconsole codeconsole commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Under @CompileStatic with closure packing (@PackedClosures or -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:

@groovy.transform.CompileStatic
class C {
    String toString() { 'C' }
    static Object self(@DelegatesTo(value=String, strategy=Closure.TO_SELF) Closure c) { c.resolveStrategy = Closure.TO_SELF; c() }
    Object a() { self { -> toString() } }                                                          // packed: 'C'
    Object b() { Closure<String> outer = { -> { -> toString() }() }; [outer(), outer.toString()] } // packed: MissingPropertyException: owner
    Object c() { Closure c = { -> memoize() }; c() }                                               // packed: VerifyError
}

Without packing, and in dynamic Groovy, a() returns the closure's own toString(), b() returns the outer closure's toString() twice, and c() returns the memoized closure. Seen in 6.0.0 too.

StaticTypesClosureWriter.callsClosureMethod now makes the packing decision decline when an implicit-this call in the body, nested closures included, targets a method that Closure declares, has the receiver type Closure that STC records under TO_SELF, or has the receiver path "owner" that STC records for an enclosing closure. WARN and STRICT report the reason. Calls resolved against the owner class still pack.

With #3187 (GROOVY-12666), StaticTypesClosureWriter conflicts where this PR adds callsClosureMethod and #3187 adds hoistedBodyUsesReceiver: keep both methods, each with its own Javadoc. PackedClosuresTransformTest conflicts with the escape-walk stack only where both append tests: keep both.

Tests

PackedClosuresTransformTest.closuresCallingObjectMethodsOnAClosureDecline, closuresCallingClosureMethodsDecline and strictModeReportsClosureReceiverDecline fail without the change. With -Pgroovy.target.closure.pack=true, ClosuresStaticCompileTest.testClosureObjectMethodResolveStrategy now passes. The packing test classes, org.codehaus.groovy.classgen.asm.sc.* and groovy.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.

…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-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 83.33333% with 3 lines in your changes missing coverage. Please review.
βœ… Project coverage is 74.1597%. Comparing base (0941dba) to head (ef5ec22).
⚠️ Report is 11 commits behind head on master.

Files with missing lines Patch % Lines
...oovy/classgen/asm/sc/StaticTypesClosureWriter.java 83.3333% 0 Missing and 3 partials ⚠️
Additional details and impacted files

Impacted file tree graph

@@                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     
Files with missing lines Coverage Ξ”
...oovy/classgen/asm/sc/StaticTypesClosureWriter.java 66.4234% <83.3333%> (+4.2385%) ⬆️

... and 13 files with indirect coverage changes

πŸš€ New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • πŸ“¦ JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@testlens-app

testlens-app Bot commented Nov 3, 2026

Copy link
Copy Markdown

βœ… All tests passed βœ…

🏷️ Commit: ef5ec22
▢️ Tests: 127314 executed
βšͺ️ Checks: 26/26 completed


Learn more about TestLens at testlens.app/docs.

@daniellansun daniellansun left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants