Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8081318

Diamond inference fails to infer capture variable parameterization

XMLWordPrintable

    • b60
    • 9

      let's consider following example:

          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) { };
              }
          }

      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 C are wildcards, then:

              The types of the fields, methods, and constructors in C<T1,...,Tn> are the types of the fields, methods, and constructors in the capture conversion of C<T1,...,Tn> (§5.1.10).

      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 comment compilation 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.***

            dlsmith Dan Smith
            grakov Georgiy Rakov (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated: