Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
@@ -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;
}
Toggle all file notes