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

Octavia Togami updated GROOVY-8828:
-----------------------------------
    Description: 
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.

  was:
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: 
[https://gist.github.com/kenzierocks/8fd31525c0229caeb6c85622f5d230b5]

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.


> 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
>            Priority: Major
>
> 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)

Reply via email to