[ 
https://issues.apache.org/jira/browse/GROOVY-11470?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17879639#comment-17879639
 ] 

Jochen Theodorou commented on GROOVY-11470:
-------------------------------------------

yes, that should be a problem of intersection. the type in the if and outside 
need to be interjected after the if-block, so I guess something like 
Comparable<? super Serializable> should have been the type for x at the point 
of assignent to p, which is obviously not compatible

> 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
>            Priority: Minor
>
> 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