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

Having multiple conditions in a guarded pattern crashes the compiler

XMLWordPrintable

    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      Windows 10 and EA Build 33 (2022/1/27)

      A DESCRIPTION OF THE PROBLEM :
      Trying to compile the following code crashes the compiler:

      public class X4 {
          public Object literal = "a";
          @SuppressWarnings("preview")
      public boolean foo() {
              String s = switch(literal) {
                  case ((CharSequence a && a instanceof String ss && ss == null) && ss != null) -> {
                      yield "a";
                  }
                  case default -> { yield "b"; }
              };
              return s.equals("a");
          }
      }

      The stack trace printed is:

      java.lang.AssertionError
              at jdk.compiler/com.sun.tools.javac.util.Assert.error(Assert.java:155)
              at jdk.compiler/com.sun.tools.javac.util.Assert.check(Assert.java:46)
              at jdk.compiler/com.sun.tools.javac.util.Bits.isMember(Bits.java:231)
              at jdk.compiler/com.sun.tools.javac.comp.Flow$AssignAnalyzer.checkInit(Flow.java:1957)
              at jdk.compiler/com.sun.tools.javac.comp.Flow$AssignAnalyzer.checkInit(Flow.java:1951)
              at jdk.compiler/com.sun.tools.javac.comp.Flow$AssignAnalyzer.visitIdent(Flow.java:2841)
              at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCIdent.accept(JCTree.java:2584)
              at jdk.compiler/com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:49)
              at jdk.compiler/com.sun.tools.javac.comp.Flow$BaseAnalyzer.scan(Flow.java:444)
              at jdk.compiler/com.sun.tools.javac.comp.Flow$AssignAnalyzer.scan(Flow.java:1743)
              at jdk.compiler/com.sun.tools.javac.comp.Flow$AssignAnalyzer.scanExpr(Flow.java:2000)
              at jdk.compiler/com.sun.tools.javac.comp.Flow$AssignAnalyzer.visitBinary(Flow.java:2834)
              at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCBinary.accept(JCTree.java:2156)
              at jdk.compiler/com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:49)
              at jdk.compiler/com.sun.tools.javac.comp.Flow$BaseAnalyzer.scan(Flow.java:444)
              at jdk.compiler/com.sun.tools.javac.comp.Flow$AssignAnalyzer.scan(Flow.java:1743)
              at jdk.compiler/com.sun.tools.javac.tree.TreeScanner.visitGuardPattern(TreeScanner.java:322)
              at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCGuardPattern.accept(JCTree.java:2385)
              at jdk.compiler/com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:49)
              at jdk.compiler/com.sun.tools.javac.comp.Flow$BaseAnalyzer.scan(Flow.java:444)
              at jdk.compiler/com.sun.tools.javac.comp.Flow$AssignAnalyzer.scan(Flow.java:1743)
              at jdk.compiler/com.sun.tools.javac.tree.TreeScanner.visitParenthesizedPattern(TreeScanner.java:316)
              at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCParenthesizedPattern.accept(JCTree.java:2343)
              at jdk.compiler/com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:49)
              at jdk.compiler/com.sun.tools.javac.comp.Flow$BaseAnalyzer.scan(Flow.java:444)
              at jdk.compiler/com.sun.tools.javac.comp.Flow$AssignAnalyzer.scan(Flow.java:1743)
              at jdk.compiler/com.sun.tools.javac.comp.Flow$AssignAnalyzer.handleSwitch(Flow.java:2455)
              at jdk.compiler/com.sun.tools.javac.comp.Flow$AssignAnalyzer.visitSwitchExpression(Flow.java:2439)

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Just get the latest EA build and compile the given code with Javac.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The code should be compiled with no error.
      ACTUAL -
       But the compiler exits with an exception:

      java.lang.AssertionError
              at jdk.compiler/com.sun.tools.javac.util.Assert.error(Assert.java:155)
              at jdk.compiler/com.sun.tools.javac.util.Assert.check(Assert.java:46)
              at jdk.compiler/com.sun.tools.javac.util.Bits.isMember(Bits.java:231)
              at jdk.compiler/com.sun.tools.javac.comp.Flow$AssignAnalyzer.checkInit(Flow.java:1957)
              at jdk.compiler/com.sun.tools.javac.comp.Flow$AssignAnalyzer.checkInit(Flow.java:1951)
              at jdk.compiler/com.sun.tools.javac.comp.Flow$AssignAnalyzer.visitIdent(Flow.java:2841)
              at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCIdent.accept(JCTree.java:2584)
              at jdk.compiler/com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:49)
              at jdk.compiler/com.sun.tools.javac.comp.Flow$BaseAnalyzer.scan(Flow.java:444)
              at jdk.compiler/com.sun.tools.javac.comp.Flow$AssignAnalyzer.scan(Flow.java:1743)
              at jdk.compiler/com.sun.tools.javac.comp.Flow$AssignAnalyzer.scanExpr(Flow.java:2000)
              at jdk.compiler/com.sun.tools.javac.comp.Flow$AssignAnalyzer.visitBinary(Flow.java:2834)
              at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCBinary.accept(JCTree.java:2156)
              at jdk.compiler/com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:49)
              at jdk.compiler/com.sun.tools.javac.comp.Flow$BaseAnalyzer.scan(Flow.java:444)
              at jdk.compiler/com.sun.tools.javac.comp.Flow$AssignAnalyzer.scan(Flow.java:1743)
              at jdk.compiler/com.sun.tools.javac.tree.TreeScanner.visitGuardPattern(TreeScanner.java:322)
              at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCGuardPattern.accept(JCTree.java:2385)
              at jdk.compiler/com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:49)
              at jdk.compiler/com.sun.tools.javac.comp.Flow$BaseAnalyzer.scan(Flow.java:444)
              at jdk.compiler/com.sun.tools.javac.comp.Flow$AssignAnalyzer.scan(Flow.java:1743)
              at jdk.compiler/com.sun.tools.javac.tree.TreeScanner.visitParenthesizedPattern(TreeScanner.java:316)
              at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCParenthesizedPattern.accept(JCTree.java:2343)
              at jdk.compiler/com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:49)
              at jdk.compiler/com.sun.tools.javac.comp.Flow$BaseAnalyzer.scan(Flow.java:444)
              at jdk.compiler/com.sun.tools.javac.comp.Flow$AssignAnalyzer.scan(Flow.java:1743)
              at jdk.compiler/com.sun.tools.javac.comp.Flow$AssignAnalyzer.handleSwitch(Flow.java:2455)
              at jdk.compiler/com.sun.tools.javac.comp.Flow$AssignAnalyzer.visitSwitchExpression(Flow.java:2439)

      ---------- BEGIN SOURCE ----------
      public class X4 {
          public Object literal = "a";
          @SuppressWarnings("preview")
      public boolean foo() {
              String s = switch(literal) {
                  case ((CharSequence a && a instanceof String ss && ss == null) && ss != null) -> {
                      yield "a";
                  }
                  case default -> { yield "b"; }
              };
              return s.equals("a");
          }
      }
      ---------- END SOURCE ----------

      FREQUENCY : always


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

              Created:
              Updated:
              Resolved: