Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8343318

javac is failing to determine if a class and a sealed interface are disjoint

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P4 P4
    • 24
    • tools
    • None
    • source
    • minimal
    • some user programs that were accepted before could be rejected by javac after this change. It could be that those programs were failing at execution time anyways so we don't expect a great impact on users
    • Language construct
    • SE

      Summary

      The javac compiler is accepting invalid programs which according to the JLS 23 should be rejected. The javac compiler is not in sync with section §5.1.6.1 of the JSL 23. In particular the javac compiler is not able to detect in some cases that a freely extensible class is disjoint from a sealed interface.

      Problem

      The javac compiler is not in sync with section §5.1.6.1 of the JLS 23. This implies that code like:

      class Test {
          sealed interface I permits C1 {}
          non-sealed class C1 implements I {}
          class C3 {}
          I m(int s, C3 c3) {
              I i = (I)c3;
          }
      }

      is accepted by the javac compiler even when according to the JLS 23 class C3 and interface I are disjoint as C3 is a freely extensible class and it is disjoint from all the permitted subclasses of interface I.

      Solution

      The proposed solution is to sync the javac compiler with section §5.1.6.1 of the JSL 23. In particular the algorithm to determine if two types are disjoint should be improved to correctly determine if a freely extensible class is disjoint or not from a sealed interface.

      Specification

      Of note for this CSR are section §5.1.6 Narrowing Reference Conversion and subsection §5.1.6.1 Allowed Narrowing Reference Conversion of the JLS 23. When determining if a narrowing reference conversion exist from a type S to a type T, see §5.1.6 there are cases for which it should be determined if two types are disjoint or not. The rules for disjointess are defined in subsection §5.1.6.1. We are in particular interested in this text of §5.1.6.1:

      • A class named C is disjoint from an interface named I if (i) it is not the case
        that C <: I , and (ii) one of the following cases applies:
          – C is final .
          – C is sealed , and all of the permitted direct subclasses of C are disjoint
            from I .
          – 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.
      
      ...
      
      • 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 .

            vromero Vicente Arturo Romero Zaldivar
            vromero Vicente Arturo Romero Zaldivar
            Jan Lahoda
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: