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

Error in javac caused by switch expression without result expressions: Internal error: stack sim error

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: In Progress
    • Priority: P4
    • Resolution: Unresolved
    • Affects Version/s: 16, 17, 18, 19
    • Fix Version/s: 19
    • Component/s: tools
    • Subcomponent:
    • CPU:
      generic
    • OS:
      generic

      Description

      A DESCRIPTION OF THE PROBLEM :
      The following java code produces a "Internal error: stack sim error on ..."
      ---
      static int test1(int i, int p){
        return 5 + switch (p){
          case 1 -> throw new RuntimeException("1");
          case 2 -> throw new RuntimeException("2");
          case 3 -> throw new RuntimeException("3");
          default -> {if(true) throw new RuntimeException(); else yield 0;}
        };
      }
      ---
      This is most related to the fact that the only "result expression" is reachable but because of the if(true) it can be stripped out.

      The following similar variants do compile however and behave as expected.
      ---
      static int test2(int i, int p){
        return switch (p){
          case 1 -> throw new RuntimeException("1");
          case 2 -> throw new RuntimeException("2");
          case 3 -> throw new RuntimeException("3");
          default -> {if(true) throw new RuntimeException(); else yield 0;}
        };
      }

      static int test3(int i, int p){
        return i < 0 ? 5 + switch (p){
          case 1 -> throw new RuntimeException("1");
          case 2 -> throw new RuntimeException("2");
          case 3 -> throw new RuntimeException("3");
          default -> {if(true) throw new RuntimeException(); else yield 0;}
        } : i;
      }

      static int test4(int i, int p){
        return switch (i){
          case 5000:
            yield 5 + switch (p){
              case 1 -> throw new RuntimeException("1");
              case 2 -> throw new RuntimeException("2");
              case 3 -> throw new RuntimeException("3");
              default -> {if(true) throw new RuntimeException(); else yield 0;}
            };
          default: yield i;
        };
      }
      ---

      Looking at the compiled bytecode 3 and 4, it does seem like the `5` is pushed to the stack, from some minor debugging it seems the stack simulation fails due to an unexpected integer on the stack


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      use the following code in a project
      ---
      static int test1(int i, int p){
        return 5 + switch (p){
          case 1 -> throw new RuntimeException("1");
          case 2 -> throw new RuntimeException("2");
          case 3 -> throw new RuntimeException("3");
          default -> {if(true) throw new RuntimeException(); else yield 0;}
        };
      }
      ---

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      It compiles without errors
      ACTUAL -
      It causes javac the throw an Internal Error

      ---------- BEGIN SOURCE ----------
      Note: the following code does NOT compile
      ---
      class Main{
        public static void main(String[] args) {
          test1(0,0);
        }

        static int test1(int i, int p){
          return 5 + switch (p){
            case 1 -> throw new RuntimeException("1");
            case 2 -> throw new RuntimeException("2");
            case 3 -> throw new RuntimeException("3");
            default -> {if(true) throw new RuntimeException(); else yield 0;}
          };
        }
      }
      ---
      ---------- END SOURCE ----------

        Attachments

        1. Pass.java
          0.9 kB
        2. Test.java
          0.3 kB

          Issue Links

            Activity

              People

              Assignee:
              gli Guoxiong Li
              Reporter:
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

                Dates

                Created:
                Updated: