Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-10156

Unreachable bytecode in switch statement

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Open
    • Minor
    • Resolution: Unresolved
    • 3.0.8
    • None
    • Compiler
    • JVM: 11.0.11 (Oracle Corporation 11.0.11+9-LTS-194)
      OS: Mac OS X 10.16 x86_64

    Description

      When switching over an enum, and for two or more case branches, groovy generates unreachable bytecode for each case branch except the last one.

      For example

      package mypackage
      import groovy.transform.CompileStatic
      
      @CompileStatic
      class SwitchDemo {
        void mySwitchMethod(MyEnum myEnum) {
          switch (myEnum) {
            case MyEnum.ONE:
              println "ONE"
              break
            case MyEnum.TWO:
              println "TWO"
              break
            case MyEnum.THREE:
              println "TWO"
              break
            default:
              println "default"
              break
          }
        }
      }
      
      @CompileStatic
      enum MyEnum {
        ONE, TWO, THREE
      }
      

      generates bytecode sequences like

      ...
         L8
         L9
          LINENUMBER 12 L9
          GOTO L10
         L3
         L11
          LINENUMBER 12 L11
          ALOAD 2
          GETSTATIC mypackage/MyEnum.TWO : Lmypackage/MyEnum;
          INVOKESTATIC org/codehaus/groovy/runtime/ScriptBytecodeAdapter.isCase (Ljava/lang/Object;Ljava/lang/Object;)Z
          IFEQ L12
         L10
      ...
      

      It looks like the first four lines above are not necessary. This results in

      nop, nop, athrow
      

      sequences in generated java bytecode.

      It would be great if this can be cleaned up and fixed. My scenario is related to JaCoCo reports which are not accurate and a bit confusing for switch statements.

      Tnx

      Attachments

        Activity

          People

            Unassigned Unassigned
            dmurat Damir Murat
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated: