Skip to content

Commit 7d2442b

Browse files
Regression test for #1788 (#1790)
* Regression test for #1788
1 parent c02aecb commit 7d2442b

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/RecordPatternTest.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3803,4 +3803,58 @@ public int foo(R r) {
38033803
"Record pattern should match the signature of the record declaration\n" +
38043804
"----------\n");
38053805
}
3806+
// https://github.com/eclipse-jdt/eclipse.jdt.core/issues/1788
3807+
// Inference issue between the diamond syntax and pattern matching (switch on objects)
3808+
public void testGHI1788() {
3809+
runConformTest(
3810+
new String[] {
3811+
"X.java",
3812+
"""
3813+
public class X {
3814+
3815+
final static class Entry<K,V> {
3816+
K key;
3817+
V value;
3818+
3819+
public Entry(K key, V value) {
3820+
this.key = key;
3821+
this.value = value;
3822+
}
3823+
3824+
public K key() { return key; }
3825+
public V value() { return value; }
3826+
public void value(V value) { this.value = value; }
3827+
@Override
3828+
public String toString() {
3829+
return "Key = " + key + " Value = " + value;
3830+
}
3831+
}
3832+
3833+
sealed interface I<V> {}
3834+
record A<V>(V value) implements I<V> {}
3835+
record B<V>(V value) implements I<V> {}
3836+
3837+
private static <K, V> Entry<K,V> foo(Entry<K, I<V>> entry) {
3838+
return new Entry<>(entry.key(), switch (entry.value()) {
3839+
case A<V>(V value) -> {
3840+
entry.value(new B<>(value));
3841+
yield value;
3842+
}
3843+
case B<V>(V value) -> value;
3844+
});
3845+
}
3846+
3847+
public static void main(String[] args) {
3848+
Entry<String, I<String>> entry = new Entry<>("KEY", new A<>("VALUE"));
3849+
System.out.println(entry);
3850+
System.out.println(foo(entry));
3851+
System.out.println(entry);
3852+
}
3853+
}
3854+
"""
3855+
},
3856+
"Key = KEY Value = A[value=VALUE]\n" +
3857+
"Key = KEY Value = VALUE\n" +
3858+
"Key = KEY Value = B[value=VALUE]");
3859+
}
38063860
}

0 commit comments

Comments
 (0)