A DESCRIPTION OF THE PROBLEM :
In this program, javac infers a Captured type of `? extends Object` as the type of the variable `x`.
I would expect to infer `B<? super T>`, since `B<? super T>` is more specific than `? extends Object`, and is a supertype of both `B<T>` and `B<? super T>`.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
javac A.java
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Compile
ACTUAL -
A.java:5: error: incompatible types: B<CAP#1> cannot be converted to B<? super T>
return x;
^
where T is a type-variable:
T extends Object declared in class A
where CAP#1 is a fresh type-variable:
CAP#1 extends Object from capture of ? extends Object
1 error
---------- BEGIN SOURCE ----------
class A<T> {
public A() {}
public B<? super T> foo() {
var x = (true) ? new B<T>() : (B<? super T>) null;
return x;
//return (true) ? new B<T>() : (B<? super T>) null; // This works
}
}
class B<T> {
public B() {}
}
---------- END SOURCE ----------
FREQUENCY : always
In this program, javac infers a Captured type of `? extends Object` as the type of the variable `x`.
I would expect to infer `B<? super T>`, since `B<? super T>` is more specific than `? extends Object`, and is a supertype of both `B<T>` and `B<? super T>`.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
javac A.java
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Compile
ACTUAL -
A.java:5: error: incompatible types: B<CAP#1> cannot be converted to B<? super T>
return x;
^
where T is a type-variable:
T extends Object declared in class A
where CAP#1 is a fresh type-variable:
CAP#1 extends Object from capture of ? extends Object
1 error
---------- BEGIN SOURCE ----------
class A<T> {
public A() {}
public B<? super T> foo() {
var x = (true) ? new B<T>() : (B<? super T>) null;
return x;
//return (true) ? new B<T>() : (B<? super T>) null; // This works
}
}
class B<T> {
public B() {}
}
---------- END SOURCE ----------
FREQUENCY : always
- relates to
-
JDK-8269348 Wrong type inference in presence of a generic with bound to Double
- Closed
-
JDK-8269737 Wrong Type-Argument inference in presence of bounds and use-site variance
- Closed