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/11/16 09:50:00 UTC

[jira] [Created] (GROOVY-10370) STC does not catch type error when assigning a type parameter to a variable of another type

Thodoris Sotiropoulos created GROOVY-10370:
----------------------------------------------

             Summary: STC does not catch type error when assigning a type parameter to a variable of another type
                 Key: GROOVY-10370
                 URL: https://issues.apache.org/jira/browse/GROOVY-10370
             Project: Groovy
          Issue Type: Bug
          Components: Static Type Checker
            Reporter: Thodoris Sotiropoulos


This issue may be related to GROOVY-9952.

I have the following program

{code:java}
class A {}

class Test {
  public static void main(String[] args) {
    A x = m(1)
    System.out.println(x)
  }

  static <T> A m(T x) {
    return x;
  }
}
{code}

h3. Actual behaviour

groovyc accepts and compiles this wrongly-typed program, something that results in a runtime exception.

{code}
Exception in thread "main" org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '1' with class 'java.lang.Integer' to class 'A'
        at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnSAM(DefaultTypeTransformation.java:420)
        at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnNumber(DefaultTypeTransformation.java:331)
        at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:251)
        at org.codehaus.groovy.vmplugin.v8.IndyInterface.fromCache(IndyInterface.java:318)
        at Test.m(test.groovy:9)
        at Test.main(test.groovy:4)
{code}

h3. Expected behaviour

groovyc should have rejected the above program.

Tested against master.



--
This message was sent by Atlassian Jira
(v8.20.1#820001)