Thodoris Sotiropoulos created GROOVY-11470:
----------------------------------------------

             Summary: type unsafety when using flow typing and wildcard
                 Key: GROOVY-11470
                 URL: https://issues.apache.org/jira/browse/GROOVY-11470
             Project: Groovy
          Issue Type: Bug
          Components: Static Type Checker
            Reporter: Thodoris Sotiropoulos


I have the following program


{code:java}
class Test {
  public static Comparable<Integer> m() {
    return 1;
  }
  public static void main(String[] args) {
    def x = m()
    if (true) {
      x = ""
    }
    Comparable<? super Integer> p = x
    p.compareTo(1)
  }
} {code}
h3. Actual behavior

The code compiles, but I receive a ClassCastException at runtime
{code:java}
Exception in thread "main" java.lang.ClassCastException: class 
java.lang.Integer cannot be cast to class java.lang.String (java.lang.Integer 
and java.lang.String are in module java.base of loader 'bootstrap')
        at java.base/java.lang.String.compareTo(String.java:144)
        at Test.main(test.groovy:16)
 {code}
h3. Expected behavior

The code should have been rejected, as the type of variable x is not 
Comparable<? super Integer>.

I suppose that the problem lies in the implementation of intersection types, 
and not in the implementation of flow-sensitive analysis.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to