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

Using 'var' keyword switch pattern matching causes compiler error

XMLWordPrintable

    • 23
    • master
    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      ~ $ uname -a
      Linux [redacted] 4.18.0-513.24.1.el8_9.x86_64 #1 SMP Thu Mar 14 14:20:09 EDT 2024 x86_64 x86_64 x86_64 GNU/Linux
      ~ $ rpm -q glibc
      glibc-2.28-236.el8_9.13.x86_64
      ~ $ getconf GNU_LIBPTHREAD_VERSION
      NPTL 2.28

      A DESCRIPTION OF THE PROBLEM :
      javac can no longer successfully compile the attached source code when upgrading from version 22.0.2 to 23. Needless to say, I could not find any release notes that indicates that this is expected behavior.

      REGRESSION : Last worked in version 22.0.2

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      In a terminal, do
      $ [path-to-jdk]/bin/javac Main.java
      >error: cannot select a static class from a parameterized type
      >1 error
      Here "Main.java" refers to the file containing the source code pasted in the source-code section below


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Compiled Java 23 class-files on the working directory
      ACTUAL -
      An error message from the javac command:
      >error: cannot select a static class from a parameterized type
      >1 error
      And no class-files present

      ---------- BEGIN SOURCE ----------
      public class Main {
          public static void main(String[] args) {
              var result = switch (get()) {
                  case MyInterface.MyBoolean(var key, var value) -> key.toString() + value.toString();
                  case MyInterface.MyString(var key, var value) -> key.toString() + value.toLowerCase();
              };
              System.out.println("result = " + result);
          }

          public sealed interface MyInterface<K extends MyInterface.KeyType, T> {
              K key();
              T value();

              record MyString(KeyType.A key, String value) implements MyInterface<KeyType.A, String> {
              }
              record MyBoolean(KeyType.B key, Boolean value) implements MyInterface<KeyType.B, Boolean> {
              }

              sealed interface KeyType {
                  enum A implements KeyType {A1, A2}
                  enum B implements KeyType {B1, B2}
              }

          }
          private static MyInterface<?, ?> get() {
              return null;
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      The problem can be avoided by replacing the 'var'-keyword with an explicit type. Note that this is only necessary for the KeyType-variables. The String and Boolean parameter works fine with 'var'

      FREQUENCY : always

            jlahoda Jan Lahoda
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: