Repository navigation
GROOVY-12651: decline closures stored or composed by compound assignments from packing - #3172
codeconsole wants to merge 21 commits into
Conversation
…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.
# 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
left a comment
There was a problem hiding this comment.
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.
# Conflicts: # src/test/groovy/org/codehaus/groovy/transform/PackedClosuresTransformTest.groovy
✅ All tests passed ✅🏷️ Commit: c96b658 Learn more about TestLens at testlens.app/docs. |
daniellansun
left a comment
There was a problem hiding this comment.
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.
With closure packing (
@PackedClosuresor-Dgroovy.target.closure.pack=true), the compile-time escape gate inClosureWriter.collectEscapingClosurestreated 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:?=and??=store the literal.<<=and>>=on a Closure slot store aComposedClosure, which keeps a clone of the literal (still a packed adapter) and passessetDelegateon 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.compileStaticCompoundStoresAreDeclinedand two new rows inPackedClosureBoundariesTest.boundariesfail without the change. The packing test classes,org.codehaus.groovy.classgen.asm.sc.*andgroovy.transform.stc.*(includingClosuresSTCTest) pass. The Checkstyle and CodeNarc gates pass.