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

Regression in STC related to generics, unknown cause

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 3.0.12
    • 4.0.5, 2.5.19, 3.0.13
    • Static Type Checker
    • None
    • Linux, Groovy 3.0.12, Java 8

    Description

      In 3.0.12 the following code fails to pass STC:

      @groovy.transform.CompileStatic
      class Request {
          Enumeration<String> getHeaderNames() { 
             def r = new Vector()
             r.add("Header")
             r.elements()
          }
          String getHeader(String header) {
             "value"
          }
      }
      
      @groovy.transform.CompileStatic
      class Test {
         void test() {
            def request = new Request()
            Set<Map<String, String>> allHeaders = []
            allHeaders.addAll(request.getHeaderNames().toList().collectEntries { headerName -> [headerName, request.getHeader(headerName)] })
         }
      }
      
      new Test().test()
      

      Error:

      $ groovy fail.groovy
      org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
      /home/octy/Documents/groovy_playground/fail.groovy: 18: [Static type checking] - Cannot call <T> java.util.Set <Map>#addAll(T[]) with arguments [java.lang.Object]
       @ line 18, column 7.
               allHeaders.addAll(request.getHeaderNames().toList().collectEntries { headerName -> [headerName, request.getHeader(headerName)] })
               ^
      
      1 error
      

      This is not a total blocker, as the generics can be specified to `collectEntries`, or the expression extracted to a typed variable; and the error will go away. However, it does make existing code fail to compile, when it worked under 3.0.11.

      Attachments

        Activity

          People

            emilles Eric Milles
            otogami Octavia Togami
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: