[vala] Fix comparison of nullable value types



commit 61d372c3d65f7830a17ac8aa34b1eb2269b311a7
Author: Simon Werbeck <simon werbeck gmail com>
Date:   Sun Jun 29 17:00:37 2014 +0200

    Fix comparison of nullable value types
    
    Fixes bug 678791

 tests/Makefile.am                |    1 +
 tests/basic-types/bug678791.vala |   15 +++++++++++++++
 vala/valabinaryexpression.vala   |   12 ++++++------
 3 files changed, 22 insertions(+), 6 deletions(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index bcf82e1..07c9877 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -34,6 +34,7 @@ TESTS = \
        basic-types/bug652380.vala \
        basic-types/bug655908.vala \
        basic-types/bug659975.vala \
+       basic-types/bug678791.vala \
        basic-types/bug686336.vala \
        namespaces.vala \
        methods/lambda.vala \
diff --git a/tests/basic-types/bug678791.vala b/tests/basic-types/bug678791.vala
new file mode 100644
index 0000000..2f3171f
--- /dev/null
+++ b/tests/basic-types/bug678791.vala
@@ -0,0 +1,15 @@
+public enum Foo {
+       BAR
+}
+
+void main () {
+       int? a = null;
+       int b = 1;
+       assert (a != b);
+       assert (b != a);
+
+       Foo? f = null;
+       Foo g = Foo.BAR;
+       assert (f != g);
+       assert (g != f);
+}
diff --git a/vala/valabinaryexpression.vala b/vala/valabinaryexpression.vala
index bdd9e5e..98b2e5e 100644
--- a/vala/valabinaryexpression.vala
+++ b/vala/valabinaryexpression.vala
@@ -422,12 +422,12 @@ public class Vala.BinaryExpression : Expression {
                        right.target_type.value_owned = false;
 
                        if (left.value_type.nullable != right.value_type.nullable) {
-                               // if only one operand is nullable, make sure the other operand is promoted 
to nullable as well
-                               if (!left.value_type.nullable) {
-                                       left.target_type.nullable = true;
-                               } else if (!right.value_type.nullable) {
-                                       right.target_type.nullable = true;
-                               }
+                               // if only one operand is nullable, make sure the other
+                               // operand is promoted to nullable as well,
+                               // reassign both, as get_arithmetic_result_type doesn't
+                               // take nullability into account
+                               left.target_type.nullable = true;
+                               right.target_type.nullable = true;
                        }
 
                        value_type = context.analyzer.bool_type;


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