RFR: 8291769: Translation of switch with record patterns could be improved [v6]

Jan Lahoda jlahoda at openjdk.org
Fri Sep 30 12:33:31 UTC 2022


On Wed, 28 Sep 2022 03:00:28 GMT, Vicente Romero <vromero at openjdk.org> wrote:

>> Jan Lahoda has updated the pull request with a new target base due to a merge or a rebase. The pull request now contains eight commits:
>> 
>>  - Merge branch 'master' into JDK-8291769
>>  - Merge branch 'master' into JDK-8291769
>>  - Reflecting review feedback.
>>  - Merge branch 'master' into JDK-8291769
>>  - Using a custom record instead of a generic Pair.
>>  - Fixing test.
>>  - Properly handle null record components.
>>  - 8291769: Translation of switch with record patterns could be improved
>
> src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TransPatterns.java line 819:
> 
>> 817:                 }
>> 818:             } else if (currentBinding != null &&
>> 819:                        commonBinding.type.tsym == currentBinding.type.tsym &&
> 
> question, what about having a map relating commonBindings and accumulators, shouldn't this allow you to group common patterns even if they are not consecutive?

This would mean we would change the order of the cases, which is something I'd like to avoid in this patch, for several reasons. Partly because there's a fairly big set of cases where it is unclear if we can do the reordering (and the exact rules when we can do reordering are debatable, and sometimes depend on very particular aspects of the spec), and partly because this patch seems complex enough without reordering.

One possibility we could do case reordering as a separate step before coalescing the prefixes, as simple use of map would not necessarily allow to avoid coalescing in cases like:

switch (o) {
     case I1 i when <condition1> -> {}
     case I2 i when <condition2> -> {}
     case I1 i when <condition3> -> {}
}

(where `I1` and `I2` are unrelated interfaces, we cannot easily change the order of the cases here, as `o` may be an object that implements both, and `<condition1>` might be `false`).

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

PR: https://git.openjdk.org/jdk/pull/9746


More information about the compiler-dev mailing list