You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Thodoris Sotiropoulos (Jira)" <ji...@apache.org> on 2021/02/22 16:46:00 UTC
[jira] [Created] (GROOVY-9952) STC does not correctly type check
variable whose type is a type variable
Thodoris Sotiropoulos created GROOVY-9952:
---------------------------------------------
Summary: STC does not correctly type check variable whose type is a type variable
Key: GROOVY-9952
URL: https://issues.apache.org/jira/browse/GROOVY-9952
Project: Groovy
Issue Type: Bug
Components: Static Type Checker
Reporter: Thodoris Sotiropoulos
I have the following program
{code:java}
class Y {}
class X<T> {
T x;
public X() {
this.x = new Y()
}
T foo() { return this.x }
}
class Main {
public static void main(String[] args) {
bar(new X<Integer>());
}
public static Integer bar(X<Integer> y) {
return y.foo()
}
}
{code}
h3. Actual Behaviour
The program above complies, and at runtime I get the exception
{code:java}
Exception in thread "main" org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'Y@57175e74' with class 'Y' to class 'java.lang.Integer'
at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToNumber(DefaultTypeTransformation.java:175)
at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnNumber(DefaultTypeTransformation.java:282)
at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:243)
at org.codehaus.groovy.vmplugin.v8.IndyInterface.fromCache(IndyInterface.java:318)
at Main.bar(groovy1.groovy:19)
at Main.main(groovy1.groovy:15)
{code}
h3. Expected Behaviour
The program should not compile. The problem is at line 5 where you assign something of type Y to a variable of type T.
--
This message was sent by Atlassian Jira
(v8.3.4#803005)