Created
June 5, 2024 17:13
-
-
Save cboudereau/a42fb9238fd9f261955e325df1797f72 to your computer and use it in GitHub Desktop.
JDK21 record/sealed interface deconstruct pattern matching bug repro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.github.cboudereau; | |
import static org.junit.jupiter.api.Assertions.assertEquals; | |
import org.junit.jupiter.api.Test; | |
public class JdkPatternMatchingBugRepoTest { | |
static record Tuple<L, R>(L fst, R snd) { | |
} | |
static sealed interface AlgebraicDataType permits A, B{} | |
static record A () implements AlgebraicDataType{} | |
static record B () implements AlgebraicDataType{} | |
//This test is failling in jdk-21 but not in jdk-23 | |
@Test | |
void A_should_not_match_B_with_interface() { | |
final AlgebraicDataType a = new A(); | |
final var actual = switch (new Tuple<>(a, a)) { | |
case Tuple<AlgebraicDataType, AlgebraicDataType>(B(), B()) -> -1; | |
case Tuple<AlgebraicDataType, AlgebraicDataType>(A(), A()) -> 1; | |
default -> 0; | |
}; | |
assertEquals(1, actual); | |
} | |
// //This test compiles and fails in jdk-21 but not in jdk-23 | |
@Test | |
void A_should_not_match_B_with_record() { | |
final var actual = switch (new Tuple<>(new A(),new A())) { | |
case Tuple<B, B>(B(), B()) -> -1; | |
case Tuple<A, A>(A(), A()) -> 1; | |
default -> 0; | |
}; | |
assertEquals(1, actual); | |
} | |
@Test | |
void A_should_match_A(){ | |
final var actual = switch(new A()) { | |
case A() -> 1; | |
default -> 0; | |
}; | |
assertEquals(1, actual); | |
} | |
@Test | |
void B_should_not_match_A(){ | |
final AlgebraicDataType algebraicDataType = new A(); | |
final var actual = switch(algebraicDataType) { | |
case B() -> 0; | |
default -> 1; | |
}; | |
assertEquals(1, actual); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment