Bug 86898 - [1.5][compiler] compiler should flag unchecked cast
Summary: [1.5][compiler] compiler should flag unchecked cast
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.1   Edit
Hardware: PC Windows XP
: P3 major (vote)
Target Milestone: 3.1 M6   Edit
Assignee: Philipe Mulet CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-03-01 06:14 EST by Nikolay Metchev CLA
Modified: 2005-03-31 09:47 EST (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Nikolay Metchev CLA 2005-03-01 06:14:25 EST
the following doesn't compile with javac and I don't think eclipse should
compile it either. Eclipse 3.1M5a however doesn't complain:
---------------------------------------------------
class B extends A<Object>
{
   void m2()
   {
      m3((X2) m());
   }
   
   void m3(X2 i)
   {
      
   }
}
public class A<T>
{
   X<? extends T> m()
   {
      return null;
   }
   
}

class X2 extends X<String>
{
   
}

class X<T>
{
   
}
------------------------------------------------------------
Comment 1 Nikolay Metchev CLA 2005-03-01 06:14:55 EST
P.S. javac says:
C:\eclipse\workspace\Scratch\src\A.java:5: inconvertible types
found   : X<capture of ? extends java.lang.Object>
required: X2
      m3((X2) m());
               ^
1 error
Comment 2 Philipe Mulet CLA 2005-03-01 16:18:55 EST
Reproduced. Indeed this feels like our bug.
Comment 3 Nikolay Metchev CLA 2005-03-02 04:38:36 EST
I remember reading that one of the recent nightly builds of eclipse was JCK 1.5
compliant. With this bug reproduced (I presume with the latest build) it would
seem silly to declare the eclipse compiler 1.5 compliant. Perhaps its worth
letting whomever is in charge of the JCK test (Sun I presume) of this test case.
Comment 4 Philipe Mulet CLA 2005-03-02 09:26:24 EST
1.5 compliance means you pass some serie of tests, it doesn't mean you have zero
bugs. Looks like the compliance test suites doesn't have the quality you expect.
Comment 5 Philipe Mulet CLA 2005-03-07 10:42:03 EST
Actually, I take my previous comment back. Based on JLS this should only be an
unsafe warning. I believe javac has a bug; we do the right thing. Glancing
through the javac bug database, they have quite a few defects in this area.

To prove my point, instead of casting to X2, it will allow casting to X<String>
only issuing a warning.
The rule is for a cast to be wrong, that expression type and cast type erasures
must be non compatible (only erasures at this point), and that there is some
intermediate type which is provably distinct, here that would be X<? extends
Object> and X<String> which aren't probably distinct. Thus the cast should be
unsafe at worst.

Added GenericTypeTest#test545
Comment 6 Philipe Mulet CLA 2005-03-07 10:45:02 EST
To prove my point, javac complains on:
     Object o3 = (X2) xo;
but doesn't on the following:
	Object o1 = (X<String>) xo;
	Object o5 = (X3<String>) xo;

Only unchecked warnings should be issued here.


====================
 import java.util.*;
 public class X<T> {
 public static void main(String[] args) {
	X<? extends Object> xo = null;
	X<String> xs = null;
	X2 x2 = null;
		
	Object o1 = (X<String>) xo;
	Object o2 = (X<? extends Object>) xs;
	Object o3 = (X2) xo;
	Object o4 = (X<? extends Object>) x2;
	Object o5 = (X3<String>) xo;
}
}
class X2 extends X<String> {
}
class X3<U> extends X<U> {
}
Comment 7 Nikolay Metchev CLA 2005-03-07 12:27:33 EST
I agree with what you have said, however instead of saying that this bug is
invalid can I just point out that as it stands eclipse does not even warn about
the cast as you suggest it should. Therefore this bug can be changed to say that
a warning should be produced with the example code.

P.S. can you point me to the relevant Sun Javac bug?
Comment 8 Philipe Mulet CLA 2005-03-07 12:41:25 EST
Interesting, I am not seeing the warning in IDE either, though I can see it
produced by compiler in test suite. 
Investigating.

As for javac bug, I am guessing, found no exact match in database. Feel free to
report it to them.
Comment 9 Philipe Mulet CLA 2005-03-07 12:46:59 EST
Understood, I wasn't testing latest jdt/core with change in cast check for
raising proper unchecked warning.
Given this defect conducted to fix up the unchecked diagnosis, will rather
consider it to be fixed.

Comment 10 Philipe Mulet CLA 2005-03-07 12:47:51 EST
Fixed unchecked warning diagnosis, updated title to reflect actual issues.
Comment 11 David Audel CLA 2005-03-31 09:47:06 EST
Verified in I20050330-0500