]> granicus.if.org Git - vim/commitdiff
patch 9.0.0840: cannot change a slice of a const list v9.0.0840
authorBram Moolenaar <Bram@vim.org>
Sun, 6 Nov 2022 18:27:17 +0000 (18:27 +0000)
committerBram Moolenaar <Bram@vim.org>
Sun, 6 Nov 2022 18:27:17 +0000 (18:27 +0000)
Problem:    Cannot change a slice of a const list. (Takumi KAGIYAMA)
Solution:   Remove the const flag from the slice type. (closes #11490)

src/testdir/test_vim9_expr.vim
src/version.c
src/vim9expr.c

index bc4a7902734ffbde1eb7f68a22c2bab87b49108e..5a178803e494ab0d0ce8a5699c09d564e46e35fb 100644 (file)
@@ -3132,6 +3132,18 @@ def Test_expr9_any_index_slice()
   unlet g:testlist
 enddef
 
+def s:GetList(): list<string>
+  return ['a', 'b', 'z']
+enddef
+
+def Test_slice_const_list()
+  const list = GetList()
+  final sliced = list[0 : 1]
+  # OK to change the list after slicing, it is a copy now
+  add(sliced, 'Z')
+  assert_equal(['a', 'b', 'Z'], sliced)
+enddef
+
 def Test_expr9_const_any_index_slice()
   var lines =<< trim END
       vim9script
index c454557cdebc126e7f23669f04df6ef9c1a81682..e7dc5a5b5c324e1b870c6389cfca8a841a7a1a96 100644 (file)
@@ -695,6 +695,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    840,
 /**/
     839,
 /**/
index 7a32974b691b70a919ef554756f0ce952ee623e6..7a089e973347e60d6efead9b23928aa76464a898 100644 (file)
@@ -185,6 +185,18 @@ compile_member(int is_slice, int *keeping_dict, cctx_T *cctx)
            // a copy is made so the member type is no longer declared
            if (typep->type_decl->tt_type == VAR_LIST)
                typep->type_decl = &t_list_any;
+
+           // a copy is made, the composite is no longer "const"
+           if (typep->type_curr->tt_flags & TTFLAG_CONST)
+           {
+               type_T *type = copy_type(typep->type_curr, cctx->ctx_type_list);
+
+               if (type != typep->type_curr)  // did get a copy
+               {
+                   type->tt_flags &= ~(TTFLAG_CONST | TTFLAG_STATIC);
+                   typep->type_curr = type;
+               }
+           }
        }
        else
        {