RFR: 8300543 Compiler Implementation for Pattern Matching for switch [v2]

Vicente Romero vromero at openjdk.org
Thu Apr 20 14:09:51 UTC 2023


On Thu, 20 Apr 2023 13:41:05 GMT, Jan Lahoda <jlahoda at openjdk.org> wrote:

>> src/java.base/share/classes/java/lang/runtime/SwitchBootstraps.java line 279:
>> 
>>> 277:     }
>>> 278: 
>>> 279:     private static int lazyDoEnumSwitch(Enum<?> target, int startIndex, Object[] labels, MethodHandles.Lookup lookup, Class<?> enumClass, MutableCallSite callSite) throws Throwable {
>> 
>> out of curiosity, under what conditions the `startIndex` would be different from `0`? also shouldn't we check that `startIndex` is `>= 0`?
>
> `startIndex` may be non-0 when there are guards in the switch. In the case of the `enumSwitch`, something like:
> 
> enum E {A, B;}
> E e = E.B;
> switch (e) {
>      case E ee when ee == E.A -> {}
>      case E ee -> {}
> }
> 
> 
> the method will be called twice, one with `startIndex == 0` and once with `startIndex == 1`, after the guard fails/returns `false`.

I see, thanks

>> src/java.base/share/classes/java/lang/runtime/SwitchBootstraps.java line 279:
>> 
>>> 277:     }
>>> 278: 
>>> 279:     private static int lazyDoEnumSwitch(Enum<?> target, int startIndex, Object[] labels, MethodHandles.Lookup lookup, Class<?> enumClass, MutableCallSite callSite) throws Throwable {
>> 
>> can `doEnumSwitch` be folded into `lazyDoEnumSwitch`? just a suggestion, I'm OK with either way just that now it is not clear that we need two methods here. Also in `doEnumSwitch` and out of curiosity what example of user code could hit this section:
>> 
>> 
>>             if (label instanceof Class<?> c) {
>>                 if (c.isAssignableFrom(targetClass))
>>                     return i;
>>             }
>> 
>> EDIT: nvm I found one example
>
> The intent here is to avoid eager initialization of enums - so, until the enum is initialized, `lazyDoEnumSwitch` will be used (which, by itself, won't compute any results, except the result for `null`), which is then replaced with `doEnumSwitch` using the enum constants directly.
> 
> The enum switches support patterns as well, so something like:
> 
> enum E {A;}
> E e = ...;
> switch (e) {
>      case E ee -> {}
> }
> 
> is valid, and should trigger the part with Class labels.

yep I found a similar test case triggering this code

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/13074#discussion_r1172646957
PR Review Comment: https://git.openjdk.org/jdk/pull/13074#discussion_r1172647746


More information about the compiler-dev mailing list