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

CompileStatic cant infer type when using map as map key

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.4.6
    • None
    • Static Type Checker
    • None

    Description

      open groovy console, paste code below

      import groovy.transform.*
      
      @CompileStatic
      void foo(Map<Map<String, String>, List<Map<String, Object>>> map){
          for(Map<String, String> key : map.keySet()) {
              def x = map[key]
              def y = x[0][""]
          }
      }
      

      and you will see it fails to compile

      Result: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
      ideaGroovyConsole.groovy: 7: [Static type checking] - Cannot find matching method java.lang.Object#getAt(int). Please check if the declared type is right and if the method exists.
       @ line 7, column 17.
                 def y = x[0][""]
                         ^
      1 error
      	at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
      	at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1075)
      	at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:591)
      	at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:569)
      	at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:546)
      	at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
      	at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
      	at groovy.lang.GroovyShell.parseClass(GroovyShell.java:688)
      	at groovy.lang.GroovyShell.run(GroovyShell.java:517)
      	at groovy.lang.GroovyShell.run(GroovyShell.java:497)
      	at sun.reflect.GeneratedMethodAccessor3.invoke(Unknown Source)
      	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      	at java.lang.reflect.Method.invoke(Method.java:498)
      	at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSite.invoke(PogoMetaMethodSite.java:169)
      	at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.call(PogoMetaMethodSite.java:71)
      	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:133)
      	at console.run(console.txt:25)
      	at groovy.ui.GroovyMain.processReader(GroovyMain.java:631)
      	at groovy.ui.GroovyMain.processFiles(GroovyMain.java:539)
      	at groovy.ui.GroovyMain.run(GroovyMain.java:382)
      	at groovy.ui.GroovyMain.process(GroovyMain.java:370)
      	at groovy.ui.GroovyMain.processArgs(GroovyMain.java:129)
      	at groovy.ui.GroovyMain.main(GroovyMain.java:109)
      	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      	at java.lang.reflect.Method.invoke(Method.java:498)
      	at org.codehaus.groovy.tools.GroovyStarter.rootLoader(GroovyStarter.java:109)
      	at org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:131)
      	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      	at java.lang.reflect.Method.invoke(Method.java:498)
      	at com.intellij.rt.execution.CommandLineWrapper.main(CommandLineWrapper.java:65)
      

      and if you change map key to other type (e.g a List<>), it will compile just fine.

      Attachments

        Activity

          People

            emilles Eric Milles
            pootow Richard Zhang
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: