]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
vala: Disallow resize() for constant arrays
authorwxx <769218589@qq.com>
Tue, 3 Aug 2021 15:07:49 +0000 (23:07 +0800)
committerRico Tzschichholz <ricotz@ubuntu.com>
Sat, 14 Aug 2021 14:32:09 +0000 (16:32 +0200)
See https://gitlab.gnome.org/GNOME/vala/issues/944

tests/Makefile.am
tests/arrays/resize-constant.test [new file with mode: 0644]
vala/valamemberaccess.vala

index f0ec0e9c4aa665330214754d7172ad16b183e34a..3b0ca32b1d5dd13277231b91e964d34895a938f5 100644 (file)
@@ -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 (file)
index 0000000..277db85
--- /dev/null
@@ -0,0 +1,6 @@
+Invalid Code
+
+void main () {
+       const int[] foo = { 1, 2, 3 };
+       foo.resize (2);
+}
index 6806c55a62a290a43cb148575697ff786e8de548..ef4bc22d20a8d9f81d66e699980f3f2df17905ee 100644 (file)
@@ -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;
                                }
                        }