[jdk17] RFR: 8268766: Desugaring of pattern matching enum switch should be improved [v2]

Jan Lahoda jlahoda at openjdk.java.net
Fri Jun 18 09:03:30 UTC 2021


On Thu, 17 Jun 2021 21:03:58 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:

>> Jan Lahoda has updated the pull request incrementally with one additional commit since the last revision:
>> 
>>   Creating a new bootstrap method for (pattern matching) enum switches, as suggested.
>
> src/java.base/share/classes/java/lang/runtime/SwitchBootstraps.java line 173:
> 
>> 171: 
>> 172:     /**
>> 173:      * Bootstrap method for linking an {@code invokedynamic} call site that
> 
> This should be made clearer - e.g. the first argument is of type `Class` and represents the enum we want to switch on. The remaining constants should be of type `String`, the names of the various constants.

That is not quite what the labels represent. The target Enum type is inferred from the bootstrap method's invocation MethodType. (Alternatively, we can add a new Class parameter to the bootstrap method.)

For the labels, please consider this switch:

         E sel = null;
         switch (sel) {
             case A -> {}
             case E e && "B".equals(e.name()) -> {}
             case C -> {}
             case E e -> {}
         }


The patterns and the constants are mixed, and the order needs to be represented somehow in the labels array, so that the switch will classify the input correctly. The method in this proposal will use `{"A", E.class, "C", E.class}` as the labels array (which is mostly consistent with the `typeSwitch` method), but we could use different encodings if needed.

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

PR: https://git.openjdk.java.net/jdk17/pull/81


More information about the core-libs-dev mailing list