Bug in the record pattern translation / record pattern spec

Remi Forax forax at univ-mlv.fr
Mon Jun 27 08:54:14 UTC 2022

Hi all,
i've already reported that issue but it was theoretical because the support of the record pattern was not integrated into the jdk 19 at that time and i was not able to explain int clearly.  Now that the support is available, this code should throw a StackOverflow error but it creates a linked list of MatchException which makes it hard to debug.

Wrapping all exceptions into a runtime exception is not a good idea when you have recursive calls which is a kind of natural when you have pattern matching, for me it's a spec issue but i want to be sure.

BTW, the bug here lies in the fact that deconstructing Cons(int value, int size, RecChain next) calls the accessor size() which itself calls RecChain::size which is a nice puzzler by itself.

public sealed interface RecChain {
  default int size() {
    return switch (this) {
      case Nil __ -> 0;
      case Cons(int value, int size, RecChain next) -> 1 + next.size();

  record Nil() implements RecChain { }

  record Cons(int value, int size, RecChain next) implements RecChain {
    public int size() {
      return size != -1? size: RecChain.super.size();

  public static void main(String[] args){
    RecChain chain = new Nil();
    for (var i = 0; i < 100; i++) {
      chain = new Cons(i, -1, chain);


More information about the amber-spec-observers mailing list