From c18da22b46b393fdcae604ca155ac7328eee871f Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=BCrg=20Billeter?= Date: Mon, 22 Mar 2010 20:01:14 +0100 Subject: [PATCH] Simplify for statements whose condition is false Fixes bug 601351. --- vala/valaforstatement.vala | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/vala/valaforstatement.vala b/vala/valaforstatement.vala index 99301fe9f..6e8be3d90 100644 --- a/vala/valaforstatement.vala +++ b/vala/valaforstatement.vala @@ -1,6 +1,6 @@ /* valaforstatement.vala * - * Copyright (C) 2006-2009 Jürg Billeter + * Copyright (C) 2006-2010 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 @@ -141,6 +141,11 @@ public class Vala.ForStatement : CodeNode, Statement { return (literal != null && literal.value); } + bool always_false (Expression condition) { + var literal = condition as BooleanLiteral; + return (literal != null && !literal.value); + } + public override bool check (SemanticAnalyzer analyzer) { // convert to simple loop @@ -152,7 +157,11 @@ public class Vala.ForStatement : CodeNode, Statement { } // do not generate if block if condition is always true - if (condition != null && !always_true (condition)) { + if (condition == null || always_true (condition)) { + } else if (always_false (condition)) { + // do not generate if block if condition is always false + body.insert_statement (0, new BreakStatement (condition.source_reference)); + } else { // condition var if_condition = new UnaryExpression (UnaryOperator.LOGICAL_NEGATION, condition, condition.source_reference); var true_block = new Block (condition.source_reference); -- 2.47.3