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 2021/02/26 23:33:00 UTC

[jira] [Commented] (GROOVY-9956) Cannot infer correct type argument when passing a subtype of the declared type argument

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

Eric Milles commented on GROOVY-9956:
-------------------------------------

I tried some variations in this method to incorporate {{lType}}, which is {{Foo<Bar>}} in this case.  Calling {{GenericsUtils.parameterizeType(lType, cceType)}} was definitely no good as it causes a number of test failures.
{code:java}
    protected void inferDiamondType(final ConstructorCallExpression cce, final ClassNode lType) {
        ClassNode cceType = cce.getType(), inferredType = lType;
        // check if constructor call expression makes use of the diamond operator
        if (cceType.getGenericsTypes() != null && cceType.getGenericsTypes().length == 0) {
            ArgumentListExpression argumentList = InvocationWriter.makeArgumentList(cce.getArguments());
            ConstructorNode constructor = cce.getNodeMetaData(DIRECT_METHOD_CALL_TARGET);
            if (!argumentList.getExpressions().isEmpty() && constructor != null) {
                ClassNode type = GenericsUtils.parameterizeType(cceType, cceType);
                type = inferReturnTypeGenerics(type, constructor, argumentList);
                if (type.isUsingGenerics()) {
                    inferredType = type;
                }
            }
            adjustGenerics(inferredType, cceType);
            storeType(cce, cceType);
        }
    }
{code}

> Cannot infer correct type argument when passing a subtype of the declared type argument
> ---------------------------------------------------------------------------------------
>
>                 Key: GROOVY-9956
>                 URL: https://issues.apache.org/jira/browse/GROOVY-9956
>             Project: Groovy
>          Issue Type: Bug
>          Components: Static compilation, Static Type Checker
>    Affects Versions: 3.0.7, 4.0.0-alpha-2
>            Reporter: Stefanos Chaliasos
>            Priority: Major
>
> I have the following Groovy program.
> {code:groovy}
> class Foo<T> {
>     T f;
>     Foo (T f) {
>       this.f = f;
>     }
> }
> interface Bar{}
> class Baz<T> implements Bar {}
> @groovy.transform.TypeChecked
> class Main  {
>   public static void main(String[] args) {
>     Foo<Bar> x1 = new Foo<Bar>(new Baz<Integer>()) // Compiles
>     Foo<Bar> x2 = new Foo<>(new Baz<Integer>()) // Does not compile
>   }
> }
> {code}
> h2. Actual Behavior
> The program does not compile, and I get the following error.
> {code:java}
> org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
> Main.groovy: 16: [Static type checking] - Incompatible generic argument types. Cannot assign Foo <Baz> to: Foo <Bar>
>  @ line 16, column 19.
>        Foo<Bar> x2 = new Foo<>(new Baz<Integer>())
>                      ^
> 1 error
> {code}
> h2. Expected Behavior
> Compile successfully.
> h2. Affected Version
> I have also tested it with the compiler from Master (commit: 666627b3be7718e3265fc42b473060bc73f42e2f).
> h2. Comment
> If I remove the type parameter from Baz, then it only fails when compiled with the compiler from the Master.
>  Specifically, the following test case compiles with 3.0.7 and 4.0.0-alpha-2 compilers but fails with the same message as the previous test case when using the compiler from the Master.
> {code:groovy}
> class Foo<T> {
>     T f;
>     Foo (T f) {
>       this.f = f;
>     }
> }
> interface Bar{}
> class Baz implements Bar {}
> @groovy.transform.TypeChecked
> class Main  {
>   public static void main(String[] args) {
>     Foo<Bar> x1 = new Foo<Bar>(new Baz()) // Compiles
>     Foo<Bar> x2 = new Foo<>(new Baz()) // Does not compile
>   }
> }
> {code}



--
This message was sent by Atlassian Jira
(v8.3.4#803005)