[vala/staging] vala: Infer needle type for "in" expression on enum



commit 5cc60c1ddfebaf05483370913f28b4074345667b
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Sun Feb 21 10:40:25 2021 +0100

    vala: Infer needle type for "in" expression on enum
    
    Fixes https://gitlab.gnome.org/GNOME/vala/issues/1138

 tests/Makefile.am              |  1 +
 tests/enums/in-inference.vala  | 14 ++++++++++++++
 vala/valabinaryexpression.vala |  5 +++++
 3 files changed, 20 insertions(+)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index c2a9c7fbe..c0e63b29c 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -310,6 +310,7 @@ TESTS = \
        enums/enums.vala \
        enums/flags.vala \
        enums/from-0-literal.vala \
+       enums/in-inference.vala \
        enums/no_gtype_to_string.vala \
        enums/switch.vala \
        enums/bug666035.vala \
diff --git a/tests/enums/in-inference.vala b/tests/enums/in-inference.vala
new file mode 100644
index 000000000..7f44df8df
--- /dev/null
+++ b/tests/enums/in-inference.vala
@@ -0,0 +1,14 @@
+[Flags]
+enum Foo {
+       FOO,
+       BAR,
+       MANAM;
+}
+
+void main () {
+       Foo foo = FOO | BAR;
+       if (MANAM in foo) {
+               assert_not_reached ();
+       }
+       assert (BAR in foo);
+}
diff --git a/vala/valabinaryexpression.vala b/vala/valabinaryexpression.vala
index f831c7cb7..9fcefde30 100644
--- a/vala/valabinaryexpression.vala
+++ b/vala/valabinaryexpression.vala
@@ -303,6 +303,11 @@ public class Vala.BinaryExpression : Expression {
                    && (operator == BinaryOperator.BITWISE_AND || operator == BinaryOperator.BITWISE_OR)) {
                        left.target_type = target_type.copy ();
                        right.target_type = target_type.copy ();
+               } else if (operator == BinaryOperator.IN) {
+                       right.check (context);
+                       if (right.value_type.type_symbol is Enum) {
+                               left.target_type = right.value_type.copy ();
+                       }
                }
                left.check (context);
                if (left.value_type != null && left.value_type.type_symbol is Enum


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