RFR: 8348410: Preview flag not checked during compilation resulting in runtime crash
Jan Lahoda
jlahoda at openjdk.org
Fri Jan 24 18:45:51 UTC 2025
On Fri, 24 Jan 2025 17:12:00 GMT, Archie Cobbs <acobbs at openjdk.org> wrote:
> JEP 455 adds support for `switch`'ing on `long`, `float`, `double`, and `boolean`. Since this feature is still in preview, the `--enable-preview` flag is required. The compiler was properly checking that when the expressions had primitive type, but not when the expression a corresponding boxed type (i.e., `Long`, `Float`, `Double`, and `Boolean`). This resulted in `BootstrapMethodError` failures at runtime. This PR closes that loophole.
I think the check needs to be on a different place - please see inline.
src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java line 1706:
> 1704: boolean intSwitch = types.isAssignable(seltype, syms.intType);
> 1705: boolean patternSwitch;
> 1706: if (seltypeUnboxed.isPrimitive() && !intSwitch) {
Sorry, but this seems wrong. Having a selector type of type `Boolean` is not a preview feature (since JDK 21). I.e. this is completely valid:
public class Test {
public static void main(String... args) {
Boolean b = true;
System.err.println(switch (b) { case Boolean bb -> bb; });
}
}
I think there needs to be a preview check in the section that handles constant labels, probably somewhere here:
https://github.com/openjdk/jdk/blob/76f792b55263faf883e54cb879d8609f87164e51/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java#L1808
test/langtools/tools/javac/patterns/PrimitivePatternsSwitchRequirePreviewBoolean.java line 1:
> 1: /*
Having tests like this is not wrong, but instead of introducing 8 new (small) files, I would rather use the `ToolBox` and `TestRunner`, or a combo test. A `TestRunner` test looks like this:
https://github.com/openjdk/jdk/blob/76f792b55263faf883e54cb879d8609f87164e51/test/langtools/tools/javac/recovery/AttrRecovery.java
-------------
Changes requested by jlahoda (Reviewer).
PR Review: https://git.openjdk.org/jdk/pull/23303#pullrequestreview-2573352428
PR Review Comment: https://git.openjdk.org/jdk/pull/23303#discussion_r1929096503
PR Review Comment: https://git.openjdk.org/jdk/pull/23303#discussion_r1929099310
More information about the compiler-dev
mailing list