[code-reflection] RFR: 8337156: Lowering of switch expressions that throw on no matching [v2]

Maurizio Cimadamore mcimadamore at openjdk.org
Fri Aug 2 15:03:50 UTC 2024


On Thu, 25 Jul 2024 09:29:10 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:

>> Mourad Abbay has updated the pull request with a new target base due to a merge or a rebase. The incremental webrev excludes the unrelated changes brought in by the merge/rebase. The pull request contains two additional commits since the last revision:
>> 
>>  - Merge branch 'code-reflection' into 8337156
>>  - Update the lowering of switch expression to throw Match Exception if no label applies
>
> src/java.base/share/classes/java/lang/reflect/code/op/ExtendedOp.java line 865:
> 
>> 863:                 if (isLabelBody) {
>> 864:                     Block.Builder expression = blocks.get(i + 1);
>> 865:                     boolean isLastLabel = i == blocks.size() - 2;
> 
> This is a bit tricky. The details on when a default case is added with a `throw` are spread between `TransPatterns` and `Lower`. Basically:
> * `TransPatterns` will turn an unconditional pattern (see below) into a default label
> * `Lower` will add a `default` throwing label, only if one isn't there
> * the type of the exception is `IncompatibleClassChangeError` if selector is an enum, or `MatchException` otherwise.
> 
> An unconditional pattern is a pattern whose type is guaranteed to always match the selector expression (e.g. because the type of the pattern is a supertype of the type of the selector expression). This property should be captured by `tree.hasUnconditionalPattern`.  In fact, that flag seems to be set also when the switch has a `default` label. So just checking that flag should suffice (but please double check).

Actually... this code is about a lowering step... I think the best way would be to automatically insert a `default` label when generating the code model in `ReflectMethods`. At that point, as explained, the compiler knows which switches are "unconditional" and which are not. We could maybe reflect this piece of info in the model, but I think it's actually better to remove edge cases and just add the missing case.

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

PR Review Comment: https://git.openjdk.org/babylon/pull/195#discussion_r1701971303


More information about the babylon-dev mailing list