[vala/staging] vala: Inline allocated arrays require length or initializer
- From: Rico Tzschichholz <ricotz src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [vala/staging] vala: Inline allocated arrays require length or initializer
- Date: Sat, 1 Feb 2020 14:00:01 +0000 (UTC)
commit e336a027c3ee11a1250abd455ebadd5b32a4756a
Author: Rico Tzschichholz <ricotz ubuntu com>
Date: Sat Feb 1 14:47:00 2020 +0100
vala: Inline allocated arrays require length or initializer
Fixes https://gitlab.gnome.org/GNOME/vala/issues/903
tests/Makefile.am | 4 ++++
tests/arrays/inline-field.test | 6 ++++++
tests/arrays/inline-local-variable.test | 5 +++++
tests/arrays/inline-parameter.test | 7 +++++++
tests/arrays/inline-struct-field.test | 8 ++++++++
vala/valafield.vala | 5 +++++
vala/valalocalvariable.vala | 5 +++++
vala/valaparameter.vala | 6 ++++++
vala/valaparser.vala | 7 +------
9 files changed, 47 insertions(+), 6 deletions(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 0c13cf0aa..14e855071 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -78,6 +78,10 @@ TESTS = \
arrays/fixed-length-concat-invalid.test \
arrays/fixed-length-non-const.test \
arrays/fixed-length-resize-invalid.test \
+ arrays/inline-field.test \
+ arrays/inline-local-variable.test \
+ arrays/inline-parameter.test \
+ arrays/inline-struct-field.test \
arrays/length-inline-assignment.vala \
arrays/struct-field-length-cname.vala \
arrays/incompatible-integer-elements.test \
diff --git a/tests/arrays/inline-field.test b/tests/arrays/inline-field.test
new file mode 100644
index 000000000..e7f419da4
--- /dev/null
+++ b/tests/arrays/inline-field.test
@@ -0,0 +1,6 @@
+Invalid Code
+
+int bar[];
+
+void main () {
+}
diff --git a/tests/arrays/inline-local-variable.test b/tests/arrays/inline-local-variable.test
new file mode 100644
index 000000000..ebcb4f377
--- /dev/null
+++ b/tests/arrays/inline-local-variable.test
@@ -0,0 +1,5 @@
+Invalid Code
+
+void main () {
+ int manam[];
+}
diff --git a/tests/arrays/inline-parameter.test b/tests/arrays/inline-parameter.test
new file mode 100644
index 000000000..3802e498e
--- /dev/null
+++ b/tests/arrays/inline-parameter.test
@@ -0,0 +1,7 @@
+Invalid Code
+
+void foo (int bar[]) {
+}
+
+void main () {
+}
diff --git a/tests/arrays/inline-struct-field.test b/tests/arrays/inline-struct-field.test
new file mode 100644
index 000000000..eba1c1ce3
--- /dev/null
+++ b/tests/arrays/inline-struct-field.test
@@ -0,0 +1,8 @@
+Invalid Code
+
+struct Foo {
+ public int bar[];
+}
+
+void main () {
+}
diff --git a/vala/valafield.vala b/vala/valafield.vala
index dc4cde63e..25eec89f4 100644
--- a/vala/valafield.vala
+++ b/vala/valafield.vala
@@ -123,6 +123,11 @@ public class Vala.Field : Variable, Lockable {
initializer = null;
}
+ if (variable_array_type != null && variable_array_type.inline_allocated
+ && !variable_array_type.fixed_length) {
+ Report.error (source_reference, "Inline allocated array as field requires to have
fixed length");
+ }
+
if (initializer != null) {
initializer.target_type = variable_type;
diff --git a/vala/valalocalvariable.vala b/vala/valalocalvariable.vala
index f441e16b8..fac3bc41c 100644
--- a/vala/valalocalvariable.vala
+++ b/vala/valalocalvariable.vala
@@ -161,6 +161,11 @@ public class Vala.LocalVariable : Variable {
initializer = null;
}
+ if (variable_array_type != null && variable_array_type.inline_allocated
+ && variable_array_type.length == null && !(initializer is ArrayCreationExpression)) {
+ Report.error (source_reference, "Inline allocated array requires either a given
length or an initializer");
+ }
+
if (initializer != null && !initializer.error) {
if (initializer.value_type is MethodType) {
if (!(initializer is MemberAccess) && !(initializer is LambdaExpression)) {
diff --git a/vala/valaparameter.vala b/vala/valaparameter.vala
index 978fec737..9c698458b 100644
--- a/vala/valaparameter.vala
+++ b/vala/valaparameter.vala
@@ -167,6 +167,12 @@ public class Vala.Parameter : Variable {
initializer.target_type = variable_type.copy ();
initializer.check (context);
}
+
+ unowned ArrayType? variable_array_type = variable_type as ArrayType;
+ if (variable_array_type != null && variable_array_type.inline_allocated
+ && !variable_array_type.fixed_length) {
+ Report.error (source_reference, "Inline allocated array as parameter requires
to have fixed length");
+ }
}
if (initializer != null) {
diff --git a/vala/valaparser.vala b/vala/valaparser.vala
index a030d24f2..2edfcce31 100644
--- a/vala/valaparser.vala
+++ b/vala/valaparser.vala
@@ -3355,12 +3355,7 @@ public class Vala.Parser : CodeVisitor {
}
string id = parse_identifier ();
- var array_type = parse_inline_array_type (type);
- if (!(type is ArrayType) && (array_type is ArrayType) && !((ArrayType)
array_type).fixed_length) {
- throw new ParseError.SYNTAX ("invalid array parameter declaration");
- } else {
- type = array_type;
- }
+ type = parse_inline_array_type (type);
var param = new Parameter (id, type, get_src (begin));
set_attributes (param, attrs);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]