]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
vala: Don't falsely resolve binary-expression to bool
authorRico Tzschichholz <ricotz@ubuntu.com>
Thu, 24 Oct 2019 12:15:56 +0000 (14:15 +0200)
committerRico Tzschichholz <ricotz@ubuntu.com>
Thu, 7 Nov 2019 11:11:48 +0000 (12:11 +0100)
Fixes https://gitlab.gnome.org/GNOME/vala/issues/869

tests/Makefile.am
tests/control-semantic/condition-not-boolean.test [new file with mode: 0644]
tests/control-semantic/expression-not-boolean.test [new file with mode: 0644]
vala/valabinaryexpression.vala

index 08e46cd05daefa4dee1d393a11e81e2ae4fa725a..0280645452303947ca92274178d921dac5038631 100644 (file)
@@ -198,6 +198,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 (file)
index 0000000..7ba2130
--- /dev/null
@@ -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 (file)
index 0000000..2be02e9
--- /dev/null
@@ -0,0 +1,5 @@
+Invalid Code
+
+void main () {
+       bool foo = true != false & 0;
+}
index 0578ba93d9447e7bdc924ebffa31f258ebb6775d..5e9b7be42754d34faf0601d17aa867cc1a6449f6 100644 (file)
@@ -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)) {