inconsistent generic types behaviour when compiling together vs. separate
Peter Levart
peter.levart at gmail.com
Wed Jan 29 06:59:38 PST 2014
Here's an example:
Create an empty directory and copy into it the following two sources:
Ref.java:
public class Ref<T> {
final RefQueue<? super T> queue = new RefQueue<>();
public static void main(String[] args) {
Ref<Object> r = new Ref<>();
RefQueue<Object> q = r.queue;
}
}
RefQueue.java:
public class RefQueue<T> {
}
Then execute:
mkdir out
javac -d out Ref.java
This compiles fine and produces two class files in out directory:
Ref.class
RefQueue.class
Now do the following:
rm RefQueue.java
mkdir out2
javac -cp out -d out2 Ref.java
Which produces compile-time error:
Ref.java:6: error: incompatible types: RefQueue<CAP#1> cannot be
converted to RefQueue<Object>
RefQueue<Object> q = r.queue;
^
where CAP#1 is a fresh type-variable:
CAP#1 extends Object super: Object from capture of ? super Object
1 error
It seems that the behaviour is inconsistent. There's also a question
whether the compile-time error is a correct behaviour or not. I tried
this with JDK 7u51, JDK 8 ea-b121 and recent JDK 9 from dev forest. They
all behave the same.
Regards, Peter
More information about the compiler-dev
mailing list