[vala/staging] vala: Disallow resize() for constant arrays



commit a0e8e4b4bfd6e8c5e374f0bbf190de7afaa15cab
Author: wxx <769218589 qq com>
Date:   Tue Aug 3 23:07:49 2021 +0800

    vala: Disallow resize() for constant arrays
    
    See https://gitlab.gnome.org/GNOME/vala/issues/944

 tests/Makefile.am                 |  1 +
 tests/arrays/resize-constant.test |  6 ++++++
 vala/valamemberaccess.vala        | 22 ++++++++++++++--------
 3 files changed, 21 insertions(+), 8 deletions(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index f0ec0e9c4..3b0ca32b1 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -130,6 +130,7 @@ TESTS = \
        arrays/struct-namespaced-initializer.vala \
        arrays/incompatible-integer-elements.test \
        arrays/resize.vala \
+       arrays/resize-constant.test \
        arrays/resize-local-size.vala \
        arrays/resize-local-size-captured.vala \
        arrays/resize-unowned-invalid.test \
diff --git a/tests/arrays/resize-constant.test b/tests/arrays/resize-constant.test
new file mode 100644
index 000000000..277db85af
--- /dev/null
+++ b/tests/arrays/resize-constant.test
@@ -0,0 +1,6 @@
+Invalid Code
+
+void main () {
+       const int[] foo = { 1, 2, 3 };
+       foo.resize (2);
+}
diff --git a/vala/valamemberaccess.vala b/vala/valamemberaccess.vala
index 6806c55a6..ef4bc22d2 100644
--- a/vala/valamemberaccess.vala
+++ b/vala/valamemberaccess.vala
@@ -519,14 +519,20 @@ public class Vala.MemberAccess : Expression {
                                }
                        }
 
-                       if (symbol_reference is ArrayResizeMethod && inner.symbol_reference is Variable) {
-                               // require the real type with its original value_owned attritubte
-                               var inner_type = context.analyzer.get_value_type_for_symbol 
(inner.symbol_reference, true) as ArrayType;
-                               if (inner_type != null && inner_type.inline_allocated) {
-                                       Report.error (source_reference, "`resize' is not supported for arrays 
with fixed length");
-                                       error = true;
-                               } else if (inner_type != null && !inner_type.value_owned) {
-                                       Report.error (source_reference, "`resize' is not allowed for unowned 
array references");
+                       if (symbol_reference is ArrayResizeMethod) {
+                               if (inner.symbol_reference is Variable) {
+                                       // require the real type with its original value_owned attritubte
+                                       var inner_type = context.analyzer.get_value_type_for_symbol 
(inner.symbol_reference, true) as ArrayType;
+                                       if (inner_type != null && inner_type.inline_allocated) {
+                                               Report.error (source_reference, "`resize' is not supported 
for arrays with fixed length");
+                                               error = true;
+                                       } else if (inner_type != null && !inner_type.value_owned) {
+                                               Report.error (source_reference, "`resize' is not allowed for 
unowned array references");
+                                               error = true;
+                                       }
+                               } else if (inner.symbol_reference is Constant) {
+                                       // disallow resize() for const array
+                                       Report.error (source_reference, "`resize' is not allowed for constant 
arrays");
                                        error = true;
                                }
                        }


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