JDK-6717221 : Safe cast considered unchecked
  • Type: Enhancement
  • Component: specification
  • Sub-Component: language
  • Affected Version: OpenJDK6
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: linux
  • CPU: unknown
  • Submitted: 2008-06-20
  • Updated: 2010-04-06
  • Resolved: 2008-08-13
Related Reports
Duplicate :  
Relates :  
Description
Consider the following program:

import java.util.*;
class cast  {
   Iterable<? extends Number> x1 = null;
   Collection<? extends Number> x2 = (Collection<? extends Number>)x1; //javac generates a warning here
}

The JLS seems to mandate a warning (JLS 5.5) for a cast that can be considered 'statically-safe':

"A cast from a type S to a parameterized type (��4.5) T is unchecked unless at least one of the following conditions hold:
1) S <: T.
2) All of the type arguments (��4.5.1) of T are unbounded wildcards.
3) T <: S and S has no subtype XT , such that the erasures (��4.6) of X and T are the same. "

Here we have that:

1) S (Iterable<? extends Number>) is *not* a subtype of T (Collection<? extends Number>)
2) T's type argument (? extends Number) is *not* an unbounded wildcard
3) There exist an *infinite) set of subtypes of S of the kind J = Collection<? extends K>, with K <: Number, such that |J| == |T|

For the above reason it seems like the described cast should raise a warning. Is there any way to fix the JLS so to make this warning disappear?

Comments
EVALUATION The Description here has been merged into 6558557 to present a more complete picture there.
13-08-2008