[vala/staging] vala: Don't allow invocation of "resize" in unowned array variables
- From: Rico Tzschichholz <ricotz src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [vala/staging] vala: Don't allow invocation of "resize" in unowned array variables
- Date: Fri, 3 Jul 2020 17:36:21 +0000 (UTC)
commit c5aa3af185bf681f26a3d9494a1475bbcaa170de
Author: Rico Tzschichholz <ricotz ubuntu com>
Date: Fri Jul 3 19:33:41 2020 +0200
vala: Don't allow invocation of "resize" in unowned array variables
Fixes https://gitlab.gnome.org/GNOME/vala/issues/928
tests/Makefile.am | 4 ++++
tests/arrays/resize-unowned-invalid-2.test | 10 ++++++++++
tests/arrays/resize-unowned-invalid-3.test | 9 +++++++++
tests/arrays/resize-unowned-invalid.test | 7 +++++++
tests/arrays/resize.vala | 29 +++++++++++++++++++++++++++++
vala/valamemberaccess.vala | 11 ++++++++---
6 files changed, 67 insertions(+), 3 deletions(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index c68512e2c..da440e248 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -100,6 +100,10 @@ TESTS = \
arrays/struct-field-initializer.vala \
arrays/struct-namespaced-initializer.vala \
arrays/incompatible-integer-elements.test \
+ arrays/resize.vala \
+ arrays/resize-unowned-invalid.test \
+ arrays/resize-unowned-invalid-2.test \
+ arrays/resize-unowned-invalid-3.test \
arrays/slice-invalid-start.test \
arrays/slice-invalid-stop.test \
arrays/slice-no-array.test \
diff --git a/tests/arrays/resize-unowned-invalid-2.test b/tests/arrays/resize-unowned-invalid-2.test
new file mode 100644
index 000000000..be1ab49ef
--- /dev/null
+++ b/tests/arrays/resize-unowned-invalid-2.test
@@ -0,0 +1,10 @@
+Invalid Code
+
+void bar (string[] foo) {
+ foo.resize (42);
+}
+
+void main () {
+ var foo = new string[23];
+ bar (foo);
+}
diff --git a/tests/arrays/resize-unowned-invalid-3.test b/tests/arrays/resize-unowned-invalid-3.test
new file mode 100644
index 000000000..b76d4b7c8
--- /dev/null
+++ b/tests/arrays/resize-unowned-invalid-3.test
@@ -0,0 +1,9 @@
+Invalid Code
+
+unowned string[] foo;
+
+void main () {
+ var bar = new string[23];
+ foo = bar;
+ foo.resize (42);
+}
diff --git a/tests/arrays/resize-unowned-invalid.test b/tests/arrays/resize-unowned-invalid.test
new file mode 100644
index 000000000..b9d7aea4d
--- /dev/null
+++ b/tests/arrays/resize-unowned-invalid.test
@@ -0,0 +1,7 @@
+Invalid Code
+
+void main () {
+ var foo = new string[23];
+ unowned string[] bar = foo;
+ bar.resize (42);
+}
diff --git a/tests/arrays/resize.vala b/tests/arrays/resize.vala
new file mode 100644
index 000000000..0ed1bc1de
--- /dev/null
+++ b/tests/arrays/resize.vala
@@ -0,0 +1,29 @@
+void bar (ref string[] foo) {
+ foo.resize (42);
+}
+
+void manam (out string[] foo) {
+ foo = new string[23];
+ foo.resize (42);
+}
+
+string[] boo;
+
+void main () {
+ {
+ var foo = new string[23];
+ foo.resize (42);
+ }
+ {
+ var foo = new string[23];
+ bar (ref foo);
+ }
+ {
+ string[] foo;
+ manam (out foo);
+ }
+ {
+ boo = new string[23];
+ boo.resize (42);
+ }
+}
diff --git a/vala/valamemberaccess.vala b/vala/valamemberaccess.vala
index 5e810d699..2f52c3983 100644
--- a/vala/valamemberaccess.vala
+++ b/vala/valamemberaccess.vala
@@ -487,11 +487,16 @@ public class Vala.MemberAccess : Expression {
}
}
- if (symbol_reference is ArrayResizeMethod && inner.value_type is ArrayType) {
- unowned ArrayType? value_array_type = inner.value_type as ArrayType;
- if (value_array_type != null && value_array_type.inline_allocated) {
+ if (symbol_reference is ArrayResizeMethod && inner.symbol_reference is Variable) {
+ // require the real type with its original value_owned attritubte
+ var inner_type = context.analyzer.get_value_type_for_symbol
(inner.symbol_reference, true)
+ .get_actual_type (inner.value_type, null, this) as ArrayType;
+ if (inner_type != null && inner_type.inline_allocated) {
Report.error (source_reference, "`resize' is not supported for arrays
with fixed length");
error = true;
+ } else if (inner_type != null && !inner_type.value_owned) {
+ Report.error (source_reference, "`resize' is not allowed for unowned
array references");
+ error = true;
}
}
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]