Summary
javac accepts code that is illegal per the JLS for pattern matching introduced with JEP 441. This proposed change is fixing this problem.
Problem
JEP 441 introduced pattern matching in JDK 21.
javac allowed "final" in front of a record pattern, something which is not allowed by the JLS:
record Foo ( int x) {}
public static void main ( String [] args ) {
Object o = args ;
switch ( o ) {
case final Foo( int x) -> {} // final not permitted by JLS
default -> {}
}
}
Programs that could be compiled (erroneously) in JDK21 with final
will now
fail to compile (correctly). This CSR proposes to address this issue in the compiler for both JDK21 updates and JDK22.
Solution
javac will now reject this illegal code with relevant compile-time error. javac will now correctly reject the final modifier before record patterns.
Specification
This is the relevant section of the JLS:
https://docs.oracle.com/javase/specs/jls/se21/html/jls-14.html#jls-RecordPattern
- csr of
-
JDK-8317300 javac erroneously allows "final" in front of a record pattern
-
- Resolved
-