JDK-6557182 : Unchecked warning *and* inconvertible types
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 5.0
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2007-05-14
  • Updated: 2012-01-13
  • Resolved: 2012-01-13
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7
7 b48Fixed
Related Reports
Relates :  
Relates :  
Relates :  
Relates :  
Description
The compiler generates both a warning and an error on this program:

class Outer<T extends Number & Comparable<String>> {
    void foo(T t) {
        Comparable<Integer> ci = (Comparable<Integer>) t;
    }
}

Outer.java:3: warning: [unchecked] unchecked cast
found   : T
required: java.lang.Comparable<java.lang.Integer>
        Comparable<Integer> ci = (Comparable<Integer>) t;
                                                       ^
Outer.java:3: inconvertible types
found   : T
required: java.lang.Comparable<java.lang.Integer>
        Comparable<Integer> ci = (Comparable<Integer>) t;
                                                       ^
1 error
1 warning

Comments
SUGGESTED FIX A webrev of this fix is available at the following URL: http://hg.openjdk.java.net/jdk7/tl/langtools/rev/4542977c959e
29-01-2009

EVALUATION Only the error message should be displayed. The warning is caused by the fact that javac is performing two separate cast conversion, one from Number to Comparable<Integer> and another from Comparable<String> to Comparable<Integer>. The former gives the warnings, while the latter gives the error. As pointed out by Gilad (see comment) the overall result of a cast conversion where the source type is a compound type T = A1 & A2 & .. An should be determined by the most restrictive behavior - e.g. if there exists a Ax so that Ax is not convertible into the target type S, then the whole cast should fail.
05-01-2009