RFR: 8292756: java.lang.AssertionError at at jdk.compiler/com.sun.tools.javac.code.Scope$ScopeImpl.leave(Scope.java:386)

Tesla I. Zhang‮ duke at openjdk.org
Sun Sep 25 00:55:12 UTC 2022

On Mon, 19 Sep 2022 18:13:27 GMT, Jan Lahoda <jlahoda at openjdk.org> wrote:

> Consider code like:
> public class JDK_8292756 {
>     private int test(Object o) {
>         int i1, i2, i3, i4, i5, i6, i7;
>         return switch(o) {
>             case Object oo when oo != null -> 0;
>             default -> 0;
>         };
>     }
> }
> Compiling this leads to a crash:
> $ javac --enable-preview -source 19 /tmp/JDK_8292756.java 
> Note: /tmp/JDK_8292756.java uses preview features of Java SE 19.
> Note: Recompile with -Xlint:preview for details.
> An exception has occurred in the compiler (19-internal). Please file a bug against the Java compiler via the Java bug reporting page (http://bugreport.java.com) after checking the Bug Database (http://bugs.java.com) for duplicates. Include your program, the following diagnostic, and the parameters passed to the Java compiler in your report. Thank you.
> java.lang.AssertionError
>         at jdk.compiler/com.sun.tools.javac.util.Assert.error(Assert.java:155)
>         at jdk.compiler/com.sun.tools.javac.util.Assert.check(Assert.java:46)
>         at jdk.compiler/com.sun.tools.javac.code.Scope$ScopeImpl.leave(Scope.java:386)
>         at jdk.compiler/com.sun.tools.javac.comp.Attr.visitBlock(Attr.java:1465)
>         at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCBlock.accept(JCTree.java:1082)
>         at jdk.compiler/com.sun.tools.javac.comp.Attr.attribTree(Attr.java:687)
>         at jdk.compiler/com.sun.tools.javac.comp.Attr.attribStat(Attr.java:761)
>         at jdk.compiler/com.sun.tools.javac.comp.Attr.visitMethodDef(Attr.java:1253)
>         at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCMethodDecl.accept(JCTree.java:912)
>         at jdk.compiler/com.sun.tools.javac.comp.Attr.attribTree(Attr.java:687)
>         at jdk.compiler/com.sun.tools.javac.comp.Attr.attribStat(Attr.java:761)
>         at jdk.compiler/com.sun.tools.javac.comp.Attr.attribClassBody(Attr.java:5621)
>         at jdk.compiler/com.sun.tools.javac.comp.Attr.attribClass(Attr.java:5512)
>         at jdk.compiler/com.sun.tools.javac.comp.Attr.attribClass(Attr.java:5336)
>         at jdk.compiler/com.sun.tools.javac.comp.Attr.attrib(Attr.java:5275)
>         at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.attribute(JavaCompiler.java:1317)
>         at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:946)
>         at jdk.compiler/com.sun.tools.javac.main.Main.compile(Main.java:317)
>         at jdk.compiler/com.sun.tools.javac.main.Main.compile(Main.java:176)
>         at jdk.compiler/com.sun.tools.javac.Main.compile(Main.java:64)
>         at jdk.compiler/com.sun.tools.javac.Main.main(Main.java:50)
> printing javac parameters to: /tmp/javac.20220919_200140.args
> The reason for the failure is this: in `Attr.handleSwitch`, there is an input scope `env`, and a sub-scope of it, `switchEnv`, is created for the switch. Eventually, another (shared) sub-scope is created for the guard expression, `bodyEnv`. All `env`, `switchEnv` and `bodyEnv` share an internal table. But, the `bodyEnv` sub-scope is created from `env`, not from `switchEnv` (as it should). Which mostly works, but if the internal table for `bodyEnv` is resized, it will correctly fix only `env`, but not `switchEnv`.
> The solution to this problem is to simply base the `bodyEnv` on `switchEnv`, not on `env`. But, in addition to that, it does not seem right the scopes don't protect from an accident like this. So the proposed patch here is enhancing the assertions in `Scope` so that a shared duplicate of a Scope can only be done from a leaf Scope.

Any chance this can be backported to JDK 19?

Several benefits of backporting to JDK 19:

+ Build tools like Gradle wouldn't support JDK 20 until GA, so having a serious bug in JDK 19 = having a JDK 19 without record patterns at all.
+ Users won't be able to extensively test the feature without being able to use the feature. Preview features are supposed to be tested.
+ Bug-fix updates are supposed to deliver bug-fixes, and this is a bug-fix.


PR: https://git.openjdk.org/jdk/pull/10347

More information about the compiler-dev mailing list