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

Paul Sandoz psandoz at openjdk.org
Fri Aug 2 19:59:56 UTC 2024


On Fri, 2 Aug 2024 15:00:57 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:

>> 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.

Yes, that seems a better approach given the knowledge the compiler has. For switch ops created manually we anyway cannot guarantee Java program behavior, same for other operations too.

FWIW modeling of `if/else if/else` statements will always add an `else` if not explicitly present with the default behavior that yields nothing.

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

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


More information about the babylon-dev mailing list