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.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]