[External] : Re: Type parameters inside super() calls?

Alex Buckley alex.buckley at oracle.com
Wed Feb 1 23:42:56 UTC 2023


// Paging Dan, since 6.5.5.1 was reworked in JLS16.

With regard to the example involving a generic constructor, 8.8.4 is 
clear about what it wants to happen:

   "References to a constructor's type parameter from an explicit 
constructor invocation statement or a nested class or interface are 
restricted, as specified in §6.5.5.1."

but 6.5.5.1 doesn't actually specify that. It specifies what happens 
when an explicit constructor invocation statement refers to a class's or 
interface's type parameter:

   "If the declaration denotes a type parameter of a generic class or 
interface C (§8.1.2, §9.1.2), then ..."

And it specifies what happens when a nested class or interface refers to 
a constructor's type parameter:

   "If the declaration denotes a type parameter of a generic method or 
constructor m (§8.4.4, §8.8.4), and the type name appears directly or 
indirectly in the body of ..., then ..."

But it has nothing to say when an explicit constructor invocation 
statement refers to a constructor's type parameter.

Catching this case is tricky given the current factoring. The flow 
probably needs to be:

-----
If a type name consists of a single Identifier ...

If the declaration denotes a type parameter of a generic class, 
interface, constructor, or method, then the type name must not occur in 
a static context, or a compile-time error occurs.

If the declaration denotes a type parameter of a generic class or 
interface C, and the type name appears in a nested class or interface 
declaration of C, then the immediately enclosing class or interface 
declaration of the type name must be an inner class of C, or a 
compile-time error occurs.

If the declaration denotes a type parameter of a generic method or 
constructor m, and the type name appears directly or indirectly in the 
body of a local class, local interface, or anonymous class D declared 
directly in the body of m, then (i) D must be an inner class, and (ii) 
the immediately enclosing class or interface declaration of the type 
name must be D or an inner class of D, or a compile-time error occurs.
-----

And Example 6.5.5.1-1 should have a couple of constructors for Box<T>, 
one non-generic and performing `super((T)...);`, the other generic in U 
and performing `super((U)...);`.

Alex

On 2/1/2023 2:55 PM, Archie Cobbs wrote:
> On Wed, Feb 1, 2023 at 4:40 PM Alex Buckley <alex.buckley at oracle.com 
> <mailto:alex.buckley at oracle.com>> wrote:
> 
>     Your reading of the JLS is correct. The cast expression `(T)obj` occurs
>     in a static context, so the use of T should be disallowed, and javac
>     should reject the program.
> 
> 
> Thanks. Filed as JDK-8301649 <https://bugs.openjdk.org/browse/JDK-8301649>.
> 
> -Archie
> 
> -- 
> Archie L. Cobbs


More information about the compiler-dev mailing list