Type parameters inside super() calls?

Archie Cobbs archie.cobbs at gmail.com
Wed Feb 1 20:45:04 UTC 2023


This program compiles without error:

import java.util.concurrent.atomic.*;
public class TypeParamStaticContext<T> extends AtomicReference<T> {
    public TypeParamStaticContext(Object obj) {
        super((T)obj);
    }
}

Yet according to my reading of the JLS, the appearance of T inside the
super() call should be disallowed:

§8.8.7.1:

An explicit constructor invocation statement introduces a static context (
§8.1.3
<https://docs.oracle.com/javase/specs/jls/se19/html/jls-8.html#jls-8.1.3>),
which limits the use of constructs that refer to the current object.
Notably, the keywords this and super are prohibited in a static context (
§15.8.3
<https://docs.oracle.com/javase/specs/jls/se19/html/jls-15.html#jls-15.8.3>,
§15.11.2
<https://docs.oracle.com/javase/specs/jls/se19/html/jls-15.html#jls-15.11.2>),
as are unqualified references to instance variables, instance methods, and type
parameters of lexically enclosing declarations (§6.5.5.1
<https://docs.oracle.com/javase/specs/jls/se19/html/jls-6.html#jls-6.5.5.1>,
§6.5.6.1
<https://docs.oracle.com/javase/specs/jls/se19/html/jls-6.html#jls-6.5.6.1>,
§15.12.3
<https://docs.oracle.com/javase/specs/jls/se19/html/jls-15.html#jls-15.12.3>).


§6.5.5.1:

If a type name consists of a single *Identifier*, then the identifier must
occur in the scope of exactly one declaration of a class, interface, or
type parameter with this name (§6.3
<https://docs.oracle.com/javase/specs/jls/se19/html/jls-6.html#jls-6.3>),
or a compile-time error occurs.

If the declaration denotes a type parameter of a generic class or interface
C (§8.1.2
<https://docs.oracle.com/javase/specs/jls/se19/html/jls-8.html#jls-8.1.2>,
§9.1.2
<https://docs.oracle.com/javase/specs/jls/se19/html/jls-9.html#jls-9.1.2>),
then both of the following must be true, or a compile-time error occurs:

   -

   The type name does not occur in a static context (§8.1.3
   <https://docs.oracle.com/javase/specs/jls/se19/html/jls-8.html#jls-8.1.3>
   )
   -

   If 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 is an inner class of C.


What am I missing?

Thanks,
-Archie

-- 
Archie L. Cobbs
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/compiler-dev/attachments/20230201/01f23400/attachment.htm>


More information about the compiler-dev mailing list