JDK-8079622 : Classes with generics that compile on 1.7.0_75 fail in 1.8.0_31
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 8
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_7
  • CPU: x86_64
  • Submitted: 2015-03-03
  • Updated: 2016-01-22
  • Resolved: 2016-01-22
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
java version "1.8.0_31"
Java(TM) SE Runtime Environment (build 1.8.0_31-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.31-b07, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 6.1.7601]

A DESCRIPTION OF THE PROBLEM :
Generics scenario that compiles in java 1.7.0_75, and does not show any compilation error in eclipse and intellij idea, does not compile in java 8. 

Probably has to do with self referencng generics similar to the Enum class (Enum<E extends Enum<E>>). Class is included in reproduction steps below.

REGRESSION.  Last worked in version 7u75

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

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
put the following class in TestGenerics.java

public class TestGenerics {

    public static class SelfReferencingType<T extends SelfReferencingType<T>> { }
    
    public static class SelfReferencing<T extends SelfReferencingType<T>> { }
    
    public SelfReferencing<?> test(final SelfReferencingType<?> sr) {
        return innerTest(sr);
    }
    
    public <T extends SelfReferencingType<T>> SelfReferencing<T> innerTest(final SelfReferencingType<T> sr) {
        return new SelfReferencing<T>();
    }
    
}

and execute:

javac TestGenerics.java

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Expected the class to compile, the same result obtained using java 1.7.0_75
ACTUAL -
TestGenerics.java:8: error: incompatible types: inference variable T has incompatible bounds
        return innerTest(sr);
                        ^
    equality constraints: CAP#1
    upper bounds: SelfReferencingType<CAP#2>,SelfReferencingType<T>
  where T is a type-variable:
    T extends SelfReferencingType<T> declared in method <T>innerTest(SelfReferencingType<T>)
  where CAP#1,CAP#2 are fresh type-variables:
    CAP#1 extends SelfReferencingType<CAP#1> from capture of ?
    CAP#2 extends SelfReferencingType<CAP#2> from capture of ?
1 error

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
public class TestGenerics {

    public static class SelfReferencingType<T extends SelfReferencingType<T>> { }
    
    public static class SelfReferencing<T extends SelfReferencingType<T>> { }
    
    public SelfReferencing<?> test(final SelfReferencingType<?> sr) {
        return innerTest(sr);
    }
    
    public <T extends SelfReferencingType<T>> SelfReferencing<T> innerTest(final SelfReferencingType<T> sr) {
        return new SelfReferencing<T>();
    }
    
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
a lot of refactoring to remove self referencing generics and use explicit type casts instead