8050021: Improper "duplicate case label" error
authorsadayapalam
Thu, 12 Mar 2015 10:24:07 +0530
changeset 29430 0fc887a8c519
parent 29429 964498d4dcec
child 29431 94880c5f1226
8050021: Improper "duplicate case label" error Reviewed-by: jlahoda
langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java
langtools/test/tools/javac/enum/EnumSwitch2.java
langtools/test/tools/javac/enum/EnumSwitch2.out
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java	Wed Mar 11 23:53:38 2015 -0700
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java	Thu Mar 12 10:24:07 2015 +0530
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -1211,10 +1211,8 @@
                             if (pattype.constValue() == null) {
                                 log.error(c.pat.pos(),
                                           (stringSwitch ? "string.const.req" : "const.expr.req"));
-                            } else if (labels.contains(pattype.constValue())) {
+                            } else if (!labels.add(pattype.constValue())) {
                                 log.error(c.pos(), "duplicate.case.label");
-                            } else {
-                                labels.add(pattype.constValue());
                             }
                         }
                     }
@@ -1251,19 +1249,17 @@
     // where
     /** Return the selected enumeration constant symbol, or null. */
     private Symbol enumConstant(JCTree tree, Type enumType) {
-        if (!tree.hasTag(IDENT)) {
-            log.error(tree.pos(), "enum.label.must.be.unqualified.enum");
-            return syms.errSymbol;
-        }
-        JCIdent ident = (JCIdent)tree;
-        Name name = ident.name;
-        for (Symbol sym : enumType.tsym.members().getSymbolsByName(name)) {
-            if (sym.kind == VAR) {
-                Symbol s = ident.sym = sym;
-                ((VarSymbol)s).getConstValue(); // ensure initializer is evaluated
-                ident.type = s.type;
-                return ((s.flags_field & Flags.ENUM) == 0)
-                    ? null : s;
+        if (tree.hasTag(IDENT)) {
+            JCIdent ident = (JCIdent)tree;
+            Name name = ident.name;
+            for (Symbol sym : enumType.tsym.members().getSymbolsByName(name)) {
+                if (sym.kind == VAR) {
+                    Symbol s = ident.sym = sym;
+                    ((VarSymbol)s).getConstValue(); // ensure initializer is evaluated
+                    ident.type = s.type;
+                    return ((s.flags_field & Flags.ENUM) == 0)
+                        ? null : s;
+                }
             }
         }
         return null;
--- a/langtools/test/tools/javac/enum/EnumSwitch2.java	Wed Mar 11 23:53:38 2015 -0700
+++ b/langtools/test/tools/javac/enum/EnumSwitch2.java	Thu Mar 12 10:24:07 2015 +0530
@@ -1,6 +1,6 @@
 /*
  * @test /nodynamiccopyright/
- * @bug 4936393
+ * @bug 4936393 8050021
  * @summary enum switch case labels required to be unqualified.
  * @author gafter
  * @compile/fail/ref=EnumSwitch2.out -XDrawDiagnostics EnumSwitch2.java
--- a/langtools/test/tools/javac/enum/EnumSwitch2.out	Wed Mar 11 23:53:38 2015 -0700
+++ b/langtools/test/tools/javac/enum/EnumSwitch2.out	Thu Mar 12 10:24:07 2015 +0530
@@ -1,4 +1,3 @@
 EnumSwitch2.java:14:16: compiler.err.enum.label.must.be.unqualified.enum
 EnumSwitch2.java:15:16: compiler.err.enum.label.must.be.unqualified.enum
-EnumSwitch2.java:15:9: compiler.err.duplicate.case.label
-3 errors
+2 errors