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



commit c0c8806ed9897b97e10c77c2a4433320298c6fb3
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 dcac3682f..54f23ef36 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -309,6 +309,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 d632c5941..6b2ad57eb 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]