You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Eric Milles (JIRA)" <ji...@apache.org> on 2019/04/02 16:52:01 UTC

[jira] [Comment Edited] (GROOVY-9058) each parameter type not correctly inferenced

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

Eric Milles edited comment on GROOVY-9058 at 4/2/19 4:51 PM:
-------------------------------------------------------------

Before the compiler gets to the above, {{StaticTypeCheckingVisitor.visitBinaryExpression}} runs and this appears to be where {{Type x = y}} is ignoring {{Type}} and saving {{typeof y}} as the inferred static type of {{x}}.

{code:java}
    public void visitBinaryExpression(BinaryExpression expression) {
        int op = expression.getOperation().getType();
        if (op == COMPARE_IDENTICAL || op == COMPARE_NOT_IDENTICAL) {
            return; // we'll report those as errors later
        }
        BinaryExpression enclosingBinaryExpression = typeCheckingContext.getEnclosingBinaryExpression();
        typeCheckingContext.pushEnclosingBinaryExpression(expression);
        try {
            final Expression leftExpression = expression.getLeftExpression();
            final Expression rightExpression = expression.getRightExpression();
            ...
            ClassNode lType = getType(leftExpression);
            ClassNode rType = getType(rightExpression);
            ...
            // In the case of "List<Object[]> foo = new Foo().bar()", lType is "List<Object[]>" and rType is "List".  resultType is "List" after the next line.  I think this is why "Map map = [a:1, b:2]" is resulting in STC bugs like "LinkedHashMap<String, Integer> is not a valid substitute for ...".
            ClassNode resultType = op==KEYWORD_IN
                    ?getResultType(rType,op,lType,reversedBinaryExpression)
                    :getResultType(lType, op, rType, expression);
            ...
            if (resultType == null) {
                resultType = lType;
            }
{code}


was (Author: emilles):
Before the compiler gets to the above, {{StaticTypeCheckingVisitor.visitBinaryExpression}} runs and this appears to be where {{Type x = y}} is ignoring {{Type}} and saving {{typeof y}} as the inferred static type.

{code:java}
    public void visitBinaryExpression(BinaryExpression expression) {
        int op = expression.getOperation().getType();
        if (op == COMPARE_IDENTICAL || op == COMPARE_NOT_IDENTICAL) {
            return; // we'll report those as errors later
        }
        BinaryExpression enclosingBinaryExpression = typeCheckingContext.getEnclosingBinaryExpression();
        typeCheckingContext.pushEnclosingBinaryExpression(expression);
        try {
            final Expression leftExpression = expression.getLeftExpression();
            final Expression rightExpression = expression.getRightExpression();
            ...
            ClassNode lType = getType(leftExpression);
            ClassNode rType = getType(rightExpression);
            ...
            // In the case of "List<Object[]> foo = new Foo().bar()", lType is "List<Object[]>" and rType is "List".  resultType is "List" after the next line.  I think this is why "Map map = [a:1, b:2]" is resulting in STC bugs like "LinkedHashMap<String, Integer> is not a valid substitute for ...".
            ClassNode resultType = op==KEYWORD_IN
                    ?getResultType(rType,op,lType,reversedBinaryExpression)
                    :getResultType(lType, op, rType, expression);
            ...
            if (resultType == null) {
                resultType = lType;
            }
{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)