Preview flag required at runtime is not checked at compile time
Archie Cobbs
archie.cobbs at gmail.com
Thu Jan 23 15:47:38 UTC 2025
Hi Stephan,
Thanks for the report. Created as JDK-8348410
<https://bugs.openjdk.org/browse/JDK-8348410>.
-Archie
On Thu, Jan 23, 2025 at 4:04 AM Stephan Herrmann <stephan.herrmann at berlin.de>
wrote:
> Using javac versions 23 and 24-ea it is possible to compile the following
> program without passing `--enable-preview`:
>
> public class Test {
>
> public static void main(String[] args) {
> new Test().d(true);
> }
>
> void d(Boolean b) {
> switch (b) {
> case true -> System.out.println("1");
> case false -> System.out.println("2");
> };
> }
> }
>
> When trying to run this program the following exception is thrown:
>
> Exception in thread "main" java.lang.BootstrapMethodError: bootstrap
> method
> initialization exception
> at
>
> java.base/java.lang.invoke.BootstrapMethodInvoker.invoke(BootstrapMethodInvoker.java:187)
> at java.base/java.lang.invoke.CallSite.makeSite(CallSite.java:316)
> at
>
> java.base/java.lang.invoke.MethodHandleNatives.linkCallSiteImpl(MethodHandleNatives.java:275)
> at
>
> java.base/java.lang.invoke.MethodHandleNatives.linkCallSite(MethodHandleNatives.java:265)
> at Test.d(Test.java:8)
> at Test.main(Test.java:4)
> Caused by: java.lang.IllegalArgumentException: label with illegal type
> found:
> class java.lang.Boolean
> at
>
> java.base/java.lang.runtime.SwitchBootstraps.verifyLabel(SwitchBootstraps.java:214)
> at
>
> java.base/java.lang.runtime.SwitchBootstraps.typeSwitch(SwitchBootstraps.java:188)
> at
>
> java.base/java.lang.invoke.BootstrapMethodInvoker.invoke(BootstrapMethodInvoker.java:143)
> ... 5 more
>
> Only when running with --enable-preview the program executes as expected.
>
> This confirms that the compiler should have checked the preview flag, so
> that
> execution without --enable-preview would signal the following, rather than
> the
> BootstrapMethodError:
>
> Error: LinkageError occurred while loading main class Test
> java.lang.UnsupportedClassVersionError: Preview features are not
> enabled for Test (class file version 67.65535). Try running with
> '--enable-preview'
>
>
> The same observation also holds for other pairs of types like Long + long.
>
> best,
> Stephan
>
--
Archie L. Cobbs
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/compiler-dev/attachments/20250123/704f2106/attachment.htm>
More information about the compiler-dev
mailing list