Bug 569922 - Pattern matching instanceof regression

Change-Id: I728d0f9c300ac6781e0dbf60edb2fc7f43330809
Signed-off-by: Jay Arthanareeswaran <jarthana@in.ibm.com>
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/PatternMatching15Test.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/PatternMatching15Test.java
index c07000b..aa742bc 100644
--- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/PatternMatching15Test.java
+++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/PatternMatching15Test.java
@@ -3281,10 +3281,9 @@
 								"public class X {\n"
 								+ "   static void foo(Object o) {\n"
 								+ "		if (!(o instanceof X x) || x != null || x!= null) {\n"
-								+ "     	System.out.println(x);\n // not allowed"
+								+ "     	System.out.println(x); // not allowed\n"
 								+ "		}\n"
 								+ "	  }\n"
-								+ "	}\n"
 								+ "	public static void main(String[] args) {\n"
 								+ "		foo(new X());\n"
 								+ "	}\n"
@@ -3292,7 +3291,7 @@
 				},
 				"----------\n" +
 				"1. ERROR in X.java (at line 5)\n" +
-				"	System.out.println(x);\n" +
+				"	System.out.println(x); // not allowed\n" +
 				"	                   ^\n" +
 				"x cannot be resolved to a variable\n" +
 				"----------\n",
@@ -3300,4 +3299,50 @@
 				true,
 				compilerOptions);
 	}
+	public void test075() {
+		Map<String, String> compilerOptions = getCompilerOptions(true);
+		runConformTest(
+				new String[] {
+						"X.java",
+						"@SuppressWarnings(\"preview\")\n"+
+								"public class X {\n"
+								+ " public boolean isMyError(Exception e) {\n"
+								+ "        return e instanceof MyError my && (my.getMessage().contains(\"something\") || my.getMessage().contains(\"somethingelse\"));\n"
+								+ " }\n"
+								+ "	public static void main(String[] args) {\n"
+								+ "		System.out.println(\"hello\");\n"
+								+ "	}\n"
+								+ "}\n"
+								+ "class MyError extends Exception {}\n",
+				},
+				"hello",
+				compilerOptions);
+	}
+	public void test076() {
+		Map<String, String> compilerOptions = getCompilerOptions(true);
+		runNegativeTest(
+				new String[] {
+						"X.java",
+						"@SuppressWarnings(\"preview\")\n"
+						+ "public class X {\n"
+						+ "   static void foo(Object o) {\n"
+						+ "	   if ( (! (o instanceof String a)) || (o instanceof String a) ) {\n"
+						+ "		   // Nothing\n"
+						+ "	   }\n"
+						+ "	  }\n"
+						+ "	public static void main(String[] args) {\n"
+						+ "		System.out.println(\"hello\");\n"
+						+ "	}\n"
+						+ "}\n",
+				},
+				"----------\n" +
+				"1. ERROR in X.java (at line 4)\n" +
+				"	if ( (! (o instanceof String a)) || (o instanceof String a) ) {\n" +
+				"	                                                         ^\n" +
+				"Duplicate local variable a\n" +
+				"----------\n",
+				null,
+				true,
+				compilerOptions);
+	}
 }
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/OR_OR_Expression.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/OR_OR_Expression.java
index aeaab4f..935ab1b 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/OR_OR_Expression.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/OR_OR_Expression.java
@@ -279,13 +279,22 @@
 	}
 	@Override
 	public void collectPatternVariablesToScope(LocalVariableBinding[] variables, BlockScope scope) {
+		LocalVariableBinding[] temp = variables;
 		this.left.collectPatternVariablesToScope(variables, scope);
 
 		// Just keep the ones in false scope
 		variables = this.left.getPatternVariablesWhenFalse();
 		this.addPatternVariablesWhenFalse(variables);
 
-		this.right.collectPatternVariablesToScope(variables, scope);
+		int length = (variables == null ? 0 : variables.length) + (temp == null ? 0 : temp.length);
+		LocalVariableBinding[] newArray = new LocalVariableBinding[length];
+		if (variables != null) {
+			System.arraycopy(variables, 0, newArray, 0, variables.length);
+		}
+		if (temp != null) {
+			System.arraycopy(temp, 0, newArray, (variables == null ? 0 : variables.length), temp.length);
+		}
+		this.right.collectPatternVariablesToScope(newArray, scope);
 		variables = this.right.getPatternVariablesWhenFalse();
 		this.addPatternVariablesWhenFalse(variables);