You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Eric Milles (Jira)" <ji...@apache.org> on 2023/04/15 17:54:00 UTC

[jira] [Updated] (GROOVY-9952) STC does not correctly type check variable whose type is a type variable

     [ https://issues.apache.org/jira/browse/GROOVY-9952?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Eric Milles updated GROOVY-9952:
--------------------------------
    Priority: Minor  (was: Major)

> 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
>            Assignee: Eric Milles
>            Priority: Minor
>
> 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.20.10#820010)