]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
Check case expressions in switch statements
authorJürg Billeter <j@bitron.ch>
Sun, 16 Aug 2009 12:20:48 +0000 (14:20 +0200)
committerJürg Billeter <j@bitron.ch>
Sun, 16 Aug 2009 12:20:48 +0000 (14:20 +0200)
Fixes bug 577052.

vala/valamemberaccess.vala
vala/valaswitchlabel.vala
vala/valaswitchsection.vala

index 3ce7399c90bda61486f45b8b95f344b916637d3c..195af93fd5d642d7b353fce47b2e39b69523d856 100644 (file)
@@ -167,7 +167,7 @@ public class Vala.MemberAccess : Expression {
        }
 
        public override bool is_constant () {
-               if (symbol_reference is Constant) {
+               if (symbol_reference is Constant || symbol_reference is EnumValue) {
                        return true;
                } else {
                        return false;
index a778ce258d8e8b8bd54d09392f4fc338d75e1e43..67fcdc25ed41de70b0531aea84ad7974b708287e 100644 (file)
@@ -1,6 +1,6 @@
 /* valaswitchlabel.vala
  *
- * Copyright (C) 2006  Jürg Billeter
+ * Copyright (C) 2006-2009  Jürg Billeter
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -31,6 +31,8 @@ public class Vala.SwitchLabel : CodeNode {
         */
        public Expression expression { get; set; }
 
+       public weak SwitchSection section { get; set; }
+
        /**
         * Creates a new switch case label.
         *
@@ -68,6 +70,18 @@ public class Vala.SwitchLabel : CodeNode {
        public override bool check (SemanticAnalyzer analyzer) {
                if (expression != null) {
                        expression.check (analyzer);
+
+                       var switch_statement = (SwitchStatement) section.parent_node;
+                       if (!expression.is_constant ()) {
+                               error = true;
+                               Report.error (expression.source_reference, "Expression must be constant");
+                               return false;
+                       }
+                       if (!expression.value_type.compatible (switch_statement.expression.value_type)) {
+                               error = true;
+                               Report.error (expression.source_reference, "Cannot convert from `%s' to `%s'".printf (expression.value_type.to_string (), switch_statement.expression.value_type.to_string ()));
+                               return false;
+                       }
                }
 
                return true;
index 27c02c2f61ffdea53ee60efc34d7da9b0bdb4959..929e0565050b768315dfcd2dc8bd83916361ff51 100644 (file)
@@ -1,6 +1,6 @@
 /* valaswitchsection.vala
  *
- * Copyright (C) 2006-2008  Jürg Billeter
+ * Copyright (C) 2006-2009  Jürg Billeter
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -46,6 +46,7 @@ public class Vala.SwitchSection : Block {
         */
        public void add_label (SwitchLabel label) {
                labels.add (label);
+               label.section = this;
        }
        
        /**