Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-10774

Static compilation error using generic classes with maps

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 3.0.11, 3.0.12
    • 3.0.13
    • Compiler
    • None
    • Windows using Java 1.8.0_211

    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]

      Attachments

        Activity

          People

            emilles Eric Milles
            Daerian AN
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: