daniellansun commented on PR #3367:
URL: https://github.com/apache/groovy/pull/3367#issuecomment-7238471240

   Thanks for the fix and the test matrix in `Groovy12839`. The overall 
approach makes sense, and the patch applies cleanly to `GROOVY_6_0_X` as well 
(the `GrabResolverTest` failure on JDK 25 is an unrelated Restlet repo hiccup).
   
   I ran into a few issues while going through the diff that we should probably 
sort out before merging:
   
   1. Dynamic 0-arg calls on primitive `Fixed1` closures throw 
`NullPointerException` instead of `MissingMethodException`. For example, `{ int 
i -> 'i:' + i }()`, `c.call()`, or `c.doCall()` from dynamic code hits 
`Fixed1.doCall(Object)` via `MetaClassImpl`'s implicit-null 0-arg match, which 
lands in `coerceSole(null)` and `loneNullRefused()`. A closure class (and 
`@CompileStatic` `c.call()`) throws `MissingMethodException` here. Overriding 
`getMethodWithCaching` / `getMethodWithoutCaching` in `PackedClosureMetaClass` 
to return `null` for 0-arg `call`/`doCall` on `Fixed1` would route these 
through `invokeMethod` -> `dispatchAll(EMPTY)` without affecting 1-arg dispatch 
or `Fixed1`'s declared signatures.
   
   2. In `destructure()`, calling 
`DefaultTypeTransformation.castToType(element, t)` breaks two edge cases and 
unnecessarily re-boxes exact primitive wrappers: `{ double d -> 'd:' + d 
}.call([Float.POSITIVE_INFINITY])` (or `NEGATIVE_INFINITY`) throws 
`GroovyRuntimeException` instead of widening to `double` infinity, and a custom 
`List#toArray()` returning a narrowed array like `Byte[]` or `Float[]` throws 
`ArrayStoreException` when writing the widened wrapper back into `elements[i]`. 
Since the generated dispatch tables already emit 
`DefaultTypeTransformation.<type>Unbox` (which handles exact wrappers and all 
JLS 5.1.2 widenings including float infinities), we can just drop that 
`castToType` write once `widens(...)` passes.
   
   3. `paramInfo` is lazily initialized without `volatile`, and 
`ParameterTypes` only stores `nativeParamTypes` in a `volatile` (not `final`) 
field, so under JLS 17.4 another thread on ARM64 can see `nativeParamTypes == 
null` and fail in `getPT()` with `UnsupportedOperationException`. On top of 
that, `ParameterTypes#setParametersTypes` writes `parameterTypes` before 
`isVargsMethod`, so the new `paramInfo().isValidMethod(Class[])` calls can see 
a stale `isVargsMethod == false` on concurrent first use of a vararg closure 
even on x86. Making `paramInfo` `volatile`, calling `info.getParameterTypes()` 
before publishing it, and using `isValidMethod(Object[])` instead of 
`isValidMethod(Class[])` avoids both races (and drops a few temporary `Class[]` 
allocations) at no hot-path cost.
   
   4. A few Javadocs and comments in `PackedClosure` are out of date now. 
`Fixed1.doCall` still says a null argument is passed through (it now throws 
`NullPointerException` for primitive parameters) and does not mention 
`IllegalArgumentException` from destructuring. `FixedIt.doCall` switched to 
`coerceSole(a)` without updating its Javadoc. `FixedN.doCall` is missing 
`NullPointerException` / `IllegalArgumentException`, and its note that a 
non-array 1-param 0-arg call binds null no longer holds for primitives. The 
inline comments on `paramInfo` (line 608) and `dispatchAll` (line 854) also 
still describe the old paths.
   
   5. In `Groovy12839.groovy`, it would be good to add 0-arg routes (`c()`, 
`c.call()`, `c.doCall()`), `byte`/`short`/`float` closures along with 
`Byte`/`Short`/`Float`/`BigInteger`/`Float.POSITIVE_INFINITY`/`NEGATIVE_INFINITY`
 and a narrowed-`toArray()` `List` (which also covers the remaining testable 
branches in `primitiveRank`), a custom non-`Runnable` SAM interface with `[{ -> 
}]` (since `Closure` already implements `Runnable`), a 5-parameter non-vararg 
closure, and a perturbed-MOP check (`use(Category)` / `DelegatingMetaClass`). 
Also on line 171, `{ ['four'] }` has `maximumNumberOfParameters == 1` (implicit 
`it`), so the `: make()` branch is dead; switching the 0-arg factories to `{ -> 
... }` fixes that. A brief comment on line 233 explaining why `array*` is 
excluded would be helpful too.
   
   A few smaller things, none strictly blocking:
   
   - `call(Object)` shrank nicely from 37 B to 31 B, though `coerceArg` went 
from 27 B to 36 B (just over `MaxInlineSize=35`). Combining the two `return` 
lines into `if (t == Object.class || t.isInstance(arg) || (arg == null && 
!t.isPrimitive())) return arg;` brings it back to 34 B. Also, primitive 1-arg 
calls like `{ int x -> }.call(1)` currently hit `arg instanceof List` in 
`coerceSoleSlow` on every call; checking the primitive wrapper match first in 
`coerceSoleSlow` avoids that.
   - In `doCallAdapted`, coercing directly into `all[base + i]` avoids writing 
back into the shared `MetaClassHelper.ARRAY_WITH_NULL` on 0-arg calls (and 
drops the dead `provided.length == 0 && arity == 1` branch). 
`FixedN.doCall(null)` can then reuse a static `SINGLE_NULL` array, and 
`coerceSoleSlow` can allocate `new Object[]{list}` only when throwing 
`MissingMethodException`.
   - Under `ExpandoMetaClass` (e.g. `enableGlobally()`), `Fixed2..4` 
single-`List` calls go through `MetaClassImpl#tryListParamMetaMethod` into the 
erased `doCall` and still coerce `GString` rather than throwing 
`IllegalArgumentException`, and lone `null` throws `NPE` vs `MME`. Fine to 
leave for a follow-up if out of scope here.
   - Replicating `ClosureMetaClass.java:289`'s accidental `NPE` makes sense for 
6.0.x parity, though a short cross-reference comment there would help keep the 
two in sync if it ever changes.
   - A brief `COMPATIBILITY.md` entry (like the 6.0.1 packed-closure ones) 
would be nice for the 6.0.2 backport, and caching the 6 compiled class sets / 
`Closure.getMethod('call', Object[].class)` (plus 
`@ResourceLock(Resources.SYSTEM_PROPERTIES)`) would speed up `Groovy12839`.
   


-- 
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]

Reply via email to