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

javac erroneously allows "final" in front of a record pattern

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P4 P4
    • 21-pool, 22
    • tools
    • None
    • source
    • low
    • Since record patterns are a new feature, it is unlikely that a significant amount of code will use it. Only a small portion of code that uses record patterns could be affected by this adjustment.
    • Language construct
    • Implementation

      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

            abimpoudis Angelos Bimpoudis
            abimpoudis Angelos Bimpoudis
            Jan Lahoda
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: