changeset 59134:b25259835ebc

8243548: Javac incorrectly collects enum fields when verifying switch expression exhaustivness Summary: When gathering enum constants for exhaustivness analysis, make sure nested enum classes are not included Reviewed-by: vromero
author jlahoda
date Mon, 04 May 2020 12:28:36 +0200
parents f08a56c38dde
children 6013cec835ac
files src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java test/langtools/tools/javac/switchexpr/ExhaustiveEnumSwitch.java
diffstat 2 files changed, 15 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java	Mon May 04 12:28:35 2020 +0200
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java	Mon May 04 12:28:36 2020 +0200
@@ -30,7 +30,6 @@
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Set;
-import java.util.stream.Collectors;
 
 import com.sun.source.tree.LambdaExpressionTree.BodyKind;
 import com.sun.tools.javac.code.*;
@@ -44,6 +43,7 @@
 import com.sun.tools.javac.util.JCDiagnostic.Error;
 import com.sun.tools.javac.util.JCDiagnostic.Warning;
 
+import com.sun.tools.javac.code.Kinds.Kind;
 import com.sun.tools.javac.code.Symbol.*;
 import com.sun.tools.javac.tree.JCTree.*;
 
@@ -697,9 +697,12 @@
             pendingExits = new ListBuffer<>();
             scan(tree.selector);
             Set<Object> constants = null;
-            if ((tree.selector.type.tsym.flags() & ENUM) != 0) {
+            TypeSymbol selectorSym = tree.selector.type.tsym;
+            if ((selectorSym.flags() & ENUM) != 0) {
                 constants = new HashSet<>();
-                for (Symbol s : tree.selector.type.tsym.members().getSymbols(s -> (s.flags() & ENUM) != 0)) {
+                Filter<Symbol> enumConstantFilter =
+                        s -> (s.flags() & ENUM) != 0 && s.kind == Kind.VAR;
+                for (Symbol s : selectorSym.members().getSymbols(enumConstantFilter)) {
                     constants.add(s.name);
                 }
             }
--- a/test/langtools/tools/javac/switchexpr/ExhaustiveEnumSwitch.java	Mon May 04 12:28:35 2020 +0200
+++ b/test/langtools/tools/javac/switchexpr/ExhaustiveEnumSwitch.java	Mon May 04 12:28:36 2020 +0200
@@ -23,7 +23,7 @@
 
 /*
  * @test
- * @bug 8206986
+ * @bug 8206986 8243548
  * @summary Verify that an switch expression over enum can be exhaustive without default.
  * @compile ExhaustiveEnumSwitch.java
  * @compile ExhaustiveEnumSwitchExtra.java
@@ -56,4 +56,12 @@
 }
 enum ExhaustiveEnumSwitchEnum {
     A, B;
+    class NestedClass {}
+    enum NestedEnum {}
+    interface NestedInterface {}
+    @interface NestedAnnotation {}
+    void nestedMethod() {}
+    static void nestedStaticMethod() {}
+    int nestedField;
+    static int nestedStaticField;
 }