codeconsole opened a new pull request, #3367:
URL: https://github.com/apache/groovy/pull/3367

   A packed closure (GEP-27, `@PackedClosures` / `groovy.target.closure.pack`) 
is documented as adapting its arguments the way metaclass dispatch on a 
generated closure class does. For a single `List` argument it did not: it 
spread the list only when the closure had some other number of parameters than 
one, so
   
   ```groovy
   Closure<Integer> c = { String s -> s.length() }
   c.call(['four'])
   ```
   
   returns 4 as a closure class and threw `MissingMethodException` when packed. 
Spock passes a mock response closure its arguments as one list, so a packed `{ 
Request request -> ... }` response failed.
   
   ### The rule being replicated
   
   `ClosureMetaClass#invokeMethod` selects `doCall` for the arguments as given. 
Only when none applies and the call has exactly one argument, a `List`, it 
selects again for the list's elements and invokes that method with them through 
a `TransformMetaMethod`, which calls the method reflectively without adapting 
the arguments. So:
   
   * the list is the argument wherever the signature accepts it (`Object`, 
untyped, implicit `it`, `List`, `Collection`, `Iterable`, a trailing array 
whose component type the list is an instance of, `{ a, Object... rest -> }`);
   * otherwise its elements are the arguments, for any number of parameters;
   * elements no signature applies to are a `MissingMethodException`;
   * elements the selection accepts but the method cannot take unconverted (a 
`GString` for a `String`, no element for a parameter that a call without 
arguments would default to null, several elements for a trailing array) are the 
`IllegalArgumentException` of `Method.invoke`.
   
   ### The change
   
   All in `org.codehaus.groovy.runtime.PackedClosure`:
   
   * One-parameter closures: the single argument now goes through `coerceSole` 
on every route (`call(Object)`, an argument array of one, 
`Fixed1`/`FixedIt.doCall(Object)`). It makes the same tests `coerceArg` made; 
an argument that is not an instance of the parameter type goes out of line as 
before, and there a `List` the signature does not accept 
(`ParameterTypes#isValidMethod`) is destructured.
   * Other arities (`doCallAdapted`, already the out-of-line path): the list is 
destructured only when the signature does not accept it as it stands, where it 
was destructured unconditionally. `{ a, Object... rest -> }` called with a list 
now binds the list to `a`, as the closure class does.
   * `destructure` applies the second step of the rule above, including the two 
exceptions.
   * A single null: `FixedN.doCall(Object...)` treats a null array as one null 
argument (dynamic call sites pass a lone null that way; it was taken as no 
arguments), and a lone null no signature accepts (null for a primitive 
parameter, or for a closure of another arity) raises `NullPointerException`, 
which is what a closure class raises there. Null for a primitive parameter 
among several arguments is a `MissingMethodException`, as on a closure class; 
it was a `GroovyCastException`.
   
   Effect on the fast path: none for an argument the parameter accepts 
directly. `coerceArg` and `coerceSole` test `t == Object.class || 
t.isInstance(arg)` and return; a null for a reference-typed parameter takes one 
more inlined test (`!t.isPrimitive()`); everything else was already out of 
line. No allocation and no `ParameterTypes` lookup is added to any path that 
dispatched without one.
   
   ### Tests
   
   `src/test/groovy/bugs/Groovy12839.groovy` compiles 37 closure literals as 
closure classes and packed (by annotation and by the system property, dynamic 
and `@CompileStatic`), calls each with 24 single-list arguments, with null and 
with a few non-list values on five routes (`Closure#call(Object)`, 
`Closure#call(Object...)`, and `c.call(x)`, `c(x)`, `c.doCall(x)` from dynamic 
code), and asserts the same result or the same exception class. It also asserts 
that every literal of the packed compilation is a `PackedClosure`, and covers 
the mock-response shape directly. 16 of its 20 test invocations fail without 
the change.
   
   Run with JDK 21: the new test, the packed-closure tests 
(`PackedClosuresTransformTest`, `ClosurePackCapabilityTest`, 
`PackedClosureBoundariesTest`, `PackedClosureDebugMetadataTest`, 
`OpenClosureTest`, `PackedDispatcherFactoryTest`, `ClosureCallMopGuardTest`, 
`PackedClosureMetaClassTest`), `groovy.ClosureTest` and the other 
`groovy.Closure*` tests, `groovy.lang.*Closure*`, 
`org.codehaus.groovy.runtime.*Closure*`, the 
`org.codehaus.groovy.classgen.asm.sc` closure tests, `VArgsTest` and 
`VarargsMethodTest`: 2608 tests in 298 classes, 0 failures, 7 skipped (in four 
classes this change does not touch).
   
   ### Backport
   
   6.0.0 and 6.0.1 have the defect. The patch applies to `GROOVY_6_0_X` 
unchanged; please consider it for 6.0.2.
   
   ### Not changed
   
   A single argument that is an array, or a single value for a trailing-array 
parameter whose component type does not accept it, still differs between the 
two forms in places (for example `{ String[] a -> }` called with an `Object[]`, 
or with `1`: `MissingMethodException` as a class, converted or 
`IllegalArgumentException` packed). Those are not list destructuring and are 
left for a separate issue.
   
   https://issues.apache.org/jira/browse/GROOVY-12839
   


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