JDK-8081797 : Type inference regression in Java 8's compiler
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 8u45
  • Priority: P3
  • Status: Closed
  • Resolution: Cannot Reproduce
  • OS: windows_8
  • CPU: x86
  • Submitted: 2015-06-01
  • Updated: 2015-06-03
  • Resolved: 2015-06-03
Related Reports
Relates :  
Description
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);
        }
    }
}



Comments
Closing this as cannot reproduce.
03-06-2015

1) Run the attached test case (Test.java) with JDK versions. 2) Checked with JDK 8, 8u45, 8u60 ea b17, and 9 ea b66 and couldn't reproduce the issue as reported by the submitter. The test always compiled fine different JDK versions.
03-06-2015