-
Enhancement
-
Resolution: Unresolved
-
P4
-
None
-
5.0, 6
-
Fix Understood
-
generic
-
generic
Consider the example in 5042462.
We have the following type variables:
<T, U extends T, V extends T>
The JLS (p463) defines lub in terms of the EST (erased supertype set).
EST(U) = {Object} and
EST(V) = {Object}.
Then EC = { Object } = MEC.
So lub(U,V) = Object.
It should be T.
We have the following type variables:
<T, U extends T, V extends T>
The JLS (p463) defines lub in terms of the EST (erased supertype set).
EST(U) = {Object} and
EST(V) = {Object}.
Then EC = { Object } = MEC.
So lub(U,V) = Object.
It should be T.
- duplicates
-
JDK-6557665 Type variables are erased during lub computation
-
- Closed
-
- relates to
-
JDK-6357966 parameters containing nested generic types avoid recursive calls
-
- Closed
-
-
JDK-6569277 inference: uninferred type args should follow their upper bounds
-
- Closed
-
-
JDK-5042462 Problem with least upper bound (lub) and type variables
-
- Closed
-
I am inclined to think we should pass the bounds of every type argument to the inference process and use them in the lub calculation.
Consider the example in 6557661, slightly modified below:
<T> Map<T,T> foo(T t1, T t2) { return null; }
<U extends Object, V extends U> Map<U,U> bar(U u, V v) {
Map<Object,Object> map1 = foo(u, v); // Allowed by javac 1.6
Map<U,U> map2 = foo(u, v); // Should be allowed
return map2;
}
Suppose bar is called with U and V as some subtypes of a common supertype that isn't Object:
Map<...,...> x = bar(new Sub1(), new Sub2());
U is inferred as Sub1 and V as Sub2. For bar's own call to foo, a good lub would calculate T=lub(U,V)=U (=Sub1), so foo will return Map<U,U> (=Map<Sub1,Sub1>) which is safe to assign to map2 and return to the call ofer bar, which can safely expect a Map<Sub1,Sub1>.