[vala/staging: 1/2] vala: Infer target_type in coalescing expressions
- From: Rico Tzschichholz <ricotz src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [vala/staging: 1/2] vala: Infer target_type in coalescing expressions
- Date: Wed, 8 Jan 2020 14:36:31 +0000 (UTC)
commit 7850210bafcdd81f5b7a36b82766ae73f0fa0610
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 | 5 +++++
3 files changed, 29 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..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;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]