Details
-
Bug
-
Status: Closed
-
Major
-
Resolution: Fixed
-
None
-
None
Description
I have the following program
class Foo {} class X<T> { T f; X(T f) { this.f = f; } T getF() { this.f; } } class A { void foo(int x, X<Foo>... y) { Foo foo = y[0].getF(); // ClastCastException at runtime } } public class Main { public static void main(String[] args) { A a = new A(); a.foo(1, new X<>(1)); } }
Actual Behaviour
groovyc compiles this program, but we get the following exception at runtime
Exception in thread "main" org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '1' with class 'java.lang.Integer' to class 'Foo' at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnSAM(DefaultTypeTransformation.java:415) at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnNumber(DefaultTypeTransformation.java:329) at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:243) at org.codehaus.groovy.vmplugin.v8.IndyInterface.fromCache(IndyInterface.java:318) at A.foo(test.groovy:16) at Main.main(test.groovy:23)
Expected Behaviour
groovyc should have rejected this program with an error similar to the following
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: test.groovy: 23: [Static type checking] - Cannot call A#foo(int, X<Foo>[]) with arguments [int, X<java.lang.Integer>] @ line 23, column 7. a.foo(1, new X<>(1)); ^1 error
Tested against master (https://github.com/apache/groovy/commit/2df019d3c93486e8a32d407d364913e7b65e1cba)