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



commit 30fd84dc866eefa7377063b94e7cf75a547a2c5b
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 71ebe31bd..0c7616da8 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -128,6 +128,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 f440580c0..c582be690 100644
--- a/vala/valamemberaccess.vala
+++ b/vala/valamemberaccess.vala
@@ -509,14 +509,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]