unchecked casts and intersection types

Liam Miller-Cushon cushon at google.com
Mon Dec 19 23:07:43 UTC 2016

I was surprised by the unchecked warning in the following code. Is this
behaviour correct?

It stems from Types.isCastable using the same logic for union and
intersection types, and in both cases requiring that each element type be
castable to the target. For intersection types, isn't it sufficient for any
element type to be castable?

class Test {
  interface A<Y> {}
  interface B<Z> extends A<Z> {}
  interface C {}

  <T extends A<String> & C> B<String> f(T t) {
    B<String> result;

    A<String> a = t;
    result = (B<String>) a; // ok

    result = (B<String>) t; // unchecked
    // required: B<String>
    // found:    T
    // where T is a type-variable:
    //   T extends A<String>,C declared in method <T>f(T)

    return result;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20161219/aefd1a8f/attachment.html>

More information about the compiler-dev mailing list