Skip to content
Permalink
Browse files
8243548: Javac incorrectly collects enum fields when verifying switch…
… expression exhaustivness

When gathering enum constants for exhaustivness analysis, make sure nested enum classes are not included

Reviewed-by: vromero
  • Loading branch information
Jan Lahoda committed May 4, 2020
1 parent 1a35219 commit 4e5542117162a029d3cf0890437f07cbe4bf5c26
Showing with 15 additions and 4 deletions.
  1. +6 −3 src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java
  2. +9 −1 test/langtools/tools/javac/switchexpr/ExhaustiveEnumSwitch.java
@@ -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 @@ public void visitSwitchExpression(JCSwitchExpression tree) {
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);
}
}
@@ -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 @@ private String print(ExhaustiveEnumSwitchEnum t) {
}
enum ExhaustiveEnumSwitchEnum {
A, B;
class NestedClass {}
enum NestedEnum {}
interface NestedInterface {}
@interface NestedAnnotation {}
void nestedMethod() {}
static void nestedStaticMethod() {}
int nestedField;
static int nestedStaticField;
}

0 comments on commit 4e55421

Please sign in to comment.