Skip to content

GROOVY-12651: decline closures stored or composed by compound assignments from packing - #3172

Open
codeconsole wants to merge 21 commits into
apache:masterfrom
codeconsole:pack-compound-escapes
Open

codeconsole wants to merge 21 commits into
apache:masterfrom
codeconsole:pack-compound-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 compile-time escape gate in ClosureWriter.collectEscapingClosures treated only = on a non-local target and << as escapes. A literal stored or composed into a field, property or index by a compound assignment was packed:

@groovy.transform.PackedClosures
class C {
    void install(Holder h) { h.handler ?= { x -> x * 2 } }
}
class Holder { Closure handler }

def h = new Holder()
new C().install(h)
h.handler.delegate = 'some delegate'   // UnsupportedOperationException: Cannot set a delegate on a @PackedClosures closure

?= and ??= store the literal. <<= and >>= on a Closure slot store a ComposedClosure, which keeps a clone of the literal (still a packed adapter) and passes setDelegate on to it. <<= on a collection appends the literal, and += stores a new collection that contains it. The walk now treats ?=, ??=, >>= and += like = when the target is not a local, and <<= like <<. The other compound operators do not leave the literal in the stored value for the standard types, so they still pack, as do stores into a local.

Under @CompileStatic, <<=, >>= and += are rewritten into method calls before class generation, so they are declined through #3171 (GROOVY-12650), which runs the escape walk before that rewrite. This PR stacks on #3174, which builds on #3173 and #3171. 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. ?= and ??= keep their shape and are fixed here in every mode.

Tests

PackedClosuresTransformTest.compoundAssignmentEscapesAreDeclined (dynamic and @TypeChecked), PackedClosuresTransformTest.compileStaticCompoundStoresAreDeclined and two new rows in PackedClosureBoundariesTest.boundaries fail without the change. The 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.74265% with 221 lines in your changes missing coverage. Please review.
✅ Project coverage is 74.4154%. Comparing base (0941dba) to head (c96b658).
⚠️ 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      #3172        +/-   ##
==================================================
+ Coverage     74.1533%   74.4154%   +0.2622%     
- Complexity      43751      44396       +645     
==================================================
  Files            1692       1699         +7     
  Lines          149516     151095      +1579     
  Branches        28366      28795       +429     
==================================================
+ Hits           110871     112438      +1567     
+ Misses          28448      28386        -62     
- Partials        10197      10271        +74     
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 39 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-12651 (on tip of #3174 → #3173 → #3171).

The escape walk only treated = and << as stores/appends. Compound forms that leave the literal (or a ComposedClosure holding a clone of it) in a non-local slot — ?=, ??=, >>=, +=, and <<= — were packed, then failed the packed adapter’s delegate guard at run time. Extending visitBinaryExpression via storesRightOperand / LEFT_SHIFT_EQUAL, while leaving operators that do not retain the literal alone, matches the packing contract. Under @CompileStatic, <<= / >>= / += are rewritten to calls and are declined through #3171’s pre-rewrite mark; ?= / ??= keep their shape and are fixed here in every mode.

Tests cover dynamic / @TypeChecked compound escapes, the SC compound-store path, and boundary rows. CI green.

Land after #3174. APPROVE.

@testlens-app

testlens-app Bot commented Nov 8, 2026 •

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: c96b658
▶️ Tests: 117261 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 tip c96b658c8d4559efc05ca024c76a721ddd328e6f against my earlier approval on 2bcaeff5bd61.

The GROOVY-12651 change is intact: compound assignments that leave the literal or a composed closure in a non-local slot are still declined, and operators that do not retain the literal alone are still left alone. The only unique-patch changes since the approval are @Internal on both ClosureWriter.markEscapingClosures overloads and JIRA markers in the tests.

CI is green on this tip. Looks good to me. This still sits above #3174 in the escape-gate stack.

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