Scott Murphy Heiberg created GROOVY-12839:
---------------------------------------------
Summary: 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.1, 6.0.0
Reporter: Scott Murphy Heiberg
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)