[vala/wip/issue/658: 39/48] Handle ref/out error msgs for anonymous delegates
- From: Rico Tzschichholz <ricotz src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [vala/wip/issue/658: 39/48] Handle ref/out error msgs for anonymous delegates
- Date: Sat, 31 Oct 2020 15:08:16 +0000 (UTC)
commit 48e899710ef4290677e7370eccefbdc545b87359
Author: Nick Schrader <nick schrader mailbox org>
Date: Sun Oct 25 18:47:52 2020 -0300
Handle ref/out error msgs for anonymous delegates
tests/Makefile.am | 3 +++
tests/delegates/anonymous-inner-ref-out.vala | 24 ++++++++++++++++++++++++
tests/delegates/anonymous-out.test | 15 +++++++++++++++
tests/delegates/anonymous-ref.test | 15 +++++++++++++++
vala/valaparser.vala | 11 +++++++++++
5 files changed, 68 insertions(+)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index df58af998..ba5f24a1d 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -341,8 +341,11 @@ TESTS = \
delegates/anonymous.vala \
delegates/anonymous-array.vala \
delegates/anonymous-generics.test \
+ delegates/anonymous-inner-ref-out.vala \
delegates/anonymous-optional-param.test \
+ delegates/anonymous-out.test \
delegates/anonymous-params.test \
+ delegates/anonymous-ref.test \
delegates/anonymous-throws.test \
delegates/anonymous-type-param.vala \
delegates/anonymous-variadic.test \
diff --git a/tests/delegates/anonymous-inner-ref-out.vala b/tests/delegates/anonymous-inner-ref-out.vala
new file mode 100644
index 000000000..a4a268a63
--- /dev/null
+++ b/tests/delegates/anonymous-inner-ref-out.vala
@@ -0,0 +1,24 @@
+void main() {
+ f(t);
+ g(d);
+}
+
+void f(delegate(ref int) => void x) {
+ int i = 42;
+ x(ref i);
+ assert (i == 7);
+}
+
+void g(delegate(out int) => void y) {
+ int i;
+ y(out i);
+ assert (i == 42);
+}
+
+void t(ref int a) {
+ a = 7;
+}
+
+void d(out int a) {
+ a = 42;
+}
diff --git a/tests/delegates/anonymous-out.test b/tests/delegates/anonymous-out.test
new file mode 100644
index 000000000..30105b705
--- /dev/null
+++ b/tests/delegates/anonymous-out.test
@@ -0,0 +1,15 @@
+Invalid Code
+
+/*
+ * 13.8-13.32: error: Anonymous delegates cannot be `out` parameters
+ * void f(ref delegate(int) => void x) {
+ * ^^^^^^^^^^^^^^^^^^^^^^^^^
+ */
+
+void main() {
+ assert (true);
+}
+
+void f(out delegate(int) => void x) {
+ x(42);
+}
diff --git a/tests/delegates/anonymous-ref.test b/tests/delegates/anonymous-ref.test
new file mode 100644
index 000000000..6bcc73b88
--- /dev/null
+++ b/tests/delegates/anonymous-ref.test
@@ -0,0 +1,15 @@
+Invalid Code
+
+/*
+ * 13.8-13.32: error: Anonymous delegates cannot be `ref` parameters
+ * void f(ref delegate(int) => void x) {
+ * ^^^^^^^^^^^^^^^^^^^^^^^^^
+ */
+
+void main() {
+ assert (true);
+}
+
+void f(ref delegate(int) => void x) {
+ x(42);
+}
diff --git a/vala/valaparser.vala b/vala/valaparser.vala
index 7a5ad9bb5..785cb206c 100644
--- a/vala/valaparser.vala
+++ b/vala/valaparser.vala
@@ -3550,16 +3550,27 @@ public class Vala.Parser : CodeVisitor {
}
DataType type;
+ string pretty_direction = null;
if (direction == ParameterDirection.IN) {
// in parameters are unowned by default
type = parse_type (false, false, false, parent, method);
} else if (direction == ParameterDirection.REF) {
// ref parameters own the value by default
type = parse_type (true, true, false, parent, method);
+ pretty_direction = "ref";
} else {
// out parameters own the value by default
type = parse_type (true, false, false, parent, method);
+ pretty_direction = "out";
}
+
+ var possibly_delegate = type as DelegateType;
+ if (possibly_delegate != null
+ && possibly_delegate.delegate_symbol.anonymous
+ && pretty_direction != null) {
+ Report.error (get_src (begin), "Anonymous delegates cannot be `" + pretty_direction +
"` parameters");
+ }
+
string id = parse_identifier ();
type = parse_inline_array_type (type);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]