Details
Description
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:
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) } }
The error only happens when the parametrized type is used as the value of the map.
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]