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 2022/05/16 19:34:00 UTC

[jira] [Updated] (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:all-tabpanel ]

Eric Milles updated GROOVY-9956:
--------------------------------
    Fix Version/s: 3.0.11

> 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
>            Assignee: Eric Milles
>            Priority: Major
>             Fix For: 4.0.0-alpha-3, 3.0.11
>
>
> 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.20.7#820007)