From: Jürg Billeter Date: Sun, 16 Aug 2009 12:20:48 +0000 (+0200) Subject: Check case expressions in switch statements X-Git-Tag: 0.7.6~200 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8eb8af894ad2334bd665f903bdff4111933de200;p=thirdparty%2Fvala.git Check case expressions in switch statements Fixes bug 577052. --- diff --git a/vala/valamemberaccess.vala b/vala/valamemberaccess.vala index 3ce7399c9..195af93fd 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 a778ce258..67fcdc25e 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 27c02c2f6..929e05650 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; } /**