RFR: 8303374: Implement JEP 455: Primitive Types in Patterns, instanceof, and switch (Preview) [v57]
Luca Kellermann
duke at openjdk.org
Sun Apr 6 10:54:07 UTC 2025
On Wed, 31 Jan 2024 10:03:23 GMT, Aggelos Biboudis <abimpoudis at openjdk.org> wrote:
>> This is the proposed patch for Primitive types in patterns, instanceof, and switch (Preview).
>>
>> Draft spec here: https://cr.openjdk.org/~abimpoudis/instanceof/latest/
>
> 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`.
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/15638#discussion_r2030113548
More information about the compiler-dev
mailing list