From: Rico Tzschichholz Date: Sat, 14 Mar 2020 14:15:51 +0000 (+0100) Subject: vala: Accept enum-values as length for inline allocated arrays X-Git-Tag: 0.48.2~22 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bc8bf47d67d1056522f5e65c188e542b94a95449;p=thirdparty%2Fvala.git vala: Accept enum-values as length for inline allocated arrays Fixes https://gitlab.gnome.org/GNOME/vala/issues/935 --- diff --git a/tests/Makefile.am b/tests/Makefile.am index dd3cb46f9..893392b94 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -79,6 +79,7 @@ TESTS = \ arrays/fixed-length-init0-not-allowed.vala \ arrays/field-global-length-cname.vala \ arrays/fixed-length-concat-invalid.test \ + arrays/fixed-length-enum-value.vala \ arrays/fixed-length-non-const.test \ arrays/fixed-length-resize-invalid.test \ arrays/inline-field.test \ diff --git a/tests/arrays/fixed-length-enum-value.vala b/tests/arrays/fixed-length-enum-value.vala new file mode 100644 index 000000000..c195eb115 --- /dev/null +++ b/tests/arrays/fixed-length-enum-value.vala @@ -0,0 +1,19 @@ +enum Foo { + BAR = 23; +} + +struct Bar { + public char array[Foo.BAR]; +} + +void foo (uint array[Foo.BAR]) { + assert (array.length == 23); +} + +void main () { + int array[Foo.BAR]; + assert (array.length == 23); + + var bar = Bar (); + assert (bar.array.length == 23); +} diff --git a/vala/valaarraytype.vala b/vala/valaarraytype.vala index bb6dceb44..9add1d0d4 100644 --- a/vala/valaarraytype.vala +++ b/vala/valaarraytype.vala @@ -289,7 +289,8 @@ public class Vala.ArrayType : ReferenceType { if (fixed_length && length != null) { length.check (context); - if (length.value_type == null || !(length.value_type is IntegerType) || !length.is_constant ()) { + if (length.value_type == null || !(length.value_type is IntegerType || length.value_type is EnumValueType) + || !length.is_constant ()) { error = true; Report.error (length.source_reference, "Expression of constant integer type expected"); return false;