Skip to content

GROOVY-12656: decline closures that escape through a composition from packing - #3177

Open
codeconsole wants to merge 30 commits into
apache:masterfrom
codeconsole:pack-composed-escapes
Open

codeconsole wants to merge 30 commits into
apache:masterfrom
codeconsole:pack-composed-escapes

Conversation

@codeconsole

@codeconsole codeconsole commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

With closure packing (@PackedClosures or -Dgroovy.target.closure.pack=true), the escape walk in ClosureWriter.collectEscapingClosures marked only a closure literal that is the stored, appended or returned value itself. A literal composed with >> or << was packed even when the composition escapes:

class Holder { Closure handler }

@groovy.transform.PackedClosures
class C {
    void install(Holder h, Closure c) { h.handler = c >> { x -> x + 1 } }
    Closure make(Closure c) { return { x -> x + 1 } << c }
}

def h = new Holder()
new C().install(h, { x -> x * 2 })
h.handler.delegate = 'd'   // UnsupportedOperationException: Cannot set a delegate on a @PackedClosures closure

ComposedClosure keeps a clone of each operand, and the clone of a packed closure is still packed. It passes setDelegate on to them, so the literal escapes with the composition. Seen in 6.0.0 too.

The walk now follows a value that is marked as escaping through >> and <<, nested compositions included, and through one-argument rightShift and leftShift calls. This covers a store to a field, property or index, a << append, a return, and a list or map literal. A composition used only in the method still packs, for example (c >> { x -> x + 1 })(n). A standalone leftShift(...) call is still not an escape.

Under @CompileStatic, BinaryExpressionTransformer turns c >> { ... } into a rightShift call before class generation, and the walk follows that form. Returned compositions, compositions stored to a field of the same class, and compositions in collection literals are therefore declined in every mode. A composition stored through a property setter or appended with << is rewritten into a setter or leftShift call; under @CompileStatic those shapes are declined through #3171 (GROOVY-12650), which runs the escape walk before that rewrite. This PR stacks on #3176 and the PRs below it.

Stack: #3171 → #3173 → #3174 → #3172 → #3176 → #3177 → #3180 → #3184. Each branch merges the one before it, so until a base lands, a later PR's diff also shows the base's commits. Merging the top PR merges the whole stack.

The walk also follows the method forms andThen and compose (aliases of >> and <<) and andThenSelf and composeSelf, so a closure composed through them and then stored or returned keeps its own class instead of failing on setDelegate. PackedClosuresTransformTest.namedCompositionMethodsEscapeWhereTheCompositionDoes covers them in dynamic, @TypeChecked and @CompileStatic code and fails without that change.

Tests

PackedClosuresTransformTest.composedClosuresEscapeWhereTheCompositionDoes (dynamic and @TypeChecked), PackedClosuresTransformTest.compileStaticComposedEscapesAreDeclined and a new 'returned composed' row in PackedClosureBoundariesTest.boundaries fail without the change. The closure-packing test classes, org.codehaus.groovy.classgen.asm.sc.* and groovy.transform.stc.* (including ClosuresSTCTest) pass. The Checkstyle and CodeNarc gates pass.

…atic

The static compilation transformers turn `list << { ... }` and a
setter-backed `obj.prop = { ... }` into method calls before class
generation, so the escape walk no longer saw those stores and packed the
closures. Record the escaping closure literals before the method body is
transformed and have the escape gate honour that mark.
…ents from packing

The escape gate treated only = on a non-local target and << as stores, so
p.handler ?= { x -> x }, ??=, <<=, >>= and += packed the closure and a
delegate later set on the stored value failed. Treat ?=, ??=, >>= and +=
like = on a field/property/index, and <<= like <<.
…nd initializer blocks

WriterController.getMethodNode() is null while a constructor is written, so
the escape and serialization gates and the @PackedClosures mode lookup saw no
enclosing method and packed a closure that escapes a constructor or an
instance initializer block (which the Verifier inlines into each
constructor). They now use the constructor node. @PackedClosures may now be
placed on a constructor; its scope includes the initializer blocks inlined
into it. A field initializer keeps its own decline reason.
…structor

The written-names lookup that decides whether a packed closure's capture is
threaded as the shared Reference also read WriterController.getMethodNode(),
which is null inside a constructor. A local written after the literal in a
constructor or an instance initializer block was copied by value, so the
closure saw its stale value. Use the constructor node there too.
@codecov-commenter

codecov-commenter commented Nov 3, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 87.74725% with 223 lines in your changes missing coverage. Please review.
✅ Project coverage is 74.4239%. Comparing base (0941dba) to head (7422273).
⚠️ Report is 121 commits behind head on master.

Files with missing lines Patch % Lines
...us/groovy/runtime/m12n/ExtensionModuleScanner.java 41.2698% 29 Missing and 8 partials ⚠️
...va/org/apache/groovy/parser/antlr4/AstBuilder.java 85.4054% 13 Missing and 14 partials ⚠️
...roovy/transform/stc/StaticTypeCheckingVisitor.java 91.4474% 2 Missing and 24 partials ⚠️
...org/apache/groovy/ast/pattern/PatternLowering.java 90.7692% 4 Missing and 20 partials ⚠️
...va/org/apache/groovy/ast/pattern/MatchPattern.java 83.2061% 7 Missing and 15 partials ⚠️
...m/sc/transformers/BinaryExpressionTransformer.java 90.3226% 1 Missing and 11 partials ⚠️
src/main/groovy/groovy/grape/GrapeUtil.groovy 68.4210% 3 Missing and 3 partials ⚠️
...codehaus/groovy/classgen/VariableScopeVisitor.java 80.6452% 4 Missing and 2 partials ⚠️
...sc/StaticTypesMethodReferenceExpressionWriter.java 81.8182% 1 Missing and 5 partials ⚠️
...rg/apache/groovy/runtime/RecordPatternSupport.java 76.1905% 2 Missing and 3 partials ⚠️
... and 23 more
Additional details and impacted files

Impacted file tree graph

@@                Coverage Diff                 @@
##               master      #3177        +/-   ##
==================================================
+ Coverage     74.1533%   74.4239%   +0.2707%     
- Complexity      43751      44413       +662     
==================================================
  Files            1692       1699         +7     
  Lines          149516     151110      +1594     
  Branches        28366      28800       +434     
==================================================
+ Hits           110871     112462      +1591     
+ Misses          28448      28378        -70     
- Partials        10197      10270        +73     
Files with missing lines Coverage Δ
src/main/java/groovy/lang/Closure.java 86.0170% <100.0000%> (+1.0496%) ⬆️
src/main/java/groovy/lang/MetaClassImpl.java 79.3315% <ø> (+0.1671%) ⬆️
src/main/java/groovy/lang/TrampolineClosure.java 96.1538% <100.0000%> (+13.8009%) ⬆️
src/main/java/groovy/transform/PackedClosures.java 100.0000% <ø> (ø)
...g/apache/groovy/parser/antlr4/GroovyLangLexer.java 100.0000% <100.0000%> (ø)
.../apache/groovy/parser/antlr4/GroovyLangParser.java 60.0000% <100.0000%> (+20.0000%) ⬆️
.../org/apache/groovy/runtime/ListPatternSupport.java 100.0000% <100.0000%> (ø)
...a/org/apache/groovy/runtime/MapPatternSupport.java 100.0000% <100.0000%> (ø)
...ava/org/codehaus/groovy/ast/GroovyCodeVisitor.java 66.6667% <100.0000%> (+14.2857%) ⬆️
...rg/codehaus/groovy/classgen/AsmClassGenerator.java 85.9986% <100.0000%> (+0.1112%) ⬆️
... and 76 more

... and 41 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.

# Conflicts:
#	src/main/java/org/codehaus/groovy/classgen/asm/ClosureWriter.java
#	src/test/groovy/org/codehaus/groovy/transform/PackedClosuresTransformTest.groovy
…s and initializer blocks

The escape marking that runs ahead of the static compilation rewrites
(GROOVY-12650) skipped constructors and never saw instance initializer
blocks, so under @CompileStatic an append (`s << { ... }`) or a
setter-backed store (`h.handler = { ... }`) there was still packed.
StaticCompilationTransformer now marks constructors as it marks methods,
and marks the initializer blocks when the flag, the class or one of its
constructors requests packing.
# Conflicts:
#	src/test/groovy/org/codehaus/groovy/transform/PackedClosuresTransformTest.groovy
…osures

# Conflicts:
#	src/main/java/org/codehaus/groovy/transform/sc/transformers/StaticCompilationTransformer.java
…r-escapes

# Conflicts:
#	src/main/java/org/codehaus/groovy/transform/sc/transformers/StaticCompilationTransformer.java

@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-12656 (on tip of #3176 and below).

A literal composed with >> / << (or the one-arg rightShift / leftShift call form SC emits) was packed even when the composition itself escapes. ComposedClosure keeps a clone of each operand and forwards setDelegate, so the packed adapter still escapes. Teaching mark to follow composition operands (nested included) while leaving an in-method-only composition packable is the correct precision. Standalone leftShift(...) remains a non-escape, as before.

SC property-setter / <<-append rewrites of composed values still rely on #3171’s pre-rewrite mark; returned / same-class field / collection-literal compositions decline in every mode via this walk.

Tests cover dynamic / @TypeChecked / @CompileStatic composed escapes and a returned-composed boundary row. CI green.

Land after #3176. APPROVE.

@testlens-app

testlens-app Bot commented Nov 8, 2026 •

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: 7422273
▶️ Tests: 114784 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.

Thanks, Scott. I re-reviewed the unique escape walk on tip 742227326d78.

Widening isCompositionCall from the one-argument rightShift / leftShift form to andThen, compose, andThenSelf and composeSelf is the right follow-up: those methods also produce a ComposedClosure that keeps a clone of the literal, so a stored or returned composition must not pack. Following every argument (none for andThenSelf()) matches that. An in-method-only andThen still packs, as the new test shows for dynamic, @TypeChecked and @CompileStatic.

The @Internal markers on markEscapingClosures and the JIRA comments on the earlier tests in this stack are fine. Looks good to me.

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