1
0
forked from aniani/vim

patch 8.2.3336: behavior of negative index in list change changed

Problem:    Behavior of negative index in list change changed. (Naruhiko
            Nishino)
Solution:   Only change it for Vim9 script. (closes #8749)
This commit is contained in:
Bram Moolenaar
2021-08-12 21:12:56 +02:00
parent ef98257593
commit 92f05f21af
3 changed files with 26 additions and 3 deletions

View File

@@ -1146,15 +1146,19 @@ list_slice_or_index(
n1 = len + n1; n1 = len + n1;
if (n1 < 0 || n1 >= len) if (n1 < 0 || n1 >= len)
{ {
// For a range we allow invalid values and return an empty // For a range we allow invalid values and for legacy script return an
// list. A list index out of range is an error. // empty list, for Vim9 script start at the first item.
// A list index out of range is an error.
if (!range) if (!range)
{ {
if (verbose) if (verbose)
semsg(_(e_listidx), (long)n1_arg); semsg(_(e_listidx), (long)n1_arg);
return FAIL; return FAIL;
} }
n1 = n1 < 0 ? 0 : len; if (in_vim9script())
n1 = n1 < 0 ? 0 : len;
else
n1 = len;
} }
if (range) if (range)
{ {

View File

@@ -42,6 +42,23 @@ func Test_list_slice()
let l[:1] += [1, 2] let l[:1] += [1, 2]
let l[2:] -= [1] let l[2:] -= [1]
call assert_equal([2, 4, 2], l) call assert_equal([2, 4, 2], l)
let lines =<< trim END
VAR l = [1, 2]
call assert_equal([1, 2], l[:])
call assert_equal([2], l[-1 : -1])
call assert_equal([1, 2], l[-2 : -1])
END
call CheckLegacyAndVim9Success(lines)
let l = [1, 2]
call assert_equal([], l[-3 : -1])
let lines =<< trim END
var l = [1, 2]
assert_equal([1, 2], l[-3 : -1])
END
call CheckDefAndScriptSuccess(lines)
endfunc endfunc
" List identity " List identity

View File

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