Details
-
Bug
-
Status: Closed
-
Major
-
Resolution: Fixed
-
2.0.5
-
IDEA IC-123.23
Description
When running the follow code a runtime error occurs:
@CompileStatic class Scratch { @CompileStatic public static void main(String[] args) { def Map<String, Set<String>> map = new HashMap<>() def set = new HashSet<String>() set.add("foo") map.put("bar", set) map.each { println(it.getKey()) // Groovyc: [Static type checking] - Cannot find matching method java.lang.Object#getKey(). def List<String> list = it.getValue() // Groovyc: [Static type checking] - Cannot find matching method java.lang.Object#getKey(). } // compiles fine and created runtime error // Cannot cast object '[foo]' with class 'java.util.HashSet' to class 'java.util.List' due to: groovy.lang.GroovyRuntimeException: Could not find matching constructor for: java.util.List(java.lang.String) map.each { Map.Entry<String, List<String>> entry -> println(entry.getKey()) def List<String> list = entry.getValue() } } }
Reason is that the type parameters for Map<String, Set<String>> and Map.Entry<String, List<String>> don't match, but the compiler doesn't reject it, although @CompileStatic is defined
Attachments
Issue Links
- depends upon
-
GROOVY-5924 Provide a mechanism to allow the type checker to know what are the expected argument types of a closure
- Closed