Field of wildcard parameterized class is passed to anonymous class created with diamond; JDK bug?

Georgiy Rakov georgiy.rakov at
Tue May 12 13:23:10 UTC 2015


let's consider following example:

    classPar<U> {

    classCls<T> {
         Cls(Tt) {}

    public classTest70  {
         public static voidtest() {
             Par<?> a =newPar<>();
             newCls<>(a.f) { };

JDK9b60 compiles it successfully however according to my understanding 
compilation should have failed because:

1. The type of 'a' local variable is a parameterized type Par<?>.
2. According to following assertion from JLS 4.5.2 the type of the field 
"f" of Par<?> is a fresh capture variable: null-type <: CAP <: Object:

    If any of the type arguments in the parameterization of Care
    wildcards, then: 


        The types of the fields, methods, and constructors in C|<|T_1
        ,...,T_n |>|are the types of the fields, methods, and
        constructors in the capture conversion of C|<|T_1 ,...,T_n

3. 'new Cls<>(a.f) { }' causes T to be inferred as capture variable CAP 
presented in step 2.
4. According to following new assertion presented in JDK-8073593 issue 
error should occur because superclass of the anonymous class is inferred 
as a type parameterized by type variable that was not declared as a type 
parameter (the capture variable CAP).

    ***/_*It is a compile-time error*_/ if the superclass or
    superinterface type of the anonymous class, T, or any subexpression
    of T, has one of the following forms:
    - A /_*type variable (4.4) that was not declared as a type
    parameter*_/ (such as /_*a type variable produced by capture
    conversion*_/ (5.1.10))
    - An intersection type (4.9)
    - A class or interface type, where the class or interface
    declaration is not accessible from the class or interface in which
    the expression appears.***
    The term "subexpression" includes type arguments of parameterized
    types (4.5), bounds of wildcards (4.5.1), and element types of array
    types (10.1). It excludes bounds of type variables.***

Could you please tell if you agree that this is really a JDK bug.

Thank you,
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>
-------------- next part --------------
class Par<U> {
    U f;

class Cls<T> {
    Cls(T t) {}

public class Test70  {
    public static void test() {
        Par<?> a = new Par<>();
        new Cls<>(a.f) { };

More information about the compiler-dev mailing list