unchecked casts and intersection types

Liam Miller-Cushon cushon at google.com
Wed Dec 21 20:34:56 UTC 2016

Thanks for the explanation!

Now, you want to cast B to something that is a subtype of both I and A. Of
> course you can cast from B to I (there could exist a type, such as C, that
> extends B and implements I). But that doesn't mean that the cast is ok -
> there's no way a B can be casted to an A - so, whatever type is going to be
> a subtype of both A and I, that type is not going to extend B, so javac is
> correct in statically rejecting the cast, I believe.
> The union is a bit of the same - albeit for different reason; if you have
> (I | A) you can have either a I or an A, so, to go to B you should make
> sure that there's a path from either of them to the type B.

The case where the target is an intersection or the source is a union seems
clear enough, and I was forgetting about symmetry. Otherwise a cast where
the source is an intersection and the target is any element of the
intersection would be sound, I think? (Similarly if it was possible for the
target to be a union.)

> In your example, however, I see that there's indeed a problem -

I filed https://bugs.openjdk.java.net/browse/JDK-8171867 to keep track of
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20161221/5cdb7a7b/attachment.html>

More information about the compiler-dev mailing list