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



commit 4f0b70ffc821d07375248667091960a428183b95
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 e95450ae4..3f7fc7824 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -68,6 +68,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 21941e3e4..3ad96e52b 100644
--- a/vala/valaarraytype.vala
+++ b/vala/valaarraytype.vala
@@ -265,7 +265,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]