[code-reflection] RFR: 8337156: Lowering of switch expressions that throw on no matching
Maurizio Cimadamore
mcimadamore at openjdk.org
Thu Jul 25 09:31:46 UTC 2024
On Thu, 25 Jul 2024 08:13:25 GMT, Mourad Abbay <mabbay at openjdk.org> wrote:
> 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).
-------------
PR Review Comment: https://git.openjdk.org/babylon/pull/195#discussion_r1691138738
More information about the babylon-dev
mailing list