Bug 570831 - [16] Pattern variables are leaked outside a block

Change-Id: Id4ac8fcf75f10699237e9c0e6a0e00230f125181
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/PatternMatching16Test.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/PatternMatching16Test.java
index 1800a00..3a59fc7 100644
--- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/PatternMatching16Test.java
+++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/PatternMatching16Test.java
@@ -3597,4 +3597,67 @@
 				true,
 				compilerOptions);
 	}
+	public void testBug570831a() {
+		Map<String, String> compilerOptions = getCompilerOptions(true);
+		runNegativeTest(
+				new String[] {
+						"X.java",
+							"public class X {\n"
+							+ "	public static void run() {\n"
+							+ "		String s = \"s\";\n"
+							+ "		Object o = null;\n"
+							+ "		{\n"
+							+ "			while (!(o instanceof String v)) {\n"
+							+ "				o = null;\n"
+							+ "			}\n"
+							+ "			s = s + v; // allowed\n"
+							+ "		}\n"
+							+ "		for (int i = 0; i < 1; i++) {\n"
+							+ "			s = s + v; // not allowed\n"
+							+ "		}\n"
+							+ "	}\n"
+							+ "}",
+				},
+				"----------\n" +
+				"1. ERROR in X.java (at line 12)\n" +
+				"	s = s + v; // not allowed\n" +
+				"	        ^\n" +
+				"v cannot be resolved to a variable\n" +
+				"----------\n",
+				null,
+				true,
+				compilerOptions);
+	}
+	public void testBug570831b() {
+		Map<String, String> compilerOptions = getCompilerOptions(true);
+		runNegativeTest(
+				new String[] {
+						"X.java",
+							"public class X {\n"
+							+ "	public static void run() {\n"
+							+ "		String s = \"s\";\n"
+							+ "		Object o = null;\n"
+							+ "		{\n"
+							+ "			int local = 0;\n"
+							+ "			while (!(o instanceof String v)) {\n"
+							+ "				o = null;\n"
+							+ "			}\n"
+							+ "			s = s + v; // allowed\n"
+							+ "		}\n"
+							+ "		for (int i = 0; i < 1; i++) {\n"
+							+ "			s = s + v; // not allowed\n"
+							+ "		}\n"
+							+ "	}\n"
+							+ "}",
+				},
+				"----------\n" +
+				"1. ERROR in X.java (at line 13)\n" +
+				"	s = s + v; // not allowed\n" +
+				"	        ^\n" +
+				"v cannot be resolved to a variable\n" +
+				"----------\n",
+				null,
+				true,
+				compilerOptions);
+	}
 }
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/parser/Parser.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/parser/Parser.java
index cf92a65..b9692c9 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/parser/Parser.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/parser/Parser.java
@@ -4628,6 +4628,9 @@
 	this.expressionLengthPtr--;
 	LocalDeclaration local = getInstanceOfVar(typeRef);
 	pushOnPatternStack(local);
+	// Only if we are not inside a block
+	if (this.realBlockPtr != -1)
+		blockReal();
 }
 protected void consumeInstanceOfExpressionWithName() {
 	// RelationalExpression_NotName ::= Name instanceof ReferenceType