[vala/staging: 3/3] vala: Accept enum-values as length for inline allocated arrays



commit bc8bf47d67d1056522f5e65c188e542b94a95449
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Sat Mar 14 15:15:51 2020 +0100

    vala: Accept enum-values as length for inline allocated arrays
    
    Fixes https://gitlab.gnome.org/GNOME/vala/issues/935

 tests/Makefile.am                         |  1 +
 tests/arrays/fixed-length-enum-value.vala | 19 +++++++++++++++++++
 vala/valaarraytype.vala                   |  3 ++-
 3 files changed, 22 insertions(+), 1 deletion(-)
---
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;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]