[
https://issues.apache.org/jira/browse/GROOVY-12839?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18152304#comment-18152304
]
ASF GitHub Bot commented on GROOVY-12839:
-----------------------------------------
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
> Packed closure with one typed parameter rejects a single List argument that a
> closure class destructures
> --------------------------------------------------------------------------------------------------------
>
> Key: GROOVY-12839
> URL: https://issues.apache.org/jira/browse/GROOVY-12839
> Project: Groovy
> Issue Type: Bug
> Components: groovy-runtime
> Affects Versions: 6.0.0, 6.0.1
> Reporter: Scott Murphy Heiberg
> Priority: Blocker
>
> GEP-27 closure packing (@PackedClosures, -Dgroovy.target.closure.pack=true;
> incubating) is meant to be indistinguishable from the closure class the same
> literal compiles to. The class comment of
> org.codehaus.groovy.runtime.PackedClosure says:
> {quote}
> Argument adaptation on every route replicates metaclass dispatch on a
> generated closure class: arity mismatches raise MissingMethodException, a
> single List destructures across a non-one-parameter signature, trailing-array
> parameters vararg-collect, and per-parameter coercion follows the metaclass
> compatibility rules
> {quote}
> A generated closure class does not limit the destructuring to "a
> non-one-parameter signature". ClosureMetaClass#invokeMethod selects doCall
> for the arguments as given, and when no doCall applies and the call has
> exactly one argument that is a List, selects again for the elements of the
> list and invokes that method with them. So a closure with one typed parameter
> that is not a supertype of the list takes a one-element list as it takes the
> element. The packed form keeps the list as the argument and fails.
> This is how Spock invokes a mock response closure (the arguments of the
> invocation as one List), so a packed response closure with one typed
> parameter fails. It was found that way.
> {code:groovy}
> class ListArg {
> static void main(String[] args) {
> Closure<Integer> c = { String s -> s.length() }
> println c.call(['four'])
> }
> }
> {code}
> Expected (and what {{java -cp groovy-6.0.1.jar groovy.ui.GroovyMain
> ListArg.groovy}} prints):
> {noformat}
> 4
> {noformat}
> Actual with {{-Dgroovy.target.closure.pack=true}}, or with
> @groovy.transform.PackedClosures on the class, dynamic or @CompileStatic:
> {noformat}
> groovy.lang.MissingMethodException: No signature of method: doCall for class:
> org.codehaus.groovy.runtime.PackedClosure$Fixed1 is applicable for argument
> types: (ArrayList) values: [[four]]
> {noformat}
> The same on every route: Closure#call(Object), Closure#call(Object...),
> c(['four']), c.call(['four']) and c.doCall(['four']) from dynamic code.
> Reproduced with 6.0.0, 6.0.1 and master (7.0.0-SNAPSHOT).
> Comparing each literal compiled both ways, with a single List or a single
> null as the argument, shows the other places where the two differ for the
> same reason (the packed form does not follow the two-step rule):
> || closure || argument || closure class || packed ||
> | \{ String s -> \} | ['four'] | s = 'four' | MissingMethodException |
> | \{ String s -> \} | [null] | s = null | MissingMethodException |
> | \{ int i -> \} | [1] | i = 1 | MissingMethodException |
> | \{ String[] a -> \} | [null], or a list holding one String[] | a = the
> element | IllegalArgumentException |
> | \{ String[] a -> \} | [1] | MissingMethodException |
> IllegalArgumentException |
> | \{ a, Object... b -> \} | ['four'] | a = ['four'], b = [] (the list is
> accepted as it stands) | a = 'four', b = [] |
> | \{ List a, Object... b -> \} | ['four'] | a = ['four'], b = [] |
> MissingMethodException |
> | \{ String a, String b -> \} | a list of a String and a GString |
> IllegalArgumentException (the elements are passed unconverted) | binds,
> converting the GString |
> | \{ String... a -> \}, \{ a, Object... b -> \} | null, called from dynamic
> code | a = null | a = [] (first), MissingMethodException (second) |
> | \{ int i -> \} | null | NullPointerException | GroovyCastException |
> | \{ -> \}, \{ a, b -> \} | null | NullPointerException |
> MissingMethodException |
> Closures whose single parameter accepts the list (Object, untyped, implicit
> it, List, Collection, Iterable, an array of a type the list is an instance
> of) already agree: the parameter is the list.
> Proposed fix (pull request to follow)
> In PackedClosure, destructure a single List argument exactly when the
> signature does not accept the list as it stands
> (ParameterTypes#isValidMethod, the test ClosureMetaClass applies), for any
> number of parameters, and then treat the elements as ClosureMetaClass does:
> MissingMethodException when no signature applies to them, no conversion
> otherwise. The check sits behind the existing "argument is not an instance of
> the parameter type" test, so a call whose argument the parameter takes
> directly costs what it did.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)