Bug 158870 - [1.5][compiler] javac inconvertible types on cast allowed by Eclipse
Summary: [1.5][compiler] javac inconvertible types on cast allowed by Eclipse
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.2   Edit
Hardware: PC Linux
: P3 normal (vote)
Target Milestone: 3.4 M3   Edit
Assignee: Philipe Mulet CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 224855 (view as bug list)
Depends on:
Blocks:
 
Reported: 2006-09-26 17:04 EDT by Zorzella Mising name CLA
Modified: 2008-04-14 06:55 EDT (History)
2 users (show)

See Also:


Attachments
Proposed patch (51.42 KB, patch)
2007-10-09 10:49 EDT, Philipe Mulet CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Zorzella Mising name CLA 2006-09-26 17:04:40 EDT
The following construct will only give forth a simple warning in Eclipse, but javac won't compile it:

    Iterator<Enum<?>> it = null;
    Iterator<Enum> error = (Iterator<Enum>)it;

javac output:

**********
inconvertible types
found   : java.util.Iterator<java.lang.Enum<?>>
required: java.util.Iterator<java.lang.Enum>
    Iterator<Enum> error = (Iterator<Enum>)it;
                                           ^
1 error

*********
Comment 1 J. Matt Casson CLA 2006-11-17 16:52:46 EST
This issue appears to correspond to Java bug 4916620 which is resolved in mustang(b55).

I believe this means that eclipse is performing correctly?
Comment 2 Maxime Daniel CLA 2006-11-22 05:45:04 EST
Error reproduced with jdk1.5.0_09 and jdk6_b104, while Eclipse I20061102-1715 shows a warning only.
Comment 3 Maxime Daniel CLA 2006-11-22 09:44:11 EST
Released GenericTypeTest#1084 (inactive).
Comment 4 Zorzella Mising name CLA 2006-11-23 14:04:06 EST
another weird case. Is this the same issue?

enum SomeEnum {};

*****

Enum<?> myvar = null;

if (myvar instanceof SomeEnum) {
}

This compiles with no warnings in Eclipse, but javac barfs at it...
Comment 5 Philipe Mulet CLA 2007-09-26 12:09:50 EDT
Also added GenericTypeTest#test1185-1186
Comment 6 Philipe Mulet CLA 2007-10-09 10:49:40 EDT
Created attachment 79958 [details]
Proposed patch
Comment 7 Philipe Mulet CLA 2007-10-09 10:51:05 EDT
Also added GenericTypeTest#test1187-1190.
Comment 8 Philipe Mulet CLA 2007-10-09 16:34:58 EDT
Released for 3.4M3.
Fixed
Comment 9 Philipe Mulet CLA 2007-10-09 16:52:43 EDT
Re: comment 4
I believe this is a bug in javac. Our behavior hasn't changed for it.
The two types are not provably distinct even under tighter rules; suspect javac is not handling the capture correctly.

Added GenericTypeTest#test1191
Comment 10 Maxime Daniel CLA 2007-10-29 08:47:23 EDT
Verified for 3.4 M3 using build I20071029-0010.
Comment 11 Philipe Mulet CLA 2008-04-10 07:26:47 EDT
*** Bug 224855 has been marked as a duplicate of this bug. ***
Comment 12 Neale Upstone CLA 2008-04-10 10:37:52 EDT
Is this correct behaviour? or, as someone suggested, is this a bug in Java?

Running javac (from JDK6uN), I still get the inconvertible types error.

Sun, do however suggest that they have fixed various bugs (http://bugs.sun.com/view_bug.do?bug_id=4916620), but not this case ,it seems.  Elsewhere (http://bugs.sun.com/view_bug.do?bug_id=6256789) they suggest chain casting, which works in Eclipse (apart from some Save Action issues) and JDK:

i.e.

While:
    Iterator<Enum<?>> it = null;
    Iterator<Enum> error = (Iterator<Enum>)it;
still throws an error,

    Iterator<Enum> error = (Iterator<Enum>)(Iterator)it;
and
    Iterator<Enum> error = (Iterator<Enum>)(Object)it;

both compile, with warnings (if -Xlint:unchecked is specified).

This, on both Sun's and Eclipse's part is a bit mad.  If we can chain cast to get this effect, then we should be able to do so without chain casting.

What is clear to me is that when I cast, I am saying "I know what type this is".

Generics are awkward enough, without the compiler starting to think it is smarter than the programmer.  In this case, I cannot see how the compiler is saving me from a ClassCastException. (Perhaps someone else can...)


Also of note: Eclipse Save Actions->Remove Unnecessary Casts does some interesting stuff:

For the first workaround, it gives you 
    Iterator<Enum> error = (Iterator)it;
which works with warnings, and for the second, it gives:
    Iterator<Enum> error = (Iterator<Enum>)it;
which is our original error, and therefore fails!

Reopen (both Eclipse and JDK) ?
Comment 13 Philipe Mulet CLA 2008-04-10 17:04:04 EDT
Please do not reopen this bug, if you have issues open a new bug.
Most of your argument comes from the fact you disagree with the Java language spec which expects the current behavior, enforced by both Eclipse and javac (i.e. blame the spec).

As to quickfix issues, pls open a separate bug.
Comment 14 Neale Upstone CLA 2008-04-10 17:52:59 EDT
Not planning to re-open, but I am curious.  
Does the Eclipse compiler implement:
  the JLS
or
  compatibility with JDK's javac ?

The reason I ask is that Sun's bug database is littered with things like "Yes, the cast implementation in javac is wrong." (see http://bugs.sun.com/view_bug.do?bug_id=6270087).

If the aim of the Eclipse compiler is to be a solid, open-source Java compiler, then I see no reason whatsoever why it should not implement correctly behaving generics, rather than re-implement bugs (whether they are in javac or JLS).

I'd agree that there is a need for Java code portability, and that people will want code compiled in Eclipse to compile with javac, but we should at least be able to have the option of a warning instead of an error for this class of bug.

Philippe: Your own comments in http://bugs.sun.com/view_bug.do?bug_id=6557279 make this exact case.  Surely Eclipse deserves better than Sun's sluggishness in addressing this.  Would you and those working on the compiler consider giving us an option, where the issue is a bug that Sun has agreed it is a bug, and the bug is still open?
Comment 15 Philipe Mulet CLA 2008-04-14 06:55:30 EDT
The cast semantics are really fuzzily spec'ed today. The spec is being clarified, and cast semantics are to be made stricter. According to the spec, only unchecked cast are diagnosed for situations where it can be proven wrong statically. Until the clarified version of the spec is out there (and we will adjust to it then), it is hard to guess which way it will end up.

I have seen situations where an in-progress javac bug got resolved differently in the end (i.e. not a bug, just fix spec).

The comment I posted in http://bugs.sun.com/view_bug.do?bug_id=6557279 is dating from before I tightened the cast semantics in Eclipse compiler to become stricter and detect early invalid cast situations (which by the spec were unchecked). I altered the behavior for raw types, but these are arguable I agree. Until the new spec clarifications materialize, I would rather keep the current behavior.