Scala Programming Language
  1. Scala Programming Language
  2. SI-6956

allow tree of ConstantType as pattern in switch

    Details

      Description

      This one is trickier, as it requires the pattern matcher and the backend to agree that tableswitches can be generated for something other than literals:

      $ cat testTableSwitch.scala 
      package test.switch
      
      object Test {
        private[this] final val ONE = 1
      
        def switchBad(i: Byte): Int = i match {
          case ONE => 1
          case 2 => 2
          case 3 => 3
          case _ => 0
        }
      
        def switchOkay(i: Byte): Int = i match {
          case 1 => 1
          case 2 => 2
          case 3 => 3
          case _ => 0
        }
      }
      
      $ ../build/quick/bin/scalac testTableSwitch.scala
      
      $ javap -classpath . -c 'test.switch.Test$'
      Compiled from "testTableSwitch.scala"
      public final class test.switch.Test$ {
        public static final test.switch.Test$ MODULE$;
      
        public static {};
          Code:
             0: new           #2                  // class test/switch/Test$
             3: invokespecial #12                 // Method "<init>":()V
             6: return        
      
        public int switchBad(byte);
          Code:
             0: iload_1       
             1: istore_2      
             2: iconst_1      
             3: iload_2       
             4: if_icmpne     15
           ... comparing with each constant ...
            43: iconst_0      
            44: istore        4
            46: iload         4
            48: ireturn       
      
        public int switchOkay(byte);
          Code:
             0: iload_1       
             1: istore_2      
             2: iload_2       
             3: tableswitch   { // 1 to 3
                           1: 40
                           2: 36
                           3: 32
                     default: 28
                }
            28: iconst_0      
            29: goto          41
            32: iconst_3      
            33: goto          41
            36: iconst_2      
            37: goto          41
            40: iconst_1      
            41: ireturn       
      }
      

        Activity

        Hide
        Paul Phillips added a comment -

        What? I can't believe this doesn't work already. The constant folder is supposed to do some folding. This isn't tricky, we just have to expose pattern trees to the constant folder.

        Show
        Paul Phillips added a comment - What? I can't believe this doesn't work already. The constant folder is supposed to do some folding. This isn't tricky, we just have to expose pattern trees to the constant folder.
        Hide
        Paul Phillips added a comment -

        OH, IT'S ANOTHER REGRESSION. Moors! Life doesn't end after typer!

          public int switchBad(byte);
            flags: ACC_PUBLIC
            Code:
              stack=2, locals=3, args_size=2
                 0: iload_1       
                 1: istore_2      
                 2: iconst_1      
                 3: iload_2       
                 4: if_icmpne     11
                 7: iconst_1      
                 8: goto          45
                11: iload_2       
                12: tableswitch   { // 2 to 3
                               2: 40
                               3: 44
                         default: 36
                    }
                36: iconst_0      
                37: goto          45
                40: iconst_2      
                41: goto          45
                44: iconst_3      
                45: ireturn       
        
        Show
        Paul Phillips added a comment - OH, IT'S ANOTHER REGRESSION. Moors! Life doesn't end after typer! public int switchBad(byte); flags: ACC_PUBLIC Code: stack=2, locals=3, args_size=2 0: iload_1 1: istore_2 2: iconst_1 3: iload_2 4: if_icmpne 11 7: iconst_1 8: goto 45 11: iload_2 12: tableswitch { // 2 to 3 2: 40 3: 44 default: 36 } 36: iconst_0 37: goto 45 40: iconst_2 41: goto 45 44: iconst_3 45: ireturn
        Hide
        Adriaan Moors added a comment -

        which version of scalac produced this bytecode?

        Show
        Adriaan Moors added a comment - which version of scalac produced this bytecode?
        Hide
        Adriaan Moors added a comment -

        nevermind, 2.10.0 -Xoldpatmat will do fine

        Show
        Adriaan Moors added a comment - nevermind, 2.10.0 -Xoldpatmat will do fine
        Hide
        Vlad Ureche added a comment -

        I was using the virtualized pattern matcher and a build of 2.10.0-wip.

        Show
        Vlad Ureche added a comment - I was using the virtualized pattern matcher and a build of 2.10.0-wip.
        Hide
        Vlad Ureche added a comment -

        Thank you Adriaan and Paul!

        Show
        Vlad Ureche added a comment - Thank you Adriaan and Paul!

          People

          • Assignee:
            Adriaan Moors
            Reporter:
            Vlad Ureche
          • Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development