JDK-4899305 : restrictions on bound of a type variable
  • Type: Bug
  • Component: specification
  • Sub-Component: language
  • Affected Version: 5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Won't Fix
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2003-07-31
  • Updated: 2007-09-25
  • Resolved: 2007-09-25
Related Reports
Duplicate :  
Description

Name: rmT116609			Date: 07/31/2003


FULL PRODUCT VERSION :
java version "1.4.2"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2-b28)
Java HotSpot(TM) Client VM (build 1.4.2-b28, mixed mode)

FULL OS VERSION :
Windows XP

EXTRA RELEVANT SYSTEM CONFIGURATION :
generic prototype compiler 2.2-ea

A DESCRIPTION OF THE PROBLEM :
The generics prototype compiler version 2.2-ea places undue restrictions on the bounds of a type variable.  From the error message I conclude that at most one type variable can appear in the bounds list of another type variable.

I do not see that this behavior is what the specification specifies. The specification does not impose any such restriction, neither in the syntax productions nor any place in the specification of the semantics of bounds.



package generics;

public class TypeVariablesInBounds {


	  public static <S, T extends Comparable<S>, R extends S & T>
	  R max(T arg1, S arg2) {
	  	return (R)((arg1.compareTo(arg2)>0)?arg1:arg2);
	  }
	  
	  public static <T extends Comparable<S>, S, R extends S & Comparable<S>>
	  R max(T arg1, S arg2) {
	  	return (R)((arg1.compareTo(arg2)>0)?arg1:arg2);
	  }
	  
	  public static <T extends Comparable<S>, S, R extends Comparable<S>>
	  R max(T arg1, S arg2) {
	  	return (R)((arg1.compareTo(arg2)>0)?arg1:arg2);
	  }

	public static void main(String[] args) {
	}
}

error messages:

TypeVariablesInBounds.java:6: a type variable may not be followed by other bounds
	  public static <S, T extends Comparable<S>, R extends S & T>
                                                                   ^
TypeVariablesInBounds.java:11: a type variable may not be followed by other bounds
	  public static <T extends Comparable<S>, S, R extends S & Comparable<S>>

                                                                             ^


In the first case the restriction would make sense if T resolves to a class type, but it need not be rejected upfront.

In the second case I do not see any reason for the restriction.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
compile source code

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
error free compilation
ACTUAL -
compile-time error messages

ERROR MESSAGES/STACK TRACES THAT OCCUR :
TypeVariablesInBounds.java:6: a type variable may not be followed by other bounds
	  public static <S, T extends Comparable<S>, R extends S & T>
                                                                   ^
TypeVariablesInBounds.java:11: a type variable may not be followed by other bounds
	  public static <T extends Comparable<S>, S, R extends S & Comparable<S>>
                                                                             ^
TypeVariablesInBounds.java:8: inconvertible types
found   : java.lang.Object
required: R
	  	return (R)((arg1.compareTo(arg2)>0)?arg1:arg2);
                          ^
TypeVariablesInBounds.java:13: inconvertible types
found   : java.lang.Object
required: R
	  	return (R)((arg1.compareTo(arg2)>0)?arg1:arg2);
                          ^
4 errors


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
package generics;

public class TypeVariablesInBounds {


	  public static <S, T extends Comparable<S>, R extends S & T>
	  R max(T arg1, S arg2) {
	  	return (R)((arg1.compareTo(arg2)>0)?arg1:arg2);
	  }
	  
	  public static <T extends Comparable<S>, S, R extends S & Comparable<S>>
	  R max(T arg1, S arg2) {
	  	return (R)((arg1.compareTo(arg2)>0)?arg1:arg2);
	  }
	  
	  public static <T extends Comparable<S>, S, R extends Comparable<S>>
	  R max(T arg1, S arg2) {
	  	return (R)((arg1.compareTo(arg2)>0)?arg1:arg2);
	  }

	public static void main(String[] args) {
	}
}

---------- END SOURCE ----------
(Incident Review ID: 193198) 
======================================================================

Comments
EVALUATION A bound is either a type variable, or a class/interface + optional interfaces (JLS 4.4). If a type variable could be followed by type variables or by (possibly parameterized) interfaces, there would likely be more mutually recursive type variables, which are very difficult to handle. Things are already complicated when a bound is simply a parameterized type, e.g. <S,R extends Comparable<S>>. Consequently, bounds are not going to change now. Both javac and Eclipse agree that S&T and S&Comparable<S> are illegal.
25-09-2007

EVALUATION True. Is there a deep reason for this restriction that I'm missing? ###@###.### 2003-08-20
20-08-2003