Bug 531714 - switch expression - re-enabling the enable-preview flag
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SwitchExpressionTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SwitchExpressionTest.java
index 9a1aee4..4421c29 100644
--- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SwitchExpressionTest.java
+++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SwitchExpressionTest.java
@@ -45,6 +45,7 @@
 		defaultOptions.put(CompilerOptions.OPTION_Compliance, CompilerOptions.VERSION_12); // FIXME
 		defaultOptions.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_12);
 		defaultOptions.put(CompilerOptions.OPTION_TargetPlatform, CompilerOptions.VERSION_12);
+		defaultOptions.put(CompilerOptions.OPTION_EnablePreviews, CompilerOptions.ENABLED);
 		return defaultOptions;
 	}
 	
@@ -257,6 +258,102 @@
 			},
 			"100");
 	}
+	public void testBug531714_008() {
+		Map<String, String> disablePreviewOptions = getCompilerOptions();
+		disablePreviewOptions.put(CompilerOptions.OPTION_EnablePreviews, CompilerOptions.DISABLED);
+		String[] testFiles = new String[] {
+				"X.java",
+				"public class X {\n" +
+				"	static int twice(int i) {\n" +
+				"		int tw = switch (i) {\n" +
+				"			case 0 -> i * 0;\n" +
+				"			case 1 -> 2;\n" +
+				"			default -> 3;\n" +
+				"		};\n" +
+				"		return tw;\n" +
+				"	}\n" +
+				"	public static void main(String[] args) {\n" +
+				"		System.out.print(twice(3));\n" +
+				"	}\n" +
+				"}\n",
+		};
 
-}
-	
\ No newline at end of file
+		String expectedProblemLog =
+				"----------\n" + 
+				"1. ERROR in X.java (at line 3)\n" + 
+				"	int tw = switch (i) {\n" + 
+				"			case 0 -> i * 0;\n" + 
+				"			case 1 -> 2;\n" + 
+				"			default -> 3;\n" + 
+				"		};\n" + 
+				"	         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + 
+				"Switch expression is a preview feature; use --enable-preview at source level 12 or above to enable\n" + 
+				"----------\n" + 
+				"2. ERROR in X.java (at line 4)\n" + 
+				"	case 0 -> i * 0;\n" + 
+				"	^^^^^^\n" + 
+				"Switch Case Label with \'->\' is a preview feature; use --enable-preview at source level 12 or above to enable\n" + 
+				"----------\n" + 
+				"3. ERROR in X.java (at line 5)\n" + 
+				"	case 1 -> 2;\n" + 
+				"	^^^^^^\n" + 
+				"Switch Case Label with \'->\' is a preview feature; use --enable-preview at source level 12 or above to enable\n" + 
+				"----------\n" + 
+				"4. ERROR in X.java (at line 6)\n" + 
+				"	default -> 3;\n" + 
+				"	^^^^^^^\n" + 
+				"Switch Case Label with \'->\' is a preview feature; use --enable-preview at source level 12 or above to enable\n" + 
+				"----------\n";
+
+		this.runNegativeTest(
+				testFiles,
+				expectedProblemLog,
+				null,
+				true,
+				disablePreviewOptions);
+	}
+	public void testBug531714_009() {
+		Map<String, String> disablePreviewOptions = getCompilerOptions();
+		disablePreviewOptions.put(CompilerOptions.OPTION_EnablePreviews, CompilerOptions.DISABLED);
+		String[] testFiles = new String[] {
+				"X.java",
+				"public class X {\n" +
+				"	static int twice(int i) {\n" +
+				"		switch (i) {\n" +
+				"			case 0 -> i * 0;\n" +
+				"			case 1 -> 2;\n" +
+				"			default -> 3;\n" +
+				"		}\n" +
+				"		return 0;\n" +
+				"	}\n" +
+				"	public static void main(String[] args) {\n" +
+				"		System.out.print(twice(3));\n" +
+				"	}\n" +
+				"}\n",
+		};
+
+		String expectedProblemLog =
+				"----------\n" + 
+				"1. ERROR in X.java (at line 4)\n" + 
+				"	case 0 -> i * 0;\n" + 
+				"	^^^^^^\n" + 
+				"Switch Case Label with \'->\' is a preview feature; use --enable-preview at source level 12 or above to enable\n" + 
+				"----------\n" + 
+				"2. ERROR in X.java (at line 5)\n" + 
+				"	case 1 -> 2;\n" + 
+				"	^^^^^^\n" + 
+				"Switch Case Label with \'->\' is a preview feature; use --enable-preview at source level 12 or above to enable\n" + 
+				"----------\n" + 
+				"3. ERROR in X.java (at line 6)\n" + 
+				"	default -> 3;\n" + 
+				"	^^^^^^^\n" + 
+				"Switch Case Label with \'->\' is a preview feature; use --enable-preview at source level 12 or above to enable\n" + 
+				"----------\n";
+		this.runNegativeTest(
+				testFiles,
+				expectedProblemLog,
+				null,
+				true,
+				disablePreviewOptions);
+	}
+}
\ No newline at end of file
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/core/compiler/IProblem.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/core/compiler/IProblem.java
index 74d784f..2facfe9 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/core/compiler/IProblem.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/core/compiler/IProblem.java
@@ -2099,12 +2099,16 @@
 	/** @since 3.16 BETA_JAVA_12 */
 	int SwitchExpressionTrailingSwitchLabels = Internal + 1605;
 	/** @since 3.16 BETA_JAVA_12 */
-	int MixedCase = Syntax + 1606;
+	int switchMixedCase = Syntax + 1606;
 	/** @since 3.16 BETA_JAVA_12 */
 	int SwitchExpressionMissingDefaultCase = Internal + 1607;
 	/** @since 3.16 BETA_JAVA_12 */
     int SwitchExpressionNotBelow12 = Internal + Syntax + 1608;
 	/** @since 3.16 BETA_JAVA_12 */
     int SwitchCaseLabelWithArrowNotBelow12 = Internal + Syntax + 1609;
+	/** @since 3.16 BETA_JAVA_12 */
+    int SwitchExpressionPreviewDisabled = Internal + Syntax + 1610;
+	/** @since 3.16 BETA_JAVA_12 */
+    int SwitchCaseLabelWithArrowPreviewDisabled = Internal + Syntax + 1611;
 
 	}
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 52cdb5f..ef860fe 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
@@ -9493,6 +9493,8 @@
 	CaseStatement caseStatement = (CaseStatement) this.astStack[this.astPtr];
 	if (!this.parsingJava12Plus) {
 		problemReporter().caseStatementWithArrowNotBelow12(caseStatement);
+	} else if (!this.options.enablePreviewFeatures){
+		problemReporter().caseStatementWithArrowIsPreview(caseStatement);		
 	}
 	caseStatement.isExpr = true;
 }
@@ -9502,6 +9504,8 @@
 	CaseStatement defaultStatement = (CaseStatement) this.astStack[this.astPtr];
 	if (!this.parsingJava12Plus) {
 		problemReporter().caseStatementWithArrowNotBelow12(defaultStatement);
+	} else if (!this.options.enablePreviewFeatures){
+		problemReporter().caseStatementWithArrowIsPreview(defaultStatement);		
 	}
 	defaultStatement.isExpr = true;
 }
@@ -9513,6 +9517,8 @@
 
 		if (!this.parsingJava12Plus) {
 			problemReporter().switchExpressionsNotBelow12(s);
+		} else if (!this.options.enablePreviewFeatures) {
+			problemReporter().switchExpressionIsPreview(s);
 		}
 
 		pushOnExpressionStack(s);
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java
index cff7a7c..291bde5 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java
@@ -11072,7 +11072,7 @@
 }
 public void switchExpressionMixedCase(ASTNode statement) {
 	this.handle(
-		IProblem.MixedCase,
+		IProblem.switchMixedCase,
 		NoArgument,
 		NoArgument,
 		statement.sourceStart,
@@ -11080,11 +11080,11 @@
 }
 public void switchExpressionsNotBelow12(SwitchExpression switchExpr) {
 	this.handle(
-			IProblem.SwitchExpressionNotBelow12,
-			NoArgument,
-			NoArgument,
-			switchExpr.sourceStart,
-			switchExpr.sourceEnd);
+		IProblem.SwitchExpressionNotBelow12,
+		NoArgument,
+		NoArgument,
+		switchExpr.sourceStart,
+		switchExpr.sourceEnd);
 }
 public void caseStatementWithArrowNotBelow12(CaseStatement caseStatement) {
 	this.handle(
@@ -11094,4 +11094,20 @@
 		caseStatement.sourceStart,
 		caseStatement.sourceEnd);
 }
+public void switchExpressionIsPreview(SwitchExpression switchExpr) {
+	this.handle(
+		IProblem.SwitchExpressionPreviewDisabled,
+		NoArgument,
+		NoArgument,
+		switchExpr.sourceStart,
+		switchExpr.sourceEnd);
+}
+public void caseStatementWithArrowIsPreview(CaseStatement caseStatement) {
+	this.handle(
+		IProblem.SwitchCaseLabelWithArrowPreviewDisabled,
+		NoArgument,
+		NoArgument,
+		caseStatement.sourceStart,
+		caseStatement.sourceEnd);
+}
 }
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/problem/messages.properties b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/problem/messages.properties
index 8f04e6e..c0c262f 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/problem/messages.properties
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/problem/messages.properties
@@ -983,6 +983,10 @@
 1607 = A switch expression should have a default case
 1608 = Switch expressions are allowed only at source level 12 or above
 1609 = Switch Case Labels with '->' are allowed only at source level 12 or above
+1608 = Switch expressions are allowed only at source level 12 or above
+1609 = Switch Case Labels with '->' are allowed only at source level 12 or above
+1610 = Switch expression is a preview feature; use --enable-preview at source level 12 or above to enable
+1611 = Switch Case Label with '->' is a preview feature; use --enable-preview at source level 12 or above to enable
 
 
 ### ELABORATIONS