groovy-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Eric Milles (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (GROOVY-9058) each parameter type not correctly inferenced
Date Wed, 27 Mar 2019 17:29:00 GMT

    [ https://issues.apache.org/jira/browse/GROOVY-9058?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16803107#comment-16803107
] 

Eric Milles commented on GROOVY-9058:
-------------------------------------

I think the issue lies within {{StaticTypeCheckingVisitor.doInferClosureParameterTypes}}.
 STC has determined that the type of the closure parameter is Object[] however, lastArg is
true and the array's component type matches the origin type of the parameter (Object since
no explicit type was given in the source). So Object is saved instead of Object[].

Adding a check {{!closureParam.isDynamicTyped()}} seems to be the ticket.  Not sure if this
causes any undesirable effects.


{code:java}
                    boolean lastArg = (i == length - 1);
                    if (lastArg && inferredType.isArray()) {
                        if (/*GRECLIPSE add*/!closureParam.isDynamicTyped() && /*GRECLIPSE
end*/inferredType.getComponentType().equals(originType)) {
                            inferredType = originType;
                        }
                    } else if (!typeCheckMethodArgumentWithGenerics(originType, inferredType,
lastArg)) {
                        addError("Expected parameter of type " + inferredType.toString(false)
+ " but got " + originType.toString(false), closureParam.getType());
                    }
                    typeCheckingContext.controlStructureVariables.put(closureParam, inferredType);
                }
            }
        }
    }
{code}

> each parameter type not correctly inferenced
> --------------------------------------------
>
>                 Key: GROOVY-9058
>                 URL: https://issues.apache.org/jira/browse/GROOVY-9058
>             Project: Groovy
>          Issue Type: Bug
>          Components: Static compilation
>    Affects Versions: 2.5.6
>            Reporter: Mauro Molinari
>            Priority: Major
>
> Consider this Java class:
> {code:java}
> package test51;
> import java.util.List;
> public class Foo {
>     public List<Object[]> bar() { return null; }
> }{code}
>  and this Groovy class:
> {code:java}
> package test51
> import groovy.transform.CompileStatic
> @CompileStatic
> class Test51 {
>     protected void foo() {
>         List<Object[]> foo = new Foo().bar()
>         foo.each { row ->
>             def o = row[0]
>         }
>     }
>     
>     List bar() {
>     }
> }{code}
> This produces a compiler error because {{row}} is resolved as {{Object}} rather than
{{Object[]}}.
> A workaround is to declare {{row}} as {{Object[] row}} in the closure parameter list.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Mime
View raw message