From: Luca Bruno Date: Tue, 5 Jul 2011 18:24:51 +0000 (+0200) Subject: Fix arithmetic expressions whose operands have different types X-Git-Tag: 0.13.1~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f70d37bd3099b8bb422ffe57e9d6b6c7415feeb7;p=thirdparty%2Fvala.git Fix arithmetic expressions whose operands have different types --- diff --git a/tests/basic-types/floats.vala b/tests/basic-types/floats.vala index 3294bb924..3f6eaa18e 100644 --- a/tests/basic-types/floats.vala +++ b/tests/basic-types/floats.vala @@ -52,6 +52,10 @@ void test_double () { d = double.MAX; assert (d == double.MAX); assert (d > double.MIN); + + // nullable + double? d2 = 10; + assert (d2 == 10); } void main () { diff --git a/vala/valabinaryexpression.vala b/vala/valabinaryexpression.vala index 2cddc2f0a..50d49488c 100644 --- a/vala/valabinaryexpression.vala +++ b/vala/valabinaryexpression.vala @@ -364,9 +364,6 @@ public class Vala.BinaryExpression : Expression { } else { DataType resulting_type; - left.target_type.nullable = false; - right.target_type.nullable = false; - if (chained) { var lbe = (BinaryExpression) left; resulting_type = context.analyzer.get_arithmetic_result_type (lbe.right.target_type, right.target_type); @@ -379,6 +376,13 @@ public class Vala.BinaryExpression : Expression { Report.error (source_reference, "Relational operation not supported for types `%s' and `%s'".printf (left.value_type.to_string (), right.value_type.to_string ())); return false; } + + if (!chained) { + left.target_type = resulting_type.copy (); + } + right.target_type = resulting_type.copy (); + left.target_type.nullable = false; + right.target_type.nullable = false; } value_type = context.analyzer.bool_type; @@ -393,9 +397,14 @@ public class Vala.BinaryExpression : Expression { return false; } - left.target_type = left.value_type.copy (); + var resulting_type = context.analyzer.get_arithmetic_result_type (left.target_type, right.target_type); + if (resulting_type != null) { + // numeric operation + left.target_type = resulting_type.copy (); + right.target_type = resulting_type.copy (); + } + left.target_type.value_owned = false; - right.target_type = right.value_type.copy (); right.target_type.value_owned = false; if (left.value_type.nullable != right.value_type.nullable) {