Assume class B
inherits from class A
. The following is legal Java:
List<A> x;
List<? super B> y = x;
In terms of the specification, this means that List<A>
assignsTo List<? super B>
. However, I am having trouble finding the part of the spec that says this is legal. In particular, I believe we should have the subtype relation
List<A> <: List<? super B>
but section 4.10 of the Java 8 spec defines the subtype relation as the transitive closure of a direct supertype relation S >1 T
, and it defines the direct supertype relation in terms of a finite function which computes a set of supertypes of T
. There is no bounded function which on input List<A>
can produce List<? super B>
since there might be an arbitrary number of B
s that inherit from A
, so the spec's subtype definition seems to break down for super wildcards. Section 4.10.2 on "Subtyping among class and interface types" does mention wildcards, but it handles only the other direction where the wildcard appears in the potential subtype (this direction fits into the computed direct supertype mechanism).
Question: What part of the spec says that the above code is legal?
The motivation is for compiler code, so it's not enough to understand why it is legal intuitively or come up with an algorithm that handles it. Since the general subtyping problem in Java is undecidable, I would like to handle exactly the same cases as the spec, and therefore want the part of the spec that handles this case.