diff options
author | Manoj Palat | 2021-09-01 00:46:30 +0000 |
---|---|---|
committer | Manoj Palat | 2021-09-01 05:45:52 +0000 |
commit | 506337e7be50a805903da19bf48bdf725cbc7669 (patch) | |
tree | c41a59f576bffbcf0ade3b2a41f7a96ee72b9532 | |
parent | 45fcafaf0b01a088c8a6d26e21a4f4b45c74fb51 (diff) | |
download | eclipse.jdt.core-506337e7be50a805903da19bf48bdf725cbc7669.tar.gz eclipse.jdt.core-506337e7be50a805903da19bf48bdf725cbc7669.tar.xz eclipse.jdt.core-506337e7be50a805903da19bf48bdf725cbc7669.zip |
Bug 575714 - [17][switch pattern] An exhaustive switch with throw gives
error
Change-Id: I22f9e628d51727cc41e9fe068e9ef464dbb347b7
Signed-off-by: Manoj Palat <manpalat@in.ibm.com>
Reviewed-on: https://git.eclipse.org/r/c/jdt/eclipse.jdt.core/+/184826
Tested-by: JDT Bot <jdt-bot@eclipse.org>
2 files changed, 84 insertions, 1 deletions
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SwitchPatternTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SwitchPatternTest.java index fd5ce24176..7c6a60353a 100644 --- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SwitchPatternTest.java +++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SwitchPatternTest.java @@ -3760,4 +3760,87 @@ public class SwitchPatternTest extends AbstractRegressionTest9 { "Illegal fall-through to a pattern\n" + "----------\n"); } + public void testBug575714_01() { + runNegativeTest( + new String[] { + "X.java", + "class X {\n"+ + " static Object foo(Object o) {\n"+ + " switch (o) {\n"+ + " case Object __ -> throw new AssertionError(); \n"+ + " }\n"+ + " }\n"+ + " public static void main(String[] args) {\n"+ + " Zork();\n"+ + " }\n"+ + "}", + }, + "----------\n" + + "1. ERROR in X.java (at line 8)\n" + + " Zork();\n" + + " ^^^^\n" + + "The method Zork() is undefined for the type X\n" + + "----------\n"); + } + public void testBug575714_02() { + runNegativeTest( + new String[] { + "X.java", + "class X {\n"+ + " static Object foo(Object o) {\n"+ + " switch (o) {\n"+ + " case Object __ -> System.out.println(\"Hello\"); \n"+ + " }\n"+ + " }\n"+ + " public static void main(String[] args) {\n"+ + " X.foo(new X());\n"+ + " }\n"+ + "}", + }, + "----------\n" + + "1. ERROR in X.java (at line 2)\n" + + " static Object foo(Object o) {\n" + + " ^^^^^^^^^^^^^\n" + + "This method must return a result of type Object\n" + + "----------\n"); + } + public void testBug575714_03() { + runConformTest( + new String[] { + "X.java", + "class X {\n"+ + " static Object foo(Object o) {\n"+ + " switch (o) {\n"+ + " case Object __ -> System.out.println(\"Hello\"); \n"+ + " }\n"+ + " return null;\n"+ + " }\n"+ + " public static void main(String[] args) {\n"+ + " X.foo(new X());\n"+ + " }\n"+ + "}", + }, + "Hello"); + } + public void testBug575714_04() { + runConformTest( + new String[] { + "X.java", + "class X {\n"+ + " static Object foo(Object o) throws Exception {\n"+ + " switch (o) {\n"+ + " case Object __ -> throw new Exception(); \n"+ + " }\n"+ + " }\n"+ + " public static void main(String[] args) {\n"+ + " try {\n"+ + " X.foo(new X());\n"+ + " } catch (Exception e) {\n"+ + " System.out.println(\"Hello\");\n"+ + " }\n"+ + " }\n"+ + "}", + }, + "Hello"); + } } 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 272bd2970a..50ba379578 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 @@ -146,7 +146,7 @@ public class SwitchStatement extends Expression { // do nothing } protected boolean needToCheckFlowInAbsenceOfDefaultBranch() { - return true; + return !this.isExhaustive(); } @Override public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, FlowInfo flowInfo) { |