[vala] Check case expressions in switch statements



commit 8eb8af894ad2334bd665f903bdff4111933de200
Author: Jürg Billeter <j bitron ch>
Date:   Sun Aug 16 14:20:48 2009 +0200

    Check case expressions in switch statements
    
    Fixes bug 577052.

 vala/valamemberaccess.vala  |    2 +-
 vala/valaswitchlabel.vala   |   16 +++++++++++++++-
 vala/valaswitchsection.vala |    3 ++-
 3 files changed, 18 insertions(+), 3 deletions(-)
---
diff --git a/vala/valamemberaccess.vala b/vala/valamemberaccess.vala
index 3ce7399..195af93 100644
--- a/vala/valamemberaccess.vala
+++ b/vala/valamemberaccess.vala
@@ -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;
diff --git a/vala/valaswitchlabel.vala b/vala/valaswitchlabel.vala
index a778ce2..67fcdc2 100644
--- a/vala/valaswitchlabel.vala
+++ b/vala/valaswitchlabel.vala
@@ -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;
diff --git a/vala/valaswitchsection.vala b/vala/valaswitchsection.vala
index 27c02c2..929e056 100644
--- a/vala/valaswitchsection.vala
+++ b/vala/valaswitchsection.vala
@@ -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;
 	}
 	
 	/**



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