[
https://issues.apache.org/jira/browse/GROOVY-11470?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18149218#comment-18149218
]
Daniel Sun commented on GROOVY-11470:
-------------------------------------
Fixed on master; added a regression test in
748609a1ce8ca37b62d99e86ce842bd5e953a5df
(https://github.com/apache/groovy/commit/748609a1ce8ca37b62d99e86ce842bd5e953a5df).
> 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
> Assignee: Daniel Sun
> Priority: Minor
> Fix For: 7.0.0-alpha-1
>
>
> 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
(v10.3.26#10030026)