diff options
author | Manoj Palat | 2019-02-22 10:43:08 +0000 |
---|---|---|
committer | Manoj Palat | 2019-02-22 10:43:08 +0000 |
commit | feb01aa7e990efe551d7871cbf27ba9f2898ad47 (patch) | |
tree | 2aac6781cb1bec1435f319177734cbb19c0cc16a | |
parent | 41e7973f4d4b6a9095b746124a1174ca3bfcbd5a (diff) | |
download | eclipse.jdt.core-feb01aa7e990efe551d7871cbf27ba9f2898ad47.tar.gz eclipse.jdt.core-feb01aa7e990efe551d7871cbf27ba9f2898ad47.tar.xz eclipse.jdt.core-feb01aa7e990efe551d7871cbf27ba9f2898ad47.zip |
switch-labeled-rule
2 files changed, 49 insertions, 0 deletions
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 512af7f814..9dc36ff9ca 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 @@ -2130,4 +2130,28 @@ public class SwitchExpressionTest extends AbstractRegressionTest { null, new String[] {"--enable-preview"}); } + public void testBug544702_01() { + runConformTest( + new String[] { + "X.java", + "public class X {\n" + + " @SuppressWarnings(\"preview\")\n" + + " public int foo(int i) {\n" + + " int k = 10;\n" + + " switch (i) {\n" + + " case 0 -> { k = 0;}\n" + + " default -> k = -1;\n" + + " }\n" + + " return k;\n" + + " }\n" + + " public static void main(String[] argv) {\n" + + " System.out.println(new X().foo(0) == 0 ? \"Success\" : \"Failure\");\n" + + " }\n" + + "\n" + + "}\n" + }, + "Success", + null, + new String[] {"--enable-preview"}); + } }
\ No newline at end of file diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/SwitchStatement.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/SwitchStatement.java index adb1c90c0d..0f49d12bf4 100644 --- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/SwitchStatement.java +++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/SwitchStatement.java @@ -84,6 +84,31 @@ public class SwitchStatement extends Expression { private LocalVariableBinding dispatchStringCopy = null; protected int getFallThroughState(Statement stmt, BlockScope blockScope) { + if (this.switchLabeledRules) { + if (stmt instanceof Expression || stmt instanceof ThrowStatement) + return BREAKING; + + if (stmt instanceof Block) { + Block block = (Block) stmt; + if (block.doesNotCompleteNormally()) { + return BREAKING; + } + // else add an implicit break + BreakStatement breakStatement = new BreakStatement(null, block.sourceEnd -1, block.sourceEnd); + breakStatement.isImplicit = true; + + int l = block.statements == null ? 0 : block.statements.length; + if (l == 0) { + block.statements = new Statement[] {breakStatement}; + } else { + Statement[] newArray = new Statement[l + 1]; + System.arraycopy(block.statements, 0, newArray, 0, l); + newArray[l] = breakStatement; + block.statements = newArray; + } + return BREAKING; + } + } return FALLTHROUGH; } protected void completeNormallyCheck(BlockScope blockScope) { |