Why does an incomplete switch expression compile successfully












12














Trying out JDK/12 EarlyAccess Build 20, where the JEP-325 Switch Expressions has been integrated as a preview feature. A sample code for the expressions (as in the JEP as well):



Scanner scanner = new Scanner(System.in);
Day day = Day.valueOf(scanner.next().toUpperCase());
int i = switch (day) {
case MONDAY,TUESDAY, WEDNESDAY:
break 0;
default:
System.out.println("Second half of the week");
// ERROR! Group doesn't contain a break with value
};


I was trying to follow the same procedure as stated in a previous question on how to Compile a JDK12 preview feature with Maven and execute the above block of code using the command line:



java --enable-preview -jar target/jdk12-updates-1.0.0-SNAPSHOT.jar


Somewhat to my expectation I got the following error :




Error: Unable to initialize main class
com.stackoverflow.nullpointer.expression.SwitchExpressionMustComplete
Caused by: java.lang.VerifyError: Bad local variable type Exception
Details: Location:
com/stackoverflow/nullpointer/expression/SwitchExpressionMustComplete.main([Ljava/lang/String;)V @66: iload
Reason:
Type top (current frame, locals[4]) is not assignable to integer
Current Frame:
bci: @66
flags: { }
locals: { '[Ljava/lang/String;', 'java/util/Scanner', 'com/stackoverflow/nullpointer/Day' }
stack: { }
Bytecode:
0000000: bb00 0259 b200 03b7 0004 4c2b b600 05b8
0000010: 0006 4db2 0007 2cb6 0008 2eaa 0000 001f
0000020: 0000 0001 0000 0003 0000 0019 0000 0019
0000030: 0000 0019 0336 04a7 000b b200 0912 0ab6
0000040: 000b 1504 3eb1
Stackmap Table:
append_frame(@52,Object[#2],Object[#34])
same_frame(@58)
same_frame(@66)



I am aware that the document points out that the code is erroneous and replacing the comment with break 1; resolves it, but the questions I have are:



Q1. Why does the compile phase succeed for the same? Shouldn't that fail at compile time itself?



Q2. What is the cause that I see such detailed error message? Could the --enable-preview feature responsible for this?










share|improve this question



























    12














    Trying out JDK/12 EarlyAccess Build 20, where the JEP-325 Switch Expressions has been integrated as a preview feature. A sample code for the expressions (as in the JEP as well):



    Scanner scanner = new Scanner(System.in);
    Day day = Day.valueOf(scanner.next().toUpperCase());
    int i = switch (day) {
    case MONDAY,TUESDAY, WEDNESDAY:
    break 0;
    default:
    System.out.println("Second half of the week");
    // ERROR! Group doesn't contain a break with value
    };


    I was trying to follow the same procedure as stated in a previous question on how to Compile a JDK12 preview feature with Maven and execute the above block of code using the command line:



    java --enable-preview -jar target/jdk12-updates-1.0.0-SNAPSHOT.jar


    Somewhat to my expectation I got the following error :




    Error: Unable to initialize main class
    com.stackoverflow.nullpointer.expression.SwitchExpressionMustComplete
    Caused by: java.lang.VerifyError: Bad local variable type Exception
    Details: Location:
    com/stackoverflow/nullpointer/expression/SwitchExpressionMustComplete.main([Ljava/lang/String;)V @66: iload
    Reason:
    Type top (current frame, locals[4]) is not assignable to integer
    Current Frame:
    bci: @66
    flags: { }
    locals: { '[Ljava/lang/String;', 'java/util/Scanner', 'com/stackoverflow/nullpointer/Day' }
    stack: { }
    Bytecode:
    0000000: bb00 0259 b200 03b7 0004 4c2b b600 05b8
    0000010: 0006 4db2 0007 2cb6 0008 2eaa 0000 001f
    0000020: 0000 0001 0000 0003 0000 0019 0000 0019
    0000030: 0000 0019 0336 04a7 000b b200 0912 0ab6
    0000040: 000b 1504 3eb1
    Stackmap Table:
    append_frame(@52,Object[#2],Object[#34])
    same_frame(@58)
    same_frame(@66)



    I am aware that the document points out that the code is erroneous and replacing the comment with break 1; resolves it, but the questions I have are:



    Q1. Why does the compile phase succeed for the same? Shouldn't that fail at compile time itself?



    Q2. What is the cause that I see such detailed error message? Could the --enable-preview feature responsible for this?










    share|improve this question

























      12












      12








      12


      4





      Trying out JDK/12 EarlyAccess Build 20, where the JEP-325 Switch Expressions has been integrated as a preview feature. A sample code for the expressions (as in the JEP as well):



      Scanner scanner = new Scanner(System.in);
      Day day = Day.valueOf(scanner.next().toUpperCase());
      int i = switch (day) {
      case MONDAY,TUESDAY, WEDNESDAY:
      break 0;
      default:
      System.out.println("Second half of the week");
      // ERROR! Group doesn't contain a break with value
      };


      I was trying to follow the same procedure as stated in a previous question on how to Compile a JDK12 preview feature with Maven and execute the above block of code using the command line:



      java --enable-preview -jar target/jdk12-updates-1.0.0-SNAPSHOT.jar


      Somewhat to my expectation I got the following error :




      Error: Unable to initialize main class
      com.stackoverflow.nullpointer.expression.SwitchExpressionMustComplete
      Caused by: java.lang.VerifyError: Bad local variable type Exception
      Details: Location:
      com/stackoverflow/nullpointer/expression/SwitchExpressionMustComplete.main([Ljava/lang/String;)V @66: iload
      Reason:
      Type top (current frame, locals[4]) is not assignable to integer
      Current Frame:
      bci: @66
      flags: { }
      locals: { '[Ljava/lang/String;', 'java/util/Scanner', 'com/stackoverflow/nullpointer/Day' }
      stack: { }
      Bytecode:
      0000000: bb00 0259 b200 03b7 0004 4c2b b600 05b8
      0000010: 0006 4db2 0007 2cb6 0008 2eaa 0000 001f
      0000020: 0000 0001 0000 0003 0000 0019 0000 0019
      0000030: 0000 0019 0336 04a7 000b b200 0912 0ab6
      0000040: 000b 1504 3eb1
      Stackmap Table:
      append_frame(@52,Object[#2],Object[#34])
      same_frame(@58)
      same_frame(@66)



      I am aware that the document points out that the code is erroneous and replacing the comment with break 1; resolves it, but the questions I have are:



      Q1. Why does the compile phase succeed for the same? Shouldn't that fail at compile time itself?



      Q2. What is the cause that I see such detailed error message? Could the --enable-preview feature responsible for this?










      share|improve this question













      Trying out JDK/12 EarlyAccess Build 20, where the JEP-325 Switch Expressions has been integrated as a preview feature. A sample code for the expressions (as in the JEP as well):



      Scanner scanner = new Scanner(System.in);
      Day day = Day.valueOf(scanner.next().toUpperCase());
      int i = switch (day) {
      case MONDAY,TUESDAY, WEDNESDAY:
      break 0;
      default:
      System.out.println("Second half of the week");
      // ERROR! Group doesn't contain a break with value
      };


      I was trying to follow the same procedure as stated in a previous question on how to Compile a JDK12 preview feature with Maven and execute the above block of code using the command line:



      java --enable-preview -jar target/jdk12-updates-1.0.0-SNAPSHOT.jar


      Somewhat to my expectation I got the following error :




      Error: Unable to initialize main class
      com.stackoverflow.nullpointer.expression.SwitchExpressionMustComplete
      Caused by: java.lang.VerifyError: Bad local variable type Exception
      Details: Location:
      com/stackoverflow/nullpointer/expression/SwitchExpressionMustComplete.main([Ljava/lang/String;)V @66: iload
      Reason:
      Type top (current frame, locals[4]) is not assignable to integer
      Current Frame:
      bci: @66
      flags: { }
      locals: { '[Ljava/lang/String;', 'java/util/Scanner', 'com/stackoverflow/nullpointer/Day' }
      stack: { }
      Bytecode:
      0000000: bb00 0259 b200 03b7 0004 4c2b b600 05b8
      0000010: 0006 4db2 0007 2cb6 0008 2eaa 0000 001f
      0000020: 0000 0001 0000 0003 0000 0019 0000 0019
      0000030: 0000 0019 0336 04a7 000b b200 0912 0ab6
      0000040: 000b 1504 3eb1
      Stackmap Table:
      append_frame(@52,Object[#2],Object[#34])
      same_frame(@58)
      same_frame(@66)



      I am aware that the document points out that the code is erroneous and replacing the comment with break 1; resolves it, but the questions I have are:



      Q1. Why does the compile phase succeed for the same? Shouldn't that fail at compile time itself?



      Q2. What is the cause that I see such detailed error message? Could the --enable-preview feature responsible for this?







      java switch-statement javac java-12 preview-feature






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 19 '18 at 19:22









      nullpointernullpointer

      44.2k1095182




      44.2k1095182
























          1 Answer
          1






          active

          oldest

          votes


















          7














          This is a known bug. See



          https://bugs.openjdk.java.net/browse/JDK-8212982


          for details on its status.






          share|improve this answer





















          • Well, not sure how these are linked, but on the contrary the code in this bug report actually fails to compile for me with error : or -> expected .... not a statement
            – nullpointer
            Nov 20 '18 at 1:47










          • How about the other part of the question, what is the reason that I see such detailed error message? (Not that I am worried about seeing it, just curious to know if I could use some JVM flag to toggle to this view for errors reported.)
            – nullpointer
            Nov 20 '18 at 3:47













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381294%2fwhy-does-an-incomplete-switch-expression-compile-successfully%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          7














          This is a known bug. See



          https://bugs.openjdk.java.net/browse/JDK-8212982


          for details on its status.






          share|improve this answer





















          • Well, not sure how these are linked, but on the contrary the code in this bug report actually fails to compile for me with error : or -> expected .... not a statement
            – nullpointer
            Nov 20 '18 at 1:47










          • How about the other part of the question, what is the reason that I see such detailed error message? (Not that I am worried about seeing it, just curious to know if I could use some JVM flag to toggle to this view for errors reported.)
            – nullpointer
            Nov 20 '18 at 3:47


















          7














          This is a known bug. See



          https://bugs.openjdk.java.net/browse/JDK-8212982


          for details on its status.






          share|improve this answer





















          • Well, not sure how these are linked, but on the contrary the code in this bug report actually fails to compile for me with error : or -> expected .... not a statement
            – nullpointer
            Nov 20 '18 at 1:47










          • How about the other part of the question, what is the reason that I see such detailed error message? (Not that I am worried about seeing it, just curious to know if I could use some JVM flag to toggle to this view for errors reported.)
            – nullpointer
            Nov 20 '18 at 3:47
















          7












          7








          7






          This is a known bug. See



          https://bugs.openjdk.java.net/browse/JDK-8212982


          for details on its status.






          share|improve this answer












          This is a known bug. See



          https://bugs.openjdk.java.net/browse/JDK-8212982


          for details on its status.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 19 '18 at 22:02









          Brian GoetzBrian Goetz

          56.3k1399115




          56.3k1399115












          • Well, not sure how these are linked, but on the contrary the code in this bug report actually fails to compile for me with error : or -> expected .... not a statement
            – nullpointer
            Nov 20 '18 at 1:47










          • How about the other part of the question, what is the reason that I see such detailed error message? (Not that I am worried about seeing it, just curious to know if I could use some JVM flag to toggle to this view for errors reported.)
            – nullpointer
            Nov 20 '18 at 3:47




















          • Well, not sure how these are linked, but on the contrary the code in this bug report actually fails to compile for me with error : or -> expected .... not a statement
            – nullpointer
            Nov 20 '18 at 1:47










          • How about the other part of the question, what is the reason that I see such detailed error message? (Not that I am worried about seeing it, just curious to know if I could use some JVM flag to toggle to this view for errors reported.)
            – nullpointer
            Nov 20 '18 at 3:47


















          Well, not sure how these are linked, but on the contrary the code in this bug report actually fails to compile for me with error : or -> expected .... not a statement
          – nullpointer
          Nov 20 '18 at 1:47




          Well, not sure how these are linked, but on the contrary the code in this bug report actually fails to compile for me with error : or -> expected .... not a statement
          – nullpointer
          Nov 20 '18 at 1:47












          How about the other part of the question, what is the reason that I see such detailed error message? (Not that I am worried about seeing it, just curious to know if I could use some JVM flag to toggle to this view for errors reported.)
          – nullpointer
          Nov 20 '18 at 3:47






          How about the other part of the question, what is the reason that I see such detailed error message? (Not that I am worried about seeing it, just curious to know if I could use some JVM flag to toggle to this view for errors reported.)
          – nullpointer
          Nov 20 '18 at 3:47




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381294%2fwhy-does-an-incomplete-switch-expression-compile-successfully%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

          ts Property 'filter' does not exist on type '{}'

          Notepad++ export/extract a list of installed plugins