Repository navigation
GROOVY-12656: decline closures that escape through a composition from packing - #3177
codeconsole wants to merge 30 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.
… dynamic property from packing
# 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-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.
# Conflicts: # src/test/groovy/org/codehaus/groovy/transform/PackedClosuresTransformTest.groovy
…andThenSelf or composeSelf
✅ All tests passed ✅🏷️ Commit: 7422273 Learn more about TestLens at testlens.app/docs. |
daniellansun
left a comment
There was a problem hiding this comment.
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.
With closure packing (
@PackedClosuresor-Dgroovy.target.closure.pack=true), the escape walk inClosureWriter.collectEscapingClosuresmarked 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:ComposedClosurekeeps a clone of each operand, and the clone of a packed closure is still packed. It passessetDelegateon 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-argumentrightShiftandleftShiftcalls. This covers a store to a field, property or index, a<<append, areturn, and a list or map literal. A composition used only in the method still packs, for example(c >> { x -> x + 1 })(n). A standaloneleftShift(...)call is still not an escape.Under
@CompileStatic,BinaryExpressionTransformerturnsc >> { ... }into arightShiftcall 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 orleftShiftcall; under@CompileStaticthose 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
andThenandcompose(aliases of>>and<<) andandThenSelfandcomposeSelf, so a closure composed through them and then stored or returned keeps its own class instead of failing onsetDelegate.PackedClosuresTransformTest.namedCompositionMethodsEscapeWhereTheCompositionDoescovers them in dynamic,@TypeCheckedand@CompileStaticcode and fails without that change.Tests
PackedClosuresTransformTest.composedClosuresEscapeWhereTheCompositionDoes(dynamic and@TypeChecked),PackedClosuresTransformTest.compileStaticComposedEscapesAreDeclinedand a new 'returned composed' row inPackedClosureBoundariesTest.boundariesfail without the change. The closure-packing test classes,org.codehaus.groovy.classgen.asm.sc.*andgroovy.transform.stc.*(includingClosuresSTCTest) pass. The Checkstyle and CodeNarc gates pass.