Possible Bug in javac unchecked cast detection

Tom Schindl tom.schindl at bestsolution.at
Thu May 1 16:11:05 UTC 2014


Hi,

While cleaning up the javafx codebase (who has a lot of use of generics)
I came across casts the eclipse java compiler marks as unchecked while
javac -Xlint:all does not warn about.

The following snippet shows gist of the problem:

> package a;
> 
> public class CastTest {
> 	public interface BaseInterface<T> {
> 
> 	} 
> 	
> 	public interface SubInterfaceA<T> extends BaseInterface<T> {
> 
> 	}
> 	
> 	public interface SubInterfaceB<T> extends BaseInterface<T> {
> 
> 	}
> 	
> 	public static void main(String[] args) {
> 		SubInterfaceA<String> a = null;
> 		SubInterfaceB<String> b = (SubInterfaceB<String>) a; // javac does not warn
> 		b = (SubInterfaceB<String>)(BaseInterface<String>)a;
> 
> 	}
> }

I first thought of a bug in the eclipse compiler but they claim their
compiler is correct because the spec says:


> "A cast from a type S to a parameterized type (§4.5) T is unchecked
>    unless at least one of the following is true:
>    *  S <: T
>    *  All of the type arguments (§4.5.1) of T are unbounded wildcards
>    *  T <: S and S has no subtype X other than T where the type arguments
>       of X are not contained in the type arguments of T."

So before fileing a bug and start fixing the javafx code I wanted to get
feedback from the compiler gurus on this list who is right and who is wrong.

Thanks

Tom


More information about the compiler-dev mailing list