]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
Report error on duplicate switch label
authorLuca Bruno <lethalman88@gmail.com>
Mon, 8 Mar 2010 22:36:51 +0000 (23:36 +0100)
committerJürg Billeter <j@bitron.ch>
Sun, 21 Mar 2010 22:33:41 +0000 (23:33 +0100)
Fixes bug 572556.

vala/valaswitchstatement.vala

index eb038bbf463fe5bcdd3368d4c47637642b94b9d2..00a8b8ffa182e5aca7ce415206b0d3c5eeb26193 100644 (file)
@@ -116,9 +116,24 @@ public class Vala.SwitchStatement : CodeNode, Statement {
                // ensure that possibly owned (string) expression stays alive
                expression.target_type = expression.value_type.copy ();
 
+               var labelset = new HashSet<string> (str_hash, str_equal);
                foreach (SwitchSection section in sections) {
                        section.check (analyzer);
 
+                       // 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)) {
+                                       Report.error (label.expression.source_reference, "Switch statement already contains this label");
+                               }
+                       }
                        add_error_types (section.get_error_types ());
                }