diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/TypePattern.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/TypePattern.java index 59c03cbe21c..f6c1feaada0 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/TypePattern.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/TypePattern.java @@ -47,8 +47,7 @@ public TypeReference getType() { } @Override public LocalVariableBinding[] bindingsWhenTrue() { - return (this.local.binding == null || this.local.isUnnamed(this.local.binding.declaringScope)) ? - NO_VARIABLES : new LocalVariableBinding[] { this.local.binding }; + return this.local.binding == null ? NO_VARIABLES : new LocalVariableBinding[] { this.local.binding }; } @Override public boolean checkUnsafeCast(Scope scope, TypeBinding castType, TypeBinding expressionType, TypeBinding match, boolean isNarrowing) { diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/UnnamedPatternsAndVariablesTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/UnnamedPatternsAndVariablesTest.java index 638c8ecb777..3831c31887e 100644 --- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/UnnamedPatternsAndVariablesTest.java +++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/UnnamedPatternsAndVariablesTest.java @@ -691,4 +691,67 @@ public void test005() { ---------- """); } + + // https://github.com/eclipse-jdt/eclipse.jdt.core/issues/2024 + // [Patterns][Unnamed] VerifyError with unnamed pattern variable in instanceof + public void testIssue2024() { + runConformTest(new String[] { + "X.java", + """ + record A(){} + record R(T t) {} + public class X { + private static boolean foo(R r) { + return r instanceof R(var _); + } + public static void main(String argv[]) { + System.out.println(foo(new R(new A())) ? "Pass" : "Fail"); + } + } + """ + }, + "Pass"); + } + // https://github.com/eclipse-jdt/eclipse.jdt.core/issues/2024 + // [Patterns][Unnamed] VerifyError with unnamed pattern variable in instanceof + public void testIssue2024_2() { + runConformTest(new String[] { + "X.java", + """ + record A(){} + record R(T t) {} + public class X { + private static boolean foo(R r) { + return r instanceof R(var _); + } + public static void main(String argv[]) { + System.out.println(foo(new R(new A())) ? "Pass" : "Fail"); + } + } + """ + }, + "Pass"); + } + // https://github.com/eclipse-jdt/eclipse.jdt.core/issues/2024 + // [Patterns][Unnamed] VerifyError with unnamed pattern variable in instanceof + public void testIssue2024_3() { + runConformTest(new String[] { + "X.java", + """ + interface I {} + record A() implements I {} + record R(T t) {} + + public class X { + private static boolean foo(R r) { + return r instanceof R(var _); + } + public static void main(String argv[]) { + System.out.println(foo(new R(new A())) ? "Pass" : "Fail"); + } + } + """ + }, + "Pass"); + } }