Bug 106451 - [1.5][compiler] Error and unchecked warnings missing for cast to parameterized type
Summary: [1.5][compiler] Error and unchecked warnings missing for cast to parameterize...
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.2   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.3 M4   Edit
Assignee: Philipe Mulet CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-08-09 06:09 EDT by Markus Keller CLA
Modified: 2007-09-27 10:15 EDT (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Markus Keller CLA 2005-08-09 06:09:41 EDT
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
Comment 1 Philipe Mulet CLA 2005-10-17 07:32:47 EDT
This is a known bug in javac:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6211853
Comment 2 Philipe Mulet CLA 2005-10-17 07:33:24 EDT
Added GenericTypeTest#test848
Comment 3 Markus Keller CLA 2005-11-04 10:09:18 EST
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.
Comment 4 Philipe Mulet CLA 2005-11-04 10:16:47 EST
Nope. The bug I mentionned is about casting to StringList which is a downcast.
Comment 5 Markus Keller CLA 2005-11-04 11:07:25 EST
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.
Comment 6 Philipe Mulet CLA 2005-11-07 04:55:07 EST
reopening for investigation
Comment 7 Philipe Mulet CLA 2006-11-22 07:40:12 EST
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).
Comment 8 Philipe Mulet CLA 2006-11-22 07:41:48 EST
This problem comes missing support for unsafe cast for situations of the form:
(CLASS) INTERFACE
Comment 9 Philipe Mulet CLA 2006-11-22 09:26:13 EST
Fix is combined with fix for bug 165143 (see patch attached there).
Released for 3.3M4
Fixed
Comment 10 Olivier Thomann CLA 2006-12-11 15:44:32 EST
Verified for 3.3M4 with I20061211-1119
Comment 11 Philipe Mulet CLA 2007-09-27 10:15:17 EDT
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)