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

Type inference regression in Java 8's compiler

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Cannot Reproduce
    • Icon: P3 P3
    • None
    • 8u45
    • tools
    • x86
    • windows_8

      FULL PRODUCT VERSION :
      java version "1.8.0_45"
      Java(TM) SE Runtime Environment (build 1.8.0_45-b15)
      Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.3.9600]

      A DESCRIPTION OF THE PROBLEM :
      There is a certain kind of generic type inference that no longer works with Java 8 compilers.

      REGRESSION. Last worked in version 7u76

      ADDITIONAL REGRESSION INFORMATION:
      java version "1.7.0_67"
      Java(TM) SE Runtime Environment (build 1.7.0_67-b01)
      Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      javac fails to compile the following code
      --------------------------------------------
      import java.util.List;

      public class Test {
          static final void a(Class<? extends List<?>> type) {
              b(newList(type));
          }

          static final <T> List<T> b(List<T> list) {
              return list;
          }

          static final <L extends List<?>> L newList(Class<L> type) {
              try {
                  return type.newInstance();
              }
              catch (Exception e) {
                  throw new RuntimeException(e);
              }
          }
      }


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The above program should compile
      ACTUAL -
      A compilation error

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Test.java:5: error: method b in class Test cannot be applied to given types;
              b(newList(type));
              ^
        required: List<T>
        found: CAP#1
        reason: inference variable L has incompatible bounds
          equality constraints: CAP#2
          upper bounds: List<CAP#3>,List<?>
        where T,L are type-variables:
          T extends Object declared in method <T>b(List<T>)
          L extends List<?> declared in method <L>newList(Class<L>)
        where CAP#1,CAP#2,CAP#3 are fresh type-variables:
          CAP#1 extends List<?> from capture of ? extends List<?>
          CAP#2 extends List<?> from capture of ? extends List<?>
          CAP#3 extends Object from capture of ?
      1 error

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      See above
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Assign the argument to b() to a local variable to prevent the need for type inference.
      --------------------------------------------
      import java.util.List;

      public class Test {
          static final void a(Class<? extends List<?>> type) {
              List<?> list = newList(type);
              b(list);
          }

          static final <T> List<T> b(List<T> list) {
              return list;
          }

          static final <L extends List<?>> L newList(Class<L> type) {
              try {
                  return type.newInstance();
              }
              catch (Exception e) {
                  throw new RuntimeException(e);
              }
          }
      }


            pardesha Pardeep Sharma
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: