]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
vala: Inline allocated arrays require length or initializer
authorRico Tzschichholz <ricotz@ubuntu.com>
Sat, 1 Feb 2020 13:47:00 +0000 (14:47 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Sat, 1 Feb 2020 13:58:43 +0000 (14:58 +0100)
Fixes https://gitlab.gnome.org/GNOME/vala/issues/903

tests/Makefile.am
tests/arrays/inline-field.test [new file with mode: 0644]
tests/arrays/inline-local-variable.test [new file with mode: 0644]
tests/arrays/inline-parameter.test [new file with mode: 0644]
tests/arrays/inline-struct-field.test [new file with mode: 0644]
vala/valafield.vala
vala/valalocalvariable.vala
vala/valaparameter.vala
vala/valaparser.vala

index 0c13cf0aa5164e5cf0d2df5e09c9b3b66a700dc5..14e85507135533e844c916ef4a2483cbebecd54f 100644 (file)
@@ -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 (file)
index 0000000..e7f419d
--- /dev/null
@@ -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 (file)
index 0000000..ebcb4f3
--- /dev/null
@@ -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 (file)
index 0000000..3802e49
--- /dev/null
@@ -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 (file)
index 0000000..eba1c1c
--- /dev/null
@@ -0,0 +1,8 @@
+Invalid Code
+
+struct Foo {
+       public int bar[];
+}
+
+void main () {
+}
index dc4cde63e11ff18daff4f36ceac8e07b9f4797b0..25eec89f4679177b4fe8c0f9287cabf0abcc619e 100644 (file)
@@ -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;
 
index f441e16b8bd220fe06ffdebc42cf76520dcbacee..fac3bc41c244a0554b35f8cbccc8f72a6ee6a3df 100644 (file)
@@ -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)) {
index 978fec737ea40f6117c8f1a0cc693e75abec8cef..9c698458b8193d2ca1bd76505d07f6338a80e561 100644 (file)
@@ -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) {
index a030d24f2162f959d823113f7cd381fceb476cc1..2edfcce31dfd005c8080a74d49ab3fd84e951165 100644 (file)
@@ -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);