diff options
author | wxx <769218589@qq.com> | 2021-08-03 23:07:49 +0800 |
---|---|---|
committer | Rico Tzschichholz <ricotz@ubuntu.com> | 2021-08-14 16:32:09 +0200 |
commit | a0e8e4b4bfd6e8c5e374f0bbf190de7afaa15cab (patch) | |
tree | cf167c432450a10bc24db257fccafa4af1cef35b | |
parent | be03c3cd323e4d28274f51404439d0e118f74b86 (diff) | |
download | vala-a0e8e4b4bfd6e8c5e374f0bbf190de7afaa15cab.tar.gz |
vala: Disallow resize() for constant arrays
See https://gitlab.gnome.org/GNOME/vala/issues/944
-rw-r--r-- | tests/Makefile.am | 1 | ||||
-rw-r--r-- | tests/arrays/resize-constant.test | 6 | ||||
-rw-r--r-- | vala/valamemberaccess.vala | 22 |
3 files changed, 21 insertions, 8 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am index f0ec0e9c4..3b0ca32b1 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -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 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 6806c55a6..ef4bc22d2 100644 --- a/vala/valamemberaccess.vala +++ b/vala/valamemberaccess.vala @@ -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; } } |