Community
Participate
Working Groups
I20050808-2000 Collection<? extends Number> asList= Arrays.asList(1, 2.2); List<Number> nums= (List<Number>) asList; // correct warning List<Number> numz= (LinkedList<Number>) asList; // type safety warning missing javac 1.5.0_04 C:\e\w\development\zz1.5\src\xy\Try.java:13: warning: [unchecked] unchecked cast found : java.util.Collection<capture of ? extends java.lang.Number> required: java.util.List<java.lang.Number> List<Number> nums= (List<Number>) asList; // correct warning: unsafe cast ^ C:\e\w\development\zz1.5\src\xy\Try.java:14: warning: [unchecked] unchecked cast found : java.util.Collection<capture of ? extends java.lang.Number> required: java.util.LinkedList<java.lang.Number> List<Number> numz= (LinkedList<Number>) asList; // type safety warning missing ^ 2 warnings
This is a known bug in javac: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6211853
Added GenericTypeTest#test848
This is *not* the same problem as javac's 6211853 or the "duplicate" 4916620. http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6211853 is about casting to (String), which is different from casting to (LinkedList<Number>). http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4916620 is about casting to a generic subtype, where javac raised an error where it should raise a type safety warning. Eclipse currently generates the right type safety warning for that example, but still fails to give a warning for the second cast in comment 0.
Nope. The bug I mentionned is about casting to StringList which is a downcast.
Hm. The example in 6211853 only works iff you know that String is final. In general that analysis is wrong and the unchecked warning must be there. I'm not aware of a special case in JLS3 regarding captures and final classes, so I think the example given there should compile with a warning. Here's a testcase (similar to the one in 6211853) that shows that not giving unchecked warnings (and in the last case even an error) can leads to CCEs. class SerializableList extends LinkedList<Serializable> { } public class Test { @SuppressWarnings({"nls", "unused"}) public static void main(String[] args) { LinkedList<String> linkedList= new LinkedList<String>(); linkedList.add("Hello"); java.util.List<? extends Serializable> a = linkedList; java.util.List<String> b = (LinkedList<String>) a; // unchecked java.util.List<Integer> c = (LinkedList<Integer>) a; // unchecked java.util.List<Runtime> d = (LinkedList<Runtime>) a; // inconvertible! c.get(0).intValue(); // fails at run time d.get(0).gc(); // fails at run time } } Eclipse I20051102-1600 compiles this without any warnings/errors. Javac 1.5.0_05 gives the correct 2 warnings and 1 error.
reopening for investigation
Indeed we are missing unchecked cast warnings. Tuned semantics to match these. As for the type mismatch error, this is another issue not yet covered by the spec, where the cast should be allowed since types are not provably distinct (since no bound check is to be considered at this stage). Added GenericTypeTest#test1083 for the scenario in comment 5; which should issue 3 unchecked cast warnings (until the type mismatch debate is addressed, and covered in other bug report).
This problem comes missing support for unsafe cast for situations of the form: (CLASS) INTERFACE
Fix is combined with fix for bug 165143 (see patch attached there). Released for 3.3M4 Fixed
Verified for 3.3M4 with I20061211-1119
Re: comment 7 > (until the type mismatch debate is addressed, > and covered in other bug report) Even under stricter rules for provable distinctness (considering the bounds of involved types), I don't see how List<capture-of ? extends Serializable> could be provably distinct from LinkedList<Runtime> There may exist a subclass of Runtime (not final) implementing Serializable, hence all would be fine (only an unchecked warning to be issue)