Java compiler bugs
During the work on the Java code related to my master’s thesis, I’ve run into two bugs in Sun’s Java compiler (using Java 6).
The first bug was Bug ID: 6570761 Possible generics regression - inconvertible types. I’ve since changed my the design so that this bug no longer affects me, but it was annoying none the less.
The second bug has been reported by others, but there doesn’t seem to exist a report for it in Sun’s bug database. I submitted a bug report to them in November of 2007, but the report seems to have been ignored since then. I figured I’d reproduce the report here in case anybody else runs into this problem.
The offending code looks like this:
interface IA {
IA op();
}
interface IB {
IB op();
}
public interface IC extends IA, IB {
IC op();
}
The error message is:
IC.java:7: types IB and IA are incompatible; both define op(), but with unrelated return types
This same issue has previously been raised for the Eclipse compiler:
- [1.5][compiler] Multiple interface inheritance is incompatible with Sun compiler
- [1.5][compiler] Compiler misses return type substitutability error with multiple inheritance and overriding method
- [news.eclipse.tools.jdt] Multiple inheritance and covariance subtyping in Java 1.5 and jdt core
It seems §9.4.1 of the Java Language Specification applies to this problem. It says:
“An interface inherits from its direct superinterfaces all methods of the superinterfaces that are not overridden by a declaration in the interface. It is possible for an interface to inherit several methods with override-equivalent signatures (§8.4.2). Such a situation does not in itself cause a compile-time error. The interface is considered to inherit all the methods. However, one of the inherited methods must must be return type substitutable for any other inherited method; otherwise, a compile-time error occurs.”
While Sun drags its feet with this issue (maybe it’ll get fixed for Java 8 in a few decades from now), you can use Eclipse. If you need to build with Ant, you can get it to use the Eclipse compiler by setting the build.compiler property to org.eclipse.jdt.core.JDTCompilerAdapter and including ecj.jar in your Ant classpath.
It seems some French guys were also grappling with this problem: Héritage multiple des interfaces et surcharge de méthodes.
Update: Finally found the right bug for the covariant return problem with the help of Jonathan Gibbons from Sun. It is Bug ID: 6294779 Problem with interface inheritance and covariant return types. The bug was created in 2005. Don’t know why I couldn’t find it with my previous searches, but maybe other people will have better luck now.