RFR: 8348410: Preview flag not checked during compilation resulting in runtime crash

Archie Cobbs acobbs at openjdk.org
Fri Jan 24 23:05:51 UTC 2025


On Fri, 24 Jan 2025 18:40:20 GMT, Jan Lahoda <jlahoda 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.
>
> 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

Ah! My apologies, I totally misunderstood the cause of this issue.

Since this is more complicated than I thought I will defer back to Angelos who is the expert.

Thanks. Closing this PR.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/23303#discussion_r1929342090


More information about the compiler-dev mailing list