JDK-6192945 : Declaration order of interdependent generic types should not matter
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 5.0
  • Priority: P5
  • Status: Closed
  • Resolution: Fixed
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2004-11-09
  • Updated: 2017-05-16
  • Resolved: 2011-03-08
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.
JDK 7 Other
7 b05Fixed OpenJDK6Resolved
Related Reports
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.5.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-b64)
Java HotSpot(TM) Client VM (build 1.5.0-b64, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows 2000 [Version 5.00.2195]

A DESCRIPTION OF THE PROBLEM :
Discussed in more detail in the generics forum (http://forum.java.sun.com/thread.jsp?forum=316&thread=566266), this bug relates to the unnecessary restriction of the declaration order of generic types in a class or method. Basically the following class declaration

    public class MyClass<E, D super E>

causes the following compiler errors

    Error:  line (1) > expected
    Error:  line (4) '{' expected

Rewritten in another form, the following declaration

    public class MyClass<E extends D, D>

causes the following compiler error

    Error:  line (1) illegal forward reference

Section 6.3 of the Language Specification says that "type parameters can appear as parts of their own bounds, or as bounds of other type parameters declared in the same section."  The current behaviour appears to contradict this.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Attempt to compile either of the two following classes:

public class MyClass<E, D super E> {}

public class MyClass<E extends D, D> {}


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Successful compilation for either form of the class declaration.
ACTUAL -
Compilation failures for both forms of the class declaration.

ERROR MESSAGES/STACK TRACES THAT OCCUR :
Compilation of

public class MyClass<E, D super E> {}

yields the following errors

    Error:  line (1) > expected
    Error:  line (4) '{' expected


Compilation of

public class MyClass<E extends D, D> {}

yields the following error

Error:  line (1) illegal forward reference


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
Either

public class MyClass<E, D super E> {}

or

public class MyClass<E extends D, D> {}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Current workaround involves swapping the declaration order of the generic types. This can be undesirable, particularly in my case, as the class needs to extend an interface, thus

public class MyClass<D super E, E> implements MyInterface<E, D>

so we have the awkward situation of the the generic types being reversed for the interface and the corresponding class, thus

MyInterface<Integer, Number> obj = new MyClass<Number, Integer>();


Note that there are situations for which there appears to be no such workaround, for example

public class SomeClass<S extends T, T extends SomeInterface<S>> {}
###@###.### 2004-11-09 20:10:37 GMT

Comments
SUGGESTED FIX Webrev of changes: http://sa.sfbay/projects/langtools/bugid_summary.pl?bugid=6192945 See also attachment 6192945.tar.gz.
25-11-2006

EVALUATION This is two seperate issues: 1. Allowing lower bounds on type variables. 2. Making the declaration order of type variables independent. ad 1. see RFE 5052956. ad 2. this is a compiler bug and here is a simple example of what should be allowed: class Test<D extends E, E> {} It is not yet clear if there are any restrictions on the use type variables (are cycles OK?) I can't find any restrictions in JLS. ###@###.### 2004-11-10 01:07:58 GMT The only (unspecified) restriction is that we cannot have a cycle in the subtype relation. ###@###.### 2004-11-11 03:53:24 GMT
10-11-2004