AN created GROOVY-10774:
---------------------------

             Summary: Static compilation error using generic classes with maps
                 Key: GROOVY-10774
                 URL: https://issues.apache.org/jira/browse/GROOVY-10774
             Project: Groovy
          Issue Type: Bug
          Components: Compiler
    Affects Versions: 3.0.12, 3.0.11
         Environment: Windows using Java 1.8.0_211
            Reporter: AN


I was updating an old library to use Groovy 3 and I found a static compilation 
error.  The error starts occurring since Groovy 2.5.16, previously tested 
versions of 2.5.x compile successfully.

The error can be reproduced with:
{code:groovy}
import groovy.transform.CompileStatic

@CompileStatic
class MyGenericClass<K> {
    //protected LinkedHashMap<K, String> valuesMap = new LinkedHashMap<>()
    protected Map<String, K> keysMap = new LinkedHashMap<>()

    void put(K k, String v) {
        keysMap.put(v, k)
        //valuesMap.put(k, v)
    }
} {code}
The error only happens when the parametrized type is used as the value of the 
map.

 
{code}
2022-09-29T14:24:28.708-0400 [ERROR] [system.err] 
C:\WP\GroovyGenerics\src\main\groovy\test\MyGenericClass.groovy: 11: [Static 
type checking] - Cannot call java.util.Map <String, K>#put(java.lang.String, K) 
with arguments [java.lang.String, K] 
2022-09-29T14:24:28.708-0400 [ERROR] [system.err]  @ line 11, column 9.
2022-09-29T14:24:28.708-0400 [ERROR] [system.err]            keysMap.put(v, k)
2022-09-29T14:24:28.708-0400 [ERROR] [system.err]            ^
2022-09-29T14:24:28.708-0400 [ERROR] [system.err] 
2022-09-29T14:24:28.708-0400 [ERROR] [system.err] 1 error
2022-09-29T14:24:28.708-0400 [ERROR] [system.err]{code}



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

Reply via email to