-
Enhancement
-
Resolution: Unresolved
-
P5
-
6
-
Fix Understood
From: Neal Gafter <###@###.###>
Subject: javac bug? spec bug? (array type as a bound)
Date: Fri, 22 Sep 2006 12:11:29 -0700
Javac rejects this:
class X<T extends Object[]> {}
While examining JLS3 to see if it is allowed, I find 3.3 says
Type variables have an optional bound, T & I1 ... In. The bound consists of either a type variable, or a class or interface type T possibly followed by further interface types I 1 , ..., In. ...
According to 4.3, array types are not class types, so javac is correct. However, JLS does allow a type argument to be an array type:
interface Foo<T> {}
class Main {
public static void main(String[] args) {
Foo<Object[]> f = null;
}
}
Here's the problem: the capture conversion as specified will infer a type variable with an array as a bound, even though no such type exists according to 3.3:
interface Foo<T> {}
class Main {
static <T> Foo<? extends T> foo(T t) { return null; }
public static void main() {
int x = foo(new Integer[0]);
}
}
// X.java: incompatible types
// found : Foo<capture of ? extends java.lang.Integer[]>
// required: int
// int x = foo(new Integer[0]);
// ^
Further, there are various places in the spec (particularly 15.12.2.7) that say things like "If X is a type variable with an upper bound that is an array type...", even though such bounds are forbidden by 3.3.
I recommend that the JLS and javac uniformly allow array types as bounds for type variables in the surface syntax.
Subject: javac bug? spec bug? (array type as a bound)
Date: Fri, 22 Sep 2006 12:11:29 -0700
Javac rejects this:
class X<T extends Object[]> {}
While examining JLS3 to see if it is allowed, I find 3.3 says
Type variables have an optional bound, T & I1 ... In. The bound consists of either a type variable, or a class or interface type T possibly followed by further interface types I 1 , ..., In. ...
According to 4.3, array types are not class types, so javac is correct. However, JLS does allow a type argument to be an array type:
interface Foo<T> {}
class Main {
public static void main(String[] args) {
Foo<Object[]> f = null;
}
}
Here's the problem: the capture conversion as specified will infer a type variable with an array as a bound, even though no such type exists according to 3.3:
interface Foo<T> {}
class Main {
static <T> Foo<? extends T> foo(T t) { return null; }
public static void main() {
int x = foo(new Integer[0]);
}
}
// X.java: incompatible types
// found : Foo<capture of ? extends java.lang.Integer[]>
// required: int
// int x = foo(new Integer[0]);
// ^
Further, there are various places in the spec (particularly 15.12.2.7) that say things like "If X is a type variable with an upper bound that is an array type...", even though such bounds are forbidden by 3.3.
I recommend that the JLS and javac uniformly allow array types as bounds for type variables in the surface syntax.
- relates to
-
JDK-8013843 15.10.3: Array access for array-bounded capture variable
- Open
-
JDK-6531075 Missing synthetic casts when accessing fields/methods of intersection types including type variables
- Closed
-
JDK-6531090 Cannot access methods/fields of a captured type belonging to an intersection type
- Closed
-
JDK-6569404 Regression: Cannot instantiate an inner class of a type variable
- Closed
-
JDK-6644562 4.9: Simplify membership of single-bound intersection types
- Open
-
JDK-8061410 Multiply-bounded reference type expressions
- Closed
(1 relates to)