Commit 8eb8af89 authored by Jürg Billeter's avatar Jürg Billeter

Check case expressions in switch statements

Fixes bug 577052.
parent f35206fb
......@@ -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;
......
/* 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;
......
/* 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;
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment