]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
vala: Don't allow "in" operation with different enum types
authorRico Tzschichholz <ricotz@ubuntu.com>
Sun, 21 Feb 2021 09:40:25 +0000 (10:40 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Sun, 21 Feb 2021 09:41:28 +0000 (10:41 +0100)
Fixes https://gitlab.gnome.org/GNOME/vala/issues/1139

tests/Makefile.am
tests/enums/in-invalid.test [new file with mode: 0644]
vala/valabinaryexpression.vala

index c0e63b29c8122253e94145280d0a3d3d95b60f22..860020161e99707e722d6e2ecc5eeb24753a5729 100644 (file)
@@ -311,6 +311,7 @@ TESTS = \
        enums/flags.vala \
        enums/from-0-literal.vala \
        enums/in-inference.vala \
+       enums/in-invalid.test \
        enums/no_gtype_to_string.vala \
        enums/switch.vala \
        enums/bug666035.vala \
diff --git a/tests/enums/in-invalid.test b/tests/enums/in-invalid.test
new file mode 100644 (file)
index 0000000..fb7ae4b
--- /dev/null
@@ -0,0 +1,18 @@
+Invalid Code
+
+[Flags]
+enum Foo {
+       BAR,
+       MANAM;
+}
+
+[Flags]
+enum Bar {
+       MANAM,
+       FOO;
+}
+
+void main() {
+       Foo foo = MANAM;
+       assert (Bar.MANAM in foo);
+}
index 9fcefde30b39b699ebc30a33b6dece9df2285a9b..d379927c0abbad078f3376b95152a51c56d01024 100644 (file)
@@ -573,6 +573,11 @@ public class Vala.BinaryExpression : Expression {
                                // integers or enums
                                left.target_type.nullable = false;
                                right.target_type.nullable = false;
+                               if (left.value_type.type_symbol is Enum && right.value_type.type_symbol is Enum
+                                   && left.value_type.type_symbol != right.value_type.type_symbol) {
+                                       error = true;
+                                       Report.error (source_reference, "Cannot look for `%s' in `%s'", left.value_type.to_string (), right.value_type.to_string ());
+                               }
                        } else if (right.value_type is ArrayType) {
                                if (!left.value_type.compatible (((ArrayType) right.value_type).element_type)) {
                                        error = true;