[vala/staging] vala: Infer target_type in coalescing expressions



commit d3ca02545fdf866cb0902b72e5be1389660145b9
Author: Jeremy Philippe <jeremy philippe gmail com>
Date:   Tue Jan 7 22:05:57 2020 +0100

    vala: Infer target_type in coalescing expressions
    
    Correctly handle reference tranfers of inner expressions.
    
    Fixes https://gitlab.gnome.org/GNOME/vala/issues/892

 tests/Makefile.am                                  |  1 +
 .../control-flow/coalesce-reference-transfer.vala  | 23 ++++++++++++++++++++++
 vala/valabinaryexpression.vala                     |  3 +++
 3 files changed, 27 insertions(+)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index a486591ec..2c66135bf 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -165,6 +165,7 @@ TESTS = \
        control-flow/assigned-local-variable.vala \
        control-flow/break.vala \
        control-flow/break-invalid.test \
+       control-flow/coalesce-reference-transfer.vala \
        control-flow/continue-invalid.test \
        control-flow/double-catch.test \
        control-flow/expressions-conditional.vala \
diff --git a/tests/control-flow/coalesce-reference-transfer.vala 
b/tests/control-flow/coalesce-reference-transfer.vala
new file mode 100644
index 000000000..4efbf3ed4
--- /dev/null
+++ b/tests/control-flow/coalesce-reference-transfer.vala
@@ -0,0 +1,23 @@
+[Compact]
+class Foo {
+       public int i;
+
+       public Foo (int i) {
+               this.i = i;
+       }
+}
+
+Foo? get_foo (int? i) {
+       return i != null ? new Foo (i) : null;
+}
+
+void main () {
+       {
+               Foo foo = get_foo (null) ?? get_foo (42);
+               assert (foo.i == 42);
+       }
+       {
+               Foo foo = get_foo (null) ?? (get_foo (null) ?? get_foo (42));
+               assert (foo.i == 42);
+       }
+}
diff --git a/vala/valabinaryexpression.vala b/vala/valabinaryexpression.vala
index 4f1f642f5..55bf091f7 100644
--- a/vala/valabinaryexpression.vala
+++ b/vala/valabinaryexpression.vala
@@ -189,6 +189,9 @@ public class Vala.BinaryExpression : Expression {
                }
 
                if (operator == BinaryOperator.COALESCE) {
+                       left.target_type = target_type.copy ();
+                       right.target_type = target_type.copy ();
+
                        if (!left.check (context)) {
                                error = true;
                                return false;


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