RFR: 8303374: Implement JEP 455: Primitive Types in Patterns, instanceof, and switch (Preview) [v57]
Chen Liang
liach at openjdk.org
Thu Apr 10 18:01:57 UTC 2025
On Sun, 6 Apr 2025 10:50:50 GMT, Luca Kellermann <duke at openjdk.org> wrote:
>> Aggelos Biboudis has updated the pull request with a new target base due to a merge or a rebase. The pull request now contains 78 commits:
>>
>> - Merge branch 'master' into primitive-patterns
>> - Update summary in ExactnessConversionsSupportTest
>> - Address review by Jan
>> - Remove redundant null check and introduce a const boolean for unconditionally exact pairs
>> - Small fix in Javadoc
>> - Tidy up Javadoc of Lower.visitTypeTest
>> - Tidy up Javadoc of IllegalArgumentException in typeSwitch
>> - Improve readability in SwitchBootstraps
>> - Update year
>> - Cleanup redundant clone
>> - ... and 68 more: https://git.openjdk.org/jdk/compare/ec56c72b...f68748b1
>
> src/java.base/share/classes/java/lang/runtime/SwitchBootstraps.java line 676:
>
>> 674: }
>> 675: else if (selectorType.equals(targetType) ||
>> 676: ((selectorType.equals(byte.class) && !targetType.equals(char.class)) ||
>
> Suggestion:
>
> ((selectorType.equals(byte.class) && targetType.isPrimitve() && !targetType.equals(boolean.class) && !targetType.equals(char.class)) ||
>
> Otherwise, `SwitchBootstraps.typeSwitch` produces wrong results for `byte`:
>
> var l = MethodHandles.lookup();
> var shortType = MethodType.methodType(int.class, short.class, int.class);
> var byteType = MethodType.methodType(int.class, byte.class, int.class);
>
> CallSite shortSwitch = SwitchBootstraps.typeSwitch(l, "", shortType, String.class);
> CallSite byteSwitch = SwitchBootstraps.typeSwitch(l, "", byteType, String.class);
>
> int shortIndex = (int) shortSwitch.getTarget().invokeExact((short) 1, 0);
> int byteIndex = (int) byteSwitch.getTarget().invokeExact((byte) 1, 0);
>
> System.out.println(shortIndex == 1); // true
> System.out.println(byteIndex == 1); // false
> System.out.println(byteIndex); // 0
>
> This would cause this code to misbehave (if it would compile, which it doesn't):
>
> byte b = 1;
> switch (b) {
> case String s -> System.out.println("How did we get here? byte is " + s.getClass());
> }
>
> So it isn't really a problem with `javac` but could cause problems for other users of `SwitchBootstraps`.
A JBS issue has been created at https://bugs.openjdk.org/browse/JDK-8354323
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/15638#discussion_r2037974588
More information about the core-libs-dev
mailing list