JDK-8301649 : Compiler allows class type parameters inside super()'s static context
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Priority: P4
  • Status: Resolved
  • Resolution: Duplicate
  • OS: generic
  • CPU: generic
  • Submitted: 2023-02-01
  • Updated: 2023-08-17
  • Resolved: 2023-08-15
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 22
22Resolved
Related Reports
Duplicate :  
Relates :  
Description
javac accepts the following program:

{noformat}
import java.util.concurrent.atomic.*;

public class TypeParamStaticContext<T> extends AtomicReference<T> {
    public TypeParamStaticContext(Object obj) {
        super((T)obj);
    }
    public <U extends T> TypeParamStaticContext(Object obj, int x) {
        super((U)obj);
    }
}
{noformat}

However, the references to {{T}} and {{U}} are invalid because the inside of a {{super()}} call is a static context.

Relevant JLS sections:

§8.8.7.1:

{quote}
An explicit constructor invocation statement introduces a static context (§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, §15.11.2), as are unqualified references to instance variables, instance methods, and type parameters of lexically enclosing declarations (§6.5.5.1, §6.5.6.1, §15.12.3).
{quote}

§6.5.5.1:

{quote}
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), or a compile-time error occurs.

If the declaration denotes a type parameter of a generic class or interface C (§8.1.2, §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)

* 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. 
{quote}

Comments
Fixed by spec change; see JDK-8302041.
15-08-2023

Discussion here: https://mail.openjdk.org/pipermail/compiler-dev/2023-February/021960.html Current thinking is the JLS needs to change, such that constructors would have a weaker variant of "static context" which doesn't exclude type parameters prior to super().
02-02-2023