JDK-6893625 : Generics case does not compile
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 6u16,6u65,7u51,8
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2009-10-21
  • Updated: 2024-04-12
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
tbdUnresolved
Related Reports
Duplicate :  
Relates :  
Relates :  
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.6.0_16"
Java(TM) SE Runtime Environment (build 1.6.0_16-b01)
Java HotSpot(TM) Client VM (build 14.2-b01, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]

A DESCRIPTION OF THE PROBLEM :
Compiler fails to infer the correct type of a wildcard capture.  Given a class A<T extends A<T>> with a method T get(), the compiler should be able to infer that the return type when calling get() on an instance of A<?> is <T extends A<T>>.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
javac Test.java

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Successfully compiles.
ACTUAL -
Compile fails with error:

Test.java:10: <T>f1(T) in Test cannot be applied to (Test.A<capture#282 of ?>)
    f1(obj.get()); // Fails to compile

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
public class Test {
  class A<T extends A<T>> {
    T get() {return null;}
  }
  
  <T extends A<T>> void f1(T t) {}
  <T extends A<?>> void f2(T t) {}
  
  void main(A<?> obj) {
    f1(obj.get()); // Fails to compile
    f2(obj.get()); // Works
  }
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Use method f2 instead of f1.

Comments
Very similar to JDK-7190296, but the generic method accepts a 'T' rather than a 'List<T>'.
11-12-2015

The bug appears similar to JDK-8039214, but that (in-progress) patch doesn't fix it.
31-07-2014

I don't see any specification issues here. It should be straightforward to infer t=CAP, where CAP <: A<CAP>. Reopening and recategorizing as a javac bug.
31-07-2014

EVALUATION This is related to 6391995 - a fix changed javac inference algorithm in a way that is not compatible with the JLS, in order to accept the following program: class T6391995 { <E> void iterate(Iterable<E> iterable) { Iterable<? extends Iterable<? extends Object>> x = null; iterate(x.iterator().next()); } } I believe that the fix is incorrect. Reassigning to spec for further evaluation.
28-07-2010