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

Incorrect `switch` `case` taken

    XMLWordPrintable

Details

    • generic
    • generic

    Description

      ADDITIONAL SYSTEM INFORMATION :
      Windows 11 with latest updates

      C:\>javac --version
      javac 21.0.1

      C:\>java --version
      openjdk 21.0.1 2023-10-17 LTS
      OpenJDK Runtime Environment Microsoft-8526870 (build 21.0.1+12-LTS)
      OpenJDK 64-Bit Server VM Microsoft-8526870 (build 21.0.1+12-LTS, mixed mode, sharing)


      A DESCRIPTION OF THE PROBLEM :
      Pattern-matching executes a `case` different from the expected one.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      javac --enable-preview -source 21 Test.java
      java --enable-preview Test


      ---------- BEGIN SOURCE ----------
      abstract class FuLiteral {}
      class FuLiteralNull extends FuLiteral {}
      class FuLiteralFalse extends FuLiteral {}
      class FuLiteralTrue extends FuLiteral {}

      public class Test
      {
      public static void main()
      {
      final FuLiteral left = new FuLiteralFalse();
      final FuLiteral right = new FuLiteralTrue();
      switch (left) {
      case FuLiteralNull _ when right instanceof FuLiteralNull:
      case FuLiteralFalse _ when right instanceof FuLiteralFalse:
      case FuLiteralTrue _ when right instanceof FuLiteralTrue:
      System.out.println("BAD");
      break;
      case FuLiteralFalse _ when right instanceof FuLiteralTrue: // <-- expected
      case FuLiteralTrue _ when right instanceof FuLiteralFalse:
      System.out.println("OK");
      break;
      default:
      break;
      }
      }
      }

      ---------- END SOURCE ----------

      FREQUENCY : always


      Attachments

        Issue Links

          Activity

            People

              abimpoudis Angelos Bimpoudis
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: