[vala] Check type of constants



commit 3b3ffa129f449b5d238c0d32476ea8eef41a6e8d
Author: Jürg Billeter <j bitron ch>
Date:   Fri Jul 10 12:39:17 2009 +0100

    Check type of constants
    
    Fixes bug 587947.

 vala/valaconstant.vala |   25 +++++++++++++++++++++++++
 1 files changed, 25 insertions(+), 0 deletions(-)
---
diff --git a/vala/valaconstant.vala b/vala/valaconstant.vala
index 81ed913..1aa299a 100644
--- a/vala/valaconstant.vala
+++ b/vala/valaconstant.vala
@@ -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;
+		}
+	}
 }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]