disjoint or not disjoint?

Stephan Herrmann stephan.herrmann at berlin.de
Tue Oct 29 11:39:33 UTC 2024


Please consider this source:

public class X1 {
     sealed interface I permits C1 {}
     non-sealed class C1 implements I {}
     class C2 extends C1 {}
     class C3 {}
     I m2(int s, C3 c3) {
         return switch (s) {
             case 0 -> (I) c3;
             case 1 -> (C1) c3;
             case 2 -> (C2) c3;
             default -> null;
         };
     }
}

javac raises errors at case 1 and case 2 but not at case 0.

If we're reading the spec correctly, then also case 0 would be illegal:

* A class named C is disjoint from an interface named I if ...
   * C is freely extensible (§8.1.1.2), and I is sealed, and C is disjoint
     from all of the permitted direct subclasses and subinterfaces of I.

with C=C3, I=I we are asking if C3 is disjoint from C1.

* A class named C is disjoint from another class named D if (i) it is not the
   case that C <: D, and (ii) it is not the case that D <: C.

with C=C3 and D=C1 we asking C3 <: C1 and C1 <: C3, both being false.

Actually, in case 1 javac agrees with this last line of reasoning.


Do you agree that this is a bug in javac, or am I missing some subtlety?

thanks
Stephan


More information about the compiler-dev mailing list