1
0
forked from aniani/vim

patch 9.0.0840: cannot change a slice of a const list

Problem:    Cannot change a slice of a const list. (Takumi KAGIYAMA)
Solution:   Remove the const flag from the slice type. (closes #11490)
This commit is contained in:
Bram Moolenaar 2022-11-06 18:27:17 +00:00
parent 69a8bb8dc1
commit adbc08fd69
3 changed files with 26 additions and 0 deletions

View 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

View File

@ -695,6 +695,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
840,
/**/
839,
/**/

View 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
{