[
https://issues.apache.org/jira/browse/GROOVY-8310?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16970953#comment-16970953
]
Eric Milles commented on GROOVY-8310:
-------------------------------------
If the matched method (bar in this case) has generics in its return type,
{{org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor#visitMethodCallExpression}}
goes into this block (circa line 3500):
{code:java}
if (isUsingGenericsOrIsArrayUsingGenerics(returnType)) {
visitMethodCallArguments(chosenReceiver.getType(),
argumentList, true, directMethodCallCandidate);
ClassNode irtg =
inferReturnTypeGenerics(chosenReceiver.getType(), directMethodCallCandidate,
callArguments, call.getGenericsTypes());
returnType = irtg != null &&
implementsInterfaceOrIsSubclassOf(irtg, returnType) ? irtg : returnType;
callArgsVisited = true;
}
{code}
This marks {{callArgsVisited}} and so it does not go into this block where the
error is generated for the other test case (circa 3550):
{code:java}
if (!callArgsVisited) {
MethodNode mn = (MethodNode)
call.getNodeMetaData(DIRECT_METHOD_CALL_TARGET);
visitMethodCallArguments(receiver, argumentList, true, mn);
// GROOVY-6219
if (mn != null) {
List<Expression> argExpressions =
argumentList.getExpressions();
Parameter[] parameters = mn.getParameters();
for (int i = 0; i < argExpressions.size() && i <
parameters.length; i += 1) {
Expression arg = argExpressions.get(i);
ClassNode pType = parameters[i].getType();
ClassNode aType = getType(arg);
if (CLOSURE_TYPE.equals(pType) &&
CLOSURE_TYPE.equals(aType)) {
if (!isAssignableTo(aType, pType)) {
addNoMatchingMethodError(receiver, name,
getArgumentTypes(argumentList), call);
call.removeNodeMetaData(DIRECT_METHOD_CALL_TARGET);
}
}
}
}
}
{code}
> Strange @CompileStatic check in Closure
> ----------------------------------------
>
> Key: GROOVY-8310
> URL: https://issues.apache.org/jira/browse/GROOVY-8310
> Project: Groovy
> Issue Type: Bug
> Components: Static compilation, Static Type Checker
> Reporter: Alexey Afanasiev
> Priority: Major
>
> This code compiles normal:
> {code}
> @CompileStatic
> class B {
> public <T> T bar(Closure<Collection<Integer>> a) {
> return null
> }
> def use() {
> bar {
> [1]
> }
> }
> }
> {code}
> Switching return type cause error in code:
> {code}
> import groovy.transform.CompileStatic
> @CompileStatic
> class B {
> public def bar(Closure<Collection<Integer>> a) {
> return null
> }
> def use() {
> bar { // Error:(21, 9) Groovyc: [Static type checking] - Cannot find
> matching method pack.B#bar(groovy.lang.Closure <java.util.List>).
> [1]
> }
> }
> }
> {code}
> I believe first example should have error too. But probably after fixing in
> that way following code will be broken:
> {code}
> def foo() {
> def nums = [1]
> def res = nums.collectMany { [it] }
> }
> {code}
--
This message was sent by Atlassian Jira
(v8.3.4#803005)