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.
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.
- duplicates
-
JDK-8332522 SwitchBootstraps::mappedEnumLookup constructs unused array
- Resolved
- relates to
-
JDK-8332528 Generate code in SwitchBootstraps.generateTypeSwitch that require fewer adaptations
- Resolved