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

Compiler fails on switch expression with switch expression in branch

    XMLWordPrintable

Details

    • generic
    • generic

    Description

      ADDITIONAL SYSTEM INFORMATION :
      Ubuntu 20.04, Java 17.0.1 (openjdk)

      A DESCRIPTION OF THE PROBLEM :
      When trying to compile a Java file with a nested switch expression (see the reproduce section), the Java compiler fails with a NullPointerException:
      ```
      An exception has occurred in the compiler (17.0.1). Please file a bug against the Java compiler via the Java bug reporting page (http://bugreport.java.com) after checking the Bug Database (http://bugs.java.com) for duplicates. Include your program, the following diagnostic, and the parameters passed to the Java compiler in your report. Thank you.
      java.lang.NullPointerException: Cannot invoke "com.sun.tools.javac.code.Symbol.isEnum()" because "expr.sym" is null
      ```

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Invoke javac TestFile.java (the content of which is pasted in source code below)

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      No exceptions, a compiled Java file.
      ACTUAL -
      A Nullpointer exception:
      An exception has occurred in the compiler (17.0.1). Please file a bug against the Java compiler via the Java bug reporting page (http://bugreport.java.com) after checking the Bug Database (http://bugs.java.com) for duplicates. Include your program, the following diagnostic, and the parameters passed to the Java compiler in your report. Thank you.
      java.lang.NullPointerException: Cannot invoke "com.sun.tools.javac.code.Symbol.isEnum()" because "expr.sym" is null

      ---------- BEGIN SOURCE ----------
      public class EnumSwitchTestCase {
        enum Type {
          FIRST
        }

        enum Subtype {
          A,
          B
        }

        enum ResultType {
          ONE,
          TWO
        }

        interface Method {
          Type type();
        }

        record FirstMethod(Subtype subtype) implements Method {
          @Override
          public Type type() {
            return Type.FIRST;
          }
        }

        record Something(Method method) {}

        record Entity(ResultType resultType) {}

        public static void main(final String[] args) {
          final var something = new Something(new FirstMethod(Subtype.A));
          final Entity entity =
              new Entity(
                  switch (something.method().type()) {
                    case FIRST -> switch (((FirstMethod) something.method()).subtype()) {
                      case A -> ResultType.ONE;
                      case B -> ResultType.TWO;
                    };
                  });
          System.out.println(entity);
        }
      }
      ---------- END SOURCE ----------

      FREQUENCY : always


      Attachments

        Issue Links

          Activity

            People

              adev ANUPAM DEV
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: