[patterns] Unchecked cast warning is absent?

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Mon Oct 2 10:19:30 UTC 2017


Yep - this should warn. We're using a cast conversion check for testing 
the pattern type against the expression type - but I don't see the cast 
conversion being used in this particular case:

http://hg.openjdk.java.net/amber/amber/file/b8181137917c/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java#l3589

Should this code use castInfo?

Maurizio


On 02/10/17 10:45, Tagir Valeev wrote:
> Hello!
>
> Just tried to play with pattern matching implementation. Seems that
> the matching against a non-reifiable type is unsafe, which is
> expected. Should the warning be displayed in this case?
>
> E.g.:
>
> import java.util.*;
>
> public class ListTest {
>
>    static void matches(Object obj) {
>      if(obj __matches List<String> list) {
>        System.out.println(list.get(0));
>      }
>    }
>
>    public static void main(String[] args) {
>      matches(Collections.singletonList(1));
>    }
> }
>
> $ ~/j/bin/javac -Xlint:all ListTest.java
> (compilation successful, no warning)
> $ ~/j/bin/java ListTest
> Exception in thread "main" java.lang.ClassCastException:
> java.base/java.lang.Integer cannot be cast to
> java.base/java.lang.String
> at ListTest.matches(ListTest.java:7)
> at ListTest.main(ListTest.java:12)
>
> With best regards,
> Tagir Valeev.



More information about the amber-dev mailing list