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 2022/05/13 14:45:00 UTC

[jira] [Created] (GROOVY-10625) Issue in subtyping leads to unsound results

Thodoris Sotiropoulos created GROOVY-10625:
----------------------------------------------

             Summary: Issue in subtyping leads to unsound results
                 Key: GROOVY-10625
                 URL: https://issues.apache.org/jira/browse/GROOVY-10625
             Project: Groovy
          Issue Type: Bug
          Components: Static Type Checker
            Reporter: Thodoris Sotiropoulos


I have the following program

{code}
class A<T> {
  T x;
  A(T x) {this.x = x;}

  A() {}
}

class B<T extends Object> extends A<T> {

  B(T x) {
    super(x);
  }

  B() {}

  T m() {
    A<T> x = new B<Object>("fd"); // Misses type error: B<Object> is not a subtype of A<T>
    return x.x;
  }
}


class Test {
  public static void main(String[] args) {
    Integer x = new B<Integer>().m();
  }
}
{code}

h3. Actual behavior

The above program contains a type error, but the compilers misses it. As a result, a ClassCastException happens at runtime:

{code}
Exception in thread "main" org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'fd' with class 'java.lang.String' to class 'java.lang.Integer'
        at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToNumber(DefaultTypeTransformation.java:180)
        at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnNumber(DefaultTypeTransformation.java:295)
        at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:252)
        at org.codehaus.groovy.vmplugin.v8.IndyInterface.fromCache(IndyInterface.java:318)
        at Test.main(test.groovy:25)
{code}

h3. Expected behaviour

The compiler should not have type checked the assignment at line 17 and have produced an error message of the form: B<Object> cannot be converted into A<T>.

Tested against master (commit: ee12bb52381e8f0583c61fc25d43de1f55b80a87)



--
This message was sent by Atlassian Jira
(v8.20.7#820007)