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

Sealed check for casts isn't applied to array components

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P4 P4
    • 20
    • tools
    • None
    • source
    • minimal
    • Source compatibility only and only in the case of sealed type and arrays combined.
    • Language construct
    • Implementation

      Summary

      javac currently accepts casts that contradict the JLS when sealed classes and arrays are involved.

      Problem

      Consider this code:

      sealed interface I permits A {}
      final class A implements I {}
      interface J {}
      
      J[] jj = null;
      I[] i = (I[]) jj;

      The cast should cause a compile-time error, as there is no narrowing conversion (JLS 5.1.6.1) from J[] to I[], as:

      • there is no narrowing conversion from J to I, as the two types are disjoint due to the sealed interface
      • for array types JLS 5.1.6.1 says:

      S is an array type SC[], that is, an array of components of type SC; T is an array type TC[], that is, an array of components of type TC; and a narrowing reference conversion exists from SC to TC.

      Solution

      javac will correctly produce compile-time error when an array type is cast to another array type, and the component types of the array types are disjoint as per JLS 5.1.6.1.

      Specification

      No change in specification. javac behavior will change to adhere to the JLS, regardless of selected source level.

            jlahoda Jan Lahoda
            dlsmith Dan Smith
            Vicente Arturo Romero Zaldivar
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: