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

Jan Lahoda jlahoda at openjdk.org
Mon Sep 19 18:20:34 UTC 2022


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.

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

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

Changes: https://git.openjdk.org/jdk/pull/10347/files
 Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=10347&range=00
  Issue: https://bugs.openjdk.org/browse/JDK-8292756
  Stats: 119 lines in 3 files changed: 106 ins; 0 del; 13 mod
  Patch: https://git.openjdk.org/jdk/pull/10347.diff
  Fetch: git fetch https://git.openjdk.org/jdk pull/10347/head:pull/10347

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


More information about the compiler-dev mailing list