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.
- csr of
-
JDK-8294550 Sealed check for casts isn't applied to array components
-
- Resolved
-