JDK-8186643 : Compiler error on generic intersection type declaration with two similar interfaces, one with a default implementation
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 8,9
  • Priority: P3
  • Status: Resolved
  • Resolution: Duplicate
  • OS: generic
  • CPU: generic
  • Submitted: 2017-08-22
  • Updated: 2017-08-25
  • Resolved: 2017-08-25
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 10
10Resolved
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
java version "1.8.0_144"
Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 10.0.14393]

A DESCRIPTION OF THE PROBLEM :
Java compiler "javac" fails on the generic intersection type declaration '<T extends I1 & IDefault>' where I1 and IDefault are interfaces, both defining the same method ('String get();'), one with a default implementation ('default String get() {return "default";};').

See also https://stackoverflow.com/q/45798233/6505250

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile following code via "javac Foo.java":

interface I1 {
    String get();
}

interface I2 {
    String get();
}

interface IDefault {
    default String get() {
    	return "default";
    };
}

public class Foo implements I1, I2, IDefault {

    @Override
    public String get() {
        return "foo";
    }

    public static void main(String[] args) {
        System.out.print(getOf(new Foo()));
    }

 // static <T extends I1 & I2> String getOf(T t) { // OK
 // static <T extends I1, IDefault> String getOf(T t) { // OK
    static <T extends I1 & IDefault> String getOf(T t) { // OK with Eclipse compiler, but fails with javac:
 //         ^
 //  where INT#1 is an intersection type:
 //    INT#1 extends Object,I1,IDefault
 //1 error

        return t.get();
    }

}



EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
No compiler error (as with the Eclipse compiler)
ACTUAL -
Compiler error (see error message below)


ERROR MESSAGES/STACK TRACES THAT OCCUR :
        static <T extends I1 & IDefault> String getOf(T t) {
                ^
  where INT#1 is an intersection type:
    INT#1 extends Object,I1,IDefault
1 error

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
interface I1 {
    String get();
}

interface I2 {
    String get();
}

interface IDefault {
    default String get() {
    	return "default";
    };
}

public class Foo implements I1, I2, IDefault {

    @Override
    public String get() {
        return "foo";
    }

    public static void main(String[] args) {
        System.out.print(getOf(new Foo()));
    }

 // static <T extends I1 & I2> String getOf(T t) { // OK
 // static <T extends I1, IDefault> String getOf(T t) { // OK
    static <T extends I1 & IDefault> String getOf(T t) { // OK with Eclipse compiler, but fails with javac:
 //         ^
 //  where INT#1 is an intersection type:
 //    INT#1 extends Object,I1,IDefault
 //1 error

        return t.get();
    }

}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Use Eclipse compiler (Eclipse Oxygen/4.7) or avoid such code


Comments
Spec requires clarification, as described in JDK-7120669.
25-08-2017

This doesn't look like javac issue, According to https://docs.oracle.com/javase/specs/jls/se8/html/jls-9.html#jls-9.4.1.3 If an interface I inherits a default method whose signature is override-equivalent with another method inherited by I, then a compile-time error occurs. (This is the case whether the other method is abstract or default.) So the compile time error, as expected. Below are the results 8u144 - Fail 8u152 - Fail 9 ea b181 - Fail
23-08-2017