]> 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>
Mon, 16 Aug 2021 10:17:29 +0000 (12:17 +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 66a20f883b234d12b385a9b9eadab25dccf45348..7c04805a90221f111d168cb2a2d241e22bf5ef29 100644 (file)
@@ -129,6 +129,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 ae8079bd28bfb8bc79b68bdda601ab6b2f1ee326..9a9b8cf5ce60a1c568a2ad933b46eef4f86fa429 100644 (file)
@@ -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;
                                }
                        }