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

Unreachable Code in SwitchBootstraps#enumSwitch

XMLWordPrintable

    • b25
    • 23
    • generic
    • generic

      A DESCRIPTION OF THE PROBLEM :
      As of commit 2581935 src/java.base/share/classes/java/lang/runtime/SwitchBootstraps.java line 289 reads

      ```java
      boolean constantsOnly = Stream.of(labels).allMatch(l -> enumClass.isAssignableFrom(EnumDesc.class));
      ```

      where `enumClass` has previously been verified to be an enum class (as its name suggests). Therefore `constantsOnly` is always `false`. This prevents the optimization in lines 291-303 from taking any effect.

      The optimization itself seems to contain a bug as well: it is described as

      ```java
      //if (selector == null) return -1
      //else if (idx == 0) return mappingArray[selector.ordinal()]; //mapping array created lazily
      //else return "typeSwitch(labels)"
      ```

      but implemented as

      ```java
      //if (selector == null) return -1
      //else if (idx == 0) return "typeSwitch(labels)"
      //else return mappingArray[selector.ordinal()]; //mapping array created lazily
      ```

      If it were not for the first issue, then the second issue would result in incorrect return values when the generated `MethodHandle` is called with a `restart` value other than zero.


            jlahoda Jan Lahoda
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: