]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
vala: Infer target_type in coalescing expressions 7850210bafcdd81f5b7a36b82766ae73f0fa0610 96/head
authorJeremy Philippe <jeremy.philippe@gmail.com>
Tue, 7 Jan 2020 21:05:57 +0000 (22:05 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Wed, 8 Jan 2020 14:01:21 +0000 (15:01 +0100)
Correctly handle reference tranfers of inner expressions.

Fixes https://gitlab.gnome.org/GNOME/vala/issues/892

tests/Makefile.am
tests/control-flow/coalesce-reference-transfer.vala [new file with mode: 0644]
vala/valabinaryexpression.vala

index a486591ecb9ad4d1a43dfa9af4f526ee6beb5583..2c66135bf9d3818cebfe229b9435a43db93e6c8d 100644 (file)
@@ -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 (file)
index 0000000..4efbf3e
--- /dev/null
@@ -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);
+       }
+}
index 4f1f642f537d5e6e18d6ee467fe88e97ff4512d7..fe559e1d56a6396a05fac92f27c2741af0a6db0b 100644 (file)
@@ -189,6 +189,11 @@ public class Vala.BinaryExpression : Expression {
                }
 
                if (operator == BinaryOperator.COALESCE) {
+                       if (target_type != null) {
+                               left.target_type = target_type.copy ();
+                               right.target_type = target_type.copy ();
+                       }
+
                        if (!left.check (context)) {
                                error = true;
                                return false;