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

javac generates illegal class file for pattern matching switch with records

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 19
    • 19
    • tools
    • None
    • b26

      File: Expr.java

      sealed interface Expr
          permits Expr.Add, Expr.Sub, Expr.Mul, Expr.Div, Expr.Const {

          record Add(Expr left, Expr right) implements Expr {
              public double eval() {
                  return left.eval() + right.eval();
              }
          }

          record Sub(Expr left, Expr right) implements Expr {
              public double eval() {
                  return left.eval() - right.eval();
              }
          }

          record Mul(Expr left, Expr right) implements Expr {
              public double eval() {
                  return left.eval() * right.eval();
              }
          }

          record Div(Expr left, Expr right) implements Expr {
              public double eval() {
                  return left.eval() / right.eval();
              }
          }

          record Const(double value) implements Expr {
              public double eval() {
                  return value;
              }
          }

          double eval();

          default String str() {
               return switch(this) {
                  case Add(Expr l, Expr r) ->
                      String.format("(%s + %s)", l.str(), r.str());
                  case Sub(Expr l, Expr r) ->
                      String.format("(%s - %s)", l.str(), r.str());
                  case Mul(Expr l, Expr r) ->
                      String.format("(%s * %s)", l.str(), r.str());
                  case Div(Expr l, Expr r) ->
                      String.format("(%s / %s)", l.str(), r.str());
                  case Const(double value) ->
                      String.format("(%f)", value);
              };
          }

          public static void main(String[] a) {
              var expr = new Add(
                  new Mul(new Const(23.44), new Const(Math.PI)),
                  new Div(new Const(3.9), new Const(11.33)));
              System.out.printf("%s = %f\n", expr.str(), expr.eval());
          }
      }


      $ ./javac --enable-preview --source 19 Expr.java
      Note: Expr.java uses preview features of Java SE 19.
      Note: Recompile with -Xlint:preview for details.
      $ ./java --enable-preview Expr
      Error: LinkageError occurred while loading main class Expr
      java.lang.ClassFormatError: Method $proxy$left in class Expr has illegal modifiers: 0x1008

            jlahoda Jan Lahoda
            sundar Sundararajan Athijegannathan
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: