Does JLS tell if this program is legal?

Stephan Herrmann stephan.herrmann at berlin.de
Thu May 23 10:19:51 UTC 2024


Thanks, Dan,

Am 22.05.24 um 02:13 schrieb Dan Smith:
>> On May 21, 2024, at 12:49 PM, Stephan Herrmann <stephan.herrmann at berlin.de> wrote:
>>
>> * Super<Inner<?>> is a supertype of T / One<?> (directly)
> 
> Note that "is a supertype" here isn't true though:
> 
> Is One<?> <: Super<Inner<?>>
> Is One<CAP> <: Super<Inner<?>>
> Is Super<Inner<CAP>> <: Super<Inner<?>>
> Is Inner<CAP> = Inner<?>
> false

Wow, another corner I hadn't yet looked at. :) I had assumed that capture 
applies only when something is being accessed / evaluated. Going from a type to 
its supertype didn't look like evaluation to me.

But now looking at §4.10.2 I see it speaking of capture indeed.

So that would change my analysis
from Super<Inner<?>> is a supertype of T / One<?>
to   Super<Inner<CAP>> is a supertype of T / One<?>

and we continue:
* S1 = Inner<X>
* T1 = Inner<CAP>
* new constraint formula created: ⟨Inner<X> = Inner<CAP>⟩

This, too, reduces to false, and again I would be without an explanation how 
'accept' could be correct.

> This is related to
> https://bugs.openjdk.org/browse/JDK-8016196
> 
> Unfortunately, JLS doesn't specify a way to map from subtypes to supertypes when wildcards are involved. (It's one of those unexplained corners of the type system.)

Isn't that given by that sentence I discovered just now?
	"Given a generic class or interface C with type parameters F1,...,Fn (n > 0), 
the direct supertypes of the parameterized type C<R1,...,Rn> where at least one 
of the Ri (1 ≤ i ≤ n) is a wildcard type argument, are the direct supertypes of 
the parameterized type C<X1,...,Xn> which is the result of applying capture 
conversion to C<R1,...,Rn> (§5.1.10)."

> I've got a solution in my mind that involves the upward projection operation we introduced in 4.10.5: capture, find the supertype, then project away the capture variable. But there are details to work out.

If I apply this strategy in ecj I get these pairs of supertypes of One<?> , Y
* One<?>, One<X>
* Super<? extends Inner<?>>, Super<Inner<X>>

In both pairs we find a wildcard as a direct type argument and hence NO new 
constraint would be generated in §18.3.1.

Is that what you have in mind?

Does javac already implement this application of projection?

thanks,
Stephan


More information about the compiler-dev mailing list