Javac throws AssertionError randomly for nested pattern matching

Kiva imkiva at islovely.icu
Fri Sep 23 12:43:32 UTC 2022


Hello! I found a severe bug when pattern-matching switches are nested.

My Java version:

openjdk 19 2022-09-20
OpenJDK Runtime Environment (build 19+37)
OpenJDK 64-Bit Server VM (build 19+37, mixed mode, sharing)

The code that triggers ICE (just a ordinary unification algorithm):

public class Bug {
  sealed interface Term {
    record Mula(Formula formula) implements Term {}
    record I() implements Term {}
    record U() implements Term {}
  }
  sealed interface Formula {
    record Lit(boolean left) implements Formula {}
    record Inv(Term i) implements Formula {}
  }

  public static Term compare(Term preLhs, Term preRhs) {
    return switch (preLhs) {
      case Term.I i -> new Term.U();
      case Term.U u -> new Term.U();
      case Term.Mula lhs -> {
        if (!(preRhs instanceof Term.Mula rhs)) yield null;
        var happy = switch (lhs.formula()) {
          case Formula.Lit ll when rhs.formula() instanceof Formula.Lit rr -> ll.left() == rr.left();
          case Formula.Inv ll when rhs.formula() instanceof Formula.Inv rr ->
            compare(ll.i(), rr.i()) != null;
          default -> false;
        };
        yield happy ? new Term.I() : null;
      }
    };
  }

  public static void main(String[] args) {
    System.out.println(System.getProperty("java.version"));
  }
}


Throws this error randomly:

An exception has occurred in the compiler (19). 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:5601)
	at jdk.compiler/com.sun.tools.javac.comp.Attr.attribClass(Attr.java:5492)
	at jdk.compiler/com.sun.tools.javac.comp.Attr.attribClass(Attr.java:5316)
	at jdk.compiler/com.sun.tools.javac.comp.Attr.attrib(Attr.java:5255)
	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.api.JavacTaskImpl.lambda$doCall$0(JavacTaskImpl.java:104)
	at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.invocationHelper(JavacTaskImpl.java:152)
	at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.doCall(JavacTaskImpl.java:100)
	at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.call(JavacTaskImpl.java:94)
	at org.gradle.internal.compiler.java.IncrementalCompileTask.call(IncrementalCompileTask.java:89)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/compiler-dev/attachments/20220923/81a9dcef/attachment.htm>


More information about the compiler-dev mailing list