RFR: 8303374: Compiler Implementation for Primitive types in patterns, instanceof, and switch (Preview) [v3]

Maurizio Cimadamore mcimadamore at openjdk.org
Tue Oct 3 10:00:48 UTC 2023


On Mon, 2 Oct 2023 14:47:59 GMT, Aggelos Biboudis <abimpoudis at openjdk.org> wrote:

>> This is the first draft of a patch for Primitive types in patterns, instanceof, and switch (Preview).
>> 
>> Draft spec here: https://cr.openjdk.org/~abimpoudis/instanceof/instanceof-20230913/specs/instanceof-jls.html
>
> Aggelos Biboudis 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 four additional commits since the last revision:
> 
>  - Merge branch 'master' into primitive-patterns
>  - Implement type pairs to exactnessMethod name
>  - Apply suggestions from code review
>    
>    Co-authored-by: Raffaello Giulietti <raffaello.giulietti at oracle.com>
>  - 8303374: Compiler Implementation for Primitive types in patterns, instanceof, and switch (Preview)

src/java.base/share/classes/java/lang/runtime/SwitchBootstraps.java line 228:

> 226:                         currentTest = trueDef;
> 227:                     } else if (currentLabelClass.isPrimitive()) {
> 228:                          if (selectorType.isInstance(Object.class)) {

As discussed offline, I believe this is wrong, and it should be replaced by `Class::equals`. That said, I think that "accidentally" you get the desired behavior here, as demonstrated by jshell:


jshell> Byte.class.isInstance(Object.class)
$6 ==> false


jshell> Object.class.isInstance(Object.class)
$8 ==> true


So, this effectively acts as a test to check if the selector type is `Object`. Of course, since `isInstance` is used, spurious stuff is picked up as well:


jshell> Class.class.isInstance(Object.class)
$7 ==> true

But this situation turns out to be non problematic, given that a primitive pattern is not applicable to a selector type Class.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/15638#discussion_r1343840181


More information about the core-libs-dev mailing list