]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
Check if the constants used as switch labels are duplicated
authorLuca Bruno <lucabru@src.gnome.org>
Mon, 15 Aug 2011 19:03:12 +0000 (21:03 +0200)
committerLuca Bruno <lucabru@src.gnome.org>
Mon, 15 Aug 2011 19:07:33 +0000 (21:07 +0200)
Fixes bug 656481.

vala/valaswitchstatement.vala

index 12ad04b89e5c652d820a0a1b2dd83afea1029815..c583f8db9d6b9f3cc8364f721c011f6bb189112d 100644 (file)
@@ -123,18 +123,21 @@ public class Vala.SwitchStatement : CodeNode, Statement {
                        section.check (context);
 
                        // check for duplicate literal case labels
-                       // FIXME: make it work for all constant expressions
                        foreach (SwitchLabel label in section.get_labels ()) {
-                               string? value = null;
-                               if (label.expression is StringLiteral) {
-                                       value = ((StringLiteral)label.expression).eval ();
-                               } else if (label.expression is Literal) {
-                                       value = ((Literal)label.expression).to_string ();
-                               }
-
-                               if (value != null && !labelset.add (value)) {
-                                       error = true;
-                                       Report.error (label.expression.source_reference, "Switch statement already contains this label");
+                               if (label.expression != null) {
+                                       string? value = null;
+                                       if (label.expression is StringLiteral) {
+                                               value = ((StringLiteral)label.expression).eval ();
+                                       } else if (label.expression is Literal) {
+                                               value = ((Literal)label.expression).to_string ();
+                                       } else if (label.expression.is_constant ()) {
+                                               value = label.expression.to_string ();
+                                       }
+
+                                       if (value != null && !labelset.add (value)) {
+                                               error = true;
+                                               Report.error (label.expression.source_reference, "Switch statement already contains this label");
+                                       }
                                }
                        }
                        add_error_types (section.get_error_types ());