-
Bug
-
Resolution: Not an Issue
-
P4
-
19
-
generic
-
generic
A DESCRIPTION OF THE PROBLEM :
The following code uses a method parameter with invalid bound, but is accepted by the compiler:
class Parent {}
class Child extends Parent {}
class Container<T extends Child> {}
public class Test {
public static <T extends Parent> int method(Container<? super T> par) { // Why is this allowed?
return 0;
}
public static void main(String args[]) {
if (method(new Container<>()) == 0) {
System.out.println("Zero");
}
}
}
We are using the type argument ? super T that has no upper bound and does not satisfy the type parameter declaration on Container<T extends Child> {}.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Just compile the give code with the latest Javac tool.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The code should be rejected by the compiler.
ACTUAL -
The code is allowed without any error.
---------- BEGIN SOURCE ----------
class Parent {}
class Child extends Parent {}
class Container<T extends Child> {}
public class Test {
public static <T extends Parent> int method(Container<? super T> par) { // Why is this allowed?
return 0;
}
public static void main(String args[]) {
if (method(new Container<>()) == 0) {
System.out.println("Zero");
}
}
}
---------- END SOURCE ----------
FREQUENCY : always
The following code uses a method parameter with invalid bound, but is accepted by the compiler:
class Parent {}
class Child extends Parent {}
class Container<T extends Child> {}
public class Test {
public static <T extends Parent> int method(Container<? super T> par) { // Why is this allowed?
return 0;
}
public static void main(String args[]) {
if (method(new Container<>()) == 0) {
System.out.println("Zero");
}
}
}
We are using the type argument ? super T that has no upper bound and does not satisfy the type parameter declaration on Container<T extends Child> {}.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Just compile the give code with the latest Javac tool.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The code should be rejected by the compiler.
ACTUAL -
The code is allowed without any error.
---------- BEGIN SOURCE ----------
class Parent {}
class Child extends Parent {}
class Container<T extends Child> {}
public class Test {
public static <T extends Parent> int method(Container<? super T> par) { // Why is this allowed?
return 0;
}
public static void main(String args[]) {
if (method(new Container<>()) == 0) {
System.out.println("Zero");
}
}
}
---------- END SOURCE ----------
FREQUENCY : always