You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Paul King (Jira)" <ji...@apache.org> on 2023/02/10 21:41:00 UTC

[jira] [Closed] (GROOVY-8828) Union types incorrectly resolved

     [ https://issues.apache.org/jira/browse/GROOVY-8828?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Paul King closed GROOVY-8828.
-----------------------------

> Union types incorrectly resolved
> --------------------------------
>
>                 Key: GROOVY-8828
>                 URL: https://issues.apache.org/jira/browse/GROOVY-8828
>             Project: Groovy
>          Issue Type: Bug
>          Components: Compiler
>    Affects Versions: 2.4.13, 2.5.3
>            Reporter: Octavia Togami
>            Assignee: Eric Milles
>            Priority: Major
>             Fix For: 4.0.5, 2.5.19, 3.0.13
>
>
> UsingĀ {{@CompileStatic}} breaks {{Map#put}} if the operand is a UnionType due to this change (found by using {{git bisect}}): [https://github.com/apache/groovy/commit/2e4413812fff5d2033e9eb163d734b539b079b6b]
> Error:
> {code:java}
> src/main/groovy/pkg/Main.groovy: 12: [Static type checking] - Cannot call java.util.LinkedHashMap <java.lang.String, pkg.Foo>#put(java.lang.String, pkg.Foo) with arguments [java.lang.String, <UnionTypepkg.Foo+pkg.Bar>]{code}
> Sample code:
> Bar.groovy:
> {code:groovy}
> package pkg
> interface Bar {String name()}
> {code}
> Foo.groovy
> {code:groovy}
> package pkg
> interface Foo {}
> {code}
> Main.groovy
> {code:groovy}
> package pkg
> import groovy.transform.CompileStatic
> @CompileStatic
> class Main {
>     static main(args) {
>         Map<String, Foo> map = [:]
>         map.values().each { Foo foo ->
>             if (foo instanceof Bar) {
>                 String name = (foo as Bar).name()
>                 map.put(name, foo)
>             }
>         }
>     }
> }
> {code}
> There is a workaround for this, which is to explicitly cast the variable to the Map's type. Changing line 12 to {{map.put(name, (Foo) foo)}} will allow the code to compile. However, this is clearly not the expected behavior.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)