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

Inference failure with unchecked subtyping and arrays

    XMLWordPrintable

Details

    • 9
    • b124
    • x86
    • generic
    • Verified

    Description

      FULL PRODUCT VERSION :
      java version "9-ea"
      Java(TM) SE Runtime Environment (build 9-ea+122)
      Java HotSpot(TM) 64-Bit Server VM (build 9-ea+122, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Mac OS X 10.11.5

      A DESCRIPTION OF THE PROBLEM :
      Code which uses generics, varargs, and overloaded methods compiles on JDK 8 but fails to compile on JDK 9-ea+122.

      Specifically, within the compose() method in the supplied Test class, the following line of code compiles on JDK 8 and 9:

      Test.<Number> allOf(conditions);

      But the following line does not compile on JDK 9:

      allOf(conditions);

      REGRESSION. Last worked in version 8u92

      ADDITIONAL REGRESSION INFORMATION:
      java version "1.8.0_92"
      Java(TM) SE Runtime Environment (build 1.8.0_92-b14)
      Java HotSpot(TM) 64-Bit Server VM (build 25.92-b14, mixed mode)

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      javac Test.java

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I expect the supplied Test class to compile on JDK 9.
      ACTUAL -
      The supplied Test class does not compile on JDK 9.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Test.java:22: error: no suitable method found for allOf(Test.Condition<? super Number>[])
      allOf(conditions);
      ^
          method Test.<T#1>allOf(Test.Condition<? super T#1>...) is not applicable
            (cannot infer type-variable(s) T#1
              (varargs mismatch; Test.Condition<? super Number>[] cannot be converted to Test.Condition<? super T#1>))
          method Test.<T#2>allOf(Iterable<? extends Test.Condition<? super T#2>>) is not applicable
            (cannot infer type-variable(s) T#2
              (argument mismatch; Test.Condition<? super Number>[] cannot be converted to Iterable<? extends Test.Condition<? super T#2>>))
        where T#1,T#2 are type-variables:
          T#1 extends Object declared in method <T#1>allOf(Test.Condition<? super T#1>...)
          T#2 extends Object declared in method <T#2>allOf(Iterable<? extends Test.Condition<? super T#2>>)
      1 error

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public class Test {

      public class Condition<T> {}

      @SafeVarargs
      public static <T> Condition<T> allOf(Condition<? super T>... conditions) {
      return null;
      }

      public static <T> Condition<T> allOf(Iterable<? extends Condition<? super T>> conditions) {
      return null;
      }

      @SafeVarargs
      public static Condition<Number> compose(Condition<? super Number>... conditions) {

      // Compiles on JDK 8 and JDK 9
      Test.<Number> allOf(conditions);

      // Compiles on JDK 8 but *fails* on JDK 9
      allOf(conditions);

      return null;
      }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      The workaround is to explicitly supply the generic type (i.e., <Number>) as follows.

      Test.<Number> allOf(conditions);

      Attachments

        Issue Links

          Activity

            People

              mcimadamore Maurizio Cimadamore
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              6 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: