Possible Bug in javac unchecked cast detection

Alex Buckley alex.buckley at oracle.com
Thu May 1 18:41:24 UTC 2014


Eclipse is right. Given the interface declarations below, a cast from 
SubInterfaceA<String> to SubInterfaceB<String> is unchecked. By JLS 
5.5.2, an unchecked warning is due unless @SuppressWarnings is in 
effect; lint mode doesn't matter.

Alex

On 5/1/2014 9:11 AM, Tom Schindl wrote:
> 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