Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 205195

Summary: [1.5][compiler] Compiler Type Inference Inconsistency with Sun javac
Product: [Eclipse Project] JDT Reporter: Daniel Spiewak <djspiewak>
Component: CoreAssignee: Srikanth Sankaran <srikanth_sankaran>
Status: CLOSED WONTFIX QA Contact:
Severity: normal    
Priority: P3 CC: kent_johnson, Olivier_Thomann, srikanth_sankaran
Version: 3.4   
Target Milestone: ---   
Hardware: PC   
OS: Windows Vista   
Whiteboard: stalebug
Attachments:
Description Flags
CompilerBrokenExample Project none

Description Daniel Spiewak CLA 2007-10-02 11:09:05 EDT
In attempting to compile a specific use of generics, casting and method type parameters, Eclipse's internal compiler handles the code perfectly, but Sun's javac fails.  I'm not sure if this is a bug in javac or a problem with the Eclipse compiler, so I'm opening the bug here.

Unfortunately, the code to repeat this problem is non-trivial, and I haven't boiled it down to a few classes yet.

   SVN repo: https://activeobjects.dev.java.net/svn/activeobjects/trunk/ActiveObjects
   SVN version: 574  (broken)
   SVN version 575 (fixed)
   SVN username: guest

   net.java.ao.EntityManager:467  (in both versions)

I appologize again for the non-trivial nature of the repeatable case.  This may be distillable to something a bit easier, but I haven't had a chance to play with it yet.
Comment 1 Daniel Spiewak CLA 2007-10-02 11:09:40 EDT
My appologies, the line is net.java.ao.EntityManager:466  in the broken version.
Comment 2 Kent Johnson CLA 2007-10-02 11:58:57 EDT
Please try to reduce the case.

What is the error from javac?
Comment 3 Daniel Spiewak CLA 2007-10-02 14:43:57 EDT
I have managed to reduce the case.  I'm zipping up and attaching a full Eclipse JDT project which reproduces (basically 5 stub classes).  The error I get with javac is as follows:

C:	
Comment 4 Daniel Spiewak CLA 2007-10-02 14:45:23 EDT
Created attachment 79572 [details]
CompilerBrokenExample Project
Comment 5 Daniel Spiewak CLA 2007-10-02 14:46:58 EDT
Seems Mylyn screwed up that trace.  Trying again...

C:\Users\Daniel Spiewak\Development\workspace\CompilerBrokenCase\src>javac -target 1.5 *.java
Driver.java:13: inconvertible types
found   : java.lang.Class<capture#935 of ? extends TestInterface<capture#262 of ?>>
required: java.lang.Class<TestInterface<?>>
                target.get((Class<TestInterface<?>>) value.getType()).put(value);
                                                                  ^
1 error
Comment 6 Kent Johnson CLA 2007-10-02 15:01:48 EDT
We're reporting an unchecked cast warning, but javac is reporting an error.

Reduced the case a bit more:

class X {
	<T> C<T> get(Class<T> clazz) { return null; }
	void test(I<?> value) {
		get((Class<I<?>>) value.getType()).put(value); // javac ERROR
	}
}
abstract class C<T> {
	abstract void put(T value);
}
interface I<T> {
	Class<? extends I<T>> getType();
}

---------------------

X.java:4: inconvertible types
found   : java.lang.Class<capture of ? extends I<capture of ?>>
required: java.lang.Class<I<?>>
                get((Class<I<?>>) value.getType()).put(value);
Comment 7 Philipe Mulet CLA 2007-10-04 04:41:41 EDT
Even more reduced scenario:

public class X {
        void test(I<?> value) {
        	Class<I<?>> cl = (Class<I<?>>) value.getType();
        }
}
interface I<T> {
        Class<? extends I<T>> getType();
}

Basically, the problem resides in the definition of provable distinctness of types, which is underspecified today. There are discussions in progress to make the spec stricter. Javac is already stricter, but also has bugs in this area (sometimes too strict than it needs to be).

I believe this is a Javac bug, and even once we make the cast rules a bit stricter to eliminate obvious incompatible types, this example should still compile clean with a warning.

Also see related issues: bug 158870, bug 90433, bug 89940, bug 90437 ...

My current work in progress for making Eclipse compiler stricter is still accepting this scenario as valid (with unchecked cast warning).
Comment 8 Daniel Spiewak CLA 2007-10-04 10:28:34 EDT
It actually makes a lot of sense that this is a bug with javac.  Logically, the type inference is sound.  Also, there are similar syntaxes allowed by javac under different situations.

Should this be opened in a bug against the JDK?
Comment 9 Philipe Mulet CLA 2007-10-04 11:29:54 EDT
I believe so. Note that the javac bug database has a couple entries for cast issues... so you may be able to find existing entries.
Comment 10 Kent Johnson CLA 2009-04-02 12:04:19 EDT
Our cast rules still differ from javac 7

We report the same error for the 2nd & 3rd, but only a warning for the 1st

class X {
 void test(I<?> value) {
  // found : Class<capture#918 of ? extends I<capture#289 of ?>>
  // required: Class<I<?>>
  Object o = (Class<I<?>>) value.getType();

  // found : Class<capture#867 of ? extends I<capture#651 of ?>>
  // required: Class<I<?>>
  Class<I<?>> c = value.getType();
 }

 Class<I<?>> test2(I<?> value) {
  // found : Class<capture#114 of ? extends I<capture#556 of ?>>
  // required: Class<I<?>>
  return value.getType();
 }
}
interface I<T> {
	Class<? extends I<T>> getType();
}
Comment 11 Kent Johnson CLA 2009-04-02 12:41:36 EDT
Another cast related bug 170064
Comment 12 Eclipse Genie CLA 2019-11-23 13:52:20 EST
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.