]> 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:27:55 +0000 (10:27 +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 71ebe31bdb76152ecf66db9657609f80da2ab611..0c7616da825a7b6395d09cba919e3596761aff62 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 f440580c018de0960b738080ccfe6b6e37f57a57..c582be690eccb3569aa1340cbba3fb750e14c985 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;
                                }
                        }