Bug 561766 - [14] Switch Expression - NPE in nested switch expression
with yield

Change-Id: I5360b9a0f65b580243196c981abedfcf7d66c849
Signed-off-by: Manoj Palat <manpalat@in.ibm.com>
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SwitchExpressionsYieldTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SwitchExpressionsYieldTest.java
index 287923d..dc2ffce 100644
--- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SwitchExpressionsYieldTest.java
+++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SwitchExpressionsYieldTest.java
@@ -26,7 +26,7 @@
 	static {
 //		TESTS_NUMBERS = new int [] { 40 };
 //		TESTS_RANGE = new int[] { 1, -1 };
-//		TESTS_NAMES = new String[] { "testBug545567" };
+//		TESTS_NAMES = new String[] { "testBug561766" };
 	}
 	
 	public static Class<?> testClass() {
@@ -4762,39 +4762,6 @@
 			},
 			"3");
 	}
-	// NPE here to correct
-	public void _testBug545567_xx() {
-		this.runNegativeTest(
-				new String[] {
-					"X.java",
-					"public class X {\n"+
-					"    @SuppressWarnings({ \"finally\" })\n"+
-					"       public static void main(String[] args) {\n"+
-					"        System.out.println(switch (1) {\n"+
-					"        case 0 -> {yield 100;}\n"+
-					"           default -> {  \n"+
-					"                try {\n"+
-					"                       yield switch(0) {\n"+
-					"               }\n"+
-					"               catch (Exception ex) {\n"+
-					"                   yield 2;\n"+
-					"                }\n"+
-					"               finally {\n"+
-					"                  yield 3;\n"+
-					"                }\n"+
-					"           }  \n"+
-					"        });  \n"+
-					"    }\n"+
-					"}\n",
-				},
-				"----------\n" +
-				"1. ERROR in X.java (at line 12)\n" +
-				"	case \"hello\" -> throw new java.io.IOException(\"hello\");\n" +
-				"	     ^^^^^^^\n" +
-				"Type mismatch: cannot convert from String to int\n" +
-				"----------\n");
-
-	}
 	public void testBug561762_001() {
 		this.runNegativeTest(
 				new String[] {
@@ -4831,4 +4798,76 @@
 				"----------\n");
 
 	}
+	public void testBug561766_001() {
+		this.runNegativeTest(
+				new String[] {
+					"X.java",
+					"public class X {\n"+
+					"    @SuppressWarnings({ \"finally\" })\n"+
+					"       public static void main(String[] args) {\n"+
+					"        System.out.println(switch (1) {\n"+
+					"        case 0 -> {yield switch(0) {}\n"+
+					"        } \n"+
+					"           default -> {\n"+
+					"                  yield 3;\n"+
+					"           }\n"+
+					"        });\n"+
+					"    }\n"+
+					"}\n",
+				},
+				"----------\n" +
+				"1. ERROR in X.java (at line 5)\n" +
+				"	case 0 -> {yield switch(0) {}\n" +
+				"	                            ^\n" +
+				"Syntax error, insert \";\" to complete BlockStatements\n" +
+				"----------\n");
+
+	}
+	public void testBug561766_002() {
+		this.runNegativeTest(
+				new String[] {
+					"X.java",
+					"public class X {\n"+
+					"    @SuppressWarnings({ \"finally\" })\n"+
+					"       public static void main(String[] args) {\n"+
+					"        System.out.println(switch (1) {\n"+
+					"        case 0 -> {yield 100;}\n"+
+					"           default -> {  \n"+
+					"                try {\n"+
+					"                       yield switch(0) {\n"+
+					"               }\n"+
+					"               catch (Exception ex) {\n"+
+					"                   yield 2;\n"+
+					"                }\n"+
+					"               finally {\n"+
+					"                  yield 3;\n"+
+					"                }\n"+
+					"           }  \n"+
+					"        });  \n"+
+					"    }\n"+
+					"}\n",
+				},
+				"----------\n" +
+				"1. ERROR in X.java (at line 9)\n" +
+				"	}\n" +
+				"	^\n" +
+				"Syntax error, insert \";\" to complete YieldStatement\n" +
+				"----------\n" +
+				"2. ERROR in X.java (at line 9)\n" +
+				"	}\n" +
+				"	^\n" +
+				"Syntax error, insert \"}\" to complete Block\n" +
+				"----------\n" +
+				"3. ERROR in X.java (at line 18)\n" +
+				"	}\n" +
+				"	^\n" +
+				"Syntax error on token \"}\", delete this token\n" +
+				"----------\n" +
+				"4. ERROR in X.java (at line 19)\n" +
+				"	}\n" +
+				"	^\n" +
+				"Syntax error, insert \"}\" to complete ClassBody\n" +
+				"----------\n");
+
+	}
 }
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/SwitchExpression.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/SwitchExpression.java
index f881a2c..e53439f 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/SwitchExpression.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/SwitchExpression.java
@@ -487,6 +487,8 @@
 			} else {
 				// re-resolving of poly expression:
 				resultExpressionsCount = this.resultExpressions != null ? this.resultExpressions.size() : 0;
+				if (resultExpressionsCount == 0)
+					return this.resolvedType = null; // error flagging would have been done during the earlier phase.
 				for (int i = 0; i < resultExpressionsCount; i++) {
 					Expression resultExpr = this.resultExpressions.get(i);
 					if (resultExpr.resolvedType == null || resultExpr.resolvedType.kind() == Binding.POLY_TYPE) {