[vala] Do not split conditional expressions in asserts



commit 4e818c1d8dc1a7683b220ed5cb0ebad69cd02ad0
Author: Jürg Billeter <j bitron ch>
Date:   Sun Oct 18 14:41:17 2009 +0200

    Do not split conditional expressions in asserts
    
    Fixes bug 577619.

 vala/valabinaryexpression.vala |   17 ++++++++++++++++-
 vala/valamethodcall.vala       |    6 ++++++
 vapi/glib-2.0.vapi             |    1 +
 3 files changed, 23 insertions(+), 1 deletions(-)
---
diff --git a/vala/valabinaryexpression.vala b/vala/valabinaryexpression.vala
index b4b35c4..2747723 100644
--- a/vala/valabinaryexpression.vala
+++ b/vala/valabinaryexpression.vala
@@ -139,6 +139,14 @@ public class Vala.BinaryExpression : Expression {
 		return left.is_non_null () && right.is_non_null ();
 	}
 
+	bool in_assert () {
+		CodeNode expr = this;
+		while (expr != null && !(expr is MethodCall && ((MethodCall) expr).is_assert)) {
+			expr = expr.parent_node;
+		}
+		return (expr != null);
+	}
+
 	public override bool check (SemanticAnalyzer analyzer) {
 		if (checked) {
 			return !error;
@@ -148,7 +156,14 @@ public class Vala.BinaryExpression : Expression {
 
 		// some expressions are not in a block,
 		// for example, expressions in method contracts
-		if (analyzer.current_symbol is Block
+		//
+		// also don't convert expressions in asserts to not execute
+		// assert expressions when disabled on the C level and
+		// avoid unusable assertion messages
+		// reachability analysis and error handling should never be
+		// necessary for assertion expressions, so it is ok to avoid
+		// the split
+		if (analyzer.current_symbol is Block && !in_assert ()
 		    && (operator == BinaryOperator.AND || operator == BinaryOperator.OR)) {
 			// convert conditional expression into if statement
 			// required for flow analysis and exception handling
diff --git a/vala/valamethodcall.vala b/vala/valamethodcall.vala
index aafb948..aedeb96 100644
--- a/vala/valamethodcall.vala
+++ b/vala/valamethodcall.vala
@@ -39,6 +39,8 @@ public class Vala.MethodCall : Expression {
 
 	public bool is_yield_expression { get; set; }
 
+	public bool is_assert { get; private set; }
+
 	public Expression _call;
 	
 	private List<Expression> argument_list = new ArrayList<Expression> ();
@@ -131,6 +133,10 @@ public class Vala.MethodCall : Expression {
 			if (ma.inner != null) {
 				target_object_type = ma.inner.value_type;
 			}
+
+			if (ma.symbol_reference != null && ma.symbol_reference.get_attribute ("Assert") != null) {
+				this.is_assert = true;
+			}
 		}
 
 		var mtype = call.value_type;
diff --git a/vapi/glib-2.0.vapi b/vapi/glib-2.0.vapi
index 38b590e..2d31384 100644
--- a/vapi/glib-2.0.vapi
+++ b/vapi/glib-2.0.vapi
@@ -1672,6 +1672,7 @@ namespace GLib {
 	public static void warn_if_fail (bool expr);
 	public static void warn_if_reached ();
 
+	[Assert]
 	public static void assert (bool expr);
 	[NoReturn]
 	public static void assert_not_reached ();



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