Additionally to the answers given already I'll add some formal explanation.
Given by 4.10.2 (emp. mine)
Given a generic type declaration C (n > 0), the direct
supertypes of the parameterized type C, where Ti (1 ≤ i ≤
n) is a type, are all of the following:
D < U1 θ,...,Uk θ>, where D is a generic type which is a
direct supertype of the generic type C and θ is the
substitution [F1:=T1,...,Fn:=Tn].
C < S1,...,Sn> , where Si contains Ti (1 ≤ i ≤ n) (§4.5.1).
The type Object, if C is a generic interface type with no
direct superinterfaces.
The raw type C.
Rule for contains
are specified at 4.5.1:
A type argument T1 is said to contain another type argument T2,
written T2 <= T1, if the set of types denoted by T2 is provably a
subset of the set of types denoted by T1 under the reflexive and
transitive closure of the following rules (where <: denotes subtyping
(§4.10)):
? extends T <= ? extends S if T <: S
? extends T <= ?
? super T <= ? super S if S <: T
? super T <= ?
? super T <= ? extends Object
T <= T
T <= ? extends T
T <= ? super T
Since T <= ? super T <= ? extends Object = ?
so applying 4.10.2 Foo<T> <: Foo<?>
we have ? extends Foo<T> <= ? extends Foo<?>
. But Foo<T> <= ? extends Foo<T>
so we have Foo<T> <= ? extends Foo<?>
.
Applying 4.10.2 we have that Set<? extends Foo<?>>
is a direct supertype of Set<Foo<T>>
.
The formal answer to why your first example does not compile may be got by assuming a contradiction. Percisely:
If Set<Foo<T>> <: Set<Foo<?>>
we have that Foo<T> <= Foo<?>
which is not possible to prove applying reflexive or transitive relations to rules from 4.5.1.