]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
Check type of constants
authorJürg Billeter <j@bitron.ch>
Fri, 10 Jul 2009 11:39:17 +0000 (12:39 +0100)
committerJürg Billeter <j@bitron.ch>
Fri, 10 Jul 2009 11:39:17 +0000 (12:39 +0100)
Fixes bug 587947.

vala/valaconstant.vala

index 81ed913fa112c7fa8d930b56f90107d040d4360d..1aa299a54bd479532f2e26032cb8927b6b1796f8 100644 (file)
@@ -170,6 +170,12 @@ public class Vala.Constant : Member, Lockable {
 
                type_reference.check (analyzer);
 
+               if (!check_const_type (type_reference, analyzer)) {
+                       error = true;
+                       Report.error (source_reference, "`%s' not supported as type for constants".printf (type_reference.to_string ()));
+                       return false;
+               }
+
                if (!external) {
                        if (initializer == null) {
                                error = true;
@@ -178,6 +184,12 @@ public class Vala.Constant : Member, Lockable {
                                initializer.target_type = type_reference;
 
                                initializer.check (analyzer);
+
+                               if (!initializer.value_type.compatible (type_reference)) {
+                                       error = true;
+                                       Report.error (source_reference, "Cannot convert from `%s' to `%s'".printf (initializer.value_type.to_string (), type_reference.to_string ()));
+                                       return false;
+                               }
                        }
                }
 
@@ -190,4 +202,17 @@ public class Vala.Constant : Member, Lockable {
 
                return !error;
        }
+
+       bool check_const_type (DataType type, SemanticAnalyzer analyzer) {
+               if (type is ValueType) {
+                       return true;
+               } else if (type is ArrayType) {
+                       var array_type = type as ArrayType;
+                       return check_const_type (array_type.element_type, analyzer);
+               } else if (type.data_type == analyzer.string_type.data_type) {
+                       return true;
+               } else {
+                       return false;
+               }
+       }
 }