Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8296790

compiler raises AssertionError in switch with record patterns

    XMLWordPrintable

Details

    • generic
    • generic

    Description

      A DESCRIPTION OF THE PROBLEM :
      The compiler raises AssertionError when compiling the provided code. We use a sealed interface to model a Binary Search Tree with two parameter types, K and V.
      The implementation of the insert method uses nested switches with both record patterns and guards.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      javac --enable-preview --release 19 BST.java

      ACTUAL -
      Note: BST.java uses preview features of Java SE 19.
      Note: Recompile with -Xlint:preview for details.
      An exception has occurred in the compiler (19.0.1). 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.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: /Users/pacog/Documents/Docencia/ED/RecordError/src/javac.20221107_175039.args



      $ cat javac.20221107_175039.args
      --enable-preview
      --release
      19
      BST.java
       

      ---------- BEGIN SOURCE ----------
      sealed interface BST<K extends Comparable<? super K>, V> {

          record EmptyBST<K extends Comparable<? super K>, V>() implements BST<K, V> {
          }

          record NodeBST<K extends Comparable<? super K>, V>(K key, V value,
                                                             BST<K, V> left, BST<K, V> right) implements BST<K, V> {
          }

          static <K extends Comparable<? super K>, V> BST<K, V> empty() {
              return new EmptyBST<>();
          }

          default BST<K, V> insert(K k, V v) {
              return switch (this) {
                  case EmptyBST<K, V>() -> new NodeBST<>(k, v, empty(), empty());
                  case NodeBST<K, V>(K key,V value,BST<K, V> left,BST<K, V> right) ->
                          switch (Integer.valueOf(k.compareTo(key))) {
                              case Integer i when i < 0 -> new NodeBST<>(key, value, left.insert(k, v), right);
                              case Integer i when i > 0 -> new NodeBST<>(key, value, left, right.insert(k, v));
                              default -> this;
                          };
              };
          }
      }

      ---------- END SOURCE ----------

      FREQUENCY : always


      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: