[vala/0.46] vala: Don't falsely resolve binary-expression to bool



commit f7b61198ccf6dd36627b72b3a3b83ee982dbd852
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Thu Oct 24 14:15:56 2019 +0200

    vala: Don't falsely resolve binary-expression to bool
    
    Fixes https://gitlab.gnome.org/GNOME/vala/issues/869

 tests/Makefile.am                                  | 2 ++
 tests/control-semantic/condition-not-boolean.test  | 7 +++++++
 tests/control-semantic/expression-not-boolean.test | 5 +++++
 vala/valabinaryexpression.vala                     | 8 +++++++-
 4 files changed, 21 insertions(+), 1 deletion(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 7c0926498..ea81bf8bb 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -200,6 +200,8 @@ TESTS = \
        control-semantic/argument-owned-ref.test \
        control-semantic/argument-value-out.test \
        control-semantic/argument-value-ref.test \
+       control-semantic/condition-not-boolean.test \
+       control-semantic/expression-not-boolean.test \
        control-semantic/literal-immutable.test \
        control-semantic/member-incompatible-type.test \
        control-semantic/member-invalid.test \
diff --git a/tests/control-semantic/condition-not-boolean.test 
b/tests/control-semantic/condition-not-boolean.test
new file mode 100644
index 000000000..7ba2130e9
--- /dev/null
+++ b/tests/control-semantic/condition-not-boolean.test
@@ -0,0 +1,7 @@
+Invalid Code
+
+void main () {
+       if (true != false & 0) {
+               assert_not_reached ();
+       }
+}
diff --git a/tests/control-semantic/expression-not-boolean.test 
b/tests/control-semantic/expression-not-boolean.test
new file mode 100644
index 000000000..2be02e98d
--- /dev/null
+++ b/tests/control-semantic/expression-not-boolean.test
@@ -0,0 +1,5 @@
+Invalid Code
+
+void main () {
+       bool foo = true != false & 0;
+}
diff --git a/vala/valabinaryexpression.vala b/vala/valabinaryexpression.vala
index 0578ba93d..5e9b7be42 100644
--- a/vala/valabinaryexpression.vala
+++ b/vala/valabinaryexpression.vala
@@ -498,7 +498,13 @@ public class Vala.BinaryExpression : Expression {
                        left.target_type.nullable = false;
                        right.target_type.nullable = false;
 
-                       value_type = left.target_type.copy ();
+                       // Don't falsely resolve to bool
+                       if (left.value_type.compatible (context.analyzer.bool_type)
+                           && !right.value_type.compatible (context.analyzer.bool_type)) {
+                               value_type = right.target_type.copy ();
+                       } else {
+                               value_type = left.target_type.copy ();
+                       }
                } else if (operator == BinaryOperator.AND
                           || operator == BinaryOperator.OR) {
                        if (!left.value_type.compatible (context.analyzer.bool_type) || 
!right.value_type.compatible (context.analyzer.bool_type)) {


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