Incompilable instanceof with super-bound

Tagir Valeev amaembo at gmail.com
Thu Mar 16 10:01:06 UTC 2023


Hello!

We've stumbled with the following code sample:

class Test {
    public static boolean isSub(Super<? super Box<?>> next) {
        return next instanceof Sub<?>;
    }

    static class Super<T> {}

    static class Sub<T> extends Super<Box<T>> { }

    static class Box<T> {}

    public static void main(String[] args) {
System.out.println(isSub(new Sub<>()));
}
}

javac 21-ea+12-971 reports an error here:

Test.java:3: error: incompatible types: Super<CAP#1> cannot be converted to
Sub<?>
        return next instanceof Sub<?>;
               ^
  where CAP#1 is a fresh type-variable:
    CAP#1 extends Object super: Box<?> from capture of ? super Box<?>
1 error

On the other hand, Eclipse compiler compiles this correctly. From my
understanding of the specification, this code should be accepted. Could you
please take a look?

Thank you in advance,
Tagir Valeev.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/compiler-dev/attachments/20230316/9bc9048f/attachment.htm>


More information about the compiler-dev mailing list