Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorManoj Palat2019-01-23 02:19:53 +0000
committerManoj Palat2019-01-23 03:28:44 +0000
commit93c279fe25a5c7ae85ed69dd9a39f76dc0efe5ef (patch)
tree8ed13ec43641c0d47ecab527e3c338b09fc9a7c7
parent2d9a1920a4701654a45ab826a6104eedac961369 (diff)
downloadeclipse.jdt.core-93c279fe25a5c7ae85ed69dd9a39f76dc0efe5ef.tar.gz
eclipse.jdt.core-93c279fe25a5c7ae85ed69dd9a39f76dc0efe5ef.tar.xz
eclipse.jdt.core-93c279fe25a5c7ae85ed69dd9a39f76dc0efe5ef.zip
Bug 543673 - [12] NPE on using var with switch expressions
-rw-r--r--org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SwitchExpressionTest.java28
-rw-r--r--org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/LocalDeclaration.java1
-rw-r--r--org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/SwitchExpression.java2
3 files changed, 30 insertions, 1 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 3e9cbf8f3c..143f7c1757 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
@@ -501,4 +501,32 @@ public class SwitchExpressionTest extends AbstractRegressionTest {
options.put(CompilerOptions.OPTION_Source, release);
}
}
+ public void testBug543673_001() {
+ runConformTest(
+ new String[] {
+ "X.java",
+ "public class X {\n" +
+ " public static int foo(Day day) {\n" +
+ "\n" +
+ " var len= switch (day) {\n" +
+ " case SUNDAY-> 6;\n" +
+ " default -> 10;\n" +
+ " };\n" +
+ "\n" +
+ " return len;\n" +
+ " }\n" +
+ "\n" +
+ " public static void main(String[] args) {\n" +
+ " System.out.println(foo(Day.SUNDAY));\n" +
+ " }\n" +
+ "}\n" +
+ "enum Day {\n" +
+ " MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;\n" +
+ "}\n"
+ },
+ "6",
+ null,
+ false,
+ new String[] { "--enable-preview"});
+ }
} \ No newline at end of file
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/LocalDeclaration.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/LocalDeclaration.java
index 50217b82fb..b67c1a4af6 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/LocalDeclaration.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/LocalDeclaration.java
@@ -251,6 +251,7 @@ public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, Fl
}
if (e instanceof SwitchExpression) {
SwitchExpression se = (SwitchExpression)e;
+ se.collectResultExpressions();
for (Expression re : se.resultExpressions) {
Expression candidate = findPolyExpression(re);
if (candidate != null) return candidate;
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 7e9e71c8db..7153c3e849 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
@@ -203,7 +203,7 @@ public class SwitchExpression extends SwitchStatement implements IPolyExpression
}
return ok;
}
- private void collectResultExpressions() {
+ /* package */ void collectResultExpressions() {
if (this.resultExpressions != null)
return; // already calculated.

Back to the top