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

Eric Milles reassigned GROOVY-10251:
------------------------------------

    Assignee: Eric Milles

> STC infers a type that does not respect the bound of type variable
> ------------------------------------------------------------------
>
>                 Key: GROOVY-10251
>                 URL: https://issues.apache.org/jira/browse/GROOVY-10251
>             Project: Groovy
>          Issue Type: Bug
>          Components: Static Type Checker
>            Reporter: Thodoris Sotiropoulos
>            Assignee: Eric Milles
>            Priority: Major
>
> I have the following program
> {code:java}
> class A<T1, T2> {
>   T2 x;
>   A(T2 x) {
>     this.x = x;
>   } 
> }
> class B {
>   <T1 extends Number, T2 extends A<Float, T1>> void foo(T2 x1, T1 x2) {
>     T1 y = x1.x; // CCE at runtime
>   }
> }
> class C<T extends D> {
>   T x;
>   C(T x) {
>     this.x = x;
>   }
>   void test() {
>     (new B()).foo(x, foo());
>   }
>   static <X extends Number> X foo() {
>     return null;
>   }
> }
> class D extends A<Float, String> {
>   D() {
>     super("");
>   }
>   
> }
> class Main {
>   public static void main(String[] args) {
>     (new C<D>(new D())).test();
>   }
> }
> {code}
> h3. Actual behaviour
> The compiler compiles the program, but we receive a ClassCastException at 
> runtime
> {code:java}
> Exception in thread "main" 
> org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast 
> object '' with class 'java.lang.String' to class 'java.lang.Number'
>         at 
> org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToNumber(DefaultTypeTransformation.java:179)
>         at 
> org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnNumber(DefaultTypeTransformation.java:294)
>         at 
> org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:251)
>         at 
> org.codehaus.groovy.vmplugin.v8.IndyInterface.fromCache(IndyInterface.java:318)
>         at B.foo(groovy16.groovy:10)
>         at C.test(groovy16.groovy:21)
>         at Main.main(groovy16.groovy:38)
> {code}
> h3. Expected behaviour
> The compiler should reject the above program.
> Specifically, the call `(new B()).foo(x, foo());` at line 21 is not 
> type-correct. This is because the function at line 9 is instantiated with `T1 
> = Number` and `T2 = A<Float, Number>`. However, during the call at line 21 we 
> pass something whose type is `D`, which is a subtype of `A<Float, String`> 
> and not of `A<Float, Number>`.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to