JDK-8145978 : Javac fails to compile valid program with intersection type containing clone()
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 8u60
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • OS: generic
  • CPU: x86_64
  • Submitted: 2015-10-08
  • Updated: 2016-05-26
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.
Other
tbd_majorUnresolved
Related Reports
Blocks :  
Description
FULL PRODUCT VERSION :
java version "1.7.0_45"
Java(TM) SE Runtime Environment (build 1.7.0_45-b18)
Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Linux titan 2.6.34.10-26906-default #1 SMP 2012-05-28 16:10:01 +0200 x86_64 x86_64 x86_64 GNU/Linux


A DESCRIPTION OF THE PROBLEM :
The javac compiler rejects this valid Java program:

public class xx {

    interface PublicCloneable {
        Object clone();
    }

    interface HasPosition {
        int getPosition();
        void setPosition(int p);
    }

    @SuppressWarnings("unchecked")
    <E extends PublicCloneable & HasPosition> E cloneAndIncrementPosition(E elem) {
        final E clone = (E)elem.clone();
        clone.setPosition(clone.getPosition() + 1);
        return clone;
    }
}

Errors reported:

xx.java:13: error: clone() in Object cannot implement clone() in PublicCloneable
    <E extends PublicCloneable & HasPosition> E cloneAndIncrementPosition(E elem) {
     ^
  attempting to assign weaker access privileges; was public
xx.java:14: error: clone() has protected access in Object
        final E clone = (E)elem.clone();
                               ^
2 errors


STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Try to compile this program:

public class xx {

    interface PublicCloneable {
        Object clone();
    }

    interface HasPosition {
        int getPosition();
        void setPosition(int p);
    }

    @SuppressWarnings("unchecked")
    <E extends PublicCloneable & HasPosition> E cloneAndIncrementPosition(E elem) {
        final E clone = (E)elem.clone();
        clone.setPosition(clone.getPosition() + 1);
        return clone;
    }
}


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Program compiles successfully.
ACTUAL -
xx.java:13: error: clone() in Object cannot implement clone() in PublicCloneable
    <E extends PublicCloneable & HasPosition> E cloneAndIncrementPosition(E elem) {
     ^
  attempting to assign weaker access privileges; was public
xx.java:14: error: clone() has protected access in Object
        final E clone = (E)elem.clone();
                               ^
2 errors


ERROR MESSAGES/STACK TRACES THAT OCCUR :
xx.java:13: error: clone() in Object cannot implement clone() in PublicCloneable
    <E extends PublicCloneable & HasPosition> E cloneAndIncrementPosition(E elem) {
     ^
  attempting to assign weaker access privileges; was public
xx.java:14: error: clone() has protected access in Object
        final E clone = (E)elem.clone();
                               ^
2 errors


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
public class xx {

    interface PublicCloneable {
        Object clone();
    }

    interface HasPosition {
        int getPosition();
        void setPosition(int p);
    }

    @SuppressWarnings("unchecked")
    <E extends PublicCloneable & HasPosition> E cloneAndIncrementPosition(E elem) {
        final E clone = (E)elem.clone();
        clone.setPosition(clone.getPosition() + 1);
        return clone;
    }
}

---------- END SOURCE ----------


Comments
Need a JLS clarification on what constitutes a well-formed intersection type: JDK-7120669
26-05-2016

I think this issue is more subtle than it seems: the following class hierarchy: interface PublicCloneable { Object clone(); } interface HasPosition { int getPosition(); void setPosition(int p); } abstract class E implements PublicCloneable, HasPosition { } Does not compile and yields an error (in both javac and Eclipse): Main.java:52: error: clone() in Object cannot implement clone() in PublicCloneable abstract class E implements PublicCloneable, HasPosition { } ^ attempting to assign weaker access privileges; was public So, I think the issue is that the intersection type is not well-formed, but compilers are failing to spot this and ending up with non-deterministic behavior. Assigning to spec for evaluation.
22-12-2015

http://stackoverflow.com/questions/33023332/why-doesnt-this-java-program-compile
22-12-2015

Test result: *************** OS: Ubuntu Linux 14.04, Windows 7 64 bit JAVAC: ###### 1.6.0_45-b06 : Fail 1.7.0_85-b15 : Fail 1.8.0-b132 : Fail 1.8.0_51-b31 : Fail 1.8.0_65-b17 : Fail 1.8.0_66-b17 : Fail 1.8.0_72-b12 : Fail 1.9.0-ea-b96 : Fail Eclipse: ###### 1.6.0_45-b06 : Pass 1.7.0_85-b15 : Pass 1.8.0-b132 : Pass 1.8.0_51-b31 : Pass 1.8.0_65-b17 : Pass 1.8.0_66-b17 : Pass 1.8.0_72-b12 : Pass 1.9.0-ea-b96 : Pass
17-12-2015