From: Jeremy Philippe Date: Tue, 7 Jan 2020 21:05:57 +0000 (+0100) Subject: vala: Infer target_type in coalescing expressions X-Git-Tag: 0.47.3~18 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fmerge-requests%2F96%2Fhead;p=thirdparty%2Fvala.git vala: Infer target_type in coalescing expressions Correctly handle reference tranfers of inner expressions. Fixes https://gitlab.gnome.org/GNOME/vala/issues/892 --- 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..fe559e1d5 100644 --- a/vala/valabinaryexpression.vala +++ b/vala/valabinaryexpression.vala @@ -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;