[
https://issues.apache.org/jira/browse/GROOVY-12840?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18152375#comment-18152375
]
ASF GitHub Bot commented on GROOVY-12840:
-----------------------------------------
daniellansun commented on PR #3368:
URL: https://github.com/apache/groovy/pull/3368#issuecomment-7238930302
Thanks for the fix and the tests. The `av instanceof Parameter || av
instanceof VariableExpression` check in `ClosureWriter#isPackable` matches how
`VariableScopeVisitor` binds lexical variables (including `for`, `catch`,
`try`-with-resources, pattern variables, and script locals), and the patch
applies cleanly to `GROOVY_6_0_X` as well (the `GrabResolverTest` failure on
JDK 25 is the same unrelated Restlet repo hiccup).
I noticed a few things while testing this that we should address before
merging:
1. Dynamic `@PackedClosures` (without `@CompileStatic`) is still broken for
these same names, including `PackMode.STRICT` and `WARN`. When `annotated` is
true, `ClosureWriter#isPackTriggered` calls `callsClosureMethod(expression)`,
and `callsClosureMethod` at line 567 checks
`CLOSURE_PROPERTY_NAMES.contains(ve.getName())` without checking
`ve.getAccessedVariable()`. Since `CLOSURE_PROPERTY_NAMES` is built from all
0-arg `Closure` getters, it contains all six `FORBIDDEN_CLOSURE_NAMES` plus
`maximumNumberOfParameters` and `parameterTypes`. So under dynamic
`@PackedClosures`, `{ String pet -> owner + ' owns ' + pet }` still does not
pack, and under `WARN`/`STRICT` it now fails with a misleading message saying
the closure calls a method on a closure object (and in `nested(String owner)`,
the inner closure still stays a closure class). Adding the same `!(av
instanceof Parameter || av instanceof VariableExpression)` guard at line 567
(or extracting a small helper) fixes all of these.
2. Because `callsClosureMethod` unconditionally declines `owner` by name
under dynamic `@PackedClosures`,
`fieldNamedLikeAClosurePropertyStillDeclinesPacking` currently passes
regardless of what `isPackable` does for `FieldNode`. Adding a `@CompileStatic`
case (such as `public String owner = 'field'`, where `isPackable` is the only
guard) along with dynamic `@PackedClosures` tests (`LENIENT` and `STRICT`) for
parameters and locals would make sure both gates are actually exercised.
3. `maximumNumberOfParameters` and `parameterTypes` are in
`CLOSURE_PROPERTY_NAMES` and their getters are in `FORBIDDEN_CLOSURE_CALLS`,
but they are missing from `FORBIDDEN_CLOSURE_NAMES`. With the local/parameter
guard in place, it seems worth adding them to `FORBIDDEN_CLOSURE_NAMES` too (or
checking `CLOSURE_PROPERTY_NAMES` directly in `isPackable` and dropping the
duplicate check in `callsClosureMethod`): right now, bare `{ int x ->
maximumNumberOfParameters }` fails with `MissingPropertyException` under
`-Dgroovy.closure.open=true`, and under `@CompileStatic` a class with `public
int maximumNumberOfParameters = 99` packs and returns `99` instead of `1` (same
for `public Class[] parameterTypes`).
A few smaller things, mostly around tests and docs:
- In `ClosurePackCapabilityTest` (or `PackedClosuresTransformTest`), it
would be good to also cover written/mutated captures (`owner = ...`, `delegate
= ...`, `resolveStrategy += ...`), `maximumNumberOfParameters` /
`parameterTypes` as parameter or local names, and a `closureClassCount` check
on `nested(String owner)` to confirm the inner closure packs too. Using `mode
?: 'dynamic'` on the `assertEquals(..., mode)` lines avoids an empty failure
label on the dynamic run.
- Updating `PackedClosures.java` line 42 slightly to clarify that it
declines references to `Closure`'s `owner`/`delegate`/... properties (not a
parameter or local of the same name) would keep the Javadoc accurate.
- One pre-existing edge case I ran into while testing: under
`@CompileStatic`, if a closure has a local or parameter named `thisObject` and
calls an enclosing-class method (like `greet()`), a classic closure class
resolves `StaticInvocationWriter#thisObjectExpression`'s `varX("thisObject")`
against that local/param instead of `Closure.getThisObject()` (throwing
`ClassCastException` for `String thisObject`, or calling `greet()` on the wrong
`Host` instance), whereas the packed closure calls `greet()` on the real
enclosing receiver. The packed behavior is actually the right one, so this is
not a bug in your patch, just worth keeping in mind as a separate
`StaticInvocationWriter` issue if parity tests ever hit that shape.
> Closure packing declines a closure that reads a parameter or local variable
> named owner, delegate or thisObject
> ---------------------------------------------------------------------------------------------------------------
>
> Key: GROOVY-12840
> URL: https://issues.apache.org/jira/browse/GROOVY-12840
> Project: Groovy
> Issue Type: Bug
> Components: Compiler
> Affects Versions: 6.0.0, 6.0.1
> Reporter: Scott Murphy Heiberg
> Priority: Blocker
>
> The packability analysis (ClosureWriter#isPackable) keeps a closure as a
> class when its body names one of the Closure pseudo-properties: owner,
> delegate, thisObject, directive, resolveStrategy, metaClass. The test is on
> the name of every VariableExpression alone, so it also declines a closure
> that reads a method parameter or a local variable of that name. Such a
> variable hides the Closure property in a closure class too, so the body does
> not use the property and nothing prevents packing it.
> {code:groovy}
> import groovy.transform.CompileStatic
> import groovy.transform.PackedClosures
> @CompileStatic
> @PackedClosures
> class OwnerName {
> static String greet(String owner, List<String> pets) {
> Closure<String> c = { String pet -> owner + ' owns ' + pet }
> println c.getClass().name
> pets.collect(c).join(', ')
> }
> static String other(String name, List<String> pets) {
> Closure<String> c = { String pet -> name + ' owns ' + pet }
> println c.getClass().name
> pets.collect(c).join(', ')
> }
> static void main(String[] args) {
> println greet('Ann', ['Rex'])
> println other('Ann', ['Rex'])
> }
> }
> {code}
> Actual (6.0.0, 6.0.1 and master, 7.0.0-SNAPSHOT):
> {noformat}
> OwnerName$_greet_closure1
> Ann owns Rex
> org.codehaus.groovy.runtime.PackedClosure$Fixed1
> Ann owns Rex
> {noformat}
> Expected: both closures packed (PackedClosure$Fixed1 twice). The results are
> right either way; the cost is a closure class that need not exist, a warning
> with @PackedClosures(mode = WARN) or groovy.target.closure.pack.report, and a
> compile error with @PackedClosures(mode = STRICT), whose message says the
> closure "needs a real Closure (uses
> owner/delegate/thisObject/resolveStrategy/super or metaClass, ...)" although
> it uses none of them. The same with dynamic compilation.
> Proposed fix (pull request to follow)
> In the visitVariableExpression of ClosureWriter#isPackable, decline for one
> of those names only when the expression's accessed variable is not a
> Parameter or a declared local variable (a VariableExpression). A name bound
> to a field or property of the enclosing class, or to nothing, still reads the
> Closure property in a closure class and keeps declining.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)