]> 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>
Sun, 22 Aug 2021 08:36:21 +0000 (10:36 +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 3d4914726235a2ffa217b1768c638381ac261b19..7b4c8f39b6e3d8ae7761b5a849ef9a58ec7c1f5a 100644 (file)
@@ -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 (file)
index 0000000..277db85
--- /dev/null
@@ -0,0 +1,6 @@
+Invalid Code
+
+void main () {
+       const int[] foo = { 1, 2, 3 };
+       foo.resize (2);
+}
index a5afbb444d3fcf50006fa6dd0f9ad6f66b870636..59c1f445a86e026a06de1d674df904e0c7a03cd5 100644 (file)
@@ -488,14 +488,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;
                                }
                        }